int skaclient2_startf (skaclient2_t_ref a, char const *prog, char const *const *argv, char const *const *envp, char const *before, unsigned int beforelen, char const *after, unsigned int afterlen, uint32 options, struct taia const *deadline, struct taia *stamp)
{
  int fds[3] ;
  unsigned int pid = child_spawn2(prog, argv, envp, 0, 0, fds) ;
  register int e ;
  if (!pid) return 0 ;
  a->sync.pid = pid ;
  a->sync.options = options ;
  skaclientin_init(&a->sync.in, fds[0]) ;
  bufalloc_init(&a->sync.out, &fd_write, fds[1]) ;
  skaclientin_init(&a->async, fds[2]) ;
  {
    char tmp[beforelen] ;
    if (skaclientin_read(&a->sync.in, tmp, beforelen, deadline, stamp) < beforelen) { e = errno ; goto err ; }
    if (byte_diff(tmp, beforelen, before)) { e = EPROTO ; goto err ; }
  }
  {
    char tmp[afterlen] ;
    if (skaclientin_read(&a->async, tmp, afterlen, deadline, stamp) < afterlen) { e = errno ; goto err ; }
    if (byte_diff(tmp, afterlen, after)) { e = EPROTO ; goto err ; }
  }
  return 1 ;

 err:
  skaclient_end(&a->sync) ;
  skaclientin_end(&a->async) ;
  errno = e ;
  return 0 ;
}
Ejemplo n.º 2
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 ;
  }
}
int skaclient_startf (skaclient_t_ref a, char const *prog, char const *const *argv, char const *const *envp, char const *banner, unsigned int bannerlen, uint32 options, struct taia const *deadline, struct taia *stamp)
{
  int fds[2] ;
  unsigned int pid = child_spawn(prog, argv, envp, 0, 0, fds) ;
  if (!pid) return 0 ;
  a->pid = pid ;
  a->options = options ;
  skaclientin_init(&a->in, fds[0]) ;
  bufalloc_init(&a->out, &fd_write, fds[1]) ;
  if (!skaclient_readbanner(a, banner, bannerlen, deadline, stamp)) goto fail ;
  return 1 ;
 fail:
  {
    register int e = errno ;
    skaclient_end(a) ;
    errno = e ;
  }
  return 0 ;
}