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