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