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

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

#include "manager.h"
#include "hashmap.h"
#include "macro.h"
#include "strv.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.requires, state)
                        if ((r = manager_add_job(m, type, dep, mode, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.soft_requires, state)
                        if ((r = manager_add_job(m, type, dep, JOB_FAIL, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.wants, state)
                        if ((r = manager_add_job(m, type, dep, JOB_FAIL, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.requisite, state)
                        if ((r = manager_add_job(m, JOB_VERIFY_STARTED, dep, mode, NULL)) < 0)
                                goto fail;
                SET_FOREACH(dep, ret->name->meta.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.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.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 detect_type(Name *name) {
        char **n;

        assert(name);

        name->meta.type = _NAME_TYPE_INVALID;

        STRV_FOREACH(n, name->meta.names) {
                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;
        }

        return 0;
}

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

        /*... */

        return 0;
}

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

        /*... */

        return 0;
}

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

        /*... */

        return 0;
}

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

        /*... */

        return 0;
}

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

        assert(name);

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

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

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

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

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

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

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

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

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

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

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

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

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

        assert(m);

        /* 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);
        }

        return 0;
}



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

        assert(m);
        assert(name);
        assert(_ret);
/* 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 (!(ret->meta.names = strv_new(name, NULL))) {
                name_free(ret);
                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;
}