]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7584d236 | 2 | /*** |
7584d236 | 3 | Copyright 2013 Zbigniew Jędrzejewski-Szmek |
7584d236 ZJS |
4 | ***/ |
5 | ||
a8fbdf54 TA |
6 | #include <errno.h> |
7 | #include <stdio.h> | |
8 | #include <string.h> | |
9 | #include <unistd.h> | |
7584d236 | 10 | |
f97b34a6 | 11 | #include "format-util.h" |
b1d4f8e1 | 12 | #include "install-printf.h" |
a8fbdf54 TA |
13 | #include "install.h" |
14 | #include "macro.h" | |
7584d236 | 15 | #include "specifier.h" |
6e3136b2 | 16 | #include "string-util.h" |
7584d236 | 17 | #include "unit-name.h" |
b1d4f8e1 | 18 | #include "user-util.h" |
7584d236 | 19 | |
19f6d710 | 20 | static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) { |
cfd559c9 ZJS |
21 | const UnitFileInstallInfo *i = userdata; |
22 | _cleanup_free_ char *prefix = NULL; | |
23 | int r; | |
24 | ||
25 | assert(i); | |
26 | ||
27 | r = unit_name_to_prefix_and_instance(i->name, &prefix); | |
28 | if (r < 0) | |
29 | return r; | |
30 | ||
31 | if (endswith(prefix, "@") && i->default_instance) { | |
32 | char *ans; | |
33 | ||
605405c6 | 34 | ans = strjoin(prefix, i->default_instance); |
cfd559c9 ZJS |
35 | if (!ans) |
36 | return -ENOMEM; | |
37 | *ret = ans; | |
ae2a15bc LP |
38 | } else |
39 | *ret = TAKE_PTR(prefix); | |
cfd559c9 ZJS |
40 | |
41 | return 0; | |
42 | } | |
43 | ||
44 | static int specifier_name(char specifier, void *data, void *userdata, char **ret) { | |
45 | const UnitFileInstallInfo *i = userdata; | |
46 | char *ans; | |
19f6d710 | 47 | |
7584d236 ZJS |
48 | assert(i); |
49 | ||
cfd559c9 ZJS |
50 | if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance) |
51 | return unit_name_replace_instance(i->name, i->default_instance, ret); | |
52 | ||
53 | ans = strdup(i->name); | |
54 | if (!ans) | |
55 | return -ENOMEM; | |
56 | *ret = ans; | |
57 | return 0; | |
7584d236 ZJS |
58 | } |
59 | ||
19f6d710 | 60 | static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) { |
cfd559c9 | 61 | const UnitFileInstallInfo *i = userdata; |
19f6d710 | 62 | |
7584d236 ZJS |
63 | assert(i); |
64 | ||
7410616c | 65 | return unit_name_to_prefix(i->name, ret); |
7584d236 ZJS |
66 | } |
67 | ||
19f6d710 | 68 | static int specifier_instance(char specifier, void *data, void *userdata, char **ret) { |
6e3136b2 | 69 | const UnitFileInstallInfo *i = userdata; |
7584d236 ZJS |
70 | char *instance; |
71 | int r; | |
72 | ||
73 | assert(i); | |
74 | ||
75 | r = unit_name_to_instance(i->name, &instance); | |
76 | if (r < 0) | |
19f6d710 LP |
77 | return r; |
78 | ||
6e3136b2 | 79 | if (isempty(instance)) { |
87e4e28d | 80 | r = free_and_strdup(&instance, strempty(i->default_instance)); |
402a81c7 ZJS |
81 | if (r < 0) |
82 | return r; | |
19f6d710 LP |
83 | } |
84 | ||
85 | *ret = instance; | |
86 | return 0; | |
7584d236 ZJS |
87 | } |
88 | ||
250e9fad ZJS |
89 | static int specifier_last_component(char specifier, void *data, void *userdata, char **ret) { |
90 | _cleanup_free_ char *prefix = NULL; | |
91 | char *dash; | |
92 | int r; | |
93 | ||
94 | r = specifier_prefix(specifier, data, userdata, &prefix); | |
95 | if (r < 0) | |
96 | return r; | |
97 | ||
98 | dash = strrchr(prefix, '-'); | |
99 | if (dash) { | |
100 | dash = strdup(dash + 1); | |
101 | if (!dash) | |
102 | return -ENOMEM; | |
103 | *ret = dash; | |
104 | } else | |
105 | *ret = TAKE_PTR(prefix); | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
cab6235f | 110 | int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) { |
7584d236 ZJS |
111 | |
112 | /* This is similar to unit_full_printf() but does not support | |
113 | * anything path-related. | |
114 | * | |
115 | * %n: the full id of the unit (foo@bar.waldo) | |
116 | * %N: the id of the unit without the suffix (foo@bar) | |
117 | * %p: the prefix (foo) | |
118 | * %i: the instance (bar) | |
119 | ||
79413b67 LP |
120 | * %U the UID of the running user |
121 | * %u the username of running user | |
7584d236 ZJS |
122 | * %m the machine ID of the running system |
123 | * %H the host name of the running system | |
124 | * %b the boot ID of the running system | |
6aaa8c2f | 125 | * %v `uname -r` of the running system |
7584d236 ZJS |
126 | */ |
127 | ||
128 | const Specifier table[] = { | |
cfd559c9 | 129 | { 'n', specifier_name, NULL }, |
7584d236 ZJS |
130 | { 'N', specifier_prefix_and_instance, NULL }, |
131 | { 'p', specifier_prefix, NULL }, | |
132 | { 'i', specifier_instance, NULL }, | |
250e9fad | 133 | { 'j', specifier_last_component, NULL }, |
7584d236 | 134 | |
79413b67 | 135 | { 'U', specifier_user_id, NULL }, |
3f0b2f0f | 136 | { 'u', specifier_user_name, NULL }, |
7584d236 ZJS |
137 | |
138 | { 'm', specifier_machine_id, NULL }, | |
139 | { 'H', specifier_host_name, NULL }, | |
140 | { 'b', specifier_boot_id, NULL }, | |
6aaa8c2f ZJS |
141 | { 'v', specifier_kernel_release, NULL }, |
142 | {} | |
7584d236 ZJS |
143 | }; |
144 | ||
145 | assert(i); | |
146 | assert(format); | |
19f6d710 | 147 | assert(ret); |
7584d236 | 148 | |
19f6d710 | 149 | return specifier_printf(format, table, i, ret); |
7584d236 | 150 | } |