]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/environment-d-generator/environment-d-generator.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / environment-d-generator / environment-d-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f63c4aab 2/***
96b2fb93 3 Copyright © 2017 Zbigniew Jędrzejewski-Szmek
f63c4aab
ZJS
4***/
5
6#include "sd-path.h"
7
8#include "conf-files.h"
9#include "def.h"
10#include "escape.h"
11#include "fileio.h"
12#include "log.h"
13#include "path-lookup.h"
14
15static int environment_dirs(char ***ret) {
16 _cleanup_strv_free_ char **dirs = NULL;
17 _cleanup_free_ char *c = NULL;
18 int r;
19
20 dirs = strv_split_nulstr(CONF_PATHS_NULSTR("environment.d"));
21 if (!dirs)
22 return -ENOMEM;
23
24 /* ~/.config/systemd/environment.d */
25 r = sd_path_home(SD_PATH_USER_CONFIGURATION, "environment.d", &c);
26 if (r < 0)
27 return r;
28
29 r = strv_extend_front(&dirs, c);
30 if (r < 0)
31 return r;
32
ae2a15bc 33 *ret = TAKE_PTR(dirs);
f63c4aab
ZJS
34 return 0;
35}
36
37static int load_and_print(void) {
38 _cleanup_strv_free_ char **dirs = NULL, **files = NULL, **env = NULL;
39 char **i;
40 int r;
41
42 r = environment_dirs(&dirs);
43 if (r < 0)
44 return r;
45
b5084605 46 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs);
f63c4aab
ZJS
47 if (r < 0)
48 return r;
49
50 /* This will mutate the existing environment, based on the presumption
51 * that in case of failure, a partial update is better than none. */
52
53 STRV_FOREACH(i, files) {
54 r = merge_env_file(&env, NULL, *i);
55 if (r == -ENOMEM)
56 return r;
57 }
58
59 STRV_FOREACH(i, env) {
60 char *t;
61 _cleanup_free_ char *q = NULL;
62
63 t = strchr(*i, '=');
64 assert(t);
65
804ee07c 66 q = shell_maybe_quote(t + 1, ESCAPE_BACKSLASH);
f63c4aab
ZJS
67 if (!q)
68 return log_oom();
69
70 printf("%.*s=%s\n", (int) (t - *i), *i, q);
71 }
72
73 return 0;
74}
75
76int main(int argc, char *argv[]) {
77 int r;
78
79 log_parse_environment();
80 log_open();
81
82 if (argc > 1) {
83 log_error("This program takes no arguments.");
84 return EXIT_FAILURE;
85 }
86
87 r = load_and_print();
88 if (r < 0)
89 log_error_errno(r, "Failed to load environment.d: %m");
90
91 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
92}