runit cleanup part 1

This commit is contained in:
Denis Vlasenko 2007-01-27 22:21:52 +00:00
parent e06bed30cf
commit 8c78395120
6 changed files with 272 additions and 191 deletions

View File

@ -54,7 +54,8 @@ static int oneread(int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,uns
for (;;) { for (;;) {
r = op(fd,buf,len); r = op(fd,buf,len);
if (r == -1) if (errno == EINTR) continue; if (r == -1 && errno == EINTR)
continue;
return r; return r;
} }
} }
@ -72,12 +73,15 @@ int buffer_feed(buffer *s)
{ {
int r; int r;
if (s->p) return s->p; if (s->p)
return s->p;
r = oneread(s->op,s->fd,s->x,s->n); r = oneread(s->op,s->fd,s->x,s->n);
if (r <= 0) return r; if (r <= 0)
return r;
s->p = r; s->p = r;
s->n -= 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; return r;
} }
@ -85,9 +89,13 @@ int buffer_bget(buffer *s,char *buf,unsigned len)
{ {
int r; int r;
if (s->p > 0) return getthis(s,buf,len); if (s->p > 0)
if (s->n <= len) return oneread(s->op,s->fd,buf,s->n); return getthis(s,buf,len);
r = buffer_feed(s); if (r <= 0) return r; 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); return getthis(s,buf,len);
} }
@ -95,9 +103,13 @@ int buffer_get(buffer *s,char *buf,unsigned len)
{ {
int r; int r;
if (s->p > 0) return getthis(s,buf,len); if (s->p > 0)
if (s->n <= len) return oneread(s->op,s->fd,buf,len); return getthis(s,buf,len);
r = buffer_feed(s); if (r <= 0) return r; 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); 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) { while (len) {
w = op(fd,(char*)buf,len); w = op(fd,(char*)buf,len);
if (w == -1) { if (w == -1) {
if (errno == EINTR) continue; if (errno == EINTR)
continue;
return -1; /* note that some data may have been written */ return -1; /* note that some data may have been written */
} }
if (w == 0) ; /* luser's fault */ /* if (w == 0) ; luser's fault */
buf += w; buf += w;
len -= 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) 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); return allwrite(s->op,s->fd,buf,len);
} }
@ -229,10 +243,10 @@ unsigned byte_chr(char *s,unsigned n,int c)
ch = c; ch = c;
t = s; t = s;
for (;;) { for (;;) {
if (!n) break; if (*t == ch) break; ++t; --n; if (!n) break;
if (!n) break; if (*t == ch) break; ++t; --n; if (*t == ch) break;
if (!n) break; if (*t == ch) break; ++t; --n; ++t;
if (!n) break; if (*t == ch) break; ++t; --n; --n;
} }
return t - s; return t - s;
} }
@ -250,10 +264,13 @@ int coe(int fd)
int fd_copy(int to,int from) int fd_copy(int to,int from)
{ {
if (to == from) return 0; if (to == from)
if (fcntl(from,F_GETFL,0) == -1) return -1; return 0;
if (fcntl(from,F_GETFL,0) == -1)
return -1;
close(to); close(to);
if (fcntl(from,F_DUPFD,to) == -1) return -1; if (fcntl(from,F_DUPFD,to) == -1)
return -1;
return 0; return 0;
} }
@ -262,8 +279,10 @@ int fd_copy(int to,int from)
int fd_move(int to,int from) int fd_move(int to,int from)
{ {
if (to == from) return 0; if (to == from)
if (fd_copy(to,from) == -1) return -1; return 0;
if (fd_copy(to,from) == -1)
return -1;
close(from); close(from);
return 0; return 0;
} }
@ -271,29 +290,47 @@ int fd_move(int to,int from)
/*** fifo.c ***/ /*** 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 ***/ /*** fmt_ptime.c ***/
unsigned fmt_ptime(char *s, struct taia *ta) { void fmt_ptime30nul(char *s, struct taia *ta) {
struct tm *t; struct tm *t;
unsigned long u; unsigned long u;
if (ta->sec.x < 4611686018427387914ULL) return 0; /* impossible? */ if (ta->sec.x < 4611686018427387914ULL)
return; /* impossible? */
u = ta->sec.x -4611686018427387914ULL; u = ta->sec.x -4611686018427387914ULL;
if (!(t = gmtime((time_t*)&u))) return 0; t = gmtime((time_t*)&u);
fmt_ulong(s, 1900 + t->tm_year); if (!t)
s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2); return; /* huh? */
s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2); //fmt_ulong(s, 1900 + t->tm_year);
s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2); //s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2);
s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2); //s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2);
s[16] = ':'; fmt_uint0(&s[17], t->tm_sec, 2); //s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2);
s[19] = '.'; fmt_uint0(&s[20], ta->nano, 9); //s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2);
return 25; //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]; static char pack[TAIA_PACK];
taia_pack(pack, t); taia_pack(pack, t);
@ -303,6 +340,7 @@ unsigned fmt_taia(char *s, struct taia *t) {
} }
#ifdef UNUSED
/*** fmt_uint.c ***/ /*** fmt_uint.c ***/
unsigned fmt_uint(char *s,unsigned u) 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 fmt_uint0(char *s,unsigned u,unsigned n)
{ {
unsigned len; unsigned len;
len = fmt_uint(FMT_LEN,u); len = fmt_uint(FMT_LEN, u);
while (len < n) { if (s) *s++ = '0'; ++len; } while (len < n) {
if (s) fmt_uint(s,u); if (s)
*s++ = '0';
++len;
}
if (s)
fmt_uint(s, u);
return len; return len;
} }
#endif
#ifdef UNUSED
/*** fmt_ulong.c ***/ /*** fmt_ulong.c ***/
unsigned fmt_ulong(char *s,unsigned long u) unsigned fmt_ulong(char *s,unsigned long u)
@ -336,18 +381,22 @@ unsigned fmt_ulong(char *s,unsigned long u)
} }
return len; return len;
} }
#endif
#ifdef UNUSED
/*** tai_now.c ***/ /*** tai_now.c ***/
void tai_now(struct tai *t) void tai_now(struct tai *t)
{ {
tai_unix(t,time((time_t *) 0)); tai_unix(t, time(NULL));
} }
#endif
/*** tai_pack.c ***/ /*** tai_pack.c ***/
static /* as it isn't used anywhere else */
void tai_pack(char *s,const struct tai *t) void tai_pack(char *s,const struct tai *t)
{ {
uint64_t x; uint64_t x;
@ -364,12 +413,14 @@ void tai_pack(char *s,const struct tai *t)
} }
#ifdef UNUSED
/*** tai_sub.c ***/ /*** 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; t->x = u->x - v->x;
} }
#endif
/*** tai_unpack.c ***/ /*** tai_unpack.c ***/
@ -410,14 +461,7 @@ void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
} }
/*** taia_approx.c ***/ #ifdef UNUSED
double taia_approx(const struct taia *t)
{
return tai_approx(&t->sec) + taia_frac(t);
}
/*** taia_frac.c ***/ /*** taia_frac.c ***/
double taia_frac(const struct taia *t) 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 ***/ /*** taia_less.c ***/
/* XXX: breaks tai encapsulation */ /* 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) void taia_now(struct taia *t)
{ {
struct timeval now; struct timeval now;
gettimeofday(&now,(struct timezone *) 0); gettimeofday(&now, NULL);
tai_unix(&t->sec,now.tv_sec); tai_unix(&t->sec, now.tv_sec);
t->nano = 1000 * now.tv_usec + 500; t->nano = 1000 * now.tv_usec + 500;
t->atto = 0; t->atto = 0;
} }
@ -454,11 +513,11 @@ void taia_now(struct taia *t)
/*** taia_pack.c ***/ /*** taia_pack.c ***/
void taia_pack(char *s,const struct taia *t) void taia_pack(char *s, const struct taia *t)
{ {
unsigned long x; unsigned long x;
tai_pack(s,&t->sec); tai_pack(s, &t->sec);
s += 8; s += 8;
x = t->atto; 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) void iopause(iopause_fd *x,unsigned len,struct taia *deadline,struct taia *stamp)
{ {
struct taia t;
int millisecs; int millisecs;
double d;
int i; int i;
if (taia_less(deadline,stamp)) if (taia_less(deadline,stamp))
millisecs = 0; millisecs = 0;
else { else {
uint64_t m;
struct taia t;
t = *stamp; t = *stamp;
taia_sub(&t,deadline,&t); taia_sub(&t, deadline, &t);
d = taia_approx(&t); millisecs = m = taia2millisec(&t);
if (d > 1000.0) d = 1000.0; if (m > 1000) millisecs = 1000;
millisecs = d * 1000.0 + 20.0; millisecs += 20;
} }
for (i = 0;i < len;++i) for (i = 0; i < len; ++i)
x[i].revents = 0; 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: 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 EAGAIN? are kernels really this dumb? */
/* XXX: how to handle EINVAL? when exactly can this happen? */ /* 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 ***/ /*** seek_set.c ***/
int seek_set(int fd,seek_pos pos) int seek_set(int fd,seek_pos pos)
{ {
if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0; if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0;
} }
#endif
/*** sig.c ***/ /*** sig.c ***/
int sig_alarm = SIGALRM; //int sig_alarm = SIGALRM;
int sig_child = SIGCHLD; //int sig_child = SIGCHLD;
int sig_cont = SIGCONT; //int sig_cont = SIGCONT;
int sig_hangup = SIGHUP; //int sig_hangup = SIGHUP;
int sig_int = SIGINT; //int sig_int = SIGINT;
int sig_pipe = SIGPIPE; //int sig_pipe = SIGPIPE;
int sig_term = SIGTERM; //int sig_term = SIGTERM;
void (*sig_defaulthandler)(int) = SIG_DFL; //void (*sig_defaulthandler)(int) = SIG_DFL;
void (*sig_ignorehandler)(int) = SIG_IGN; //void (*sig_ignorehandler)(int) = SIG_IGN;
/*** sig_block.c ***/ /*** sig_block.c ***/
@ -947,10 +1008,9 @@ unsigned str_chr(const char *s,int c)
ch = c; ch = c;
t = s; t = s;
for (;;) { for (;;) {
if (!*t) break; if (*t == ch) break; ++t; if (!*t) break;
if (!*t) break; if (*t == ch) break; ++t; if (*t == ch) break;
if (!*t) break; if (*t == ch) break; ++t; ++t;
if (!*t) break; if (*t == ch) break; ++t;
} }
return t - s; return t - s;
} }
@ -960,7 +1020,7 @@ unsigned str_chr(const char *s,int c)
int wait_nohang(int *wstat) 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; int r;
do do
r = waitpid(pid,wstat,0); r = waitpid(pid, wstat, 0);
while ((r == -1) && (errno == EINTR)); while ((r == -1) && (errno == EINTR));
return r; return r;
} }

