]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/debug-generator/debug-generator.c
2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
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.
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.
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/>.
20 #include "alloc-util.h"
22 #include "parse-util.h"
23 #include "proc-cmdline.h"
25 #include "string-util.h"
27 #include "unit-name.h"
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;
36 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
41 if (streq(key
, "systemd.mask")) {
44 log_error("Missing argument for systemd.mask= kernel command line parameter.");
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
);
57 } else if (streq(key
, "systemd.wants")) {
60 log_error("Missing argument for systemd.want= kernel command line parameter.");
64 r
= unit_name_mangle(value
, UNIT_NAME_NOGLOB
, &n
);
66 return log_error_errno(r
, "Failed to glob unit name: %m");
68 r
= strv_consume(&arg_wants
, n
);
73 } else if (streq(key
, "systemd.debug-shell")) {
76 r
= parse_boolean(value
);
78 log_error("Failed to parse systemd.debug-shell= argument '%s', ignoring.", value
);
82 arg_debug_shell
= true;
83 } else if (streq(key
, "systemd.unit")) {
86 log_error("Missing argument for systemd.unit= kernel command line parameter.");
88 r
= free_and_strdup(&arg_default_unit
, value
);
90 return log_error_errno(r
, "Failed to set default unit %s: %m", value
);
95 target
= runlevel_to_target(key
);
97 r
= free_and_strdup(&arg_default_unit
, target
);
99 return log_error_errno(r
, "Failed to set default unit %s: %m", target
);
106 static int generate_mask_symlinks(void) {
110 if (strv_isempty(arg_mask
))
113 STRV_FOREACH(u
, arg_mask
) {
114 _cleanup_free_
char *p
= NULL
;
116 p
= strjoin(arg_dest
, "/", *u
, NULL
);
120 if (symlink("/dev/null", p
) < 0)
121 r
= log_error_errno(errno
,
122 "Failed to create mask symlink %s: %m",
129 static int generate_wants_symlinks(void) {
133 if (strv_isempty(arg_wants
))
136 STRV_FOREACH(u
, arg_wants
) {
137 _cleanup_free_
char *p
= NULL
, *f
= NULL
;
139 p
= strjoin(arg_dest
, "/", arg_default_unit
, ".wants/", *u
, NULL
);
143 f
= strappend(SYSTEM_DATA_UNIT_PATH
"/", *u
);
147 mkdir_parents_label(p
, 0755);
149 if (symlink(f
, p
) < 0)
150 r
= log_error_errno(errno
,
151 "Failed to create wants symlink %s: %m",
158 int main(int argc
, char *argv
[]) {
161 if (argc
> 1 && argc
!= 4) {
162 log_error("This program takes three or no arguments.");
169 log_set_target(LOG_TARGET_SAFE
);
170 log_parse_environment();
175 r
= free_and_strdup(&arg_default_unit
, SPECIAL_DEFAULT_TARGET
);
177 log_error_errno(r
, "Failed to set default unit %s: %m", SPECIAL_DEFAULT_TARGET
);
181 r
= parse_proc_cmdline(parse_proc_cmdline_item
, NULL
);
183 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
185 if (arg_debug_shell
) {
186 r
= strv_extend(&arg_wants
, "debug-shell.service");
193 r
= generate_mask_symlinks();
195 q
= generate_wants_symlinks();
200 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;