summaryrefslogtreecommitdiff
path: root/manager.c
blob: 0ad60b2b0bd85dea191782aaf718ff73f21c3304 (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
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <assert.h>
#include <errno.h>
#include <string.h>

#include "manager.h"
#include "hashmap.h"
#include "macro.h"
#include "strv.h"
#include "load-fragment.h"

Manager* manager_new(void) {
        Manager *m;

        if (!(m = new0(Manager, 1)))
                return NULL;

        if (!(m->names = hashmap_new(string_hash_func, string_compare_func)))
                goto fail;

        if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
                goto fail;

        if (!(m->jobs_to_add = hashmap_new(trivial_hash_func, trivial_compare_func)))
                goto fail;

        if (!(m->jobs_to_remove = set_new(trivial_hash_func, trivial_compare_func)))
                goto fail;

        return m;

fail:
        manager_free(m);
        return NULL;
}

void manager_free(Manager *m) {
        Name *n;

        assert(m);

        while ((n = hashmap_first(m->names)))
                name_free(n);

        hashmap_free(m->names);
        hashmap_free(m->jobs);

        /* FIXME: This is incomplete */

        hashmap_free(m->jobs_to_add);
        set_free(m->jobs_to_remove);

        free(m);
}

int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, Job **_ret) {
        Job *ret, *other;
        void *state;
        Name *dep;
        int r;

        assert(m);
        assert(type < _JOB_TYPE_MAX);
        assert(name);
        assert(mode < _JOB_MODE_MAX);
        assert(_ret);

        /* Check for conflicts, first against the jobs we shall
         * create */
        if ((other = hashmap_get(m->jobs_to_add, name))) {

                if (other->type != type)
                        return -EEXIST;

        } else if (name->meta.job) {

                if (name->meta.job->type != type) {

                        if (mode == JOB_FAIL)
                                return -EEXIST;

                        if ((r = set_put(m->jobs_to_remove, name->meta.job)) < 0)
                                return r;
                }
        }

        if (!(ret = job_new(m, type, name)))
                return -ENOMEM;

        if ((r = hashmap_put(m->jobs_to_add, name, ret)) < 0)
                goto fail;

        if (type == JOB_START || type == JOB_VERIFY_STARTED || type == JOB_RESTART_FINISH) {
                SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
                        if ((r = manager_add_job(m, type, dep, mode, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
                        if ((r = manager_add_job(m, type, dep, JOB_FAIL, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
                        if ((r = manager_add_job(m, type, dep, JOB_FAIL, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
                        if ((r = manager_add_job(m, JOB_VERIFY_STARTED, dep, mode, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
                        if ((r = manager_add_job(m, JOB_VERIFY_STARTED, dep, JOB_FAIL, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
                        if ((r = manager_add_job(m, type, dep, mode, NULL)) < 0)
                                goto fail;

        } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {

                SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
                        if ((r = manager_add_job(m, type, dep, mode, NULL)) < 0)
                                goto fail;
        }

        if (_ret)
                *_ret = ret;

        return 0;

fail:
        job_free(ret);

        return r;
}


Job *manager_get_job(Manager *m, uint32_t id) {
        assert(m);

        return hashmap_get(m->jobs, UINT32_TO_PTR(id));
}

Name *manager_get_name(Manager *m, const char *name) {
        assert(m);
        assert(name);

        return hashmap_get(m->names, name);
}

static int verify_type(Name *name) {
        char *n;
        void *state;

        assert(name);

        /* Checks that all aliases of this name have the same and valid type */

        SET_FOREACH(n, name->meta.names, state) {
                NameType t;

                if ((t = name_type_from_string(n)) == _NAME_TYPE_INVALID)
                        return -EINVAL;

                if (name->meta.type == _NAME_TYPE_INVALID) {
                        name->meta.type = t;
                        continue;
                }

                if (name->meta.type != t)
                        return -EINVAL;
        }

        if (name->meta.type == _NAME_TYPE_INVALID)
                return -EINVAL;

        return 0;
}

static int service_load_sysv(Service *s) {
        assert(s);

        /* Load service data from SysV init scripts, preferably with
         * LSB headers ... */

        return 0;
}

static int name_load_fstab(Name *n) {
        assert(n);
        assert(n->meta.type == NAME_MOUNT || n->meta.type == NAME_AUTOMOUNT);

        /* Load mount data from /etc/fstab */

        return 0;
}

static int snapshot_load(Snapshot *s) {
        assert(s);

        /* Load snapshots from disk */

        return 0;
}

static int name_load_dropin(Name *n) {
        assert(n);

        /* Load dependencies from drop-in directories */

        return 0;
}

static int load(Name *name) {
        int r;

        assert(name);

        if (name->meta.state != NAME_STUB)
                return 0;

        if ((r = verify_type(name)) < 0)
                return r;

        if (name->meta.type == NAME_SERVICE) {

                /* Load a .service file */
                if ((r = name_load_fragment(name)) == 0)
                        goto finish;

                /* Load a classic init script */
                if (r == -ENOENT)
                        if ((r = service_load_sysv(SERVICE(name))) == 0)
                                goto finish;

        } else if (name->meta.type == NAME_MOUNT ||
                   name->meta.type == NAME_AUTOMOUNT) {

                if ((r = name_load_fstab(name)) == 0)
                        goto finish;

        } else if (name->meta.type == NAME_SNAPSHOT) {

                if ((r = snapshot_load(SNAPSHOT(name))) == 0)
                        goto finish;

        } else {
                if ((r = name_load_fragment(name)) == 0)
                        goto finish;
        }

        name->meta.state = NAME_FAILED;
        return r;

finish:
        if ((r = name_load_dropin(name)) < 0)
                return r;

        name->meta.state = NAME_LOADED;
        return 0;
}

static int dispatch_load_queue(Manager *m) {
        Meta *meta;

        assert(m);

        /* Make sure we are not run recursively */
        if (m->dispatching_load_queue)
                return 0;

        m->dispatching_load_queue = true;

        /* Dispatches the load queue. Takes a name from the queue and
         * tries to load its data until the queue is empty */

        while ((meta = m->load_queue)) {
                load(NAME(meta));
                LIST_REMOVE(Meta, m->load_queue, meta);
        }

        m->dispatching_load_queue = false;

        return 0;
}

int manager_load_name(Manager *m, const char *name, Name **_ret) {
        Name *ret;
        NameType t;
        int r;
        char *n;

        assert(m);
        assert(name);
        assert(_ret);

        if (!name_is_valid(name))
                return -EINVAL;

        /* This will load the service information files, but not actually
         * start any services or anything */

        if ((ret = manager_get_name(m, name)))
                goto finish;

        if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
                return -EINVAL;

        if (!(ret = name_new(m)))
                return -ENOMEM;

        ret->meta.type = t;

        if (!(n = strdup(name))) {
                name_free(ret);
                return -ENOMEM;
        }

        if (set_put(ret->meta.names, n) < 0) {
                name_free(ret);
                free(n);
                return -ENOMEM;
        }

        if ((r = name_link(ret)) < 0) {
                name_free(ret);
                return r;
        }

        /* At this point the new entry is created and linked. However,
         * not loaded. Now load this entry and all its dependencies
         * recursively */

        dispatch_load_queue(m);

finish:

        *_ret = ret;
        return 0;
}