Esempio n. 1
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 ;
}
Esempio n. 2
0
int main (int argc, char const *const *argv, char const *const *envp)
{
  unsigned int backlog = 20 ;
  int flagreuse = 1 ;
  PROG = "s6-ipcserver-socketbinder" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "Ddb:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'D' : flagreuse = 0 ; break ;
        case 'd' : flagreuse = 1 ; break ;
        case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (argc < 2) dieusage() ;
  close(0) ;
  if (ipc_stream()) strerr_diefu1sys(111, "create socket") ;
  {
    mode_t m = umask(0) ;
    if ((flagreuse ? ipc_bind_reuse(0, argv[0]) : ipc_bind(0, argv[0])) < 0)
      strerr_diefu2sys(111, "bind to ", argv[0]) ;
    umask(m) ;
  }
  if (ipc_listen(0, backlog) < 0) strerr_diefu2sys(111, "listen to ", argv[0]) ;

  pathexec_run(argv[1], argv + 1, envp) ;
  strerr_dieexec(111, argv[1]) ;
}
Esempio n. 3
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]) ;
}
Esempio n. 4
0
int main (int argc, char const *const *argv)
{
  stralloc sa = STRALLOC_ZERO ;
  unsigned int n = 8 ;
  PROG = "s6-uniquename" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "n:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'n' : if (!uint0_scan(l.arg, &n)) usage() ; break ;
        default : usage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (argc < 1) usage() ;
  if (!stralloc_cats(&sa, argv[0])) strerr_diefu1sys(111, "stralloc_cats") ;
  if ((n ? random_sauniquename(&sa, n) : sauniquename(&sa)) < 0)
    strerr_diefu1sys(111, "make unique name") ;
  if (!stralloc_catb(&sa, "\n", 1)) strerr_diefu1sys(111, "stralloc_cats") ;
  if (allwrite(1, sa.s, sa.len) < sa.len) strerr_diefu1sys(111, "write to stdout") ;
  return 0 ;
}
Esempio n. 5
0
int main (int argc, char const *const *argv, char const *const *envp)
{
  char const *s = env_get2(envp, VAR) ;
  unsigned int fd = 1 ;
  unsigned int timeout = 0 ;
  int df = 1, keep = 0 ;
  PROG = "sdnotify-wrapper" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "d:ft:k", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'd' : if (!uint0_scan(l.arg, &fd)) dieusage() ; break ;
        case 'f' : df = 0 ; break ;
        case 't' : if (!uint0_scan(l.arg, &timeout)) dieusage() ; break ;
        case 'k' : keep = 1 ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;

  if (!s) xpathexec_run(argv[0], argv, envp) ;
  else
  {
    pid_t parent = getpid() ;
    pid_t child ;
    int p[2] ;
    if (pipe(p) < 0) strerr_diefu1sys(111, "pipe") ;
    child = df ? doublefork() : fork() ;
    if (child < 0) strerr_diefu1sys(111, df ? "doublefork" : "fork") ;
    else if (!child)
    {
      PROG = "sdnotify-wrapper (child)" ;
      close(p[1]) ;
      return run_child(p[0], timeout, parent, s) ;
    }
    close(p[0]) ;
    if (fd_move((int)fd, p[1]) < 0) strerr_diefu1sys(111, "move descriptor") ;
    if (keep) xpathexec_run(argv[0], argv, envp) ;
    else xpathexec_r(argv, envp, env_len(envp), VAR, sizeof(VAR)) ;
  }
}
Esempio n. 6
0
int
main (int argc, char * const argv[], char const * const *envp)
{
    PROG = "aa-ctty";
    int fd = 0;
    int steal = 0;

    for (;;)
    {
        struct option longopts[] = {
            { "double-output",      no_argument,        NULL,   'D' },
            { "fd",                 required_argument,  NULL,   'f' },
            { "help",               no_argument,        NULL,   'h' },
            { "steal",              no_argument,        NULL,   's' },
            { "version",            no_argument,        NULL,   'V' },
            { NULL, 0, 0, 0 }
        };
        int c;

        c = getopt_long (argc, argv, "+Df:hsV", longopts, NULL);
        if (c == -1)
            break;
        switch (c)
        {
            case 'D':
                aa_set_double_output (1);
                break;

            case 'f':
                if (!uint0_scan (optarg, &fd))
                    aa_strerr_diefu1sys (1, "set fd");
                break;

            case 'h':
                dieusage (0);

            case 's':
                steal = 1;
                break;

            case 'V':
                aa_die_version ();

            default:
                dieusage (1);
        }
    }
    argc -= optind;
    argv += optind;

    if (argc == 0)
        dieusage (1);

    if (ioctl (fd, TIOCSCTTY, steal) < 0)
        aa_strerr_warnu1sys ("set controlling terminal");

    pathexec_run (argv[0], (char const * const *) argv, envp);
    aa_strerr_dieexec (111, argv[0]);
}
Esempio n. 7
0
int main (int argc, char const *const *argv, char const *const *envp)
{
  int fd, fd2 ;
  unsigned int flags = 0 ;
  int what = -1 ;
  int changemode = 0 ;
  PROG = "redirfd" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "rwuacxnb", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'r' : what = O_RDONLY ; flags &= ~(O_APPEND|O_CREAT|O_TRUNC|O_EXCL) ; break ;
        case 'w' : what = O_WRONLY ; flags |= O_CREAT|O_TRUNC ; flags &= ~(O_APPEND|O_EXCL) ; break ;
        case 'u' : what = O_RDWR ; flags &= ~(O_APPEND|O_CREAT|O_TRUNC|O_EXCL) ; break ;
        case 'a' : what = O_WRONLY ; flags |= O_CREAT|O_APPEND ; flags &= ~(O_TRUNC|O_EXCL) ; break ;
        case 'c' : what = O_WRONLY ; flags |= O_APPEND ; flags &= ~(O_CREAT|O_TRUNC|O_EXCL) ; break ;
        case 'x' : what = O_WRONLY ; flags |= O_CREAT|O_EXCL ; flags &= ~(O_APPEND|O_TRUNC) ; break ;
        case 'n' : flags |= O_NONBLOCK ; break ;
        case 'b' : changemode = 1 ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if ((argc < 3) || (what == -1)) dieusage() ;
  if (!uint0_scan(argv[0], (unsigned int *)&fd)) dieusage() ;
  flags |= what ;
  fd2 = open3(argv[1], flags, 0666) ;
  if ((fd2 == -1) && (what == O_WRONLY) && (errno == ENXIO))
  {
    register int e ;
    int fdr = open_read(argv[1]) ;
    if (fdr == -1) strerr_diefu2sys(111, "open_read ", argv[1]) ;
    fd2 = open3(argv[1], flags, 0666) ;
    e = errno ;
    fd_close(fdr) ;
    errno = e ;
  }
  if (fd2 == -1) strerr_diefu2sys(111, "open ", argv[1]) ;
  if (fd_move(fd, fd2) == -1)
  {
    char fmt[UINT_FMT] ;
    fmt[uint_fmt(fmt, fd2)] = 0 ;
    strerr_diefu4sys(111, "move fd ", fmt, " to fd ", argv[0]) ;
  }
  if (changemode)
  {
    if (((flags & O_NONBLOCK) ? ndelay_off(fd) : ndelay_on(fd)) < 0)
      strerr_diefu1sys(111, "change blocking mode") ;
  }
  pathexec_run(argv[2], argv+2, envp) ;
  strerr_dieexec(111, argv[2]) ;
}
Esempio n. 8
0
int s6rc_read_uint (char const *file, unsigned int *u)
{
  char buf[UINT_FMT + 1] ;
  register int r = openreadnclose(file, buf, UINT_FMT) ;
  if (r < 0) return (errno == ENOENT) ? 0 : -1 ;
  buf[byte_chr(buf, r, '\n')] = 0 ;
  if (!uint0_scan(buf, u)) return (errno = EINVAL, -1) ;
  return 1 ;
}
Esempio n. 9
0
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 ;
}
Esempio n. 10
0
int main (int argc, char const *const *argv, char const *const *envp)
{
  unsigned int timeout = 0 ;
  int dodelete = 0 ;
  PROG = "s6-fdholder-retrieve" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "Dt:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'D' : dodelete = 1 ; break ;
        case 't' : if (!uint0_scan(l.arg, &timeout)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
    if (argc < 3) dieusage() ;
  }

  {
    char const *newargv[13 + argc] ;
    unsigned int m = 0 ;
    char fmtt[UINT_FMT] ;
    newargv[m++] = S6_BINPREFIX "s6-ipcclient" ;
    newargv[m++] = "-l0" ;
    newargv[m++] = "--" ;
    newargv[m++] = *argv++ ;
    newargv[m++] = S6_BINPREFIX "s6-fdholder-retrievec" ;
    if (dodelete) newargv[m++] = "-D" ;
    if (timeout)
    {
      fmtt[uint_fmt(fmtt, timeout)] = 0 ;
      newargv[m++] = "-t" ;
      newargv[m++] = fmtt ;
    }
    newargv[m++] = "--" ;
    newargv[m++] = *argv++ ;
    newargv[m++] = EXECLINE_EXTBINPREFIX "fdclose" ;
    newargv[m++] = "6" ;
    newargv[m++] = EXECLINE_EXTBINPREFIX "fdclose" ;
    newargv[m++] = "7" ;
    while (*argv) newargv[m++] = *argv++ ;
    newargv[m++] = 0 ;
    pathexec_run(newargv[0], newargv, envp) ;
    strerr_dieexec(111, newargv[0]) ;
  }
}
Esempio n. 11
0
int main (int argc, char const *const *argv)
{
  char fmt[IP6_FMT] ;
  char ip[16] ;
  unsigned int n ;
  unsigned int i = 0 ;
  unsigned int what = 0 ;
  int finite = 0 ;
  PROG = "s6-randomip" ;
  for (;;)
  {
    register int opt = subgetopt(argc, argv, "46n:") ;
    if (opt == -1) break ;
    switch (opt)
    {
      case '4' : what |= 1 ; break ;
      case '6' : what |= 2 ; break ;
      case 'n' :
        if (!uint0_scan(subgetopt_here.arg, &n)) dieusage() ;
        finite = 1 ;
        break ;
      default : dieusage() ;
    }
  }
  argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
  if (!what) what = 1 ;
  what = 1 << (1 << what) ; 
  if (!badrandom_init()) strerr_diefu1sys(111, "init RNG") ;
  for (i = 0 ; !finite || (i < n) ; i++)
  {
    unsigned int len = what ;
    if (len > 16)
    {
      char c ;
      if (badrandom_string(&c, 1) < 1)
        strerr_diefu1sys(111, "badrandom_string") ;
      len = (c & 1) ? 16 : 4 ;
    }
    if (badrandom_string(ip, len) < 4)
      strerr_diefu1sys(111, "badrandom_string") ;
    len = (len == 16) ? ip6_fmt(fmt, ip) : ip4_fmt(fmt, ip) ;
    fmt[len++] = '\n' ;
    if (buffer_put(buffer_1, fmt, len) < (int)len)
      strerr_diefu1sys(111, "write to stdout") ;
  }
  if (!buffer_flush(buffer_1))
    strerr_diefu1sys(111, "write to stdout") ;
  return 0 ;
}
Esempio n. 12
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]) ;
}
Esempio n. 13
0
int skaserver2_sync (bufalloc *b, char const *before, unsigned int beforelen, char const *after, unsigned int afterlen, struct taia const *deadline, struct taia *stamp)
{
  char const *x = env_get("SKACLIENT_ADDITIONAL_FDS") ;
  if (x)
  {
    unsigned int fd ;
    if (!uint0_scan(x, &fd)) return (errno = EINVAL, 0) ;
    if (!bufalloc_put(bufalloc_1, before, beforelen)) return 0 ;
    if (!timed_bufalloc_flush(bufalloc_1, deadline, stamp)) return 0 ;
    if (ndelay_on(fd) < 0) return 0 ;
    bufalloc_init(b, &fd_write, (int)fd) ;
    if (!bufalloc_put(b, after, afterlen)) return 0 ;
    if (!timed_bufalloc_flush(b, deadline, stamp)) return 0 ;
    return 1 ;
  }
  else
  {
    int p[2] ;
    if (pipe(p) < 0) return 0 ;
    if (ndelay_on(p[1]) < 0) goto fail ;
    if (!bufalloc_put(bufalloc_1, before, beforelen)) goto fail ;
    {
      char pack[4] ;
      uint32_pack_big(pack, 1) ;
      if (!bufalloc_put(bufalloc_1, pack, 4)) goto fail ;
    }
    if (!timed_bufalloc_flush(bufalloc_1, deadline, stamp)) goto fail ;
    if (!timed_ancil_fd_send(1, p[0], deadline, stamp)) goto fail ;
    if (!bufalloc_put(bufalloc_1, after, afterlen)) goto fail ;
    if (!timed_bufalloc_flush(bufalloc_1, deadline, stamp)) goto fail ;
#ifndef HASANCILAUTOCLOSE
    fd_close(p[0]) ;
#endif
    bufalloc_init(b, &fd_write, p[1]) ;
    return 1 ;
   fail:
    {
      register int e = errno ;
      fd_close(p[1]) ;
      fd_close(p[0]) ;
      errno = e ;
    }
    return 0 ;
  }
}
Esempio n. 14
0
int exlsn_exlp (int argc, char const **argv, char const *const *envp, exlsn_t *info)
{
  subgetopt_t localopt = SUBGETOPT_ZERO ;
  unsigned int nmin = 0 ;
  int n ;
  for (;;)
  {
    register int opt = subgetopt_r(argc, argv, "P:", &localopt) ;
    if (opt < 0) break ;
    switch (opt)
    {
      case 'P' : if (uint0_scan(localopt.arg, &nmin)) break ;
      default : return -3 ;
    }
  }
  argc -= localopt.ind ; argv += localopt.ind ;
  if (!argc) return -3 ;
  n = exlp(nmin, envp, info) ;
  if (n < 0) return n ;
  return localopt.ind ;
}
Esempio n. 15
0
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 ;
}
Esempio n. 16
0
int
main (int argc, char * const argv[])
{
    PROG = "aa-start";
    const char *path_repo = "/run/services";
    const char *path_list = NULL;
    int i;

    aa_secs_timeout = DEFAULT_TIMEOUT_SECS;
    for (;;)
    {
        struct option longopts[] = {
            { "double-output",      no_argument,        NULL,   'D' },
            { "help",               no_argument,        NULL,   'h' },
            { "listdir",            required_argument,  NULL,   'l' },
            { "dry-list",           no_argument,        NULL,   'n' },
            { "repodir",            required_argument,  NULL,   'r' },
            { "timeout",            required_argument,  NULL,   't' },
            { "version",            no_argument,        NULL,   'V' },
            { "verbose",            no_argument,        NULL,   'v' },
            { "no-wants",           no_argument,        NULL,   'W' },
            { NULL, 0, 0, 0 }
        };
        int c;

        c = getopt_long (argc, argv, "Dhl:nr:t:VvW", longopts, NULL);
        if (c == -1)
            break;
        switch (c)
        {
            case 'D':
                aa_set_double_output (1);
                break;

            case 'h':
                dieusage (0);

            case 'l':
                unslash (optarg);
                path_list = optarg;
                break;

            case 'n':
                if (mode & AA_MODE_IS_DRY)
                    mode |= AA_MODE_IS_DRY_FULL;
                else
                    mode |= AA_MODE_IS_DRY;
                break;

            case 'r':
                unslash (optarg);
                path_repo = optarg;
                break;

            case 't':
                if (!uint0_scan (optarg, &aa_secs_timeout))
                    aa_strerr_diefu2sys (ERR_IO, "set default timeout to ", optarg);
                break;

            case 'V':
                aa_die_version ();

            case 'v':
                verbose = 1;
                break;

            case 'W':
                no_wants = 1;
                break;

            default:
                dieusage (1);
        }
    }
    argc -= optind;
    argv += optind;

    cols = get_cols (1);
    is_utf8 = is_locale_utf8 ();

    if (!path_list && argc < 1)
        dieusage (1);

    if (aa_init_repo (path_repo, (mode & AA_MODE_IS_DRY) ? AA_REPO_READ : AA_REPO_WRITE) < 0)
        aa_strerr_diefu2sys (ERR_IO, "init repository ", path_repo);

    if (path_list)
    {
        stralloc sa = STRALLOC_ZERO;
        int r;

        if (*path_list != '/' && *path_list != '.')
            stralloc_cats (&sa, LISTDIR_PREFIX);
        stralloc_catb (&sa, path_list, strlen (path_list) + 1);
        r = aa_scan_dir (&sa, 1, it_start, NULL);
        stralloc_free (&sa);
        if (r < 0)
            aa_strerr_diefu3sys (-r, "read list directory ",
                    (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list,
                    (*path_list != '/' && *path_list != '.') ? path_list : "");
    }

    tain_now_g ();

    for (i = 0; i < argc; ++i)
        if (str_equal (argv[i], "-"))
        {
            if (process_names_from_stdin ((names_cb) add_service, NULL) < 0)
                aa_strerr_diefu1sys (ERR_IO, "process names from stdin");
        }
        else
            add_service (argv[i], NULL);

    mainloop (mode, scan_cb);

    if (!(mode & AA_MODE_IS_DRY))
    {
        aa_bs_noflush (AA_OUT, "\n");
        put_title (1, PROG, "Completed.", 1);
        aa_show_stat_nb (nb_already, "Already up", ANSI_HIGHLIGHT_GREEN_ON);
        aa_show_stat_nb (nb_done, "Started", ANSI_HIGHLIGHT_GREEN_ON);
        show_stat_service_names (&ga_timedout, "Timed out", ANSI_HIGHLIGHT_RED_ON);
        show_stat_service_names (&ga_failed, "Failed", ANSI_HIGHLIGHT_RED_ON);
        show_stat_service_names (&ga_depend, "Dependency failed", ANSI_HIGHLIGHT_RED_ON);
        aa_show_stat_names (aa_names.s, &ga_io, "I/O error", ANSI_HIGHLIGHT_RED_ON);
        aa_show_stat_names (aa_names.s, &ga_unknown, "Unknown", ANSI_HIGHLIGHT_RED_ON);
        aa_show_stat_names (aa_names.s, &ga_skipped, "Skipped", ANSI_HIGHLIGHT_YELLOW_ON);
    }

    genalloc_free (int, &ga_timedout);
    genalloc_free (int, &ga_failed);
    genalloc_free (int, &ga_depend);
    genalloc_free (size_t, &ga_io);
    genalloc_free (size_t, &ga_unknown);
    genalloc_free (size_t, &ga_skipped);
    genalloc_free (pid_t, &ga_pid);
    genalloc_free (int, &aa_tmp_list);
    genalloc_free (int, &aa_main_list);
    stralloc_free (&aa_names);
    genalloc_deepfree (struct progress, &ga_progress, free_progress);
    aa_free_services (close_fd);
    genalloc_free (iopause_fd, &ga_iop);
    return rc;
}
Esempio n. 17
0
File: s6-svc.c Progetto: 8l/s6
int main (int argc, char const *const *argv, char const *const *envp)
{
  char data[DATASIZE+1] = "-" ;
  unsigned int datalen = 1 ;
  unsigned int timeout = 0 ;
  char updown[3] = "-\0" ;
  PROG = "s6-svc" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "abqhkti12pcoduxOXyT:w:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'a' :
        case 'b' :
        case 'q' :
        case 'h' :
        case 'k' :
        case 't' :
        case 'i' :
        case '1' :
        case '2' :
        case 'p' :
        case 'c' :
        case 'o' :
        case 'd' :
        case 'u' :
        case 'x' :
        case 'O' :
        case 'X' :
        case 'y' :
        {
          if (datalen >= DATASIZE) strerr_dief1x(100, "too many commands") ;
          data[datalen++] = opt ;
          break ;
        }
        case 'T' : if (!uint0_scan(l.arg, &timeout)) dieusage() ; break ;
        case 'w' :
        {
          if (byte_chr("dDuUrR", 6, l.arg[0]) >= 6) dieusage() ;
          updown[1] = l.arg[0] ;
          break ;
        }
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;
  if (argc > 1) strerr_warn1x("ignoring extra arguments") ;

  if (datalen <= 1) return 0 ;
  if (updown[1] == 'U' || updown[1] == 'R')
  {
    unsigned int arglen = str_len(argv[0]) ;
    char fn[arglen + 17] ;
    byte_copy(fn, arglen, argv[0]) ;
    byte_copy(fn + arglen, 17, "/notification-fd") ;
    if (access(fn, F_OK) < 0)
    {
      if (errno != ENOENT) strerr_diefu2sys(111, "access ", fn) ;
      updown[1] = updown[1] == 'U' ? 'u' : 'r' ;
      strerr_warnw2x(fn, " not present - ignoring request for readiness notification") ;
    }
  }

  if (updown[1])
  {
    char const *newargv[11] ;
    unsigned int m = 0 ;
    char fmt[UINT_FMT] ;
    newargv[m++] = S6_BINPREFIX "s6-svlisten1" ;
    newargv[m++] = updown ;
    if (timeout)
    {
      fmt[uint_fmt(fmt, timeout)] = 0 ;
      newargv[m++] = "-t" ;
      newargv[m++] = fmt ;
    }
    newargv[m++] = "--" ;
    newargv[m++] = argv[0] ;
    newargv[m++] = S6_BINPREFIX "s6-svc" ;
    newargv[m++] = data ;
    newargv[m++] = "--" ;
    newargv[m++] = argv[0] ;
    newargv[m++] = 0 ;
    pathexec_run(newargv[0], newargv, envp) ;
    strerr_dieexec(111, newargv[0]) ;
  }
  else
  {
    register int r = s6_svc_writectl(argv[0], S6_SUPERVISE_CTLDIR, data + 1, datalen - 1) ;
    if (r < 0) strerr_diefu2sys(111, "control ", argv[0]) ;
    else if (!r) strerr_diefu3x(100, "control ", argv[0], ": supervisor not listening") ;
  }
  return 0 ;
}
Esempio n. 18
0
int main (int argc, char const *const *argv)
{
  tain_t deadline, tto ;
  unsigned int dirlen ;
  char const *live = S6RC_LIVE_BASE ;
  char const *compiled = S6RC_COMPILED_BASE ;
  PROG = "s6-rc-init" ;
  {
    unsigned int t = 0 ;
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "c:l:t:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'c' : compiled = l.arg ; break ;
        case 'l' : live = l.arg ; break ;
        case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
    if (t) tain_from_millisecs(&tto, t) ;
    else tto = tain_infinite_relative ;
  }
  if (!argc) dieusage() ;

  if (compiled[0] != '/')
    strerr_dief2x(100, compiled, " is not an absolute path") ;
  if (live[0] != '/')
    strerr_dief2x(100, live, " is not an absolute path") ;
  if (argv[0][0] != '/')
    strerr_dief2x(100, argv[0], " is not an absolute path") ;

  tain_now_g() ;
  tain_add_g(&deadline, &tto) ;

  if (!s6rc_sanitize_dir(&satmp, live, &dirlen)) dienomem() ;
  llen = satmp.len ;
  if (!stralloc_cats(&satmp, ":initial") || !stralloc_0(&satmp))
    strerr_diefu1sys(111, "stralloc_catb") ;

  {
    int fdlock ;
    int fdcompiled ;
    int ok ;
    s6rc_db_t db ;
    unsigned int n ;
    char lfn[llen + 13] ;
    char cfn[llen + 23] ;


   /* Create the real dir, lock it, symlink */

    unlink(live) ;
    rm_rf(satmp.s) ;
    if (mkdir(satmp.s, 0755) < 0) strerr_diefu2sys(111, "mkdir ", satmp.s) ;
    if (!s6rc_lock(satmp.s, 2, &fdlock, 0, 0, 0))
    {
      char tmp[satmp.len] ;
      byte_copy(tmp, satmp.len, satmp.s) ;
      cleanup() ;
      strerr_diefu2sys(111, "take lock on ", tmp) ;
    }
    byte_copy(lfn, llen, satmp.s) ;
    lfn[llen] = 0 ;
    if (symlink(satmp.s + dirlen, lfn) < 0)
    {
      char tmp[satmp.len - dirlen] ;
      byte_copy(tmp, satmp.len - dirlen, satmp.s + dirlen) ;
      cleanup() ;
      strerr_diefu4sys(111, "symlink ", tmp, " to ", lfn) ;
    }
    

   /* compiled */

    fdcompiled = open_readb(compiled) ;
    if (fdcompiled < 0)
    {
      cleanup() ;
      strerr_diefu2sys(111, "open ", compiled) ;
    }
    byte_copy(lfn + llen, 10, "/compiled") ;
    if (symlink(compiled, lfn) < 0)
    {
      cleanup() ;
      strerr_diefu4sys(111, "symlink ", compiled, " to ", lfn) ;
    }


   /* scandir */

    byte_copy(lfn + llen + 1, 8, "scandir") ;
    if (symlink(argv[0], lfn) < 0)
    {
      cleanup() ;
      strerr_diefu4sys(111, "symlink ", argv[0], " to ", lfn) ;
    }


   /* state */

    byte_copy(lfn + llen + 1, 6, "state") ;
    {
      register int r = s6rc_db_read_sizes(fdcompiled, &db) ;
      if (r <= 0)
      {
        cleanup() ;
        if (r < 0) strerr_diefu2sys(111, "read database size in ", compiled) ;
        else strerr_dief2x(4, "invalid database size in ", compiled) ;
      }
      close(fdcompiled) ;
      n = db.nshort + db.nlong ;
      {
        char zero[n] ;
        byte_zero(zero, n) ;
        if (!openwritenclose_unsafe(lfn, zero, n))
        {
          cleanup() ;
          strerr_diefu2sys(111, "write ", lfn) ;
        }
      }
    }


   /* servicedirs */

    byte_copy(lfn + llen + 1, 12, "servicedirs") ;
    byte_copy(cfn, llen + 1, lfn) ;
    byte_copy(cfn + llen + 1, 21, "compiled/servicedirs") ;
    if (!hiercopy(cfn, lfn))
    {
      cleanup() ;
      strerr_diefu4sys(111, "recursively copy ", cfn, " to ", lfn) ;
    }


   /* start the supervisors */

    lfn[llen] = 0 ;
    ok = s6rc_servicedir_manage_g(lfn, &deadline) ;
    if (!ok)
    {
      cleanup() ;
      strerr_diefu3sys(111, "supervise service directories in ", lfn, "/servicedirs") ;
    }
    if (ok & 2)
      strerr_warnw3x("s6-svscan not running on ", lfn, "/scandir") ;
  }  
  return 0 ;
}
Esempio n. 19
0
int main (int argc, char const **argv, char const *const *envp)
{
  iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ;
  tain_t deadline, tto ;
  ftrigr_t a = FTRIGR_ZERO ;
  int argc1 ;
  unsigned int i = 0 ;
  char or = 0 ;
  PROG = "s6-ftrig-listen" ;
  {
    unsigned int t = 0 ;
    for (;;)
    {
      register int opt = subgetopt(argc, argv, "aot:") ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'a' : or = 0 ; break ;
        case 'o' : or = 1 ; break ;
        case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
        default : dieusage() ;
      }
    }
    if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ;
    argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
  }
  if (argc < 4) dieusage() ;
  argc1 = el_semicolon(argv) ;
  if (!argc1 || (argc1 & 1) || (argc == argc1 + 1)) dieusage() ;
  if (argc1 >= argc) strerr_dief1x(100, "unterminated fifodir+regex block") ;
  tain_now_g() ;
  tain_add_g(&deadline, &tto) ;
  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") ;

  if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
  x[1].fd = ftrigr_fd(&a) ;

  {
    int pid = 0 ;
    unsigned int idlen = argc1 >> 1 ;
    uint16 ids[idlen] ;
    for (; i < idlen ; i++)
    {
      ids[i] = ftrigr_subscribe_g(&a, argv[i<<1], argv[(i<<1)+1], 0, &deadline) ;
      if (!ids[i]) strerr_diefu4sys(111, "subscribe to ", argv[i<<1], " with regexp ", argv[(i<<1)+1]) ;
    }

    pid = child_spawn0(argv[argc1 + 1], argv + argc1 + 1, envp) ;
    if (!pid) strerr_diefu2sys(111, "spawn ", argv[argc1 + 1]) ;

    for (;;)
    {
      register int r ;
      i = 0 ;
      while (i < idlen)
      {
        char dummy ;
        r = ftrigr_check(&a, ids[i], &dummy) ;
        if (r < 0) strerr_diefu1sys(111, "ftrigr_check") ;
        else if (!r) i++ ;
        else if (or) idlen = 0 ;
        else ids[i] = ids[--idlen] ;
      }
      if (!idlen) 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 ;
}
Esempio n. 20
0
File: s6-svlisten.c Progetto: 8l/s6
int main (int argc, char const **argv, char const *const *envp)
{
  tain_t deadline, tto ;
  int spfd ;
  int argc1 ;
  int or = 0 ;
  int wantup = 1, wantready = 0, wantrestart = 0 ;
  PROG = "s6-svlisten" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    unsigned int t = 0 ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "uUdDrRaot:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'u' : wantup = 1 ; wantready = 0 ; break ;
        case 'U' : wantup = 1 ; wantready = 1 ; break ;
        case 'd' : wantup = 0 ; wantready = 0 ; break ;
        case 'D' : wantup = 0 ; wantready = 1 ; break ;
        case 'r' : wantrestart = 1 ; wantready = 0 ; break ;
        case 'R' : wantrestart = 1 ; wantready = 1 ; break ;
        case 'a' : or = 0 ; break ;
        case 'o' : or = 1 ; break ;
        case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
    if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ;
  }
  if (argc < 3) dieusage() ;

  argc1 = el_semicolon(argv) ;
  if (!argc1 || argc == argc1 + 1) dieusage() ;
  if (argc1 >= argc) strerr_dief1x(100, "unterminated servicedir block") ;
  if (wantrestart && or)
  {
    or = 0 ;
    strerr_warnw3x("-o is unsupported when combined with -", wantready ? "R" : "r", "- using -a instead") ;
  }

  tain_now_g() ;
  tain_add_g(&deadline, &tto) ;
  spfd = s6_svlisten_selfpipe_init() ;

  {
    s6_svlisten_t foo = S6_SVLISTEN_ZERO ;
    pid_t pid ;
    uint16 ids[argc1] ;
    unsigned char upstate[bitarray_div8(argc1)] ;
    unsigned char readystate[bitarray_div8(argc1)] ;
    s6_svlisten_init(argc1, argv, &foo, ids, upstate, readystate, &deadline) ;
    pid = child_spawn0(argv[argc1 + 1], argv + argc1 + 1, envp) ;
    if (!pid) strerr_diefu2sys(111, "spawn ", argv[argc1 + 1]) ;
    if (wantrestart)
    {
      argc1 = s6_svlisten_loop(&foo, 0, 1, or, &deadline, spfd, &s6_svlisten_signal_handler) ;
      if (argc1) return argc1 ;
      wantup = 1 ;
    }
    return s6_svlisten_loop(&foo, wantup, wantready, or, &deadline, spfd, &s6_svlisten_signal_handler) ;
  }
}
Esempio n. 21
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]) ;
  }
}
Esempio n. 22
0
int main (int argc, char const *const *argv, char const *const *envp)
{
  unsigned int strict = el_getstrict() ;
  unsigned int b = 0 ;
  unsigned int n = 0 ;
  unsigned int sharp ;
  unsigned int i = 0 ;
  PROG = "shift" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "n:b:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'n' :
          if (!uint0_scan(l.arg, &n)) dieusage() ;
          i = 1 ;
          break ;
        case 'b' :
          if (!uint0_scan(l.arg, &b)) dieusage() ;
          i = 1 ;
          break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;
  if (i) i = 0 ; else n = 1 ;
  {
    char const *x = env_get2(envp, "#") ;
    if (!x) strerr_dienotset(100, "#") ;
    if (!uint0_scan(x, &sharp)) strerr_dieinvalid(100, "#") ;
  }


 /* Shift n args */

  if (n > sharp)
  {
    if (strict)
    {
      char fmtn[UINT_FMT] ;
      char fmtsharp[UINT_FMT] ;
      fmtn[uint_fmt(fmtn, n)] = 0 ;
      fmtsharp[uint_fmt(fmtsharp, sharp)] = 0 ;
      if (strict == 1)
        strerr_warnwu5x("shift", " ", fmtn, " arguments: got ", fmtsharp) ;
      else
        strerr_diefu5x(100, "shift", " ", fmtn, " arguments: got ", fmtsharp) ;
    }
    n = sharp ;
  }


 /* Shift b blocks */

  for (; i < b ; i++)
  {
    for (;;)
    {
      char const *x ;
      unsigned int base = n ;
      char fmt[UINT_FMT] ;
      fmt[uint_fmt(fmt, ++n)] = 0 ;
      if (n > sharp)
      {
        char fmti[UINT_FMT] ;
        fmti[uint_fmt(fmt, i)] = 0 ;
        strerr_diefu6x(100, "shift", " block ", fmti, ": too few arguments (", fmt, ")") ;
      }
      x = env_get2(envp, fmt) ;
      if (!x) strerr_dienotset(100, fmt) ;
      if ((x[0] == EXECLINE_BLOCK_END_CHAR) && (!EXECLINE_BLOCK_END_CHAR || !x[1])) break ;
      if ((x[0] != EXECLINE_BLOCK_QUOTE_CHAR) && strict)
      {
        char fmti[UINT_FMT] ;
        char fmtp[UINT_FMT] ;
        fmti[uint_fmt(fmti, i)] = 0 ;
        fmtp[uint_fmt(fmtp, n - base)] = 0 ;
        if (strict == 1)
          strerr_warnw6x("unquoted positional ", x, " at block ", fmti, " position ", fmtp) ;
        else
          strerr_dief6x(100, "unquoted positional ", x, " at block ", fmti, " position ", fmtp) ;
      }
    }
  }


 /* n = shift value; modify the env */

  {
    register unsigned int i = 1 ;
    char fmt[UINT_FMT] ;
    fmt[uint_fmt(fmt, sharp - n)] = 0 ;
    if (!pathexec_env("#", fmt)) strerr_diefu1sys(111, "pathexec_env") ;
    for (; i <= sharp ; i++)
    {
      char fmu[UINT_FMT] ;
      fmt[uint_fmt(fmt, i)] = 0 ;
      fmu[uint_fmt(fmu, i + n)] = 0 ;
      if (!pathexec_env(fmt, i <= (sharp - n) ? env_get2(envp, fmu) : 0))
        strerr_diefu1sys(111, "pathexec_env") ;
    }
  }
  pathexec(argv) ;
  strerr_dieexec(111, argv[0]) ;
}
Esempio n. 23
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) ;
}
Esempio n. 24
0
File: s6-setlock.c Progetto: 8l/s6
int main (int argc, char const *const *argv, char const *const *envp)
{
  unsigned int nb = 0, ex = 1 ;
  unsigned int timeout = 0 ;
  PROG = "s6-setlock" ;
  for (;;)
  {
    register int opt = subgetopt(argc, argv, "nNrwt:") ;
    if (opt == -1) break ;
    switch (opt)
    {
      case 'n' : nb = 1 ; break ;
      case 'N' : nb = 0 ; break ;
      case 'r' : ex = 0 ; break ;
      case 'w' : ex = 1 ; break ;
      case 't' : if (!uint0_scan(subgetopt_here.arg, &timeout)) dieusage() ;
                 nb = 2 ; break ;
      default : dieusage() ;
    }
  }
  argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
  if (argc < 2) dieusage() ;

  if (nb < 2)
  {
    int fd = open_create(argv[0]) ;
    if (fd == -1) strerr_diefu2sys(111, "open_create ", argv[0]) ;
    if ((*f[ex][nb])(fd) == -1) strerr_diefu2sys(1, "lock ", argv[0]) ;
  }
  else
  {
    char const *cargv[3] = { "s6lockd-helper", argv[0], 0 } ;
    char const *cenvp[2] = { ex ? "S6LOCK_EX=1" : 0, 0 } ;
    iopause_fd x = { .events = IOPAUSE_READ } ;
    tain_t deadline ;
    int p[2] ;
    unsigned int pid ;
    char c ;
    tain_now_g() ;
    tain_from_millisecs(&deadline, timeout) ;
    tain_add_g(&deadline, &deadline) ;
    pid = child_spawn(S6_LIBEXECPREFIX "s6lockd-helper", cargv, cenvp, p, 2) ;
    if (!pid) strerr_diefu2sys(111, "spawn ", S6_LIBEXECPREFIX "s6lockd-helper") ;
    x.fd = p[0] ;
    for (;;)
    {
      register int r = iopause_g(&x, 1, &deadline) ;
      if (r < 0) strerr_diefu1sys(111, "iopause") ;
      if (!r)
      {
        kill(pid, SIGTERM) ;
        errno = ETIMEDOUT ;
        strerr_diefu1sys(1, "acquire lock") ;
      }
      r = sanitize_read(fd_read(p[0], &c, 1)) ;
      if (r < 0) strerr_diefu1sys(111, "read ack from helper") ;
      if (r) break ;
    }
    if (c != '!') strerr_dief1x(111, "helper sent garbage ack") ;
    fd_close(p[0]) ;
    if (uncoe(p[1]) < 0) strerr_diefu1sys(111, "uncoe fd to helper") ;
  }
  pathexec_run(argv[1], argv+1, envp) ;
  strerr_dieexec(111, argv[1]) ;
}
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 ;
}