about summary refs log tree commit diff
path: root/src/daemontools-extras/s6-setsid.c
blob: 6fd42dd3a23f46452dbc1aedf63beb0a89617e86 (plain) (blame)
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
67
68
69
70
/* ISC license. */

#include <unistd.h>
#include <signal.h>

#include <skalibs/types.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr.h>
#include <skalibs/sig.h>
#include <skalibs/exec.h>

#define USAGE "s6-setsid [ -s | -b | -f | -g ] [ -i | -I | -q ] [ -d ctty ] prog..."
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char const *const *argv)
{
  unsigned int ctty = 0, what = 0, insist = 1 ;
  PROG = "s6-setsid" ;
  {
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "sbfgiIqd:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 's' : what = 0 ; break ;
        case 'b' : what = 1 ; break ;
        case 'f' : what = 2 ; break ;
        case 'g' : what = 3 ; break ;
        case 'i' : insist = 2 ; break ;
        case 'I' : insist = 1 ; break ;
        case 'q' : insist = 0 ; break ;
        case 'd' : if (!uint0_scan(l.arg, &ctty)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;

  if (what)
  {
    if (setpgid(0, 0) < 0) switch (insist)
    {
      case 2 : strerr_diefu1sys(111, "setpgid") ;
      case 1 : strerr_warnwu1sys("setpgid") ; break ;
      default : break ;
    }

    if (what >= 2)
    {
      if (what == 3) sig_altignore(SIGTTOU) ;
      if (tcsetpgrp(ctty, getpid()) < 0) switch (insist)
      {
        case 2 : strerr_diefu1sys(111, "tcsetpgrp") ;
        case 1 : strerr_warnwu1sys("tcsetpgrp") ; break ;
        default : break ;
      }
    }
  }
  else if (setsid() < 0) switch (insist)
  {
    case 2 : strerr_diefu1sys(111, "setsid") ;
    case 1 : strerr_warnwu1sys("setsid") ; break ;
    default : break ;
  }

  xexec(argv) ;
}