]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
man: document what SIGUSR1 and SIGUSR2 do to resolved
[thirdparty/systemd.git] / src / shared / install-printf.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "formats-util.h"
26 #include "install-printf.h"
27 #include "install.h"
28 #include "macro.h"
29 #include "specifier.h"
30 #include "unit-name.h"
31 #include "user-util.h"
32
33 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
34 UnitFileInstallInfo *i = userdata;
35
36 assert(i);
37
38 return unit_name_to_prefix_and_instance(i->name, ret);
39 }
40
41 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
42 UnitFileInstallInfo *i = userdata;
43
44 assert(i);
45
46 return unit_name_to_prefix(i->name, ret);
47 }
48
49 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
50 UnitFileInstallInfo *i = userdata;
51 char *instance;
52 int r;
53
54 assert(i);
55
56 r = unit_name_to_instance(i->name, &instance);
57 if (r < 0)
58 return r;
59
60 if (!instance) {
61 instance = strdup("");
62 if (!instance)
63 return -ENOMEM;
64 }
65
66 *ret = instance;
67 return 0;
68 }
69
70 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
71 char *t;
72
73 /* If we are UID 0 (root), this will not result in NSS,
74 * otherwise it might. This is good, as we want to be able to
75 * run this in PID 1, where our user ID is 0, but where NSS
76 * lookups are not allowed. */
77
78 t = getusername_malloc();
79 if (!t)
80 return -ENOMEM;
81
82 *ret = t;
83 return 0;
84 }
85
86 static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) {
87
88 if (asprintf(ret, UID_FMT, getuid()) < 0)
89 return -ENOMEM;
90
91 return 0;
92 }
93
94 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
95
96 /* This is similar to unit_full_printf() but does not support
97 * anything path-related.
98 *
99 * %n: the full id of the unit (foo@bar.waldo)
100 * %N: the id of the unit without the suffix (foo@bar)
101 * %p: the prefix (foo)
102 * %i: the instance (bar)
103
104 * %U the UID of the running user
105 * %u the username of running user
106 * %m the machine ID of the running system
107 * %H the host name of the running system
108 * %b the boot ID of the running system
109 * %v `uname -r` of the running system
110 */
111
112 const Specifier table[] = {
113 { 'n', specifier_string, i->name },
114 { 'N', specifier_prefix_and_instance, NULL },
115 { 'p', specifier_prefix, NULL },
116 { 'i', specifier_instance, NULL },
117
118 { 'U', specifier_user_id, NULL },
119 { 'u', specifier_user_name, NULL },
120
121 { 'm', specifier_machine_id, NULL },
122 { 'H', specifier_host_name, NULL },
123 { 'b', specifier_boot_id, NULL },
124 { 'v', specifier_kernel_release, NULL },
125 {}
126 };
127
128 assert(i);
129 assert(format);
130 assert(ret);
131
132 return specifier_printf(format, table, i, ret);
133 }