Commit graph

57 commits

Author SHA1 Message Date
Lennart Poettering fbd0b64f44
tree-wide: make use of new STRLEN() macro everywhere (#7639)
Let's employ coccinelle to do this for us.

Follow-up for #7625.
2017-12-14 19:02:29 +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
Zbigniew Jędrzejewski-Szmek ac097c841e Remove a bunch of unused variables
gcc does not warn about those, because of the _cleanup_ usage.
clang is smarter here.
2017-11-01 23:06:44 +01:00
Yu Watanabe 4c70109600 tree-wide: use IN_SET macro (#6977) 2017-10-04 16:01:32 +02:00
Jan Synacek 0cde65e263 test-cpu-set-util.c: fix typo in comment (#6916) 2017-09-26 16:07:34 +02:00
Zbigniew Jędrzejewski-Szmek a2deeb41f6 Merge pull request #6891 from poettering/read-line
add read_line() helper as bounded getline() and make use of it at some places
2017-09-24 20:51:01 +02:00
Zbigniew Jędrzejewski-Szmek 2c9de13912 test-fileio: also test read_line() with actual files
Just in case the real FILE and the one from fmemopen weren't exactly
the same.
2017-09-24 10:50:27 +02:00
Zbigniew Jędrzejewski-Szmek 9707d55213 test-fileio: close two leaked file handles 2017-09-24 10:50:27 +02:00
Lennart Poettering b183713383 fileio: make write_string_stream() accept flags parameter
Let's make write_string_stream() and write_string_file() more alike, and
pass the same flag set so that we can remove a number of boolean
parameters.
2017-09-22 20:55:34 +02:00
Lennart Poettering 4f9a66a32d fileio: add new helper call read_line() as bounded getline() replacement
read_line() is much like getline(), and returns a line read from a
FILE*, of arbitrary sizes. In contrast to gets() it will grow the buffer
dynamically, and in contrast to getline() it will place a user-specified
boundary on the line.
2017-09-22 20:34:15 +02:00
Lennart Poettering e6a7ec4b8e io-util: add new IOVEC_INIT/IOVEC_MAKE macros
This adds IOVEC_INIT() and IOVEC_MAKE() for initializing iovec structures
from a pointer and a size. On top of these IOVEC_INIT_STRING() and
IOVEC_MAKE_STRING() are added which take a string and automatically
determine the size of the string using strlen().

This patch removes the old IOVEC_SET_STRING() macro, given that
IOVEC_MAKE_STRING() is now useful for similar purposes. Note that the
old IOVEC_SET_STRING() invocations were two characters shorter than the
new ones using IOVEC_MAKE_STRING(), but I think the new syntax is more
readable and more generic as it simply resolves to a C99 literal
structure initialization. Moreover, we can use very similar syntax now
for initializing strings and pointer+size iovec entries. We canalso use
the new macros to initialize function parameters on-the-fly or array
definitions. And given that we shouldn't have so many ways to do the
same stuff, let's just settle on the new macros.

(This also converts some code to use _cleanup_ where dynamically
allocated strings were using IOVEC_SET_STRING() before, to modernize
things a bit)
2017-09-22 15:28:04 +02: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
Zbigniew Jędrzejewski-Szmek 184d190473 Tighten checking for variable validity
In the future we might want to allow additional syntax (for example
"unset VAR". But let's check that the data we're getting does not contain
anything unexpected.
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
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
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 605405c6cc tree-wide: drop NULL sentinel from strjoin
This makes strjoin and strjoina more similar and avoids the useless final
argument.

spatch -I . -I ./src -I ./src/basic -I ./src/basic -I ./src/shared -I ./src/shared -I ./src/network -I ./src/locale -I ./src/login -I ./src/journal -I ./src/journal -I ./src/timedate -I ./src/timesync -I ./src/nspawn -I ./src/resolve -I ./src/resolve -I ./src/systemd -I ./src/core -I ./src/core -I ./src/libudev -I ./src/udev -I ./src/udev/net -I ./src/udev -I ./src/libsystemd/sd-bus -I ./src/libsystemd/sd-event -I ./src/libsystemd/sd-login -I ./src/libsystemd/sd-netlink -I ./src/libsystemd/sd-network -I ./src/libsystemd/sd-hwdb -I ./src/libsystemd/sd-device -I ./src/libsystemd/sd-id128 -I ./src/libsystemd-network --sp-file coccinelle/strjoin.cocci --in-place $(git ls-files src/*.c)

git grep -e '\bstrjoin\b.*NULL' -l|xargs sed -i -r 's/strjoin\((.*), NULL\)/strjoin(\1)/'

This might have missed a few cases (spatch has a really hard time dealing
with _cleanup_ macros), but that's no big issue, they can always be fixed
later.
2016-10-23 11:43:27 -04:00
Topi Miettinen 646853bdd8 fileio: simplify mkostemp_safe() (#4090)
According to its manual page, flags given to mkostemp(3) shouldn't include
O_RDWR, O_CREAT or O_EXCL flags as these are always included. Beyond
those, the only flag that all callers (except a few tests where it
probably doesn't matter) use is O_CLOEXEC, so set that unconditionally.
2016-09-13 08:20:38 +02:00
Daniel Kahn Gillmor 1a7906ae91 basic: define HEXDIGITS
define HEXDIGITS alongside DIGITS, and use it where it's already useful.  We'll
use it again shortly when parsing MAC addresses.
2016-05-17 16:55:20 -04:00
Ronny Chevalier 897891f02e tests: move fileio related tests to test-fileio.c 2016-03-03 19:04:06 +01: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 cf0fbc49e6 tree-wide: sort includes
Sort the includes accoding to the new coding style.
2015-11-16 22:09:36 +01:00
Lennart Poettering eb3da9012f util-lib: optionally, when writing a string to a file, verify string on failure
With this change, the idiom:

    r = write_string_file(p, buf, 0);
    if (r < 0) {
           if (verify_one_line_file(p, buf) > 0)
                   r = 0;
    }

gets reduced to:

    r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE);

i.e. when writing the string fails and the new flag
WRITE_STRING_FILE_VERIFY_ON_FAILURE is specified we'll not return a
failure immediately, but check the contents of the file. If it matches
what we wanted to write we suppress the error and exit cleanly.
2015-11-13 13:02:49 +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 6bedfcbb29 util-lib: split string parsing related calls from util.[ch] into parse-util.[ch] 2015-10-27 13:25:55 +01:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +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
Aaro Koskinen c4cd1d4d93 fileio: make get_status_field() more generic
All users of get_status_field() expect the field pattern to occur in
the beginning of a line, and the delimiter is ':'.

Hardcode this into the function, and also skip any whitespace before ':'
to support fields in files like /proc/cpuinfo. Add support for returning
the full field value (currently stops on first whitespace).

Rename the function so it's easier to ensure all callers switch to new
semantics.
2015-09-30 15:57:55 +03:00
Daniel Mack 4c1fc3e404 fileio: consolidate write_string_file*()
Merge write_string_file(), write_string_file_no_create() and
write_string_file_atomic() into write_string_file() and provide a flags mask
that allows combinations of atomic writing, newline appending and automatic
file creation. Change all users accordingly.
2015-07-06 19:19:25 -04:00
Daniel Mack 40beecdb6d fileio: add 'enforce_newline' argument to write_string_stream()
Add a flag to control whether write_string_stream() should always enforce a
trailing newline character in the file.
2015-07-06 17:31:44 -04:00
Ronny Chevalier 0b452006de shared: add process-util.[ch] 2015-04-10 23:54:49 +02:00
Lennart Poettering 039f0e70a0 env-util: don't include files from src/core/ 2014-12-23 19:15:27 +01:00
Lennart Poettering cda134ab1e copy: teach copy_bytes() sendfile() support, and then replace sendfile_full() by it 2014-11-07 01:19:56 +01:00
Tom Gundersen 696c24fced test: fileio - make coverity happy
Explicitly check the length of the read.

Fixes CID#1250803.
2014-11-05 16:54:22 +01:00
Ronny Chevalier e07995a3e2 tests: add tests for fileio.c
add tests for the following functions:
- write_string_file_no_create
- load_env_file_pairs
2014-10-31 10:57:21 +01:00
Lukas Nykryn 7491ccf2cb environment: append unit_id to error messages regarding EnvironmentFile 2014-10-17 16:05:57 +02:00
Thomas Hindoe Paaboel Andersen bdf7026e95 test: only use assert_se
The asserts used in the tests should never be allowed to be
optimized away
2014-10-04 23:55:35 +02:00
Thomas Hindoe Paaboel Andersen 2b01a801f6 test-fileio: Remove dead check
t cannot be null here
2014-09-30 23:40:40 +02:00
Ronny Chevalier 76082570b8 tests: fix resource & mem leaks 2014-09-18 12:09:10 +02:00
Thomas Hindoe Paaboel Andersen cca0efb047 test: silence a coverity report
We check the actual contents of the file on the line after but we
might as well also check the number of bytes read here.

Found by coverity. Fixes: CID#1237521
2014-09-16 22:02:23 +02:00
Ronny Chevalier 0709b74374 tests: add tests for fileio.c
add tests for:
- write_string_stream
- write_string_file
- sendfile_full
2014-08-18 18:43:50 +02:00
Lennart Poettering 717603e391 machinectl: show /etc/os-release information of container in status output 2014-07-03 17:54:24 +02:00
Lennart Poettering 2d5bdf5bc0 always use the same code for creating temporary files
Let's unify our code here, and also always specifiy O_CLOEXEC.
2014-01-28 13:47:35 +01:00
Thomas Hindoe Paaboel Andersen 5fba7bbfa4 scan-build: silence some warnings
test-fileio/test-strv:
Use the streq_ptr to make build-scan not worry about passing in a null
to a nonnull function.

test-dhcp-option:
Prevent a theoretical null pointer dereference
2013-12-12 23:08:47 +01:00
Thomas Hindoe Paaboel Andersen d514feaa87 test-fileio: replace mktemp with mkstemp to avoid warnings
This is a fairly useless thing to do but it makes the compilers
and analyzers shut up about the use of mktemp.
2013-12-12 02:46:09 +01:00
Zbigniew Jędrzejewski-Szmek 1e5413f74f Add more tests and fix capability logging 2013-09-19 16:22:59 -05:00
Zbigniew Jędrzejewski-Szmek d8a1100346 test-fileio: assume that Buffers may be missing 2013-09-17 15:50:49 -05:00
Zbigniew Jędrzejewski-Szmek 442e00839e Assume that /proc/meminfo can be missing
Travis tests are failing, probably because /proc/meminfo is not available
in the test environment. The same might be true in some virtualized systems,
so just treat missing /proc/meminfo as a sign that hibernation is not
possible.
2013-09-16 09:47:28 -05:00
Zbigniew Jędrzejewski-Szmek 69ab808815 Advertise hibernation only if there's enough free swap
Condition that is checked is taken from upower:
  active(anon) < free swap * 0.98

This is really stupid, because the kernel knows the situation better,
e.g. there could be two swap files, and then hibernation would be
impossible despite passing this check, or the kernel could start
supporting compressed swap and/or compressed hibernation images, and
then this this check would be too stringent. Nevertheless, until
we have something better, this should at least return a true negative
if there's no swap.

Logging of capabilities in the journal is changed to not strip leading
zeros. I consider this more readable anyway.

http://cgit.freedesktop.org/upower/tree/src/up-daemon.c#n613
https://bugzilla.redhat.com/show_bug.cgi?id=1007059
2013-09-13 19:41:52 -04:00
Zbigniew Jędrzejewski-Szmek ac4c8d6da8 Allow tabs in environment files
bash allows them, and so should we.

string_has_cc is changed to allow tabs, and if they are not wanted,
they must be now checked for explicitly. There are two other callers,
apart from the env file loaders, and one already checked anyway, and
the other is changed to check.

https://bugs.freedesktop.org/show_bug.cgi?id=68592
https://bugs.gentoo.org/show_bug.cgi?id=481554
2013-09-11 21:58:22 -04:00