View File

@ -107,46 +107,46 @@ extern int fifo_make(const char *,int);
/*** fmt.h ***/ /*** fmt.h ***/
#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */ //#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
#define FMT_LEN ((char *) 0) /* convenient abbreviation */ //#define FMT_LEN ((char *) 0) /* convenient abbreviation */
extern unsigned fmt_uint(char *,unsigned); //extern unsigned fmt_uint(char *,unsigned);
extern unsigned fmt_uint0(char *,unsigned,unsigned); //extern unsigned fmt_uint0(char *,unsigned,unsigned);
extern unsigned fmt_xint(char *,unsigned); //extern unsigned fmt_xint(char *,unsigned);
extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned); //extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned);
extern unsigned fmt_ushort(char *,unsigned short); //extern unsigned fmt_ushort(char *,unsigned short);
extern unsigned fmt_xshort(char *,unsigned short); //extern unsigned fmt_xshort(char *,unsigned short);
extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short); //extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short);
extern unsigned fmt_ulong(char *,unsigned long); //extern unsigned fmt_ulong(char *,unsigned long);
extern unsigned fmt_xlong(char *,unsigned long); //extern unsigned fmt_xlong(char *,unsigned long);
extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long); //extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long);
extern unsigned fmt_plusminus(char *,int); //extern unsigned fmt_plusminus(char *,int);
extern unsigned fmt_minus(char *,int); //extern unsigned fmt_minus(char *,int);
extern unsigned fmt_0x(char *,int); //extern unsigned fmt_0x(char *,int);
extern unsigned fmt_str(char *,const char *); //extern unsigned fmt_str(char *,const char *);
extern unsigned fmt_strn(char *,const char *,unsigned); //extern unsigned fmt_strn(char *,const char *,unsigned);
/*** tai.h ***/ /*** tai.h ***/
struct tai { struct tai {
uint64_t x; uint64_t x;
} ; };
#define tai_unix(t,u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u))) #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_add(struct tai *,const struct tai *,const struct tai *);
extern void tai_sub(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_less(t,u) ((t)->x < (u)->x)
#define TAI_PACK 8 #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_unpack(const char *,struct tai *);
extern void tai_uint(struct tai *,unsigned); extern void tai_uint(struct tai *,unsigned);
@ -158,14 +158,14 @@ struct taia {
struct tai sec; struct tai sec;
unsigned long nano; /* 0...999999999 */ unsigned long nano; /* 0...999999999 */
unsigned long atto; /* 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 void taia_now(struct taia *);
extern double taia_approx(const struct taia *); //extern double taia_approx(const struct taia *);
extern double taia_frac(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_add(struct taia *,const struct taia *,const struct taia *);
extern void taia_addsec(struct taia *,const struct taia *,int); 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 #define TAIA_PACK 16
extern void taia_pack(char *,const struct taia *); 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 //#define TAIA_FMTFRAC 19
extern unsigned taia_fmtfrac(char *,const struct taia *); //extern unsigned taia_fmtfrac(char *,const struct taia *);
extern void taia_uint(struct taia *,unsigned); extern void taia_uint(struct taia *,unsigned);
@ -187,10 +187,13 @@ extern void taia_uint(struct taia *,unsigned);
#define FMT_PTIME 30 #define FMT_PTIME 30
extern unsigned fmt_ptime(char *, struct taia *); /* NUL terminated */
extern unsigned fmt_taia(char *, struct taia *); extern void fmt_ptime30nul(char *, struct taia *);
/* NOT terminated! */
extern unsigned fmt_taia25(char *, struct taia *);
#ifdef UNUSED
/*** gen_alloc.h ***/ /*** gen_alloc.h ***/
#define GEN_ALLOC_typedef(ta,type,field,len,a) \ #define GEN_ALLOC_typedef(ta,type,field,len,a) \
@ -233,7 +236,6 @@ int ta_append(ta *x,const type *i) \
/*** stralloc.h ***/ /*** stralloc.h ***/
#if 0
GEN_ALLOC_typedef(stralloc,char,s,len,a) GEN_ALLOC_typedef(stralloc,char,s,len,a)
extern int stralloc_ready(stralloc *,unsigned); extern int stralloc_ready(stralloc *,unsigned);
@ -314,6 +316,7 @@ extern int readclose(int,stralloc *,unsigned);
/*** scan.h ***/ /*** scan.h ***/
#if 0
extern unsigned scan_uint(const char *,unsigned *); extern unsigned scan_uint(const char *,unsigned *);
extern unsigned scan_xint(const char *,unsigned *); extern unsigned scan_xint(const char *,unsigned *);
extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,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_long(const char *,long *);
extern unsigned scan_8long(const char *,unsigned long *); extern unsigned scan_8long(const char *,unsigned long *);
#endif
/*** seek.h ***/ /*** seek.h ***/
@ -345,30 +349,27 @@ typedef unsigned long seek_pos;
extern seek_pos seek_cur(int); 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_end(int);
extern int seek_trunc(int,seek_pos); 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 ***/ /*** sig.h ***/
extern int sig_alarm; //extern int sig_alarm;
extern int sig_child; //extern int sig_child;
extern int sig_cont; //extern int sig_cont;
extern int sig_hangup; //extern int sig_hangup;
extern int sig_int; //extern int sig_int;
extern int sig_pipe; //extern int sig_pipe;
extern int sig_term; //extern int sig_term;
extern void (*sig_defaulthandler)(int);
extern void (*sig_ignorehandler)(int);
extern void sig_catch(int,void (*)(int)); extern void sig_catch(int,void (*)(int));
#define sig_ignore(s) (sig_catch((s),sig_ignorehandler)) #define sig_ignore(s) (sig_catch((s),SIG_IGN))
#define sig_uncatch(s) (sig_catch((s),sig_defaulthandler)) #define sig_uncatch(s) (sig_catch((s),SIG_DFL))
extern void sig_block(int); extern void sig_block(int);
extern void sig_unblock(int); extern void sig_unblock(int);

View File

@ -307,10 +307,10 @@ static void startservice(struct svdir *s)
close(logpipe[0]); close(logpipe[0]);
} }
} }
sig_uncatch(sig_child); sig_uncatch(SIGCHLD);
sig_unblock(sig_child); sig_unblock(SIGCHLD);
sig_uncatch(sig_term); sig_uncatch(SIGTERM);
sig_unblock(sig_term); sig_unblock(SIGTERM);
execve(*run, run, environ); execve(*run, run, environ);
if (s->islog) if (s->islog)
fatal2_cannot("start log/", *run); fatal2_cannot("start log/", *run);
@ -406,10 +406,10 @@ int runsv_main(int argc, char **argv)
ndelay_on(selfpipe[0]); ndelay_on(selfpipe[0]);
ndelay_on(selfpipe[1]); ndelay_on(selfpipe[1]);
sig_block(sig_child); sig_block(SIGCHLD);
sig_catch(sig_child, s_child); sig_catch(SIGCHLD, s_child);
sig_block(sig_term); sig_block(SIGTERM);
sig_catch(sig_term, s_term); sig_catch(SIGTERM, s_term);
xchdir(dir); xchdir(dir);
svd[0].pid = 0; svd[0].pid = 0;
@ -533,11 +533,11 @@ int runsv_main(int argc, char **argv)
taia_uint(&deadline, 3600); taia_uint(&deadline, 3600);
taia_add(&deadline, &now, &deadline); taia_add(&deadline, &now, &deadline);
sig_unblock(sig_term); sig_unblock(SIGTERM);
sig_unblock(sig_child); sig_unblock(SIGCHLD);
iopause(x, 2+haslog, &deadline, &now); iopause(x, 2+haslog, &deadline, &now);
sig_block(sig_term); sig_block(SIGTERM);
sig_block(sig_child); sig_block(SIGCHLD);
while (read(selfpipe[0], &ch, 1) == 1) while (read(selfpipe[0], &ch, 1) == 1)
; ;

