]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/debug-generator/debug-generator.c
util-lib: various improvements to kernel command line parsing
[thirdparty/systemd.git] / src / debug-generator / debug-generator.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "mkdir.h"
22 #include "parse-util.h"
23 #include "proc-cmdline.h"
24 #include "special.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "unit-name.h"
28 #include "util.h"
29
30 static char *arg_default_unit = NULL;
31 static const char *arg_dest = "/tmp";
32 static char **arg_mask = NULL;
33 static char **arg_wants = NULL;
34 static bool arg_debug_shell = false;
35
36 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
37 int r;
38
39 assert(key);
40
41 if (streq(key, "systemd.mask")) {
42 char *n;
43
44 if (proc_cmdline_value_missing(key, value))
45 return 0;
46
47 r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n);
48 if (r < 0)
49 return log_error_errno(r, "Failed to glob unit name: %m");
50
51 r = strv_consume(&arg_mask, n);
52 if (r < 0)
53 return log_oom();
54
55 } else if (streq(key, "systemd.wants")) {
56 char *n;
57
58 if (proc_cmdline_value_missing(key, value))
59 return 0;
60
61 r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n);
62 if (r < 0)
63 return log_error_errno(r, "Failed to glob unit name: %m");
64
65 r = strv_consume(&arg_wants, n);
66 if (r < 0)
67 return log_oom();
68
69 } else if (proc_cmdline_key_streq(key, "systemd.debug_shell")) {
70
71 if (value) {
72 r = parse_boolean(value);
73 if (r < 0)
74 log_error("Failed to parse systemd.debug_shell= argument '%s', ignoring.", value);
75 else
76 arg_debug_shell = r;
77 } else
78 arg_debug_shell = true;
79
80 } else if (streq(key, "systemd.unit")) {
81
82 if (proc_cmdline_value_missing(key, value))
83 return 0;
84
85 r = free_and_strdup(&arg_default_unit, value);
86 if (r < 0)
87 return log_error_errno(r, "Failed to set default unit %s: %m", value);
88
89 } else if (!value) {
90 const char *target;
91
92 target = runlevel_to_target(key);
93 if (target) {
94 r = free_and_strdup(&arg_default_unit, target);
95 if (r < 0)
96 return log_error_errno(r, "Failed to set default unit %s: %m", target);
97 }
98 }
99
100 return 0;
101 }
102
103 static int generate_mask_symlinks(void) {
104 char **u;
105 int r = 0;
106
107 if (strv_isempty(arg_mask))
108 return 0;
109
110 STRV_FOREACH(u, arg_mask) {
111 _cleanup_free_ char *p = NULL;
112
113 p = strjoin(arg_dest, "/", *u);
114 if (!p)
115 return log_oom();
116
117 if (symlink("/dev/null", p) < 0)
118 r = log_error_errno(errno,
119 "Failed to create mask symlink %s: %m",
120 p);
121 }
122
123 return r;
124 }
125
126 static int generate_wants_symlinks(void) {
127 char **u;
128 int r = 0;
129
130 if (strv_isempty(arg_wants))
131 return 0;
132
133 STRV_FOREACH(u, arg_wants) {
134 _cleanup_free_ char *p = NULL, *f = NULL;
135 const char *target = arg_default_unit ?: SPECIAL_DEFAULT_TARGET;
136
137 p = strjoin(arg_dest, "/", target, ".wants/", *u);
138 if (!p)
139 return log_oom();
140
141 f = strappend(SYSTEM_DATA_UNIT_PATH "/", *u);
142 if (!f)
143 return log_oom();
144
145 mkdir_parents_label(p, 0755);
146
147 if (symlink(f, p) < 0)
148 r = log_error_errno(errno,
149 "Failed to create wants symlink %s: %m",
150 p);
151 }
152
153 return r;
154 }
155
156 int main(int argc, char *argv[]) {
157 int r, q;
158
159 if (argc > 1 && argc != 4) {
160 log_error("This program takes three or no arguments.");
161 return EXIT_FAILURE;
162 }
163
164 if (argc > 1)
165 arg_dest = argv[2];
166
167 log_set_target(LOG_TARGET_SAFE);
168 log_parse_environment();
169 log_open();
170
171 umask(0022);
172
173 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
174 if (r < 0)
175 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
176
177 if (arg_debug_shell) {
178 r = strv_extend(&arg_wants, "debug-shell.service");
179 if (r < 0) {
180 r = log_oom();
181 goto finish;
182 }
183 }
184
185 r = generate_mask_symlinks();
186
187 q = generate_wants_symlinks();
188 if (q < 0)
189 r = q;
190
191 finish:
192 arg_default_unit = mfree(arg_default_unit);
193
194 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
195 }