preinit: print errno (in hex, it's easier) for failures

This commit is contained in:
Daniel Barlow 2023-11-04 23:58:33 +00:00
parent 6db982f25f
commit c3ccee6506
2 changed files with 48 additions and 10 deletions

View file

@ -1,3 +1,5 @@
#include <unistd.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -12,6 +14,33 @@ static int begins_with(char * str, char * prefix)
return 1; return 1;
} }
char * pr_u32(int32_t input) {
static char buf[9];
const char *digits = "0123456789abcdef";
int i=0;
buf[i] = digits[(input & 0xf0000000) >> 28];
buf[i+1] = digits[(input & 0x0f000000) >> 24];
if(buf[i] != '0' || buf[i+1] != '0') i+=2;
buf[i] = digits[(input & 0x00f00000) >> 20];
buf[i+1] = digits[(input & 0x000f0000) >> 16];
if(buf[i] != '0' || buf[i+1] != '0') i+=2;
buf[i] = digits[(input & 0x0000f000) >> 12];
buf[i+1] = digits[(input & 0x00000f00) >> 8];
if(buf[i] != '0' || buf[i+1] != '0') i+=2;
buf[i] = digits[(input & 0x000000f0) >> 4];
buf[i+1] = digits[(input & 0x0000000f)];
i+=2;
buf[i] ='\0';
write(2, buf, i);
return buf;
}
void parseopts(char * cmdline, char **root, char **rootfstype) { void parseopts(char * cmdline, char **root, char **rootfstype) {
*root = 0; *root = 0;
@ -21,7 +50,7 @@ void parseopts(char * cmdline, char **root, char **rootfstype) {
if(begins_with(p, "root=")) { if(begins_with(p, "root=")) {
*root = p + strlen("root="); *root = p + strlen("root=");
while(*p && (*p != ' ')) p++; while(*p && (*p != ' ')) p++;
if(*p) { if(*p) {
*p = '\0'; *p = '\0';
p++; p++;
@ -43,6 +72,8 @@ void parseopts(char * cmdline, char **root, char **rootfstype) {
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// cc -DTESTS -o parseopts parseopts.c && ./parseopts
#define die(fmt, ...) do { printf(fmt, __VA_ARGS__); exit(1); } while(0) #define die(fmt, ...) do { printf(fmt, __VA_ARGS__); exit(1); } while(0)
#define S(x) #x #define S(x) #x
#define expect_equal(actual, expected) \ #define expect_equal(actual, expected) \
@ -91,7 +122,11 @@ int main()
if(strlen(rootfstype)) die("expected empty rootfstype, got \"%s\"", rootfstype); if(strlen(rootfstype)) die("expected empty rootfstype, got \"%s\"", rootfstype);
if(strlen(root)) die("expected null root, got \"%s\"", root); if(strlen(root)) die("expected null root, got \"%s\"", root);
expect_equal("01", pr_u32(1));
expect_equal("ab", pr_u32(0xab));
expect_equal("0abc", pr_u32(0xabc));
expect_equal("aabc", pr_u32(0xaabc));
expect_equal("deadcafe", pr_u32(0xdeadcafe));
} }
#endif #endif

View file

@ -13,7 +13,9 @@
void parseopts(char * cmdline, char **root, char **rootfstype); void parseopts(char * cmdline, char **root, char **rootfstype);
#define ERR(x) write(2, x, strlen(x)) #define ERR(x) write(2, x, strlen(x))
#define AVER(c) do { if(c < 0) ERR("failed: " #c); } while(0) #define AVER(c) do { if(c < 0) { ERR("failed: " #c ": error=0x" ); pr_u32(errno); ERR("\n"); } } while(0)
char * pr_u32(int32_t input);
static void die() { static void die() {
/* if init exits, it causes a kernel panic. On the Turris /* if init exits, it causes a kernel panic. On the Turris
@ -52,7 +54,7 @@ int main(int argc, char *argv[], char *envp[])
write(1, banner, strlen(banner)); write(1, banner, strlen(banner));
mount("none", "/proc", "proc", 0, NULL); AVER(mount("none", "/proc", "proc", 0, NULL));
int cmdline = open("/proc/cmdline", O_RDONLY, 0); int cmdline = open("/proc/cmdline", O_RDONLY, 0);
@ -66,18 +68,19 @@ int main(int argc, char *argv[], char *envp[])
write(1, "cmdline: \"", 10); write(1, "cmdline: \"", 10);
write(1, buf, len); write(1, buf, len);
write(1, "\"\n", 2); write(1, "\"\n", 2);
}; } else {
ERR("failed: open(\"/proc/cmdline\")\n");
die();
}
parseopts(buf, &rootdevice, &rootfstype); parseopts(buf, &rootdevice, &rootfstype);
if(rootdevice) { if(rootdevice) {
if(!rootfstype) rootfstype = "jffs2"; /* backward compatibility */ if(!rootfstype) rootfstype = "jffs2"; /* backward compatibility */
write(1, "rootdevice ", 11); write(1, "rootdevice ", 11);
write(1, rootdevice, strlen(rootdevice)); write(1, rootdevice, strlen(rootdevice));
write(1, " (", 2); write(1, " (", 2);
write(1, rootfstype, strlen(rootfstype)); write(1, rootfstype, strlen(rootfstype));
write(1, ")\n", 1); write(1, ")\n", 2);
AVER(mount(rootdevice, "/target/persist", rootfstype, 0, NULL)); AVER(mount(rootdevice, "/target/persist", rootfstype, 0, NULL));
AVER(mount("/target/persist/nix", "/target/nix", AVER(mount("/target/persist/nix", "/target/nix",
"bind", MS_BIND, NULL)); "bind", MS_BIND, NULL));
@ -87,7 +90,7 @@ int main(int argc, char *argv[], char *envp[])
AVER(chdir("/target")); AVER(chdir("/target"));
AVER(mount("/target", "/", "bind", MS_BIND | MS_REC, NULL)); AVER(mount("/target", "/", "bind", MS_BIND | MS_REC, NULL));
AVER(chroot(".")); AVER(chroot("."));
argv[0] = "init"; argv[0] = "init";
argv[1] = NULL; argv[1] = NULL;