]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
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"
07630cea 26#include "string-util.h"
6bedfcbb 27#include "parse-util.h"
326bb68c
LP
28
29static const char *arg_dest = "/tmp";
30static char **arg_mask = NULL;
3c5a87a8 31static char **arg_wants = NULL;
326bb68c
LP
32static bool arg_debug_shell = false;
33
34static int parse_proc_cmdline_item(const char *key, const char *value) {
35 int r;
36
3c5a87a8
LP
37 assert(key);
38
326bb68c
LP
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
7410616c
LP
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");
326bb68c
LP
49
50 r = strv_consume(&arg_mask, n);
51 if (r < 0)
52 return log_oom();
53 }
3c5a87a8
LP
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
7410616c
LP
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");
3c5a87a8
LP
65
66 r = strv_consume(&arg_wants, n);
67 if (r < 0)
68 return log_oom();
69 }
70
326bb68c
LP
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
86static 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) {
3c5a87a8 94 _cleanup_free_ char *p = NULL;
326bb68c 95
3c5a87a8 96 p = strjoin(arg_dest, "/", *u, NULL);
326bb68c
LP
97 if (!p)
98 return log_oom();
99
1f6b4113 100 if (symlink("/dev/null", p) < 0)
94c156cd
LP
101 r = log_error_errno(errno,
102 "Failed to create mask symlink %s: %m",
103 p);
326bb68c
LP
104 }
105
106 return r;
107}
108
3c5a87a8
LP
109static int generate_wants_symlinks(void) {
110 char **u;
111 int r = 0;
326bb68c 112
3c5a87a8 113 if (strv_isempty(arg_wants))
326bb68c
LP
114 return 0;
115
3c5a87a8
LP
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();
326bb68c 126
3c5a87a8 127 mkdir_parents_label(p, 0755);
326bb68c 128
1f6b4113 129 if (symlink(f, p) < 0)
94c156cd
LP
130 r = log_error_errno(errno,
131 "Failed to create wants symlink %s: %m",
132 p);
326bb68c
LP
133 }
134
3c5a87a8 135 return r;
326bb68c
LP
136}
137
138int 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
b5884878
LP
155 r = parse_proc_cmdline(parse_proc_cmdline_item);
156 if (r < 0)
da927ba9 157 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
326bb68c 158
3c5a87a8
LP
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
326bb68c
LP
167 r = generate_mask_symlinks();
168
3c5a87a8 169 q = generate_wants_symlinks();
326bb68c
LP
170 if (q < 0)
171 r = q;
172
3c5a87a8 173finish:
326bb68c
LP
174 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
175
176}