]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/debug-generator/debug-generator.c
Merge pull request #1962 from mbiebl/install-completion-networkctl
[thirdparty/systemd.git] / src / debug-generator / debug-generator.c
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 "alloc-util.h"
23 #include "mkdir.h"
24 #include "parse-util.h"
25 #include "proc-cmdline.h"
26 #include "special.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unit-name.h"
30 #include "util.h"
31
32 static char *arg_default_unit = NULL;
33 static const char *arg_dest = "/tmp";
34 static char **arg_mask = NULL;
35 static char **arg_wants = NULL;
36 static bool arg_debug_shell = false;
37
38 static int parse_proc_cmdline_item(const char *key, const char *value) {
39 int r;
40
41 assert(key);
42
43 if (streq(key, "systemd.mask")) {
44
45 if (!value)
46 log_error("Missing argument for systemd.mask= kernel command line parameter.");
47 else {
48 char *n;
49
50 r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n);
51 if (r < 0)
52 return log_error_errno(r, "Failed to glob unit name: %m");
53
54 r = strv_consume(&arg_mask, n);
55 if (r < 0)
56 return log_oom();
57 }
58
59 } else if (streq(key, "systemd.wants")) {
60
61 if (!value)
62 log_error("Missing argument for systemd.want= kernel command line parameter.");
63 else {
64 char *n;
65
66 r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n);
67 if (r < 0)
68 return log_error_errno(r, "Failed to glob unit name: %m");
69
70 r = strv_consume(&arg_wants, n);
71 if (r < 0)
72 return log_oom();
73 }
74
75 } else if (streq(key, "systemd.debug-shell")) {
76
77 if (value) {
78 r = parse_boolean(value);
79 if (r < 0)
80 log_error("Failed to parse systemd.debug-shell= argument '%s', ignoring.", value);
81 else
82 arg_debug_shell = r;
83 } else
84 arg_debug_shell = true;
85 } else if (streq(key, "systemd.unit")) {
86
87 if (!value)
88 log_error("Missing argument for systemd.unit= kernel command line parameter.");
89 else {
90 r = free_and_strdup(&arg_default_unit, value);
91 if (r < 0)
92 return log_error_errno(r, "Failed to set default unit %s: %m", value);
93 }
94 } else if (!value) {
95 const char *target;
96
97 target = runlevel_to_target(key);
98 if (target) {
99 r = free_and_strdup(&arg_default_unit, target);
100 if (r < 0)
101 return log_error_errno(r, "Failed to set default unit %s: %m", target);
102 }
103 }
104
105 return 0;
106 }
107
108 static int generate_mask_symlinks(void) {
109 char **u;
110 int r = 0;
111
112 if (strv_isempty(arg_mask))
113 return 0;
114
115 STRV_FOREACH(u, arg_mask) {
116 _cleanup_free_ char *p = NULL;
117
118 p = strjoin(arg_dest, "/", *u, NULL);
119 if (!p)
120 return log_oom();
121
122 if (symlink("/dev/null", p) < 0)
123 r = log_error_errno(errno,
124 "Failed to create mask symlink %s: %m",
125 p);
126 }
127
128 return r;
129 }
130
131 static int generate_wants_symlinks(void) {
132 char **u;
133 int r = 0;
134
135 if (strv_isempty(arg_wants))
136 return 0;
137
138 STRV_FOREACH(u, arg_wants) {
139 _cleanup_free_ char *p = NULL, *f = NULL;
140
141 p = strjoin(arg_dest, "/", arg_default_unit, ".wants/", *u, NULL);
142 if (!p)
143 return log_oom();
144
145 f = strappend(SYSTEM_DATA_UNIT_PATH "/", *u);
146 if (!f)
147 return log_oom();
148
149 mkdir_parents_label(p, 0755);
150
151 if (symlink(f, p) < 0)
152 r = log_error_errno(errno,
153 "Failed to create wants symlink %s: %m",
154 p);
155 }
156
157 return r;
158 }
159
160 int main(int argc, char *argv[]) {
161 int r, q;
162
163 if (argc > 1 && argc != 4) {
164 log_error("This program takes three or no arguments.");
165 return EXIT_FAILURE;
166 }
167
168 if (argc > 1)
169 arg_dest = argv[2];
170
171 log_set_target(LOG_TARGET_SAFE);
172 log_parse_environment();
173 log_open();
174
175 umask(0022);
176
177 r = free_and_strdup(&arg_default_unit, SPECIAL_DEFAULT_TARGET);
178 if (r < 0) {
179 log_error_errno(r, "Failed to set default unit %s: %m", SPECIAL_DEFAULT_TARGET);
180 goto finish;
181 }
182
183 r = parse_proc_cmdline(parse_proc_cmdline_item);
184 if (r < 0)
185 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
186
187 if (arg_debug_shell) {
188 r = strv_extend(&arg_wants, "debug-shell.service");
189 if (r < 0) {
190 r = log_oom();
191 goto finish;
192 }
193 }
194
195 r = generate_mask_symlinks();
196
197 q = generate_wants_symlinks();
198 if (q < 0)
199 r = q;
200
201 finish:
202 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
203
204 }