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