summaryrefslogtreecommitdiff
path: root/name.c
blob: 340e8e424ab542bce3b1b5500a91cf2350c94657 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*-*- Mode: C; c-basic-offset: 8 -*-*/

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

#include "set.h"
#include "name.h"
#include "macro.h"
#include "strv.h"
#include "load-fragment.h"

NameType name_type_from_string(const char *n) {
        NameType t;
        static const char* suffixes[_NAME_TYPE_MAX] = {
                [NAME_SERVICE] = ".service",
                [NAME_TIMER] = ".timer",
                [NAME_SOCKET] = ".socket",
                [NAME_MILESTONE] = ".milestone",
                [NAME_DEVICE] = ".device",
                [NAME_MOUNT] = ".mount",
                [NAME_AUTOMOUNT] = ".automount",
                [NAME_SNAPSHOT] = ".snapshot",
        };

        assert(n);

        for (t = 0; t < _NAME_TYPE_MAX; t++)
                if (endswith(n, suffixes[t]))
                        return t;

        return _NAME_TYPE_INVALID;
}

#define VALID_CHARS                             \
        "0123456789"                            \
        "abcdefghijklmnopqrstuvwxyz"            \
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
        "-_"

bool name_is_valid(const char *n) {
        NameType t;
        const char *e, *i;

        assert(n);

        t = name_type_from_string(n);
        if (t < 0 || t >= _NAME_TYPE_MAX)
                return false;

        if (!(e = strrchr(n, '.')))
                return false;

        for (i = n; i < e; i++)
                if (!strchr(VALID_CHARS, *i))
                        return false;

        return true;
}

Name *name_new(Manager *m) {
        Name *n;

        assert(m);

        if (!(n = new0(Name, 1)))
                return NULL;

        if (!(n->meta.names = set_new(string_hash_func, string_compare_func))) {
                free(n);
                return NULL;
        }

        /* Not much initialization happening here at this time */
        n->meta.manager = m;
        n->meta.type = _NAME_TYPE_INVALID;
        n->meta.state = NAME_STUB;

        /* We don't link the name here, that is left for name_link() */

        return n;
}

/* FIXME: Does not rollback on failure! */
int name_link_names(Name *n, bool replace) {
        char *t;
        void *state;
        int r;

        assert(n);

        if (!n->meta.linked)
                return 0;

        /* Link all names that aren't linked yet. */

        SET_FOREACH(t, n->meta.names, state)
                if (replace) {
                        if ((r = hashmap_replace(n->meta.manager->names, t, n)) < 0)
                                return r;
                } else {
                        if ((r = hashmap_put(n->meta.manager->names, t, n)) < 0)
                                return r;
                }

        return 0;
}

int name_link(Name *n) {
        int r;

        assert(n);
        assert(!set_isempty(n->meta.names));
        assert(!n->meta.linked);

        if ((r = name_sanitize(n)) < 0)
                return r;

        n->meta.linked = true;

        if ((r = name_link_names(n, false)) < 0) {
                char *t;
                void *state;

                /* Rollback the registered names */
                SET_FOREACH(t, n->meta.names, state)
                        hashmap_remove_value(n->meta.manager->names, t, n);

                n->meta.linked = false;
                return r;
        }

        if (n->meta.state == NAME_STUB)
                LIST_PREPEND(Meta, n->meta.manager->load_queue, &n->meta);

        return 0;
}

static void bidi_set_free(Name *name, Set *s) {
        void *state;
        Name *other;

        assert(name);

        /* Frees the set and makes sure we are dropped from the
         * inverse pointers */

        SET_FOREACH(other, s, state) {
                NameDependency d;

                for (d = 0; d < _NAME_DEPENDENCY_MAX; d++)
                        set_remove(other->meta.dependencies[d], name);
        }

        set_free(s);
}

