]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/install-printf.c
util-lib: split out user/group/uid/gid calls into user-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 "formats-util.h"
25 #include "install-printf.h"
26 #include "specifier.h"
27 #include "unit-name.h"
28 #include "user-util.h"
29 #include "util.h"
30
31 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
32 UnitFileInstallInfo *i = userdata;
33
34 assert(i);
35
36 return unit_name_to_prefix_and_instance(i->name, ret);
37 }
38
39 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
40 UnitFileInstallInfo *i = userdata;
41
42 assert(i);
43
44 return unit_name_to_prefix(i->name, ret);
45 }
46
47 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
48 UnitFileInstallInfo *i = userdata;
49 char *instance;
50 int r;
51
52 assert(i);
53
54 r = unit_name_to_instance(i->name, &instance);
55 if (r < 0)
56 return r;
57
58 if (!instance) {
59 instance = strdup("");
60 if (!instance)
61 return -ENOMEM;
62 }
63
64 *ret = instance;
65 return 0;
66 }
67
68 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
69 UnitFileInstallInfo *i = userdata;
70 const char *username;
71 _cleanup_free_ char *tmp = NULL;
72 char *printed = NULL;
73
74 assert(i);
75
76 if (i->user)
77 username = i->user;
78 else
79 /* get USER env from env or our own uid */
80 username = tmp = getusername_malloc();
81
82 switch (specifier) {
83 case 'u':
84 printed = strdup(username);
85 break;
86 case 'U': {
87 /* fish username from passwd */
88 uid_t uid;
89 int r;
90
91 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
92 if (r < 0)
93 return r;
94
95 if (asprintf(&printed, UID_FMT, uid) < 0)
96 return -ENOMEM;
97 break;
98 }}
99
100
101 *ret = printed;
102 return 0;
103 }
104
105
106 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
107
108 /* This is similar to unit_full_printf() but does not support
109 * anything path-related.
110 *
111 * %n: the full id of the unit (foo@bar.waldo)
112 * %N: the id of the unit without the suffix (foo@bar)
113 * %p: the prefix (foo)
114 * %i: the instance (bar)
115
116 * %U the UID of the configured user or running user
117 * %u the username of the configured user or running user
118 * %m the machine ID of the running system
119 * %H the host name of the running system
120 * %b the boot ID of the running system
121 * %v `uname -r` of the running system
122 */
123
124 const Specifier table[] = {
125 { 'n', specifier_string, i->name },
126 { 'N', specifier_prefix_and_instance, NULL },
127 { 'p', specifier_prefix, NULL },
128 { 'i', specifier_instance, NULL },
129
130 { 'U', specifier_user_name, NULL },
131 { 'u', specifier_user_name, NULL },
132
133 { 'm', specifier_machine_id, NULL },
134 { 'H', specifier_host_name, NULL },
135 { 'b', specifier_boot_id, NULL },
136 { 'v', specifier_kernel_release, NULL },
137 {}
138 };
139
140 assert(i);
141 assert(format);
142 assert(ret);
143
144 return specifier_printf(format, table, i, ret);
145 }