summaryrefslogtreecommitdiff
path: root/src/conn-tools/s6-sudo.c
blob: d4e3b03f6aeae62c6f64b147bc4c317e1a0dbe02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 /* ISC license. */

#include <skalibs/types.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/djbunix.h>
#include <s6/config.h>

#define USAGE "s6-sudo [ -q | -Q | -v ] [ -p bindpath ] [ -l localname ] [ -e ] [ -t timeout ] [ -T timeoutrun ] path [ args... ]"
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char const *const *argv, char const *const *envp)
{
  unsigned int verbosity = 1, t = 0, T = 0 ;
  char const *bindpath = 0 ;
  char const *localname = 0 ;
  int nodoenv = 0 ;
  PROG = "s6-sudo" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "qQvp:l:et:T:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'q' : if (verbosity) verbosity-- ; break ;
        case 'Q' : verbosity = 1 ; break ;
        case 'v' : verbosity++ ; break ;
        case 'p' : bindpath = l.arg ; break ;
        case 'l' : localname = l.arg ; break ;
        case 'e' : nodoenv = 1 ; break ;
        case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
        case 'T' : if (!uint0_scan(l.arg, &T)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;
  {
    char const *eargv[9 + argc + ((verbosity < 2 ? 1 : verbosity-1)) + ((!!bindpath + !!localname) << 1) + nodoenv] ;
    char fmt1[UINT_FMT] ;
    char fmt2[UINT_FMT] ;
    unsigned int n = 0 ;
    eargv[n++] = S6_BINPREFIX "s6-ipcclient" ;
    if (!verbosity) eargv[n++] = "-Q" ;
    else while (--verbosity) eargv[n++] = "-v" ;
    if (bindpath) { eargv[n++] = "-p" ; eargv[n++] = bindpath ; }
    if (localname) { eargv[n++] = "-l" ; eargv[n++] = localname ; }
    eargv[n++] = "--" ;
    eargv[n++] = *argv++ ; argc-- ;
    eargv[n++] = S6_BINPREFIX "s6-sudoc" ;
    if (nodoenv) eargv[n++] = "-e" ;
    eargv[n++] = "-t" ;
    fmt1[uint_fmt(fmt1, t)] = 0 ;
    eargv[n++] = fmt1 ;
    eargv[n++] = "-T" ;
    fmt2[uint_fmt(fmt2, T)] = 0 ;
    eargv[n++] = fmt2 ;
    eargv[n++] = "--" ;
    while (argc--) eargv[n++] = *argv++ ;
    eargv[n++] = 0 ;
    xpathexec_run(eargv[0], eargv, envp) ;
  }
}