void name_free(Name *name) {
        NameDependency d;
        char *t;

        assert(name);

        /* Detach from next 'bigger' objects */
        if (name->meta.linked) {
                char *t;
                void *state;

                SET_FOREACH(t, name->meta.names, state)
                        hashmap_remove_value(name->meta.manager->names, t, name);

                if (name->meta.state == NAME_STUB)
                        LIST_REMOVE(Meta, name->meta.manager->load_queue, &name->meta);
        }

        /* Free data and next 'smaller' objects */
        if (name->meta.job)
                job_free(name->meta.job);

        for (d = 0; d < _NAME_DEPENDENCY_MAX; d++)
                bidi_set_free(name, name->meta.dependencies[d]);

        switch (name->meta.type) {

                case NAME_SOCKET: {
                        unsigned i;
                        Socket *s = SOCKET(name);

                        for (i = 0; i < s->n_fds; i++)
                                close_nointr(s->fds[i]);
                        break;
                }

                case NAME_DEVICE: {
                        Device *d = DEVICE(name);

                        free(d->sysfs);
                        break;
                }

                case NAME_MOUNT: {
                        Mount *m = MOUNT(name);

                        free(m->path);
                        break;
                }

                case NAME_AUTOMOUNT: {
                        Automount *a = AUTOMOUNT(name);

                        free(a->path);
                        break;
                }

                default:
                        ;
        }

        free(name->meta.description);

        while ((t = set_steal_first(name->meta.names)))
                free(t);
        set_free(name->meta.names);

        free(name);
}

bool name_is_ready(Name *name) {

        assert(name);

        if (name->meta.state != NAME_LOADED)
                return false;

        assert(name->meta.type < _NAME_TYPE_MAX);

        switch (name->meta.type) {
                case NAME_SERVICE: {
                        Service *s = SERVICE(name);

                        return
                                s->state == SERVICE_RUNNING ||
                                s->state == SERVICE_RELOAD_PRE ||
                                s->state == SERVICE_RELOAD ||
                                s->state == SERVICE_RELOAD_POST;
                }

                case NAME_TIMER: {
                        Timer *t = TIMER(name);

                        return
                                t->state == TIMER_WAITING ||
                                t->state == TIMER_RUNNING;
                }

                case NAME_SOCKET: {
                        Socket *s = SOCKET(name);

                        return
                                s->state == SOCKET_LISTENING ||
                                s->state == SOCKET_RUNNING;
                }

                case NAME_MILESTONE:
                        return MILESTONE(name)->state == MILESTONE_ACTIVE;

                case NAME_DEVICE:
                        return DEVICE(name)->state == DEVICE_AVAILABLE;

                case NAME_MOUNT:
                        return MOUNT(name)->state == MOUNT_MOUNTED;

                case NAME_AUTOMOUNT: {
                        Automount *a = AUTOMOUNT(name);

                        return
                                a->state == AUTOMOUNT_WAITING ||
                                a->state == AUTOMOUNT_RUNNING;
                }

                case NAME_SNAPSHOT:
                        return SNAPSHOT(name)->state == SNAPSHOT_ACTIVE;


                case _NAME_TYPE_MAX:
                case _NAME_TYPE_INVALID:
                        ;
        }

        assert_not_reached("Unknown name type.");
        return false;
}

static int ensure_in_set(Set **s, void *data) {
        int r;

        assert(s);
        assert(data);

        if (!*s)
                if (!(*s = set_new(trivial_hash_func, trivial_compare_func)))
                        return -ENOMEM;

        if ((r = set_put(*s, data)) < 0)
                if (r != -EEXIST)
                        return r;

        return 0;
}

static int ensure_merge(Set **s, Set *other) {

        if (!other)
                return 0;

        if (*s)
                return set_merge(*s, other);

        if (!(*s = set_copy(other)))
                return -ENOMEM;

        return 0;
}

/* FIXME: Does not rollback on failure! */
int name_merge(Name *name, Name *other) {
        int r;
        NameDependency d;

        assert(name);
        assert(other);
        assert(name->meta.manager == other->meta.manager);

        /* This merges 'other' into 'name'. FIXME: This does not
         * rollback on failure. */

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

        if (other->meta.state != NAME_STUB)
                return -EINVAL;

        /* Merge names */
        if ((r = ensure_merge(&name->meta.names, other->meta.names)) < 0)
                return r;

        /* Merge dependencies */
        for (d = 0; d < _NAME_DEPENDENCY_MAX; d++)
                if ((r = ensure_merge(&name->meta.dependencies[d], other->meta.dependencies[d])) < 0)
                        return r;

        /* Hookup new deps and names */
        if (name->meta.linked) {
                if ((r = name_sanitize(name)) < 0)
                        return r;

                if ((r = name_link_names(name, true)) < 0)
                        return r;
        }

        return 0;
}

