]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/debug-generator/debug-generator.c
treewide: use log_*_errno whenever %m is in the format string
[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 "util.h"
23 #include "strv.h"
24 #include "unit-name.h"
25 #include "mkdir.h"
26
27 static const char *arg_dest = "/tmp";
28 static char **arg_mask = NULL;
29 static char **arg_wants = NULL;
30 static bool arg_debug_shell = false;
31
32 static int parse_proc_cmdline_item(const char *key, const char *value) {
33 int r;
34
35 assert(key);
36
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
44 n = unit_name_mangle(value, MANGLE_NOGLOB);
45 if (!n)
46 return log_oom();
47
48 r = strv_consume(&arg_mask, n);
49 if (r < 0)
50 return log_oom();
51 }
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
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
84 static 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) {
92 _cleanup_free_ char *p = NULL;
93
94 p = strjoin(arg_dest, "/", *u, NULL);
95 if (!p)
96 return log_oom();
97
98 if (symlink("/dev/null", p) < 0) {
99 log_error_errno(errno, "Failed to create mask symlink %s: %m", p);
100 r = -errno;
101 }
102 }
103
104 return r;
105 }
106
107 static int generate_wants_symlinks(void) {
108 char **u;
109 int r = 0;
110
111 if (strv_isempty(arg_wants))
112 return 0;
113
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();
124
125 mkdir_parents_label(p, 0755);
126
127 if (symlink(f, p) < 0) {
128 log_error_errno(errno, "Failed to create wants symlink %s: %m", p);
129 r = -errno;
130 }
131 }
132
133 return r;
134 }
135
136 int 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 r = parse_proc_cmdline(parse_proc_cmdline_item);
154 if (r < 0)
155 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
156
157 if (arg_debug_shell) {
158 r = strv_extend(&arg_wants, "debug-shell.service");
159 if (r < 0) {
160 r = log_oom();
161 goto finish;
162 }
163 }
164
165 r = generate_mask_symlinks();
166
167 q = generate_wants_symlinks();
168 if (q < 0)
169 r = q;
170
171 finish:
172 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
173
174 }