]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/debug-generator/debug-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
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.
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.
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/>.
21 #include "alloc-util.h"
23 #include "parse-util.h"
24 #include "proc-cmdline.h"
26 #include "string-util.h"
28 #include "unit-name.h"
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;
37 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
42 if (streq(key
, "systemd.mask")) {
45 if (proc_cmdline_value_missing(key
, value
))
48 r
= unit_name_mangle(value
, UNIT_NAME_NOGLOB
, &n
);
50 return log_error_errno(r
, "Failed to glob unit name: %m");
52 r
= strv_consume(&arg_mask
, n
);
56 } else if (streq(key
, "systemd.wants")) {
59 if (proc_cmdline_value_missing(key
, value
))
62 r
= unit_name_mangle(value
, UNIT_NAME_NOGLOB
, &n
);
64 return log_error_errno(r
, "Failed to glob unit name: %m");
66 r
= strv_consume(&arg_wants
, n
);
70 } else if (proc_cmdline_key_streq(key
, "systemd.debug_shell")) {
73 r
= parse_boolean(value
);
75 log_error("Failed to parse systemd.debug_shell= argument '%s', ignoring.", value
);
79 arg_debug_shell
= true;
81 } else if (streq(key
, "systemd.unit")) {
83 if (proc_cmdline_value_missing(key
, value
))
86 r
= free_and_strdup(&arg_default_unit
, value
);
88 return log_error_errno(r
, "Failed to set default unit %s: %m", value
);
93 target
= runlevel_to_target(key
);
95 r
= free_and_strdup(&arg_default_unit
, target
);
97 return log_error_errno(r
, "Failed to set default unit %s: %m", target
);
104 static int generate_mask_symlinks(void) {
108 if (strv_isempty(arg_mask
))
111 STRV_FOREACH(u
, arg_mask
) {
112 _cleanup_free_
char *p
= NULL
;
114 p
= strjoin(arg_dest
, "/", *u
);
118 if (symlink("/dev/null", p
) < 0)
119 r
= log_error_errno(errno
,
120 "Failed to create mask symlink %s: %m",
127 static int generate_wants_symlinks(void) {
131 if (strv_isempty(arg_wants
))
134 STRV_FOREACH(u
, arg_wants
) {
135 _cleanup_free_
char *p
= NULL
, *f
= NULL
;
136 const char *target
= arg_default_unit
?: SPECIAL_DEFAULT_TARGET
;
138 p
= strjoin(arg_dest
, "/", target
, ".wants/", *u
);
142 f
= strappend(SYSTEM_DATA_UNIT_PATH
"/", *u
);
146 mkdir_parents_label(p
, 0755);
148 if (symlink(f
, p
) < 0)
149 r
= log_error_errno(errno
,
150 "Failed to create wants symlink %s: %m",
157 int main(int argc
, char *argv
[]) {
160 if (argc
> 1 && argc
!= 4) {
161 log_error("This program takes three or no arguments.");
168 log_set_target(LOG_TARGET_SAFE
);
169 log_parse_environment();
174 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
176 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
178 if (arg_debug_shell
) {
179 r
= strv_extend(&arg_wants
, "debug-shell.service");
186 r
= generate_mask_symlinks();
188 q
= generate_wants_symlinks();
193 arg_default_unit
= mfree(arg_default_unit
);
195 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;