]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/debug-generator/debug-generator.c
Merge pull request #1681 from ssahani/journal
[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
28 static const char *arg_dest = "/tmp";
29 static char **arg_mask = NULL;
30 static char **arg_wants = NULL;
31 static bool arg_debug_shell = false;
32
33 static int parse_proc_cmdline_item(const char *key, const char *value) {
34 int r;
35
36 assert(key);
37
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
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");
48
49 r = strv_consume(&arg_mask, n);
50 if (r < 0)
51 return log_oom();
52 }
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
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");
64
65 r = strv_consume(&arg_wants, n);
66 if (r < 0)
67 return log_oom();
68 }
69
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
85 static 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) {
93 _cleanup_free_ char *p = NULL;
94
95 p = strjoin(arg_dest, "/", *u, NULL);
96 if (!p)
97 return log_oom();
98
99 if (symlink("/dev/null", p) < 0)
100 r = log_error_errno(errno,
101 "Failed to create mask symlink %s: %m",
102 p);
103 }
104
105 return r;
106 }
107
108 static int generate_wants_symlinks(void) {
109 char **u;
110 int r = 0;
111
112 if (strv_isempty(arg_wants))
113 return 0;
114
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();
125
126 mkdir_parents_label(p, 0755);
127
128 if (symlink(f, p) < 0)
129 r = log_error_errno(errno,
130 "Failed to create wants symlink %s: %m",
131 p);
132 }
133
134 return r;
135 }
136
137 int 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
154 r = parse_proc_cmdline(parse_proc_cmdline_item);
155 if (r < 0)
156 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
157
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
166 r = generate_mask_symlinks();
167
168 q = generate_wants_symlinks();
169 if (q < 0)
170 r = q;
171
172 finish:
173 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
174
175 }