summaryrefslogtreecommitdiff
path: root/src/instance/s6-instance-maker.c
blob: e70e56903651a75aee9c44f13504ec824c819d0a (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ISC license. */

#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/uio.h>

#include <skalibs/uint64.h>
#include <skalibs/types.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr.h>
#include <skalibs/sgetopt.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <skalibs/skamisc.h>

#include <execline/config.h>

#include <s6/config.h>
#include <s6/auto.h>

#define USAGE "s6-instance-maker [ -c max ] [ -u user ] [ -l loguser ] [ -L logdir ] [ -t stamptype ] [ -n nfiles ] [ -s filesize ] [ -S maxsize ] [ -P prefix ] [ -r service[/logger[/pipeline]] ] template dir"
#define dieusage() strerr_dieusage(100, USAGE)

typedef struct svinfo_s svinfo, *svinfo_ref ;
struct svinfo_s
{
  char const *user ;
  unsigned int maxinstances ;
} ;

static stralloc sa = STRALLOC_ZERO ;

static int write_run (buffer *b, void *data)
{
  svinfo *t = data ;
  size_t l ;
  char fmt[UINT_FMT] ;
  l = uint_fmt(fmt, t->maxinstances) ;
  if (buffer_puts(b, EXECLINE_EXTBINPREFIX "execlineb -P\n\n"
    EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n") < 0) return 0 ;
  if (t->user)
  {
    if (!string_quote(&sa, t->user, strlen(t->user))) return 0 ;
    if (buffer_puts(b, S6_EXTBINPREFIX "s6-setuidgid ") < 0
     || buffer_put(b, sa.s, sa.len) < 0
     || buffer_put(b, "\n", 1) < 0) goto err ;
    sa.len = 0 ;
  }
  if (buffer_puts(b, S6_EXTBINPREFIX "s6-svscan -d3 -c") < 0
   || buffer_put(b, fmt, l) < 0
   || buffer_putsflush(b, " -- instance\n") < 0) return 0 ;
  return 1 ;

err:
  sa.len = 0 ;
  return 0 ;
}

static void write_service (char const *dir, char const *template, char const *user, unsigned int maxinstances, char const *logger)
{
  svinfo data = { .user = user, .maxinstances = maxinstances } ;
  size_t dirlen = strlen(dir) ;
  mode_t m = umask(0) ;
  char fn[dirlen + 21] ;
  memcpy(fn, dir, dirlen) ;
  memcpy(fn + dirlen, "/instance", 10) ;
  m = umask(0) ;
  if (mkdir(fn, 0755) == -1) strerr_diefu2sys(111, "mkdir ", fn) ;
  memcpy(fn + dirlen + 9, "s", 2) ;
  if (mkdir(fn, 0755) == -1) strerr_diefu2sys(111, "mkdir ", fn) ;
  umask(m) ;
  memcpy(fn + dirlen + 10, "/.template", 11) ;
  if (!hiercopy_tmp(template, fn, &sa))
    strerr_diefu4sys(111, "copy file hierarchy from ", template, " to ", fn) ;
  s6_auto_write_service(dir, 3, &write_run, &data, logger) ;
}

int main (int argc, char const *const *argv)
{
  char const *user = "" ;
  char const *loguser = 0 ;
  char const *logdir = 0 ;
  unsigned int maxinstances = 500 ;
  unsigned int stamptype = 1 ;
  unsigned int nfiles = 10 ;
  uint64_t filesize = 1000000 ;
  uint64_t maxsize = 0 ;
  char const *prefix = 0 ;
  char *rcinfo[3] = { 0, 0, 0 } ;
  size_t dirlen ;
  PROG = "s6-instance-maker" ;
  {
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "u:l:L:c:t:n:s:S:P:r:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'u' : user = l.arg ; break ;
        case 'l' : loguser = l.arg ; break ;
        case 'L' : logdir = l.arg ; break ;
        case 'c' : if (!uint0_scan(l.arg, &maxinstances)) dieusage() ; break ;
        case 't' : if (!uint0_scan(l.arg, &stamptype)) dieusage() ; break ;
        case 'n' : if (!uint0_scan(l.arg, &nfiles)) dieusage() ; break ;
        case 's' : if (!uint640_scan(l.arg, &filesize)) dieusage() ; break ;
        case 'S' : if (!uint640_scan(l.arg, &maxsize)) dieusage() ; break ;
        case 'P' : prefix = l.arg ; break ;
	case 'r' : rcinfo[0] = (char *)l.arg ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  if (argc < 2) dieusage() ;
  if (logdir && logdir[0] != '/')
    strerr_dief1x(100, "logdir must be absolute") ;
  if (stamptype > 3) strerr_dief1x(100, "stamptype must be 0, 1, 2 or 3") ;
  if (strchr(user, ' ') || strchr(user, '\t') || strchr(user, '\n'))
    strerr_dief1x(100, "invalid user") ;
  if (maxinstances < 1) maxinstances = 1 ;
  if (maxinstances > 90000) maxinstances = 90000 ;
  if (rcinfo[0])
  {
    if (strchr(rcinfo[0], '\n'))
      strerr_dief2x(100, "newlines", " are forbidden in s6-rc names") ;
    if (rcinfo[0][0] == '/')
      strerr_dief2x(100, "service", " name cannot be empty") ;
    rcinfo[1] = strchr(rcinfo[0], '/') ;
    if (rcinfo[1])
    {
      *rcinfo[1]++ = 0 ;
      if (!rcinfo[1][0]) strerr_dief1x(100, "argument to -r must be: service or service/logger or service/logger/pipeline") ;
      if (rcinfo[1][0] == '/')
        strerr_dief2x(100, "logger", " name cannot be empty") ;
      if (!logdir) strerr_dief1x(100, "logger specifiec (-r) but logdir not specified (-L)") ;
      rcinfo[2] = strchr(rcinfo[1], '/') ;
      if (rcinfo[2])
      {
        *rcinfo[2]++ = 0 ;
        if (!rcinfo[2][0]) strerr_dief2x(100, "pipeline", " name cannot be empty") ;
        if (strchr(rcinfo[2], '/')) strerr_dief2x(100, "slashes", " are forbidden in s6-rc names") ;
      }
    }
    else if (logdir)
      strerr_dief1x(100, "logdir specified (-L) but logger name not specified (-r)") ;
  }

  dirlen = strlen(argv[1]) ;
  if (rcinfo[0])
  {
    size_t svclen = strlen(rcinfo[0]) ;
    size_t loglen = rcinfo[1] ? strlen(rcinfo[1]) : 0 ;
    char dir[dirlen + 2 + (svclen > loglen ? svclen : loglen)] ;
    memcpy(dir, argv[1], dirlen) ;
    dir[dirlen] = '/' ;
    if (mkdir(argv[1], 0755) < 0 && errno != EEXIST)
      strerr_diefu2sys(111, "mkdir ", argv[2]) ;
    memcpy(dir + dirlen + 1, rcinfo[0], svclen + 1) ;
    write_service(dir, user, argv[0], maxinstances, rcinfo[1] ? rcinfo[1] : "") ;
    if (rcinfo[1])
    {
      memcpy(dir + dirlen + 1, rcinfo[1], loglen + 1) ;
      s6_auto_write_logger_tmp(dir, loguser, logdir, stamptype, nfiles, filesize, maxsize, prefix, rcinfo[0], rcinfo[2], &sa) ;
    }
  }
  else
  {
    write_service(argv[1], user, argv[0], maxinstances, 0) ;
    if (logdir)
    {
      char dir[dirlen + 5] ;
      memcpy(dir, argv[1], dirlen) ;
      memcpy(dir + dirlen, "/log", 5) ;
      s6_auto_write_logger_tmp(dir, loguser, logdir, stamptype, nfiles, filesize, maxsize, prefix, 0, 0, &sa) ;
    }
  }
  return 0 ;
}