about summary refs log tree commit diff
path: root/src/daemontools-extras/s6-setuidgid.c
blob: f6103c114424adf5b05a6976d362af22ad8348dc (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
/* ISC license. */

#include <string.h>

#include <skalibs/strerr2.h>
#include <skalibs/exec.h>

#include <s6/config.h>

#define USAGE "s6-setuidgid username prog..."
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char *const *argv)
{
  char const *newargv[argc + 7] ;
  char *colon ;
  unsigned int m = 0 ;
  PROG = "s6-setuidgid" ;
  if (argc < 3) dieusage() ;
  argv++ ;
  colon = strchr(argv[0], ':') ;
  if (colon)
  {
    *colon = 0 ;
    newargv[m++] = S6_BINPREFIX "s6-applyuidgid" ;
    newargv[m++] = "-u" ;
    newargv[m++] = argv[0] ;
    newargv[m++] = "-g" ;
    newargv[m++] = colon + 1 ;
    newargv[m++] = "-G" ;
    newargv[m++] = "" ;
    argv++ ;
  }
  else
  {
    newargv[m++] = S6_BINPREFIX "s6-envuidgid" ;
    newargv[m++] = *argv++ ;
    newargv[m++] = S6_BINPREFIX "s6-applyuidgid" ;
    newargv[m++] = "-Uz" ;
  }
  newargv[m++] = "--" ;
  while (*argv) newargv[m++] = *argv++ ;
  newargv[m++] = 0 ;
  xexec(newargv) ;
}