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