From 7cf367f120d6c6a2e7d918f177d823691d061ca7 Mon Sep 17 00:00:00 2001 From: Nick Shipp Date: Wed, 12 Apr 2017 02:56:22 -0400 Subject: Switch to dietlibc --- Makefile | 6 ++++-- README | 2 ++ sinit.c | 23 +++++++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index fbe3a30..f4e0a45 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,16 @@ include config.mk -CC = musl-gcc +CC = diet -Os gcc OBJ = sinit.o BIN = sinit -CFLAGS = -Os +CFLAGS = -Os -flto -s -fno-asynchronous-unwind-tables -fno-unwind-tables +LDFLAGS = -flto -Os -static -Wl,--gc-sections -Wl,--strip-all -Wl,-z,norelro -fno-asynchronous-unwind-tables -Wl,--build-id=none all: $(BIN) poweroff reboot $(BIN): $(OBJ) $(CC) $(LDFLAGS) -o $@ $(OBJ) $(LDLIBS) + strip -R .comment -R .jcr -R .eh_data -R .note -R .eh_frame $@ $(OBJ): config.h diff --git a/README b/README index a9e9dda..5f7e671 100644 --- a/README +++ b/README @@ -13,6 +13,8 @@ My fork of sinit adds: * Base system mounting defined in config.h * Static reboot and poweroff binaries which just send the signals listed below +When I build this with dietlibc, the resulting binary is currently 5272 bytes. + Why? ---- diff --git a/sinit.c b/sinit.c index 897c35a..6ae54c0 100644 --- a/sinit.c +++ b/sinit.c @@ -14,6 +14,7 @@ #include #include #include +#include #define LEN(x) (sizeof (x) / sizeof *(x)) @@ -44,15 +45,17 @@ main(void) int sig; size_t i; +#ifdef SANE if (getpid() != 1) return 1; + chdir("/"); +#endif + #ifdef PERF spawn(NULL, perf); #endif - chdir("/"); - sigfillset(&set); sigprocmask(SIG_BLOCK, &set, NULL); @@ -99,8 +102,10 @@ sigpoweroff(void) kill_wait(); sync(); reboot(RB_POWER_OFF); +#ifdef SANE /* only reachable on error */ write(2, "poweroff failed\n", 16); +#endif } static void @@ -117,8 +122,10 @@ sigreboot(void) kill_wait(); sync(); reboot(RB_AUTOBOOT); +#ifdef SANE /* only reachable on error */ - write(2, "reboot failed?\n", 15); + write(2, "reboot failed\n", 15); +#endif } static void @@ -139,10 +146,12 @@ spawn(int (*dep)(), char *const argv[]) } /* printf("spawn: %s\n", argv[0]); */ execvp(argv[0], argv); +#ifdef SANE write(2, "can't exec\n", 11); _exit(1); case -1: write(2, "can't fork\n", 11); +#endif } } @@ -160,17 +169,19 @@ spawn_as(uid_t uid, gid_t gid, char *const env[], char *const argv[]) setgroups(LEN(groups), groups); setuid(uid); execve(argv[0], argv, env); +#ifdef SANE write(2, "can't exec\n", 11); _exit(1); case -1: write(2, "can't fork\n", 11); +#endif } } static void mounts(void) { - int i; + size_t i; mkdir("/dev/pts", 0755); mkdir("/dev/shm", 0755); for (i=0;i < LEN(static_mounts);i++) { @@ -180,10 +191,12 @@ mounts(void) static_mounts[i].flags, static_mounts[i].data) != 0) { +#ifdef SANE write(2, "mount failed for ", 17); write(2, static_mounts[i].target, strlen(static_mounts[i].target)); write(2, "\n", 1); +#endif } } @@ -205,12 +218,14 @@ ifup(const char *iface) int sk; sk = socket(AF_INET, SOCK_DGRAM, 0); +#ifdef SANE if (sk < 0) { write(2, "ifup failed for ", 16); write(2, iface, strlen(iface)); write(2, "\n", 1); return; } +#endif memset(&ifr, 0, sizeof(struct ifreq)); strncpy(ifr.ifr_name, iface, IFNAMSIZ); -- cgit v1.2.3-70-g09d2