summaryrefslogtreecommitdiff
path: root/src/s6-rc/s6-rc-bundle.c
blob: c171fdc7e1a6d6ee764fec44871ebdecc44dee11 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* ISC license. */

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <skalibs/uint32.h>
#include <skalibs/bytestr.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/sgetopt.h>
#include <skalibs/bitarray.h>
#include <skalibs/djbunix.h>
#include <skalibs/cdb.h>
#include <skalibs/cdb_make.h>
#include <skalibs/unix-transactional.h>
#include <execline/execline.h>
#include <s6-rc/config.h>
#include <s6-rc/s6rc.h>

#define USAGE "s6-rc-bundle [ -l live ] [ -c compiled ] command... (use s6-rc-bundle help for more information)"
#define dieusage() strerr_dieusage(100, USAGE)

static void cleanup (char const *compiled)
{
  int e = errno ;
  unsigned int len = str_len(compiled) ;
  char fn[len + sizeof("/resolve.cdb.new")] ;
  byte_copy(fn, len, compiled) ;
  byte_copy(fn + len, sizeof("/resolve.cdb.new"), "/resolve.cdb.new") ;
  unlink(fn) ;
  errno = e ;
}


 /* switch to renameat when it's more portable */

static inline int renameit (char const *compiled, char const *src, char const *dst)
{
  unsigned int clen = str_len(compiled) ;
  unsigned int srclen = str_len(src) ;
  unsigned int dstlen = str_len(dst) ;
  char srcfn[clen + srclen + 2] ;
  char dstfn[clen + dstlen + 2] ;
  byte_copy(srcfn, clen, compiled) ;
  srcfn[clen] = '/' ;
  byte_copy(srcfn + clen + 1, srclen + 1, src) ;
  byte_copy(dstfn, clen, compiled) ;
  dstfn[clen] = '/' ;
  byte_copy(dstfn + clen + 1, dstlen + 1, dst) ;
  return rename(srcfn, dstfn) ;
}

static void check (cdb_t *cr, s6rc_db_t *db, char const *name, int h, int force, char const *compiled)
{
  unsigned int namelen = str_len(name) ;
  register int r = cdb_find(cr, name, namelen) ;
  if (r < 0) strerr_diefu3sys(111, "cdb_find in ", compiled, "/resolve.cdb") ;
  if (!r)
  {
    if (!h && !force)
      strerr_dief4x(3, "identifier ", name, "does not exist in database ", compiled) ;
    return ;
  }
  if (h && !force)
    strerr_dief4x(1, "identifier ", name, " exists in database ", compiled) ;
  if (cdb_datalen(cr) == 4)
  {
    uint32 x ;
    char pack[4] ;
    if (cdb_read(cr, pack, 4, cdb_datapos(cr)) < 0)
      strerr_diefu3sys(111, "cdb_read ", compiled, "/resolve.cdb") ;
    uint32_unpack_big(pack, &x) ;
    if (x >= db->nshort + db->nlong)
      strerr_dief2x(4, "invalid database in ", compiled) ;
    if (!str_diff(name, db->string + db->services[x].name))
      strerr_dief4x(5, "identifier ", name, " does not represent a bundle for database ", compiled) ;
  }
}