/* FIXME: Does not rollback on failure! */
static int augment(Name *n) {
        int r;
        void* state;
        Name *other;

        assert(n);

        /* Adds in the missing links to make all dependencies
         * bidirectional. */

        SET_FOREACH(other, n->meta.dependencies[NAME_BEFORE], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_AFTER], n)) < 0)
                        return r;
        SET_FOREACH(other, n->meta.dependencies[NAME_AFTER], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_BEFORE], n)) < 0)
                        return r;

        SET_FOREACH(other, n->meta.dependencies[NAME_CONFLICTS], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_CONFLICTS], n)) < 0)
                        return r;

        SET_FOREACH(other, n->meta.dependencies[NAME_REQUIRES], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_REQUIRED_BY], n)) < 0)
                        return r;
        SET_FOREACH(other, n->meta.dependencies[NAME_REQUISITE], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_REQUIRED_BY], n)) < 0)
                        return r;

        SET_FOREACH(other, n->meta.dependencies[NAME_WANTS], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n)) < 0)
                        return r;
        SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUIRES], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n)) < 0)
                        return r;
        SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUISITE], state)
                if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n)) < 0)
                        return r;

        return 0;
}

int name_sanitize(Name *n) {
        NameDependency d;

        assert(n);

        /* Remove loops */
        for (d = 0; d < _NAME_DEPENDENCY_MAX; d++)
                set_remove(n->meta.dependencies[d], n);

        return augment(n);
}

const char* name_id(Name *n) {
        assert(n);

        return set_first(n->meta.names);
}

void name_dump(Name *n, FILE *f, const char *prefix) {

        static const char* const state_table[_NAME_STATE_MAX] = {
                [NAME_STUB] = "stub",
                [NAME_LOADED] = "loaded",
                [NAME_FAILED] = "failed"
        };

        static const char* const socket_state_table[_SOCKET_STATE_MAX] = {
                [SOCKET_DEAD] = "dead",
                [SOCKET_BEFORE] = "before",
                [SOCKET_START_PRE] = "start-pre",
                [SOCKET_START] = "start",
                [SOCKET_START_POST] = "start-post",
                [SOCKET_LISTENING] = "listening",
                [SOCKET_RUNNING] = "running",
                [SOCKET_STOP_PRE] = "stop-pre",
                [SOCKET_STOP] = "stop",
                [SOCKET_STOP_POST] = "stop-post",
                [SOCKET_MAINTAINANCE] = "maintainance"
        };

        static const char* const dependency_table[_NAME_DEPENDENCY_MAX] = {
                [NAME_REQUIRES] = "Requires",
                [NAME_SOFT_REQUIRES] = "SoftRequires",
                [NAME_WANTS] = "Wants",
                [NAME_REQUISITE] = "Requisite",
                [NAME_SOFT_REQUISITE] = "SoftRequisite",
                [NAME_REQUIRED_BY] = "RequiredBy",
                [NAME_WANTED_BY] = "WantedBy",
                [NAME_CONFLICTS] = "Conflicts",
                [NAME_BEFORE] = "Before",
                [NAME_AFTER] = "After",
        };

        void *state;
        char *t;
        NameDependency d;

        assert(n);

        if (!prefix)
                prefix = "";

        fprintf(f,
                "%sName %s:\n"
                "%s\tDescription: %s\n"
                "%s\tName State: %s\n",
                prefix, name_id(n),
                prefix, n->meta.description ? n->meta.description : name_id(n),
                prefix, state_table[n->meta.state]);

        fprintf(f, "%s\tNames: ", prefix);
        SET_FOREACH(t, n->meta.names, state)
                fprintf(f, "%s ", t);
        fprintf(f, "\n");

        for (d = 0; d < _NAME_DEPENDENCY_MAX; d++) {
                void *state;
                Name *other;

                if (set_isempty(n->meta.dependencies[d]))
                        continue;

                fprintf(f, "%s\t%s: ", prefix, dependency_table[d]);

                SET_FOREACH(other, n->meta.dependencies[d], state)
                        fprintf(f, "%s ", name_id(other));

                fprintf(f, "\n");
        }


        switch (n->meta.type) {
                case NAME_SOCKET: {
                        int r;
                        char *s = NULL;
                        const char *t;

                        if ((r = address_print(&n->socket.address, &s)) < 0)
                                t = strerror(-r);
                        else
                                t = s;

                        fprintf(f,
                                "%s\tAddress: %s\n"
                                "%s\tSocket State: %s\n",
                                prefix, t,
                                prefix, socket_state_table[n->socket.state]);

                        free(s);
                        break;
                }

                default:
                        ;
        }

        if (n->meta.job) {
                char *p;

                if (asprintf(&p, "%s\t", prefix) >= 0)
                        prefix = p;
                else
                        p = NULL;

                job_dump(n->meta.job, f, prefix);
                free(p);
        }
}

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 -ENOENT;
}

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;
}

int name_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;

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

        if ((r = name_link_names(name, false)) < 0)
                return r;

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