Esempio n. 1
0
void el_execsequence (char const *const *argv1, char const *const *argv2, char const *const *envp)
{
  if (!argv2[0])
  {
    pathexec0_run(argv1, envp) ;
    strerr_dieexec(111, argv1[0]) ;
  }
  else
  {
    int wstat ;
    unsigned int j = 2 ;
#ifdef EXECLINE_OLD_VARNAMES
    char fmt[UINT_FMT * 2 + 15] = "?=" ;
#else
    char fmt[UINT_FMT + 1] = "?=" ;
#endif
    pid_t pid = el_spawn0(argv1[0], argv1, envp) ;
    if (!pid) strerr_warnwu2sys("spawn ", argv1[0]) ;
    if (wait_pid(pid, &wstat) < 0)
      strerr_diefu2sys(111, "wait for ", argv1[0]) ;
    j += uint_fmt(fmt + j, wait_status(wstat)) ; fmt[j++] = 0 ;
#ifdef EXECLINE_OLD_VARNAMES
    byte_copy(fmt + j, 13, "LASTEXITCODE=") ; j += 13 ;
    j += uint_fmt(fmt + j, wait_status(wstat)) ; fmt[j++] = 0 ;
#endif
    pathexec_r(argv2, envp, env_len(envp), fmt, j) ;
  }
  strerr_dieexec(111, argv2[0]) ;
}
Esempio n. 2
0
static inline void tryfinish (int islast)
{
  register pid_t pid = fork() ;
  if (pid < 0)
  {
    strerr_warnwu2sys("fork for ", "./finish") ;
    if (islast) bail() ;
    state = DOWN ;
    settimeout(1) ;
    return ;
  }
  else if (!pid)
  {
    char fmt0[UINT_FMT] ;
    char fmt1[UINT_FMT] ;
    char *cargv[4] = { "finish", fmt0, fmt1, 0 } ;
    selfpipe_finish() ;
    fmt0[uint_fmt(fmt0, WIFSIGNALED(status.wstat) ? 256 : WEXITSTATUS(status.wstat))] = 0 ;
    fmt1[uint_fmt(fmt1, WTERMSIG(status.wstat))] = 0 ;
    if (flagsetsid) setsid() ;
    execve("./finish", cargv, (char *const *)environ) ;
    _exit(127) ;
  }
  status.pid = pid ;
  status.flagfinishing = 1 ;
  state = islast ? LASTFINISH : FINISH ;
}
Esempio n. 3
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]) ;
}
Esempio n. 4
0
int netstring_put (buffer *b, char const *s, unsigned int len, unsigned int *written)
{
  char fmt[UINT_FMT] ;
  unsigned int n = uint_fmt(fmt, len) ;
  if (*written > len + n + 2) return (errno = EINVAL, 0) ;
  fmt[n] = ':' ;
  if (*written < n + 1)
  {
    unsigned int w = *written ;
    int r = buffer_putall(b, fmt, n+1, &w) ;
    if (r < 0) return (*written = w, 0) ;
    *written = n+1 ;
  }
  if (*written < n+1 + len)
  {
    unsigned int w = *written - (n+1) ;
    int r = buffer_putall(b, s, len, &w) ;
    *written = w + (n+1) ;
    if (r < 0) return (*written = n+1 + w, 0) ;
    *written = n+1 + len ;
  }
  {
    unsigned int w = 0 ;
    int r = buffer_putall(b, ",", 1, &w) ;
    if (r < 0) return 0 ;
  }
  *written = 0 ;
  return 1 ;
}
Esempio n. 5
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. 6
0
unsigned int localtm_fmt (char *s, struct tm const *l)
{
  char *p = s ;
  p += uint_fmt(p, 1900 + l->tm_year) ; *p++ = '-' ;
  uint0_fmt(p, 1 + l->tm_mon, 2) ; p += 2 ; *p++ = '-' ;
  uint0_fmt(p, l->tm_mday, 2) ; p += 2 ; *p++ = ' ' ;
  uint0_fmt(p, l->tm_hour, 2) ; p += 2 ; *p++ = ':' ;
  uint0_fmt(p, l->tm_min, 2) ; p += 2 ; *p++ = ':' ;
  uint0_fmt(p, l->tm_sec, 2) ; p += 2 ;
  return p - s ;
}
Esempio n. 7
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 ;
}
Esempio n. 8
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. 9
0
static void _kill (pid_t pid, int sig)
{
    char buf[UINT_FMT];
    unsigned int u;

    u = pid;
    buf[uint_fmt (buf, u)] = 0;
    aa_bs_noflush (AA_OUT, "kill(");
    if (u == (unsigned int) -1)
        aa_bs_noflush (AA_OUT, "-1");
    else
        aa_bs_noflush (AA_OUT, buf);
    aa_bs_noflush (AA_OUT, ",");
    aa_bs_noflush (AA_OUT, sig_name (sig));
    aa_bs_flush (AA_OUT, ")\n");
}
Esempio n. 10
0
s6_accessrules_result_t s6_accessrules_keycheck_ip4 (void const *key, void *data, s6_accessrules_params_t *params, s6_accessrules_backend_func_t_ref check1)
{
  char fmt[IP4_FMT + UINT_FMT + 6] = "ip4/" ;
  uint32 ip ;
  unsigned int i = 0 ;
  uint32_unpack_big((char const *)key, &ip) ;
  for (; i <= 32 ; i++)
  {
    register s6_accessrules_result_t r ;
    register unsigned int len = 4 + ip4_fmtu32(fmt+4, (i == 32) ? 0 : ip & ~((1U << i) - 1)) ;
    fmt[len++] = '_' ;
    len += uint_fmt(fmt + len, 32 - i) ;
    r = (*check1)(fmt, len, data, params) ;
    if (r != S6_ACCESSRULES_NOTFOUND) return r ;
  }
  return S6_ACCESSRULES_NOTFOUND ;
}
Esempio n. 11
0
int netstring_appendv (stralloc *sa, siovec_t const *v, unsigned int n)
{
  char fmt[UINT_FMT] ;
  unsigned int len = 0, pos ;
  register unsigned int i = 0 ;
  for (; i < n ; i++) len += v[i].len ;
  pos = uint_fmt(fmt, len) ;
  if (!stralloc_readyplus(sa, len + pos + 2)) return 0 ;
  fmt[pos] = ':' ;
  byte_copy(sa->s + sa->len, pos+1, fmt) ;
  sa->len += pos+1 ;
  for (i = 0 ; i < n ; i++)
  {
    byte_copy(sa->s + sa->len, v[i].len, v[i].s) ;
    sa->len += v[i].len ;
  }
  sa->s[sa->len++] = ',' ;
  return 1 ;
}
Esempio n. 12
0
int el_popenv (stralloc *sa, char const *const *envp, unsigned int envlen, char const *const *list, unsigned int listlen)
{
  unsigned int i = 0, salen = sa->len, count = 0 ;
  for (; i < envlen ; i++)
  {
    unsigned int equal, colon, n ;
    unsigned int j = 0 ;
    for (; j < listlen ; j++) if (str_start(envp[i], list[j])) break ;
    if (j == listlen) goto copyit ;
    j = str_len(list[j]) ;
    colon = j + str_chr(envp[i] + j, ':') ;
    equal = j + str_chr(envp[i] + j, '=') ;
    if (!envp[i][equal]) goto badenv ;
    if (colon >= equal) { count++ ; continue ; }
    if (colon + 1 + uint_scan(envp[i] + colon + 1, &n) != equal) goto copyit ;
    if (!n) goto copyit ;
    if (!stralloc_catb(sa, envp[i], colon)) goto err ;
    if (n > 1)
    {
      char fmt[UINT_FMT+1] = ":" ;
      n = 1 + uint_fmt(fmt+1, n-1) ;
      if (!stralloc_catb(sa, fmt, n)) goto err ;
    }
    if (!stralloc_catb(sa, envp[i] + equal, str_len(envp[i] + equal) + 1)) goto err ;
    continue ;
copyit:
    if (!stralloc_catb(sa, envp[i], str_len(envp[i]) + 1)) goto err ;
  }
  return (int)count ;

badenv :
  errno = EINVAL ;
err:
  sa->len = salen ;
  return -1 ;
}
Esempio n. 13
0
int main (int argc, char const *const *argv, char const *const *envp)
{
  char const *def = 0 ;
  int insist = 0, chomp = 0 ;
  PROG = "backtick" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "eniD:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
	case 'e' : break ; /* compat */
        case 'n' : chomp = 1 ; break ;
        case 'i' : insist = 1 ; break ;
        case 'D' : def = l.arg ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (argc < 2) dieusage() ;
  if (!argv[0][0]) dieusage() ;
  if (!argv[1][0]) strerr_dief1x(100, "empty block") ;
  {
    unsigned int m = 0, i = 1 ;
    int fd = dup(0) ;
    char const *newargv[argc + 15] ;
    char fmt[UINT_FMT] ;
    if (fd < 0)
    {
      if (errno != EBADF) strerr_diefu1sys(111, "dup stdin") ;
    }
    else fmt[uint_fmt(fmt, (unsigned int)fd)] = 0 ;
    newargv[m++] = EXECLINE_BINPREFIX "pipeline" ;
    newargv[m++] = "--" ;
    while (argv[i] && argv[i][0] != EXECLINE_BLOCK_END_CHAR && (!EXECLINE_BLOCK_END_CHAR || (argv[i][0] && argv[i][1])))
      newargv[m++] = argv[i++] ;
    if (!argv[i]) strerr_dief1x(100, "unterminated block") ;
    newargv[m++] = "" ; i++ ;
    newargv[m++] = EXECLINE_BINPREFIX "withstdinas" ;
    if (insist) newargv[m++] = "-i" ;
    if (chomp) newargv[m++] = "-n" ;
    if (def)
    {
      newargv[m++] = "-D" ;
      newargv[m++] = def ;
    }
    newargv[m++] = "-!" ;
    newargv[m++] = "--" ;
    newargv[m++] = argv[0] ;
    if (fd < 0)
    {
      newargv[m++] = EXECLINE_BINPREFIX "fdclose" ;
      newargv[m++] = "0" ;
    }
    else
    {
      newargv[m++] = EXECLINE_BINPREFIX "fdmove" ;
      newargv[m++] = "0" ;
      newargv[m++] = fmt ;
    }
    while (argv[i]) newargv[m++] = argv[i++] ;
    newargv[m++] = 0 ;
    pathexec_run(newargv[0], newargv, envp) ;
    strerr_dieexec(111, newargv[0]) ;
  }
}
Esempio n. 14
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. 15
0
unsigned int NAME (char const *prog, char const *const *argv, char const *const *envp, unsigned int uid, unsigned int gid, int *fds)
{
  int p[NPIPES][2] ;
  int syncpipe[2] ;
  int pid ;
  int e ;
  if (pipe(p[0]) < 0) return 0 ;
  if (pipe(p[1]) < 0) { e = errno ; goto errp0 ; }
#ifdef _CHILD_SPAWN2_
  if (pipe(p[2]) < 0) { e = errno ; goto errp1 ; }
#endif
  if (pipe(syncpipe) < 0) { e = errno ; goto errp2 ; }
  if (coe(syncpipe[1]) < 0) { e = errno ; goto errsp ; }
  pid = fork() ;
  if (pid < 0) { e = errno ; goto errsp ; }
  else if (!pid)
  {
    unsigned int m = 25 ;
    unsigned int n = str_len(PROG) ;
    char fmt[25 + UINT_FMT] = "SKACLIENT2_ADDITIONAL_FD" ;
    char name[n + 9] ;
    byte_copy(name, n, PROG) ;
    byte_copy(name + n, 9, " (child)") ;
    PROG = name ;
#ifdef _CHILD_SPAWN2_
    fmt[m-1] = '=' ;
    m += uint_fmt(fmt + m, p[2][1]) ;
    fmt[m++] = 0 ;
    fd_close(p[2][0]) ;
#endif
    fd_close(syncpipe[0]) ;
    fd_close(p[1][1]) ;
    fd_close(p[0][0]) ;
    if ((fd_move(0, p[1][0]) < 0) || (fd_move(1, p[0][1]) < 0)) goto syncdie ;
    if (gid && (prot_gid(gid) < 0)) goto syncdie ;
    if (uid && (prot_uid(uid) < 0)) goto syncdie ;
    /*
       XXX: we should unignore signals here, but there's a compromise to be
       XXX: found between bloat, API complexity, and theoretical correctness.
       XXX: Not sure what The Right Thing is; just not unignoring atm.
       XXX: 20130326 edit: The Right Thing is probably to leave the ignore
       XXX: status alone, because programs that do actual signal handling
       XXX: are likely to use a selfpipe, and selfpipe_finish() restores
       XXX: old signal handlers - it knows what to do and we don't. So,
       XXX: leave that work to selfpipe_finish().
    */
    sig_blocknone() ; /* empty sigprocmask in the child, always */
    pathexec_r_name(prog, argv, envp, env_len(envp), fmt, m) ;

   syncdie:
    {
      char c = errno ;
      fd_write(syncpipe[1], &c, 1) ;
    }
    _exit(111) ;
  }
  {
    char c ;
    fd_close(syncpipe[1]) ;
#ifdef _CHILD_SPAWN2_
    fd_close(p[2][1]) ;
#endif
    fd_close(p[1][0]) ;
    fd_close(p[0][1]) ;
    p[1][0] = fd_read(syncpipe[0], &c, 1) ;
    if (p[1][0] < 0) goto killclosewait ;
    else if (p[1][0]) { e = c ; goto closewait ; }
  }
  fd_close(syncpipe[0]) ;
  if ((ndelay_on(p[0][0]) < 0) || (coe(p[0][0]) < 0)
   || (ndelay_on(p[1][1]) < 0) || (coe(p[1][1]) < 0)) goto killclosewait ;
#ifdef _CHILD_SPAWN2_
  if ((ndelay_on(p[2][0]) < 0) || (coe(p[2][0]) < 0)) goto killclosewait ;
  fds[2] = p[2][0] ;
#endif
  fds[0] = p[0][0] ;
  fds[1] = p[1][1] ;
  return (unsigned int)pid ;

 errsp:
  fd_close(syncpipe[1]) ;
  fd_close(syncpipe[0]) ;  
 errp2:
#ifdef _CHILD_SPAWN2_
  fd_close(p[2][1]) ;
  fd_close(p[2][0]) ;
 errp1:
#endif  
  fd_close(p[1][1]) ;
  fd_close(p[1][0]) ;  
 errp0:
  fd_close(p[0][1]) ;
  fd_close(p[0][0]) ;  
  errno = e ;
  return 0 ;

 killclosewait:
  e = errno ;
  kill(pid, SIGKILL) ;
 closewait:
#ifdef _CHILD_SPAWN2_
  fd_close(p[2][0]) ;
#endif
  fd_close(p[1][1]) ;
  fd_close(p[0][0]) ;
  wait_pid(&syncpipe[1], pid) ;
  errno = e ;
  return 0 ;
}
Esempio n. 16
0
int main (int argc, char const *const *argv, char const *const *envp)
{
  char const *delim = DELIM_DEFAULT ;
  char const *codes = 0 ;
  int crunch = 0, chomp = 0, not = 1, par = 0 ;
  PROG = "forbacktickx" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "epnCc0d:o:x:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
	case 'e' : break ; /* compat */
        case 'p' : par = 1 ; break ;
        case 'n' : chomp = 1 ; break ;
        case 'C' : crunch = 1 ; break ;
        case 'c' : crunch = 0 ; break ;
        case '0' : delim = 0 ; break ;
        case 'd' : delim = l.arg ; break ;
        case 'o' :
        {
          unsigned short okcodes[256] ;
          unsigned int nbc ;
          if (!ushort_scanlist(okcodes, 256, l.arg, &nbc)) dieusage() ;
          codes = l.arg ;
          not = 0 ;
          break ;
        }
        case 'x' :
        {
          unsigned short okcodes[256] ;
          unsigned int nbc ;
          if (!ushort_scanlist(okcodes, 256, l.arg, &nbc)) dieusage() ;
          codes = l.arg ;
          not = 1 ;
          break ;
        }
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (argc < 2) dieusage() ;
  if (!argv[0][0]) dieusage() ;
  if (!argv[1][0]) strerr_dief1x(100, "empty block") ;
  {
    unsigned int m = 0, i = 1 ;
    int fd = dup(0) ;
    char const *newargv[argc + 18] ;
    char fmt[UINT_FMT] ;
    if (fd < 0)
    {
      if (errno != EBADF) strerr_diefu1sys(111, "dup stdin") ;
    }
    else fmt[uint_fmt(fmt, (unsigned int)fd)] = 0 ;
    newargv[m++] = EXECLINE_BINPREFIX "pipeline" ;
    newargv[m++] = "--" ;
    while (argv[i] && argv[i][0] != EXECLINE_BLOCK_END_CHAR && (!EXECLINE_BLOCK_END_CHAR || (argv[i][0] && argv[i][1])))
      newargv[m++] = argv[i++] ;
    if (!argv[i]) strerr_dief1x(100, "unterminated block") ;
    newargv[m++] = "" ; i++ ;
    newargv[m++] = EXECLINE_BINPREFIX "unexport" ;
    newargv[m++] = "!" ;
    newargv[m++] = EXECLINE_BINPREFIX "forstdin" ;
    if (par) newargv[m++] = "-p" ;
    if (chomp) newargv[m++] = "-n" ;
    if (crunch) newargv[m++] = "-C" ;
    if (!delim) newargv[m++] = "-0" ;
    else if (str_diff(delim, DELIM_DEFAULT))
    {
      newargv[m++] = "-d" ;
      newargv[m++] = delim ;
    }
    if (codes)
    {
      newargv[m++] = not ? "-x" : "-o" ;
      newargv[m++] = codes ;
    }
    newargv[m++] = "--" ;
    newargv[m++] = argv[0] ;
    if (fd < 0)
    {
      newargv[m++] = EXECLINE_BINPREFIX "fdclose" ;
      newargv[m++] = "0" ;
    }
    else
    {
      newargv[m++] = EXECLINE_BINPREFIX "fdmove" ;
      newargv[m++] = "0" ;
      newargv[m++] = fmt ;
    }
    while (argv[i]) newargv[m++] = argv[i++] ;
    newargv[m++] = 0 ;
    pathexec_run(newargv[0], newargv, envp) ;
    strerr_dieexec(111, newargv[0]) ;
  }
}
Esempio n. 17
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 ;
}
Esempio n. 18
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. 19
0
int
main (int argc, char * const argv[])
{
    PROG = "aa-kill";

    for (;;)
    {
        struct option longopts[] = {
            { "double-output",      no_argument,        NULL,   'D' },
            { "help",               no_argument,        NULL,   'h' },
            { "kill",               no_argument,        NULL,   'k' },
            { "skip-at",            no_argument,        NULL,   's' },
            { "term",               no_argument,        NULL,   't' },
            { "hup",                no_argument,        NULL,   'u' },
            { "version",            no_argument,        NULL,   'V' },
            { NULL, 0, 0, 0 }
        };
        int c;

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

            case 'h':
                dieusage (0);

            case 'k':
                send.kill = 1;
                break;

            case 's':
                send.skip_at = 1;
                break;

            case 't':
                send.term = 1;
                break;

            case 'u':
                send.hup = 1;
                break;

            case 'V':
                aa_die_version ();

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

    if (argc > 0 || (!send.hup && !send.term && !send.kill))
        dieusage (1);

    if (send.skip_at)
    {
        stralloc sa = STRALLOC_ZERO;
        unsigned int u;

        u = (unsigned int) getpid ();
        ownpid[uint_fmt (ownpid, u)] = '\0';

        if (!stralloc_catb (&sa, "/proc", sizeof ("/proc")))
            aa_strerr_diefu1sys (1, "stralloc_catb");
        if (aa_scan_dir (&sa, 0, it_kill, &sa) < 0)
            aa_strerr_diefu1sys (1, "scan /proc");
        stralloc_free (&sa);
    }
    else
    {
        if (send.hup)
        {
            sig_ignore (SIGHUP);
            _kill (-1, SIGHUP);
        }

        if (send.term)
        {
            sig_ignore (SIGTERM);
            _kill (-1, SIGTERM);
            _kill (-1, SIGCONT);
        }

        if (send.kill)
            _kill (-1, SIGKILL);
    }

    return 0;
}
Esempio n. 20
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]) ;
  }
}
static int doit (char const *s, unsigned int len)
{
  if (delimlen)
  {
    if (!len--)
    {
      switch (strictness)
      {
        case 1 :
        case 2 :
          strerr_warnw1x("empty line") ;
          break ;
        case 3 :
          buffer_flush(buffer_1) ;
          strerr_dief1x(100, "empty line") ;
        default : break ;
      }
      return 1 ;
    }
    if (byte_chr(delim, delimlen, *s) >= delimlen)
    {
      switch (strictness)
      {
        case 0 : return 0 ;
        case 1 :
        {
          strerr_warnw1x("invalid starting quote character") ;
          return 0 ;
        }
        case 2 :
        {
          char fmt[40] ;
          unsigned int n = len < 39 ? len+1 : 36 ;
          byte_copy(fmt, n, s) ;
          if (len >= 39)
          {
            byte_copy(fmt+n, 3, "...") ;
            n += 3 ;
          }
          fmt[n] = 0 ;
          strerr_warnw3x("invalid starting quote character", " in line: ", fmt) ;
          return 0 ;
        }
        case 3 :
        {
          buffer_flush(buffer_1) ;
          strerr_dief1x(100, "invalid starting quote character") ;
        }
        default : strerr_dief1x(101, "can't happen: unknown strictness") ;
      }
    }
  }
  {
    unsigned int r, w ;
    char d[len] ;
    if (!string_unquote_withdelim(d, &w, s + !!delimlen, len, &r, delim, delimlen))
    {
      switch (strictness)
      {
        case 0 : return 0 ;
        case 1 :
        {
          strerr_warnwu1sys("unquote") ;
          return 0 ;
        }
        case 2 :
        {
          char fmt[40] ;
          unsigned int n = (len + !!delimlen) < 40 ? (len + !!delimlen) : 36 ;
          byte_copy(fmt, n, s) ;
          if ((len + !!delimlen) >= 40)
          {
            byte_copy(fmt+n, 3, "...") ;
            n += 3 ;
          }
          fmt[n] = 0 ;
          strerr_warnwu3sys("unquote", " line: ", fmt) ;
          return 0 ;
        }
        case 3 :
        {
          int e = errno ;
          buffer_flush(buffer_1) ;
          errno = e ;
          strerr_diefu1sys(100, "unquote") ;
        }
        default : strerr_dief1x(101, "can't happen: unknown strictness") ;
      }
    }
    if (delimlen)
    {
      if (r == len)
      {
        switch (strictness)
        {
          case 0 : return 0 ;
          case 1 :
          {
            strerr_warnwu2x("unquote", ": no ending quote character") ;
            return 0 ;
          }
          case 2 :
          {
            char fmt[40] ;
            unsigned int n = len < 40 ? len : 36 ;
            byte_copy(fmt, n, s) ;
            if (len >= 40)
            {
              byte_copy(fmt+n, 3, "...") ;
              n += 3 ;
            }
            fmt[n] = 0 ;
            strerr_warnwu5x("unquote", ": no ending quote character", " in ", "line: ", fmt) ;
            return 0 ;
          }
          case 3 :
          {
            int e = errno ;
            buffer_flush(buffer_1) ;
            errno = e ;
            strerr_diefu2x(100, "unquote", ": no ending quote character") ;
          }
          default : strerr_dief1x(101, "can't happen: unknown strictness") ;
        }
      }
      else if ((r < len-1) && (strictness >= 2))
      {
        char fmtnum[UINT_FMT] ;
        char fmtden[UINT_FMT] ;
        char fmt[40] ;
        unsigned int n = len < 39 ? len+1 : 36 ;
        byte_copy(fmt, n, s) ;
        if (len >= 39)
        {
          byte_copy(fmt+n, 3, "...") ;
          n += 3 ;
        }
        fmt[n] = 0 ;
        fmtnum[uint_fmt(fmtnum, r+1)] = 0 ;
        fmtden[uint_fmt(fmtden, len)] = 0 ;
        strerr_warnw7x("found ending quote character at position ", fmtnum, "/", fmtden, ", ignoring remainder of ", "line: ", fmt) ;
      }
    }
    if (buffer_putalign(buffer_1, d, w) < (int)w)
      strerr_diefu1sys(111, "write to stdout") ;
  }
  return 1 ;
}