static void modify_resolve (int fdcompiled, s6rc_db_t *db, char const *const *todel, unsigned int todeln, char const *const *toadd, char const *const *const *toadd_contents, unsigned int toaddn, int force, char const *compiled)
{
  cdb_t cr = CDB_ZERO ;
  struct cdb_make cw = CDB_MAKE_ZERO ;
  uint32 kpos ;
  unsigned int i = toaddn ;
  int fdw ;
  int fdr = open_readatb(fdcompiled, "resolve.cdb") ;
  unsigned int n = db->nlong + db->nshort ;
  unsigned char bits[bitarray_div8(n)] ;
  if (fdr < 0) strerr_diefu3sys(111, "open ", compiled, "/resolve.cdb") ;
  if (!cdb_init_map(&cr, fdr, 1))
    strerr_diefu3sys(111, "cdb_init ", compiled, "/resolve.cdb") ;
  while (i--) check(&cr, db, toadd[i], 1, force, compiled) ;
  i = todeln ;
  while (i--) check(&cr, db, todel[i], 0, force, compiled) ;
  fdw = open_truncatb(fdcompiled, "resolve.cdb.new") ;
  if (fdw < 0) strerr_diefu3sys(111, "open ", compiled, "/resolve.cdb.new") ;
  if (cdb_make_start(&cw, fdw) < 0)
  {
    cleanup(compiled) ;
    strerr_diefu1sys(111, "cdb_make_start") ;
  }
  cdb_traverse_init(&cr, &kpos) ;
  for (;;)
  {
    register int r = cdb_nextkey(&cr, &kpos) ;
    if (r < 0)
    {
      cleanup(compiled) ;
      strerr_diefu3sys(111, "cdb_nextkey in ", compiled, "/resolve.cdb") ;
    }
    if (!r) break ;
    {
      unsigned int klen = cdb_keylen(&cr) ;
      char ktmp[klen + 1] ;
      if (cdb_read(&cr, ktmp, klen, cdb_keypos(&cr)) < 0)
      {
        cleanup(compiled) ;
        strerr_diefu3sys(111, "cdb_read ", compiled, "/resolve.cdb") ;
      }
      for (i = 0 ; i < todeln ; i++) if (!str_diffn(todel[i], ktmp, klen)) break ;
      if (i >= todeln)
      {
        unsigned int dlen = cdb_datalen(&cr) ;
        char dtmp[dlen + 1] ;
        if (cdb_read(&cr, dtmp, dlen, cdb_datapos(&cr)) < 0)
        {
          cleanup(compiled) ;
          strerr_diefu3sys(111, "cdb_read ", compiled, "/resolve.cdb") ;
        }
        if (cdb_make_add(&cw, ktmp, klen, dtmp, dlen) < 0)
        {
          cleanup(compiled) ;
          strerr_diefu1sys(111, "cdb_make_add") ;
        }
      }
    }
  }
  i = toaddn ;
  while (i--)
  {
    char const *const *p = toadd_contents[i] ;
    unsigned int total = 0 ;
    byte_zero(bits, bitarray_div8(n)) ;
    for (; *p ; p++)
    {
      register int r = cdb_find(&cr, *p, str_len(*p)) ;
      if (r < 0) strerr_diefu3sys(111, "cdb_find in ", compiled, "/resolve.cdb") ;
      if (!r) strerr_dief4x(3, "identifier ", *p, "does not exist in database ", compiled) ;
      {
        unsigned int j = cdb_datalen(&cr) ;
        char pack[j + 1] ;
        if (cdb_read(&cr, pack, j, cdb_datapos(&cr)) < 0)
        {
          cleanup(compiled) ;
          strerr_diefu3sys(111, "cdb_read ", compiled, "/resolve.cdb") ;
        }
        j >>= 2 ;
        while (j--)
        {
          uint32 x ;
          uint32_unpack_big(pack + (j << 2), &x) ;
          if (x >= db->nshort + db->nlong)
            strerr_dief2x(4, "invalid database in ", compiled) ;
          if (!bitarray_testandset(bits, x)) total++ ;
        }
      }
    }
    {
      char pack[(total << 2) + 1] ;
      char *s = pack ;
      uint32 j = n ;
      while (j--) if (bitarray_peek(bits, j))
      {
        uint32_pack_big(s, j) ;
        s += 4 ;
      }
      if (cdb_make_add(&cw, toadd[i], str_len(toadd[i]), pack, total << 2) < 0)
      {
        cleanup(compiled) ;
        strerr_diefu1sys(111, "cdb_make_add") ;
      }
    }
  }
  cdb_free(&cr) ;
  close(fdr) ;
  if (cdb_make_finish(&cw) < 0 || fsync(fdw) < 0)
  {
    cleanup(compiled) ;
    strerr_diefu3sys(111, "write to ", compiled, "/resolve.cdb.new") ;
  }
  close(fdw) ;
  if (renameit(compiled, "resolve.cdb.new", "resolve.cdb") < 0)
  {
    cleanup(compiled) ;
    strerr_diefu2sys(111, "rename resolve.cdb.new to resolve.cdb in ", compiled) ;
  }
}

static inline void parse_multiple (int argc, char const **argv, unsigned int *todeln, unsigned int *toaddn, char const **toadd, char const *const **toadd_contents)
{
  unsigned int m = 0 ;
  int argc1 = el_semicolon(argv) ;
  if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ;
  *todeln = argc1 ;
  argv += argc1 + 1 ; argc -= argc1 + 1 ;
  while (argc)
  {
    toadd[m] = *argv++ ;
    if (!--argc) strerr_dief1x(100, "missing bundle contents block") ;
    argc1 = el_semicolon(argv) ;
    if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ;
    argv[argc1] = 0 ;
    toadd_contents[m++] = argv ;
    argv += argc1 + 1 ; argc -= argc1 + 1 ;
  }
  *toaddn = m ;
}

