]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
5135d0671e50ac5b7ee19cd4256270fd7e521afa
[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 = TAKE_PTR(prefix);
55
56 return 0;
57 }
58
59 static int specifier_name(char specifier, void *data, void *userdata, char **ret) {
60 const UnitFileInstallInfo *i = userdata;
61 char *ans;
62
63 assert(i);
64
65 if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance)
66 return unit_name_replace_instance(i->name, i->default_instance, ret);
67
68 ans = strdup(i->name);
69 if (!ans)
70 return -ENOMEM;
71 *ret = ans;
72 return 0;
73 }
74
75 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
76 const UnitFileInstallInfo *i = userdata;
77
78 assert(i);
79
80 return unit_name_to_prefix(i->name, ret);
81 }
82
83 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
84 const UnitFileInstallInfo *i = userdata;
85 char *instance;
86 int r;
87
88 assert(i);
89
90 r = unit_name_to_instance(i->name, &instance);
91 if (r < 0)
92 return r;
93
94 if (isempty(instance)) {
95 r = free_and_strdup(&instance, strempty(i->default_instance));
96 if (r < 0)
97 return r;
98 }
99
100 *ret = instance;
101 return 0;
102 }
103
104 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
105
106 /* This is similar to unit_full_printf() but does not support
107 * anything path-related.
108 *
109 * %n: the full id of the unit (foo@bar.waldo)
110 * %N: the id of the unit without the suffix (foo@bar)
111 * %p: the prefix (foo)
112 * %i: the instance (bar)
113
114 * %U the UID of the running user
115 * %u the username of running user
116 * %m the machine ID of the running system
117 * %H the host name of the running system
118 * %b the boot ID of the running system
119 * %v `uname -r` of the running system
120 */
121
122 const Specifier table[] = {
123 { 'n', specifier_name, NULL },
124 { 'N', specifier_prefix_and_instance, NULL },
125 { 'p', specifier_prefix, NULL },
126 { 'i', specifier_instance, NULL },
127
128 { 'U', specifier_user_id, NULL },
129 { 'u', specifier_user_name, NULL },
130
131 { 'm', specifier_machine_id, NULL },
132 { 'H', specifier_host_name, NULL },
133 { 'b', specifier_boot_id, NULL },
134 { 'v', specifier_kernel_release, NULL },
135 {}
136 };
137
138 assert(i);
139 assert(format);
140 assert(ret);
141
142 return specifier_printf(format, table, i, ret);
143 }