int s6_svc_main (int argc, char const *const *argv, char const *optstring, char const *usage, char const *controldir) { char data[DATASIZE] ; unsigned int datalen = 0 ; register int r ; for (;;) { register int opt = subgetopt(argc, argv, optstring) ; if (opt == -1) break ; if (opt == '?') strerr_dieusage(100, usage) ; if (datalen >= DATASIZE) strerr_dief1x(100, "too many commands") ; data[datalen++] = opt ; } argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; if (!argc) strerr_dieusage(100, usage) ; { unsigned int arglen = str_len(*argv) ; unsigned int cdirlen = str_len(controldir) ; char tmp[arglen + cdirlen + 10] ; byte_copy(tmp, arglen, *argv) ; tmp[arglen] = '/' ; byte_copy(tmp + arglen + 1, cdirlen, controldir) ; byte_copy(tmp + arglen + 1 + cdirlen, 9, "/control") ; r = s6_svc_write(tmp, data, datalen) ; } if (r < 0) strerr_diefu2sys(111, "control ", *argv) ; else if (!r) strerr_diefu3x(100, "control ", *argv, ": supervisor not listening") ; return 0 ; }
int main (int argc, char const *const *argv) { mode_t mode = 0 ; unsigned int m ; PROG = "s6-chmod" ; if (argc < 3) strerr_dieusage(100, USAGE) ; if (!uint0_oscan(argv[1], &m)) strerr_dieusage(100, USAGE) ; if (m & 0001) mode |= S_IXOTH ; if (m & 0002) mode |= S_IWOTH ; if (m & 0004) mode |= S_IROTH ; if (m & 0010) mode |= S_IXGRP ; if (m & 0020) mode |= S_IWGRP ; if (m & 0040) mode |= S_IRGRP ; if (m & 0100) mode |= S_IXUSR ; if (m & 0200) mode |= S_IWUSR ; if (m & 0400) mode |= S_IRUSR ; if (m & 01000) mode |= S_ISVTX ; if (m & 02000) mode |= S_ISGID ; if (m & 04000) mode |= S_ISUID ; if (chmod(argv[2], mode) == -1) strerr_diefu2sys(111, "change mode of ", argv[2]) ; return 0 ; }
int main (int argc, char const *const *argv, char const *const *envp) { unsigned int fd ; int block = 1 ; PROG = "fdblock" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "n", &l) ; if (opt == -1) break ; switch (opt) { case 'n' : block = 0 ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if ((argc < 2) || !uint0_scan(argv[0], &fd)) strerr_dieusage(100, USAGE) ; if ((block ? ndelay_off(fd) : ndelay_on(fd)) < 0) strerr_diefu1sys(111, block ? "ndelay_off" : "ndelay_on") ; pathexec_run(argv[1], argv+1, envp) ; strerr_dieexec(111, argv[1]) ; }
int main (int argc, char const *const *argv) { stralloc sa = STRALLOC_ZERO ; int nl = 1 ; PROG = "s6-dirname" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "n", &l) ; if (opt == -1) break ; switch (opt) { case 'n' : nl = 0 ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) strerr_dieusage(100, USAGE) ; if (!sadirname(&sa, argv[0], str_len(argv[0]))) strerr_diefu2sys(111, "get dirname of ", argv[0]) ; if (nl && !stralloc_catb(&sa, "\n", 1)) strerr_diefu2sys(111, "get dirname of ", argv[0]) ; if (allwrite(1, sa.s, sa.len) < sa.len) strerr_diefu1sys(111, "write to stdout") ; return 0 ; }
int main (int argc, char const *const *argv) { unsigned int mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH ; PROG = "s6-mkfifo" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "m:", &l) ; if (opt == -1) break ; switch (opt) { case 'm': if (uint0_oscan(l.arg, &mode)) break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) strerr_dieusage(100, USAGE) ; umask(S_IXUSR|S_IXGRP|S_IXOTH) ; for (; *argv ; argv++) if (mkfifo(*argv, mode) < 0) strerr_diefu2sys(111, "mkfifo ", *argv) ; return 0 ; }
int main (int argc, char const *const *argv, char const *const *envp) { int incr = 10 ; int strict = 0 ; PROG = "s6-nice" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "Iin:", &l) ; if (opt == -1) break ; switch (opt) { case 'I' : strict = 0 ; break ; case 'i' : strict = 1 ; break ; case 'n': if (!int_scan(l.arg, &incr)) strerr_dieusage(100, USAGE) ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) strerr_dieusage(100, USAGE) ; errno = 0 ; if ((nice(incr) < 0) && errno) { char fmt[1+UINT_FMT] ; fmt[int_fmt(fmt, incr)] = 0 ; if (strict) strerr_diefu2sys(111, "nice to ", fmt) ; else strerr_warnwu2sys("nice to ", fmt) ; } pathexec_run(argv[0], argv, envp) ; strerr_dieexec((errno == ENOENT) ? 127 : 126, argv[0]) ; }
int main (int argc, char const *const *argv, char const *const *envp) { int uid = -1, gid = -1 ; PROG = "s6-chown" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "Uu:g:", &l) ; if (opt == -1) break ; switch (opt) { case 'u': { unsigned int u ; if (!uint0_scan(l.arg, &u)) strerr_dieusage(100, USAGE) ; uid = u ; break ; } case 'g': { unsigned int g ; if (!uint0_scan(l.arg, &g)) strerr_dieusage(100, USAGE) ; gid = g ; break ; } case 'U': { unsigned int x ; char const *s = env_get2(envp, "UID") ; if (!s) strerr_dienotset(100, "UID") ; if (!uint0_scan(s, &x)) strerr_dieinvalid(100, "UID") ; uid = x ; s = env_get2(envp, "GID") ; if (!s) strerr_dienotset(100, "GID") ; if (!uint0_scan(s, &x)) strerr_dieinvalid(100, "GID") ; gid = x ; break ; } default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) strerr_dieusage(100, USAGE) ; if (chown(*argv, uid, gid) == -1) strerr_diefu2sys(111, "chown ", argv[0]) ; return 0 ; }
int main (int argc, char const *const *argv, char const *const *envp) { unsigned int len ; int fd; PROG = "cgjoin" ; if (argc < 3) strerr_dieusage(100, USAGE) ; len = strlen(argv[1]) ; { char path[len + PREFIXLEN + SUFFIXLEN + 1] ; char pid[UINT_FMT + 1] ; unsigned int k ; // format this program's PID as a string k = uint_fmt(pid, getpid()) ; pid[k++] = 0 ; // Build up the path to the cgrup filesystem memcpy(path, PREFIX, PREFIXLEN); memcpy(path + PREFIXLEN, argv[1], len); memcpy(path + PREFIXLEN + len, SUFFIX, SUFFIXLEN); path[PREFIXLEN + len + SUFFIXLEN] = 0 ; doparents(path); // Write the pid to the cgroup's tasks file if ((fd = open_write(path)) < 0) strerr_dief2x(100, "cannot open cgroup tasks file: ", path) ; fd_write(fd, pid, k) ; close(fd) ; } pathexec_run(argv[2], argv+2, envp) ; strerr_dieexec(111, argv[2]) ; }
int main (int argc, char const *const *argv) { genalloc lines = GENALLOC_ZERO ; /* array of stralloc */ char sep = '\n' ; int flagcheck = 0 ; PROG = "s6-sort" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "bcfru0", &l) ; if (opt == -1) break ; switch (opt) { case 'b' : flagnoblanks = 1 ; break ; case 'c' : flagcheck = 1 ; break ; case 'f' : comp = &strncasecmp ; break ; case 'r' : flagreverse = 1 ; break ; case 'u' : flaguniq = 1 ; break ; case '0' : sep = '\0' ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if (slurplines(&lines, sep) < 0) strerr_diefu1sys(111, "read from stdin") ; if (flagcheck) return !check(genalloc_s(stralloc, &lines), genalloc_len(stralloc, &lines)) ; qsort(genalloc_s(stralloc, &lines), genalloc_len(stralloc, &lines), sizeof(stralloc), (qsortcmp_t_ref)&sacmp) ; if (flaguniq) uniq(&lines) ; if (!outputlines(genalloc_s(stralloc, &lines), genalloc_len(stralloc, &lines))) strerr_diefu1sys(111, "write to stdout") ; return 0 ; }
static int setit (char const *h) { struct taia a ; if (!timestamp_scan(h, &a)) strerr_dieusage(100, USAGE) ; if (!taia_setnow(&a)) strerr_diefu1sys(111, "taia_setnow") ; return 0 ; }
int main (int argc, char const *const *argv) { PROG = "s6-cleanfifodir" ; if (argc < 2) strerr_dieusage(100, USAGE) ; if (!ftrigw_clean(argv[1])) strerr_diefu2sys(111, "clean up fifodir at ", argv[1]) ; return 0 ; }
int main (int argc, char const *const *argv) { PROG = "s6-rename" ; if (argc < 3) strerr_dieusage(100, USAGE) ; if (rename(argv[1], argv[2]) == -1) strerr_diefu4sys(111, "rename ", argv[1], " to ", argv[2]) ; return 0 ; }
int main (int argc, char const *const *argv, char const *const *envp) { PROG = "cd" ; if (argc < 3) strerr_dieusage(100, USAGE) ; if (chdir(argv[1]) == -1) strerr_diefu2sys(111, "chdir to ", argv[1]) ; pathexec_run(argv[2], argv+2, envp) ; strerr_dieexec(111, argv[2]) ; }
int main (int argc, char const *const *argv) { int fd ; PROG = "s6-freeramdisk" ; if (argc < 2) strerr_dieusage(100, USAGE) ; fd = open(argv[1], O_RDWR) ; if (fd < 0) strerr_diefu3sys(111, "open ", argv[1], " in read-write mode") ; if (ioctl(fd, BLKFLSBUF) < 0) strerr_diefu2sys(111, "ioctl ", argv[1]) ; return 0 ; }
int main (int argc, char const *const *argv) { s6_svstatus_t status ; char fmt[UINT_FMT] ; int isup, normallyup ; PROG = "s6-svstat" ; if (argc < 2) strerr_dieusage(100, USAGE) ; argv++ ; argc-- ; if (!s6_svstatus_read(*argv, &status)) strerr_diefu2sys(111, "read status for ", *argv) ; { struct stat st ; unsigned int dirlen = str_len(*argv) ; char fn[dirlen + 6] ; byte_copy(fn, dirlen, *argv) ; byte_copy(fn + dirlen, 6, "/down") ; if (stat(fn, &st) == -1) if (errno != ENOENT) strerr_diefu2sys(111, "stat ", fn) ; else normallyup = 1 ; else normallyup = 0 ; } taia_now_g() ; if (taia_future(&status.stamp)) taia_copynow(&status.stamp) ; taia_sub(&status.stamp, &STAMP, &status.stamp) ; isup = status.pid && !status.flagfinishing ; if (isup) { buffer_putnoflush(buffer_1small,"up (pid ", 8) ; buffer_putnoflush(buffer_1small, fmt, uint_fmt(fmt, status.pid)) ; buffer_putnoflush(buffer_1small, ") ", 2) ; } else buffer_putnoflush(buffer_1small, "down ", 5) ; buffer_putnoflush(buffer_1small, fmt, uint64_fmt(fmt, status.stamp.sec.x)) ; buffer_putnoflush(buffer_1small," seconds", 8) ; if (isup && !normallyup) buffer_putnoflush(buffer_1small, ", normally down", 15) ; if (!isup && normallyup) buffer_putnoflush(buffer_1small, ", normally up", 13) ; if (isup && status.flagpaused) buffer_putnoflush(buffer_1small, ", paused", 8) ; if (!isup && (status.flagwant == 'u')) buffer_putnoflush(buffer_1small, ", want up", 10) ; if (isup && (status.flagwant == 'd')) buffer_putnoflush(buffer_1small, ", want down", 12) ; if (buffer_putflush(buffer_1small, "\n", 1) == -1) strerr_diefu1sys(111, "write to stdout") ; return 0 ; }
static void doit (int res, char const *arg) { struct rlimit r ; if (getrlimit(res, &r) < 0) strerr_diefu1sys(111, "getrlimit") ; if ((arg[0] == '=') && !arg[1]) r.rlim_cur = r.rlim_max ; else { uint64 n ; if (!uint640_scan(arg, &n)) strerr_dieusage(100, USAGE) ; if (n > (uint64)r.rlim_max) n = (uint64)r.rlim_max ; r.rlim_cur = (rlim_t)n ; } if (setrlimit(res, &r) == -1) strerr_diefu1sys(111, "setrlimit") ; }
int main (int argc, char const *const *argv) { tain_t deadline, tto ; ftrigr_t a = FTRIGR_ZERO ; uint16 id ; char pack[2] = " \n" ; PROG = "s6-ftrig-wait" ; { unsigned int t = 0 ; for (;;) { register int opt = subgetopt(argc, argv, "t:") ; if (opt == -1) break ; switch (opt) { case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ; default : strerr_dieusage(100, USAGE) ; } } if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ; argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; } if (argc < 2) strerr_dieusage(100, USAGE) ; tain_now_g() ; tain_add_g(&deadline, &tto) ; if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ; id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ; if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ; if (ftrigr_wait_or_g(&a, &id, 1, &deadline, &pack[0]) == -1) strerr_diefu2sys((errno == ETIMEDOUT) ? 1 : 111, "match regexp on ", argv[1]) ; if (allwrite(1, pack, 2) < 2) strerr_diefu1sys(111, "write to stdout") ; return 0 ; }
int main (int argc, char const *const *argv) { stralloc src = STRALLOC_ZERO ; PROG = "s6-unquote-filter" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "qQvwd:", &l) ; if (opt == -1) break ; switch (opt) { case 'q': strictness = 0 ; break ; case 'Q': strictness = 1 ; break ; case 'v': strictness = 2 ; break ; case 'w': strictness = 3 ; break ; case 'd': delim = l.arg ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } delimlen = str_len(delim) ; for (;;) { int r ; src.len = 0 ; r = skagetln(buffer_0f1, &src, '\n') ; if (!r) break ; if (r < 0) { if (errno != EPIPE) strerr_diefu1sys(111, "read from stdin") ; } else src.len-- ; if (!doit(src.s, src.len)) { if (buffer_putalign(buffer_1, src.s, src.len) < (int)src.len) strerr_diefu1sys(111, "write to stdout") ; } if (r > 0) { if (buffer_putalign(buffer_1, "\n", 1) < 1) strerr_diefu1sys(111, "write to stdout") ; } } return 0 ; }
int main (int argc, char const *const *argv, char const *const *envp) { int df = 0 ; PROG = "heredoc" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "d", &l) ; if (opt == -1) break ; switch (opt) { case 'd' : df = 1 ; break ; default : dieusage() ; } } argc -= l.ind ; argv += l.ind ; } if (argc < 3) dieusage() ; { int fd[2] ; unsigned int fdr ; int pid ; if (!uint0_scan(argv[0], &fdr)) strerr_dieusage(100, USAGE) ; if (pipe(fd) < 0) strerr_diefu1sys(111, "pipe") ; pid = df ? doublefork() : fork() ; switch (pid) { case -1: strerr_diefu2sys(111, df ? "double" : "", "fork") ; case 0: { unsigned int len = str_len(argv[1]) ; PROG = "heredoc (child)" ; fd_close(fd[0]) ; if (allwrite(fd[1], argv[1], len) < len) strerr_diefu1sys(111, "allwrite") ; return 0 ; } } fd_close(fd[1]) ; if (fd_move((int)fdr, fd[0]) == -1) strerr_diefu2sys(111, "read on fd ", argv[0]) ; } pathexec_run(argv[2], argv+2, envp) ; strerr_dieexec(111, argv[2]) ; }
int main (int argc, char const *const *argv) { unsigned int verbosity = 1 ; tain_t deadline ; PROG = "s6-rc-dryrun" ; { unsigned int t = 1000 ; subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "v:t:", &l) ; if (opt == -1) break ; switch (opt) { case 'v': if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; case 't': if (!uint0_scan(l.arg, &t)) dieusage() ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; tain_from_millisecs(&deadline, t) ; } if (!argc) dieusage() ; if (verbosity) { buffer_puts(buffer_1, PROG) ; buffer_put(buffer_1, ":", 1) ; for (; *argv ; argv++) { buffer_put(buffer_1, " ", 1) ; buffer_puts(buffer_1, *argv) ; } buffer_putflush(buffer_1, "\n", 1) ; } tain_now_g() ; tain_add_g(&deadline, &deadline) ; deepsleepuntil_g(&deadline) ; return 0 ; }
int main (int argc, char const *const *argv) { unsigned int count = 0 ; flags_t flags = FLAGS_ZERO ; PROG = "s6-grep" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "Fcnqv", &l) ; if (opt == -1) break ; switch (opt) { case 'F': flags.fixed = 1 ; break ; case 'c': flags.count = 1 ; break ; case 'n': flags.num = 1 ; break ; case 'q': flags.quiet = 1 ; break ; case 'v': flags.not = 1 ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) strerr_dieusage(100, USAGE) ; { stralloc line = STRALLOC_ZERO ; struct sredfa *re = 0 ; unsigned int num = 0 ; unsigned int arglen = 0 ; if (flags.fixed) arglen = str_len(argv[0]) ; else { re = sredfa_new() ; if (!re) strerr_diefu1sys(111, "sredfa_new") ; if (!sredfa_from_regexp(re, argv[0])) strerr_diefu1sys(111, "compile regular expression") ; } for (;;) { register int r ; line.len = 0 ; r = skagetln(buffer_0f1, &line, '\n') ; if (!r) break ; if (r < 0) { if ((errno != EPIPE) || !stralloc_catb(&line, "\n", 1)) strerr_diefu1sys(111, "read from stdin") ; } num++ ; r = flags.fixed ? (str_strn(line.s, line.len-1, argv[0], arglen) < line.len - 1) : sredfa_match(re, line.s, line.len-1) ; if (r < 0) { int e = errno ; buffer_flush(buffer_1) ; errno = e ; strerr_diefu1sys(111, "match line against pattern") ; } if (r ^ flags.not) { count++ ; if (!flags.quiet && !flags.count) { if (flags.num) { char fmt[UINT_FMT] ; register unsigned int n = uint_fmt(fmt, num) ; fmt[n++] = ':' ; if (buffer_putalign(buffer_1, fmt, n) < (int)n) strerr_diefu1sys(111, "write to stdout") ; } if (buffer_putalign(buffer_1, line.s, line.len) < (int)line.len) strerr_diefu1sys(111, "write to stdout") ; } } } if (flags.quiet) return !count ; stralloc_free(&line) ; if (!flags.fixed) sredfa_delete(re) ; } if (flags.count) { char fmt[UINT_FMT] ; register unsigned int n = uint_fmt(fmt, count) ; fmt[n++] = '\n' ; if (buffer_putalign(buffer_1, fmt, n) < (int)n) strerr_diefu1sys(111, "write to stdout") ; } return !count ; }
int main (int argc, char const *const *argv) { genalloc list = GENALLOC_ZERO ; /* array of diuint */ char delim = '\t' ; unsigned long what = 0 ; PROG = "s6-cut" ; { subgetopt_t l = SUBGETOPT_ZERO ; int flagnodel = 1 ; for (;;) { register int opt = subgetopt_r(argc, argv, "nsb:c:f:d:", &l) ; if (opt == -1) break ; switch (opt) { case 'n': break ; /* ignored */ case 's': flagnodel = 0 ; break ; case 'd': delim = *l.arg ; break ; case 'b': case 'c': { if (what) strerr_dieusage(100, USAGE) ; what = 2 ; scanlist(&list, l.arg) ; break ; } case 'f': { if (what) strerr_dieusage(100, USAGE) ; what = 4 ; scanlist(&list, l.arg) ; break ; } default : strerr_dieusage(100, USAGE) ; } } what += flagnodel ; argc -= l.ind ; argv += l.ind ; } if (!genalloc_len(diuint, &list)) strerr_dieusage(100, USAGE) ; diuintalloc_normalize(&list) ; if (!argc) { if (!doit(0, genalloc_s(diuint, &list), genalloc_len(diuint, &list), what, delim)) strerr_diefu1sys(111, "cut stdin") ; } else { for (; *argv ; argv++) { if ((*argv[0] == '-') && !*argv[1]) { if (!doit(0, genalloc_s(diuint, &list), genalloc_len(diuint, &list), what, delim)) strerr_diefu1sys(111, "process stdin") ; } else { int fd = open_readb(*argv) ; if (fd == -1) strerr_diefu3sys(111, "open ", *argv, " for reading") ; if (!doit(fd, genalloc_s(diuint, &list), genalloc_len(diuint, &list), what, delim)) strerr_diefu2sys(111, "cut ", *argv) ; fd_close(fd) ; } } } return 0 ; }
int main (int argc, char *const *argv) { stralloc3 blah = STRALLOC3_ZERO ; PROG = "s6-update-symlinks" ; if (argc < 3) strerr_dieusage(100, USAGE) ; { register char *const *p = argv + 1 ; for (; *p ; p++) if (**p != '/') strerr_dieusage(100, USAGE) ; } { register unsigned int i = str_len(argv[1]) ; while (i && (argv[1][i-1] == '/')) argv[1][--i] = 0 ; if (!i) strerr_diefu1x(100, "replace root directory") ; } if (!makeuniquename(&blah.dst, argv[1], MAGICNEW)) strerr_diefu2sys(111, "make random unique name based on ", argv[1]) ; if ((unlink(blah.dst.s) == -1) && (errno != ENOENT)) strerr_diefu2sys(111, "unlink ", blah.dst.s) ; { char *const *p = argv + 2 ; for (; *p ; p++) { register int r ; blah.src.len = 0 ; if (!stralloc_cats(&blah.src, *p) || !stralloc_0(&blah.src)) strerr_diefu1sys(111, "make stralloc") ; r = addlink(&blah, 0, 0) ; if (r < 0) { stralloc_free(&blah.tmp) ; stralloc_free(&blah.src) ; cleanup(&blah.dst, 0) ; stralloc_free(&blah.dst) ; if (r == CONFLICT) strerr_dief4x(100, "destination ", errdst.s, " conflicts with source ", errsrc.s) ; else strerr_dief2sys(111, "error processing ", *p) ; } } } stralloc_free(&blah.tmp) ; if (rename(blah.dst.s, argv[1]) == -1) /* be atomic if possible */ { blah.src.len = 0 ; if (!makeuniquename(&blah.src, argv[1], MAGICOLD)) { cleanup(&blah.dst, 0) ; strerr_diefu2sys(111, "make random unique name based on ", argv[1]) ; } if (rename(argv[1], blah.src.s) == -1) { cleanup(&blah.dst, 0) ; strerr_diefu4sys(111, "rename ", argv[1], " to ", blah.src.s) ; } /* XXX: unavoidable race condition here: argv[1] does not exist */ if (rename(blah.dst.s, argv[1]) == -1) { rename(blah.src.s, argv[1]) ; cleanup(&blah.dst, 0) ; strerr_diefu4sys(111, "rename ", blah.dst.s, " to ", argv[1]) ; } stralloc_free(&blah.dst) ; if (rm_rf_in_tmp(&blah.src, 0) == -1) strerr_warnwu2sys("remove old directory ", blah.src.s) ; stralloc_free(&blah.src) ; } return 0 ; }
int main (int argc, char const *const *argv, char const *const *envp) { PROG = "s6-softlimit" ; for (;;) { register int opt = sgetopt(argc, argv, "a:c:d:f:l:m:o:p:r:s:t:") ; if (opt == -1) break ; switch (opt) { case 'a' : #ifdef RLIMIT_AS doit(RLIMIT_AS, subgetopt_here.arg) ; #endif #ifdef RLIMIT_VMEM doit(RLIMIT_VMEM, subgetopt_here.arg) ; #endif break ; case 'c' : #ifdef RLIMIT_CORE doit(RLIMIT_CORE, subgetopt_here.arg) ; #endif break ; case 'd' : #ifdef RLIMIT_DATA doit(RLIMIT_DATA, subgetopt_here.arg) ; #endif break ; case 'f' : #ifdef RLIMIT_FSIZE doit(RLIMIT_FSIZE, subgetopt_here.arg) ; #endif break ; case 'l' : #ifdef RLIMIT_MEMLOCK doit(RLIMIT_MEMLOCK, subgetopt_here.arg) ; #endif break ; case 'm' : #ifdef RLIMIT_DATA doit(RLIMIT_DATA, subgetopt_here.arg) ; #endif #ifdef RLIMIT_STACK doit(RLIMIT_STACK, subgetopt_here.arg) ; #endif #ifdef RLIMIT_MEMLOCK doit(RLIMIT_MEMLOCK, subgetopt_here.arg) ; #endif #ifdef RLIMIT_VMEM doit(RLIMIT_VMEM, subgetopt_here.arg) ; #endif #ifdef RLIMIT_AS doit(RLIMIT_AS, subgetopt_here.arg) ; #endif break ; case 'o' : #ifdef RLIMIT_NOFILE doit(RLIMIT_NOFILE, subgetopt_here.arg) ; #endif #ifdef RLIMIT_OFILE doit(RLIMIT_OFILE, subgetopt_here.arg) ; #endif break ; case 'p' : #ifdef RLIMIT_NPROC doit(RLIMIT_NPROC, subgetopt_here.arg) ; #endif break ; case 'r' : #ifdef RLIMIT_RSS doit(RLIMIT_RSS, subgetopt_here.arg) ; #endif break ; case 's' : #ifdef RLIMIT_STACK doit(RLIMIT_STACK, subgetopt_here.arg) ; #endif break ; case 't' : #ifdef RLIMIT_CPU doit(RLIMIT_CPU, subgetopt_here.arg) ; #endif break ; } } argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; if (!argc) strerr_dieusage(100, USAGE) ; pathexec_run(argv[0], argv, envp) ; strerr_dieexec(111, argv[0]) ; }
int main (int argc, char const *const *argv, char const *const *envp) { unsigned int kbufsz = 65536, verbosity = 1 ; char const *linevar = 0 ; char const *targ = 0 ; PROG = "s6-devd" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "qvb:l:t:", &l) ; if (opt == -1) break ; switch (opt) { case 'q' : if (verbosity) verbosity-- ; break ; case 'v' : verbosity++ ; break ; case 'b' : if (!uint0_scan(l.arg, &kbufsz)) dieusage() ; break ; case 'l' : linevar = l.arg ; break ; case 't' : if (!check_targ(l.arg)) dieusage() ; targ = l.arg ; break ; default : dieusage() ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) strerr_dieusage(100, USAGE) ; { unsigned int m = 0, pos = 0 ; char fmt[UINT_FMT * 3] ; char const *newargv[argc + 15] ; newargv[m++] = S6_LINUX_UTILS_BINPREFIX "s6-uevent-listener" ; if (verbosity != 1) { newargv[m++] = "-v" ; newargv[m++] = fmt + pos ; pos += uint_fmt(fmt + pos, verbosity) ; fmt[pos++] = 0 ; } if (kbufsz != 65536) { newargv[m++] = "-b" ; newargv[m++] = fmt + pos ; pos += uint_fmt(fmt + pos, kbufsz) ; fmt[pos++] = 0 ; } newargv[m++] = "--" ; newargv[m++] = S6_LINUX_UTILS_BINPREFIX "s6-uevent-spawner" ; if (verbosity != 1) { newargv[m++] = "-v" ; newargv[m++] = fmt + pos ; pos += uint_fmt(fmt + pos, verbosity) ; fmt[pos++] = 0 ; } if (linevar) { newargv[m++] = "-l" ; newargv[m++] = linevar ; } if (targ) { newargv[m++] = "-t" ; newargv[m++] = targ ; } newargv[m++] = "--" ; while (*argv) newargv[m++] = *argv++ ; newargv[m++] = 0 ; pathexec_run(newargv[0], newargv, envp) ; strerr_dieexec(111, newargv[0]) ; } }
int main (int argc, char const *const *argv) { tain_t deadline ; int sfd ; PROG = "bevt_relayd" ; if (argc < 2) strerr_dieusage(100, USAGE) ; if (ndelay_on(0) < 0) strerr_diefu2sys(111, "ndelay_on ", "0") ; if (ndelay_on(1) < 0) strerr_diefu2sys(111, "ndelay_on ", "1") ; if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; sfd = selfpipe_init() ; if (sfd < 0) strerr_diefu1sys(111, "selfpipe_init") ; { sigset_t set ; sigemptyset(&set) ; sigaddset(&set, SIGCHLD) ; sigaddset(&set, SIGTERM) ; sigaddset(&set, SIGQUIT) ; sigaddset(&set, SIGHUP) ; sigaddset(&set, SIGABRT) ; sigaddset(&set, SIGINT) ; if (selfpipe_trapset(&set) < 0) strerr_diefu1sys(111, "trap signals") ; } if(bevt_relay_db_init(argv[1])<0) strerr_diefu1sys(111, "db init failed") ; tain_now_g() ; tain_addsec_g(&deadline, 2) ; if (!skaclient_server_01x_init_g(BEVT_RELAY_BANNER1, BEVT_RELAY_BANNER1_LEN, BEVT_RELAY_BANNER2, BEVT_RELAY_BANNER2_LEN, &deadline)) strerr_diefu1sys(111, "sync with client") ; for (;;) { register unsigned int n = 0 ; iopause_fd x[6 + n] ; int r ; if(mfd<0) handle_connect_central(); tain_add_g(&deadline, &tain_infinite_relative) ; x[0].fd = 0 ; x[0].events = IOPAUSE_EXCEPT | IOPAUSE_READ ; x[1].fd = 1 ; x[1].events = IOPAUSE_EXCEPT | (unixmessage_sender_isempty(unixmessage_sender_1) ? 0 : IOPAUSE_WRITE ) ; x[2].fd = sfd ; x[2].events = IOPAUSE_READ ; x[3].fd = bozclient_sfd(¢ral_client_g); x[3].events = IOPAUSE_READ ; x[4].fd = bozclient_sfd(¢ral_client_g); x[4].events = (bozclient_siswritable(¢ral_client_g) ? IOPAUSE_WRITE : 0) ; x[5].fd = unixmessage_sender_fd(unixmessage_sender_x) ; x[5].events = (unixmessage_sender_isempty(unixmessage_sender_x) ? 0 : IOPAUSE_WRITE) ; r = iopause_g(x, 5 + n, &deadline) ; if (r < 0) { cleanup() ; strerr_diefu1sys(111, "iopause") ; } /* client closed */ if ((x[0].revents | x[1].revents) & IOPAUSE_EXCEPT) break ; /* client is sync reading */ if (x[1].revents & IOPAUSE_WRITE) { if (!unixmessage_sender_flush(unixmessage_sender_1) && !error_isagain(errno)) { cleanup() ; strerr_diefu1sys(111, "flush sync out") ; } } /* client is async rzading */ if (x[5].revents & IOPAUSE_WRITE) { if (!unixmessage_sender_flush(unixmessage_sender_x) && !error_isagain(errno)) { cleanup() ; strerr_diefu1sys(111, "flush async out") ; } } /* signals arrived */ if (x[2].revents & (IOPAUSE_READ | IOPAUSE_EXCEPT)) handle_signals() ; /* main socket read or close */ if (x[3].revents & IOPAUSE_READ) { handle_close_central(); continue; } /* main socket close */ if (x[4].revents & IOPAUSE_WRITE) { bozclient_flush(¢ral_client_g); } /* main socket close */ if (x[5].revents & IOPAUSE_WRITE) { if (!unixmessage_sender_flush(unixmessage_sender_x) && !error_isagain(errno)) { cleanup() ; strerr_diefu1sys(111, "flush stdout") ; } } /* client is sync writing */ if (!unixmessage_receiver_isempty(unixmessage_receiver_0) || x[0].revents & IOPAUSE_READ) { if (unixmessage_handle(unixmessage_receiver_0, &bevt_relay_parse_prot_cmd, 0) < 0) { if (errno == EPIPE) break ; /* normal exit */ cleanup() ; strerr_diefu1sys(111, "handle messages from client") ; } } } cleanup() ; return 0 ; }
int main (int argc, char const *const *argv, char const *const *envp) { iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ; struct taia deadline, tto ; ftrigr_t a = FTRIGR_ZERO ; int pid ; uint16 id ; PROG = "s6-ftrig-listen1" ; { unsigned int t = 0 ; for (;;) { register int opt = subgetopt(argc, argv, "t:") ; if (opt == -1) break ; switch (opt) { case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ; default : strerr_dieusage(100, USAGE) ; } } if (t) taia_from_millisecs(&tto, t) ; else tto = infinitetto ; argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; } if (argc < 3) strerr_dieusage(100, USAGE) ; taia_now_g() ; taia_add_g(&deadline, &tto) ; if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ; id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ; if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ; x[0].fd = selfpipe_init() ; if (x[0].fd < 0) strerr_diefu1sys(111, "selfpipe_init") ; if (selfpipe_trap(SIGCHLD) < 0) strerr_diefu1sys(111, "selfpipe_trap") ; if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "sig_ignore") ; x[1].fd = ftrigr_fd(&a) ; pid = fork() ; switch (pid) { case -1 : strerr_diefu1sys(111, "fork") ; case 0 : { PROG = "s6-ftrig-listen1 (child)" ; pathexec_run(argv[2], argv+2, envp) ; strerr_dieexec(111, argv[2]) ; } } for (;;) { char dummy ; register int r = ftrigr_check(&a, id, &dummy) ; if (r < 0) strerr_diefu1sys(111, "ftrigr_check") ; if (r) break ; r = iopause_g(x, 2, &deadline) ; if (r < 0) strerr_diefu1sys(111, "iopause") ; else if (!r) { errno = ETIMEDOUT ; strerr_diefu1sys(1, "get expected event") ; } if (x[0].revents & IOPAUSE_READ) handle_signals() ; if (x[1].revents & IOPAUSE_READ) { if (ftrigr_update(&a) < 0) strerr_diefu1sys(111, "ftrigr_update") ; } } return 0 ; }
int main (int argc, char const *const *argv) { iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ; PROG = "s6-svscan" ; { subgetopt_t l = SUBGETOPT_ZERO ; unsigned int t = 5000 ; for (;;) { register int opt = subgetopt_r(argc, argv, "t:c:", &l) ; if (opt == -1) break ; switch (opt) { case 't' : if (uint0_scan(l.arg, &t)) break ; case 'c' : if (uint0_scan(l.arg, &max)) break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; if (t) tain_from_millisecs(&defaulttimeout, t) ; else defaulttimeout = tain_infinite_relative ; if (max < 2) max = 2 ; } /* Init phase. If something fails here, we can die, because it means that something is seriously wrong with the system, and we can't run correctly anyway. */ if (argc && (chdir(argv[0]) < 0)) strerr_diefu1sys(111, "chdir") ; x[1].fd = s6_supervise_lock(S6_SVSCAN_CTLDIR) ; x[0].fd = selfpipe_init() ; if (x[0].fd < 0) strerr_diefu1sys(111, "selfpipe_init") ; if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; { sigset_t set ; sigemptyset(&set) ; sigaddset(&set, SIGCHLD) ; sigaddset(&set, SIGALRM) ; sigaddset(&set, SIGTERM) ; sigaddset(&set, SIGHUP) ; sigaddset(&set, SIGQUIT) ; sigaddset(&set, SIGABRT) ; sigaddset(&set, SIGINT) ; sigaddset(&set, SIGUSR1) ; if (selfpipe_trapset(&set) < 0) strerr_diefu1sys(111, "trap signals") ; } { struct svinfo_s blob[max] ; /* careful with that stack, Eugene */ services = blob ; tain_now_g() ; /* Loop phase. From now on, we must not die. Temporize on recoverable errors, and panic on serious ones. */ while (cont) { int r ; tain_add_g(&deadline, &defaulttimeout) ; reap() ; scan() ; killthem() ; r = iopause_g(x, 2, &deadline) ; if (r < 0) panic("iopause") ; else if (!r) wantscan = 1 ; else { if ((x[0].revents | x[1].revents) & IOPAUSE_EXCEPT) { errno = EIO ; panic("check internal pipes") ; } if (x[0].revents & IOPAUSE_READ) handle_signals() ; if (x[1].revents & IOPAUSE_READ) handle_control(x[1].fd) ; } } /* Finish phase. */ selfpipe_finish() ; killthem() ; reap() ; } { char const *eargv[3] = { FINISH_PROG, finish_arg, 0 } ; execve(eargv[0], (char **)eargv, (char *const *)environ) ; } panicnosp("exec finish script " FINISH_PROG) ; }
int main (int argc, char const *const *argv) { iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ; PROG = "s6-supervise" ; if (argc < 2) strerr_dieusage(100, USAGE) ; if (chdir(argv[1]) < 0) strerr_diefu2sys(111, "chdir to ", argv[1]) ; { register unsigned int proglen = str_len(PROG) ; register unsigned int namelen = str_len(argv[1]) ; char progname[proglen + namelen + 2] ; byte_copy(progname, proglen, PROG) ; progname[proglen] = ' ' ; byte_copy(progname + proglen + 1, namelen + 1, argv[1]) ; PROG = progname ; if (!fd_sanitize()) strerr_diefu1sys(111, "sanitize stdin and stdout") ; x[1].fd = s6_supervise_lock(S6_SUPERVISE_CTLDIR) ; if (!ftrigw_fifodir_make(S6_SUPERVISE_EVENTDIR, getegid(), 0)) strerr_diefu2sys(111, "mkfifodir ", S6_SUPERVISE_EVENTDIR) ; x[0].fd = selfpipe_init() ; if (x[0].fd == -1) strerr_diefu1sys(111, "init selfpipe") ; if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; { sigset_t set ; sigemptyset(&set) ; sigaddset(&set, SIGTERM) ; sigaddset(&set, SIGHUP) ; sigaddset(&set, SIGQUIT) ; sigaddset(&set, SIGCHLD) ; if (selfpipe_trapset(&set) < 0) strerr_diefu1sys(111, "trap signals") ; } if (!ftrigw_clean(S6_SUPERVISE_EVENTDIR)) strerr_warnwu2sys("ftrigw_clean ", S6_SUPERVISE_EVENTDIR) ; { struct stat st ; if (stat("down", &st) == -1) { if (errno != ENOENT) strerr_diefu1sys(111, "stat down") ; } else status.flagwantup = 0 ; if (stat("nosetsid", &st) == -1) { if (errno != ENOENT) strerr_diefu1sys(111, "stat nosetsid") ; } else flagsetsid = 0 ; } tain_now_g() ; settimeout(0) ; tain_copynow(&status.stamp) ; announce() ; ftrigw_notifyb_nosig(S6_SUPERVISE_EVENTDIR, "s", 1) ; while (cont) { register int r = iopause_g(x, 2, &deadline) ; if (r < 0) strerr_diefu1sys(111, "iopause") ; else if (!r) (*actions[state][V_TIMEOUT])() ; else { if ((x[0].revents | x[1].revents) & IOPAUSE_EXCEPT) strerr_diefu1x(111, "iopause: trouble with pipes") ; if (x[0].revents & IOPAUSE_READ) handle_signals() ; else if (x[1].revents & IOPAUSE_READ) handle_control(x[1].fd) ; } } ftrigw_notifyb_nosig(S6_SUPERVISE_EVENTDIR, "x", 1) ; } return 0 ; }