int s6net_ident_reply_get (char *s, ip46_t const *remoteip, uint16 remoteport, ip46_t const *localip, uint16 localport, tain_t const *deadline, tain_t *stamp)
{
  unsigned int len ;
  int fd ;
  if (ip46_is6(remoteip) != ip46_is6(localip)) return (errno = EAFNOSUPPORT, -1) ;
  fd = socket_tcp46(ip46_is6(remoteip)) ;
  if (fd < 0) return -1 ;
  if (socket_bind46(fd, localip, 0) < 0) goto err ;
  if (!socket_deadlineconnstamp46(fd, remoteip, 113, deadline, stamp)) goto err ;
  {
    char buf[S6NET_IDENT_REPLY_SIZE + 1] ;
    char fmt[UINT16_FMT] ;
    buffer b = BUFFER_INIT(&buffer_write, fd, buf, 256) ;
    unsigned int n = uint16_fmt(fmt, remoteport) ;
    buffer_putnoflush(&b, fmt, n) ;
    buffer_putnoflush(&b, " , ", 3) ;
    n = uint16_fmt(fmt, localport) ;
    buffer_putnoflush(&b, fmt, n) ;
    buffer_putnoflush(&b, "\r\n", 2) ;
    if (!buffer_timed_flush(&b, deadline, stamp)) goto err ;
    buffer_init(&b, &buffer_read, fd, buf, S6NET_IDENT_REPLY_SIZE + 1) ;
    if (sanitize_read(timed_getlnmax(&b, s, S6NET_IDENT_REPLY_SIZE, &len, '\n', deadline, stamp)) <= 0) goto err ;
  }
  fd_close(fd) ;
  if (!len--) return (errno = EPROTO, -1) ;
  s[len] = 0 ;
  return (int)len ;

err:
  fd_close(fd) ;
  return -1 ;
}
Esempio n. 2
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 ;
}