1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdbool.h>
5 
6 #include "macro.h"
7 #include "unit-def.h"
8 
9 #define UNIT_NAME_MAX 256
10 
11 typedef enum UnitNameFlags {
12         UNIT_NAME_PLAIN    = 1 << 0, /* Allow foo.service */
13         UNIT_NAME_TEMPLATE = 1 << 1, /* Allow foo@.service */
14         UNIT_NAME_INSTANCE = 1 << 2, /* Allow foo@bar.service */
15         UNIT_NAME_ANY = UNIT_NAME_PLAIN|UNIT_NAME_TEMPLATE|UNIT_NAME_INSTANCE,
16         _UNIT_NAME_INVALID = -EINVAL,
17 } UnitNameFlags;
18 
19 bool unit_name_is_valid(const char *n, UnitNameFlags flags) _pure_;
20 bool unit_prefix_is_valid(const char *p) _pure_;
21 bool unit_instance_is_valid(const char *i) _pure_;
22 bool unit_suffix_is_valid(const char *s) _pure_;
23 
24 int unit_name_to_prefix(const char *n, char **ret);
25 UnitNameFlags unit_name_to_instance(const char *n, char **ret);
unit_name_classify(const char * n)26 static inline UnitNameFlags unit_name_classify(const char *n) {
27         return unit_name_to_instance(n, NULL);
28 }
29 int unit_name_to_prefix_and_instance(const char *n, char **ret);
30 
31 UnitType unit_name_to_type(const char *n) _pure_;
32 
33 int unit_name_change_suffix(const char *n, const char *suffix, char **ret);
34 
35 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret);
36 int unit_name_build_from_type(const char *prefix, const char *instance, UnitType, char **ret);
37 
38 char *unit_name_escape(const char *f);
39 int unit_name_unescape(const char *f, char **ret);
40 int unit_name_path_escape(const char *f, char **ret);
41 int unit_name_path_unescape(const char *f, char **ret);
42 
43 int unit_name_replace_instance(const char *f, const char *i, char **ret);
44 
45 int unit_name_template(const char *f, char **ret);
46 
47 int unit_name_hash_long(const char *name, char **ret);
48 bool unit_name_is_hashed(const char *name);
49 
50 int unit_name_from_path(const char *path, const char *suffix, char **ret);
51 int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret);
52 int unit_name_to_path(const char *name, char **ret);
53 
54 typedef enum UnitNameMangle {
55         UNIT_NAME_MANGLE_GLOB = 1 << 0,
56         UNIT_NAME_MANGLE_WARN = 1 << 1,
57 } UnitNameMangle;
58 
59 int unit_name_mangle_with_suffix(const char *name, const char *operation, UnitNameMangle flags, const char *suffix, char **ret);
60 
unit_name_mangle(const char * name,UnitNameMangle flags,char ** ret)61 static inline int unit_name_mangle(const char *name, UnitNameMangle flags, char **ret) {
62         return unit_name_mangle_with_suffix(name, NULL, flags, ".service", ret);
63 }
64 
65 int slice_build_parent_slice(const char *slice, char **ret);
66 int slice_build_subslice(const char *slice, const char *name, char **subslice);
67 bool slice_name_is_valid(const char *name);
68 
69 bool unit_name_prefix_equal(const char *a, const char *b);
70