View File

@ -70,8 +70,8 @@ static void runsv(int no, char *name)
prog[0] = "runsv"; prog[0] = "runsv";
prog[1] = name; prog[1] = name;
prog[2] = 0; prog[2] = 0;
sig_uncatch(sig_hangup); sig_uncatch(SIGHUP);
sig_uncatch(sig_term); sig_uncatch(SIGTERM);
if (pgrp) setsid(); if (pgrp) setsid();
execvp(prog[0], prog); execvp(prog[0], prog);
//pathexec_run(*prog, prog, (char* const*)environ); //pathexec_run(*prog, prog, (char* const*)environ);
@ -197,8 +197,8 @@ int runsvdir_main(int argc, char **argv)
if (!argv || !*argv) usage(); if (!argv || !*argv) usage();
} }
sig_catch(sig_term, s_term); sig_catch(SIGTERM, s_term);
sig_catch(sig_hangup, s_hangup); sig_catch(SIGHUP, s_hangup);
svdir = *argv++; svdir = *argv++;
if (argv && *argv) { if (argv && *argv) {
rplog = *argv; rplog = *argv;
@ -276,12 +276,12 @@ int runsvdir_main(int argc, char **argv)
taia_uint(&deadline, check ? 1 : 5); taia_uint(&deadline, check ? 1 : 5);
taia_add(&deadline, &now, &deadline); taia_add(&deadline, &now, &deadline);
sig_block(sig_child); sig_block(SIGCHLD);
if (rplog) if (rplog)
iopause(io, 1, &deadline, &now); iopause(io, 1, &deadline, &now);
else else
iopause(0, 0, &deadline, &now); iopause(0, 0, &deadline, &now);
sig_unblock(sig_child); sig_unblock(SIGCHLD);
if (rplog && (io[0].revents | IOPAUSE_READ)) if (rplog && (io[0].revents | IOPAUSE_READ))
while (read(logpipe[0], &ch, 1) > 0) while (read(logpipe[0], &ch, 1) > 0)

View File

@ -326,28 +326,38 @@ int sv_main(int argc, char **argv)
service++; service++;
} }
if (*cbk) if (*cbk) {
for (;;) { for (;;) {
//TODO: tdiff resolution is way too high. seconds will be enough
taia_sub(&tdiff, &tnow, &tstart); taia_sub(&tdiff, &tnow, &tstart);
service = servicex; want_exit = 1; service = servicex; want_exit = 1;
for (i = 0; i < services; ++i, ++service) { for (i = 0; i < services; ++i, ++service) {
if (!*service) continue; if (!*service)
continue;
if ((**service != '/') && (**service != '.')) { if ((**service != '/') && (**service != '.')) {
if ((chdir(varservice) == -1) || (chdir(*service) == -1)) { if (chdir(varservice) == -1)
fail("cannot change to service directory"); goto chdir_failed;
*service = 0;
}
} else if (chdir(*service) == -1) {
fail("cannot change to service directory");
*service = 0;
} }
if (*service) { if (cbk(acts) != 0) *service = 0; else want_exit = 0; } if (chdir(*service) == -1) {
if (*service && taia_approx(&tdiff) > waitsec) { 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); 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 */ puts(""); /* will also flush the output */
if (kll) control("k"); if (kll)
*service = 0; control("k");
nullify_service:
*service = NULL;
} }
if (fchdir(curdir) == -1) if (fchdir(curdir) == -1)
fatal_cannot("change to original directory"); fatal_cannot("change to original directory");
@ -356,5 +366,6 @@ int sv_main(int argc, char **argv)
usleep(420000); usleep(420000);
taia_now(&tnow); taia_now(&tnow);
} }
}
return rc > 99 ? 99 : rc; return rc > 99 ? 99 : rc;
} }

