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