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