]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
util-lib: split out allocation calls into alloc-util.[ch]
[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 UnitFileInstallInfo *i = userdata;
71 const char *username;
72 _cleanup_free_ char *tmp = NULL;
73 char *printed = NULL;
74
75 assert(i);
76
77 if (i->user)
78 username = i->user;
79 else
80 /* get USER env from env or our own uid */
81 username = tmp = getusername_malloc();
82
83 switch (specifier) {
84 case 'u':
85 printed = strdup(username);
86 break;
87 case 'U': {
88 /* fish username from passwd */
89 uid_t uid;
90 int r;
91
92 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
93 if (r < 0)
94 return r;
95
96 if (asprintf(&printed, UID_FMT, uid) < 0)
97 return -ENOMEM;
98 break;
99 }}
100
101
102 *ret = printed;
103 return 0;
104 }
105
106
107 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
108
109 /* This is similar to unit_full_printf() but does not support
110 * anything path-related.
111 *
112 * %n: the full id of the unit (foo@bar.waldo)
113 * %N: the id of the unit without the suffix (foo@bar)
114 * %p: the prefix (foo)
115 * %i: the instance (bar)
116
117 * %U the UID of the configured user or running user
118 * %u the username of the configured user or running user
119 * %m the machine ID of the running system
120 * %H the host name of the running system
121 * %b the boot ID of the running system
122 * %v `uname -r` of the running system
123 */
124
125 const Specifier table[] = {
126 { 'n', specifier_string, i->name },
127 { 'N', specifier_prefix_and_instance, NULL },
128 { 'p', specifier_prefix, NULL },
129 { 'i', specifier_instance, NULL },
130
131 { 'U', specifier_user_name, NULL },
132 { 'u', specifier_user_name, NULL },
133
134 { 'm', specifier_machine_id, NULL },
135 { 'H', specifier_host_name, NULL },
136 { 'b', specifier_boot_id, NULL },
137 { 'v', specifier_kernel_release, NULL },
138 {}
139 };
140
141 assert(i);
142 assert(format);
143 assert(ret);
144
145 return specifier_printf(format, table, i, ret);
146 }