]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/install-printf.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / shared / install-printf.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7584d236 2
a8fbdf54
TA
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
7584d236 7
f97b34a6 8#include "format-util.h"
b1d4f8e1 9#include "install-printf.h"
a8fbdf54
TA
10#include "install.h"
11#include "macro.h"
7584d236 12#include "specifier.h"
6e3136b2 13#include "string-util.h"
7584d236 14#include "unit-name.h"
b1d4f8e1 15#include "user-util.h"
7584d236 16
303ee601 17static int specifier_prefix_and_instance(char specifier, const void *data, const void *userdata, char **ret) {
cfd559c9
ZJS
18 const UnitFileInstallInfo *i = userdata;
19 _cleanup_free_ char *prefix = NULL;
20 int r;
21
22 assert(i);
23
24 r = unit_name_to_prefix_and_instance(i->name, &prefix);
25 if (r < 0)
26 return r;
27
28 if (endswith(prefix, "@") && i->default_instance) {
29 char *ans;
30
605405c6 31 ans = strjoin(prefix, i->default_instance);
cfd559c9
ZJS
32 if (!ans)
33 return -ENOMEM;
34 *ret = ans;
ae2a15bc
LP
35 } else
36 *ret = TAKE_PTR(prefix);
cfd559c9
ZJS
37
38 return 0;
39}
40
303ee601 41static int specifier_name(char specifier, const void *data, const void *userdata, char **ret) {
cfd559c9
ZJS
42 const UnitFileInstallInfo *i = userdata;
43 char *ans;
19f6d710 44
7584d236
ZJS
45 assert(i);
46
cfd559c9
ZJS
47 if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance)
48 return unit_name_replace_instance(i->name, i->default_instance, ret);
49
50 ans = strdup(i->name);
51 if (!ans)
52 return -ENOMEM;
53 *ret = ans;
54 return 0;
7584d236
ZJS
55}
56
303ee601 57static int specifier_prefix(char specifier, const void *data, const void *userdata, char **ret) {
cfd559c9 58 const UnitFileInstallInfo *i = userdata;
19f6d710 59
7584d236
ZJS
60 assert(i);
61
7410616c 62 return unit_name_to_prefix(i->name, ret);
7584d236
ZJS
63}
64
303ee601 65static int specifier_instance(char specifier, const void *data, const void *userdata, char **ret) {
6e3136b2 66 const UnitFileInstallInfo *i = userdata;
7584d236
ZJS
67 char *instance;
68 int r;
69
70 assert(i);
71
72 r = unit_name_to_instance(i->name, &instance);
73 if (r < 0)
19f6d710
LP
74 return r;
75
6e3136b2 76 if (isempty(instance)) {
87e4e28d 77 r = free_and_strdup(&instance, strempty(i->default_instance));
402a81c7
ZJS
78 if (r < 0)
79 return r;
19f6d710
LP
80 }
81
82 *ret = instance;
83 return 0;
7584d236
ZJS
84}
85
303ee601 86static int specifier_last_component(char specifier, const void *data, const void *userdata, char **ret) {
250e9fad
ZJS
87 _cleanup_free_ char *prefix = NULL;
88 char *dash;
89 int r;
90
91 r = specifier_prefix(specifier, data, userdata, &prefix);
92 if (r < 0)
93 return r;
94
95 dash = strrchr(prefix, '-');
96 if (dash) {
97 dash = strdup(dash + 1);
98 if (!dash)
99 return -ENOMEM;
100 *ret = dash;
101 } else
102 *ret = TAKE_PTR(prefix);
103
104 return 0;
105}
106
303ee601 107int install_full_printf(const UnitFileInstallInfo *i, const char *format, char **ret) {
7584d236
ZJS
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 },
250e9fad 129 { 'j', specifier_last_component, NULL },
7584d236 130
b75f0c69
DC
131 { 'g', specifier_group_name, NULL },
132 { 'G', specifier_group_id, NULL },
79413b67 133 { 'U', specifier_user_id, NULL },
3f0b2f0f 134 { 'u', specifier_user_name, NULL },
7584d236
ZJS
135
136 { 'm', specifier_machine_id, NULL },
137 { 'H', specifier_host_name, NULL },
138 { 'b', specifier_boot_id, NULL },
6aaa8c2f
ZJS
139 { 'v', specifier_kernel_release, NULL },
140 {}
7584d236
ZJS
141 };
142
143 assert(i);
144 assert(format);
19f6d710 145 assert(ret);
7584d236 146
19f6d710 147 return specifier_printf(format, table, i, ret);
7584d236 148}