chiark / gitweb /
Fix service file to match installed elogind binary location
[elogind.git] / src / basic / proc-cmdline.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
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
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <string.h>
23
24 #include "alloc-util.h"
25 #include "extract-word.h"
26 #include "fileio.h"
27 #include "macro.h"
28 #include "parse-util.h"
29 #include "proc-cmdline.h"
30 #include "process-util.h"
31 //#include "special.h"
32 #include "string-util.h"
33 #include "util.h"
34 #include "virt.h"
35
36 int proc_cmdline(char **ret) {
37         const char *e;
38         assert(ret);
39
40         /* For testing purposes it is sometimes useful to be able to override what we consider /proc/cmdline to be */
41         e = secure_getenv("SYSTEMD_PROC_CMDLINE");
42         if (e) {
43                 char *m;
44
45                 m = strdup(e);
46                 if (!m)
47                         return -ENOMEM;
48
49                 *ret = m;
50                 return 0;
51         }
52
53         if (detect_container() > 0)
54                 return get_process_cmdline(1, 0, false, ret);
55         else
56                 return read_one_line_file("/proc/cmdline", ret);
57 }
58
59 int proc_cmdline_parse(proc_cmdline_parse_t parse_item, void *data, unsigned flags) {
60
61         _cleanup_free_ char *line = NULL;
62         const char *p;
63         int r;
64
65         assert(parse_item);
66
67         r = proc_cmdline(&line);
68         if (r < 0)
69                 return r;
70
71         p = line;
72         for (;;) {
73                 _cleanup_free_ char *word = NULL;
74                 char *value, *key, *q;
75
76                 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
77                 if (r < 0)
78                         return r;
79                 if (r == 0)
80                         break;
81
82                 key = word;
83
84                 /* Filter out arguments that are intended only for the initrd */
85                 q = startswith(word, "rd.");
86                 if (q) {
87                         if (!in_initrd())
88                                 continue;
89
90                         if (flags & PROC_CMDLINE_STRIP_RD_PREFIX)
91                                 key = q;
92                 }
93
94                 value = strchr(key, '=');
95                 if (value)
96                         *(value++) = 0;
97
98                 r = parse_item(key, value, data);
99                 if (r < 0)
100                         return r;
101         }
102
103         return 0;
104 }
105
106 static bool relaxed_equal_char(char a, char b) {
107
108         return a == b ||
109                 (a == '_' && b == '-') ||
110                 (a == '-' && b == '_');
111 }
112
113 #if 0 /// UNNEEDED by elogind
114 char *proc_cmdline_key_startswith(const char *s, const char *prefix) {
115
116         assert(s);
117         assert(prefix);
118
119         /* Much like startswith(), but considers "-" and "_" the same */
120
121         for (; *prefix != 0; s++, prefix++)
122                 if (!relaxed_equal_char(*s, *prefix))
123                         return NULL;
124
125         return (char*) s;
126 }
127 #endif // 0
128
129 bool proc_cmdline_key_streq(const char *x, const char *y) {
130         assert(x);
131         assert(y);
132
133         /* Much like streq(), but considers "-" and "_" the same */
134
135         for (; *x != 0 || *y != 0; x++, y++)
136                 if (!relaxed_equal_char(*x, *y))
137                         return false;
138
139         return true;
140 }
141
142 #if 0 /// UNNEEDED by elogind
143 int proc_cmdline_get_key(const char *key, unsigned flags, char **value) {
144         _cleanup_free_ char *line = NULL, *ret = NULL;
145         bool found = false;
146         const char *p;
147         int r;
148
149         /* Looks for a specific key on the kernel command line. Supports two modes:
150          *
151          * a) The "value" parameter is used. In this case a parameter beginning with the "key" string followed by "="
152          *    is searched, and the value following this is returned in "value".
153          *
154          * b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a
155          *    separate word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then
156          *    this is also accepted, and "value" is returned as NULL.
157          *
158          * c) The "value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
159          *
160          * In all three cases, > 0 is returned if the key is found, 0 if not. */
161
162         if (isempty(key))
163                 return -EINVAL;
164
165         if ((flags & PROC_CMDLINE_VALUE_OPTIONAL) && !value)
166                 return -EINVAL;
167
168         r = proc_cmdline(&line);
169         if (r < 0)
170                 return r;
171
172         p = line;
173         for (;;) {
174                 _cleanup_free_ char *word = NULL;
175                 const char *e;
176
177                 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
178                 if (r < 0)
179                         return r;
180                 if (r == 0)
181                         break;
182
183                 /* Automatically filter out arguments that are intended only for the initrd, if we are not in the
184                  * initrd. */
185                 if (!in_initrd() && startswith(word, "rd."))
186                         continue;
187
188                 if (value) {
189                         e = proc_cmdline_key_startswith(word, key);
190                         if (!e)
191                                 continue;
192
193                         if (*e == '=') {
194                                 r = free_and_strdup(&ret, e+1);
195                                 if (r < 0)
196                                         return r;
197
198                                 found = true;
199
200                         } else if (*e == 0 && (flags & PROC_CMDLINE_VALUE_OPTIONAL))
201                                 found = true;
202
203                 } else {
204                         if (streq(word, key))
205                                 found = true;
206                 }
207         }
208
209         if (value) {
210                 *value = ret;
211                 ret = NULL;
212         }
213
214         return found;
215 }
216
217 int proc_cmdline_get_bool(const char *key, bool *ret) {
218         _cleanup_free_ char *v = NULL;
219         int r;
220
221         assert(ret);
222
223         r = proc_cmdline_get_key(key, PROC_CMDLINE_VALUE_OPTIONAL, &v);
224         if (r < 0)
225                 return r;
226         if (r == 0) {
227                 *ret = false;
228                 return 0;
229         }
230
231         if (v) { /* parameter passed */
232                 r = parse_boolean(v);
233                 if (r < 0)
234                         return r;
235                 *ret = r;
236         } else /* no parameter passed */
237                 *ret = true;
238
239         return 1;
240 }
241
242 int shall_restore_state(void) {
243         bool ret;
244         int r;
245
246         r = proc_cmdline_get_bool("systemd.restore_state", &ret);
247         if (r < 0)
248                 return r;
249
250         return r > 0 ? ret : true;
251 }
252
253 static const char * const rlmap[] = {
254         "emergency", SPECIAL_EMERGENCY_TARGET,
255         "-b",        SPECIAL_EMERGENCY_TARGET,
256         "rescue",    SPECIAL_RESCUE_TARGET,
257         "single",    SPECIAL_RESCUE_TARGET,
258         "-s",        SPECIAL_RESCUE_TARGET,
259         "s",         SPECIAL_RESCUE_TARGET,
260         "S",         SPECIAL_RESCUE_TARGET,
261         "1",         SPECIAL_RESCUE_TARGET,
262         "2",         SPECIAL_MULTI_USER_TARGET,
263         "3",         SPECIAL_MULTI_USER_TARGET,
264         "4",         SPECIAL_MULTI_USER_TARGET,
265         "5",         SPECIAL_GRAPHICAL_TARGET,
266         NULL
267 };
268
269 static const char * const rlmap_initrd[] = {
270         "emergency", SPECIAL_EMERGENCY_TARGET,
271         "rescue",    SPECIAL_RESCUE_TARGET,
272         NULL
273 };
274
275 const char* runlevel_to_target(const char *word) {
276         size_t i;
277         const char * const *rlmap_ptr = in_initrd() ? rlmap_initrd
278                                                     : rlmap;
279
280         if (!word)
281                 return NULL;
282
283         if (in_initrd() && (word = startswith(word, "rd.")) == NULL)
284                 return NULL;
285
286         for (i = 0; rlmap_ptr[i] != NULL; i += 2)
287                 if (streq(word, rlmap_ptr[i]))
288                         return rlmap_ptr[i+1];
289
290         return NULL;
291 }
292 #endif // 0