summaryrefslogtreecommitdiff
path: root/src/libs6rc/s6rc_db_read.c
blob: b233cfe77fd256f2486cc2272e26bd4470585b4a (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
/* ISC license. */

#include <string.h>
#include <stdint.h>

#include <skalibs/buffer.h>
#include <skalibs/env.h>
#include <skalibs/djbunix.h>
#include <skalibs/unix-transactional.h>
#include <skalibs/posixishard.h>
#include <s6-rc/s6rc-db.h>

static int s6rc_db_check_valid_string (char const *string, size_t stringlen, size_t pos)
{
  if (pos >= stringlen) return 0 ;
  if (strnlen(string + pos, stringlen - pos) == stringlen - pos) return 0 ;
  return 1 ;
}

static inline int s6rc_db_check_valid_strings (char const *string, size_t stringlen, size_t pos, unsigned int n)
{
  while (n--)
  {
    if (!s6rc_db_check_valid_string(string, stringlen, pos)) return 0 ;
    pos += strlen(string + pos) + 1 ;
  }
  return 1 ;
}

static inline int s6rc_db_read_uints (buffer *b, unsigned int max, uint32_t *p, unsigned int n)
{
  uint32_t x ;
  while (n--)
  {
    if (!s6rc_db_read_uint32(b, &x)) return -1 ;
    if (x >= max) return 0 ;
    *p++ = x ;
  }
  return 1 ;
}


static inline int s6rc_db_read_services (buffer *b, s6rc_db_t *db)
{
  unsigned int n = db->nshort + db->nlong ;
  unsigned int nargvs = db->nargvs ;
  unsigned int argvpos = 0 ;
  unsigned int i = 0 ;
  for (; i < n ; i++)
  {
    s6rc_service_t *sv = db->services + i ;
    if (!s6rc_db_read_uint32(b, &sv->name)) return -1 ;
    if (sv->name >= db->stringlen) return 0 ;
    if (!s6rc_db_check_valid_string(db->string, db->stringlen, sv->name)) return 0 ;
    if (!s6rc_db_read_uint32(b, &sv->flags)) return -1 ;
    if (!s6rc_db_read_uint32(b, &sv->timeout[0])) return -1 ;
    if (!s6rc_db_read_uint32(b, &sv->timeout[1])) return -1 ;
    if (!s6rc_db_read_uint32(b, &sv->ndeps[0])) return -1 ;
    if (!s6rc_db_read_uint32(b, &sv->ndeps[1])) return -1 ;
    if (!s6rc_db_read_uint32(b, &sv->deps[0])) return -1 ;
    if (sv->deps[0] > db->ndeps || sv->deps[0] + sv->ndeps[0] > db->ndeps)
      return 0 ;
    if (!s6rc_db_read_uint32(b, &sv->deps[1])) return -1 ;
    if (sv->deps[1] > db->ndeps || sv->deps[1] + sv->ndeps[1] > db->ndeps)
      return 0 ;
    if (i < db->nlong)
    {
      if (!s6rc_db_read_uint32(b, &sv->x.longrun.consumer)) return -1 ;
      if (!s6rc_db_read_uint32(b, &sv->x.longrun.nproducers)) return -1 ;
      if (!s6rc_db_read_uint32(b, &sv->x.longrun.producers)) return -1 ;
    }
    else
    {
      unsigned int j = 0 ;
      for (; j < 2 ; j++)
      {
        uint32_t pos, argc ;
        if (!s6rc_db_read_uint32(b, &argc)) return -1 ;
        if (argc > nargvs) return 0 ;
        if (!s6rc_db_read_uint32(b, &pos)) return -1 ;
        if (!s6rc_db_check_valid_strings(db->string, db->stringlen, pos, argc)) return 0 ;
        if (!env_make((char const **)db->argvs + argvpos, argc, db->string + pos, db->stringlen - pos)) return -1 ;
        sv->x.oneshot.argv[j] = argvpos ;
        sv->x.oneshot.argc[j] = argc ;
        argvpos += argc ; nargvs -= argc ;
        if (!nargvs--) return 0 ;
        db->argvs[argvpos++] = 0 ;
      }
    }
    {
      char c ;
      if (buffer_get(b, &c, 1) < 1) return -1 ;
      if (c != '\376') return 0 ;
    }
  }
  if (nargvs) return 0 ;
  return 1 ;
}

static inline int s6rc_db_read_string (buffer *b, char *string, size_t len)
{
  if (buffer_get(b, string, len) < (ssize_t)len) return -1 ;
  return 1 ;
}

static inline int s6rc_db_read_buffer (buffer *b, s6rc_db_t *db)
{
  {
    char banner[S6RC_DB_BANNER_START_LEN] ;
    if (buffer_get(b, banner, S6RC_DB_BANNER_START_LEN) < S6RC_DB_BANNER_START_LEN) return -1 ;
    if (memcmp(banner, S6RC_DB_BANNER_START, S6RC_DB_BANNER_START_LEN)) return 0 ;
  }

  {
    int r = s6rc_db_read_string(b, db->string, db->stringlen) ;
    if (r < 1) return r ;
    r = s6rc_db_read_uints(b, db->nshort + db->nlong, db->deps, db->ndeps << 1) ;
    if (r < 1) return r ;
    r = s6rc_db_read_uints(b, db->nlong, db->producers, db->nproducers) ;
    if (r < 1) return r ;
    r = s6rc_db_read_services(b, db) ;
    if (r < 1) return r ;
  }

  {
    char banner[S6RC_DB_BANNER_END_LEN] ;
    if (buffer_get(b, banner, S6RC_DB_BANNER_END_LEN) < S6RC_DB_BANNER_END_LEN) return -1 ;
    if (memcmp(banner, S6RC_DB_BANNER_END, S6RC_DB_BANNER_END_LEN)) return 0 ;
  }
  return 1 ;
}

int s6rc_db_read (int fdcompiled, s6rc_db_t *db)
{
  int r ;
  buffer b ;
  char buf[BUFFER_INSIZE] ;
  int fd = open_readatb(fdcompiled, "db") ;
  if (fd < 0) return -1 ;
  buffer_init(&b, &buffer_read, fd, buf, BUFFER_INSIZE) ;
  r = s6rc_db_read_buffer(&b, db) ;
  fd_close(fd) ;
  return r ;
}