Skip to content

Commit f63c4aa

Browse files
committed
environment-generator: new generator to peruse environment.d
Why the strange name: the prefix is necessary to follow our own advice that environment generators should have numerical prefixes. I also put -d- in the name because otherwise the name was very easy to mistake with systemd.environment-generator. This additional letter clarifies that this on special generator that supports environment.d files.
1 parent 37f3ffc commit f63c4aa

File tree

8 files changed

+208
-2
lines changed

8 files changed

+208
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
/*.tar.bz2
2020
/*.tar.gz
2121
/*.tar.xz
22+
/30-systemd-environment-d-generator
2223
/GPATH
2324
/GRTAGS
2425
/GSYMS

Makefile-man.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ MANPAGES += \
110110
man/systemd-debug-generator.8 \
111111
man/systemd-delta.1 \
112112
man/systemd-detect-virt.1 \
113+
man/systemd-environment-d-generator.8 \
113114
man/systemd-escape.1 \
114115
man/systemd-fsck@.service.8 \
115116
man/systemd-fstab-generator.8 \
@@ -185,6 +186,7 @@ MANPAGES += \
185186
man/udev_new.3 \
186187
man/udevadm.8
187188
MANPAGES_ALIAS += \
189+
man/30-systemd-environment-d-generator.8 \
188190
man/SD_ALERT.3 \
189191
man/SD_BUS_ERROR_ACCESS_DENIED.3 \
190192
man/SD_BUS_ERROR_ADDRESS_IN_USE.3 \
@@ -542,6 +544,7 @@ MANPAGES_ALIAS += \
542544
man/udev_ref.3 \
543545
man/udev_unref.3 \
544546
man/user.conf.d.5
547+
man/30-systemd-environment-d-generator.8: man/systemd-environment-d-generator.8
545548
man/SD_ALERT.3: man/sd-daemon.3
546549
man/SD_BUS_ERROR_ACCESS_DENIED.3: man/sd-bus-errors.3
547550
man/SD_BUS_ERROR_ADDRESS_IN_USE.3: man/sd-bus-errors.3
@@ -899,6 +902,9 @@ man/udev_monitor_unref.3: man/udev_monitor_new_from_netlink.3
899902
man/udev_ref.3: man/udev_new.3
900903
man/udev_unref.3: man/udev_new.3
901904
man/user.conf.d.5: man/systemd-system.conf.5
905+
man/30-systemd-environment-d-generator.html: man/systemd-environment-d-generator.html
906+
$(html-alias)
907+
902908
man/SD_ALERT.html: man/sd-daemon.html
903909
$(html-alias)
904910

@@ -2768,6 +2774,7 @@ EXTRA_DIST += \
27682774
man/systemd-debug-generator.xml \
27692775
man/systemd-delta.xml \
27702776
man/systemd-detect-virt.xml \
2777+
man/systemd-environment-d-generator.xml \
27712778
man/systemd-escape.xml \
27722779
man/systemd-firstboot.xml \
27732780
man/systemd-fsck@.service.xml \

Makefile.am

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ systemgenerator_PROGRAMS = \
426426
systemd-system-update-generator \
427427
systemd-debug-generator
428428

429+
userenvgenerator_PROGRAMS = \
430+
30-systemd-environment-d-generator
431+
429432
dist_bashcompletion_data = \
430433
shell-completion/bash/busctl \
431434
shell-completion/bash/journalctl \
@@ -2817,6 +2820,13 @@ systemd_system_update_generator_SOURCES = \
28172820
systemd_system_update_generator_LDADD = \
28182821
libsystemd-shared.la
28192822

2823+
# ------------------------------------------------------------------------------
2824+
30_systemd_environment_d_generator_SOURCES = \
2825+
src/environment-d-generator/environment-d-generator.c
2826+
2827+
30_systemd_environment_d_generator_LDADD = \
2828+
libsystemd-shared.la
2829+
28202830
# ------------------------------------------------------------------------------
28212831
if ENABLE_HIBERNATE
28222832
systemgenerator_PROGRAMS += \
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version='1.0'?> <!--*-nxml-*-->
2+
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3+
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4+
<!ENTITY % entities SYSTEM "custom-entities.ent" >
5+
%entities;
6+
]>
7+
8+
<!--
9+
This file is part of systemd.
10+
11+
Copyright 2017 Zbigniew Jędrzejewski-Szmek
12+
13+
systemd is free software; you can redistribute it and/or modify it
14+
under the terms of the GNU Lesser General Public License as published by
15+
the Free Software Foundation; either version 2.1 of the License, or
16+
(at your option) any later version.
17+
18+
systemd is distributed in the hope that it will be useful, but
19+
WITHOUT ANY WARRANTY; without even the implied warranty of
20+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
Lesser General Public License for more details.
22+
23+
You should have received a copy of the GNU Lesser General Public License
24+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
25+
-->
26+
<refentry id="systemd-environment-d-generator">
27+
28+
<refentryinfo>
29+
<title>systemd-environment-d-generator</title>
30+
<productname>systemd</productname>
31+
32+
<authorgroup>
33+
<author>
34+
<contrib>Developer</contrib>
35+
<firstname>Zbigniew</firstname>
36+
<surname>Jędrzejewski-Szmek</surname>
37+
<email>zbyszek@in.waw.pl</email>
38+
</author>
39+
</authorgroup>
40+
</refentryinfo>
41+
42+
<refmeta>
43+
<refentrytitle>systemd-environment-d-generator</refentrytitle>
44+
<manvolnum>8</manvolnum>
45+
</refmeta>
46+
47+
<refnamediv>
48+
<refname>systemd-environment-d-generator</refname>
49+
<refname>30-systemd-environment-d-generator</refname>
50+
<refpurpose>Load variables specified by <filename>environment.d</filename>
51+
</refpurpose>
52+
</refnamediv>
53+
54+
<refsynopsisdiv>
55+
<para><filename>&userenvgeneratordir;/30-systemd-environment-d-generator</filename></para>
56+
</refsynopsisdiv>
57+
58+
<refsect1>
59+
<title>Description</title>
60+
61+
<para><filename>systemd-environment-d-generator</filename> is a
62+
<citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>
63+
that reads environment configuration specified by
64+
<citerefentry><refentrytitle>environment.d</refentrytitle><manvolnum>7</manvolnum></citerefentry>
65+
configuration files and passes it to the
66+
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
67+
user manager instance.</para>
68+
</refsect1>
69+
70+
<refsect1>
71+
<title>See Also</title>
72+
<para>
73+
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
74+
<citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
75+
<citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
76+
<citerefentry><refentrytitle>systemd.generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>
77+
</para>
78+
</refsect1>
79+
80+
</refentry>

man/systemd.environment-generator.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<title>See also</title>
152152

153153
<para>
154+
<citerefentry><refentrytitle>systemd-environment-d-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
154155
<citerefentry><refentrytitle>systemd.generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
155156
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
156157
<citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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, (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);
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+
}

src/shared/path-lookup.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ int lookup_paths_init(
503503
append = true;
504504
}
505505

506-
/* FIXME: empty components in other places should be
507-
* rejected. */
506+
/* FIXME: empty components in other places should be rejected. */
508507

509508
r = path_split_and_make_absolute(e, &paths);
510509
if (r < 0)

0 commit comments

Comments
 (0)