static inline void print_help (void)
{
  static char const *help =
"s6-rc-bundle help\n"
"s6-rc-bundle add bundlename contents...\n"
"s6-rc-bundle delete bundlenames...\n"
"s6-rc-bundle multiple { to-delete } to-add { contents... } ... (execlineb syntax)\n" ;
  if (buffer_putsflush(buffer_1, help) < 0)
    strerr_diefu1sys(111, "write to stdout") ;
}

static inline unsigned int lookup (char const *const *table, char const *command)
{
  register unsigned int i = 0 ;
  for (; table[i] ; i++) if (!str_diff(command, table[i])) break ;
  return i ;
}

static inline unsigned int parse_command (char const *command)
{
  static char const *const command_table[5] =
  {
    "help",
    "add",
    "delete",
    "multiple",
    0
  } ;
  register unsigned int i = lookup(command_table, command) ;
  if (!command_table[i]) dieusage() ;
  return i ;
}

int main (int argc, char const **argv)
{
  char const *live = S6RC_LIVE_BASE ;
  char const *compiled = 0 ;
  unsigned int what ;
  int force = 0 ;
  PROG = "s6-rc-bundle" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "fl:c:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'f' : force = 1 ; break ;
        case 'l' : live = l.arg ; break ;
        case 'c' : compiled = l.arg ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  if (!argc) dieusage() ;
  what = parse_command(*argv++) ;
  if (!what)
  {
    print_help() ;
    return 0 ;
  }
  if (!--argc) dieusage() ;
  if (what != 2 && argc < 2) dieusage() ;

  {
    unsigned int livelen = str_len(live) ;
    int fdcompiled = -1 ;
    int compiledlock ;
    s6rc_db_t dbblob ;
    char compiledblob[compiled ? str_len(compiled) : livelen + 10] ;

    if (!compiled)
    {
      byte_copy(compiledblob, livelen, live) ;
      byte_copy(compiledblob + livelen, 10, "/compiled") ;
      compiled = compiledblob ;
    }

    if (!s6rc_lock(0, 0, 0, compiled, 2, &compiledlock))
      strerr_diefu2sys(111, "take lock on ", compiled) ;
    fdcompiled = open_readb(compiled) ;
    if (fdcompiled < 0)
      strerr_diefu2sys(111, "open ", compiled) ;


   /* Read the sizes of the compiled db */

    fdcompiled = open_readb(compiled) ;
    if (!s6rc_db_read_sizes(fdcompiled, &dbblob))
      strerr_diefu3sys(111, "read ", compiled, "/n") ;


   /* Allocate enough stack for the db */

    {
      unsigned int n = dbblob.nshort + dbblob.nlong ;
      s6rc_service_t serviceblob[n] ;
      char const *argvblob[dbblob.nargvs] ;
      uint32 depsblob[dbblob.ndeps << 1] ;
      char stringblob[dbblob.stringlen] ;
      register int r ;

      dbblob.services = serviceblob ;
      dbblob.argvs = argvblob ;
      dbblob.deps = depsblob ;
      dbblob.string = stringblob ;


     /* Read the db from the file */

      r = s6rc_db_read(fdcompiled, &dbblob) ;
      if (r < 0) strerr_diefu3sys(111, "read ", compiled, "/db") ;
      if (!r) strerr_dief3x(4, "invalid service database in ", compiled, "/db") ;


     /* Perform the action */

      switch (what)
      {
        case 1 : /* add */
        {
          char const *const *contents = argv + 1 ;
          modify_resolve(fdcompiled, &dbblob, 0, 0, argv, &contents, 1, force, compiled) ;
          break ;
        }
        case 2 : /* delete */
          modify_resolve(fdcompiled, &dbblob, argv, argc, 0, 0, 0, force, compiled) ;
          break ;
        case 3 : /* multiple */
        {
          unsigned int toaddn, todeln ;
          char const *toadd[argc - 1] ;
          char const *const *toadd_contents[argc - 1] ;
          parse_multiple(argc, argv, &todeln, &toaddn, toadd, toadd_contents) ;
          modify_resolve(fdcompiled, &dbblob, argv, todeln, toadd, toadd_contents, toaddn, force, compiled) ;
          break ;
        }
      }
    }
  }
  return 0 ;
}