Exemplo n.º 1
0
int main (void)
{
  PROG = "s6-tai64nlocal" ;
  for (;;)
  {
    unsigned int p = 0 ;
    int r = skagetln(buffer_0f1, &satmp, '\n') ;
    if (r == -1)
      if (errno != EPIPE)
        strerr_diefu1sys(111, "read from stdin") ;
      else r = 1 ;
    else if (!r) break ;
    if (satmp.len > TIMESTAMP)
    {
      tain_t a ;
      p = timestamp_scan(satmp.s, &a) ;
      if (p)
      {
        char fmt[LOCALTMN_FMT+1] ;
        localtmn_t local ;
        unsigned int len ;
        localtmn_from_tain(&local, &a, 1) ;
        len = localtmn_fmt(fmt, &local) ;
        if (buffer_put(buffer_1, fmt, len) < 0)
          strerr_diefu1sys(111, "write to stdout") ;
      }
    }
    if (buffer_put(buffer_1, satmp.s + p, satmp.len - p) < 0)
      strerr_diefu1sys(111, "write to stdout") ;
    satmp.len = 0 ;
  }
  return 0 ;
}
Exemplo n.º 2
0
int main (int argc, char const *const *argv)
{
  stralloc sa = STRALLOC_ZERO ;
  for (;;)
  {
    struct tm tm ;
    uint64 tt ;
    time_t t ;
    char *p ;
    int r ;
    char fmt[UINT64_FMT] ;
    sa.len = 0 ;
    r = skagetln(buffer_0, &sa, '\n') ;
    if (r < 0) strerr_diefu1sys(111, "read from stdin") ;
    if (!r) break ;
    sa.s[sa.len-1] = 0 ;
    if (!strptime(sa.s, "+%Y-%m-%d", &tm)) continue ;
    tm.tm_sec = 59 ;
    tm.tm_min = 59 ;
    tm.tm_hour = 23 ;
    t = mktime(&tm) ;
    if (t < 0) strerr_diefu1sys(111, "mktime") ;
    tt = t + 10 ;
    add_leapsecs(&tt) ;
    if (!genalloc_append(uint64, &table, &tt))
      strerr_diefu1sys(111, "genalloc_append") ;
    fmt[uint64_fmt(fmt, tt)] = 0 ;
    buffer_puts(buffer_1, "  TAI_MAGIC + ") ;
    buffer_puts(buffer_1, fmt) ;
    buffer_puts(buffer_1, ",\n") ;
  }
  buffer_unput(buffer_1, 2) ;
  buffer_putsflush(buffer_1, "\n") ;
  return 0 ;
}
Exemplo n.º 3
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 ;
}
Exemplo n.º 4
0
static void trig (uint16 id, char what, char info)
{
  char pack[4] ;
  unixmessage_t m = { .s = pack, .len = 4, .fds = 0, .nfds = 0 } ;
  uint16_pack_big(pack, id) ;
  pack[2] = what ; pack[3] = info ;
  if (!unixmessage_put(unixmessage_sender_x, &m))
  {
    cleanup() ;
    strerr_diefu1sys(111, "build answer") ;
  }
}

static void answer (char c)
{
  unixmessage_t m = { .s = &c, .len = 1, .fds = 0, .nfds = 0 } ;
  if (!unixmessage_put(unixmessage_sender_1, &m))
  {
    cleanup() ;
    strerr_diefu1sys(111, "unixmessage_put") ;
  }
}

