]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/install-printf.c
bootspec: fix debug message about default entry
[thirdparty/systemd.git] / src / shared / install-printf.c
CommitLineData
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 35static 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
61static 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 77static 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 85static 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)) {
87e4e28d 97 r = free_and_strdup(&instance, strempty(i->default_instance));
402a81c7
ZJS
98 if (r < 0)
99 return r;
19f6d710
LP
100 }
101
102 *ret = instance;
103 return 0;
7584d236
ZJS
104}
105
cab6235f 106int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
7584d236
ZJS
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
79413b67
LP
116 * %U the UID of the running user
117 * %u the username of running user
7584d236
ZJS
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
6aaa8c2f 121 * %v `uname -r` of the running system
7584d236
ZJS
122 */
123
124 const Specifier table[] = {
cfd559c9 125 { 'n', specifier_name, NULL },
7584d236
ZJS
126 { 'N', specifier_prefix_and_instance, NULL },
127 { 'p', specifier_prefix, NULL },
128 { 'i', specifier_instance, NULL },
129
79413b67 130 { 'U', specifier_user_id, NULL },
3f0b2f0f 131 { 'u', specifier_user_name, NULL },
7584d236
ZJS
132
133 { 'm', specifier_machine_id, NULL },
134 { 'H', specifier_host_name, NULL },
135 { 'b', specifier_boot_id, NULL },
6aaa8c2f
ZJS
136 { 'v', specifier_kernel_release, NULL },
137 {}
7584d236
ZJS
138 };
139
140 assert(i);
141 assert(format);
19f6d710 142 assert(ret);
7584d236 143
19f6d710 144 return specifier_printf(format, table, i, ret);
7584d236 145}