]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/debug-generator/debug-generator.c
debug-generator: add new kernel cmdline option systemd.wants= to add units to the...
[thirdparty/systemd.git] / src / debug-generator / debug-generator.c
CommitLineData
326bb68c
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 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 "util.h"
23#include "strv.h"
24#include "unit-name.h"
25#include "mkdir.h"
26
27static const char *arg_dest = "/tmp";
28static char **arg_mask = NULL;
3c5a87a8 29static char **arg_wants = NULL;
326bb68c
LP
30static bool arg_debug_shell = false;
31
32static int parse_proc_cmdline_item(const char *key, const char *value) {
33 int r;
34
3c5a87a8
LP
35 assert(key);
36
326bb68c
LP
37 if (streq(key, "systemd.mask")) {
38
39 if (!value)
40 log_error("Missing argument for systemd.mask= kernel command line parameter.");
41 else {
42 char *n;
43
3c5a87a8 44 n = unit_name_mangle(value, MANGLE_NOGLOB);
326bb68c
LP
45 if (!n)
46 return log_oom();
47
48 r = strv_consume(&arg_mask, n);
49 if (r < 0)
50 return log_oom();
51 }
3c5a87a8
LP
52
53 } else if (streq(key, "systemd.wants")) {
54
55 if (!value)
56 log_error("Missing argument for systemd.want= kernel command line parameter.");
57 else {
58 char *n;
59
60 n = unit_name_mangle(value, MANGLE_NOGLOB);
61 if (!n)
62 return log_oom();
63
64 r = strv_consume(&arg_wants, n);
65 if (r < 0)
66 return log_oom();
67 }
68
326bb68c
LP
69 } else if (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
81 return 0;
82}
83
84static int generate_mask_symlinks(void) {
85 char **u;
86 int r = 0;
87
88 if (strv_isempty(arg_mask))
89 return 0;
90
91 STRV_FOREACH(u, arg_mask) {
3c5a87a8 92 _cleanup_free_ char *p = NULL;
326bb68c 93
3c5a87a8 94 p = strjoin(arg_dest, "/", *u, NULL);
326bb68c
LP
95 if (!p)
96 return log_oom();
97
98 if (symlink("/dev/null", p) < 0) {
99 log_error("Failed to create mask symlink %s: %m", p);
100 r = -errno;
101 }
102 }
103
104 return r;
105}
106
3c5a87a8
LP
107static int generate_wants_symlinks(void) {
108 char **u;
109 int r = 0;
326bb68c 110
3c5a87a8 111 if (strv_isempty(arg_wants))
326bb68c
LP
112 return 0;
113
3c5a87a8
LP
114 STRV_FOREACH(u, arg_wants) {
115 _cleanup_free_ char *p = NULL, *f = NULL;
116
117 p = strjoin(arg_dest, "/default.target.wants/", *u, NULL);
118 if (!p)
119 return log_oom();
120
121 f = strappend(SYSTEM_DATA_UNIT_PATH "/", *u);
122 if (!f)
123 return log_oom();
326bb68c 124
3c5a87a8 125 mkdir_parents_label(p, 0755);
326bb68c 126
3c5a87a8
LP
127 if (symlink(f, p) < 0) {
128 log_error("Failed to create wants symlink %s: %m", p);
129 r = -errno;
130 }
326bb68c
LP
131 }
132
3c5a87a8 133 return r;
326bb68c
LP
134}
135
136int main(int argc, char *argv[]) {
137 int r, q;
138
139 if (argc > 1 && argc != 4) {
140 log_error("This program takes three or no arguments.");
141 return EXIT_FAILURE;
142 }
143
144 if (argc > 1)
145 arg_dest = argv[2];
146
147 log_set_target(LOG_TARGET_SAFE);
148 log_parse_environment();
149 log_open();
150
151 umask(0022);
152
153 if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
154 return EXIT_FAILURE;
155
3c5a87a8
LP
156 if (arg_debug_shell) {
157 r = strv_extend(&arg_wants, "debug-shell.service");
158 if (r < 0) {
159 r = log_oom();
160 goto finish;
161 }
162 }
163
326bb68c
LP
164 r = generate_mask_symlinks();
165
3c5a87a8 166 q = generate_wants_symlinks();
326bb68c
LP
167 if (q < 0)
168 r = q;
169
3c5a87a8 170finish:
326bb68c
LP
171 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
172
173}