]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/proc-cmdline.c
Merge pull request #1359 from jengelh/ue
[thirdparty/systemd.git] / src / basic / proc-cmdline.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
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 <stdbool.h>
23 #include <stddef.h>
24 #include <string.h>
25
26 #include "alloc-util.h"
27 #include "extract-word.h"
28 #include "fileio.h"
29 #include "macro.h"
30 #include "parse-util.h"
31 #include "proc-cmdline.h"
32 #include "process-util.h"
33 #include "special.h"
34 #include "string-util.h"
35 #include "util.h"
36 #include "virt.h"
37
38 int proc_cmdline(char **ret) {
39 assert(ret);
40
41 if (detect_container() > 0)
42 return get_process_cmdline(1, 0, false, ret);
43 else
44 return read_one_line_file("/proc/cmdline", ret);
45 }
46
47 int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value)) {
48 _cleanup_free_ char *line = NULL;
49 const char *p;
50 int r;
51
52 assert(parse_item);
53
54 r = proc_cmdline(&line);
55 if (r < 0)
56 return r;
57
58 p = line;
59 for (;;) {
60 _cleanup_free_ char *word = NULL;
61 char *value = NULL;
62
63 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
64 if (r < 0)
65 return r;
66 if (r == 0)
67 break;
68
69 /* Filter out arguments that are intended only for the
70 * initrd */
71 if (!in_initrd() && startswith(word, "rd."))
72 continue;
73
74 value = strchr(word, '=');
75 if (value)
76 *(value++) = 0;
77
78 r = parse_item(word, value);
79 if (r < 0)
80 return r;
81 }
82
83 return 0;
84 }
85
86 int get_proc_cmdline_key(const char *key, char **value) {
87 _cleanup_free_ char *line = NULL, *ret = NULL;
88 bool found = false;
89 const char *p;
90 int r;
91
92 assert(key);
93
94 r = proc_cmdline(&line);
95 if (r < 0)
96 return r;
97
98 p = line;
99 for (;;) {
100 _cleanup_free_ char *word = NULL;
101 const char *e;
102
103 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
104 if (r < 0)
105 return r;
106 if (r == 0)
107 break;
108
109 /* Filter out arguments that are intended only for the
110 * initrd */
111 if (!in_initrd() && startswith(word, "rd."))
112 continue;
113
114 if (value) {
115 e = startswith(word, key);
116 if (!e)
117 continue;
118
119 r = free_and_strdup(&ret, e);
120 if (r < 0)
121 return r;
122
123 found = true;
124 } else {
125 if (streq(word, key))
126 found = true;
127 }
128 }
129
130 if (value) {
131 *value = ret;
132 ret = NULL;
133 }
134
135 return found;
136
137 }
138
139 int shall_restore_state(void) {
140 _cleanup_free_ char *value = NULL;
141 int r;
142
143 r = get_proc_cmdline_key("systemd.restore_state=", &value);
144 if (r < 0)
145 return r;
146 if (r == 0)
147 return true;
148
149 return parse_boolean(value);
150 }
151
152 static const char * const rlmap[] = {
153 "emergency", SPECIAL_EMERGENCY_TARGET,
154 "-b", SPECIAL_EMERGENCY_TARGET,
155 "rescue", SPECIAL_RESCUE_TARGET,
156 "single", SPECIAL_RESCUE_TARGET,
157 "-s", SPECIAL_RESCUE_TARGET,
158 "s", SPECIAL_RESCUE_TARGET,
159 "S", SPECIAL_RESCUE_TARGET,
160 "1", SPECIAL_RESCUE_TARGET,
161 "2", SPECIAL_MULTI_USER_TARGET,
162 "3", SPECIAL_MULTI_USER_TARGET,
163 "4", SPECIAL_MULTI_USER_TARGET,
164 "5", SPECIAL_GRAPHICAL_TARGET,
165 };
166
167 const char* runlevel_to_target(const char *word) {
168 size_t i;
169
170 if (!word)
171 return NULL;
172
173 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
174 if (streq(word, rlmap[i]))
175 return rlmap[i+1];
176
177 return NULL;
178 }