]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/environment-d-generator/environment-d-generator.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / environment-d-generator / environment-d-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f63c4aab
ZJS
2/***
3 This file is part of systemd.
4
5 Copyright 2017 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#include "sd-path.h"
22
23#include "conf-files.h"
24#include "def.h"
25#include "escape.h"
26#include "fileio.h"
27#include "log.h"
28#include "path-lookup.h"
29
30static int environment_dirs(char ***ret) {
31 _cleanup_strv_free_ char **dirs = NULL;
32 _cleanup_free_ char *c = NULL;
33 int r;
34
35 dirs = strv_split_nulstr(CONF_PATHS_NULSTR("environment.d"));
36 if (!dirs)
37 return -ENOMEM;
38
39 /* ~/.config/systemd/environment.d */
40 r = sd_path_home(SD_PATH_USER_CONFIGURATION, "environment.d", &c);
41 if (r < 0)
42 return r;
43
44 r = strv_extend_front(&dirs, c);
45 if (r < 0)
46 return r;
47
48 *ret = dirs;
49 dirs = NULL;
50 return 0;
51}
52
53static int load_and_print(void) {
54 _cleanup_strv_free_ char **dirs = NULL, **files = NULL, **env = NULL;
55 char **i;
56 int r;
57
58 r = environment_dirs(&dirs);
59 if (r < 0)
60 return r;
61
b5084605 62 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs);
f63c4aab
ZJS
63 if (r < 0)
64 return r;
65
66 /* This will mutate the existing environment, based on the presumption
67 * that in case of failure, a partial update is better than none. */
68
69 STRV_FOREACH(i, files) {
70 r = merge_env_file(&env, NULL, *i);
71 if (r == -ENOMEM)
72 return r;
73 }
74
75 STRV_FOREACH(i, env) {
76 char *t;
77 _cleanup_free_ char *q = NULL;
78
79 t = strchr(*i, '=');
80 assert(t);
81
804ee07c 82 q = shell_maybe_quote(t + 1, ESCAPE_BACKSLASH);
f63c4aab
ZJS
83 if (!q)
84 return log_oom();
85
86 printf("%.*s=%s\n", (int) (t - *i), *i, q);
87 }
88
89 return 0;
90}
91
92int main(int argc, char *argv[]) {
93 int r;
94
95 log_parse_environment();
96 log_open();
97
98 if (argc > 1) {
99 log_error("This program takes no arguments.");
100 return EXIT_FAILURE;
101 }
102
103 r = load_and_print();
104 if (r < 0)
105 log_error_errno(r, "Failed to load environment.d: %m");
106
107 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
108}