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