commit a72d3ccba330a3a9075a06601d6a12e5360a1072
parent f8dd9a074545f4aebc2bf60ad86aff5e5c86fcee
Author: Friedel Schön <[email protected]>
Date: Mon, 13 May 2024 16:09:36 +0200
formatting
Diffstat:
A | .clang-format | | | 46 | ++++++++++++++++++++++++++++++++++++++++++++++ |
M | ftrigger.c | | | 293 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
M | governor.c | | | 158 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
M | hard-reboot.c | | | 49 | ++++++++++++++++++++++++------------------------- |
M | killall5.c | | | 68 | ++++++++++++++++++++++++++++++++++++-------------------------------- |
M | minit-update.c | | | 376 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
M | minit.c | | | 1149 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
M | minit.h | | | 24 | ++++++++++++------------ |
M | msvc.c | | | 632 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
M | opendevconsole.c | | | 25 | +++++++++++++------------ |
M | openreadclose.c | | | 48 | ++++++++++++++++++++++++------------------------ |
M | pidfilehack.c | | | 96 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
M | powersave.c | | | 123 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
M | serdo.c | | | 459 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
M | shutdown.c | | | 460 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
M | split.c | | | 69 | +++++++++++++++++++++++++++++++++++---------------------------------- |
M | t.c | | | 6 | +++--- |
M | waitinterface.c | | | 92 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
M | waitport.c | | | 273 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
M | write_proc.c | | | 20 | ++++++++++---------- |
20 files changed, 2315 insertions(+), 2151 deletions(-)
diff --git a/.clang-format b/.clang-format
@@ -0,0 +1,46 @@
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: true
+AlignConsecutiveDeclarations: true
+AlignConsecutiveMacros: true
+AlignEscapedNewlines: true
+AlignOperands: AlignAfterOperator
+AllowShortBlocksOnASingleLine: true
+AllowShortIfStatementsOnASingleLine: true
+AllowShortLoopsOnASingleLine: true
+BreakBeforeBraces: Attach
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializers: BeforeComma
+BreakInheritanceList: BeforeComma
+BreakStringLiterals: false
+ColumnLimit: 0
+Cpp11BracedListStyle: false
+FixNamespaceComments: true
+IncludeBlocks: Regroup
+IndentCaseLabels: true
+IndentPPDirectives: AfterHash
+IndentWidth: 4
+MaxEmptyLinesToKeep: 2
+NamespaceIndentation: All
+PointerAlignment: Left
+SortIncludes: true
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: true
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceBeforeSquareBrackets: false
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 4
+SpacesInAngles: false
+SpacesInCStyleCastParentheses: false
+SpacesInConditionalStatement: false
+SpacesInContainerLiterals: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+TabWidth: 4
+UseTab: ForIndentation
diff --git a/ftrigger.c b/ftrigger.c
@@ -1,121 +1,121 @@
-#include <stdio.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <sys/stat.h>
+#include <errno.h>
#include <fcntl.h>
-#include <sys/inotify.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <errno.h>
-#include <poll.h>
+#include <sys/inotify.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <sys/wait.h>
-#include <signal.h>
+#include <unistd.h>
struct trigger {
- const char* filename,* command;
- char* dironly,* fileonly;
- struct stat ss;
- int idd,idf,created; /* inotify-deskriptor */
- /* created = 1: got a create event on the dir, added a watch on the file, but no modify events on the file yet */
-}* root;
+ const char *filename, *command;
+ char * dironly, *fileonly;
+ struct stat ss;
+ int idd, idf, created; /* inotify-deskriptor */
+ /* created = 1: got a create event on the dir, added a watch on the file, but no modify events on the file yet */
+}* root;
int n;
void sighandler(int signum) {
- int status;
- (void)signum;
- wait(&status);
+ int status;
+ (void) signum;
+ wait(&status);
}
int v;
-int main(int argc,char* argv[]) {
- int i,in;
- const char* command="make";
- struct stat ss;
- char buf[2048];
- struct inotify_event* ie=(struct inotify_event*)buf;
- struct pollfd p;
- static struct sigaction sa;
+int main(int argc, char* argv[]) {
+ int i, in;
+ const char* command = "make";
+ struct stat ss;
+ char buf[2048];
+ struct inotify_event* ie = (struct inotify_event*) buf;
+ struct pollfd p;
+ static struct sigaction sa;
- sa.sa_flags=SA_RESTART|SA_NOCLDSTOP;
- sa.sa_handler=SIG_IGN;
- sigaction(SIGCHLD,&sa,0);
- in=inotify_init();
- root=(struct trigger*)alloca(sizeof(struct trigger)*argc);
- memset(root,0,sizeof(struct trigger)*argc);
- for (i=1; i<argc; ++i) {
- if (!strcmp(argv[i],"-v")) {
- v=1;
- continue;
- }
- if (argv[i][0]=='@') {
- command=argv[i]+1;
- ++i;
- }
- if (argv[i]) {
- char* c;
- root[n].filename=argv[i];
- root[n].command=command;
- root[n].dironly=alloca(strlen(root[n].filename)+3);
- c=strrchr(root[n].filename,'/');
- if (c) {
- size_t m=c-root[n].filename;
- strcpy(root[n].dironly,root[n].filename);
- root[n].fileonly=root[n].dironly+m+1;
- root[n].fileonly[-1]=0;
- } else {
- root[n].dironly[0]='.';
- root[n].dironly[1]=0;
- root[n].fileonly=root[n].dironly+2;
- strcpy(root[n].fileonly,root[n].filename);
- }
- ++n;
- }
- }
+ sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
+ sa.sa_handler = SIG_IGN;
+ sigaction(SIGCHLD, &sa, 0);
+ in = inotify_init();
+ root = (struct trigger*) alloca(sizeof(struct trigger) * argc);
+ memset(root, 0, sizeof(struct trigger) * argc);
+ for (i = 1; i < argc; ++i) {
+ if (!strcmp(argv[i], "-v")) {
+ v = 1;
+ continue;
+ }
+ if (argv[i][0] == '@') {
+ command = argv[i] + 1;
+ ++i;
+ }
+ if (argv[i]) {
+ char* c;
+ root[n].filename = argv[i];
+ root[n].command = command;
+ root[n].dironly = alloca(strlen(root[n].filename) + 3);
+ c = strrchr(root[n].filename, '/');
+ if (c) {
+ size_t m = c - root[n].filename;
+ strcpy(root[n].dironly, root[n].filename);
+ root[n].fileonly = root[n].dironly + m + 1;
+ root[n].fileonly[-1] = 0;
+ } else {
+ root[n].dironly[0] = '.';
+ root[n].dironly[1] = 0;
+ root[n].fileonly = root[n].dironly + 2;
+ strcpy(root[n].fileonly, root[n].filename);
+ }
+ ++n;
+ }
+ }
- for (i=0; i<n; ++i) {
- root[i].idf=-1;
- if (stat(root[i].filename,&root[i].ss)!=0)
- fprintf(stderr, "warning: could not stat file \"%s\": %s\n", root[i].filename, strerror(errno));
- else
- root[i].idf=inotify_add_watch(in,root[i].filename,
- IN_CLOSE_WRITE|IN_MOVE_SELF|IN_DELETE_SELF|IN_MODIFY);
- root[i].idd=inotify_add_watch(in,root[i].dironly,
- IN_MOVED_TO|IN_CREATE);
- root[i].created=0;
- }
+ for (i = 0; i < n; ++i) {
+ root[i].idf = -1;
+ if (stat(root[i].filename, &root[i].ss) != 0)
+ fprintf(stderr, "warning: could not stat file \"%s\": %s\n", root[i].filename, strerror(errno));
+ else
+ root[i].idf = inotify_add_watch(in, root[i].filename,
+ IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF | IN_MODIFY);
+ root[i].idd = inotify_add_watch(in, root[i].dironly,
+ IN_MOVED_TO | IN_CREATE);
+ root[i].created = 0;
+ }
- p.fd=in;
- p.events=POLLIN;
+ p.fd = in;
+ p.events = POLLIN;
- for (;;) {
-again:
- switch (poll(&p,1,1000)) {
- case -1:
- if (errno==EINTR) {
- int status;
- wait(&status);
- break;
- }
- return 1;
- case 0:
- {
- int foundone=0;
- for (i=0; i<n; ++i)
- if (root[i].created==1) {
- memset(&root[i].ss,0,sizeof(root[i].ss));
- root[i].created=0;
- foundone=1;
- }
- if (foundone) goto goodevent;
- }
- continue;
- case 1: break;
- };
- if (read(in,buf,sizeof(buf)) < (ssize_t)sizeof(*ie)) {
- fprintf(stderr, "error reading inotify notification: %s\n", strerror(errno));
- exit(111);
- }
+ for (;;) {
+ again:
+ switch (poll(&p, 1, 1000)) {
+ case -1:
+ if (errno == EINTR) {
+ int status;
+ wait(&status);
+ break;
+ }
+ return 1;
+ case 0: {
+ int foundone = 0;
+ for (i = 0; i < n; ++i)
+ if (root[i].created == 1) {
+ memset(&root[i].ss, 0, sizeof(root[i].ss));
+ root[i].created = 0;
+ foundone = 1;
+ }
+ if (foundone) goto goodevent;
+ }
+ continue;
+ case 1:
+ break;
+ };
+ if (read(in, buf, sizeof(buf)) < (ssize_t) sizeof(*ie)) {
+ fprintf(stderr, "error reading inotify notification: %s\n", strerror(errno));
+ exit(111);
+ }
#if 0
buffer_puts(buffer_1,"got event for wd ");
@@ -153,72 +153,73 @@ again:
#endif
- for (i=0; i<n; ++i) {
- if (root[i].idf==ie->wd) {
- if (ie->mask & IN_MODIFY) {
- root[i].created=0;
- root[i].idf=inotify_add_watch(in,root[i].filename,
- IN_CLOSE_WRITE|IN_MOVE_SELF|IN_DELETE_SELF);
- continue;
- }
- if (ie->mask & (IN_DELETE_SELF|IN_MOVE_SELF)) {
+ for (i = 0; i < n; ++i) {
+ if (root[i].idf == ie->wd) {
+ if (ie->mask & IN_MODIFY) {
+ root[i].created = 0;
+ root[i].idf = inotify_add_watch(in, root[i].filename,
+ IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF);
+ continue;
+ }
+ if (ie->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
#if 0
buffer_putmflush(buffer_1,root[i].filename," was ",
ie->mask&IN_DELETE_SELF ? "deleted" : "renamed",
", cancelling subscription\n");
#endif
- inotify_rm_watch(in,ie->wd);
- root[i].idf=-1;
- goto again;
- }
- goto goodevent;
- } else if (root[i].idd==ie->wd) {
- if (!strcmp(ie->name,root[i].fileonly)) {
+ inotify_rm_watch(in, ie->wd);
+ root[i].idf = -1;
+ goto again;
+ }
+ goto goodevent;
+ } else if (root[i].idd == ie->wd) {
+ if (!strcmp(ie->name, root[i].fileonly)) {
#if 0
buffer_putmflush(buffer_1,"the file we were interested in, ",
root[i].filename,", has been created. Adding subscription.\n");
#endif
- if (root[i].idf!=-1)
- inotify_rm_watch(in,root[i].idf);
- root[i].idf=inotify_add_watch(in,root[i].filename,
- IN_CLOSE_WRITE|IN_MOVE_SELF|IN_DELETE_SELF|IN_MODIFY);
- root[i].created=1;
- /* if the file was created, it will be empty now, wait for
- * the IN_CLOSE_WRITE event. */
- if (ie->mask & IN_CREATE) {
+ if (root[i].idf != -1)
+ inotify_rm_watch(in, root[i].idf);
+ root[i].idf = inotify_add_watch(in, root[i].filename,
+ IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF | IN_MODIFY);
+ root[i].created = 1;
+ /* if the file was created, it will be empty now, wait for
+ * the IN_CLOSE_WRITE event. */
+ if (ie->mask & IN_CREATE) {
#if 0
buffer_putmflush(buffer_1,"file was created, so it will be empty now. Skipping until we get a close event.\n");
#endif
- goto again;
- }
- goto goodevent;
- /* if the file was moved, this is a genuine event we are
- * interested in. fall through */
- } else goto again;
+ goto again;
+ }
+ goto goodevent;
+ /* if the file was moved, this is a genuine event we are
+ * interested in. fall through */
+ } else
+ goto again;
#if 0
buffer_putmflush(buffer_1,"(directory event for \"",root[i].filename,"\")\n");
#endif
- }
- }
+ }
+ }
#if 0
buffer_putmflush(buffer_1,"ignoring irrelevant event.\n");
#endif
- goto again;
+ goto again;
-goodevent:
+ goodevent:
#if 0
buffer_putmflush(buffer_1,"got through to stat\n");
#endif
- for (i=0; i<n; ++i) {
- if (stat(root[i].filename,&ss)==0 && memcmp(&ss,&root[i].ss,sizeof(ss))) {
- memcpy(&root[i].ss,&ss,sizeof(ss));
- if (v)
- fprintf(stderr, "file \"%s\" changed, running command \"%s\"\n",root[i].filename,root[i].command);
- if (vfork()==0) {
- exit(system(root[i].command));
+ for (i = 0; i < n; ++i) {
+ if (stat(root[i].filename, &ss) == 0 && memcmp(&ss, &root[i].ss, sizeof(ss))) {
+ memcpy(&root[i].ss, &ss, sizeof(ss));
+ if (v)
+ fprintf(stderr, "file \"%s\" changed, running command \"%s\"\n", root[i].filename, root[i].command);
+ if (vfork() == 0) {
+ exit(system(root[i].command));
+ }
+ }
+ }
}
- }
- }
- }
- return 0;
+ return 0;
}
diff --git a/governor.c b/governor.c
@@ -1,84 +1,88 @@
-#include <unistd.h>
-#include <string.h>
#include <fcntl.h>
-
#include <libowfat/errmsg.h>
+#include <string.h>
+#include <unistd.h>
-const char acstate[]="/proc/acpi/ac_adapter/AC/state";
-const char sysacstate[]="/sys/class/power_supply/AC/online";
-char governor[]="/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor";
-const char powersave[]="powersave";
-const char performance[]="performance";
-const char ondemand[]="conservative";
+const char acstate[] = "/proc/acpi/ac_adapter/AC/state";
+const char sysacstate[] = "/sys/class/power_supply/AC/online";
+char governor[] = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor";
+const char powersave[] = "powersave";
+const char performance[] = "performance";
+const char ondemand[] = "conservative";
-int main(int argc,char* argv[]) {
- char buf[1024];
- int fd,r;
- const char* s;
- errmsg_iam("switch_governor");
+int main(int argc, char* argv[]) {
+ char buf[1024];
+ int fd, r;
+ const char* s;
+ errmsg_iam("switch_governor");
- if (argc<1)
- s="governor";
- else if ((s=strrchr(argv[0],'/')))
- ++s;
- else
- s=argv[0];
- if (!strcmp(s,"slow")) {
- s=powersave; goto writeit;
- } else if (!strcmp(s,"fast")) {
- s=performance; goto writeit;
- } else if (!strcmp(s,"auto")) {
- s=ondemand; goto writeit;
- }
+ if (argc < 1)
+ s = "governor";
+ else if ((s = strrchr(argv[0], '/')))
+ ++s;
+ else
+ s = argv[0];
+ if (!strcmp(s, "slow")) {
+ s = powersave;
+ goto writeit;
+ } else if (!strcmp(s, "fast")) {
+ s = performance;
+ goto writeit;
+ } else if (!strcmp(s, "auto")) {
+ s = ondemand;
+ goto writeit;
+ }
- /* first try /sys, if that fails attempt /proc */
- if ((fd=open(sysacstate,O_RDONLY))==-1) {
- if ((fd=open(acstate,O_RDONLY))==-1)
- diesys(1,"could not open ",acstate);
- if ((r=read(fd,buf,sizeof(buf)))<1)
- diesys(1,"read error");
- if (r>1000)
- die(1,"too much data in ",acstate);
- buf[r]=0;
- close(fd);
- if (strstr(buf,"off-line"))
- s=powersave;
- else if (strstr(buf,"on-line"))
- s=ondemand;
- else
- die(1,"ac adapter neither on-line nor off-line!?");
- } else {
- if ((r=read(fd,buf,sizeof(buf)))<1)
- diesys(1,"read error");
- if (r>2)
- die(1,"too much data in ",sysacstate);
- buf[r]=0;
- close(fd);
- if (buf[0]=='0')
- s=powersave;
- else if (buf[0]=='1')
- s=ondemand;
- else
- die(1,"ac adapter neither on-line nor off-line!?");
- }
-writeit:
- {
- int i;
- char* x=strstr(governor,"cpu0");
- if (x) x+=3;
- for (i=0; i<32; ++i) {
- if ((fd=open(governor,O_WRONLY))==-1) {
- if (x && *x=='0')
- diesys(1,"could not open ",governor);
- break;
- }
- r=strlen(s);
- if (write(fd,s,r)!=r)
- diesys(1,"write error");
- close(fd);
- if (x) ++(*x); else break;
- }
- }
- carp("switched cpufreq governor to ",s);
- return 0;
+ /* first try /sys, if that fails attempt /proc */
+ if ((fd = open(sysacstate, O_RDONLY)) == -1) {
+ if ((fd = open(acstate, O_RDONLY)) == -1)
+ diesys(1, "could not open ", acstate);
+ if ((r = read(fd, buf, sizeof(buf))) < 1)
+ diesys(1, "read error");
+ if (r > 1000)
+ die(1, "too much data in ", acstate);
+ buf[r] = 0;
+ close(fd);
+ if (strstr(buf, "off-line"))
+ s = powersave;
+ else if (strstr(buf, "on-line"))
+ s = ondemand;
+ else
+ die(1, "ac adapter neither on-line nor off-line!?");
+ } else {
+ if ((r = read(fd, buf, sizeof(buf))) < 1)
+ diesys(1, "read error");
+ if (r > 2)
+ die(1, "too much data in ", sysacstate);
+ buf[r] = 0;
+ close(fd);
+ if (buf[0] == '0')
+ s = powersave;
+ else if (buf[0] == '1')
+ s = ondemand;
+ else
+ die(1, "ac adapter neither on-line nor off-line!?");
+ }
+writeit: {
+ int i;
+ char* x = strstr(governor, "cpu0");
+ if (x) x += 3;
+ for (i = 0; i < 32; ++i) {
+ if ((fd = open(governor, O_WRONLY)) == -1) {
+ if (x && *x == '0')
+ diesys(1, "could not open ", governor);
+ break;
+ }
+ r = strlen(s);
+ if (write(fd, s, r) != r)
+ diesys(1, "write error");
+ close(fd);
+ if (x)
+ ++(*x);
+ else
+ break;
+ }
+}
+ carp("switched cpufreq governor to ", s);
+ return 0;
}
diff --git a/hard-reboot.c b/hard-reboot.c
@@ -1,36 +1,35 @@
-#include <unistd.h>
-#include <sys/reboot.h>
-#include <stdlib.h>
-
#include <libowfat/str.h>
+#include <stdlib.h>
#include <string.h>
+#include <sys/reboot.h>
+#include <unistd.h>
#define ABORTMSG "hard-reboot: Aborted.\n"
-#define USAGE "Say \"hard-reboot (RESTART|HALT|POWER_OFF)\" if you really mean it.\n"
+#define USAGE "Say \"hard-reboot (RESTART|HALT|POWER_OFF)\" if you really mean it.\n"
void usage(void) {
- write(2, ABORTMSG, strlen(ABORTMSG));
- write(2, USAGE, strlen(USAGE));
- exit(1);
+ write(2, ABORTMSG, strlen(ABORTMSG));
+ write(2, USAGE, strlen(USAGE));
+ exit(1);
}
-int main(int argc, char *argv[]) {
- if (argc!=2)
- usage();
+int main(int argc, char* argv[]) {
+ if (argc != 2)
+ usage();
- sync();
- sync();
- sync();
- if (strcmp(argv[1], "RESTART")==0) {
- reboot(RB_AUTOBOOT);
- } else if (strcmp(argv[1], "HALT")==0) {
- reboot(RB_HALT_SYSTEM);
- } else if (strcmp(argv[1], "POWER_OFF")==0) {
- reboot(RB_POWER_OFF);
- } else {
- usage();
- }
+ sync();
+ sync();
+ sync();
+ if (strcmp(argv[1], "RESTART") == 0) {
+ reboot(RB_AUTOBOOT);
+ } else if (strcmp(argv[1], "HALT") == 0) {
+ reboot(RB_HALT_SYSTEM);
+ } else if (strcmp(argv[1], "POWER_OFF") == 0) {
+ reboot(RB_POWER_OFF);
+ } else {
+ usage();
+ }
- while(1)
- sleep(10);
+ while (1)
+ sleep(10);
}
diff --git a/killall5.c b/killall5.c
@@ -19,50 +19,54 @@ as published by the Free Software Foundation; either version
#include <signal.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include <sys/types.h>
+#include <unistd.h>
-#define USAGE "Usage: killall5 SIGNAL\n"
+#define USAGE "Usage: killall5 SIGNAL\n"
#define NOPROC "No processes found - /proc not mounted?\n"
-int main(int argc, char **argv)
-{
- struct dirent *dir;
- DIR *dirstream;
- register pid_t pid, sid, mypid, mysid;
- int signal=-1;
- unsigned int sig_sent=0;
+int main(int argc, char** argv) {
+ struct dirent* dir;
+ DIR* dirstream;
+ register pid_t pid, sid, mypid, mysid;
+ int signal = -1;
+ unsigned int sig_sent = 0;
- if (argc == 2) {
- if (argv[1][0] == '-') argv[1]++;
- signal=atoi(argv[1]);
- }
+ if (argc == 2) {
+ if (argv[1][0] == '-') argv[1]++;
+ signal = atoi(argv[1]);
+ }
- if ( (signal < 1) || ( signal > 31) ) { write(2,USAGE,sizeof USAGE - 1); return 1; }
+ if ((signal < 1) || (signal > 31)) {
+ write(2, USAGE, sizeof USAGE - 1);
+ return 1;
+ }
- kill(-1,SIGSTOP);
+ kill(-1, SIGSTOP);
- if ( (dirstream=opendir("/proc"))) {
+ if ((dirstream = opendir("/proc"))) {
- mypid=getpid();
- mysid=getsid(0);
+ mypid = getpid();
+ mysid = getsid(0);
- while ( (dir=readdir(dirstream))){
- pid=atoi(dir->d_name);
+ while ((dir = readdir(dirstream))) {
+ pid = atoi(dir->d_name);
- if (pid > 1 ){
- sig_sent=1;
- sid=getsid(pid);
- if ( (pid != mypid) &&
- ( sid !=mysid)) kill(pid,signal);
- }
- }
- }
+ if (pid > 1) {
+ sig_sent = 1;
+ sid = getsid(pid);
+ if ((pid != mypid) &&
+ (sid != mysid)) kill(pid, signal);
+ }
+ }
+ }
- kill(-1,SIGCONT);
- if (!sig_sent) { write(2,NOPROC, sizeof NOPROC -1); return 1; }
+ kill(-1, SIGCONT);
+ if (!sig_sent) {
+ write(2, NOPROC, sizeof NOPROC - 1);
+ return 1;
+ }
-return 0;
+ return 0;
}
-
diff --git a/minit-update.c b/minit-update.c
@@ -1,111 +1,110 @@
-#include <errno.h>
+#include "minit.h"
+
#include <dirent.h>
+#include <errno.h>
#include <fcntl.h>
+#include <libowfat/buffer.h>
+#include <libowfat/str.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/file.h>
-#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <libowfat/str.h>
-#include <libowfat/buffer.h>
-
-#include "minit.h"
-
-#define USAGE "Usage: minit-update [ -v [ -u ] ]\n"
+#define USAGE "Usage: minit-update [ -v [ -u ] ]\n"
#define BUFLEN 1500
/*
increases file size by almost 4k
#define WITH_STRERROR */
-static char buf[BUFLEN+1];
+static char buf[BUFLEN + 1];
static unsigned int verbose;
-static int do_update;
+static int do_update;
-void feed_struct_to_minit(struct process *data);
+void feed_struct_to_minit(struct process* data);
-ssize_t read_outfd(void *buf, size_t count);
+ssize_t read_outfd(void* buf, size_t count);
-void addprocess(struct process *p);
+void addprocess(struct process* p);
-void die(const char *msg) {
- buffer_putsflush(buffer_2, msg);
- _exit(111);
+void die(const char* msg) {
+ buffer_putsflush(buffer_2, msg);
+ _exit(111);
}
-void buffer_putsnlflush(buffer *b, const char *msg) {
- buffer_puts(b, msg);
- buffer_putflush(b, "\n",1);
+void buffer_putsnlflush(buffer* b, const char* msg) {
+ buffer_puts(b, msg);
+ buffer_putflush(b, "\n", 1);
}
#ifdef WITH_STRERROR
-void buffer_puts_strerror(const char *msg) {
- buffer_puts(buffer_2, "minit-update: ");
- buffer_puts(buffer_2, msg);
- buffer_putsnlflush(buffer_2, strerror(errno));
+void buffer_puts_strerror(const char* msg) {
+ buffer_puts(buffer_2, "minit-update: ");
+ buffer_puts(buffer_2, msg);
+ buffer_putsnlflush(buffer_2, strerror(errno));
}
#else
-#define buffer_puts_strerror(a) buffer_putsflush(buffer_2, a)
-#endif
+# define buffer_puts_strerror(a) buffer_putsflush(buffer_2, a)
+#endif
-void *xmalloc(size_t size) {
- void *ptr=malloc(size);
- if (!ptr) die("malloc() failed\n");
- return ptr;
+void* xmalloc(size_t size) {
+ void* ptr = malloc(size);
+ if (!ptr) die("malloc() failed\n");
+ return ptr;
}
-void copywrite (const char *service) {
- strncpy(buf+1,service,BUFLEN);
- buf[BUFLEN]=0;
- write(infd,buf,str_len(buf));
+void copywrite(const char* service) {
+ strncpy(buf + 1, service, BUFLEN);
+ buf[BUFLEN] = 0;
+ write(infd, buf, str_len(buf));
}
int read_reply_from_minit(void) {
- if (read_outfd(buf,BUFLEN)==1) {
- if (buf[0]=='1') return 1;
- if (buf[0]=='0') buffer_puts(buffer_2,"expected '1' from minit, got '0' - minit too old?\n");
- }
- /* XXX: Uuuh. Should this be checked?
- else buffer_putsflush(buffer_2, "minit response not understood\n");
- */
-return 0;
+ if (read_outfd(buf, BUFLEN) == 1) {
+ if (buf[0] == '1') return 1;
+ if (buf[0] == '0') buffer_puts(buffer_2, "expected '1' from minit, got '0' - minit too old?\n");
+ }
+ /* XXX: Uuuh. Should this be checked?
+ else buffer_putsflush(buffer_2, "minit response not understood\n");
+ */
+ return 0;
}
-void find_service(int subdir, char *name, char *parent) {
- struct stat statbuf;
- char *service=0;
- DIR *dirstream=0;
- struct dirent *dir;
-
- if (chdir(name)) return;
-
- if (parent) {
- service=xmalloc(str_len(parent) + str_len(name) + 2 );
- strcpy(service, parent);
- strcat(service, "/");
- strcat(service, name);
- } else {
- if (subdir) {
- service=xmalloc(str_len(name)+1);
- strcpy(service, name);
- }
- }
+void find_service(int subdir, char* name, char* parent) {
+ struct stat statbuf;
+ char* service = 0;
+ DIR* dirstream = 0;
+ struct dirent* dir;
+
+ if (chdir(name)) return;
+
+ if (parent) {
+ service = xmalloc(str_len(parent) + str_len(name) + 2);
+ strcpy(service, parent);
+ strcat(service, "/");
+ strcat(service, name);
+ } else {
+ if (subdir) {
+ service = xmalloc(str_len(name) + 1);
+ strcpy(service, name);
+ }
+ }
#if 0
buffer_putsnlflush(buffer_1,service);
#endif
- if (service) { /* request and read a "struct process" from minit */
- struct process tmp;
+ if (service) { /* request and read a "struct process" from minit */
+ struct process tmp;
#if 0
int j;
for (j=0; j<=maxprocess; ++j) { /* skip duplicates */
@@ -113,142 +112,147 @@ void find_service(int subdir, char *name, char *parent) {
}
#endif
- if (verbose) {
- buffer_puts(buffer_1, "minit-update: status for ");
- buffer_puts(buffer_1, service);
- }
-
- buf[0]='D';
- copywrite(service);
-
- switch (read_outfd(&tmp,sizeof(tmp))) {
- case sizeof(tmp):
- tmp.name=strdup(service);
- addprocess(&tmp);
- if (verbose) buffer_puts(buffer_1, " saved.\n");
- break;
- case 1:
- if (verbose) buffer_puts(buffer_1, " failed - minit has no information on this service\n");
- #if 0
+ if (verbose) {
+ buffer_puts(buffer_1, "minit-update: status for ");
+ buffer_puts(buffer_1, service);
+ }
+
+ buf[0] = 'D';
+ copywrite(service);
+
+ switch (read_outfd(&tmp, sizeof(tmp))) {
+ case sizeof(tmp):
+ tmp.name = strdup(service);
+ addprocess(&tmp);
+ if (verbose) buffer_puts(buffer_1, " saved.\n");
+ break;
+ case 1:
+ if (verbose) buffer_puts(buffer_1, " failed - minit has no information on this service\n");
+#if 0
break;
default:
buffer_puts(buffer_1, " failed - read incomplete structure!\n");
- #endif
- }
- }
-
-dirstream=opendir(".");
-if (!dirstream) goto ret;
-
-while ( (dir=readdir(dirstream))){
- if (dir->d_name[0]!='.') {
- if(!lstat(dir->d_name, &statbuf)) {
- if (S_ISDIR(statbuf.st_mode)) {
- find_service(1, dir->d_name, service);
+#endif
+ }
+ }
+
+ dirstream = opendir(".");
+ if (!dirstream) goto ret;
+
+ while ((dir = readdir(dirstream))) {
+ if (dir->d_name[0] != '.') {
+ if (!lstat(dir->d_name, &statbuf)) {
+ if (S_ISDIR(statbuf.st_mode)) {
+ find_service(1, dir->d_name, service);
#if 0
} else {
buffer_putsnlflush(buffer_1,dir->d_name);
#endif
- }
- } else {
- buffer_puts(buffer_2, dir->d_name);
- buffer_puts(buffer_2, ": cannot stat\n");
- buffer_puts_strerror("lstat() failed: ");
- }
- }
-} /* while */
-
-closedir(dirstream);
+ }
+ } else {
+ buffer_puts(buffer_2, dir->d_name);
+ buffer_puts(buffer_2, ": cannot stat\n");
+ buffer_puts_strerror("lstat() failed: ");
+ }
+ }
+ } /* while */
+
+ closedir(dirstream);
ret:
-if (service) free(service);
-chdir(MINITROOT);
-if (parent) chdir(parent);
-buffer_flush(buffer_1);
-}
-
-
-int main(int argc, char **argv) {
- int i;
-
- if (argc < 2) die(USAGE);
-
- while (argc>1) {
- argc--;
- if (argv[argc][0]=='-') {
- switch(argv[argc][1]) {
- case 'v': verbose++; break;
- case 'u': do_update=1; break;
- default:
- buffer_puts(buffer_2,"minit-update: Unknown Option: ");
- buffer_putsnlflush(buffer_2,argv[argc]);
- }
- } else die(USAGE);
- }
-
- infd=open(MINITROOT "/in",O_WRONLY);
- outfd=open(MINITROOT "/out",O_RDONLY);
-
- if (infd<0 || outfd<0) die("could not open " MINITROOT "/in or " MINITROOT "/out\n");
-
- while (lockf(infd,F_TLOCK,1)) {
- buffer_puts_strerror("could not acquire lock: ");
- sleep(1);
- }
-
- find_service(0,MINITROOT,0);
-
- if (maxprocess == -1)
- die("Could not extract running services from minit\n");
-
- if (verbose) buffer_putsflush(buffer_1, "minit-update: telling minit to execve itself\n");
-
- if (!do_update) {
- buffer_putsflush(buffer_2, "Test mode: No update done.\n");
- return 0;
- }
-
- write(infd,"update",6);
- sleep(1);
-
- for (i=0; i<=maxprocess; i++) {
- if (verbose) {
- buffer_puts(buffer_1, "minit-update: restoring status for ");
- buffer_putsnlflush(buffer_1, root[i].name);
- }
-
- buf[0]='U';
- copywrite(root[i].name);
-
- read_reply_from_minit();
-
- write(infd,&root[i],sizeof (struct process));
-
- if (read_reply_from_minit() && verbose) {
- buffer_puts(buffer_1, "minit-update: restored service ");
- buffer_putsnlflush(buffer_1, root[i].name);
- }
-
- } /* for() */
-
-return 0;
+ if (service) free(service);
+ chdir(MINITROOT);
+ if (parent) chdir(parent);
+ buffer_flush(buffer_1);
+}
+
+
+int main(int argc, char** argv) {
+ int i;
+
+ if (argc < 2) die(USAGE);
+
+ while (argc > 1) {
+ argc--;
+ if (argv[argc][0] == '-') {
+ switch (argv[argc][1]) {
+ case 'v':
+ verbose++;
+ break;
+ case 'u':
+ do_update = 1;
+ break;
+ default:
+ buffer_puts(buffer_2, "minit-update: Unknown Option: ");
+ buffer_putsnlflush(buffer_2, argv[argc]);
+ }
+ } else
+ die(USAGE);
+ }
+
+ infd = open(MINITROOT "/in", O_WRONLY);
+ outfd = open(MINITROOT "/out", O_RDONLY);
+
+ if (infd < 0 || outfd < 0) die("could not open " MINITROOT "/in or " MINITROOT "/out\n");
+
+ while (lockf(infd, F_TLOCK, 1)) {
+ buffer_puts_strerror("could not acquire lock: ");
+ sleep(1);
+ }
+
+ find_service(0, MINITROOT, 0);
+
+ if (maxprocess == -1)
+ die("Could not extract running services from minit\n");
+
+ if (verbose) buffer_putsflush(buffer_1, "minit-update: telling minit to execve itself\n");
+
+ if (!do_update) {
+ buffer_putsflush(buffer_2, "Test mode: No update done.\n");
+ return 0;
+ }
+
+ write(infd, "update", 6);
+ sleep(1);
+
+ for (i = 0; i <= maxprocess; i++) {
+ if (verbose) {
+ buffer_puts(buffer_1, "minit-update: restoring status for ");
+ buffer_putsnlflush(buffer_1, root[i].name);
+ }
+
+ buf[0] = 'U';
+ copywrite(root[i].name);
+
+ read_reply_from_minit();
+
+ write(infd, &root[i], sizeof(struct process));
+
+ if (read_reply_from_minit() && verbose) {
+ buffer_puts(buffer_1, "minit-update: restored service ");
+ buffer_putsnlflush(buffer_1, root[i].name);
+ }
+
+ } /* for() */
+
+ return 0;
}
-ssize_t read_outfd(void *buf, size_t count) {
- ssize_t br=read(outfd,buf,count);
+ssize_t read_outfd(void* buf, size_t count) {
+ ssize_t br = read(outfd, buf, count);
- if (br<0) buffer_puts_strerror("Error reading from outfd: ");
-return br;
+ if (br < 0) buffer_puts_strerror("Error reading from outfd: ");
+ return br;
}
-void addprocess(struct process *p) {
- if (maxprocess+1>=processalloc) {
- struct process *fump;
- processalloc+=8;
- if ((fump=(struct process *)realloc(root,processalloc*sizeof(struct process)))==0) die("realloc() failed\n ");
- root=fump;
- }
- memmove(&root[++maxprocess],p,sizeof(struct process));
+void addprocess(struct process* p) {
+ if (maxprocess + 1 >= processalloc) {
+ struct process* fump;
+ processalloc += 8;
+ if ((fump = (struct process*) realloc(root, processalloc * sizeof(struct process))) == 0) die("realloc() failed\n ");
+ root = fump;
+ }
+ memmove(&root[++maxprocess], p, sizeof(struct process));
}
diff --git a/minit.c b/minit.c
@@ -1,76 +1,76 @@
#define _GNU_SOURCE
-#include <sys/types.h>
-#include <time.h>
-#include <string.h>
+#include "minit.h"
+
+#include "write12.h"
+
+#include <alloca.h>
+#include <errno.h>
#include <fcntl.h>
-#include <unistd.h>
+#include <libowfat/compiletimeassert.h>
+#include <libowfat/fmt.h>
+#include <libowfat/str.h>
#include <limits.h>
-#include <errno.h>
-#include <sys/socket.h>
-#include <sys/un.h>
+#include <linux/kd.h>
#include <poll.h>
#include <signal.h>
-#include <sys/wait.h>
#include <stdio.h>
-#include <linux/kd.h>
-#include <sys/ioctl.h>
#include <stdlib.h>
-#include <alloca.h>
+#include <string.h>
+#include <sys/ioctl.h>
#include <sys/reboot.h>
-#include "write12.h"
-
-#include <libowfat/fmt.h>
-#include <libowfat/str.h>
-#include <libowfat/compiletimeassert.h>
-
-#include "minit.h"
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
compiletimeassert(sizeof(MINITROOT) + 64 < PATH_MAX);
#define MALLOC_TEST
#if !defined(__dietlibc__) && !defined(__GLIBC__)
-#undef MALLOC_TEST
+# undef MALLOC_TEST
#endif
#ifdef MALLOC_TEST
-extern void* __libc_malloc(size_t size);
-extern void* __libc_realloc(void* x,size_t size);
-extern void __libc_free(void* x);
-static char malloc_buf[1024];
-static unsigned long n;
+extern void* __libc_malloc(size_t size);
+extern void* __libc_realloc(void* x, size_t size);
+extern void __libc_free(void* x);
+static char malloc_buf[1024];
+static unsigned long n;
static struct process procbuf[50];
-void *malloc(size_t size) {
- if (n+size<sizeof(malloc_buf)) {
- char* tmp=malloc_buf+n;
- n+=size;
- n=(n+3)&~3;
- return tmp;
- }
- return __libc_malloc(size);
+void* malloc(size_t size) {
+ if (n + size < sizeof(malloc_buf)) {
+ char* tmp = malloc_buf + n;
+ n += size;
+ n = (n + 3) & ~3;
+ return tmp;
+ }
+ return __libc_malloc(size);
}
void free(void* x) {
- if ((char*)x>=malloc_buf && (char*)x<malloc_buf+sizeof(malloc_buf)) return;
- __libc_free(x);
+ if ((char*) x >= malloc_buf && (char*) x < malloc_buf + sizeof(malloc_buf)) return;
+ __libc_free(x);
}
-void* realloc(void* x,size_t size) {
- if (x==0 || x==procbuf) {
- void* y;
- if (size<=sizeof(procbuf))
- return procbuf;
- y=__libc_malloc(size);
- if (!y) return 0;
- memcpy(y,x,size);
- return y;
- }
- return __libc_realloc(x,size);
+void* realloc(void* x, size_t size) {
+ if (x == 0 || x == procbuf) {
+ void* y;
+ if (size <= sizeof(procbuf))
+ return procbuf;
+ y = __libc_malloc(size);
+ if (!y) return 0;
+ memcpy(y, x, size);
+ return y;
+ }
+ return __libc_realloc(x, size);
}
#endif
char** Argv;
#undef printf
-extern int printf(const char *format,...);
+extern int printf(const char* format, ...);
extern void opendevconsole();
@@ -81,10 +81,10 @@ static int doupdate;
static int i_am_init;
-extern int openreadclose(char *fn, char **buf, size_t *len);
-extern char **split(char *buf,int c,size_t* len,size_t plus,size_t ofs);
+extern int openreadclose(char* fn, char** buf, size_t* len);
+extern char** split(char* buf, int c, size_t* len, size_t plus, size_t ofs);
-extern char **environ;
+extern char** environ;
#define HISTORY 10
#ifdef HISTORY
@@ -92,350 +92,364 @@ int history[HISTORY];
#endif
/* return index of service in process data structure or -1 if not found */
-int findservice(char *service) {
- int i;
- for (i=0; i<=maxprocess; ++i) {
- if (!strcmp(root[i].name,service))
- return i;
- }
- return -1;
+int findservice(char* service) {
+ int i;
+ for (i = 0; i <= maxprocess; ++i) {
+ if (!strcmp(root[i].name, service))
+ return i;
+ }
+ return -1;
}
/* look up process index in data structure by PID */
int findbypid(pid_t pid) {
- int i;
- for (i=0; i<=maxprocess; ++i) {
- if (root[i].pid == pid)
- return i;
- }
- return -1;
+ int i;
+ for (i = 0; i <= maxprocess; ++i) {
+ if (root[i].pid == pid)
+ return i;
+ }
+ return -1;
}
/* clear circular dependency detection flags */
void circsweep() {
- int i;
- for (i=0; i<=maxprocess; ++i)
- root[i].circular=0;
+ int i;
+ for (i = 0; i <= maxprocess; ++i)
+ root[i].circular = 0;
}
/* add process to data structure, return index or -1 */
-int addprocess(struct process *p) {
- if (maxprocess+1>=processalloc) {
- struct process *fump;
- processalloc+=8;
- if ((fump=(struct process *)realloc(root,processalloc*sizeof(struct process)))==0) return -1;
- root=fump;
- }
- memmove(&root[++maxprocess],p,sizeof(struct process));
- return maxprocess;
+int addprocess(struct process* p) {
+ if (maxprocess + 1 >= processalloc) {
+ struct process* fump;
+ processalloc += 8;
+ if ((fump = (struct process*) realloc(root, processalloc * sizeof(struct process))) == 0) return -1;
+ root = fump;
+ }
+ memmove(&root[++maxprocess], p, sizeof(struct process));
+ return maxprocess;
}
/* load a service into the process data structure and return index or -1
* if failed */
-int loadservice(char *service) {
- struct process tmp;
- int fd;
- if (*service==0) return -1;
- fd=findservice(service);
- if (fd>=0) return fd;
- if (chdir(MINITROOT) || chdir(service)) return -1;
- if (!(tmp.name=strdup(service))) return -1;
- tmp.pid=0;
- fd=open("respawn",O_RDONLY);
- if (fd>=0) {
- tmp.respawn=1;
- close(fd);
- } else
- tmp.respawn=0;
- tmp.startedat=0;
- tmp.circular=0;
- tmp.__stdin=0; tmp.__stdout=1;
- {
- char *logservice=alloca(str_len(service)+5);
- strcpy(logservice,service);
- strcat(logservice,"/log");
- tmp.logservice=loadservice(logservice);
- if (tmp.logservice>=0) {
- int pipefd[2];
- if (pipe(pipefd)) return -1;
- fcntl(pipefd[0],F_SETFD,FD_CLOEXEC);
- fcntl(pipefd[1],F_SETFD,FD_CLOEXEC);
- root[tmp.logservice].__stdin=pipefd[0];
- tmp.__stdout=pipefd[1];
- }
- }
- return addprocess(&tmp);
+int loadservice(char* service) {
+ struct process tmp;
+ int fd;
+ if (*service == 0) return -1;
+ fd = findservice(service);
+ if (fd >= 0) return fd;
+ if (chdir(MINITROOT) || chdir(service)) return -1;
+ if (!(tmp.name = strdup(service))) return -1;
+ tmp.pid = 0;
+ fd = open("respawn", O_RDONLY);
+ if (fd >= 0) {
+ tmp.respawn = 1;
+ close(fd);
+ } else
+ tmp.respawn = 0;
+ tmp.startedat = 0;
+ tmp.circular = 0;
+ tmp.__stdin = 0;
+ tmp.__stdout = 1;
+ {
+ char* logservice = alloca(str_len(service) + 5);
+ strcpy(logservice, service);
+ strcat(logservice, "/log");
+ tmp.logservice = loadservice(logservice);
+ if (tmp.logservice >= 0) {
+ int pipefd[2];
+ if (pipe(pipefd)) return -1;
+ fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
+ fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
+ root[tmp.logservice].__stdin = pipefd[0];
+ tmp.__stdout = pipefd[1];
+ }
+ }
+ return addprocess(&tmp);
}
/* usage: isup(findservice("sshd")).
* returns nonzero if process is up */
int isup(int service) {
- if (service<0) return 0;
- return (root[service].pid!=0);
+ if (service < 0) return 0;
+ return (root[service].pid != 0);
}
-int startservice(int service,int pause,int father);
+int startservice(int service, int pause, int father);
-void sulogin() { /* exiting on an initialization failure is not a good idea for init */
- char *argv[]={"sulogin",0};
- if (i_am_init)
- execve("/sbin/sulogin",argv,environ);
- _exit(1);
+void sulogin() { /* exiting on an initialization failure is not a good idea for init */
+ char* argv[] = { "sulogin", 0 };
+ if (i_am_init)
+ execve("/sbin/sulogin", argv, environ);
+ _exit(1);
}
#undef debug
void handlekilled(pid_t killed) {
- int i;
+ int i;
#ifdef debug
- {
- char buf[50];
- snprintf(buf,50," %d\n",killed);
- write(2,buf,str_len(buf));
- }
+ {
+ char buf[50];
+ snprintf(buf, 50, " %d\n", killed);
+ write(2, buf, str_len(buf));
+ }
#endif
- if (killed == (pid_t)-1) {
- static int saidso;
- if (!saidso) { write(2,"all services exited.\n",21); saidso=1; }
- if (i_am_init) sulogin(); else exit(0);
- }
- if (killed==0) return;
- i=findbypid(killed);
+ if (killed == (pid_t) -1) {
+ static int saidso;
+ if (!saidso) {
+ write(2, "all services exited.\n", 21);
+ saidso = 1;
+ }
+ if (i_am_init)
+ sulogin();
+ else
+ exit(0);
+ }
+ if (killed == 0) return;
+ i = findbypid(killed);
#if 0
printf("%d exited, idx %d -> service %s\n",killed,i,i>=0?root[i].name:"[unknown]");
#endif
- if (i>=0) {
- root[i].pid=0;
- if (root[i].respawn) {
+ if (i >= 0) {
+ root[i].pid = 0;
+ if (root[i].respawn) {
#if 0
printf("restarting %s\n",root[i].name);
#endif
- circsweep();
- startservice(i,time(0)-root[i].startedat<1,root[i].father);
- } else {
- root[i].startedat=time(0);
- root[i].pid=1;
- }
- }
+ circsweep();
+ startservice(i, time(0) - root[i].startedat < 1, root[i].father);
+ } else {
+ root[i].startedat = time(0);
+ root[i].pid = 1;
+ }
+ }
}
/* called from inside the service directory, return the PID or 0 on error */
-pid_t forkandexec(int pause,int service) {
- char **argv=0;
- int count=0;
- pid_t p;
- int fd;
- size_t len;
- int islink;
- char *s=0;
- size_t argc;
- char *argv0=0;
+pid_t forkandexec(int pause, int service) {
+ char** argv = 0;
+ int count = 0;
+ pid_t p;
+ int fd;
+ size_t len;
+ int islink;
+ char* s = 0;
+ size_t argc;
+ char* argv0 = 0;
again:
- switch (p=fork()) {
- case (pid_t)-1:
- if (count>3) return 0;
- sleep(++count*2);
- goto again;
- case 0:
- /* child */
-
- if (i_am_init) {
- ioctl(0, TIOCNOTTY, 0);
- if (setsid() == -1) {
- __write2("setsid failed unexpectedly.\n");
- // This should never fail. init is run as root.
- // If it does fail, don't exit for fear of bricking the system
- }
- opendevconsole();
-/* ioctl(0, TIOCSCTTY, 1); */
- int r = tcsetpgrp(0, getpgrp());
- if (r == -1 && errno!=ENOTTY) { // will get this error for log services
- __write2("tcsetpgrp failed unexpectedly: ");
- switch (errno) {
- case EBADF: __write2("EBADF\n"); break;
- case EINVAL: __write2("EINVAL\n"); break;
-// case ENOTTY: __write2("ENOTTY\n"); break;
- case EPERM: __write2("EPERM\n"); break;
- default: __write2("unhandled\n");
- }
- // This should never fail. init is run as root.
- // If it does fail, don't exit for fear of bricking the system
- }
- }
-
- if (pause) {
- struct timespec req;
- req.tv_sec=0;
- req.tv_nsec=500000000;
- nanosleep(&req,0);
- }
- if ((fd=open("in",O_RDONLY))!=-1) {
- if (dup2(fd,0) != 0) {
- __write2("dup2 failed unexpectedly.\n");
- // This should never fail. init is run as root.
- // If it does fail, don't exit for fear of bricking the system
- }
- fcntl(0,F_SETFD,0);
- }
- if ((fd=open("out",O_WRONLY))!=-1) {
- if (dup2(fd,1) != 1 || dup2(fd,2) != 2) {
- __write2("dup2 failed unexpectedly.\n");
- // This should never fail. init is run as root.
- // If it does fail, don't exit for fear of bricking the system
- }
- fcntl(1,F_SETFD,0);
- fcntl(2,F_SETFD,0);
- }
- // openreadclose and split allocate memory.
- // We leak it here, because we are in the child process that is
- // about to execve somebody else, at which point the OS frees all.
- if (!openreadclose("nice",&s,&len)) {
- int n=atoi(s);
- nice(n);
- s=0;
- }
- if (!openreadclose("params",&s,&len)) {
- argv=split(s,'\n',&argc,2,1);
- if (argc && argv[argc-1][0]==0) --argc; // if params ended on newline, don't pass empty last arg
- argv[argc]=0;
- } else {
- argv=(char**)alloca(2*sizeof(char*));
- argv[1]=0;
- }
- argv0=(char*)alloca(PATH_MAX+1);
- if (!argv) _exit(1);
- if (readlink("run",argv0,PATH_MAX)<0) {
- if (errno!=EINVAL) _exit(1); /* not a symbolic link */
- strcpy(argv0,"run");
- islink=0;
- } else
- islink=1;
- argv[0]=strrchr(argv0,'/');
- if (argv[0])
- argv[0]++;
- else
- argv[0]=argv0;
- argv[0]=strdupa(argv[0]);
- if (islink && argv0[0]!='/') {
- char* c=alloca(strlen(argv0)+sizeof(MINITROOT)+strlen(root[service].name+2));
- char* d;
- int fd=open(".",O_RDONLY|O_DIRECTORY);
- stpcpy(stpcpy(stpcpy(stpcpy(c,MINITROOT "/"),root[service].name),"/"),argv0);
- if (fd!=-1) {
- /* c is now something like /etc/minit/default/../../../var/whatever/doit
- * attempt to clean it up a little */
- d=strrchr(c,'/');
- *d=0;
- if (chdir(c)==0) {
- *d='/';
- if (getcwd(argv0,PATH_MAX))
- strncat(argv0,d,PATH_MAX);
- fchdir(fd);
- close(fd);
- } else {
- *d='/';
- argv0=c;
+ switch (p = fork()) {
+ case (pid_t) -1:
+ if (count > 3) return 0;
+ sleep(++count * 2);
+ goto again;
+ case 0:
+ /* child */
+
+ if (i_am_init) {
+ ioctl(0, TIOCNOTTY, 0);
+ if (setsid() == -1) {
+ __write2("setsid failed unexpectedly.\n");
+ // This should never fail. init is run as root.
+ // If it does fail, don't exit for fear of bricking the system
+ }
+ opendevconsole();
+ /* ioctl(0, TIOCSCTTY, 1); */
+ int r = tcsetpgrp(0, getpgrp());
+ if (r == -1 && errno != ENOTTY) { // will get this error for log services
+ __write2("tcsetpgrp failed unexpectedly: ");
+ switch (errno) {
+ case EBADF:
+ __write2("EBADF\n");
+ break;
+ case EINVAL:
+ __write2("EINVAL\n");
+ break;
+ // case ENOTTY: __write2("ENOTTY\n"); break;
+ case EPERM:
+ __write2("EPERM\n");
+ break;
+ default:
+ __write2("unhandled\n");
+ }
+ // This should never fail. init is run as root.
+ // If it does fail, don't exit for fear of bricking the system
+ }
+ }
+
+ if (pause) {
+ struct timespec req;
+ req.tv_sec = 0;
+ req.tv_nsec = 500000000;
+ nanosleep(&req, 0);
+ }
+ if ((fd = open("in", O_RDONLY)) != -1) {
+ if (dup2(fd, 0) != 0) {
+ __write2("dup2 failed unexpectedly.\n");
+ // This should never fail. init is run as root.
+ // If it does fail, don't exit for fear of bricking the system
+ }
+ fcntl(0, F_SETFD, 0);
+ }
+ if ((fd = open("out", O_WRONLY)) != -1) {
+ if (dup2(fd, 1) != 1 || dup2(fd, 2) != 2) {
+ __write2("dup2 failed unexpectedly.\n");
+ // This should never fail. init is run as root.
+ // If it does fail, don't exit for fear of bricking the system
+ }
+ fcntl(1, F_SETFD, 0);
+ fcntl(2, F_SETFD, 0);
+ }
+ // openreadclose and split allocate memory.
+ // We leak it here, because we are in the child process that is
+ // about to execve somebody else, at which point the OS frees all.
+ if (!openreadclose("nice", &s, &len)) {
+ int n = atoi(s);
+ nice(n);
+ s = 0;
+ }
+ if (!openreadclose("params", &s, &len)) {
+ argv = split(s, '\n', &argc, 2, 1);
+ if (argc && argv[argc - 1][0] == 0) --argc; // if params ended on newline, don't pass empty last arg
+ argv[argc] = 0;
+ } else {
+ argv = (char**) alloca(2 * sizeof(char*));
+ argv[1] = 0;
+ }
+ argv0 = (char*) alloca(PATH_MAX + 1);
+ if (!argv) _exit(1);
+ if (readlink("run", argv0, PATH_MAX) < 0) {
+ if (errno != EINVAL) _exit(1); /* not a symbolic link */
+ strcpy(argv0, "run");
+ islink = 0;
+ } else
+ islink = 1;
+ argv[0] = strrchr(argv0, '/');
+ if (argv[0])
+ argv[0]++;
+ else
+ argv[0] = argv0;
+ argv[0] = strdupa(argv[0]);
+ if (islink && argv0[0] != '/') {
+ char* c = alloca(strlen(argv0) + sizeof(MINITROOT) + strlen(root[service].name + 2));
+ char* d;
+ int fd = open(".", O_RDONLY | O_DIRECTORY);
+ stpcpy(stpcpy(stpcpy(stpcpy(c, MINITROOT "/"), root[service].name), "/"), argv0);
+ if (fd != -1) {
+ /* c is now something like /etc/minit/default/../../../var/whatever/doit
+ * attempt to clean it up a little */
+ d = strrchr(c, '/');
+ *d = 0;
+ if (chdir(c) == 0) {
+ *d = '/';
+ if (getcwd(argv0, PATH_MAX))
+ strncat(argv0, d, PATH_MAX);
+ fchdir(fd);
+ close(fd);
+ } else {
+ *d = '/';
+ argv0 = c;
+ }
+ } else
+ argv0 = c;
+ }
+ if (root[service].__stdin != 0) {
+ if (dup2(root[service].__stdin, 0) != 0) {
+ __write2("dup2 failed unexpectedly.\n");
+ // This should never fail. init is run as root.
+ // If it does fail, don't exit for fear of bricking the system
+ }
+ fcntl(0, F_SETFD, 0);
+ }
+ if (root[service].__stdout != 1) {
+ if (dup2(root[service].__stdout, 1) != 1 || dup2(root[service].__stdout, 2) != 2) {
+ __write2("dup2 failed unexpectedly.\n");
+ // This should never fail. init is run as root.
+ // If it does fail, don't exit for fear of bricking the system
+ }
+ fcntl(1, F_SETFD, 0);
+ fcntl(2, F_SETFD, 0);
+ }
+ {
+ int i;
+ for (i = 3; i < 1024; ++i) close(i);
+ }
+ chdir("root");
+ execve(argv0, argv, environ);
+ _exit(1);
+ default:
+ fd = open("sync", O_RDONLY);
+ if (fd >= 0) {
+ close(fd);
+ waitpid(p, 0, 0);
+ return 1;
+ }
+ return p;
}
- } else
- argv0=c;
- }
- if (root[service].__stdin != 0) {
- if (dup2(root[service].__stdin,0) != 0) {
- __write2("dup2 failed unexpectedly.\n");
- // This should never fail. init is run as root.
- // If it does fail, don't exit for fear of bricking the system
- }
- fcntl(0,F_SETFD,0);
- }
- if (root[service].__stdout != 1) {
- if (dup2(root[service].__stdout,1) != 1 || dup2(root[service].__stdout,2) != 2) {
- __write2("dup2 failed unexpectedly.\n");
- // This should never fail. init is run as root.
- // If it does fail, don't exit for fear of bricking the system
- }
- fcntl(1,F_SETFD,0);
- fcntl(2,F_SETFD,0);
- }
- {
- int i;
- for (i=3; i<1024; ++i) close(i);
- }
- chdir("root");
- execve(argv0,argv,environ);
- _exit(1);
- default:
- fd=open("sync",O_RDONLY);
- if (fd>=0) {
- close(fd);
- waitpid(p,0,0);
- return 1;
- }
- return p;
- }
}
/* start a service, return nonzero on error */
-int startnodep(int service,int pause) {
- /* step 1: see if the process is already up */
- if (isup(service)) return 0;
- /* step 2: fork and exec service, put PID in data structure */
- if (chdir(MINITROOT) || chdir(root[service].name)) return -1;
- root[service].startedat=time(0);
- root[service].pid=forkandexec(pause,service);
- return root[service].pid;
+int startnodep(int service, int pause) {
+ /* step 1: see if the process is already up */
+ if (isup(service)) return 0;
+ /* step 2: fork and exec service, put PID in data structure */
+ if (chdir(MINITROOT) || chdir(root[service].name)) return -1;
+ root[service].startedat = time(0);
+ root[service].pid = forkandexec(pause, service);
+ return root[service].pid;
}
-int startservice(int service,int pause,int father) {
- int dir=-1;
- unsigned long len;
- char *s=0;
- pid_t pid=0;
- if (service<0) return 0;
- if (root[service].circular)
- return 0;
- root[service].circular=1;
+int startservice(int service, int pause, int father) {
+ int dir = -1;
+ unsigned long len;
+ char* s = 0;
+ pid_t pid = 0;
+ if (service < 0) return 0;
+ if (root[service].circular)
+ return 0;
+ root[service].circular = 1;
#if 0
printf("setting father of %d (%s) to %d (%s)\n",
service,root[service].name,father,father>=0?root[father].name:"[msvc]");
#endif
- root[service].father=father;
+ root[service].father = father;
#ifdef HISTORY
- {
- memmove(history+1,history,sizeof(int)*((HISTORY)-1));
- history[0]=service;
- }
+ {
+ memmove(history + 1, history, sizeof(int) * ((HISTORY) -1));
+ history[0] = service;
+ }
#endif
- if (root[service].logservice>=0)
- startservice(root[service].logservice,pause,service);
- if (chdir(MINITROOT) || chdir(root[service].name)) return -1;
- if ((dir=open(".",O_RDONLY))>=0) {
- // openreadclose allocates memory and reads the file contents into it.
- // Need to free(s) independent of openreadclose return value
- if (!openreadclose("depends",&s,&len)) {
- char **deps=0;
- size_t depc,i;
- deps=split(s,'\n',&depc,0,0);
- for (i=0; i<depc; i++) {
- int Service,blacklisted,j;
- if (deps[i][0]=='#') continue;
- Service=loadservice(deps[i]);
+ if (root[service].logservice >= 0)
+ startservice(root[service].logservice, pause, service);
+ if (chdir(MINITROOT) || chdir(root[service].name)) return -1;
+ if ((dir = open(".", O_RDONLY)) >= 0) {
+ // openreadclose allocates memory and reads the file contents into it.
+ // Need to free(s) independent of openreadclose return value
+ if (!openreadclose("depends", &s, &len)) {
+ char** deps = 0;
+ size_t depc, i;
+ deps = split(s, '\n', &depc, 0, 0);
+ for (i = 0; i < depc; i++) {
+ int Service, blacklisted, j;
+ if (deps[i][0] == '#') continue;
+ Service = loadservice(deps[i]);
#if 1
- for (j=blacklisted=0; Argv[j]; ++j)
- if (Argv[j][0]=='-' && !strcmp(Argv[j]+1,deps[i])) {
- blacklisted=1;
- ++Argv[j];
- break;
- }
+ for (j = blacklisted = 0; Argv[j]; ++j)
+ if (Argv[j][0] == '-' && !strcmp(Argv[j] + 1, deps[i])) {
+ blacklisted = 1;
+ ++Argv[j];
+ break;
+ }
#endif
- if (Service>=0 && root[Service].pid!=1 && !blacklisted)
- startservice(Service,0,service);
- }
- fchdir(dir);
- free(deps);
- }
- free(s);
- pid=startnodep(service,pause);
+ if (Service >= 0 && root[Service].pid != 1 && !blacklisted)
+ startservice(Service, 0, service);
+ }
+ fchdir(dir);
+ free(deps);
+ }
+ free(s);
+ pid = startnodep(service, pause);
#if 0
write(1,"started service ",17);
@@ -447,22 +461,22 @@ int startservice(int service,int pause,int father) {
write(1,buf,str_len(buf));
}
#endif
- close(dir);
- dir=-1;
- }
- chdir(MINITROOT);
- return pid;
+ close(dir);
+ dir = -1;
+ }
+ chdir(MINITROOT);
+ return pid;
}
static void _puts(const char* s) {
- write(1,s,str_len(s));
+ write(1, s, str_len(s));
}
void childhandler() {
- int status;
- pid_t killed;
+ int status;
+ pid_t killed;
#ifdef debug
- write(2,"wait...",7);
+ write(2, "wait...", 7);
#endif
#if 0
if (getpid()!=1) {
@@ -475,261 +489,268 @@ void childhandler() {
}
#endif
#ifdef UPDATE
-if (doupdate) return;
+ if (doupdate) return;
#endif
- do {
- killed=waitpid(-1,&status,WNOHANG);
- handlekilled(killed);
- } while (killed && killed!=(pid_t)-1);
+ do {
+ killed = waitpid(-1, &status, WNOHANG);
+ handlekilled(killed);
+ } while (killed && killed != (pid_t) -1);
}
-static volatile int dowinch=0;
-static volatile int doint=0;
+static volatile int dowinch = 0;
+static volatile int doint = 0;
-void sigchild(int sig) { (void)sig; }
-void sigwinch(int sig) { (void)sig; dowinch=1; }
-void sigint(int sig) { (void)sig; doint=1; }
+void sigchild(int sig) { (void) sig; }
+void sigwinch(int sig) {
+ (void) sig;
+ dowinch = 1;
+}
+void sigint(int sig) {
+ (void) sig;
+ doint = 1;
+}
-int main(int argc, char *argv[]) {
- /* Schritt 1: argv[1] als Service nehmen und starten */
- int count=0;
- int i;
- struct pollfd pfd;
- time_t last=time(0);
- int nfds=1;
+int main(int argc, char* argv[]) {
+ /* Schritt 1: argv[1] als Service nehmen und starten */
+ int count = 0;
+ int i;
+ struct pollfd pfd;
+ time_t last = time(0);
+ int nfds = 1;
#ifdef HISTORY
- for (i=0; i<HISTORY; ++i)
- history[i]=-1;
+ for (i = 0; i < HISTORY; ++i)
+ history[i] = -1;
#endif
- Argv=argv;
+ Argv = argv;
- infd=open(MINITROOT "/in",O_RDWR);
- outfd=open(MINITROOT "/out",O_RDWR|O_NONBLOCK);
+ infd = open(MINITROOT "/in", O_RDWR);
+ outfd = open(MINITROOT "/out", O_RDWR | O_NONBLOCK);
- if (getpid()==1) {
- int fd;
- i_am_init=1;
- reboot(RB_DISABLE_CAD);
- if ((fd=open("/dev/console",O_RDWR|O_NOCTTY))!=-1) {
- ioctl(fd, KDSIGACCEPT, SIGWINCH);
- close(fd);
- } else
- ioctl(0, KDSIGACCEPT, SIGWINCH);
- }
-/* signal(SIGPWR,sighandler); don't know what to do about it */
-/* signal(SIGHUP,sighandler); ??? */
- {
- static struct sigaction sa;
- sigemptyset(&sa.sa_mask);
- errno=0;
- sa.sa_sigaction=0;
- sa.sa_flags=SA_RESTART | SA_NOCLDSTOP;
- sa.sa_handler=sigchild; sigaction(SIGCHLD,&sa,0);
- sa.sa_flags=SA_RESTART;
- if (i_am_init) {
- sa.sa_handler=sigint; sigaction(SIGINT,&sa,0); /* ctrl-alt-del */
- sa.sa_handler=sigwinch; sigaction(SIGWINCH,&sa,0); /* keyboard request */
- }
- if (errno) _puts("sigaction failed!\n");
- }
+ if (getpid() == 1) {
+ int fd;
+ i_am_init = 1;
+ reboot(RB_DISABLE_CAD);
+ if ((fd = open("/dev/console", O_RDWR | O_NOCTTY)) != -1) {
+ ioctl(fd, KDSIGACCEPT, SIGWINCH);
+ close(fd);
+ } else
+ ioctl(0, KDSIGACCEPT, SIGWINCH);
+ }
+ /* signal(SIGPWR,sighandler); don't know what to do about it */
+ /* signal(SIGHUP,sighandler); ??? */
+ {
+ static struct sigaction sa;
+ sigemptyset(&sa.sa_mask);
+ errno = 0;
+ sa.sa_sigaction = 0;
+ sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
+ sa.sa_handler = sigchild;
+ sigaction(SIGCHLD, &sa, 0);
+ sa.sa_flags = SA_RESTART;
+ if (i_am_init) {
+ sa.sa_handler = sigint;
+ sigaction(SIGINT, &sa, 0); /* ctrl-alt-del */
+ sa.sa_handler = sigwinch;
+ sigaction(SIGWINCH, &sa, 0); /* keyboard request */
+ }
+ if (errno) _puts("sigaction failed!\n");
+ }
- if (infd<0 || outfd<0) {
- _puts("minit: could not open " MINITROOT "/in or " MINITROOT "/out\n");
- sulogin();
- nfds=0;
- } else
- pfd.fd=infd;
- pfd.events=POLLIN;
+ if (infd < 0 || outfd < 0) {
+ _puts("minit: could not open " MINITROOT "/in or " MINITROOT "/out\n");
+ sulogin();
+ nfds = 0;
+ } else
+ pfd.fd = infd;
+ pfd.events = POLLIN;
- fcntl(infd,F_SETFD,FD_CLOEXEC);
- fcntl(outfd,F_SETFD,FD_CLOEXEC);
+ fcntl(infd, F_SETFD, FD_CLOEXEC);
+ fcntl(outfd, F_SETFD, FD_CLOEXEC);
#ifdef UPDATE
- {
- struct flock fl;
- fl.l_whence=SEEK_CUR;
- fl.l_start=0;
- fl.l_len=0;
- fl.l_pid=0;
- if ( (0 == fcntl(infd,F_GETLK,&fl)) &&
- (fl.l_type != F_UNLCK )) doupdate=1;
- }
+ {
+ struct flock fl;
+ fl.l_whence = SEEK_CUR;
+ fl.l_start = 0;
+ fl.l_len = 0;
+ fl.l_pid = 0;
+ if ((0 == fcntl(infd, F_GETLK, &fl)) &&
+ (fl.l_type != F_UNLCK)) doupdate = 1;
+ }
- if(!doupdate) {
+ if (!doupdate) {
#endif
- for (i=1; i<argc; i++) {
- circsweep();
- if (startservice(loadservice(argv[i]),0,-1)) count++;
- }
- circsweep();
- if (!count) startservice(loadservice("default"),0,-1);
+ for (i = 1; i < argc; i++) {
+ circsweep();
+ if (startservice(loadservice(argv[i]), 0, -1)) count++;
+ }
+ circsweep();
+ if (!count) startservice(loadservice("default"), 0, -1);
#ifdef UPDATE
- }
+ }
#endif
- for (;;) {
- int i;
- char buf[1501];
- time_t now;
- if (doint) {
- doint=0;
- startservice(loadservice("ctrlaltdel"),0,-1);
- }
- if (dowinch) {
- dowinch=0;
- startservice(loadservice("kbreq"),0,-1);
- }
- childhandler();
- now=time(0);
- if (now<last || now-last>30) {
- /* The system clock was reset. Compensate. */
- long diff=last-now;
- int j;
-
- for (j=0; j<=maxprocess; ++j)
- root[j].startedat-=diff;
- }
- last=now;
- switch (poll(&pfd,nfds,5000)) {
- case -1:
- if (errno==EINTR) {
- childhandler();
- break;
- }
- opendevconsole();
- _puts("poll failed!\n");
- sulogin();
- /* what should we do if poll fails?! */
- break;
- case 1:
- i=read(infd,buf,1500);
- if (i>1) {
- int idx=0,tmp;
- buf[i]=0;
+ for (;;) {
+ int i;
+ char buf[1501];
+ time_t now;
+ if (doint) {
+ doint = 0;
+ startservice(loadservice("ctrlaltdel"), 0, -1);
+ }
+ if (dowinch) {
+ dowinch = 0;
+ startservice(loadservice("kbreq"), 0, -1);
+ }
+ childhandler();
+ now = time(0);
+ if (now < last || now - last > 30) {
+ /* The system clock was reset. Compensate. */
+ long diff = last - now;
+ int j;
+
+ for (j = 0; j <= maxprocess; ++j)
+ root[j].startedat -= diff;
+ }
+ last = now;
+ switch (poll(&pfd, nfds, 5000)) {
+ case -1:
+ if (errno == EINTR) {
+ childhandler();
+ break;
+ }
+ opendevconsole();
+ _puts("poll failed!\n");
+ sulogin();
+ /* what should we do if poll fails?! */
+ break;
+ case 1:
+ i = read(infd, buf, 1500);
+ if (i > 1) {
+ int idx = 0, tmp;
+ buf[i] = 0;
/* write(1,buf,str_len(buf)); write(1,"\n",1); */
#ifdef UPDATE
- if(!strcmp(buf,"update")) {
- execve("/sbin/minit",argv, environ);
- }
+ if (!strcmp(buf, "update")) {
+ execve("/sbin/minit", argv, environ);
+ }
- if (((buf[0]!='U') && buf[0]!='s') && ((idx=findservice(buf+1))<0)
- && strcmp(buf,"d-"))
+ if (((buf[0] != 'U') && buf[0] != 's') && ((idx = findservice(buf + 1)) < 0) && strcmp(buf, "d-"))
#else
- if (buf[0]!='s' && ((idx=findservice(buf+1))<0) && strcmp(buf,"d-") )
+ if (buf[0] != 's' && ((idx = findservice(buf + 1)) < 0) && strcmp(buf, "d-"))
#endif
-error:
- write(outfd,"0",1);
- else {
- switch (buf[0]) {
- case 'p':
- write(outfd,buf,fmt_ulong(buf,root[idx].pid));
- break;
+ error:
+ write(outfd, "0", 1);
+ else {
+ switch (buf[0]) {
+ case 'p':
+ write(outfd, buf, fmt_ulong(buf, root[idx].pid));
+ break;
#ifdef UPDATE
- case 'D':
- doupdate=1;
- write(outfd, &root[idx], sizeof(struct process));
- break;
- case 'U':
- doupdate=1;
- write(outfd,"1",1);
- if (1==poll(&pfd,nfds,5000)) {
- struct process tmp;
- if (read(infd,&tmp,sizeof tmp) != sizeof(tmp)) {
- __write2("update failed, struct size mismatch.\n");
- goto ok;
- }
- tmp.name=strdup(buf+1);
- addprocess(&tmp);
- }
- goto ok;
+ case 'D':
+ doupdate = 1;
+ write(outfd, &root[idx], sizeof(struct process));
+ break;
+ case 'U':
+ doupdate = 1;
+ write(outfd, "1", 1);
+ if (1 == poll(&pfd, nfds, 5000)) {
+ struct process tmp;
+ if (read(infd, &tmp, sizeof tmp) != sizeof(tmp)) {
+ __write2("update failed, struct size mismatch.\n");
+ goto ok;
+ }
+ tmp.name = strdup(buf + 1);
+ addprocess(&tmp);
+ }
+ goto ok;
#endif
- case 'r':
- root[idx].respawn=0;
- goto ok;
- case 'R':
- root[idx].respawn=1;
- goto ok;
- case 'C':
- if (kill(root[idx].pid,0)) { /* check if still active */
- handlekilled(root[idx].pid); /* no!?! remove form active list */
- goto error;
- }
- goto ok;
- case 'P':
- {
- unsigned char *x=(unsigned char*)buf+str_len(buf)+1;
- unsigned char c;
- tmp=0;
- while ((c=*x++-'0')<10) tmp=tmp*10+c;
- }
- if (tmp>0) {
- if (kill(tmp,0)) goto error;
- }
- root[idx].pid=tmp;
- goto ok;
- case 's':
- idx=loadservice(buf+1);
- if (idx<0) goto error;
- if (root[idx].pid<2) {
- root[idx].pid=0;
- circsweep();
- idx=startservice(idx,0,-1);
- if (idx==0) {
- write(outfd,"0",1);
- break;
- }
- }
-ok:
- write(outfd,"1",1);
- break;
- case 'u':
- write(outfd,buf,fmt_ulong(buf,time(0)-root[idx].startedat));
- break;
- case 'd':
- write(outfd,"1:",2);
- {
- int i;
+ case 'r':
+ root[idx].respawn = 0;
+ goto ok;
+ case 'R':
+ root[idx].respawn = 1;
+ goto ok;
+ case 'C':
+ if (kill(root[idx].pid, 0)) { /* check if still active */
+ handlekilled(root[idx].pid); /* no!?! remove form active list */
+ goto error;
+ }
+ goto ok;
+ case 'P': {
+ unsigned char* x = (unsigned char*) buf + str_len(buf) + 1;
+ unsigned char c;
+ tmp = 0;
+ while ((c = *x++ - '0') < 10) tmp = tmp * 10 + c;
+ }
+ if (tmp > 0) {
+ if (kill(tmp, 0)) goto error;
+ }
+ root[idx].pid = tmp;
+ goto ok;
+ case 's':
+ idx = loadservice(buf + 1);
+ if (idx < 0) goto error;
+ if (root[idx].pid < 2) {
+ root[idx].pid = 0;
+ circsweep();
+ idx = startservice(idx, 0, -1);
+ if (idx == 0) {
+ write(outfd, "0", 1);
+ break;
+ }
+ }
+ ok:
+ write(outfd, "1", 1);
+ break;
+ case 'u':
+ write(outfd, buf, fmt_ulong(buf, time(0) - root[idx].startedat));
+ break;
+ case 'd':
+ write(outfd, "1:", 2);
+ {
+ int i;
#if 0
printf("looking for father==%d\n",idx);
#endif
- for (i=0; i<=maxprocess; ++i) {
+ for (i = 0; i <= maxprocess; ++i) {
#if 0
printf("pid of %d(%s) is %lu, father is %d\n",
i,root[i].name?root[i].name:"[none]",root[i].pid,root[i].father);
#endif
- if (root[i].father==idx)
- write(outfd,root[i].name,str_len(root[i].name)+1);
- }
- write(outfd,"\0",2);
- }
- break;
- }
- }
- } else {
- if (buf[0]=='h') {
+ if (root[i].father == idx)
+ write(outfd, root[i].name, str_len(root[i].name) + 1);
+ }
+ write(outfd, "\0", 2);
+ }
+ break;
+ }
+ }
+ } else {
+ if (buf[0] == 'h') {
#ifdef HISTORY
- write(outfd,"1:",2);
- {
- int i;
- for (i=0; i<HISTORY; ++i)
- if (history[i]!=-1)
- write(outfd,root[history[i]].name,str_len(root[history[i]].name)+1);
- write(outfd,"\0",2);
- }
+ write(outfd, "1:", 2);
+ {
+ int i;
+ for (i = 0; i < HISTORY; ++i)
+ if (history[i] != -1)
+ write(outfd, root[history[i]].name, str_len(root[history[i]].name) + 1);
+ write(outfd, "\0", 2);
+ }
#else
- write(outfd,"0",1);
+ write(outfd, "0", 1);
#endif
- }
- }
- break;
- default:
+ }
+ }
+ break;
+ default:
#ifdef UPDATE
- doupdate=0;
+ doupdate = 0;
#endif
- break;
- }
- }
+ break;
+ }
+ }
}
diff --git a/minit.h b/minit.h
@@ -4,18 +4,18 @@
#ifndef NOVARS
static struct process {
- char *name;
-/* char **argv; */
- pid_t pid;
- char respawn;
- char circular;
- time_t startedat;
- int father; /* the service who started me or -1 if I was started directly */
- int __stdin,__stdout;
- int logservice;
-} *root;
+ char* name;
+ /* char **argv; */
+ pid_t pid;
+ char respawn;
+ char circular;
+ time_t startedat;
+ int father; /* the service who started me or -1 if I was started directly */
+ int __stdin, __stdout;
+ int logservice;
+}* root;
-static int infd,outfd;
-static int maxprocess=-1;
+static int infd, outfd;
+static int maxprocess = -1;
static int processalloc;
#endif
diff --git a/msvc.c b/msvc.c
@@ -1,339 +1,385 @@
+#include <errno.h>
#include <fcntl.h>
-#include <sys/file.h>
+#include <libowfat/buffer.h>
+#include <libowfat/errmsg.h>
+#include <libowfat/fmt.h>
+#include <libowfat/str.h>
#include <signal.h>
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>
+#include <sys/file.h>
#include <unistd.h>
-#include <errno.h>
-
-#include <libowfat/str.h>
-#include <libowfat/fmt.h>
-#include <libowfat/buffer.h>
-#include <libowfat/errmsg.h>
#define NOVARS
#include "minit.h"
-static int infd,outfd;
+static int infd, outfd;
static char buf[1500];
void addservice(char* service) {
- char* x;
- if (str_start(service,MINITROOT "/"))
- service+=sizeof(MINITROOT "/") -1;
- x=service+str_len(service)-1;
- while (x>service && *x=='/') { *x=0; --x; }
- strncpy(buf+1,service,1400);
- buf[1400]=0;
+ char* x;
+ if (str_start(service, MINITROOT "/"))
+ service += sizeof(MINITROOT "/") - 1;
+ x = service + str_len(service) - 1;
+ while (x > service && *x == '/') {
+ *x = 0;
+ --x;
+ }
+ strncpy(buf + 1, service, 1400);
+ buf[1400] = 0;
}
int addreadwrite(char* service) {
- addservice(service);
- write(infd,buf,str_len(buf));
- return read(outfd,buf,1500);
+ addservice(service);
+ write(infd, buf, str_len(buf));
+ return read(outfd, buf, 1500);
}
/* return PID, 0 if error */
-pid_t __readpid(char *service) {
- int len;
- buf[0]='p';
- len=addreadwrite(service);
- if (len<0) return 0;
- buf[len]=0;
- return atoi(buf);
+pid_t __readpid(char* service) {
+ int len;
+ buf[0] = 'p';
+ len = addreadwrite(service);
+ if (len < 0) return 0;
+ buf[len] = 0;
+ return atoi(buf);
}
/* return nonzero if error */
-int respawn(char *service,int yesno) {
- int len;
- buf[0]=yesno?'R':'r';
- len=addreadwrite(service);
- return (len!=1 || buf[0]=='0');
+int respawn(char* service, int yesno) {
+ int len;
+ buf[0] = yesno ? 'R' : 'r';
+ len = addreadwrite(service);
+ return (len != 1 || buf[0] == '0');
}
/* return nonzero if error */
-int setpid(char *service, pid_t pid) {
- char *tmp;
- int len;
- buf[0]='P';
- addservice(service);
- tmp=buf+str_len(buf)+1;
- tmp[fmt_ulong(tmp,pid)]=0;
- write(infd,buf,str_len(buf)+str_len(tmp)+2);
- len=read(outfd,buf,1500);
- return (len!=1 || buf[0]=='0');
+int setpid(char* service, pid_t pid) {
+ char* tmp;
+ int len;
+ buf[0] = 'P';
+ addservice(service);
+ tmp = buf + str_len(buf) + 1;
+ tmp[fmt_ulong(tmp, pid)] = 0;
+ write(infd, buf, str_len(buf) + str_len(tmp) + 2);
+ len = read(outfd, buf, 1500);
+ return (len != 1 || buf[0] == '0');
}
/* return nonzero if error */
-int check_remove(char *service) {
- int len;
- buf[0]='C';
- len=addreadwrite(service);
- return (len!=1 || buf[0]=='0');
+int check_remove(char* service) {
+ int len;
+ buf[0] = 'C';
+ len = addreadwrite(service);
+ return (len != 1 || buf[0] == '0');
}
/* return nonzero if error */
-int startservice(char *service) {
- int len;
- buf[0]='s';
- len=addreadwrite(service);
- return (len!=1 || buf[0]=='0');
+int startservice(char* service) {
+ int len;
+ buf[0] = 's';
+ len = addreadwrite(service);
+ return (len != 1 || buf[0] == '0');
}
/* return uptime, 0 if error */
-unsigned long uptime(char *service) {
- int len;
- buf[0]='u';
- len=addreadwrite(service);
- if (len<0) return 0;
- buf[len]=0;
- return atoi(buf);
+unsigned long uptime(char* service) {
+ int len;
+ buf[0] = 'u';
+ len = addreadwrite(service);
+ if (len < 0) return 0;
+ buf[len] = 0;
+ return atoi(buf);
}
void dumphistory() {
- char tmp[16384];
- int i,j;
- char first,last;
- first=1; last='x';
- write(infd,"h",1);
- for (;;) {
- int done;
- j=read(outfd,tmp,sizeof(tmp));
- if (j<1) break;
- done=i=0;
- if (first) {
- if (tmp[0]=='0') {
- carp("minit compiled without history support.");
- return;
- }
- i+=2;
- } else {
- if (!tmp[0] && last=='\n') break;
- }
- for (; i<j; ++i)
- if (!tmp[i]) {
- tmp[i]=done?0:'\n';
- if (i<j && !tmp[i+1]) { done=1; --j; }
- }
- if (first)
- write(1,tmp+2,j-2);
- else
- write(1,tmp,j);
- if (done) break;
- last=tmp[j-1];
- first=0;
- }
+ char tmp[16384];
+ int i, j;
+ char first, last;
+ first = 1;
+ last = 'x';
+ write(infd, "h", 1);
+ for (;;) {
+ int done;
+ j = read(outfd, tmp, sizeof(tmp));
+ if (j < 1) break;
+ done = i = 0;
+ if (first) {
+ if (tmp[0] == '0') {
+ carp("minit compiled without history support.");
+ return;
+ }
+ i += 2;
+ } else {
+ if (!tmp[0] && last == '\n') break;
+ }
+ for (; i < j; ++i)
+ if (!tmp[i]) {
+ tmp[i] = done ? 0 : '\n';
+ if (i < j && !tmp[i + 1]) {
+ done = 1;
+ --j;
+ }
+ }
+ if (first)
+ write(1, tmp + 2, j - 2);
+ else
+ write(1, tmp, j);
+ if (done) break;
+ last = tmp[j - 1];
+ first = 0;
+ }
}
void dumpdependencies(char* service) {
- char tmp[16384];
- int i,j;
- char first,last;
- buf[0]='d';
- addservice(service);
- write(infd,buf,str_len(buf));
- first=1; last='x';
- for (;;) {
- int done;
- j=read(outfd,tmp,sizeof(tmp));
- if (j<1) break;
- done=i=0;
- if (first) {
- if (tmp[0]=='0') {
- carp(service,": no such service.");
- return;
- }
- i+=2;
- } else {
- if (!tmp[0] && last=='\n') break;
- }
- for (; i<j; ++i)
- if (!tmp[i]) {
- tmp[i]=done?0:'\n';
- if (i<j && !tmp[i+1]) { done=1; --j; }
- }
- if (first)
- write(1,tmp+2,j-2);
- else
- write(1,tmp,j);
- if (done) break;
- last=tmp[j-1];
- first=0;
- }
+ char tmp[16384];
+ int i, j;
+ char first, last;
+ buf[0] = 'd';
+ addservice(service);
+ write(infd, buf, str_len(buf));
+ first = 1;
+ last = 'x';
+ for (;;) {
+ int done;
+ j = read(outfd, tmp, sizeof(tmp));
+ if (j < 1) break;
+ done = i = 0;
+ if (first) {
+ if (tmp[0] == '0') {
+ carp(service, ": no such service.");
+ return;
+ }
+ i += 2;
+ } else {
+ if (!tmp[0] && last == '\n') break;
+ }
+ for (; i < j; ++i)
+ if (!tmp[i]) {
+ tmp[i] = done ? 0 : '\n';
+ if (i < j && !tmp[i + 1]) {
+ done = 1;
+ --j;
+ }
+ }
+ if (first)
+ write(1, tmp + 2, j - 2);
+ else
+ write(1, tmp, j);
+ if (done) break;
+ last = tmp[j - 1];
+ first = 0;
+ }
}
-int main(int argc,char *argv[]) {
- if (argc<2) {
- msg(
- "usage: msvc -[uodpchaitkogC] service\n"
- " msvc -Ppid service\n"
- " -u\tup; start service with respawn\n"
- " -o\tonce; start service without respawn\n"
- " -d\tdown; disable respawn, stop service\n"
- " -p\tpause; send SIGSTOP\n"
- " -c\tcontinue; send SIGCONT\n"
- " -h\thangup; send SIGHUP\n"
- " -a\talarm; send SIGALRM\n"
- " -i\tintr; send SIGINT\n"
- " -t\tterminate; send SIGTERM\n"
- " -k\tkill; send SIGKILL\n"
- " -g\tget; output just the PID\n"
- " -Ppid\tset PID of service (for pidfilehack)\n"
- " -D service\tprint services started as dependency\n"
- " -H\tprint last n respawned services\n"
- " -C\tClear; remove service form active list\n");
- return 0;
- }
- errmsg_iam("msvc");
- infd=open(MINITROOT "/in",O_WRONLY);
- outfd=open(MINITROOT "/out",O_RDONLY);
- if (infd>=0) {
- while (lockf(infd,F_LOCK,1)) {
- carp("could not acquire lock!");
- sleep(1);
- }
- if (argc==2 && argv[1][1]!='H') {
- pid_t pid=__readpid(argv[1]);
- if (buf[0]!='0') {
- unsigned long len;
- unsigned long ut=uptime(argv[1]);
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ msg(
+ "usage: msvc -[uodpchaitkogC] service\n"
+ " msvc -Ppid service\n"
+ " -u\tup; start service with respawn\n"
+ " -o\tonce; start service without respawn\n"
+ " -d\tdown; disable respawn, stop service\n"
+ " -p\tpause; send SIGSTOP\n"
+ " -c\tcontinue; send SIGCONT\n"
+ " -h\thangup; send SIGHUP\n"
+ " -a\talarm; send SIGALRM\n"
+ " -i\tintr; send SIGINT\n"
+ " -t\tterminate; send SIGTERM\n"
+ " -k\tkill; send SIGKILL\n"
+ " -g\tget; output just the PID\n"
+ " -Ppid\tset PID of service (for pidfilehack)\n"
+ " -D service\tprint services started as dependency\n"
+ " -H\tprint last n respawned services\n"
+ " -C\tClear; remove service form active list\n");
+ return 0;
+ }
+ errmsg_iam("msvc");
+ infd = open(MINITROOT "/in", O_WRONLY);
+ outfd = open(MINITROOT "/out", O_RDONLY);
+ if (infd >= 0) {
+ while (lockf(infd, F_LOCK, 1)) {
+ carp("could not acquire lock!");
+ sleep(1);
+ }
+ if (argc == 2 && argv[1][1] != 'H') {
+ pid_t pid = __readpid(argv[1]);
+ if (buf[0] != '0') {
+ unsigned long len;
+ unsigned long ut = uptime(argv[1]);
- if (isatty(1)) {
- char tmp[FMT_ULONG+20];
- char tmp2[FMT_ULONG];
- char* what;
+ if (isatty(1)) {
+ char tmp[FMT_ULONG + 20];
+ char tmp2[FMT_ULONG];
+ char* what;
- if (pid==0) what="down "; else
- if (pid==1) what="finished "; else {
- len=fmt_str(tmp,"up (pid ");
- len+=fmt_ulong(tmp+len,pid);
- tmp[len+fmt_str(tmp+len,") ")]=0;
- what=tmp;
- }
- tmp2[fmt_ulong(tmp2,ut)]=0;
- msg(argv[1],": ",what,tmp2," seconds");
- } else {
- char tmp[FMT_ULONG*2+5];
- len=fmt_ulong(tmp,pid);
- tmp[len]=' '; ++len;
- len+=fmt_ulong(tmp+len,ut);
- tmp[len]='\n';
- write(1,tmp,len+1);
- }
+ if (pid == 0)
+ what = "down ";
+ else if (pid == 1)
+ what = "finished ";
+ else {
+ len = fmt_str(tmp, "up (pid ");
+ len += fmt_ulong(tmp + len, pid);
+ tmp[len + fmt_str(tmp + len, ") ")] = 0;
+ what = tmp;
+ }
+ tmp2[fmt_ulong(tmp2, ut)] = 0;
+ msg(argv[1], ": ", what, tmp2, " seconds");
+ } else {
+ char tmp[FMT_ULONG * 2 + 5];
+ len = fmt_ulong(tmp, pid);
+ tmp[len] = ' ';
+ ++len;
+ len += fmt_ulong(tmp + len, ut);
+ tmp[len] = '\n';
+ write(1, tmp, len + 1);
+ }
- if (pid==0) return 2; else if (pid==1) return 3; else return 0;
- } else
- carp(argv[1],": no such service.");
- return 1;
- } else {
- int i;
- int ret=0;
- int sig=0;
- pid_t pid;
- if (argv[1][0]=='-') {
- switch (argv[1][1]) {
- case 'g':
- for (i=2; i<argc; ++i) {
- pid=__readpid(argv[i]);
- if (pid<2) {
- carp(argv[i],pid==1?": service terminated":": no such service");
- ret=1;
- } else {
- char tmp[FMT_ULONG];
- int i;
- tmp[i=fmt_ulong(tmp,pid)]='\n';
- write(1,tmp,i+1);
- }
- }
- break;
- case 'p': sig=SIGSTOP; goto dokill; break;
- case 'c': sig=SIGCONT; goto dokill; break;
- case 'h': sig=SIGHUP; goto dokill; break;
- case 'a': sig=SIGALRM; goto dokill; break;
- case 'i': sig=SIGINT; goto dokill; break;
- case 't': sig=SIGTERM; goto dokill; break;
- case 'k': sig=SIGKILL; goto dokill; break;
- case 'o':
- for (i=2; i<argc; ++i)
- if (startservice(argv[i]) || respawn(argv[i],0)) {
- carp("Could not start ",argv[i]);
- ret=1;
- }
- break;
- case 'd':
- for (i=2; i<argc; ++i) {
- pid=__readpid(argv[i]);
- if (pid==0) {
- carp(argv[i],": no such service");
- ret=1;
- } else if (pid==1)
- continue;
- else
- if (respawn(argv[i],0) || kill(pid,SIGTERM) || kill(pid,SIGCONT))
- carp(argv[i],": failed to send signal");
- }
- break;
- case 'u':
- for (i=2; i<argc; ++i)
- if (startservice(argv[i]) || respawn(argv[i],1)) {
- carp("Could not start ",argv[i]);
- ret=1;
- }
- break;
- case 'C':
- for (i=2; i<argc; ++i)
- if (check_remove(argv[i])) {
- carp(argv[i]," could not be cleared");
- ret=1;
- }
- break;
- case 'P':
- pid=atoi(argv[1]+2);
- if (pid>1)
- if (setpid(argv[2],pid)) {
- carp("Could not set PID of service ",argv[2]);
- ret=1;
- }
- break;
- case 'H':
- dumphistory();
- break;
- case 'D':
- dumpdependencies(argv[2]);
- break;
- }
- }
- return ret;
-dokill:
- for (i=2; i<argc; i++) {
- pid=__readpid(argv[i]);
- if (!pid) {
- carp(argv[i],": no such service");
- ret=1;
- } else if (pid==1) {
- carp(argv[i],": service not running");
- ret=1;
- } else if (kill(pid,sig)) {
- char tmp[FMT_ULONG];
- char tmp2[FMT_ULONG];
- char* s;
- switch (errno) {
- case EINVAL: s="invalid signal"; break;
- case EPERM: s="permission denied"; break;
- case ESRCH: s="no such pid"; break;
- default: s="unknown error";
- }
- tmp[fmt_ulong(tmp,sig)]=0;
- tmp2[fmt_ulong(tmp2,pid)]=0;
- carp(argv[i],": could not send signal ",tmp," to PID ",tmp2,": ",s);
- ret=1;
+ if (pid == 0)
+ return 2;
+ else if (pid == 1)
+ return 3;
+ else
+ return 0;
+ } else
+ carp(argv[1], ": no such service.");
+ return 1;
+ } else {
+ int i;
+ int ret = 0;
+ int sig = 0;
+ pid_t pid;
+ if (argv[1][0] == '-') {
+ switch (argv[1][1]) {
+ case 'g':
+ for (i = 2; i < argc; ++i) {
+ pid = __readpid(argv[i]);
+ if (pid < 2) {
+ carp(argv[i], pid == 1 ? ": service terminated" : ": no such service");
+ ret = 1;
+ } else {
+ char tmp[FMT_ULONG];
+ int i;
+ tmp[i = fmt_ulong(tmp, pid)] = '\n';
+ write(1, tmp, i + 1);
+ }
+ }
+ break;
+ case 'p':
+ sig = SIGSTOP;
+ goto dokill;
+ break;
+ case 'c':
+ sig = SIGCONT;
+ goto dokill;
+ break;
+ case 'h':
+ sig = SIGHUP;
+ goto dokill;
+ break;
+ case 'a':
+ sig = SIGALRM;
+ goto dokill;
+ break;
+ case 'i':
+ sig = SIGINT;
+ goto dokill;
+ break;
+ case 't':
+ sig = SIGTERM;
+ goto dokill;
+ break;
+ case 'k':
+ sig = SIGKILL;
+ goto dokill;
+ break;
+ case 'o':
+ for (i = 2; i < argc; ++i)
+ if (startservice(argv[i]) || respawn(argv[i], 0)) {
+ carp("Could not start ", argv[i]);
+ ret = 1;
+ }
+ break;
+ case 'd':
+ for (i = 2; i < argc; ++i) {
+ pid = __readpid(argv[i]);
+ if (pid == 0) {
+ carp(argv[i], ": no such service");
+ ret = 1;
+ } else if (pid == 1)
+ continue;
+ else if (respawn(argv[i], 0) || kill(pid, SIGTERM) || kill(pid, SIGCONT))
+ carp(argv[i], ": failed to send signal");
+ }
+ break;
+ case 'u':
+ for (i = 2; i < argc; ++i)
+ if (startservice(argv[i]) || respawn(argv[i], 1)) {
+ carp("Could not start ", argv[i]);
+ ret = 1;
+ }
+ break;
+ case 'C':
+ for (i = 2; i < argc; ++i)
+ if (check_remove(argv[i])) {
+ carp(argv[i], " could not be cleared");
+ ret = 1;
+ }
+ break;
+ case 'P':
+ pid = atoi(argv[1] + 2);
+ if (pid > 1)
+ if (setpid(argv[2], pid)) {
+ carp("Could not set PID of service ", argv[2]);
+ ret = 1;
+ }
+ break;
+ case 'H':
+ dumphistory();
+ break;
+ case 'D':
+ dumpdependencies(argv[2]);
+ break;
+ }
+ }
+ return ret;
+ dokill:
+ for (i = 2; i < argc; i++) {
+ pid = __readpid(argv[i]);
+ if (!pid) {
+ carp(argv[i], ": no such service");
+ ret = 1;
+ } else if (pid == 1) {
+ carp(argv[i], ": service not running");
+ ret = 1;
+ } else if (kill(pid, sig)) {
+ char tmp[FMT_ULONG];
+ char tmp2[FMT_ULONG];
+ char* s;
+ switch (errno) {
+ case EINVAL:
+ s = "invalid signal";
+ break;
+ case EPERM:
+ s = "permission denied";
+ break;
+ case ESRCH:
+ s = "no such pid";
+ break;
+ default:
+ s = "unknown error";
+ }
+ tmp[fmt_ulong(tmp, sig)] = 0;
+ tmp2[fmt_ulong(tmp2, pid)] = 0;
+ carp(argv[i], ": could not send signal ", tmp, " to PID ", tmp2, ": ", s);
+ ret = 1;
+ }
+ }
+ return ret;
+ }
+ } else {
+ carp("minit: could not open " MINITROOT "/in or " MINITROOT "/out");
+ return 1;
}
- }
- return ret;
- }
- } else {
- carp("minit: could not open " MINITROOT "/in or " MINITROOT "/out");
- return 1;
- }
}
diff --git a/opendevconsole.c b/opendevconsole.c
@@ -1,17 +1,18 @@
-#include <fcntl.h>
-#include <unistd.h>
#include "write12.h"
+
+#include <fcntl.h>
#include <stdlib.h>
+#include <unistd.h>
void opendevconsole() {
- int fd;
- if ((fd=open("/dev/console",O_RDWR|O_NOCTTY))>=0) {
- if (dup2(fd,0) != 0 ||
- dup2(fd,1) != 1 ||
- dup2(fd,2) != 2) {
- __write2("dup2 failed unexpectedly.\n");
- exit(111);
- }
- if (fd>2) close(fd);
- }
+ int fd;
+ if ((fd = open("/dev/console", O_RDWR | O_NOCTTY)) >= 0) {
+ if (dup2(fd, 0) != 0 ||
+ dup2(fd, 1) != 1 ||
+ dup2(fd, 2) != 2) {
+ __write2("dup2 failed unexpectedly.\n");
+ exit(111);
+ }
+ if (fd > 2) close(fd);
+ }
}
diff --git a/openreadclose.c b/openreadclose.c
@@ -1,28 +1,28 @@
#ifndef EMBEDDED
-#define _FILE_OFFSET_BITS 64
-#include <unistd.h>
-#include <fcntl.h>
-#include <stdlib.h>
+# define _FILE_OFFSET_BITS 64
+# include <fcntl.h>
+# include <stdlib.h>
+# include <unistd.h>
#endif
-int openreadclose(char *fn, char **buf, size_t *len) {
- int fd=open(fn,O_RDONLY);
- if (fd<0) return -1;
- if (!*buf) {
- off_t o = lseek(fd, 0, SEEK_END);
- if (o < 0 || o > 0x7fffffff) goto error; // impose sanity limits
- *len = o;
- *buf=(char*)malloc(*len+1);
- if (!*buf) {
-error:
- close(fd);
- return -1;
- }
- }
- *len=pread(fd,*buf,*len,0);
- if (*len == (unsigned long)-1)
- (*buf)[0]=0;
- else
- (*buf)[*len]=0;
- return close(fd);
+int openreadclose(char* fn, char** buf, size_t* len) {
+ int fd = open(fn, O_RDONLY);
+ if (fd < 0) return -1;
+ if (!*buf) {
+ off_t o = lseek(fd, 0, SEEK_END);
+ if (o < 0 || o > 0x7fffffff) goto error; // impose sanity limits
+ *len = o;
+ *buf = (char*) malloc(*len + 1);
+ if (!*buf) {
+ error:
+ close(fd);
+ return -1;
+ }
+ }
+ *len = pread(fd, *buf, *len, 0);
+ if (*len == (unsigned long) -1)
+ (*buf)[0] = 0;
+ else
+ (*buf)[*len] = 0;
+ return close(fd);
}
diff --git a/pidfilehack.c b/pidfilehack.c
@@ -1,8 +1,8 @@
-#include <unistd.h>
#include <errno.h>
-#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
/* purpose: argv[1] is the full path to a PID file,
* argv+2 is the daemon to run.
@@ -13,50 +13,50 @@
extern char** environ;
int main(int argc, char* argv[]) {
- int count=0;
- if (argc<3) {
- write(1,"usage: pidfilehack service /var/run/daemon.pid /usr/sbin/daemon args...\n",72);
- return 0;
- }
- if (unlink(argv[2])) {
- if (errno!=ENOENT) {
- perror("could not remove pid file");
- return 1;
- }
- }
- switch (fork()) {
- case -1:
- perror("could not fork");
- return 2;
- case 0: /* child */
- execve(argv[3],argv+3,environ);
- perror("execvp failed");
- return 3;
- }
- do {
- int fd=open(argv[2],O_RDONLY);
- if (fd>=0) {
- static char buf[100] = "-P";
- int len=read(fd,buf+2,98);
- close(fd);
- if (len>0) {
- char* _argv[] = { "msvc", 0, 0, 0 };
- if (buf[len+1]=='\n')
- buf[len+1]=0;
- else
- buf[len+2]=0;
- _argv[1]=buf;
- _argv[2]=argv[1];
-/* printf("execvp %s %s %s\n",_argv[0],_argv[1],_argv[2]); */
- execvp(_argv[0],_argv);
- perror("execvp failed");
- return 0;
- } /* else
- printf("file there but open returned %d\n",fd); */
- } /* else
- printf("%s not there yet\n",argv[2]); */
- sleep(1);
- if (++count>=30)
- exit(0);
- } while (1);
+ int count = 0;
+ if (argc < 3) {
+ write(1, "usage: pidfilehack service /var/run/daemon.pid /usr/sbin/daemon args...\n", 72);
+ return 0;
+ }
+ if (unlink(argv[2])) {
+ if (errno != ENOENT) {
+ perror("could not remove pid file");
+ return 1;
+ }
+ }
+ switch (fork()) {
+ case -1:
+ perror("could not fork");
+ return 2;
+ case 0: /* child */
+ execve(argv[3], argv + 3, environ);
+ perror("execvp failed");
+ return 3;
+ }
+ do {
+ int fd = open(argv[2], O_RDONLY);
+ if (fd >= 0) {
+ static char buf[100] = "-P";
+ int len = read(fd, buf + 2, 98);
+ close(fd);
+ if (len > 0) {
+ char* _argv[] = { "msvc", 0, 0, 0 };
+ if (buf[len + 1] == '\n')
+ buf[len + 1] = 0;
+ else
+ buf[len + 2] = 0;
+ _argv[1] = buf;
+ _argv[2] = argv[1];
+ /* printf("execvp %s %s %s\n",_argv[0],_argv[1],_argv[2]); */
+ execvp(_argv[0], _argv);
+ perror("execvp failed");
+ return 0;
+ } /* else
+ printf("file there but open returned %d\n",fd); */
+ } /* else
+ printf("%s not there yet\n",argv[2]); */
+ sleep(1);
+ if (++count >= 30)
+ exit(0);
+ } while (1);
}
diff --git a/powersave.c b/powersave.c
@@ -1,87 +1,86 @@
#define _GNU_SOURCE
-#include <sys/types.h>
-#include <string.h>
-#include <stdlib.h>
#include <dirent.h>
+#include <libowfat/open.h>
+#include <linux/sockios.h>
+#include <net/if.h>
#include <netinet/in.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
#include <sys/socket.h>
-#include <net/if.h>
+#include <sys/types.h>
#include <unistd.h>
-#include <linux/sockios.h>
-#include <sys/ioctl.h>
-
-#include <libowfat/open.h>
-int write_to(const char* filename,const char* string) {
- int fd=open_write(filename);
- if (fd!=-1) {
- write(fd,string,strlen(string));
- fd=close(fd);
- }
- return fd;
+int write_to(const char* filename, const char* string) {
+ int fd = open_write(filename);
+ if (fd != -1) {
+ write(fd, string, strlen(string));
+ fd = close(fd);
+ }
+ return fd;
}
void handle(const char* s) {
- size_t l=strlen(s);
- char* x=alloca(l+20);
- strcpy(x,s);
- strcpy(x+l,"/power/control");
- write_to(x,"auto");
+ size_t l = strlen(s);
+ char* x = alloca(l + 20);
+ strcpy(x, s);
+ strcpy(x + l, "/power/control");
+ write_to(x, "auto");
}
void dwim(const char* name) {
- DIR* D;
- struct dirent* d;
- if (chdir(name)) return;
- if (!(D=opendir("."))) return;
- while ((d=readdir(D))) {
-// puts(d->d_name);
- handle(d->d_name);
- }
- closedir(D);
+ DIR* D;
+ struct dirent* d;
+ if (chdir(name)) return;
+ if (!(D = opendir("."))) return;
+ while ((d = readdir(D))) {
+ // puts(d->d_name);
+ handle(d->d_name);
+ }
+ closedir(D);
}
void pci() {
- dwim("/sys/bus/pci/devices");
+ dwim("/sys/bus/pci/devices");
}
void usb() {
- dwim("/sys/bus/usb/devices");
+ dwim("/sys/bus/usb/devices");
}
void wakeonlan(const char* interface) {
- struct ethtool_wolinfo {
- uint32_t cmd,supported,wolopts;
- char sopass[6];
- } wol;
- int fd=socket(PF_INET,SOCK_DGRAM,0);
- struct ifreq ifr;
- if (strlen(interface) > IFNAMSIZ)
- return;
- memset(&ifr,0,sizeof(ifr));
- strncpy(ifr.ifr_name,interface,sizeof(ifr.ifr_name));
- ifr.ifr_data=(void*)&wol;
- memset(&wol,0,sizeof(wol));
- wol.cmd=6; // ETHTOOL_SWOL
- wol.wolopts=0;
- ioctl(fd,SIOCETHTOOL,&ifr);
- close(fd);
+ struct ethtool_wolinfo {
+ uint32_t cmd, supported, wolopts;
+ char sopass[6];
+ } wol;
+ int fd = socket(PF_INET, SOCK_DGRAM, 0);
+ struct ifreq ifr;
+ if (strlen(interface) > IFNAMSIZ)
+ return;
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
+ ifr.ifr_data = (void*) &wol;
+ memset(&wol, 0, sizeof(wol));
+ wol.cmd = 6; // ETHTOOL_SWOL
+ wol.wolopts = 0;
+ ioctl(fd, SIOCETHTOOL, &ifr);
+ close(fd);
}
int main() {
- write_to("/sys/class/scsi_host/host0/link_power_management_policy","min_power");
- write_to("/sys/class/scsi_host/host1/link_power_management_policy","min_power");
- write_to("/sys/class/scsi_host/host2/link_power_management_policy","min_power");
- write_to("/sys/class/scsi_host/host3/link_power_management_policy","min_power");
- write_to("/sys/class/scsi_host/host4/link_power_management_policy","min_power");
- write_to("/sys/class/scsi_host/host5/link_power_management_policy","min_power");
- write_to("/sys/class/scsi_host/host6/link_power_management_policy","min_power");
- write_to("/sys/module/snd_hda_intel/parameters/power_save","1");
- write_to("/sys/bus/usb/devices/1-3/power/control","auto");
- write_to("/sys/bus/usb/devices/1-4/power/control","auto");
- write_to("/proc/sys/vm/dirty_writeback_centisecs","1500");
- pci();
- wakeonlan("eth0");
-// usb();
- return 0;
+ write_to("/sys/class/scsi_host/host0/link_power_management_policy", "min_power");
+ write_to("/sys/class/scsi_host/host1/link_power_management_policy", "min_power");
+ write_to("/sys/class/scsi_host/host2/link_power_management_policy", "min_power");
+ write_to("/sys/class/scsi_host/host3/link_power_management_policy", "min_power");
+ write_to("/sys/class/scsi_host/host4/link_power_management_policy", "min_power");
+ write_to("/sys/class/scsi_host/host5/link_power_management_policy", "min_power");
+ write_to("/sys/class/scsi_host/host6/link_power_management_policy", "min_power");
+ write_to("/sys/module/snd_hda_intel/parameters/power_save", "1");
+ write_to("/sys/bus/usb/devices/1-3/power/control", "auto");
+ write_to("/sys/bus/usb/devices/1-4/power/control", "auto");
+ write_to("/proc/sys/vm/dirty_writeback_centisecs", "1500");
+ pci();
+ wakeonlan("eth0");
+ // usb();
+ return 0;
}
diff --git a/serdo.c b/serdo.c
@@ -1,241 +1,272 @@
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
#include <ctype.h>
-#include <sys/wait.h>
-#include <sys/resource.h>
-#include <string.h>
-
-#include <libowfat/str.h>
+#include <fcntl.h>
#include <libowfat/byte.h>
-#include <libowfat/scan.h>
#include <libowfat/errmsg.h>
+#include <libowfat/scan.h>
+#include <libowfat/str.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
extern char** environ;
#define MAXENV 256
-char* envp[MAXENV+2];
-int envc;
+char* envp[MAXENV + 2];
+int envc;
int continueonerror;
int envset(char* s) {
- int i,l;
- if (s[l=str_chr(s,'=')]!='=') return -1;
- ++l;
- for (i=0; i<envc; ++i)
- if (byte_equal(envp[i],l,s)) {
- envp[i]=s;
- return 0;
- }
- if (envc<MAXENV) {
- envp[envc]=s;
- envp[++envc]=0;
- return 0;
- }
- return -1;
+ int i, l;
+ if (s[l = str_chr(s, '=')] != '=') return -1;
+ ++l;
+ for (i = 0; i < envc; ++i)
+ if (byte_equal(envp[i], l, s)) {
+ envp[i] = s;
+ return 0;
+ }
+ if (envc < MAXENV) {
+ envp[envc] = s;
+ envp[++envc] = 0;
+ return 0;
+ }
+ return -1;
}
int spawn(char** argv, int last) {
- int i,ignore;
- ignore=(argv[0][0]=='-');
- if (ignore) ++argv[0];
- if (str_equal(argv[0],"cd")) {
- if (chdir(argv[1])==-1) {
- carpsys("chdir failed");
-failornot:
- return ignore?0:-1;
- }
- return 0;
- } else if (str_equal(argv[0],"export")) {
- for (i=1; argv[i]; ++i) envset(argv[i]);
- return 0;
- } else if (str_equal(argv[0],"exec")) {
- ++argv;
- last=1;
- } else if (str_equal(argv[0],"ulimit")) {
- struct rlimit rl;
- for (i=1; argv[i] && argv[i+1]; i+=2) {
- int id=-1;
- if (argv[i][0]!='-') {
-ulimitsyntax:
- carp("ulimit syntax error: ",argv[i]);
- continue;
- }
- switch(argv[i][1]) {
- case 'c': id=RLIMIT_CORE; break;
- case 'd': id=RLIMIT_DATA; break;
- case 'e': id=RLIMIT_NICE; break;
- case 'f': id=RLIMIT_FSIZE; break;
- case 'i': id=RLIMIT_SIGPENDING; break;
- case 'l': id=RLIMIT_MEMLOCK; break;
- case 'm': id=RLIMIT_RSS; break;
- case 'n': id=RLIMIT_NOFILE; break;
- case 'r': id=RLIMIT_RTPRIO; break;
- case 's': id=RLIMIT_STACK; break;
- case 't': id=RLIMIT_CPU; break;
- case 'u': id=RLIMIT_NPROC; break;
- case 'v': id=RLIMIT_AS; break;
- case 'x': id=RLIMIT_LOCKS; break;
- default: goto ulimitsyntax;
- }
- if (!strcmp(argv[i+1],"unlimited")) {
- rl.rlim_cur=rl.rlim_max=RLIM_INFINITY;
- } else {
- unsigned long ul;
- if (argv[i+1][scan_ulong(argv[i+1],&ul)])
- goto ulimitsyntax;
- rl.rlim_cur=rl.rlim_max=ul;
- }
- if (setrlimit(id,&rl)==-1) {
- carpsys("ulimit failed");
- goto failornot;
- }
- }
- return 0;
- }
- if (!last) {
- if ((i=fork())==-1) diesys(1,"cannot fork");
- } else i=0;
- if (!i) {
- /* child */
- environ=envp;
- _exit(execvp(argv[0],argv));
- }
- if (waitpid(i,&i,0)==-1) diesys(1,"waitpid failed");
- if (ignore) return 0;
- if (!WIFEXITED(i))
- return -1;
- return WEXITSTATUS(i);
+ int i, ignore;
+ ignore = (argv[0][0] == '-');
+ if (ignore) ++argv[0];
+ if (str_equal(argv[0], "cd")) {
+ if (chdir(argv[1]) == -1) {
+ carpsys("chdir failed");
+ failornot:
+ return ignore ? 0 : -1;
+ }
+ return 0;
+ } else if (str_equal(argv[0], "export")) {
+ for (i = 1; argv[i]; ++i) envset(argv[i]);
+ return 0;
+ } else if (str_equal(argv[0], "exec")) {
+ ++argv;
+ last = 1;
+ } else if (str_equal(argv[0], "ulimit")) {
+ struct rlimit rl;
+ for (i = 1; argv[i] && argv[i + 1]; i += 2) {
+ int id = -1;
+ if (argv[i][0] != '-') {
+ ulimitsyntax:
+ carp("ulimit syntax error: ", argv[i]);
+ continue;
+ }
+ switch (argv[i][1]) {
+ case 'c':
+ id = RLIMIT_CORE;
+ break;
+ case 'd':
+ id = RLIMIT_DATA;
+ break;
+ case 'e':
+ id = RLIMIT_NICE;
+ break;
+ case 'f':
+ id = RLIMIT_FSIZE;
+ break;
+ case 'i':
+ id = RLIMIT_SIGPENDING;
+ break;
+ case 'l':
+ id = RLIMIT_MEMLOCK;
+ break;
+ case 'm':
+ id = RLIMIT_RSS;
+ break;
+ case 'n':
+ id = RLIMIT_NOFILE;
+ break;
+ case 'r':
+ id = RLIMIT_RTPRIO;
+ break;
+ case 's':
+ id = RLIMIT_STACK;
+ break;
+ case 't':
+ id = RLIMIT_CPU;
+ break;
+ case 'u':
+ id = RLIMIT_NPROC;
+ break;
+ case 'v':
+ id = RLIMIT_AS;
+ break;
+ case 'x':
+ id = RLIMIT_LOCKS;
+ break;
+ default:
+ goto ulimitsyntax;
+ }
+ if (!strcmp(argv[i + 1], "unlimited")) {
+ rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
+ } else {
+ unsigned long ul;
+ if (argv[i + 1][scan_ulong(argv[i + 1], &ul)])
+ goto ulimitsyntax;
+ rl.rlim_cur = rl.rlim_max = ul;
+ }
+ if (setrlimit(id, &rl) == -1) {
+ carpsys("ulimit failed");
+ goto failornot;
+ }
+ }
+ return 0;
+ }
+ if (!last) {
+ if ((i = fork()) == -1) diesys(1, "cannot fork");
+ } else
+ i = 0;
+ if (!i) {
+ /* child */
+ environ = envp;
+ _exit(execvp(argv[0], argv));
+ }
+ if (waitpid(i, &i, 0) == -1) diesys(1, "waitpid failed");
+ if (ignore) return 0;
+ if (!WIFEXITED(i))
+ return -1;
+ return WEXITSTATUS(i);
}
-int run(char* s,int last) {
- int i,spaces;
- char** argv,**next;
- for (i=spaces=0; s[i]; ++i) if (s[i]==' ') ++spaces;
- next=argv=alloca((spaces+2)*sizeof(char*));
+int run(char* s, int last) {
+ int i, spaces;
+ char **argv, **next;
+ for (i = spaces = 0; s[i]; ++i)
+ if (s[i] == ' ') ++spaces;
+ next = argv = alloca((spaces + 2) * sizeof(char*));
- while (*s) {
- while (*s && isspace(*s)) ++s;
- if (*s=='"') {
- ++s;
- *next=s;
- while (*s && (*s != '"' || s[-1] == '\\')) ++s;
- if (!*s) {
- --*next;
- break;
- }
- *s=0;
- ++s;
- } else if (*s=='\'') {
- ++s;
- *next=s;
- while (*s && (*s != '\'' || s[-1] == '\\')) ++s;
- if (!*s) {
- --*next;
- break;
- }
- *s=0;
- ++s;
- } else {
- *next=s;
- while (*s && *s!=' ' && *s!='\t') ++s;
- if (*s) {
- *s=0;
- ++s;
- }
- }
- ++next;
- }
- *next=0;
+ while (*s) {
+ while (*s && isspace(*s)) ++s;
+ if (*s == '"') {
+ ++s;
+ *next = s;
+ while (*s && (*s != '"' || s[-1] == '\\')) ++s;
+ if (!*s) {
+ --*next;
+ break;
+ }
+ *s = 0;
+ ++s;
+ } else if (*s == '\'') {
+ ++s;
+ *next = s;
+ while (*s && (*s != '\'' || s[-1] == '\\')) ++s;
+ if (!*s) {
+ --*next;
+ break;
+ }
+ *s = 0;
+ ++s;
+ } else {
+ *next = s;
+ while (*s && *s != ' ' && *s != '\t') ++s;
+ if (*s) {
+ *s = 0;
+ ++s;
+ }
+ }
+ ++next;
+ }
+ *next = 0;
- return spawn(argv,last);
+ return spawn(argv, last);
}
-int execute(char* s,int dontexeclast) {
- char* start;
- int r;
- r=0;
- while (*s) {
- int last;
- while (isspace(*s)) ++s;
- if (*s == '#') {
- while (*s && *s != '\n') ++s;
- continue;
- }
- start=s;
+int execute(char* s, int dontexeclast) {
+ char* start;
+ int r;
+ r = 0;
+ while (*s) {
+ int last;
+ while (isspace(*s)) ++s;
+ if (*s == '#') {
+ while (*s && *s != '\n') ++s;
+ continue;
+ }
+ start = s;
- while (*s && *s != '\n') ++s;
- /* advance to the end of the line */
- if (*s) {
- /* not the last line in the file */
- char* tmp;
- *s=0;
- ++s;
- /* If it's the last command in the file, we do not fork+exec but
- * we execve it directly. So we need to find out here if this is
- * the last command in the file. For that we need to skip all the
- * lines after it that are comments */
- for (tmp=s; *tmp; ++tmp)
- if (!isspace(*tmp) && *tmp=='#') {
- for (++tmp; *tmp && *tmp!='\n'; ++tmp) ;
- } else
- break;
- last=(*tmp==0);
- } else
- last=1;
- if (dontexeclast) last=0;
- r=run(start,last);
- if (r!=0 && !continueonerror)
- break;
- }
- return r;
+ while (*s && *s != '\n') ++s;
+ /* advance to the end of the line */
+ if (*s) {
+ /* not the last line in the file */
+ char* tmp;
+ *s = 0;
+ ++s;
+ /* If it's the last command in the file, we do not fork+exec but
+ * we execve it directly. So we need to find out here if this is
+ * the last command in the file. For that we need to skip all the
+ * lines after it that are comments */
+ for (tmp = s; *tmp; ++tmp)
+ if (!isspace(*tmp) && *tmp == '#') {
+ for (++tmp; *tmp && *tmp != '\n'; ++tmp)
+ ;
+ } else
+ break;
+ last = (*tmp == 0);
+ } else
+ last = 1;
+ if (dontexeclast) last = 0;
+ r = run(start, last);
+ if (r != 0 && !continueonerror)
+ break;
+ }
+ return r;
}
-int batch(char* s,int dontexeclast) {
- struct stat ss;
- int fd=open(s,O_RDONLY);
- char* map;
- if (fd==-1) diesys(1,"could not open ",s);
- if (fstat(fd,&ss)==-1) diesys(1,"could not stat ",s);
- if (ss.st_size>32768) die(1,"file ",s," is too large");
- map=alloca(ss.st_size+1);
- if (read(fd,map,ss.st_size)!=(long)ss.st_size) diesys(1,"read error");
- map[ss.st_size]=0;
- close(fd);
+int batch(char* s, int dontexeclast) {
+ struct stat ss;
+ int fd = open(s, O_RDONLY);
+ char* map;
+ if (fd == -1) diesys(1, "could not open ", s);
+ if (fstat(fd, &ss) == -1) diesys(1, "could not stat ", s);
+ if (ss.st_size > 32768) die(1, "file ", s, " is too large");
+ map = alloca(ss.st_size + 1);
+ if (read(fd, map, ss.st_size) != (long) ss.st_size) diesys(1, "read error");
+ map[ss.st_size] = 0;
+ close(fd);
- return execute(map,dontexeclast);
+ return execute(map, dontexeclast);
}
-int main(int argc,char* argv[],char* env[]) {
- static char* fakeargv[]={0,"script",0};
- int r;
- int failing=0;
- int havefail=!access("fail",O_RDONLY);
- (void)argc;
- if (argc<2) {
- if (!access("script",O_RDONLY))
- argv=fakeargv;
- else
- die(1,"usage: serdo [-c] filename");
- }
- errmsg_iam("serdo");
- for (envc=0; envc<MAXENV && env[envc]; ++envc) envp[envc]=env[envc];
- envp[envc]=0;
- if (str_equal(argv[1],"-c")) {
- continueonerror=1;
- ++argv;
- }
- while (*++argv) {
- if ((r=batch(*argv,!!argv[1] || (havefail && !failing)))) {
- if (!failing && havefail) {
- fakeargv[1]="fail";
- argv=fakeargv;
- failing=1;
- continue;
- }
- return r;
- }
- }
- return 0;
+int main(int argc, char* argv[], char* env[]) {
+ static char* fakeargv[] = { 0, "script", 0 };
+ int r;
+ int failing = 0;
+ int havefail = !access("fail", O_RDONLY);
+ (void) argc;
+ if (argc < 2) {
+ if (!access("script", O_RDONLY))
+ argv = fakeargv;
+ else
+ die(1, "usage: serdo [-c] filename");
+ }
+ errmsg_iam("serdo");
+ for (envc = 0; envc < MAXENV && env[envc]; ++envc) envp[envc] = env[envc];
+ envp[envc] = 0;
+ if (str_equal(argv[1], "-c")) {
+ continueonerror = 1;
+ ++argv;
+ }
+ while (*++argv) {
+ if ((r = batch(*argv, !!argv[1] || (havefail && !failing)))) {
+ if (!failing && havefail) {
+ fakeargv[1] = "fail";
+ argv = fakeargv;
+ failing = 1;
+ continue;
+ }
+ return r;
+ }
+ }
+ return 0;
}
diff --git a/shutdown.c b/shutdown.c
@@ -1,280 +1,284 @@
/*
* Notes:
* - uncomment `#define ALLOW_SUID' below if you want users other than
- * root to be able to shut down the system.
- * - after compiling, install under /sbin/shutdown with chgrp adm and
+ * root to be able to shut down the system.
+ * - after compiling, install under /sbin/shutdown with chgrp adm and
* chmod 4750 for SUID root, or 0700 for root only
* - uncomment `#define USE_MINIT' below if you want to use shutdown
- * with minit. If defined, shutdown will try to bring down the services
+ * with minit. If defined, shutdown will try to bring down the services
* halt (for -h or -o) or reboot (-r) before killing all other processes.
- * Please make sure that you have a depends in reboot and halt that
+ * Please make sure that you have a depends in reboot and halt that
* will bring down all respawning services. A respawning service during
* shutdown might cause you to wait for a fsck during the next boot
* - If you do not use minit shutdown will bring your system down similar
* to SysV-Inits shutdown with -n
*
- * TODO:
+ * TODO:
* - add a function for wall-messages
* - cleanup
*/
-#include <sys/types.h>
-#include <sys/reboot.h>
-#include <signal.h>
#include <fcntl.h>
+#include <libowfat/str.h>
+#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>
+#include <sys/reboot.h>
+#include <sys/types.h>
#include <sys/wait.h>
-#include <libowfat/str.h>
-
#ifdef __dietlibc__
-#include <write12.h>
+# include <write12.h>
#else
-#include <unistd.h>
-static inline int __write1(const char*s) { return write(1,s,strlen(s)); }
-static inline int __write2(const char*s) { return write(2,s,strlen(s)); }
+# include <unistd.h>
+static inline int __write1(const char* s) { return write(1, s, strlen(s)); }
+static inline int __write2(const char* s) { return write(2, s, strlen(s)); }
#endif
#define ALLOW_SUID
#define USE_MINIT
#ifdef USE_MINIT
-#define NOVARS
-#include "minit.h"
+# define NOVARS
+# include "minit.h"
#endif
extern void opendevconsole();
-extern char **environ;
-extern int openreadclose(char *fn, char **buf, size_t *len);
-extern char **split(char *buf,int c,size_t* len,size_t plus,size_t ofs);
-extern char *optarg;
+extern char** environ;
+extern int openreadclose(char* fn, char** buf, size_t* len);
+extern char** split(char* buf, int c, size_t* len, size_t plus, size_t ofs);
+extern char* optarg;
-void wall(char *buf) {
- __write2(buf);
+void wall(char* buf) {
+ __write2(buf);
}
-int exec_cmd(char *cmd, ...) {
- char *argv[10];
- va_list arguments;
- pid_t pid;
- int i;
-
- va_start(arguments, cmd);
- for (i=0;i<9 && (argv[i] = va_arg(arguments, char *)) != NULL; i++);
- argv[i] = NULL;
- va_end(arguments);
- pid = fork();
- if (pid < 0) return -1;
- if (pid > 0) {
- wait(NULL);
- } else {
- execve(cmd, argv, environ);
- //perror("execvp failed");
- exit(0);
- }
- return 0;
+int exec_cmd(char* cmd, ...) {
+ char* argv[10];
+ va_list arguments;
+ pid_t pid;
+ int i;
+
+ va_start(arguments, cmd);
+ for (i = 0; i < 9 && (argv[i] = va_arg(arguments, char*)) != NULL; i++)
+ ;
+ argv[i] = NULL;
+ va_end(arguments);
+ pid = fork();
+ if (pid < 0) return -1;
+ if (pid > 0) {
+ wait(NULL);
+ } else {
+ execve(cmd, argv, environ);
+ // perror("execvp failed");
+ exit(0);
+ }
+ return 0;
}
#ifdef USE_MINIT
-static int infd, outfd;
+static int infd, outfd;
static char buf[1500];
-int minit_serviceDown(char *service) {
- char *s=0;
- unsigned long len;
- pid_t pid=0;
-
- if (!service || !*service) return 0;
- if (chdir(MINITROOT) || chdir(service)) return -1;
-
- if (!openreadclose("depends", &s, &len)) {
- char **deps;
- size_t depc, i;
- deps=split(s, '\n', &depc, 0, 0);
- for (i=0; i<depc; i++) {
- if (deps[i][0] == '#') continue;
- minit_serviceDown(deps[i]);
- }
- }
-
- // get the pid
- buf[0]='p';
- strncpy(buf+1, service, 1400);
- write(infd, buf, strlen(buf));
- len=read(outfd, buf, 1500);
- if (len != 0) {
- buf[len]=0;
- pid = atoi(buf);
- }
-
- if (strcmp("reboot",service) && strcmp("halt",service) && pid > 1) {
- int i;
- __write2("\t--> "); __write2(service);
- buf[0]='r'; // we want to disable respawning first
- strncpy(buf+1, service, 1400);
- buf[1400]=0;
- write(infd, buf, strlen(buf));
- if (read(outfd, buf, 1500) < 1)
- __write2("\t(status read failed)");
- i=kill(pid, SIGTERM);
- if (i == 0) __write2("\t\tdone\n");
- else __write2("\t\tfailed\n");
- }
- return 0;
+int minit_serviceDown(char* service) {
+ char* s = 0;
+ unsigned long len;
+ pid_t pid = 0;
+
+ if (!service || !*service) return 0;
+ if (chdir(MINITROOT) || chdir(service)) return -1;
+
+ if (!openreadclose("depends", &s, &len)) {
+ char** deps;
+ size_t depc, i;
+ deps = split(s, '\n', &depc, 0, 0);
+ for (i = 0; i < depc; i++) {
+ if (deps[i][0] == '#') continue;
+ minit_serviceDown(deps[i]);
+ }
+ }
+
+ // get the pid
+ buf[0] = 'p';
+ strncpy(buf + 1, service, 1400);
+ write(infd, buf, strlen(buf));
+ len = read(outfd, buf, 1500);
+ if (len != 0) {
+ buf[len] = 0;
+ pid = atoi(buf);
+ }
+
+ if (strcmp("reboot", service) && strcmp("halt", service) && pid > 1) {
+ int i;
+ __write2("\t--> ");
+ __write2(service);
+ buf[0] = 'r'; // we want to disable respawning first
+ strncpy(buf + 1, service, 1400);
+ buf[1400] = 0;
+ write(infd, buf, strlen(buf));
+ if (read(outfd, buf, 1500) < 1)
+ __write2("\t(status read failed)");
+ i = kill(pid, SIGTERM);
+ if (i == 0)
+ __write2("\t\tdone\n");
+ else
+ __write2("\t\tfailed\n");
+ }
+ return 0;
}
int minit_shutdown(int level) {
- int retval;
-
- __write2("Shutting down minit services: \n");
- infd=open(MINITROOT "/in", O_WRONLY);
- outfd=open(MINITROOT "/out", O_RDONLY);
- if (infd>=0) {
- while (lockf(infd, F_TLOCK, 1)) {
- __write2("could not acquire lock!\n");
- sleep(1);
- }
- }
-
- retval=minit_serviceDown(level?"halt":"reboot");
- close(infd); close(outfd);
- return retval;
+ int retval;
+
+ __write2("Shutting down minit services: \n");
+ infd = open(MINITROOT "/in", O_WRONLY);
+ outfd = open(MINITROOT "/out", O_RDONLY);
+ if (infd >= 0) {
+ while (lockf(infd, F_TLOCK, 1)) {
+ __write2("could not acquire lock!\n");
+ sleep(1);
+ }
+ }
+
+ retval = minit_serviceDown(level ? "halt" : "reboot");
+ close(infd);
+ close(outfd);
+ return retval;
}
#endif
void printUsage() {
- __write2("usage: shutdown -[rhosmn] -[t secs]\n"
- "\t -r: reboot after shutdown\n"
- "\t -h: halt after shutdown\n"
- "\t -o: power-off after shutdown\n"
- "\t -s: single-user console after shutdown\n"
- "\t -m: only shutdown the minit-part\n"
- "\t -n: do not shutdown services using minit\n"
- "\t -t secs: delay between SIGTERM and SIGKILL\n");
+ __write2("usage: shutdown -[rhosmn] -[t secs]\n"
+ "\t -r: reboot after shutdown\n"
+ "\t -h: halt after shutdown\n"
+ "\t -o: power-off after shutdown\n"
+ "\t -s: single-user console after shutdown\n"
+ "\t -m: only shutdown the minit-part\n"
+ "\t -n: do not shutdown services using minit\n"
+ "\t -t secs: delay between SIGTERM and SIGKILL\n");
}
-int main(int argc, char *const argv[]) {
- int c;
- int cfg_downlevel=2;
- /* 0: reboot
- * 1: halt
- * 2: power off
- */
- unsigned int cfg_delay = 3;
- int cfg_minitonly = 0;
- int cfg_sulogin = 0;
-
- #ifdef ALLOW_SUID
- if (setuid(geteuid()) == -1) {
- __write2("setuid(geteuid()) failed.\n");
- return 111;
- }
- #endif
- if (getuid() != 0) {
- __write2("you are not root, go away!\n");
- return 1;
- }
-
- if (argc<2) {
- printUsage();
- return 0;
- }
-
- /* parse commandline options */
- while((c = getopt(argc, argv, "rhosmnt:")) != EOF) {
- switch(c) {
- case 'r': /* do we have to reboot... */
- cfg_downlevel = 0;
- break;
- case 'h': /* ...halt.. */
- cfg_downlevel = 1;
- break;
- case 's': /* rescue system */
- cfg_sulogin = 1;
- break;
- case 'm': /* exit after minit down */
- cfg_minitonly = 1;
- break;
- case 'o': /* ..or power off? */
- cfg_downlevel = 2;
- break;
- case 't': /* delay between SIGTERM and SIGKILL */
- cfg_delay = atoi(optarg);
- break;
- default:
- printUsage();
- return 1;
- }
- }
-
- opendevconsole();
-
- switch (cfg_downlevel) {
- case 0:
- wall("system is going down for reboot NOW\n");
- break;
- case 1:
- wall("system is going down for system halt NOW\n");
- break;
- case 2:
- wall("system is going down for power-off NOW\n");
- break;
- }
-
- /*
- * catch some signals;
- * getting killed after killing the controlling terminal wouldn't be
- * such a great thing...
- */
- signal(SIGQUIT, SIG_IGN);
- signal(SIGCHLD, SIG_IGN);
- signal(SIGHUP, SIG_IGN);
- signal(SIGTSTP, SIG_IGN);
- signal(SIGTTIN, SIG_IGN);
- signal(SIGTTOU, SIG_IGN);
-
- // real shutdown? then lets rock..
- #ifdef USE_MINIT
- minit_shutdown(cfg_downlevel);
- if (cfg_minitonly) return 0;
- #endif
-
- /* kill all processes still left */
- __write2("sending all processes the TERM signal...\n");
- kill(-1, SIGTERM);
- sleep(cfg_delay);
-
- __write2("sending all processes the KILL signal...\n");
- kill(-1, SIGKILL);
-
- if (cfg_sulogin) {
- char* suargs[]={"sulogin",0};
- execve("/sbin/sulogin", suargs, 0);
- __write2("execve() /sbin/sulogin failed\n");
- return 1;
- }
-
- /* sync buffers */
- sync();
-
- exec_cmd("/sbin/swapoff", "swapoff", "-a", (char *) 0);
- exec_cmd("/bin/umount", "umount", "-a", (char *) 0);
- exec_cmd("/bin/mount", "mount", "-o", "remount,ro", "/", (char *) 0);
-
- sync();
-
- /* and finally reboot, halt or power-off the system */
- if (cfg_downlevel == 0) {
- reboot(RB_AUTOBOOT);
- } else if (cfg_downlevel == 1) {
- reboot(RB_HALT_SYSTEM);
- } else {
- reboot(RB_POWER_OFF);
- }
- return 0;
+int main(int argc, char* const argv[]) {
+ int c;
+ int cfg_downlevel = 2;
+ /* 0: reboot
+ * 1: halt
+ * 2: power off
+ */
+ unsigned int cfg_delay = 3;
+ int cfg_minitonly = 0;
+ int cfg_sulogin = 0;
+
+#ifdef ALLOW_SUID
+ if (setuid(geteuid()) == -1) {
+ __write2("setuid(geteuid()) failed.\n");
+ return 111;
+ }
+#endif
+ if (getuid() != 0) {
+ __write2("you are not root, go away!\n");
+ return 1;
+ }
+
+ if (argc < 2) {
+ printUsage();
+ return 0;
+ }
+
+ /* parse commandline options */
+ while ((c = getopt(argc, argv, "rhosmnt:")) != EOF) {
+ switch (c) {
+ case 'r': /* do we have to reboot... */
+ cfg_downlevel = 0;
+ break;
+ case 'h': /* ...halt.. */
+ cfg_downlevel = 1;
+ break;
+ case 's': /* rescue system */
+ cfg_sulogin = 1;
+ break;
+ case 'm': /* exit after minit down */
+ cfg_minitonly = 1;
+ break;
+ case 'o': /* ..or power off? */
+ cfg_downlevel = 2;
+ break;
+ case 't': /* delay between SIGTERM and SIGKILL */
+ cfg_delay = atoi(optarg);
+ break;
+ default:
+ printUsage();
+ return 1;
+ }
+ }
+
+ opendevconsole();
+
+ switch (cfg_downlevel) {
+ case 0:
+ wall("system is going down for reboot NOW\n");
+ break;
+ case 1:
+ wall("system is going down for system halt NOW\n");
+ break;
+ case 2:
+ wall("system is going down for power-off NOW\n");
+ break;
+ }
+
+ /*
+ * catch some signals;
+ * getting killed after killing the controlling terminal wouldn't be
+ * such a great thing...
+ */
+ signal(SIGQUIT, SIG_IGN);
+ signal(SIGCHLD, SIG_IGN);
+ signal(SIGHUP, SIG_IGN);
+ signal(SIGTSTP, SIG_IGN);
+ signal(SIGTTIN, SIG_IGN);
+ signal(SIGTTOU, SIG_IGN);
+
+// real shutdown? then lets rock..
+#ifdef USE_MINIT
+ minit_shutdown(cfg_downlevel);
+ if (cfg_minitonly) return 0;
+#endif
+
+ /* kill all processes still left */
+ __write2("sending all processes the TERM signal...\n");
+ kill(-1, SIGTERM);
+ sleep(cfg_delay);
+
+ __write2("sending all processes the KILL signal...\n");
+ kill(-1, SIGKILL);
+
+ if (cfg_sulogin) {
+ char* suargs[] = { "sulogin", 0 };
+ execve("/sbin/sulogin", suargs, 0);
+ __write2("execve() /sbin/sulogin failed\n");
+ return 1;
+ }
+
+ /* sync buffers */
+ sync();
+
+ exec_cmd("/sbin/swapoff", "swapoff", "-a", (char*) 0);
+ exec_cmd("/bin/umount", "umount", "-a", (char*) 0);
+ exec_cmd("/bin/mount", "mount", "-o", "remount,ro", "/", (char*) 0);
+
+ sync();
+
+ /* and finally reboot, halt or power-off the system */
+ if (cfg_downlevel == 0) {
+ reboot(RB_AUTOBOOT);
+ } else if (cfg_downlevel == 1) {
+ reboot(RB_HALT_SYSTEM);
+ } else {
+ reboot(RB_POWER_OFF);
+ }
+ return 0;
}
diff --git a/split.c b/split.c
@@ -2,44 +2,45 @@
/* split buf into n strings that are separated by c. return n as *len.
* Allocate plus more slots and leave the first ofs of them alone. */
-char **split(char *buf,int c,size_t* len,size_t plus,size_t ofs) {
- int n=1;
- char **v=0;
- char **w;
- /* step 1: count tokens */
- char *s;
- for (s=buf; *s; s++) if (*s==c) n++;
- /* step 2: allocate space for pointers */
- v=(char **)malloc((n+plus)*sizeof(char*));
- if (!v) return 0;
- w=v+ofs;
- *w++=buf;
- for (s=buf; ; s++) {
- while (*s && *s!=c) s++;
- if (*s==0) break;
- if (*s==c) {
- *s=0;
- *w++=s+1;
- }
- }
- *len=w-v;
- return v;
+char** split(char* buf, int c, size_t* len, size_t plus, size_t ofs) {
+ int n = 1;
+ char** v = 0;
+ char** w;
+ /* step 1: count tokens */
+ char* s;
+ for (s = buf; *s; s++)
+ if (*s == c) n++;
+ /* step 2: allocate space for pointers */
+ v = (char**) malloc((n + plus) * sizeof(char*));
+ if (!v) return 0;
+ w = v + ofs;
+ *w++ = buf;
+ for (s = buf;; s++) {
+ while (*s && *s != c) s++;
+ if (*s == 0) break;
+ if (*s == c) {
+ *s = 0;
+ *w++ = s + 1;
+ }
+ }
+ *len = w - v;
+ return v;
}
#ifdef UNITTEST
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
+# include <assert.h>
+# include <stdio.h>
+# include <string.h>
int main() {
- char inp[]="foo\nbar\nbaz";
- size_t len;
- char** x=split(inp, '\n', &len, 2, 1);
- assert(len==4 && !strcmp(x[1],"foo") && !strcmp(x[2],"bar") && !strcmp(x[3],"baz") && x[4]==NULL);
- free(x);
- char inp2[]="fnord";
- x=split(inp2, '\n', &len, 2, 1);
- assert(len==2 && !strcmp(x[1],"fnord") && x[2]==NULL);
- return 0;
+ char inp[] = "foo\nbar\nbaz";
+ size_t len;
+ char** x = split(inp, '\n', &len, 2, 1);
+ assert(len == 4 && !strcmp(x[1], "foo") && !strcmp(x[2], "bar") && !strcmp(x[3], "baz") && x[4] == NULL);
+ free(x);
+ char inp2[] = "fnord";
+ x = split(inp2, '\n', &len, 2, 1);
+ assert(len == 2 && !strcmp(x[1], "fnord") && x[2] == NULL);
+ return 0;
}
#endif
diff --git a/t.c b/t.c
@@ -1,7 +1,7 @@
-#include <sys/types.h>
#include <stdio.h>
+#include <sys/types.h>
main() {
- pid_t p=30056;
- printf("spawned pid %d\n",p);
+ pid_t p = 30056;
+ printf("spawned pid %d\n", p);
}
diff --git a/waitinterface.c b/waitinterface.c
@@ -1,50 +1,50 @@
-#include <unistd.h>
-#include <time.h>
-#include <sys/stat.h>
-#include <sys/socket.h>
-#include <string.h>
-#include <netinet/in.h>
+#include <libowfat/errmsg.h>
+#include <libowfat/fmt.h>
#include <net/if.h>
+#include <netinet/in.h>
+#include <string.h>
#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
-#include <libowfat/fmt.h>
-#include <libowfat/errmsg.h>
-
-int main(int argc,char* argv[],char* envp[]) {
- unsigned int i;
- struct timespec req,rem;
- struct stat ss;
- char* sys;
- int found;
- errmsg_iam("waitinterface");
- if (argc<2)
- die(0,"usage: waitinterface wlan0 /usr/sbin/dhcpcd wlan0\n\twaits for wlan0 to show up and then executes the rest of the command line");
- sys=fmt_strm_alloca("/sys/class/net/",argv[1]);
- req.tv_sec=0; req.tv_nsec=100000000;
- for (i=0; i<100; ++i) {
- if ((found=lstat(sys,&ss))==0) break;
- nanosleep(&req,&rem);
- }
- if (found==-1)
- die(1,"interface not showing up");
- if (strstr(argv[0],"up")) {
- /* they want the interface to be up, too */
- int s=socket(PF_INET6,SOCK_DGRAM,IPPROTO_IP);
- struct ifreq ifr;
- if (s==-1) s=socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);
- if (s==-1) diesys(1,"socket");
- strncpy(ifr.ifr_name,argv[1],sizeof(ifr.ifr_name));
- for (i=0; i<100; ++i) {
- if (ioctl(s,SIOCGIFFLAGS,&ifr)==0 && (ifr.ifr_flags&IFF_UP)) break;
- nanosleep(&req,&rem);
- }
- close(s);
- if (!(ifr.ifr_flags&IFF_UP))
- die(1,"interface not up");
- }
- if (argv[2]) {
- execve(argv[2],argv+2,envp);
- diesys(1,"execve");
- }
- return 0;
+int main(int argc, char* argv[], char* envp[]) {
+ unsigned int i;
+ struct timespec req, rem;
+ struct stat ss;
+ char* sys;
+ int found;
+ errmsg_iam("waitinterface");
+ if (argc < 2)
+ die(0, "usage: waitinterface wlan0 /usr/sbin/dhcpcd wlan0\n\twaits for wlan0 to show up and then executes the rest of the command line");
+ sys = fmt_strm_alloca("/sys/class/net/", argv[1]);
+ req.tv_sec = 0;
+ req.tv_nsec = 100000000;
+ for (i = 0; i < 100; ++i) {
+ if ((found = lstat(sys, &ss)) == 0) break;
+ nanosleep(&req, &rem);
+ }
+ if (found == -1)
+ die(1, "interface not showing up");
+ if (strstr(argv[0], "up")) {
+ /* they want the interface to be up, too */
+ int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
+ struct ifreq ifr;
+ if (s == -1) s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+ if (s == -1) diesys(1, "socket");
+ strncpy(ifr.ifr_name, argv[1], sizeof(ifr.ifr_name));
+ for (i = 0; i < 100; ++i) {
+ if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0 && (ifr.ifr_flags & IFF_UP)) break;
+ nanosleep(&req, &rem);
+ }
+ close(s);
+ if (!(ifr.ifr_flags & IFF_UP))
+ die(1, "interface not up");
+ }
+ if (argv[2]) {
+ execve(argv[2], argv + 2, envp);
+ diesys(1, "execve");
+ }
+ return 0;
}
diff --git a/waitport.c b/waitport.c
@@ -1,150 +1,153 @@
+#include <ctype.h>
#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <time.h>
-#include <string.h>
#include <fcntl.h>
-#include <ctype.h>
-
+#include <libowfat/buffer.h>
#include <libowfat/fmt.h>
-#include <libowfat/scan.h>
#include <libowfat/ip4.h>
#include <libowfat/ip6.h>
+#include <libowfat/scan.h>
#include <libowfat/stralloc.h>
-#include <libowfat/buffer.h>
-
-static int netstat(const char* addr,unsigned int wantedport) {
- /* see linux/Documentation/networking/proc_net_tcp.txt */
- int fd=-1;
- char rbuf[4096]; /* since these files can become arbitrarily large, we'll use a real buffer to read from them */
- const char* filenames[] = { "/proc/net/tcp6", "/proc/net/tcp" };
- buffer b;
- unsigned int fn;
- stralloc line;
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
- for (fn=0; fn<sizeof(filenames)/sizeof(filenames[0]); ++fn) {
- const char* filename=filenames[fn];
+static int netstat(const char* addr, unsigned int wantedport) {
+ /* see linux/Documentation/networking/proc_net_tcp.txt */
+ int fd = -1;
+ char rbuf[4096]; /* since these files can become arbitrarily large, we'll use a real buffer to read from them */
+ const char* filenames[] = { "/proc/net/tcp6", "/proc/net/tcp" };
+ buffer b;
+ unsigned int fn;
+ stralloc line;
+
+ for (fn = 0; fn < sizeof(filenames) / sizeof(filenames[0]); ++fn) {
+ const char* filename = filenames[fn];
+
+ fd = open(filename, O_RDONLY | O_CLOEXEC);
+ if (fd == -1)
+ continue;
+ buffer_init(&b, read, fd, rbuf, sizeof(rbuf)); /* can't fail */
+ for (;;) {
+ int r;
+ char* c;
+ char* local;
+ int v6;
+ stralloc_zero(&line);
+ if ((r = buffer_getline_sa(&b, &line)) == -1) {
+ close(fd);
+ fprintf(stderr, "read error from %s\n", filename);
+ exit(1);
+ }
+ if (r == 0) break;
+ if (line.len < 1 || line.s[line.len - 1] != '\n') {
+ parseerror:
+ close(fd);
+ fprintf(stderr, "parse error from %s\n", filename);
+ exit(1);
+ }
+ line.s[line.len - 1] = 0; /* string is now null terminated */
+
+ /* First token is something like "917:", skip */
+ for (c = line.s; *c && *c != ':'; ++c)
+ ;
+ if (*c != ':') continue; /* first line is boilerplate text and has no :-token, skip it */
+ ++c;
+ for (; *c == ' '; ++c)
+ ;
+ /* Next token is something like "00000000:1770" or "00000000000000000000000000000000:0016" */
+ local = c;
+ for (; isxdigit(*c); ++c)
+ ;
+ if (c - local != 8 && c - local != 32) /* we only support ipv4 and ipv6; this is neither */
+ continue;
+ v6 = (c - local == 32);
+ if (*c != ':') goto parseerror;
+ for (r = 1; r < 5; ++r) {
+ if (!isxdigit(c[r])) goto parseerror;
+ }
+ if (c[5] != ' ') goto parseerror;
+ c[5] = 0;
+ c += 6;
+ /* Next token is the same thing, but we don't really need it, so
+ * just skip till next whitespace */
+ for (; *c && *c != ' '; ++c)
+ ;
+ if (*c != ' ') goto parseerror;
+ ++c;
+ /* Next is the state; if we are looking at tcp, 0A means LISTEN */
+ if (filename[10] == 't' && c[0] == '0' && c[1] == 'A') {
+ /* TCP LISTEN */
+ size_t n;
+ union {
+ char ip[16];
+ uint32_t ints[4];
+ } omgwtfbbq;
+ char ipstring[FMT_IP6];
+ unsigned short port;
+ unsigned long temp;
+
+ /* we can only be here if the hex string is 8 or 32 bytes long, see above */
+ for (n = 0; n < (unsigned int) v6 * 3 + 1; ++n) {
+ scan_xlongn(local + n * 8, 8, &temp);
+ omgwtfbbq.ints[n] = temp;
+ }
+
+ if (scan_xshort(local + (((unsigned int) v6 * 3 + 1) * 8) + 1, &port) != 4) /* can't happen, we validated with isxdigit */
+ goto parseerror;
+
+ ipstring[v6 ? fmt_ip6c(ipstring, omgwtfbbq.ip) : fmt_ip4(ipstring, omgwtfbbq.ip)] = 0;
+
+ if (!strcmp(ipstring, addr ? addr : (v6 ? "::" : "0.0.0.0")) && port == wantedport) {
+ close(fd);
+ return 1;
+ }
+ }
+ }
+ close(fd);
+ fd = -1;
+ }
- fd=open(filename,O_RDONLY|O_CLOEXEC);
- if (fd==-1)
- continue;
- buffer_init(&b,read,fd,rbuf,sizeof(rbuf)); /* can't fail */
- for (;;) {
- int r;
- char* c;
- char* local;
- int v6;
- stralloc_zero(&line);
- if ((r=buffer_getline_sa(&b,&line))==-1) {
- close(fd);
- fprintf(stderr,"read error from %s\n",filename);
- exit(1);
- }
- if (r==0) break;
- if (line.len < 1 || line.s[line.len-1]!='\n') {
-parseerror:
- close(fd);
- fprintf(stderr,"parse error from %s\n",filename);
- exit(1);
- }
- line.s[line.len-1]=0; /* string is now null terminated */
+ return 0;
+}
- /* First token is something like "917:", skip */
- for (c=line.s; *c && *c!=':'; ++c) ;
- if (*c != ':') continue; /* first line is boilerplate text and has no :-token, skip it */
- ++c;
- for (; *c==' '; ++c) ;
- /* Next token is something like "00000000:1770" or "00000000000000000000000000000000:0016" */
- local=c;
- for (; isxdigit(*c); ++c) ;
- if (c-local != 8 && c-local != 32) /* we only support ipv4 and ipv6; this is neither */
- continue;
- v6=(c-local==32);
- if (*c!=':') goto parseerror;
- for (r=1; r<5; ++r) {
- if (!isxdigit(c[r])) goto parseerror;
- }
- if (c[5]!=' ') goto parseerror;
- c[5]=0;
- c+=6;
- /* Next token is the same thing, but we don't really need it, so
- * just skip till next whitespace */
- for (; *c && *c!=' '; ++c) ;
- if (*c!=' ') goto parseerror;
- ++c;
- /* Next is the state; if we are looking at tcp, 0A means LISTEN */
- if (filename[10]=='t' && c[0]=='0' && c[1]=='A') {
- /* TCP LISTEN */
- size_t n;
- union {
- char ip[16];
- uint32_t ints[4];
- } omgwtfbbq;
- char ipstring[FMT_IP6];
+int main(int argc, char* argv[], char* envp[]) {
unsigned short port;
- unsigned long temp;
- /* we can only be here if the hex string is 8 or 32 bytes long, see above */
- for (n=0; n<(unsigned int)v6*3+1; ++n) {
- scan_xlongn(local+n*8,8,&temp);
- omgwtfbbq.ints[n]=temp;
+ unsigned int i;
+ struct timespec req, rem;
+
+ char* s = argv[1];
+ char* t = strchr(s, '/');
+
+ if (argc < 2)
+ usage:
+ printf("usage: waitport ::/111 some.rpcd\n\twaits for a service to bind to TCP port 111 on ::, then executes the rest of the command line\n");
+ exit(0);
+ {
+ if (t) {
+ *t = 0;
+ if (scan_ushort(t + 1, &port) == 0) goto usage;
+ } else {
+ if (scan_ushort(s, &port) == 0) goto usage;
+ s = 0;
+ }
}
- if (scan_xshort(local+(((unsigned int)v6*3+1)*8)+1,&port)!=4) /* can't happen, we validated with isxdigit */
- goto parseerror;
-
- ipstring[v6 ? fmt_ip6c(ipstring,omgwtfbbq.ip) : fmt_ip4(ipstring,omgwtfbbq.ip)]=0;
-
- if (!strcmp(ipstring,addr?addr : (v6?"::":"0.0.0.0")) && port==wantedport) {
- close(fd);
- return 1;
+ req.tv_sec = 0;
+ req.tv_nsec = 100000000;
+ for (i = 0; i < 1000; ++i) {
+ if (netstat(s, port)) {
+ if (argv[2]) {
+ execve(argv[2], argv + 2, envp);
+ fprintf(stderr, "execve: %s\n", strerror(errno));
+ exit(1);
+ }
+ return 0;
+ }
+ nanosleep(&req, &rem);
}
-
- }
- }
- close(fd);
- fd=-1;
- }
-
- return 0;
-}
-
-int main(int argc,char* argv[],char* envp[]) {
- unsigned short port;
-
- unsigned int i;
- struct timespec req,rem;
-
- char* s=argv[1];
- char* t=strchr(s,'/');
-
- if (argc<2)
-usage:
- printf("usage: waitport ::/111 some.rpcd\n\twaits for a service to bind to TCP port 111 on ::, then executes the rest of the command line\n");
- exit(0);
- {
- if (t) {
- *t=0;
- if (scan_ushort(t+1,&port)==0) goto usage;
- } else {
- if (scan_ushort(s,&port)==0) goto usage;
- s=0;
- }
- }
-
- req.tv_sec=0; req.tv_nsec=100000000;
- for (i=0; i<1000; ++i) {
- if (netstat(s,port)) {
- if (argv[2]) {
- execve(argv[2],argv+2,envp);
- fprintf(stderr,"execve: %s\n",strerror(errno));
- exit(1);
- }
- return 0;
- }
- nanosleep(&req,&rem);
- }
- fprintf(stderr,"service on port not showing up");
- exit(1);
+ fprintf(stderr, "service on port not showing up");
+ exit(1);
}
diff --git a/write_proc.c b/write_proc.c
@@ -1,17 +1,17 @@
-#include <unistd.h>
#include <fcntl.h>
#include <string.h>
+#include <unistd.h>
#define USAGE "write_proc <value> <path_to_proc_file>\n"
-int main(int argc,char*argv[]) {
- int fd;
- if (argc!=3) goto usage;
- if ((fd=open(argv[2],O_WRONLY))==-1) goto usage;
- write(fd,argv[1],strlen(argv[1]));
- close(fd);
- return 0;
+int main(int argc, char* argv[]) {
+ int fd;
+ if (argc != 3) goto usage;
+ if ((fd = open(argv[2], O_WRONLY)) == -1) goto usage;
+ write(fd, argv[1], strlen(argv[1]));
+ close(fd);
+ return 0;
usage:
- write(2,USAGE,strlen(USAGE));
- return 1;
+ write(2, USAGE, strlen(USAGE));
+ return 1;
}