]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/install-printf.c
core: let's upgrade the log level for service processes dying of signal (#4415)
[thirdparty/systemd.git] / src / shared / install-printf.c
CommitLineData
7584d236
ZJS
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
a8fbdf54
TA
20#include <errno.h>
21#include <stdio.h>
22#include <string.h>
23#include <unistd.h>
7584d236 24
b1d4f8e1
LP
25#include "formats-util.h"
26#include "install-printf.h"
a8fbdf54
TA
27#include "install.h"
28#include "macro.h"
7584d236
ZJS
29#include "specifier.h"
30#include "unit-name.h"
b1d4f8e1 31#include "user-util.h"
7584d236 32
19f6d710 33static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
cab6235f 34 UnitFileInstallInfo *i = userdata;
19f6d710 35
7584d236
ZJS
36 assert(i);
37
7410616c 38 return unit_name_to_prefix_and_instance(i->name, ret);
7584d236
ZJS
39}
40
19f6d710 41static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
cab6235f 42 UnitFileInstallInfo *i = userdata;
19f6d710 43
7584d236
ZJS
44 assert(i);
45
7410616c 46 return unit_name_to_prefix(i->name, ret);
7584d236
ZJS
47}
48
19f6d710 49static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
cab6235f 50 UnitFileInstallInfo *i = userdata;
7584d236
ZJS
51 char *instance;
52 int r;
53
54 assert(i);
55
56 r = unit_name_to_instance(i->name, &instance);
57 if (r < 0)
19f6d710
LP
58 return r;
59
60 if (!instance) {
61 instance = strdup("");
62 if (!instance)
63 return -ENOMEM;
64 }
65
66 *ret = instance;
67 return 0;
7584d236
ZJS
68}
69
19f6d710 70static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
79413b67 71 char *t;
3f0b2f0f 72
79413b67
LP
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. */
3f0b2f0f 77
79413b67
LP
78 t = getusername_malloc();
79 if (!t)
80 return -ENOMEM;
19f6d710 81
79413b67 82 *ret = t;
19f6d710 83 return 0;
3f0b2f0f
ZJS
84}
85
79413b67
LP
86static 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}
3f0b2f0f 93
cab6235f 94int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
7584d236
ZJS
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
79413b67
LP
104 * %U the UID of the running user
105 * %u the username of running user
7584d236
ZJS
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
6aaa8c2f 109 * %v `uname -r` of the running system
7584d236
ZJS
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
79413b67 118 { 'U', specifier_user_id, NULL },
3f0b2f0f 119 { 'u', specifier_user_name, NULL },
7584d236
ZJS
120
121 { 'm', specifier_machine_id, NULL },
122 { 'H', specifier_host_name, NULL },
123 { 'b', specifier_boot_id, NULL },
6aaa8c2f
ZJS
124 { 'v', specifier_kernel_release, NULL },
125 {}
7584d236
ZJS
126 };
127
128 assert(i);
129 assert(format);
19f6d710 130 assert(ret);
7584d236 131
19f6d710 132 return specifier_printf(format, table, i, ret);
7584d236 133}