]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/proc-cmdline.c
util-lib: split out allocation calls into alloc-util.[ch]
[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 "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 "string-util.h"
30 #include "util.h"
31 #include "virt.h"
32
33 int proc_cmdline(char **ret) {
34 assert(ret);
35
36 if (detect_container() > 0)
37 return get_process_cmdline(1, 0, false, ret);
38 else
39 return read_one_line_file("/proc/cmdline", ret);
40 }
41
42 int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value)) {
43 _cleanup_free_ char *line = NULL;
44 const char *p;
45 int r;
46
47 assert(parse_item);
48
49 r = proc_cmdline(&line);
50 if (r < 0)
51 return r;
52
53 p = line;
54 for (;;) {
55 _cleanup_free_ char *word = NULL;
56 char *value = NULL;
57
58 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
59 if (r < 0)
60 return r;
61 if (r == 0)
62 break;
63
64 /* Filter out arguments that are intended only for the
65 * initrd */
66 if (!in_initrd() && startswith(word, "rd."))
67 continue;
68
69 value = strchr(word, '=');
70 if (value)
71 *(value++) = 0;
72
73 r = parse_item(word, value);
74 if (r < 0)
75 return r;
76 }
77
78 return 0;
79 }
80
81 int get_proc_cmdline_key(const char *key, char **value) {
82 _cleanup_free_ char *line = NULL, *ret = NULL;
83 bool found = false;
84 const char *p;
85 int r;
86
87 assert(key);
88
89 r = proc_cmdline(&line);
90 if (r < 0)
91 return r;
92
93 p = line;
94 for (;;) {
95 _cleanup_free_ char *word = NULL;
96 const char *e;
97
98 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
99 if (r < 0)
100 return r;
101 if (r == 0)
102 break;
103
104 /* Filter out arguments that are intended only for the
105 * initrd */
106 if (!in_initrd() && startswith(word, "rd."))
107 continue;
108
109 if (value) {
110 e = startswith(word, key);
111 if (!e)
112 continue;
113
114 r = free_and_strdup(&ret, e);
115 if (r < 0)
116 return r;
117
118 found = true;
119 } else {
120 if (streq(word, key))
121 found = true;
122 }
123 }
124
125 if (value) {
126 *value = ret;
127 ret = NULL;
128 }
129
130 return found;
131
132 }
133
134 int shall_restore_state(void) {
135 _cleanup_free_ char *value = NULL;
136 int r;
137
138 r = get_proc_cmdline_key("systemd.restore_state=", &value);
139 if (r < 0)
140 return r;
141 if (r == 0)
142 return true;
143
144 return parse_boolean(value) != 0;
145 }