chiark / gitweb /
5f8fb4aa1710ed08d4ba440da5e4db758b8d1b5c
[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 #if 0 /// UNNEEDED by elogind
136 int shall_restore_state(void) {
137         _cleanup_free_ char *value = NULL;
138         int r;
139
140         r = get_proc_cmdline_key("systemd.restore_state=", &value);
141         if (r < 0)
142                 return r;
143         if (r == 0)
144                 return true;
145
146         return parse_boolean(value);
147 }
148
149 static const char * const rlmap[] = {
150         "emergency", SPECIAL_EMERGENCY_TARGET,
151         "-b",        SPECIAL_EMERGENCY_TARGET,
152         "rescue",    SPECIAL_RESCUE_TARGET,
153         "single",    SPECIAL_RESCUE_TARGET,
154         "-s",        SPECIAL_RESCUE_TARGET,
155         "s",         SPECIAL_RESCUE_TARGET,
156         "S",         SPECIAL_RESCUE_TARGET,
157         "1",         SPECIAL_RESCUE_TARGET,
158         "2",         SPECIAL_MULTI_USER_TARGET,
159         "3",         SPECIAL_MULTI_USER_TARGET,
160         "4",         SPECIAL_MULTI_USER_TARGET,
161         "5",         SPECIAL_GRAPHICAL_TARGET,
162 };
163
164 const char* runlevel_to_target(const char *word) {
165         size_t i;
166
167         if (!word)
168                 return NULL;
169
170         for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
171                 if (streq(word, rlmap[i]))
172                         return rlmap[i+1];
173
174         return NULL;
175 }
176 #endif // 0