]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
shared/install: fix DefaultInstance expansion in %i
[thirdparty/systemd.git] / src / shared / install-printf.c
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
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "formats-util.h"
26 #include "install-printf.h"
27 #include "install.h"
28 #include "macro.h"
29 #include "specifier.h"
30 #include "string-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
33
34 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
35 UnitFileInstallInfo *i = userdata;
36
37 assert(i);
38
39 return unit_name_to_prefix_and_instance(i->name, ret);
40 }
41
42 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
43 UnitFileInstallInfo *i = userdata;
44
45 assert(i);
46
47 return unit_name_to_prefix(i->name, ret);
48 }
49
50 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
51 const UnitFileInstallInfo *i = userdata;
52 char *instance;
53 int r;
54
55 assert(i);
56
57 r = unit_name_to_instance(i->name, &instance);
58 if (r < 0)
59 return r;
60
61 if (isempty(instance)) {
62 instance = strdup(i->default_instance ?: "");
63 if (!instance)
64 return -ENOMEM;
65 }
66
67 *ret = instance;
68 return 0;
69 }
70
71 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
72 char *t;
73
74 /* If we are UID 0 (root), this will not result in NSS,
75 * otherwise it might. This is good, as we want to be able to
76 * run this in PID 1, where our user ID is 0, but where NSS
77 * lookups are not allowed. */
78
79 t = getusername_malloc();
80 if (!t)
81 return -ENOMEM;
82
83 *ret = t;
84 return 0;
85 }
86
87 static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) {
88
89 if (asprintf(ret, UID_FMT, getuid()) < 0)
90 return -ENOMEM;
91
92 return 0;
93 }
94
95 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
96
97 /* This is similar to unit_full_printf() but does not support
98 * anything path-related.
99 *
100 * %n: the full id of the unit (foo@bar.waldo)
101 * %N: the id of the unit without the suffix (foo@bar)
102 * %p: the prefix (foo)
103 * %i: the instance (bar)
104
105 * %U the UID of the running user
106 * %u the username of running user
107 * %m the machine ID of the running system
108 * %H the host name of the running system
109 * %b the boot ID of the running system
110 * %v `uname -r` of the running system
111 */
112
113 const Specifier table[] = {
114 { 'n', specifier_string, i->name },
115 { 'N', specifier_prefix_and_instance, NULL },
116 { 'p', specifier_prefix, NULL },
117 { 'i', specifier_instance, NULL },
118
119 { 'U', specifier_user_id, NULL },
120 { 'u', specifier_user_name, NULL },
121
122 { 'm', specifier_machine_id, NULL },
123 { 'H', specifier_host_name, NULL },
124 { 'b', specifier_boot_id, NULL },
125 { 'v', specifier_kernel_release, NULL },
126 {}
127 };
128
129 assert(i);
130 assert(format);
131 assert(ret);
132
133 return specifier_printf(format, table, i, ret);
134 }