]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
Merge pull request #7444 from poettering/dbus-no-spec
[thirdparty/systemd.git] / src / shared / install-printf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "format-util.h"
27 #include "install-printf.h"
28 #include "install.h"
29 #include "macro.h"
30 #include "specifier.h"
31 #include "string-util.h"
32 #include "unit-name.h"
33 #include "user-util.h"
34
35 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
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
49 ans = strjoin(prefix, i->default_instance);
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;
64
65 assert(i);
66
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;
75 }
76
77 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
78 const UnitFileInstallInfo *i = userdata;
79
80 assert(i);
81
82 return unit_name_to_prefix(i->name, ret);
83 }
84
85 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
86 const UnitFileInstallInfo *i = userdata;
87 char *instance;
88 int r;
89
90 assert(i);
91
92 r = unit_name_to_instance(i->name, &instance);
93 if (r < 0)
94 return r;
95
96 if (isempty(instance)) {
97 r = free_and_strdup(&instance, strempty(i->default_instance));
98 if (r < 0)
99 return r;
100 }
101
102 *ret = instance;
103 return 0;
104 }
105
106 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
107
108 /* This is similar to unit_full_printf() but does not support
109 * anything path-related.
110 *
111 * %n: the full id of the unit (foo@bar.waldo)
112 * %N: the id of the unit without the suffix (foo@bar)
113 * %p: the prefix (foo)
114 * %i: the instance (bar)
115
116 * %U the UID of the running user
117 * %u the username of running user
118 * %m the machine ID of the running system
119 * %H the host name of the running system
120 * %b the boot ID of the running system
121 * %v `uname -r` of the running system
122 */
123
124 const Specifier table[] = {
125 { 'n', specifier_name, NULL },
126 { 'N', specifier_prefix_and_instance, NULL },
127 { 'p', specifier_prefix, NULL },
128 { 'i', specifier_instance, NULL },
129
130 { 'U', specifier_user_id, NULL },
131 { 'u', specifier_user_name, NULL },
132
133 { 'm', specifier_machine_id, NULL },
134 { 'H', specifier_host_name, NULL },
135 { 'b', specifier_boot_id, NULL },
136 { 'v', specifier_kernel_release, NULL },
137 {}
138 };
139
140 assert(i);
141 assert(format);
142 assert(ret);
143
144 return specifier_printf(format, table, i, ret);
145 }