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