]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
Merge pull request #8676 from keszybz/drop-license-boilerplate
[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
8 #include <errno.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include "format-util.h"
14 #include "install-printf.h"
15 #include "install.h"
16 #include "macro.h"
17 #include "specifier.h"
18 #include "string-util.h"
19 #include "unit-name.h"
20 #include "user-util.h"
21
22 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
23 const UnitFileInstallInfo *i = userdata;
24 _cleanup_free_ char *prefix = NULL;
25 int r;
26
27 assert(i);
28
29 r = unit_name_to_prefix_and_instance(i->name, &prefix);
30 if (r < 0)
31 return r;
32
33 if (endswith(prefix, "@") && i->default_instance) {
34 char *ans;
35
36 ans = strjoin(prefix, i->default_instance);
37 if (!ans)
38 return -ENOMEM;
39 *ret = ans;
40 } else
41 *ret = TAKE_PTR(prefix);
42
43 return 0;
44 }
45
46 static int specifier_name(char specifier, void *data, void *userdata, char **ret) {
47 const UnitFileInstallInfo *i = userdata;
48 char *ans;
49
50 assert(i);
51
52 if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance)
53 return unit_name_replace_instance(i->name, i->default_instance, ret);
54
55 ans = strdup(i->name);
56 if (!ans)
57 return -ENOMEM;
58 *ret = ans;
59 return 0;
60 }
61
62 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
63 const UnitFileInstallInfo *i = userdata;
64
65 assert(i);
66
67 return unit_name_to_prefix(i->name, ret);
68 }
69
70 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
71 const UnitFileInstallInfo *i = userdata;
72 char *instance;
73 int r;
74
75 assert(i);
76
77 r = unit_name_to_instance(i->name, &instance);
78 if (r < 0)
79 return r;
80
81 if (isempty(instance)) {
82 r = free_and_strdup(&instance, strempty(i->default_instance));
83 if (r < 0)
84 return r;
85 }
86
87 *ret = instance;
88 return 0;
89 }
90
91 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
92
93 /* This is similar to unit_full_printf() but does not support
94 * anything path-related.
95 *
96 * %n: the full id of the unit (foo@bar.waldo)
97 * %N: the id of the unit without the suffix (foo@bar)
98 * %p: the prefix (foo)
99 * %i: the instance (bar)
100
101 * %U the UID of the running user
102 * %u the username of running user
103 * %m the machine ID of the running system
104 * %H the host name of the running system
105 * %b the boot ID of the running system
106 * %v `uname -r` of the running system
107 */
108
109 const Specifier table[] = {
110 { 'n', specifier_name, NULL },
111 { 'N', specifier_prefix_and_instance, NULL },
112 { 'p', specifier_prefix, NULL },
113 { 'i', specifier_instance, NULL },
114
115 { 'U', specifier_user_id, NULL },
116 { 'u', specifier_user_name, NULL },
117
118 { 'm', specifier_machine_id, NULL },
119 { 'H', specifier_host_name, NULL },
120 { 'b', specifier_boot_id, NULL },
121 { 'v', specifier_kernel_release, NULL },
122 {}
123 };
124
125 assert(i);
126 assert(format);
127 assert(ret);
128
129 return specifier_printf(format, table, i, ret);
130 }