]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7584d236 ZJS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Zbigniew Jędrzejewski-Szmek | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
a8fbdf54 TA |
21 | #include <errno.h> |
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
7584d236 | 25 | |
f97b34a6 | 26 | #include "format-util.h" |
b1d4f8e1 | 27 | #include "install-printf.h" |
a8fbdf54 TA |
28 | #include "install.h" |
29 | #include "macro.h" | |
7584d236 | 30 | #include "specifier.h" |
6e3136b2 | 31 | #include "string-util.h" |
7584d236 | 32 | #include "unit-name.h" |
b1d4f8e1 | 33 | #include "user-util.h" |
7584d236 | 34 | |
19f6d710 | 35 | static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) { |
cfd559c9 ZJS |
36 | const UnitFileInstallInfo *i = userdata; |
37 | _cleanup_free_ char *prefix = NULL; | |
38 | int r; | |
39 | ||
40 | assert(i); | |
41 | ||
42 | r = unit_name_to_prefix_and_instance(i->name, &prefix); | |
43 | if (r < 0) | |
44 | return r; | |
45 | ||
46 | if (endswith(prefix, "@") && i->default_instance) { | |
47 | char *ans; | |
48 | ||
605405c6 | 49 | ans = strjoin(prefix, i->default_instance); |
cfd559c9 ZJS |
50 | if (!ans) |
51 | return -ENOMEM; | |
52 | *ret = ans; | |
53 | } else { | |
54 | *ret = prefix; | |
55 | prefix = NULL; | |
56 | } | |
57 | ||
58 | return 0; | |
59 | } | |
60 | ||
61 | static int specifier_name(char specifier, void *data, void *userdata, char **ret) { | |
62 | const UnitFileInstallInfo *i = userdata; | |
63 | char *ans; | |
19f6d710 | 64 | |
7584d236 ZJS |
65 | assert(i); |
66 | ||
cfd559c9 ZJS |
67 | if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance) |
68 | return unit_name_replace_instance(i->name, i->default_instance, ret); | |
69 | ||
70 | ans = strdup(i->name); | |
71 | if (!ans) | |
72 | return -ENOMEM; | |
73 | *ret = ans; | |
74 | return 0; | |
7584d236 ZJS |
75 | } |
76 | ||
19f6d710 | 77 | static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) { |
cfd559c9 | 78 | const UnitFileInstallInfo *i = userdata; |
19f6d710 | 79 | |
7584d236 ZJS |
80 | assert(i); |
81 | ||
7410616c | 82 | return unit_name_to_prefix(i->name, ret); |
7584d236 ZJS |
83 | } |
84 | ||
19f6d710 | 85 | static int specifier_instance(char specifier, void *data, void *userdata, char **ret) { |
6e3136b2 | 86 | const UnitFileInstallInfo *i = userdata; |
7584d236 ZJS |
87 | char *instance; |
88 | int r; | |
89 | ||
90 | assert(i); | |
91 | ||
92 | r = unit_name_to_instance(i->name, &instance); | |
93 | if (r < 0) | |
19f6d710 LP |
94 | return r; |
95 | ||
6e3136b2 | 96 | if (isempty(instance)) { |
402a81c7 ZJS |
97 | r = free_and_strdup(&instance, i->default_instance ?: ""); |
98 | if (r < 0) | |
99 | return r; | |
19f6d710 LP |
100 | } |
101 | ||
102 | *ret = instance; | |
103 | return 0; | |
7584d236 ZJS |
104 | } |
105 | ||
19f6d710 | 106 | static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) { |
79413b67 | 107 | char *t; |
3f0b2f0f | 108 | |
79413b67 LP |
109 | /* If we are UID 0 (root), this will not result in NSS, |
110 | * otherwise it might. This is good, as we want to be able to | |
111 | * run this in PID 1, where our user ID is 0, but where NSS | |
6309e51e | 112 | * lookups are not allowed. |
3f0b2f0f | 113 | |
6309e51e ZJS |
114 | * We don't user getusername_malloc() here, because we don't want to look |
115 | * at $USER, to remain consistent with specifer_user_id() below. | |
116 | */ | |
117 | ||
118 | t = uid_to_name(getuid()); | |
79413b67 LP |
119 | if (!t) |
120 | return -ENOMEM; | |
19f6d710 | 121 | |
79413b67 | 122 | *ret = t; |
19f6d710 | 123 | return 0; |
3f0b2f0f ZJS |
124 | } |
125 | ||
79413b67 LP |
126 | static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) { |
127 | ||
128 | if (asprintf(ret, UID_FMT, getuid()) < 0) | |
129 | return -ENOMEM; | |
130 | ||
131 | return 0; | |
132 | } | |
3f0b2f0f | 133 | |
cab6235f | 134 | int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) { |
7584d236 ZJS |
135 | |
136 | /* This is similar to unit_full_printf() but does not support | |
137 | * anything path-related. | |
138 | * | |
139 | * %n: the full id of the unit (foo@bar.waldo) | |
140 | * %N: the id of the unit without the suffix (foo@bar) | |
141 | * %p: the prefix (foo) | |
142 | * %i: the instance (bar) | |
143 | ||
79413b67 LP |
144 | * %U the UID of the running user |
145 | * %u the username of running user | |
7584d236 ZJS |
146 | * %m the machine ID of the running system |
147 | * %H the host name of the running system | |
148 | * %b the boot ID of the running system | |
6aaa8c2f | 149 | * %v `uname -r` of the running system |
7584d236 ZJS |
150 | */ |
151 | ||
152 | const Specifier table[] = { | |
cfd559c9 | 153 | { 'n', specifier_name, NULL }, |
7584d236 ZJS |
154 | { 'N', specifier_prefix_and_instance, NULL }, |
155 | { 'p', specifier_prefix, NULL }, | |
156 | { 'i', specifier_instance, NULL }, | |
157 | ||
79413b67 | 158 | { 'U', specifier_user_id, NULL }, |
3f0b2f0f | 159 | { 'u', specifier_user_name, NULL }, |
7584d236 ZJS |
160 | |
161 | { 'm', specifier_machine_id, NULL }, | |
162 | { 'H', specifier_host_name, NULL }, | |
163 | { 'b', specifier_boot_id, NULL }, | |
6aaa8c2f ZJS |
164 | { 'v', specifier_kernel_release, NULL }, |
165 | {} | |
7584d236 ZJS |
166 | }; |
167 | ||
168 | assert(i); | |
169 | assert(format); | |
19f6d710 | 170 | assert(ret); |
7584d236 | 171 | |
19f6d710 | 172 | return specifier_printf(format, table, i, ret); |
7584d236 | 173 | } |