chiark / gitweb /
swap: adjust swap.c in a similar way to what we just did to mount.c
[elogind.git] / src / basic / unit-name.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "alloc-util.h"
27 #include "bus-label.h"
28 //#include "glob-util.h"
29 #include "hexdecoct.h"
30 #include "macro.h"
31 #include "path-util.h"
32 #include "string-table.h"
33 #include "string-util.h"
34 #include "strv.h"
35 #include "unit-name.h"
36
37 /* Characters valid in a unit name. */
38 #define VALID_CHARS                             \
39         DIGITS                                  \
40         LETTERS                                 \
41         ":-_.\\"
42
43 /* The same, but also permits the single @ character that may appear */
44 #define VALID_CHARS_WITH_AT                     \
45         "@"                                     \
46         VALID_CHARS
47
48 /* All chars valid in a unit name glob */
49 #define VALID_CHARS_GLOB                        \
50         VALID_CHARS_WITH_AT                     \
51         "[]!-*?"
52
53 bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
54         const char *e, *i, *at;
55
56         assert((flags & ~(UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0);
57
58         if (_unlikely_(flags == 0))
59                 return false;
60
61         if (isempty(n))
62                 return false;
63
64         if (strlen(n) >= UNIT_NAME_MAX)
65                 return false;
66
67         e = strrchr(n, '.');
68         if (!e || e == n)
69                 return false;
70
71         if (unit_type_from_string(e + 1) < 0)
72                 return false;
73
74         for (i = n, at = NULL; i < e; i++) {
75
76                 if (*i == '@' && !at)
77                         at = i;
78
79                 if (!strchr("@" VALID_CHARS, *i))
80                         return false;
81         }
82
83         if (at == n)
84                 return false;
85
86         if (flags & UNIT_NAME_PLAIN)
87                 if (!at)
88                         return true;
89
90         if (flags & UNIT_NAME_INSTANCE)
91                 if (at && e > at + 1)
92                         return true;
93
94         if (flags & UNIT_NAME_TEMPLATE)
95                 if (at && e == at + 1)
96                         return true;
97
98         return false;
99 }
100
101 bool unit_prefix_is_valid(const char *p) {
102
103         /* We don't allow additional @ in the prefix string */
104
105         if (isempty(p))
106                 return false;
107
108         return in_charset(p, VALID_CHARS);
109 }
110
111 bool unit_instance_is_valid(const char *i) {
112
113         /* The max length depends on the length of the string, so we
114          * don't really check this here. */
115
116         if (isempty(i))
117                 return false;
118
119         /* We allow additional @ in the instance string, we do not
120          * allow them in the prefix! */
121
122         return in_charset(i, "@" VALID_CHARS);
123 }
124
125 bool unit_suffix_is_valid(const char *s) {
126         if (isempty(s))
127                 return false;
128
129         if (s[0] != '.')
130                 return false;
131
132         if (unit_type_from_string(s + 1) < 0)
133                 return false;
134
135         return true;
136 }
137
138 #if 0 /// UNNEEDED by elogind
139 int unit_name_to_prefix(const char *n, char **ret) {
140         const char *p;
141         char *s;
142
143         assert(n);
144         assert(ret);
145
146         if (!unit_name_is_valid(n, UNIT_NAME_ANY))
147                 return -EINVAL;
148
149         p = strchr(n, '@');
150         if (!p)
151                 p = strrchr(n, '.');
152
153         assert_se(p);
154
155         s = strndup(n, p - n);
156         if (!s)
157                 return -ENOMEM;
158
159         *ret = s;
160         return 0;
161 }
162
163 int unit_name_to_instance(const char *n, char **instance) {
164         const char *p, *d;
165         char *i;
166
167         assert(n);
168         assert(instance);
169
170         if (!unit_name_is_valid(n, UNIT_NAME_ANY))
171                 return -EINVAL;
172
173         /* Everything past the first @ and before the last . is the instance */
174         p = strchr(n, '@');
175         if (!p) {
176                 *instance = NULL;
177                 return 0;
178         }
179
180         p++;
181
182         d = strrchr(p, '.');
183         if (!d)
184                 return -EINVAL;
185
186         i = strndup(p, d-p);
187         if (!i)
188                 return -ENOMEM;
189
190         *instance = i;
191         return 1;
192 }
193
194 int unit_name_to_prefix_and_instance(const char *n, char **ret) {
195         const char *d;
196         char *s;
197
198         assert(n);
199         assert(ret);
200
201         if (!unit_name_is_valid(n, UNIT_NAME_ANY))
202                 return -EINVAL;
203
204         d = strrchr(n, '.');
205         if (!d)
206                 return -EINVAL;
207
208         s = strndup(n, d - n);
209         if (!s)
210                 return -ENOMEM;
211
212         *ret = s;
213         return 0;
214 }
215
216 UnitType unit_name_to_type(const char *n) {
217         const char *e;
218
219         assert(n);
220
221         if (!unit_name_is_valid(n, UNIT_NAME_ANY))
222                 return _UNIT_TYPE_INVALID;
223
224         assert_se(e = strrchr(n, '.'));
225
226         return unit_type_from_string(e + 1);
227 }
228
229 int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
230         char *e, *s;
231         size_t a, b;
232
233         assert(n);
234         assert(suffix);
235         assert(ret);
236
237         if (!unit_name_is_valid(n, UNIT_NAME_ANY))
238                 return -EINVAL;
239
240         if (!unit_suffix_is_valid(suffix))
241                 return -EINVAL;
242
243         assert_se(e = strrchr(n, '.'));
244
245         a = e - n;
246         b = strlen(suffix);
247
248         s = new(char, a + b + 1);
249         if (!s)
250                 return -ENOMEM;
251
252         strcpy(mempcpy(s, n, a), suffix);
253         *ret = s;
254
255         return 0;
256 }
257 #endif // 0
258
259 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) {
260         char *s;
261
262         assert(prefix);
263         assert(suffix);
264         assert(ret);
265
266         if (!unit_prefix_is_valid(prefix))
267                 return -EINVAL;
268
269         if (instance && !unit_instance_is_valid(instance))
270                 return -EINVAL;
271
272         if (!unit_suffix_is_valid(suffix))
273                 return -EINVAL;
274
275         if (!instance)
276                 s = strappend(prefix, suffix);
277         else
278                 s = strjoin(prefix, "@", instance, suffix);
279         if (!s)
280                 return -ENOMEM;
281
282         *ret = s;
283         return 0;
284 }
285
286 #if 0 /// UNNEEDED by elogind
287 static char *do_escape_char(char c, char *t) {
288         assert(t);
289
290         *(t++) = '\\';
291         *(t++) = 'x';
292         *(t++) = hexchar(c >> 4);
293         *(t++) = hexchar(c);
294
295         return t;
296 }
297
298 static char *do_escape(const char *f, char *t) {
299         assert(f);
300         assert(t);
301
302         /* do not create units with a leading '.', like for "/.dotdir" mount points */
303         if (*f == '.') {
304                 t = do_escape_char(*f, t);
305                 f++;
306         }
307
308         for (; *f; f++) {
309                 if (*f == '/')
310                         *(t++) = '-';
311                 else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f))
312                         t = do_escape_char(*f, t);
313                 else
314                         *(t++) = *f;
315         }
316
317         return t;
318 }
319
320 char *unit_name_escape(const char *f) {
321         char *r, *t;
322
323         assert(f);
324
325         r = new(char, strlen(f)*4+1);
326         if (!r)
327                 return NULL;
328
329         t = do_escape(f, r);
330         *t = 0;
331
332         return r;
333 }
334
335 int unit_name_unescape(const char *f, char **ret) {
336         _cleanup_free_ char *r = NULL;
337         char *t;
338
339         assert(f);
340
341         r = strdup(f);
342         if (!r)
343                 return -ENOMEM;
344
345         for (t = r; *f; f++) {
346                 if (*f == '-')
347                         *(t++) = '/';
348                 else if (*f == '\\') {
349                         int a, b;
350
351                         if (f[1] != 'x')
352                                 return -EINVAL;
353
354                         a = unhexchar(f[2]);
355                         if (a < 0)
356                                 return -EINVAL;
357
358                         b = unhexchar(f[3]);
359                         if (b < 0)
360                                 return -EINVAL;
361
362                         *(t++) = (char) (((uint8_t) a << 4U) | (uint8_t) b);
363                         f += 3;
364                 } else
365                         *(t++) = *f;
366         }
367
368         *t = 0;
369
370         *ret = r;
371         r = NULL;
372
373         return 0;
374 }
375
376 int unit_name_path_escape(const char *f, char **ret) {
377         char *p, *s;
378
379         assert(f);
380         assert(ret);
381
382         p = strdupa(f);
383         if (!p)
384                 return -ENOMEM;
385
386         path_kill_slashes(p);
387
388         if (STR_IN_SET(p, "/", ""))
389                 s = strdup("-");
390         else {
391                 char *e;
392
393                 if (!path_is_safe(p))
394                         return -EINVAL;
395
396                 /* Truncate trailing slashes */
397                 e = endswith(p, "/");
398                 if (e)
399                         *e = 0;
400
401                 /* Truncate leading slashes */
402                 if (p[0] == '/')
403                         p++;
404
405                 s = unit_name_escape(p);
406         }
407         if (!s)
408                 return -ENOMEM;
409
410         *ret = s;
411         return 0;
412 }
413
414 int unit_name_path_unescape(const char *f, char **ret) {
415         char *s;
416         int r;
417
418         assert(f);
419
420         if (isempty(f))
421                 return -EINVAL;
422
423         if (streq(f, "-")) {
424                 s = strdup("/");
425                 if (!s)
426                         return -ENOMEM;
427         } else {
428                 char *w;
429
430                 r = unit_name_unescape(f, &w);
431                 if (r < 0)
432                         return r;
433
434                 /* Don't accept trailing or leading slashes */
435                 if (startswith(w, "/") || endswith(w, "/")) {
436                         free(w);
437                         return -EINVAL;
438                 }
439
440                 /* Prefix a slash again */
441                 s = strappend("/", w);
442                 free(w);
443                 if (!s)
444                         return -ENOMEM;
445
446                 if (!path_is_safe(s)) {
447                         free(s);
448                         return -EINVAL;
449                 }
450         }
451
452         if (ret)
453                 *ret = s;
454         else
455                 free(s);
456
457         return 0;
458 }
459
460 int unit_name_replace_instance(const char *f, const char *i, char **ret) {
461         const char *p, *e;
462         char *s;
463         size_t a, b;
464
465         assert(f);
466         assert(i);
467         assert(ret);
468
469         if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
470                 return -EINVAL;
471         if (!unit_instance_is_valid(i))
472                 return -EINVAL;
473
474         assert_se(p = strchr(f, '@'));
475         assert_se(e = strrchr(f, '.'));
476
477         a = p - f;
478         b = strlen(i);
479
480         s = new(char, a + 1 + b + strlen(e) + 1);
481         if (!s)
482                 return -ENOMEM;
483
484         strcpy(mempcpy(mempcpy(s, f, a + 1), i, b), e);
485
486         *ret = s;
487         return 0;
488 }
489
490 int unit_name_template(const char *f, char **ret) {
491         const char *p, *e;
492         char *s;
493         size_t a;
494
495         assert(f);
496         assert(ret);
497
498         if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
499                 return -EINVAL;
500
501         assert_se(p = strchr(f, '@'));
502         assert_se(e = strrchr(f, '.'));
503
504         a = p - f;
505
506         s = new(char, a + 1 + strlen(e) + 1);
507         if (!s)
508                 return -ENOMEM;
509
510         strcpy(mempcpy(s, f, a + 1), e);
511
512         *ret = s;
513         return 0;
514 }
515
516 int unit_name_from_path(const char *path, const char *suffix, char **ret) {
517         _cleanup_free_ char *p = NULL;
518         char *s = NULL;
519         int r;
520
521         assert(path);
522         assert(suffix);
523         assert(ret);
524
525         if (!unit_suffix_is_valid(suffix))
526                 return -EINVAL;
527
528         r = unit_name_path_escape(path, &p);
529         if (r < 0)
530                 return r;
531
532         s = strappend(p, suffix);
533         if (!s)
534                 return -ENOMEM;
535
536         *ret = s;
537         return 0;
538 }
539
540 int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret) {
541         _cleanup_free_ char *p = NULL;
542         char *s;
543         int r;
544
545         assert(prefix);
546         assert(path);
547         assert(suffix);
548         assert(ret);
549
550         if (!unit_prefix_is_valid(prefix))
551                 return -EINVAL;
552
553         if (!unit_suffix_is_valid(suffix))
554                 return -EINVAL;
555
556         r = unit_name_path_escape(path, &p);
557         if (r < 0)
558                 return r;
559
560         s = strjoin(prefix, "@", p, suffix);
561         if (!s)
562                 return -ENOMEM;
563
564         *ret = s;
565         return 0;
566 }
567
568 int unit_name_to_path(const char *name, char **ret) {
569         _cleanup_free_ char *prefix = NULL;
570         int r;
571
572         assert(name);
573
574         r = unit_name_to_prefix(name, &prefix);
575         if (r < 0)
576                 return r;
577
578         return unit_name_path_unescape(prefix, ret);
579 }
580
581 char *unit_dbus_path_from_name(const char *name) {
582         _cleanup_free_ char *e = NULL;
583
584         assert(name);
585
586         e = bus_label_escape(name);
587         if (!e)
588                 return NULL;
589
590         return strappend("/org/freedesktop/systemd1/unit/", e);
591 }
592
593 int unit_name_from_dbus_path(const char *path, char **name) {
594         const char *e;
595         char *n;
596
597         e = startswith(path, "/org/freedesktop/systemd1/unit/");
598         if (!e)
599                 return -EINVAL;
600
601         n = bus_label_unescape(e);
602         if (!n)
603                 return -ENOMEM;
604
605         *name = n;
606         return 0;
607 }
608
609 const char* unit_dbus_interface_from_type(UnitType t) {
610
611         static const char *const table[_UNIT_TYPE_MAX] = {
612                 [UNIT_SERVICE] = "org.freedesktop.systemd1.Service",
613                 [UNIT_SOCKET] = "org.freedesktop.systemd1.Socket",
614                 [UNIT_TARGET] = "org.freedesktop.systemd1.Target",
615                 [UNIT_DEVICE] = "org.freedesktop.systemd1.Device",
616                 [UNIT_MOUNT] = "org.freedesktop.systemd1.Mount",
617                 [UNIT_AUTOMOUNT] = "org.freedesktop.systemd1.Automount",
618                 [UNIT_SWAP] = "org.freedesktop.systemd1.Swap",
619                 [UNIT_TIMER] = "org.freedesktop.systemd1.Timer",
620                 [UNIT_PATH] = "org.freedesktop.systemd1.Path",
621                 [UNIT_SLICE] = "org.freedesktop.systemd1.Slice",
622                 [UNIT_SCOPE] = "org.freedesktop.systemd1.Scope",
623         };
624
625         if (t < 0)
626                 return NULL;
627         if (t >= _UNIT_TYPE_MAX)
628                 return NULL;
629
630         return table[t];
631 }
632
633 const char *unit_dbus_interface_from_name(const char *name) {
634         UnitType t;
635
636         t = unit_name_to_type(name);
637         if (t < 0)
638                 return NULL;
639
640         return unit_dbus_interface_from_type(t);
641 }
642
643 static char *do_escape_mangle(const char *f, UnitNameMangle allow_globs, char *t) {
644         const char *valid_chars;
645
646         assert(f);
647         assert(IN_SET(allow_globs, UNIT_NAME_GLOB, UNIT_NAME_NOGLOB));
648         assert(t);
649
650         /* We'll only escape the obvious characters here, to play
651          * safe. */
652
653         valid_chars = allow_globs == UNIT_NAME_GLOB ? VALID_CHARS_GLOB : VALID_CHARS_WITH_AT;
654
655         for (; *f; f++) {
656                 if (*f == '/')
657                         *(t++) = '-';
658                 else if (!strchr(valid_chars, *f))
659                         t = do_escape_char(*f, t);
660                 else
661                         *(t++) = *f;
662         }
663
664         return t;
665 }
666
667 /**
668  *  Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
669  *  /blah/blah is converted to blah-blah.mount, anything else is left alone,
670  *  except that @suffix is appended if a valid unit suffix is not present.
671  *
672  *  If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
673  */
674 int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret) {
675         char *s, *t;
676         int r;
677
678         assert(name);
679         assert(suffix);
680         assert(ret);
681
682         if (isempty(name)) /* We cannot mangle empty unit names to become valid, sorry. */
683                 return -EINVAL;
684
685         if (!unit_suffix_is_valid(suffix))
686                 return -EINVAL;
687
688         /* Already a fully valid unit name? If so, no mangling is necessary... */
689         if (unit_name_is_valid(name, UNIT_NAME_ANY))
690                 goto good;
691
692         /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
693         if (allow_globs == UNIT_NAME_GLOB &&
694             string_is_glob(name) &&
695             in_charset(name, VALID_CHARS_GLOB))
696                 goto good;
697
698         if (is_device_path(name)) {
699                 r = unit_name_from_path(name, ".device", ret);
700                 if (r >= 0)
701                         return 1;
702                 if (r != -EINVAL)
703                         return r;
704         }
705
706         if (path_is_absolute(name)) {
707                 r = unit_name_from_path(name, ".mount", ret);
708                 if (r >= 0)
709                         return 1;
710                 if (r != -EINVAL)
711                         return r;
712         }
713
714         s = new(char, strlen(name) * 4 + strlen(suffix) + 1);
715         if (!s)
716                 return -ENOMEM;
717
718         t = do_escape_mangle(name, allow_globs, s);
719         *t = 0;
720
721         /* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow "foo.*" as a
722          * valid glob. */
723         if ((allow_globs != UNIT_NAME_GLOB || !string_is_glob(s)) && unit_name_to_type(s) < 0)
724                 strcpy(t, suffix);
725
726         *ret = s;
727         return 1;
728
729 good:
730         s = strdup(name);
731         if (!s)
732                 return -ENOMEM;
733
734         *ret = s;
735         return 0;
736 }
737
738 int slice_build_parent_slice(const char *slice, char **ret) {
739         char *s, *dash;
740         int r;
741
742         assert(slice);
743         assert(ret);
744
745         if (!slice_name_is_valid(slice))
746                 return -EINVAL;
747
748         if (streq(slice, "-.slice")) {
749                 *ret = NULL;
750                 return 0;
751         }
752
753         s = strdup(slice);
754         if (!s)
755                 return -ENOMEM;
756
757         dash = strrchr(s, '-');
758         if (dash)
759                 strcpy(dash, ".slice");
760         else {
761                 r = free_and_strdup(&s, "-.slice");
762                 if (r < 0) {
763                         free(s);
764                         return r;
765                 }
766         }
767
768         *ret = s;
769         return 1;
770 }
771 #endif // 0
772
773 int slice_build_subslice(const char *slice, const char*name, char **ret) {
774         char *subslice;
775
776         assert(slice);
777         assert(name);
778         assert(ret);
779
780         if (!slice_name_is_valid(slice))
781                 return -EINVAL;
782
783         if (!unit_prefix_is_valid(name))
784                 return -EINVAL;
785
786         if (streq(slice, "-.slice"))
787                 subslice = strappend(name, ".slice");
788         else {
789                 char *e;
790
791                 assert_se(e = endswith(slice, ".slice"));
792
793                 subslice = new(char, (e - slice) + 1 + strlen(name) + 6 + 1);
794                 if (!subslice)
795                         return -ENOMEM;
796
797                 stpcpy(stpcpy(stpcpy(mempcpy(subslice, slice, e - slice), "-"), name), ".slice");
798         }
799
800         *ret = subslice;
801         return 0;
802 }
803
804 bool slice_name_is_valid(const char *name) {
805         const char *p, *e;
806         bool dash = false;
807
808         if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
809                 return false;
810
811         if (streq(name, "-.slice"))
812                 return true;
813
814         e = endswith(name, ".slice");
815         if (!e)
816                 return false;
817
818         for (p = name; p < e; p++) {
819
820                 if (*p == '-') {
821
822                         /* Don't allow initial dash */
823                         if (p == name)
824                                 return false;
825
826                         /* Don't allow multiple dashes */
827                         if (dash)
828                                 return false;
829
830                         dash = true;
831                 } else
832                         dash = false;
833         }
834
835         /* Don't allow trailing hash */
836         if (dash)
837                 return false;
838
839         return true;
840 }
841
842 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
843         [UNIT_SERVICE] = "service",
844         [UNIT_SOCKET] = "socket",
845         [UNIT_TARGET] = "target",
846         [UNIT_DEVICE] = "device",
847         [UNIT_MOUNT] = "mount",
848         [UNIT_AUTOMOUNT] = "automount",
849         [UNIT_SWAP] = "swap",
850         [UNIT_TIMER] = "timer",
851         [UNIT_PATH] = "path",
852         [UNIT_SLICE] = "slice",
853         [UNIT_SCOPE] = "scope",
854 };
855
856 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
857
858 #if 0 /// UNNEEDED by elogind
859 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
860         [UNIT_STUB] = "stub",
861         [UNIT_LOADED] = "loaded",
862         [UNIT_NOT_FOUND] = "not-found",
863         [UNIT_ERROR] = "error",
864         [UNIT_MERGED] = "merged",
865         [UNIT_MASKED] = "masked"
866 };
867
868 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
869
870 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
871         [UNIT_ACTIVE] = "active",
872         [UNIT_RELOADING] = "reloading",
873         [UNIT_INACTIVE] = "inactive",
874         [UNIT_FAILED] = "failed",
875         [UNIT_ACTIVATING] = "activating",
876         [UNIT_DEACTIVATING] = "deactivating"
877 };
878
879 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
880
881 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
882         [AUTOMOUNT_DEAD] = "dead",
883         [AUTOMOUNT_WAITING] = "waiting",
884         [AUTOMOUNT_RUNNING] = "running",
885         [AUTOMOUNT_FAILED] = "failed"
886 };
887
888 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
889
890 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
891         [DEVICE_DEAD] = "dead",
892         [DEVICE_TENTATIVE] = "tentative",
893         [DEVICE_PLUGGED] = "plugged",
894 };
895
896 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
897
898 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
899         [MOUNT_DEAD] = "dead",
900         [MOUNT_MOUNTING] = "mounting",
901         [MOUNT_MOUNTING_DONE] = "mounting-done",
902         [MOUNT_MOUNTED] = "mounted",
903         [MOUNT_REMOUNTING] = "remounting",
904         [MOUNT_UNMOUNTING] = "unmounting",
905         [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
906         [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
907         [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
908         [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
909         [MOUNT_FAILED] = "failed"
910 };
911
912 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
913
914 static const char* const path_state_table[_PATH_STATE_MAX] = {
915         [PATH_DEAD] = "dead",
916         [PATH_WAITING] = "waiting",
917         [PATH_RUNNING] = "running",
918         [PATH_FAILED] = "failed"
919 };
920
921 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
922
923 static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
924         [SCOPE_DEAD] = "dead",
925         [SCOPE_RUNNING] = "running",
926         [SCOPE_ABANDONED] = "abandoned",
927         [SCOPE_STOP_SIGTERM] = "stop-sigterm",
928         [SCOPE_STOP_SIGKILL] = "stop-sigkill",
929         [SCOPE_FAILED] = "failed",
930 };
931
932 DEFINE_STRING_TABLE_LOOKUP(scope_state, ScopeState);
933
934 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
935         [SERVICE_DEAD] = "dead",
936         [SERVICE_START_PRE] = "start-pre",
937         [SERVICE_START] = "start",
938         [SERVICE_START_POST] = "start-post",
939         [SERVICE_RUNNING] = "running",
940         [SERVICE_EXITED] = "exited",
941         [SERVICE_RELOAD] = "reload",
942         [SERVICE_STOP] = "stop",
943         [SERVICE_STOP_SIGABRT] = "stop-sigabrt",
944         [SERVICE_STOP_SIGTERM] = "stop-sigterm",
945         [SERVICE_STOP_SIGKILL] = "stop-sigkill",
946         [SERVICE_STOP_POST] = "stop-post",
947         [SERVICE_FINAL_SIGTERM] = "final-sigterm",
948         [SERVICE_FINAL_SIGKILL] = "final-sigkill",
949         [SERVICE_FAILED] = "failed",
950         [SERVICE_AUTO_RESTART] = "auto-restart",
951 };
952
953 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
954
955 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
956         [SLICE_DEAD] = "dead",
957         [SLICE_ACTIVE] = "active"
958 };
959
960 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
961
962 static const char* const socket_state_table[_SOCKET_STATE_MAX] = {
963         [SOCKET_DEAD] = "dead",
964         [SOCKET_START_PRE] = "start-pre",
965         [SOCKET_START_CHOWN] = "start-chown",
966         [SOCKET_START_POST] = "start-post",
967         [SOCKET_LISTENING] = "listening",
968         [SOCKET_RUNNING] = "running",
969         [SOCKET_STOP_PRE] = "stop-pre",
970         [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
971         [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
972         [SOCKET_STOP_POST] = "stop-post",
973         [SOCKET_FINAL_SIGTERM] = "final-sigterm",
974         [SOCKET_FINAL_SIGKILL] = "final-sigkill",
975         [SOCKET_FAILED] = "failed"
976 };
977
978 DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState);
979
980 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
981         [SWAP_DEAD] = "dead",
982         [SWAP_ACTIVATING] = "activating",
983         [SWAP_ACTIVATING_DONE] = "activating-done",
984         [SWAP_ACTIVE] = "active",
985         [SWAP_DEACTIVATING] = "deactivating",
986         [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
987         [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
988         [SWAP_FAILED] = "failed"
989 };
990
991 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
992
993 static const char* const target_state_table[_TARGET_STATE_MAX] = {
994         [TARGET_DEAD] = "dead",
995         [TARGET_ACTIVE] = "active"
996 };
997
998 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
999
1000 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
1001         [TIMER_DEAD] = "dead",
1002         [TIMER_WAITING] = "waiting",
1003         [TIMER_RUNNING] = "running",
1004         [TIMER_ELAPSED] = "elapsed",
1005         [TIMER_FAILED] = "failed"
1006 };
1007
1008 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
1009
1010 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1011         [UNIT_REQUIRES] = "Requires",
1012         [UNIT_REQUISITE] = "Requisite",
1013         [UNIT_WANTS] = "Wants",
1014         [UNIT_BINDS_TO] = "BindsTo",
1015         [UNIT_PART_OF] = "PartOf",
1016         [UNIT_REQUIRED_BY] = "RequiredBy",
1017         [UNIT_REQUISITE_OF] = "RequisiteOf",
1018         [UNIT_WANTED_BY] = "WantedBy",
1019         [UNIT_BOUND_BY] = "BoundBy",
1020         [UNIT_CONSISTS_OF] = "ConsistsOf",
1021         [UNIT_CONFLICTS] = "Conflicts",
1022         [UNIT_CONFLICTED_BY] = "ConflictedBy",
1023         [UNIT_BEFORE] = "Before",
1024         [UNIT_AFTER] = "After",
1025         [UNIT_ON_FAILURE] = "OnFailure",
1026         [UNIT_TRIGGERS] = "Triggers",
1027         [UNIT_TRIGGERED_BY] = "TriggeredBy",
1028         [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
1029         [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom",
1030         [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf",
1031         [UNIT_REFERENCES] = "References",
1032         [UNIT_REFERENCED_BY] = "ReferencedBy",
1033 };
1034
1035 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
1036 #endif // 0
1037
1038 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
1039         [NOTIFY_NONE] = "none",
1040         [NOTIFY_MAIN] = "main",
1041         [NOTIFY_EXEC] = "exec",
1042         [NOTIFY_ALL] = "all"
1043 };
1044
1045 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);