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