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