]>
Commit | Line | Data |
---|---|---|
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 | ||
30 | static 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 | ||
ae2a15bc | 48 | *ret = TAKE_PTR(dirs); |
f63c4aab ZJS |
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 | ||
b5084605 | 61 | r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs); |
f63c4aab ZJS |
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 | ||
804ee07c | 81 | q = shell_maybe_quote(t + 1, ESCAPE_BACKSLASH); |
f63c4aab ZJS |
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 | } |