Ejemplo n.º 1
0
Archivo: s6-svlisten.c Proyecto: 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) ;
  }
}
Ejemplo n.º 2
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 ;
}
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 ;
}