From 8c7839512039212ba88e48d856d0ac9835fdec67 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 27 Jan 2007 22:21:52 +0000 Subject: [PATCH] runit cleanup part 1 --- runit/runit_lib.c | 210 +++++++++++++++++++++++++++++----------------- runit/runit_lib.h | 97 ++++++++++----------- runit/runsv.c | 24 +++--- runit/runsvdir.c | 12 +-- runit/sv.c | 39 +++++---- runit/svlogd.c | 81 ++++++++++-------- 6 files changed, 272 insertions(+), 191 deletions(-) diff --git a/runit/runit_lib.c b/runit/runit_lib.c index 2953235f0..ee69429b2 100644 --- a/runit/runit_lib.c +++ b/runit/runit_lib.c @@ -54,7 +54,8 @@ static int oneread(int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,uns for (;;) { r = op(fd,buf,len); - if (r == -1) if (errno == EINTR) continue; + if (r == -1 && errno == EINTR) + continue; return r; } } @@ -72,12 +73,15 @@ int buffer_feed(buffer *s) { int r; - if (s->p) return s->p; + if (s->p) + return s->p; r = oneread(s->op,s->fd,s->x,s->n); - if (r <= 0) return r; + if (r <= 0) + return r; s->p = r; s->n -= r; - if (s->n > 0) memmove(s->x + s->n,s->x,r); + if (s->n > 0) + memmove(s->x + s->n,s->x,r); return r; } @@ -85,9 +89,13 @@ int buffer_bget(buffer *s,char *buf,unsigned len) { int r; - if (s->p > 0) return getthis(s,buf,len); - if (s->n <= len) return oneread(s->op,s->fd,buf,s->n); - r = buffer_feed(s); if (r <= 0) return r; + if (s->p > 0) + return getthis(s,buf,len); + if (s->n <= len) + return oneread(s->op,s->fd,buf,s->n); + r = buffer_feed(s); + if (r <= 0) + return r; return getthis(s,buf,len); } @@ -95,9 +103,13 @@ int buffer_get(buffer *s,char *buf,unsigned len) { int r; - if (s->p > 0) return getthis(s,buf,len); - if (s->n <= len) return oneread(s->op,s->fd,buf,len); - r = buffer_feed(s); if (r <= 0) return r; + if (s->p > 0) + return getthis(s,buf,len); + if (s->n <= len) + return oneread(s->op,s->fd,buf,len); + r = buffer_feed(s); + if (r <= 0) + return r; return getthis(s,buf,len); } @@ -122,10 +134,11 @@ static int allwrite(int (*op)(int fd,char *buf,unsigned len),int fd,const char * while (len) { w = op(fd,(char*)buf,len); if (w == -1) { - if (errno == EINTR) continue; + if (errno == EINTR) + continue; return -1; /* note that some data may have been written */ } - if (w == 0) ; /* luser's fault */ + /* if (w == 0) ; luser's fault */ buf += w; len -= w; } @@ -183,7 +196,8 @@ int buffer_put(buffer *s,const char *buf,unsigned len) int buffer_putflush(buffer *s,const char *buf,unsigned len) { - if (buffer_flush(s) == -1) return -1; + if (buffer_flush(s) == -1) + return -1; return allwrite(s->op,s->fd,buf,len); } @@ -229,10 +243,10 @@ unsigned byte_chr(char *s,unsigned n,int c) ch = c; t = s; for (;;) { - if (!n) break; if (*t == ch) break; ++t; --n; - if (!n) break; if (*t == ch) break; ++t; --n; - if (!n) break; if (*t == ch) break; ++t; --n; - if (!n) break; if (*t == ch) break; ++t; --n; + if (!n) break; + if (*t == ch) break; + ++t; + --n; } return t - s; } @@ -250,10 +264,13 @@ int coe(int fd) int fd_copy(int to,int from) { - if (to == from) return 0; - if (fcntl(from,F_GETFL,0) == -1) return -1; + if (to == from) + return 0; + if (fcntl(from,F_GETFL,0) == -1) + return -1; close(to); - if (fcntl(from,F_DUPFD,to) == -1) return -1; + if (fcntl(from,F_DUPFD,to) == -1) + return -1; return 0; } @@ -262,8 +279,10 @@ int fd_copy(int to,int from) int fd_move(int to,int from) { - if (to == from) return 0; - if (fd_copy(to,from) == -1) return -1; + if (to == from) + return 0; + if (fd_copy(to,from) == -1) + return -1; close(from); return 0; } @@ -271,29 +290,47 @@ int fd_move(int to,int from) /*** fifo.c ***/ -int fifo_make(const char *fn,int mode) { return mkfifo(fn,mode); } +int fifo_make(const char *fn,int mode) +{ + return mkfifo(fn,mode); +} /*** fmt_ptime.c ***/ -unsigned fmt_ptime(char *s, struct taia *ta) { +void fmt_ptime30nul(char *s, struct taia *ta) { struct tm *t; unsigned long u; - if (ta->sec.x < 4611686018427387914ULL) return 0; /* impossible? */ + if (ta->sec.x < 4611686018427387914ULL) + return; /* impossible? */ u = ta->sec.x -4611686018427387914ULL; - if (!(t = gmtime((time_t*)&u))) return 0; - fmt_ulong(s, 1900 + t->tm_year); - s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2); - s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2); - s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2); - s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2); - s[16] = ':'; fmt_uint0(&s[17], t->tm_sec, 2); - s[19] = '.'; fmt_uint0(&s[20], ta->nano, 9); - return 25; + t = gmtime((time_t*)&u); + if (!t) + return; /* huh? */ + //fmt_ulong(s, 1900 + t->tm_year); + //s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2); + //s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2); + //s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2); + //s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2); + //s[16] = ':'; fmt_uint0(&s[17], t->tm_sec, 2); + //s[19] = '.'; fmt_uint0(&s[20], ta->nano, 9); + sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%09u", + (unsigned)(1900 + t->tm_year), + (unsigned)(t->tm_mon+1), + (unsigned)(t->tm_mday), + (unsigned)(t->tm_hour), + (unsigned)(t->tm_min), + (unsigned)(t->tm_sec), + (unsigned)(ta->nano) + ); + /* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */ + /* 5 + 3 + 3 + 3 + 3 + 3 + 9 = */ + /* 20 (up to '.' inclusive) + 9 (not including '\0') */ + return; } -unsigned fmt_taia(char *s, struct taia *t) { +unsigned fmt_taia25(char *s, struct taia *t) { static char pack[TAIA_PACK]; taia_pack(pack, t); @@ -303,6 +340,7 @@ unsigned fmt_taia(char *s, struct taia *t) { } +#ifdef UNUSED /*** fmt_uint.c ***/ unsigned fmt_uint(char *s,unsigned u) @@ -316,13 +354,20 @@ unsigned fmt_uint(char *s,unsigned u) unsigned fmt_uint0(char *s,unsigned u,unsigned n) { unsigned len; - len = fmt_uint(FMT_LEN,u); - while (len < n) { if (s) *s++ = '0'; ++len; } - if (s) fmt_uint(s,u); + len = fmt_uint(FMT_LEN, u); + while (len < n) { + if (s) + *s++ = '0'; + ++len; + } + if (s) + fmt_uint(s, u); return len; } +#endif +#ifdef UNUSED /*** fmt_ulong.c ***/ unsigned fmt_ulong(char *s,unsigned long u) @@ -336,18 +381,22 @@ unsigned fmt_ulong(char *s,unsigned long u) } return len; } +#endif +#ifdef UNUSED /*** tai_now.c ***/ void tai_now(struct tai *t) { - tai_unix(t,time((time_t *) 0)); + tai_unix(t, time(NULL)); } +#endif /*** tai_pack.c ***/ +static /* as it isn't used anywhere else */ void tai_pack(char *s,const struct tai *t) { uint64_t x; @@ -364,12 +413,14 @@ void tai_pack(char *s,const struct tai *t) } +#ifdef UNUSED /*** tai_sub.c ***/ -void tai_sub(struct tai *t,const struct tai *u,const struct tai *v) +void tai_sub(struct tai *t, const struct tai *u, const struct tai *v) { t->x = u->x - v->x; } +#endif /*** tai_unpack.c ***/ @@ -410,14 +461,7 @@ void taia_add(struct taia *t,const struct taia *u,const struct taia *v) } -/*** taia_approx.c ***/ - -double taia_approx(const struct taia *t) -{ - return tai_approx(&t->sec) + taia_frac(t); -} - - +#ifdef UNUSED /*** taia_frac.c ***/ double taia_frac(const struct taia *t) @@ -426,6 +470,21 @@ double taia_frac(const struct taia *t) } +/*** taia_approx.c ***/ + +double taia_approx(const struct taia *t) +{ + return t->sec->x + taia_frac(t); +} +#endif + +static +uint64_t taia2millisec(const struct taia *t) +{ + return (t->sec.x * 1000) + (t->nano / 1000000); +} + + /*** taia_less.c ***/ /* XXX: breaks tai encapsulation */ @@ -445,8 +504,8 @@ int taia_less(const struct taia *t,const struct taia *u) void taia_now(struct taia *t) { struct timeval now; - gettimeofday(&now,(struct timezone *) 0); - tai_unix(&t->sec,now.tv_sec); + gettimeofday(&now, NULL); + tai_unix(&t->sec, now.tv_sec); t->nano = 1000 * now.tv_usec + 500; t->atto = 0; } @@ -454,11 +513,11 @@ void taia_now(struct taia *t) /*** taia_pack.c ***/ -void taia_pack(char *s,const struct taia *t) +void taia_pack(char *s, const struct taia *t) { unsigned long x; - tai_pack(s,&t->sec); + tai_pack(s, &t->sec); s += 8; x = t->atto; @@ -575,25 +634,25 @@ GEN_ALLOC_append(stralloc,char,s,len,a,i,n,x,30,stralloc_readyplus,stralloc_appe void iopause(iopause_fd *x,unsigned len,struct taia *deadline,struct taia *stamp) { - struct taia t; int millisecs; - double d; int i; if (taia_less(deadline,stamp)) millisecs = 0; else { + uint64_t m; + struct taia t; t = *stamp; - taia_sub(&t,deadline,&t); - d = taia_approx(&t); - if (d > 1000.0) d = 1000.0; - millisecs = d * 1000.0 + 20.0; + taia_sub(&t, deadline, &t); + millisecs = m = taia2millisec(&t); + if (m > 1000) millisecs = 1000; + millisecs += 20; } - for (i = 0;i < len;++i) + for (i = 0; i < len; ++i) x[i].revents = 0; - poll(x,len,millisecs); + poll(x, len, millisecs); /* XXX: some kernels apparently need x[0] even if len is 0 */ /* XXX: how to handle EAGAIN? are kernels really this dumb? */ /* XXX: how to handle EINVAL? when exactly can this happen? */ @@ -867,26 +926,28 @@ unsigned scan_ulong(const char *s,unsigned long *u) } +#ifdef UNUSED /*** seek_set.c ***/ int seek_set(int fd,seek_pos pos) { if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0; } +#endif /*** sig.c ***/ -int sig_alarm = SIGALRM; -int sig_child = SIGCHLD; -int sig_cont = SIGCONT; -int sig_hangup = SIGHUP; -int sig_int = SIGINT; -int sig_pipe = SIGPIPE; -int sig_term = SIGTERM; +//int sig_alarm = SIGALRM; +//int sig_child = SIGCHLD; +//int sig_cont = SIGCONT; +//int sig_hangup = SIGHUP; +//int sig_int = SIGINT; +//int sig_pipe = SIGPIPE; +//int sig_term = SIGTERM; -void (*sig_defaulthandler)(int) = SIG_DFL; -void (*sig_ignorehandler)(int) = SIG_IGN; +//void (*sig_defaulthandler)(int) = SIG_DFL; +//void (*sig_ignorehandler)(int) = SIG_IGN; /*** sig_block.c ***/ @@ -947,10 +1008,9 @@ unsigned str_chr(const char *s,int c) ch = c; t = s; for (;;) { - if (!*t) break; if (*t == ch) break; ++t; - if (!*t) break; if (*t == ch) break; ++t; - if (!*t) break; if (*t == ch) break; ++t; - if (!*t) break; if (*t == ch) break; ++t; + if (!*t) break; + if (*t == ch) break; + ++t; } return t - s; } @@ -960,7 +1020,7 @@ unsigned str_chr(const char *s,int c) int wait_nohang(int *wstat) { - return waitpid(-1,wstat,WNOHANG); + return waitpid(-1, wstat, WNOHANG); } @@ -971,7 +1031,7 @@ int wait_pid(int *wstat, int pid) int r; do - r = waitpid(pid,wstat,0); + r = waitpid(pid, wstat, 0); while ((r == -1) && (errno == EINTR)); return r; } diff --git a/runit/runit_lib.h b/runit/runit_lib.h index f4beb560e..f594f8f69 100644 --- a/runit/runit_lib.h +++ b/runit/runit_lib.h @@ -107,46 +107,46 @@ extern int fifo_make(const char *,int); /*** fmt.h ***/ -#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */ -#define FMT_LEN ((char *) 0) /* convenient abbreviation */ +//#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */ +//#define FMT_LEN ((char *) 0) /* convenient abbreviation */ -extern unsigned fmt_uint(char *,unsigned); -extern unsigned fmt_uint0(char *,unsigned,unsigned); -extern unsigned fmt_xint(char *,unsigned); -extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned); -extern unsigned fmt_ushort(char *,unsigned short); -extern unsigned fmt_xshort(char *,unsigned short); -extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short); -extern unsigned fmt_ulong(char *,unsigned long); -extern unsigned fmt_xlong(char *,unsigned long); -extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long); +//extern unsigned fmt_uint(char *,unsigned); +//extern unsigned fmt_uint0(char *,unsigned,unsigned); +//extern unsigned fmt_xint(char *,unsigned); +//extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned); +//extern unsigned fmt_ushort(char *,unsigned short); +//extern unsigned fmt_xshort(char *,unsigned short); +//extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short); +//extern unsigned fmt_ulong(char *,unsigned long); +//extern unsigned fmt_xlong(char *,unsigned long); +//extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long); -extern unsigned fmt_plusminus(char *,int); -extern unsigned fmt_minus(char *,int); -extern unsigned fmt_0x(char *,int); +//extern unsigned fmt_plusminus(char *,int); +//extern unsigned fmt_minus(char *,int); +//extern unsigned fmt_0x(char *,int); -extern unsigned fmt_str(char *,const char *); -extern unsigned fmt_strn(char *,const char *,unsigned); +//extern unsigned fmt_str(char *,const char *); +//extern unsigned fmt_strn(char *,const char *,unsigned); /*** tai.h ***/ struct tai { uint64_t x; -} ; +}; #define tai_unix(t,u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u))) -extern void tai_now(struct tai *); +//extern void tai_now(struct tai *); -#define tai_approx(t) ((double) ((t)->x)) +//#define tai_approx(t) ((double) ((t)->x)) -extern void tai_add(struct tai *,const struct tai *,const struct tai *); -extern void tai_sub(struct tai *,const struct tai *,const struct tai *); -#define tai_less(t,u) ((t)->x < (u)->x) +//extern void tai_add(struct tai *,const struct tai *,const struct tai *); +//extern void tai_sub(struct tai *,const struct tai *,const struct tai *); +//#define tai_less(t,u) ((t)->x < (u)->x) #define TAI_PACK 8 -extern void tai_pack(char *,const struct tai *); +//extern void tai_pack(char *,const struct tai *); extern void tai_unpack(const char *,struct tai *); extern void tai_uint(struct tai *,unsigned); @@ -158,14 +158,14 @@ struct taia { struct tai sec; unsigned long nano; /* 0...999999999 */ unsigned long atto; /* 0...999999999 */ -} ; +}; -extern void taia_tai(const struct taia *,struct tai *); +//extern void taia_tai(const struct taia *,struct tai *); extern void taia_now(struct taia *); -extern double taia_approx(const struct taia *); -extern double taia_frac(const struct taia *); +//extern double taia_approx(const struct taia *); +//extern double taia_frac(const struct taia *); extern void taia_add(struct taia *,const struct taia *,const struct taia *); extern void taia_addsec(struct taia *,const struct taia *,int); @@ -175,10 +175,10 @@ extern int taia_less(const struct taia *,const struct taia *); #define TAIA_PACK 16 extern void taia_pack(char *,const struct taia *); -extern void taia_unpack(const char *,struct taia *); +//extern void taia_unpack(const char *,struct taia *); -#define TAIA_FMTFRAC 19 -extern unsigned taia_fmtfrac(char *,const struct taia *); +//#define TAIA_FMTFRAC 19 +//extern unsigned taia_fmtfrac(char *,const struct taia *); extern void taia_uint(struct taia *,unsigned); @@ -187,10 +187,13 @@ extern void taia_uint(struct taia *,unsigned); #define FMT_PTIME 30 -extern unsigned fmt_ptime(char *, struct taia *); -extern unsigned fmt_taia(char *, struct taia *); +/* NUL terminated */ +extern void fmt_ptime30nul(char *, struct taia *); +/* NOT terminated! */ +extern unsigned fmt_taia25(char *, struct taia *); +#ifdef UNUSED /*** gen_alloc.h ***/ #define GEN_ALLOC_typedef(ta,type,field,len,a) \ @@ -233,7 +236,6 @@ int ta_append(ta *x,const type *i) \ /*** stralloc.h ***/ -#if 0 GEN_ALLOC_typedef(stralloc,char,s,len,a) extern int stralloc_ready(stralloc *,unsigned); @@ -314,6 +316,7 @@ extern int readclose(int,stralloc *,unsigned); /*** scan.h ***/ +#if 0 extern unsigned scan_uint(const char *,unsigned *); extern unsigned scan_xint(const char *,unsigned *); extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,unsigned *); @@ -337,6 +340,7 @@ extern unsigned scan_memcmp(const char *,const char *,unsigned); extern unsigned scan_long(const char *,long *); extern unsigned scan_8long(const char *,unsigned long *); +#endif /*** seek.h ***/ @@ -345,30 +349,27 @@ typedef unsigned long seek_pos; extern seek_pos seek_cur(int); -extern int seek_set(int,seek_pos); +//extern int seek_set(int,seek_pos); extern int seek_end(int); extern int seek_trunc(int,seek_pos); -#define seek_begin(fd) (seek_set((fd),(seek_pos) 0)) +//#define seek_begin(fd) (seek_set((fd),(seek_pos) 0)) /*** sig.h ***/ -extern int sig_alarm; -extern int sig_child; -extern int sig_cont; -extern int sig_hangup; -extern int sig_int; -extern int sig_pipe; -extern int sig_term; - -extern void (*sig_defaulthandler)(int); -extern void (*sig_ignorehandler)(int); +//extern int sig_alarm; +//extern int sig_child; +//extern int sig_cont; +//extern int sig_hangup; +//extern int sig_int; +//extern int sig_pipe; +//extern int sig_term; extern void sig_catch(int,void (*)(int)); -#define sig_ignore(s) (sig_catch((s),sig_ignorehandler)) -#define sig_uncatch(s) (sig_catch((s),sig_defaulthandler)) +#define sig_ignore(s) (sig_catch((s),SIG_IGN)) +#define sig_uncatch(s) (sig_catch((s),SIG_DFL)) extern void sig_block(int); extern void sig_unblock(int); diff --git a/runit/runsv.c b/runit/runsv.c index aace041a8..24a51f2b1 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -307,10 +307,10 @@ static void startservice(struct svdir *s) close(logpipe[0]); } } - sig_uncatch(sig_child); - sig_unblock(sig_child); - sig_uncatch(sig_term); - sig_unblock(sig_term); + sig_uncatch(SIGCHLD); + sig_unblock(SIGCHLD); + sig_uncatch(SIGTERM); + sig_unblock(SIGTERM); execve(*run, run, environ); if (s->islog) fatal2_cannot("start log/", *run); @@ -406,10 +406,10 @@ int runsv_main(int argc, char **argv) ndelay_on(selfpipe[0]); ndelay_on(selfpipe[1]); - sig_block(sig_child); - sig_catch(sig_child, s_child); - sig_block(sig_term); - sig_catch(sig_term, s_term); + sig_block(SIGCHLD); + sig_catch(SIGCHLD, s_child); + sig_block(SIGTERM); + sig_catch(SIGTERM, s_term); xchdir(dir); svd[0].pid = 0; @@ -533,11 +533,11 @@ int runsv_main(int argc, char **argv) taia_uint(&deadline, 3600); taia_add(&deadline, &now, &deadline); - sig_unblock(sig_term); - sig_unblock(sig_child); + sig_unblock(SIGTERM); + sig_unblock(SIGCHLD); iopause(x, 2+haslog, &deadline, &now); - sig_block(sig_term); - sig_block(sig_child); + sig_block(SIGTERM); + sig_block(SIGCHLD); while (read(selfpipe[0], &ch, 1) == 1) ; diff --git a/runit/runsvdir.c b/runit/runsvdir.c index 3290da5e6..41581a8c8 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c @@ -70,8 +70,8 @@ static void runsv(int no, char *name) prog[0] = "runsv"; prog[1] = name; prog[2] = 0; - sig_uncatch(sig_hangup); - sig_uncatch(sig_term); + sig_uncatch(SIGHUP); + sig_uncatch(SIGTERM); if (pgrp) setsid(); execvp(prog[0], prog); //pathexec_run(*prog, prog, (char* const*)environ); @@ -197,8 +197,8 @@ int runsvdir_main(int argc, char **argv) if (!argv || !*argv) usage(); } - sig_catch(sig_term, s_term); - sig_catch(sig_hangup, s_hangup); + sig_catch(SIGTERM, s_term); + sig_catch(SIGHUP, s_hangup); svdir = *argv++; if (argv && *argv) { rplog = *argv; @@ -276,12 +276,12 @@ int runsvdir_main(int argc, char **argv) taia_uint(&deadline, check ? 1 : 5); taia_add(&deadline, &now, &deadline); - sig_block(sig_child); + sig_block(SIGCHLD); if (rplog) iopause(io, 1, &deadline, &now); else iopause(0, 0, &deadline, &now); - sig_unblock(sig_child); + sig_unblock(SIGCHLD); if (rplog && (io[0].revents | IOPAUSE_READ)) while (read(logpipe[0], &ch, 1) > 0) diff --git a/runit/sv.c b/runit/sv.c index 054053619..6594e1451 100644 --- a/runit/sv.c +++ b/runit/sv.c @@ -326,28 +326,38 @@ int sv_main(int argc, char **argv) service++; } - if (*cbk) + if (*cbk) { for (;;) { +//TODO: tdiff resolution is way too high. seconds will be enough taia_sub(&tdiff, &tnow, &tstart); service = servicex; want_exit = 1; for (i = 0; i < services; ++i, ++service) { - if (!*service) continue; + if (!*service) + continue; if ((**service != '/') && (**service != '.')) { - if ((chdir(varservice) == -1) || (chdir(*service) == -1)) { - fail("cannot change to service directory"); - *service = 0; - } - } else if (chdir(*service) == -1) { - fail("cannot change to service directory"); - *service = 0; + if (chdir(varservice) == -1) + goto chdir_failed; } - if (*service) { if (cbk(acts) != 0) *service = 0; else want_exit = 0; } - if (*service && taia_approx(&tdiff) > waitsec) { + if (chdir(*service) == -1) { + chdir_failed: + fail("cannot change to service directory"); + goto nullify_service; + } + if (cbk(acts) != 0) + goto nullify_service; + want_exit = 0; + //if (taia_approx(&tdiff) > waitsec) + if (tdiff.sec.x >= waitsec) { kll ? printf(KILL) : printf(TIMEOUT); - if (svstatus_get() > 0) { svstatus_print(*service); ++rc; } + if (svstatus_get() > 0) { + svstatus_print(*service); + ++rc; + } puts(""); /* will also flush the output */ - if (kll) control("k"); - *service = 0; + if (kll) + control("k"); + nullify_service: + *service = NULL; } if (fchdir(curdir) == -1) fatal_cannot("change to original directory"); @@ -356,5 +366,6 @@ int sv_main(int argc, char **argv) usleep(420000); taia_now(&tnow); } + } return rc > 99 ? 99 : rc; } diff --git a/runit/svlogd.c b/runit/svlogd.c index b089c5498..67ffc443c 100644 --- a/runit/svlogd.c +++ b/runit/svlogd.c @@ -100,11 +100,13 @@ static void warnx(char *m0, char *m1) } static void pause_nomem(void) { - bb_error_msg(PAUSE"out of memory"); sleep(3); + bb_error_msg(PAUSE"out of memory"); + sleep(3); } static void pause1cannot(char *m0) { - bb_perror_msg(PAUSE"cannot %s", m0); sleep(3); + bb_perror_msg(PAUSE"cannot %s", m0); + sleep(3); } static void pause2cannot(char *m0, char *m1) { @@ -115,7 +117,8 @@ static void pause2cannot(char *m0, char *m1) static char* wstrdup(const char *str) { char *s; - while (!(s = strdup(str))) pause_nomem(); + while (!(s = strdup(str))) + pause_nomem(); return s; } @@ -135,12 +138,12 @@ static unsigned processorstart(struct logdir *ld) int fd; /* child */ - sig_uncatch(sig_term); - sig_uncatch(sig_alarm); - sig_uncatch(sig_hangup); - sig_unblock(sig_term); - sig_unblock(sig_alarm); - sig_unblock(sig_hangup); + sig_uncatch(SIGTERM); + sig_uncatch(SIGALRM); + sig_uncatch(SIGHUP); + sig_unblock(SIGTERM); + sig_unblock(SIGALRM); + sig_unblock(SIGHUP); if (verbose) bb_error_msg(INFO"processing: %s/%s", ld->name, ld->fnsave); @@ -164,6 +167,7 @@ static unsigned processorstart(struct logdir *ld) if (fd_move(5, fd) == -1) bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); +// getenv("SHELL")? prog[0] = "sh"; prog[1] = "-c"; prog[2] = ld->processor; @@ -180,10 +184,10 @@ static unsigned processorstop(struct logdir *ld) char f[28]; if (ld->ppid) { - sig_unblock(sig_hangup); + sig_unblock(SIGHUP); while (wait_pid(&wstat, ld->ppid) == -1) pause2cannot("wait for processor", ld->name); - sig_block(sig_hangup); + sig_block(SIGHUP); ld->ppid = 0; } if (ld->fddir == -1) return 1; @@ -212,7 +216,8 @@ static unsigned processorstop(struct logdir *ld) bb_error_msg(WARNING"cannot unlink: %s/%s", ld->name, ld->fnsave); while (rename("newstate", "state") == -1) pause2cannot("rename state", ld->name); - if (verbose) bb_error_msg(INFO"processed: %s/%s", ld->name, f); + if (verbose) + bb_error_msg(INFO"processed: %s/%s", ld->name, f); while (fchdir(fdwdir) == -1) pause1cannot("change to initial working directory"); return 1; @@ -242,11 +247,13 @@ static void rmoldest(struct logdir *ld) errno = 0; } } - if (errno) warn2("cannot read directory", ld->name); + if (errno) + warn2("cannot read directory", ld->name); closedir(d); if (ld->nmax && (n > ld->nmax)) { - if (verbose) bb_error_msg(INFO"delete: %s/%s", ld->name, oldest); + if (verbose) + bb_error_msg(INFO"delete: %s/%s", ld->name, oldest); if ((*oldest == '@') && (unlink(oldest) == -1)) warn2("cannot unlink oldest logfile", ld->name); } @@ -276,9 +283,10 @@ static unsigned rotate(struct logdir *ld) ld->fnsave[27] = '\0'; do { taia_now(&now); - fmt_taia(ld->fnsave, &now); + fmt_taia25(ld->fnsave, &now); errno = 0; - } while ((stat(ld->fnsave, &st) != -1) || (errno != ENOENT)); + stat(ld->fnsave, &st); + } while (errno != ENOENT); if (ld->tmax && taia_less(&ld->trotate, &now)) { taia_uint(&ld->trotate, ld->tmax); @@ -523,9 +531,10 @@ static unsigned logdir_open(struct logdir *ld, const char *fn) ld->fnsave[27] = '\0'; do { taia_now(&now); - fmt_taia(ld->fnsave, &now); + fmt_taia25(ld->fnsave, &now); errno = 0; - } while ((stat(ld->fnsave, &st) != -1) || (errno != ENOENT)); + stat(ld->fnsave, &st); + } while (errno != ENOENT); while (rename("current", ld->fnsave) == -1) pause2cannot("rename current", ld->name); rmoldest(ld); @@ -608,15 +617,15 @@ static int buffer_pread(int fd, char *s, unsigned len) while (1) { /* Comment? */ - sig_unblock(sig_term); - sig_unblock(sig_child); - sig_unblock(sig_alarm); - sig_unblock(sig_hangup); + sig_unblock(SIGTERM); + sig_unblock(SIGCHLD); + sig_unblock(SIGALRM); + sig_unblock(SIGHUP); iopause(&in, 1, &trotate, &now); - sig_block(sig_term); - sig_block(sig_child); - sig_block(sig_alarm); - sig_block(sig_hangup); + sig_block(SIGTERM); + sig_block(SIGCHLD); + sig_block(SIGALRM); + sig_block(SIGHUP); i = safe_read(fd, s, len); if (i >= 0) break; if (errno != EAGAIN) { @@ -764,14 +773,14 @@ int svlogd_main(int argc, char **argv) in.events = IOPAUSE_READ; ndelay_on(in.fd); - sig_block(sig_term); - sig_block(sig_child); - sig_block(sig_alarm); - sig_block(sig_hangup); - sig_catch(sig_term, sig_term_handler); - sig_catch(sig_child, sig_child_handler); - sig_catch(sig_alarm, sig_alarm_handler); - sig_catch(sig_hangup, sig_hangup_handler); + sig_block(SIGTERM); + sig_block(SIGCHLD); + sig_block(SIGALRM); + sig_block(SIGHUP); + sig_catch(SIGTERM, sig_term_handler); + sig_catch(SIGCHLD, sig_child_handler); + sig_catch(SIGALRM, sig_alarm_handler); + sig_catch(SIGHUP, sig_hangup_handler); logdirs_reopen(); @@ -788,10 +797,10 @@ int svlogd_main(int argc, char **argv) taia_now(&now); switch (timestamp) { case 1: - fmt_taia(stamp, &now); + fmt_taia25(stamp, &now); break; default: /* case 2: */ - fmt_ptime(stamp, &now); + fmt_ptime30nul(stamp, &now); break; } memcpy(line, stamp, 25);