Commit Graph

35 Commits

Author SHA1 Message Date
Lennart Poettering 62d74c78b5 coccinelle: add reallocarray() coccinelle script
Let's systematically make use of reallocarray() whereever we invoke
realloc() with a product of two values.
2018-03-02 12:39:07 +01:00
Zbigniew Jędrzejewski-Szmek 00d4b1e684 basic: shorten the code a bit in two places
gcc complains that len might be used unitialized, but afaict, this is not true.
2018-02-26 15:47:12 +01:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Lubomir Rintel c7d797bbdf basic/env-util: don't relax unesaping of serialized environment strings
We wrote them ourselves -- they shouldn't contain invalid sequences.
2017-10-11 15:05:38 +02:00
Lubomir Rintel ea43bdd1d7 basic/env-util: drop the validation when deserializing environment
The environment variables we've serialized can quite possibly contain
characters outside the set allowed by env_assignment_is_valid(). In
fact, my environment seems to contain a couple of these:

  * TERMCAP set by screen contains a '\x7f' character
  * BASH_FUNC_module%% variable has a '%' character in name

Strict check of environment variables name and value certainly makes sense for
unit files, but not so much for deserialization of values we already had
in our environment.
2017-10-11 15:01:32 +02:00
Yu Watanabe 4c70109600 tree-wide: use IN_SET macro (#6977) 2017-10-04 16:01:32 +02:00
Zbigniew Jędrzejewski-Szmek 71e0acccfd nss: use secure_getenv for behaviour-modifying booleans (#6817)
Follow up for fe102d6ab1.
2017-09-14 09:20:27 +02:00
Zbigniew Jędrzejewski-Szmek 417eefb170 basic/env-util: when serializing, actually use escaping
Fixes #6152.
2017-06-23 20:46:33 -04:00
Zbigniew Jędrzejewski-Szmek d233c99ac8 manager: just warn about an invalid environment entry
Apart from bugs (as in #6152), this can happen if we ever make
our requirements for environment entries more stringent. As with
the rest of deserialization, we should just warn and continue.
2017-06-23 20:46:33 -04:00
Ronny Chevalier 16eefcafed env-util: fix memory leak (#5962)
If cunescape succeeds, but the assignment is not valid, uce is not freed.
2017-05-14 10:30:40 -04:00
Zbigniew Jędrzejewski-Szmek 1d046f5720 basic/env-util: drop leftover comment 2017-02-21 11:53:42 -05:00
Ray Strode b82f58bfe3 basic: support default and alternate values for env expansion
Sometimes it's useful to provide a default value during an environment
expansion, if the environment variable isn't already set.

For instance $XDG_DATA_DIRS is suppose to default to:

/usr/local/share/:/usr/share/

if it's not yet set. That means callers wishing to augment
XDG_DATA_DIRS need to manually add those two values.

This commit changes replace_env to support the following shell
compatible default value syntax:

XDG_DATA_DIRS=/foo:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share}

Likewise, it's useful to provide an alternate value during an
environment expansion, if the environment variable isn't already set.

For instance, $LD_LIBRARY_PATH will inadvertently search the current
working directory if it starts or ends with a colon, so the following
is usually wrong:

LD_LIBRARY_PATH=/foo/lib:${LD_LIBRARY_PATH}

To address that, this changes replace_env to support the following
shell compatible alternate value syntax:

LD_LIBRARY_PATH=/foo/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

[zj: gate the new syntax under REPLACE_ENV_ALLOW_EXTENDED switch, so
existing callers are not modified.]
2017-02-20 23:32:53 -05:00
Ray Strode 4bed076c5f basic: add replace_env_n function
It's like replace_env, but lets you pass in a substring.
2017-02-20 23:32:53 -05:00
Zbigniew Jędrzejewski-Szmek ccad1fd07c Allow braceless variables to be expanded
(Only in environment.d files.)

We have only basic compatibility with shell syntax, but specifying variables
without using braces is probably more common, and I think a lot of people would
be surprised if this didn't work.
2017-02-20 23:30:50 -05:00
Zbigniew Jędrzejewski-Szmek cb4499d005 basic/env-util: use _cleanup_ in replace_env() 2017-02-20 18:49:14 -05:00
Ray Strode 37f3ffca27 basic: add new merge_env_file function
merge_env_file is a new function, that's like load_env_file, but takes a
pre-existing environment as an input argument. New environment entries are
merged. Variable expansion is performed.

Falling back to the process environment is supported (when a flag is set).
Alternatively this could be implemented as passing an additional fallback
environment array, but later on we're adding another flag to allow braceless
expansion, and the two flags can be combined in one arg, so there's less
stuff to pass around.
2017-02-20 18:49:14 -05:00
Ray Strode d8ad241f54 basic: drop unnecessary strempty() call in replace_env
strempty() converts a NULL value to empty string, so
that it can be passed on to functions that don't support NULL.

replace_env calls strempty before passing its value on to strappend.

strappend supports NULL just fine, though, so this commit drops the
strempty call.
2017-02-20 18:49:14 -05:00
Ray Strode 6162512cde basic: fix strv_env_get_n for unclean arrays
If an environment array has duplicates, strv_env_get_n returns
the results for the first match. This is wrong, because later
entries in the environment are supposed to replace earlier
entries.
2017-02-20 18:49:14 -05:00
Zbigniew Jędrzejewski-Szmek c8cebc36b0 basic/env-util: drop _pure_ from static function 2017-02-20 18:49:14 -05:00
Zbigniew Jędrzejewski-Szmek 99003e01b6 env-util,fileio: immediately replace variables in load_env_file_push()
strv_env_replace was calling env_match(), which in effect allowed multiple
values for the same key to be inserted into the environment block. That's
pointless, because APIs to access variables only return a single value (the
latest entry), so it's better to keep the block clean, i.e. with just a single
entry for each key.

Add a new helper function that simply tests if the part before '=' is equal in
two strings and use that in strv_env_replace.

In load_env_file_push, use strv_env_replace to immediately replace the previous
assignment with a matching name.

Afaict, none of the callers are materially affected by this change, but it
seems like some pointless work was being done, if the same value was set
multiple times. We'd go through parsing and assigning the value for each
entry. With this change, we handle just the last one.
2017-02-20 18:49:14 -05:00
Zbigniew Jędrzejewski-Szmek fe902fa496 core/manager: move environment serialization out to basic/env-util.c
This protocol is generally useful, we might just as well reuse it for the
env. generators.

The implementation is changed a bit: instead of making a new strv and freeing
the old one, just mutate the original. This is much faster with larger arrays,
while in fact atomicity is preserved, since we only either insert the new
entry or not, without being in inconsistent state.

v2:
- fix confusion with return value
2017-02-20 18:49:14 -05:00
Zbigniew Jędrzejewski-Szmek a1e45b8ba3 basic/env-uil: fix assertion failure in strv_env_replace (#4688)
free_and_replace sets the setcond argument to NULL (it's designed
to be used with _clenaup_ macros), and we don't want that here.

Fixes #4684.
2016-11-17 22:25:01 +01:00
Zbigniew Jędrzejewski-Szmek 54ac349445 core/load-fragment: modify existing environment instead of copying strv over and over 2016-11-05 18:54:27 -04:00
Zbigniew Jędrzejewski-Szmek 6b430fdb7c tree-wide: use mfree more 2016-10-16 23:35:39 -04:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Thomas Hindoe Paaboel Andersen 93cc7779e0 basic: re-sort includes
My previous patch to only include what we use accidentially placed
the added inlcudes in non-sorted order.
2015-12-01 23:40:17 +01:00
Thomas Hindoe Paaboel Andersen 11c3a36649 basic: include only what we use
This is a cleaned up result of running iwyu but without forward
declarations on src/basic.
2015-11-30 21:51:03 +01:00
Filipe Brandenburger b4c14404b3 execute: Add new PassEnvironment= directive
This directive allows passing environment variables from the system
manager to spawned services. Variables in the system manager can be set
inside a container by passing `--set-env=...` options to systemd-spawn.

Tested with an on-disk test.service unit. Tested using multiple variable
names on a single line, with an empty setting to clear the current list
of variables, with non-existing variables.

Tested using `systemd-run -p PassEnvironment=VARNAME` to confirm it
works with transient units.

Confirmed that `systemctl show` will display the PassEnvironment
settings.

Checked that man pages are generated correctly.

No regressions in `make check`.
2015-11-11 07:55:23 -08:00
Lennart Poettering b41b9d2ae9 systemctl: add a way to explicitly request client-side unit installing
This adds support for a new environment variable
SYSTEMCTL_INSTALL_CLIENT_SIDE, that ensures that systemctl executes
install operations client-side instead of passing them to PID1. This is
useful in debugging situations, but even beyond that. However, we don't
want to make it official API, hence let's just make it an undocumented
environment variable.

Similar, add a second variable, SYSTEMCTL_SKIP_SYSV which allows
skipping the SysV chkconfig fall-back if set. This is useful for similar
reasons, and exposed as undocumented as environment variable for similar
reasons, too.
2015-10-31 19:09:20 +01:00
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Michal Schmidt df553b586b basic: fix env expansion for strings leading with two dollar signs
The way to escape a literal dollar sign is to write "$$". But this does
not work right if it's at the beginning of the argument. Fix it.
2015-10-07 14:43:55 +02:00
Richard Maw 8adaf7bd23 strv: convert strv_split_quotes into a generic strv_split_extract
strv_split_extract is to strv_split_quotes as extract_first_word was to
unquote_first_word.

Now there's extract_first_word for extracting a single argument,
extract_many_words for extracting a bounded number of arguments,
and strv_split_extract for extracting an arbitrary number of arguments.
2015-08-07 15:50:43 +00:00
Richard Maw 6868560773 util: change unquote_*_word to extract_*_word
It now takes a separators argument, which defaults to WHITESPACE if NULL
is passed.
2015-08-07 15:50:42 +00:00
Kay Sievers a095315b3c build-sys: split internal basic/ library from shared/
basic/      can be used by everything
            cannot use anything outside of basic/

libsystemd/ can use basic/
            cannot use shared/

shared/     can use libsystemd/
2015-06-11 10:52:46 +02:00
Renamed from src/shared/env-util.c (Browse further)