This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_conf.c
251 lines (191 loc) · 6.84 KB
/
init_conf.c
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
#include <string.h>
#include <stddef.h>
#include "config.h"
#include "init.h"
#include "init_conf.h"
#include "sys.h"
/* How reconfiguring works:
0. newblock is mmapped
1. new config is compiled into newblock
2. processes not in the new inittab are stopped
3. remaining pids are transferred from inittab to newblock
4. newblock replaces inittab, cfgblock is munmapped
Up until step 4 init uses the old inittab structure, because step 2
implies waiting for pids that have no place for them in newtab.
This file only handles step 1. configure() is the entry point.
It sets up newblock and returns 0 on success.
In case init gets another reconfigure request while in the main
loop during step 2, compiled newtab is discarded and we're back
to step 1.
Note that until rewirepointers() call late in the process, all pointers
in struct config, struct initrec and envp aren't real pointers, they are
offsets from the start of newblock. This is to avoid a lot of hassle
in case mremap changes the block address. */
/* default/built-in stuff */
const char* inittab = INITTAB;
const char* initdir = INITDIR;
/* addinitrec() fills all pointers in initrecs with offsets from newblock
to allow using MREMAP_MAYMOVE. Once newblock and newenviron are all
set up, we need to make those offsets into real pointers ("repoint" them)
by adding the base address of newblock.
Because the offsets are pointer-typed and (pointer + pointer) operation
is illegal, we turn them into integers by subtracting NULL. */
static void* repoint(void* p)
{
if(p - NULL > newblock.ptr)
return NULL; // XXX: should never happen
return p ? (newblock.addr + (p - NULL)) : p;
}
#define REPOINT(a) a = repoint(a)
/* Warning: while NCF->inittab, NCF->env and initrec.argv-s within inittab
are arrays of pointers, the fields of NCF are pointers themselves
but initrec.argv is not.
Thus, the contents of all three must be repointed (that's rewireptrsarray)
but initrec.argv must not be touched, unlike NCF->inittab and NCF->env.
Since char** or initrec** are not cast silently to void**, there are
explicit casts here which may mask compiler warnings. */
static void rewireptrsarray(void** a)
{
void** p;
for(p = a; *p; p++)
REPOINT(*p);
}
static void rewirepointers()
{
struct initrec** pp;
REPOINT(NCF->inittab);
rewireptrsarray((void**) NCF->inittab);
REPOINT(NCF->env);
rewireptrsarray((void**) NCF->env);
for(pp = NCF->inittab; *pp; pp++)
rewireptrsarray((void**) (*pp)->argv);
}
/* Make type* array[] style structure in newblock from a ptrlist
located at listoff in newblock. The array is NULL-terminated
at the back and/or at the front.
(inittab needs front NULL for reverse pass in initpass)
Because the pointers are only available when all the data has
been placed, the pointer array ends up after the actual data
in newblock, with back-referencing pointers.
This function is used to lay out config.inittab and config.env,
but not for initrec.argv which gets a different treatment. */
static int addptrsarray(offset listoff, int terminate)
{
struct ptrlist* list = newblockptr(listoff, struct ptrlist*);
int rem = list->count;
int ptrn = rem;
void** ptrs;
struct ptrnode* node;
offset nodeoff; /* in scratchblock */
offset ptrsoff; /* in newblock */
if(rem < 0) return -1;
if(terminate & NULL_FRONT) ptrn++;
if(terminate & NULL_BACK ) ptrn++;
if((ptrsoff = extendblock(ptrn*sizeof(void*))) < 0)
return -1;
ptrs = newblockptr(ptrsoff, void**);
/* leading NULL pointer is used as terminator
when traversing inittab backwards */
if(terminate & NULL_FRONT) {
*(ptrs++) = NULL;
ptrsoff += sizeof(void*);
}
/* set up offsets; repoiting will happen later */
for(nodeoff = list->head; rem && nodeoff; rem--) {
node = newblockptr(nodeoff, struct ptrnode*);
*(ptrs++) = NULL + nodeoff + sizeof(struct ptrnode);
nodeoff = node->next;
} if(rem)
/* less elements than expected; this may break initpass,
so let's not take chances */
return -1;
if(terminate & NULL_BACK)
*ptrs = NULL;
return ptrsoff;
}
/* Once all initrecs are in place, inittab[] and env[] pointer arrays
are appended, with pointers (well offsets at this point) referring
back to initrecs. */
static int finishinittab(void)
{
offset off;
if((off = addptrsarray(TABLIST, NULL_BOTH)) < 0)
return -1;
else
NCF->inittab = NULL + off;
if((off = addptrsarray(ENVLIST, NULL_BACK)) < 0)
return -1;
else
NCF->env = NULL + off;
NCF->initnum = SCR->inittab.count;
return 0;
}
/* Old inittab is cfg->inittab (which may or may not be CFG->inittab),
new inittab is NCF->inittab. Old inittab may be missing during initial
configuration without built-in one.
Still, even without cfg we need to initialize pids of run-once entries. */
static void transferpids(void)
{
struct initrec* p;
struct initrec* q;
struct initrec** qq;
for(qq = NCF->inittab; (q = *qq); qq++) {
/* Prevent w-type entries from being spawned during
the next initpass() just because they are new.
This requires (currlevel == nextlevel) which is enforced
with S_RECONF. */
if((q->flags & C_ONCE) && levelmatch(q, currlevel))
q->pid = -1;
if(!cfg) /* first call, no inittab to transfer pids from */
continue;
if(!*q->name) /* can't transfer unnamed entries */
continue;
if(!(p = findentry(q->name)))
/* the entry is new, nothing to transfer here */
continue;
q->pid = p->pid;
q->flags |= (p->flags & (P_MANUAL | P_FAILED | P_WAS_OK));
q->lastrun = p->lastrun;
q->lastsig = p->lastsig;
}
}
/* If successful, configure() leaves a valid struct config in newblock.
Otherwise, it should clean up after itself.
S_RECONFIG must be set in state to let main() know it's got to
pick up the new configuration, but that is done outside of configure
because the two places that call configure() handle failure a bit
differently.
configure() itself does not replace cfg.
That is done later by setnewconf(), called later from main(). */
int configure(int strict)
{
const int headersize = sizeof(struct config) + sizeof(struct scratch);
if(mmapblock(headersize))
goto nomap;
if(readinittab(inittab, strict))
goto unmap;
#ifdef INITDIR
if(readinitdir(initdir, strict))
goto unmap;
#endif
if(finishinittab())
goto unmap;
rewirepointers();
/* check if there are any entries at all */
if(NCF->initnum)
return 0; /* ok, we're good */
warn("no entries found in inittab");
unmap: munmapblock();
nomap: return -1;
}
/* Once initpass reports it's ok to switch configurations,
main calls setnewconf. By this point, all live pids and process
flags are still in the (old) cfg, so setnewconf starts by copying
anything relevant over to newblock.
After that, the blocks are exchanged and we're done. */
void setnewconf(void)
{
transferpids();
cfg = newblockptr(0, struct config*);
exchangeblocks();
}