View File

@ -100,11 +100,13 @@ static void warnx(char *m0, char *m1)
} }
static void pause_nomem(void) 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) 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) static void pause2cannot(char *m0, char *m1)
{ {
@ -115,7 +117,8 @@ static void pause2cannot(char *m0, char *m1)
static char* wstrdup(const char *str) static char* wstrdup(const char *str)
{ {
char *s; char *s;
while (!(s = strdup(str))) pause_nomem(); while (!(s = strdup(str)))
pause_nomem();
return s; return s;
} }
@ -135,12 +138,12 @@ static unsigned processorstart(struct logdir *ld)
int fd; int fd;
/* child */ /* child */
sig_uncatch(sig_term); sig_uncatch(SIGTERM);
sig_uncatch(sig_alarm); sig_uncatch(SIGALRM);
sig_uncatch(sig_hangup); sig_uncatch(SIGHUP);
sig_unblock(sig_term); sig_unblock(SIGTERM);
sig_unblock(sig_alarm); sig_unblock(SIGALRM);
sig_unblock(sig_hangup); sig_unblock(SIGHUP);
if (verbose) if (verbose)
bb_error_msg(INFO"processing: %s/%s", ld->name, ld->fnsave); 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) if (fd_move(5, fd) == -1)
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
// getenv("SHELL")?
prog[0] = "sh"; prog[0] = "sh";
prog[1] = "-c"; prog[1] = "-c";
prog[2] = ld->processor; prog[2] = ld->processor;
@ -180,10 +184,10 @@ static unsigned processorstop(struct logdir *ld)
char f[28]; char f[28];
if (ld->ppid) { if (ld->ppid) {
sig_unblock(sig_hangup); sig_unblock(SIGHUP);
while (wait_pid(&wstat, ld->ppid) == -1) while (wait_pid(&wstat, ld->ppid) == -1)
pause2cannot("wait for processor", ld->name); pause2cannot("wait for processor", ld->name);
sig_block(sig_hangup); sig_block(SIGHUP);
ld->ppid = 0; ld->ppid = 0;
} }
if (ld->fddir == -1) return 1; 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); bb_error_msg(WARNING"cannot unlink: %s/%s", ld->name, ld->fnsave);
while (rename("newstate", "state") == -1) while (rename("newstate", "state") == -1)
pause2cannot("rename state", ld->name); 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) while (fchdir(fdwdir) == -1)
pause1cannot("change to initial working directory"); pause1cannot("change to initial working directory");
return 1; return 1;
@ -242,11 +247,13 @@ static void rmoldest(struct logdir *ld)
errno = 0; errno = 0;
} }
} }
if (errno) warn2("cannot read directory", ld->name); if (errno)
warn2("cannot read directory", ld->name);
closedir(d); closedir(d);
if (ld->nmax && (n > ld->nmax)) { 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)) if ((*oldest == '@') && (unlink(oldest) == -1))
warn2("cannot unlink oldest logfile", ld->name); warn2("cannot unlink oldest logfile", ld->name);
} }
@ -276,9 +283,10 @@ static unsigned rotate(struct logdir *ld)
ld->fnsave[27] = '\0'; ld->fnsave[27] = '\0';
do { do {
taia_now(&now); taia_now(&now);
fmt_taia(ld->fnsave, &now); fmt_taia25(ld->fnsave, &now);
errno = 0; 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)) { if (ld->tmax && taia_less(&ld->trotate, &now)) {
taia_uint(&ld->trotate, ld->tmax); taia_uint(&ld->trotate, ld->tmax);
@ -523,9 +531,10 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
ld->fnsave[27] = '\0'; ld->fnsave[27] = '\0';
do { do {
taia_now(&now); taia_now(&now);
fmt_taia(ld->fnsave, &now); fmt_taia25(ld->fnsave, &now);
errno = 0; errno = 0;
} while ((stat(ld->fnsave, &st) != -1) || (errno != ENOENT)); stat(ld->fnsave, &st);
} while (errno != ENOENT);
while (rename("current", ld->fnsave) == -1) while (rename("current", ld->fnsave) == -1)
pause2cannot("rename current", ld->name); pause2cannot("rename current", ld->name);
rmoldest(ld); rmoldest(ld);
@ -608,15 +617,15 @@ static int buffer_pread(int fd, char *s, unsigned len)
while (1) { while (1) {
/* Comment? */ /* Comment? */
sig_unblock(sig_term); sig_unblock(SIGTERM);
sig_unblock(sig_child); sig_unblock(SIGCHLD);
sig_unblock(sig_alarm); sig_unblock(SIGALRM);
sig_unblock(sig_hangup); sig_unblock(SIGHUP);
iopause(&in, 1, &trotate, &now); iopause(&in, 1, &trotate, &now);
sig_block(sig_term); sig_block(SIGTERM);
sig_block(sig_child); sig_block(SIGCHLD);
sig_block(sig_alarm); sig_block(SIGALRM);
sig_block(sig_hangup); sig_block(SIGHUP);
i = safe_read(fd, s, len); i = safe_read(fd, s, len);
if (i >= 0) break; if (i >= 0) break;
if (errno != EAGAIN) { if (errno != EAGAIN) {
@ -764,14 +773,14 @@ int svlogd_main(int argc, char **argv)
in.events = IOPAUSE_READ; in.events = IOPAUSE_READ;
ndelay_on(in.fd); ndelay_on(in.fd);
sig_block(sig_term); sig_block(SIGTERM);
sig_block(sig_child); sig_block(SIGCHLD);
sig_block(sig_alarm); sig_block(SIGALRM);
sig_block(sig_hangup); sig_block(SIGHUP);
sig_catch(sig_term, sig_term_handler); sig_catch(SIGTERM, sig_term_handler);
sig_catch(sig_child, sig_child_handler); sig_catch(SIGCHLD, sig_child_handler);
sig_catch(sig_alarm, sig_alarm_handler); sig_catch(SIGALRM, sig_alarm_handler);
sig_catch(sig_hangup, sig_hangup_handler); sig_catch(SIGHUP, sig_hangup_handler);
logdirs_reopen(); logdirs_reopen();
@ -788,10 +797,10 @@ int svlogd_main(int argc, char **argv)
taia_now(&now); taia_now(&now);
switch (timestamp) { switch (timestamp) {
case 1: case 1:
fmt_taia(stamp, &now); fmt_taia25(stamp, &now);
break; break;
default: /* case 2: */ default: /* case 2: */
fmt_ptime(stamp, &now); fmt_ptime30nul(stamp, &now);
break; break;
} }
memcpy(line, stamp, 25); memcpy(line, stamp, 25);