minit

A small yet feature-complete init (http://fefe.de/minit/)
Log | Files | Refs | README | LICENSE

commit ffa4f42bf119e72cc64229675f3f814d4e9f44ff
parent a17662d10c520f18054ad4c4d9ac2445c0f656b0
Author: leitner <leitner>
Date:   Fri,  2 Feb 2001 20:35:36 +0000

added pidfilehack.

Diffstat:
MMakefile | 26+++++++++++++++++++-------
Mmsvc.c | 8++++----
Apidfilehack.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,18 +1,22 @@ -all: minit msvc +all: minit msvc pidfilehack + +CFLAGS=-pipe -march=i386 -fomit-frame-pointer -Os -I../dietlibc/include +LDFLAGS=../dietlibc/start.o ../dietlibc/dietlibc.a minit: minit.o split.o openreadclose.o - gcc -g -o minit $^ + gcc -g $(LDFLAGS) -o minit $^ msvc: msvc.o - gcc -g -o msvc msvc.o + gcc -g $(LDFLAGS) -o msvc msvc.o %.o: %.c - gcc -pipe -g -c $^ + gcc $(CFLAGS) -c $^ diet: minit.c split.o openreadclose.o - gcc -nostdlib -o minit -pipe -Os -m386 minit.c split.c openreadclose.c ../dietlibc/start.o ../dietlibc/dietlibc.a - gcc -nostdlib -o msvc -pipe -Os -m386 msvc.c ../dietlibc/start.o ../dietlibc/dietlibc.a - strip -R .note -R .comment minit msvc + gcc -nostdlib -o minit -pipe -Os -m386 -I../dietlibc/include minit.c split.c openreadclose.c ../dietlibc/start.o ../dietlibc/dietlibc.a + gcc -nostdlib -o msvc -pipe -Os -m386 -I../dietlibc/include msvc.c ../dietlibc/start.o ../dietlibc/dietlibc.a + gcc -nostdlib -o pidfilehack -pipe -Os -m386 -I../dietlibc/include pidfilehack.c ../dietlibc/start.o ../dietlibc/dietlibc.a + strip -R .note -R .comment minit msvc pidfilehack diet2: minit.c split.o openreadclose.o gcc -nostdlib -g -o minit -pipe minit.c split.c openreadclose.c ../dietlibc/start.o ../dietlibc/dietlibc.a @@ -22,3 +26,11 @@ clean: test: test.c gcc -nostdlib -o $@ $^ -I../dietlibc/include ../dietlibc/start.o ../dietlibc/dietlibc.a + +pidfilehack: pidfilehack.c + gcc -nostdlib -pipe -g -o $@ $^ ../dietlibc/start.o ../dietlibc/dietlibc.a + +install: + install minit msvc pidfilehack /usr/sbin + test -d /etc/minit || mkdir /etc/minit + mkfifo -m 600 /etc/minit/in /etc/minit/out diff --git a/msvc.c b/msvc.c @@ -20,7 +20,7 @@ unsigned int fmt_ulong(register char *s,register unsigned long u) } /* return PID, 0 if error */ -pid_t getpid(char *service) { +pid_t __readpid(char *service) { int len; buf[0]='p'; strncpy(buf+1,service,1400); @@ -99,7 +99,7 @@ main(int argc,char *argv[]) { sleep(1); } if (argc==2) { - pid_t pid=getpid(argv[1]); + pid_t pid=__readpid(argv[1]); if (buf[0]!='0') { unsigned long len; write(1,argv[1],strlen(argv[1])); @@ -134,7 +134,7 @@ main(int argc,char *argv[]) { fprintf(stderr,"Could not start %s\n",argv[2]); break; case 'd': /* TODO: down */ - pid=getpid(argv[2]); + pid=__readpid(argv[2]); if (pid==0) { puts("service not found"); return 1; @@ -156,7 +156,7 @@ main(int argc,char *argv[]) { return 0; dokill: for (i=2; i<=argc; i++) { - pid=getpid(argv[2]); + pid=__readpid(argv[2]); if (kill(pid,sig)) { fprintf(stderr,"Could not send signal to PID %d\n",pid); } diff --git a/pidfilehack.c b/pidfilehack.c @@ -0,0 +1,62 @@ +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <sys/fcntl.h> + +/* purpose: argv[1] is the full path to a PID file, + * argv+2 is the daemon to run. + * the daemon is expected to fork in the background and write its PID in + * the pid file. + */ + +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); + pid_t p; + if (fd>=0) { + static char buf[100] = "-P"; + int len=read(fd,buf+2,100); + 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); +}