static void remove (unsigned int i)
{
  ftrigio_deepfree(a + i) ;
  a[i] = a[--n] ;
  a[i].b.c.x = a[i].buf ;
}
Exemplo n.º 5
0
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 ;
}
Exemplo n.º 6
0
Arquivo: s6-softlimit.c Projeto: 8l/s6
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") ;
}
Exemplo n.º 7
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 ;
}
Exemplo n.º 8
0
void el_substandrun_str (stralloc *src, unsigned int srcbase, char const *const *envp, exlsn_t const *info)
{
    stralloc dst = STRALLOC_ZERO ;
    register int r = el_substitute(&dst, src->s + srcbase, src->len, info->vars.s, info->values.s, genalloc_s(elsubst_t const, &info->data), genalloc_len(elsubst_t const, &info->data)) ;
    if (r < 0) strerr_diefu1sys(111, "el_substitute") ;
    if (!r) _exit(0) ;
    stralloc_free(src) ;
    {
        char const *v[r + 1] ;
        if (!env_make(v, r, dst.s, dst.len)) strerr_diefu1sys(111, "env_make") ;
        v[r] = 0 ;
        pathexec_r(v, envp, env_len(envp), info->modifs.s, info->modifs.len) ;
    }
    strerr_dieexec(111, dst.s) ;
}
Exemplo n.º 9
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]) ;
}
Exemplo n.º 10
0
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 ;
}
Exemplo n.º 11
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 ;
}
Exemplo n.º 12
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]) ;
}
Exemplo n.º 13
0
int main (int argc, char const *const *argv)
{
  stralloc sa = STRALLOC_ZERO ;
  int path = 0, nl = 1 ;
  PROG = "s6-linkname" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "nf", &l) ;
      if (opt == -1) break ;
      switch(opt)
      {
        case 'n' : nl = 0 ; break ;
        case 'f' : path = 1 ; break ;
        default :  dieusage() ;
      }
    }
    argv += l.ind ; argc -= l.ind ;
  }
  if (!argc) dieusage() ;

  if ((path ? sarealpath(&sa, *argv) : sareadlink(&sa, *argv)) == -1)
    strerr_diefu2sys(111, "resolve ", *argv) ;

  if ((buffer_putalign(buffer_1small, sa.s, sa.len) == -1)
   || (nl && (buffer_putalign(buffer_1small, "\n", 1)) == -1)
   || (buffer_flush(buffer_1small) == -1))
    strerr_diefu1sys(111, "write to stdout") ;

 /* stralloc_free(&sa) ; */
  return 0 ;
}
Exemplo n.º 14
0
int main ()
{
  PROG = "s6-reboot" ;
  sync() ;
  reboot(RB_AUTOBOOT) ;
  strerr_diefu1sys(111, "reboot()") ;
}
Exemplo n.º 15
0
int main ()
{
  PROG = "s6-poweroff" ;
  sync() ;
  reboot(RB_POWER_OFF) ;
  strerr_diefu1sys(111, "reboot()") ;
}
Exemplo n.º 16
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 ;
}
Exemplo n.º 17
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)) ;
  }
}
Exemplo n.º 18
0
static void answer (char c)
{
  if (!bufalloc_put(bufalloc_1, &c, 1))
  {
    cleanup() ;
    strerr_diefu1sys(111, "bufalloc_put") ;
  }
}
Exemplo n.º 19
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]) ;
}
Exemplo n.º 20
0
static int getit (void)
{
  char fmt[TIMESTAMP+1] ;
  timestamp(fmt) ;
  fmt[TIMESTAMP] = '\n' ;
  if (allwrite(1, fmt, TIMESTAMP+1) < TIMESTAMP+1)
    strerr_diefu1sys(111, "write to stdout") ;
  return 0 ;
}
Exemplo n.º 21
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]) ;
}
Exemplo n.º 22
0
static void handle_signals (void)
{
  for (;;) switch (selfpipe_read())
  {
    case -1 : strerr_diefu1sys(111, "selfpipe_read") ;
    case 0 : return ;
    case SIGCHLD : wait_reap() ; break ;
    default : strerr_dief1x(101, "unexpected data in selfpipe") ;
  }
}
Exemplo n.º 23
0
//*****************************************************************************
//*****************************************************************************
int bevt_client_init(void) {
    random_name(&bevt_client_g.rid[0], 8);

    bevt_client_g.sfd = selfpipe_init() ;
    if (bevt_client_g.sfd < 0) strerr_diefu1sys(111, "selfpipe_init") ;
    {
        sigset_t set ;
        sigemptyset(&set) ;
        sigaddset(&set, SIGCHLD) ;
        if (selfpipe_trapset(&set) < 0)
            strerr_diefu1sys(111, "trap signals") ;
    }

    BEVT_DEBUG_LOG_INFO("rid(%s)", (long long int)bevt_client_g.rid);
    if(bevt_client_start_relay(0)<0)
        return -1;

    return (errno=0,0);
}
Exemplo n.º 24
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 ;
}
Exemplo n.º 25
0
static void trig (uint16 id, char what, char info)
{
  char pack[7] = "4:idwi," ;
  uint16_pack_big(pack+2, id) ;
  pack[4] = what ; pack[5] = info ;
  if (!bufalloc_put(&asyncout, pack, 7))
  {
    cleanup() ;
    strerr_diefu1sys(111, "build answer") ;
  }
}
Exemplo n.º 26
0
static inline void notify_systemd (pid_t pid, char const *socketpath)
{
  size_t n = 16 ;
  char fmt[16 + PID_FMT] = "READY=1\nMAINPID=" ;
  int fd = ipc_datagram_b() ;
  if (fd < 0) strerr_diefu1sys(111, "create socket") ;
  n += pid_fmt(fmt + n, pid) ;
  fmt[n++] = '\n' ;
  if (!ipc_sendto(fd, fmt, n, socketpath))
    strerr_diefu2sys(111, "send notification message to ", socketpath) ;
  close(fd) ;
}
Exemplo n.º 27
0
void s6_svlisten_init (int argc, char const *const *argv, s6_svlisten_t *foo, uint16 *ids, unsigned char *upstate, unsigned char *readystate, tain_t const *deadline)
{
  register unsigned int i = 0 ;
  foo->n = (unsigned int)argc ;
  foo->ids = ids ;
  foo->upstate = upstate ;
  foo->readystate = readystate ;
  if (!ftrigr_startf_g(&foo->a, deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
  for (; i < foo->n ; i++)
  {
    s6_svstatus_t status = S6_SVSTATUS_ZERO ;
    unsigned int len = str_len(argv[i]) ;
    char s[len + 1 + sizeof(S6_SUPERVISE_EVENTDIR)] ;
    byte_copy(s, len, argv[i]) ;
    s[len] = '/' ;
    byte_copy(s + len + 1, sizeof(S6_SUPERVISE_EVENTDIR), S6_SUPERVISE_EVENTDIR) ;
    foo->ids[i] = ftrigr_subscribe_g(&foo->a, s, "D|u|U|d", FTRIGR_REPEAT, deadline) ;
    if (!foo->ids[i]) strerr_diefu2sys(111, "subscribe to events for ", argv[i]) ;
    if (!s6_svstatus_read(argv[i], &status)) strerr_diefu1sys(111, "s6_svstatus_read") ;
    bitarray_poke(foo->upstate, i, status.pid && !status.flagfinishing) ;
    bitarray_poke(foo->readystate, i, status.flagready) ;
  }
}
Exemplo n.º 28
0
int main (void)
{
  char buf[TAIN_PACK] ;
  tain_t now, adj ;
  localtmn_t l ;
  char fmt[LOCALTMN_FMT] ;
  PROG = "s6-clockview" ;

  if (allread(0, buf, TAIN_PACK) < TAIN_PACK) strerr_diefu1sys(111, "read from stdin") ;
  tain_unpack(buf, &adj) ;
  if (!sysclock_get(&now)) strerr_diefu1sys(111, "sysclock_get") ;
  if (!localtmn_from_sysclock(&l, &now, 1)) strerr_diefu1sys(111, "localtmn_from_sysclock") ;
  if (buffer_puts(buffer_1, "before: ") < 0) goto fail ;
  if (buffer_put(buffer_1, fmt, localtmn_fmt(fmt, &l)) < 0) goto fail ;
  tain_add(&now, &now, &adj) ;
  if (!localtmn_from_sysclock(&l, &now, 1)) strerr_diefu1sys(111, "localtmn_from_sysclock") ;
  if (buffer_puts(buffer_1, "\nafter:  ") < 0) goto fail ;
  if (buffer_put(buffer_1, fmt, localtmn_fmt(fmt, &l)) < 0) goto fail ;
  if (buffer_putflush(buffer_1, "\n", 1) < 0) goto fail ;
  return 0 ;
 fail:
  strerr_diefu1sys(111, "write to stdout") ;
}
Exemplo n.º 29
0
static void handle_signals (void)
{
  for (;;)
  {
    char c = selfpipe_read() ;
    switch (c)
    {
      case -1 : strerr_diefu1sys(111, "selfpipe_read") ;
      case 0 : return ;
      case SIGCHLD :
        if (!status.pid) wait_reap() ;
        else
        {
          int wstat ;
          int r = wait_pid_nohang(status.pid, &wstat) ;
          if (r < 0)
            if (errno != ECHILD) strerr_diefu1sys(111, "wait_pid_nohang") ;
            else break ;
          else if (!r) break ;
          status.pid = wstat ; /* don't overwrite status.wstat if it's ./finish */
          (*actions[state][V_CHLD])() ;
        }
        break ;
      case SIGTERM :
        (*actions[state][V_TERM])() ;
        break ;
      case SIGHUP :
        (*actions[state][V_HUP])() ;
        break ;
      case SIGQUIT :
        (*actions[state][V_QUIT])() ;
        break ;
      default :
        strerr_dief1x(101, "internal error: inconsistent signal state. Please submit a bug-report.") ;
    }
  }
}
Exemplo n.º 30
0
static void handle_signals (void) {
    for (;;) {
        switch (selfpipe_read()) {
            case -1 : cleanup() ; strerr_diefu1sys(111, "selfpipe_read") ;
            case 0 : return ;
            case SIGTERM :
            case SIGQUIT :
            case SIGHUP :
            case SIGABRT :
            case SIGINT : cleanup() ; _exit(0) ;
            case SIGCHLD : wait_reap() ; break ;
            default : cleanup() ; X() ;
        }
    }
}