Commit graph

95 commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek 649bde89ca test: use pclose() for popen()
Coverity CID#1432701.
2020-09-14 22:32:52 +02:00
Lennart Poettering df8b14b591 test-fileio: test test strings with shell, too 2020-09-14 12:26:41 +02:00
Lennart Poettering 6fe3196350 test-fileio: use test strings that are actually valid in shell 2020-09-14 11:08:43 +02:00
Lennart Poettering b93d3f6b81 fileio: add support for read_full_file() on AF_UNIX stream sockets
Optionally, teach read_full_file() the ability to connect to an AF_UNIX
socket if the specified path points to one.
2020-07-21 10:23:44 +02:00
Lennart Poettering 6028d766d1 macro: introduce DISABLE_WARNING_TYPE_LIMITS and make use of it everywhere 2020-05-25 18:25:09 +02:00
Topi Miettinen 3c14dc61f7 tests: various small fixes for strict systems
Don't assume that 4MB can be allocated from stack since there could be smaller
DefaultLimitSTACK= in force, so let's use malloc(). NUL terminate the huge
strings by hand, also ensure termination in test_lz4_decompress_partial() and
optimize the memset() for the string.

Some items in /proc and /etc may not be accessible to poor unprivileged users
due to e.g. SELinux, BOFH or both, so check for EACCES and EPERM.

/var/tmp may be a symlink to /tmp and then path_compare() will always fail, so
let's stick to /tmp like elsewhere.

/tmp may be mounted with noexec option and then trying to execute scripts from
there would fail.

Detect and warn if seccomp is already in use, which could make seccomp test
fail if the syscalls are already blocked.

Unset $TMPDIR so it will not break specifier tests where %T is assumed to be
/tmp and %V /var/tmp.
2020-04-26 20:18:48 +02:00
Zbigniew Jędrzejewski-Szmek 30b84c78ea test-fileio: fix bogus error when /proc/cmdline contains newlines
The kernel does not sanitize /proc/cmdline. E.g. when running under qemu, it is
easy to pass a string with newline by mistake. We use read_one_line_file(), so
we would read only the first list of the file, and
write_string_file(WRITE_STRING_FILE_VERIFY_ON_FAILURE) would fail because the
target file is obviously different. Change to a kernel-generated file to avoid
the issue.

v2:
- use /proc/version instead of /proc/uptime for attempted writes, so the test
  test passes even if test_write_string_file_verify() takes more than 10 ms ;]
2020-03-28 11:46:47 +01:00
Lennart Poettering 72fd79b3ce test-fileio: cast EOF to (char) before comparing with char explicitly
EOF is defined to -1, hence on platforms that have "char" unsigned we
can't compare it as-is, except if we accept an implicit cast. let's make
it an explicit cast, acknowledging the issue.

Fixes: #14118
2019-11-26 14:55:47 +01:00
Yu Watanabe 8cec0a5c32 tree-wide: drop duplicated blank lines
```
$ for i in */*.[ch] */*/*.[ch]; do sed -e '/^$/ {N; s/\n$//g}' -i $i; done
$ git checkout HEAD -- basic/linux shared/linux
```
2019-07-15 18:41:27 +02:00
Zbigniew Jędrzejewski-Szmek 2aa07538ba test: minor modernization
Coverity was complaining that read() does not terminate the data. But
we did that termination earlier, so covirity is wrong (CID#1402306, CID#1402340).
Let's modernize the style a bit nevertheless.

(size_t) cast is needed to avoid the warning about comparison, iff
the value is not a constant.
2019-07-12 00:17:45 +02:00
Ben Boeckel 5238e95759 codespell: fix spelling errors 2019-04-29 16:47:18 +02:00
Zbigniew Jędrzejewski-Szmek 673a1e6fb9 Add fmemopen_unlocked() and use unlocked ops in fuzzers and some other tests
This might make things marginially faster. I didn't benchmark though.
2019-04-12 11:44:57 +02:00
Zbigniew Jędrzejewski-Szmek b45556375e test-fileio: do not use variable before checking return value
Coverity is unhappy because we use "line" in the assert that checks
the return value. It doesn't matter much, but let's clean this up.
Also, let's not assume that /proc/cmdline contains anything.

CID #1400219.
2019-03-27 11:52:54 +01:00
Zbigniew Jędrzejewski-Szmek d3bdba3816 test-fileio: avoid warning about ineffective comparison
On arm64 with gcc-8.2.1-5.fc29.aarch64:
../src/test/test-fileio.c:645:29: warning: comparison is always false due to limited range of data type [-Wtype-limits]
                 assert_se(c == EOF || safe_fgetc(f, &c) == 1);
                             ^~

Casting c to int is not enough, gcc is able to figure out that the original
type was unsigned and still warns. So let's just silence the warning like
in test-sizeof.c.
2019-03-20 14:00:12 +01:00
Louis Taylor e4a8db1fbd util-lib: follow shell syntax for escape in quotes
Fixes #10659.

This changes the behaviour of parsing environment files to more closely
follow POSIX shell standards.

This has the effect that these variables defined in a file:

    VAR1='\value'
    VAR2="\value"

Are now interpreted as `\value` instead of interpreting the `\`
character and interpreting them as `value`.

For more information about the behaviour followed, see:

	http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
2019-01-17 13:42:54 +00:00
Lennart Poettering cdce33f987 test-fileio: add explicit check for safe_fgetc() with 0xFF 2018-12-20 12:11:18 +01:00
Lennart Poettering 517b776042 fileio: fix read_one_line() when reading bytes > 0x7F
Fixes: #11218
2018-12-20 12:11:18 +01:00
Zbigniew Jędrzejewski-Szmek 7f9d1aedec test-fileio: test safe_fgetc directly
Non-ascii chars are used so that we get both "positive" and "negative"
characters (on the arches where char is signed).
2018-12-18 15:03:22 +01:00
Lennart Poettering 3946d5762f test: add test case for read_nul_string() 2018-12-18 15:03:22 +01:00
Lennart Poettering 6baac700a8 tests: add a test that checks read_line() properly handles line endings at EOF
As requested here: https://github.com/systemd/systemd/pull/11129#discussion_r241588835
2018-12-14 12:57:32 +01:00
Lennart Poettering 838894b0c6 fileio: make read_line() handle various line endings correctly
This adds support for windows line endings.

More importantly though with this change a newline followed by EOF is
considered a single line end.
2018-12-14 09:12:17 +01:00
Lennart Poettering 686d13b9f2 util-lib: split out env file parsing code into env-file.c
It's quite complex, let's split this out.

No code changes, just some file rearranging.
2018-12-02 13:22:29 +01:00
Lennart Poettering e4de72876e util-lib: split out all temporary file related calls into tmpfiles-util.c
This splits out a bunch of functions from fileio.c that have to do with
temporary files. Simply to make the header files a bit shorter, and to
group things more nicely.

No code changes, just some rearranging of source files.
2018-12-02 13:22:29 +01:00
Lennart Poettering 928b3da0f1 test: remove unnecessary include 2018-12-02 13:22:29 +01:00
Lennart Poettering 13df9c398d fileio: automatically add NULL sentinel to parse_env_file()
Let's modernize things a bit.
2018-11-14 17:01:55 +01:00
Lennart Poettering aa8fbc74e3 fileio: drop "newline" parameter for env file parsers
Now that we don't (mis-)use the env file parser to parse kernel command
lines there's no need anymore to override the used newline character
set. Let's hence drop the argument and just "\n\r" always. This nicely
simplifies our code.
2018-11-14 17:01:54 +01:00
Zbigniew Jędrzejewski-Szmek d8351049a8 Introduce fmkostemp_safe and use it in tests
No functional change.
2018-10-26 12:56:51 +02:00
Zbigniew Jędrzejewski-Szmek 6d7c403324 tests: use a helper function to parse environment and open logging
The advantages are that we save a few lines, and that we can override
logging using environment variables in more test executables.
2018-09-14 09:29:57 +02:00
Lennart Poettering 0c69794138 tree-wide: remove Lennart's copyright lines
These lines are generally out-of-date, incomplete and unnecessary. With
SPDX and git repository much more accurate and fine grained information
about licensing and authorship is available, hence let's drop the
per-file copyright notice. Of course, removing copyright lines of others
is problematic, hence this commit only removes my own lines and leaves
all others untouched. It might be nicer if sooner or later those could
go away too, making git the only and accurate source of authorship
information.
2018-06-14 10:20:20 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Lennart Poettering 1a5a177eaf fileio: accept FILE* in addition to path in parse_env_file()
Most our other parsing functions do this, let's do this here too,
internally we accept that anyway. Also, the closely related
load_env_file() and load_env_file_pairs() also do this, so let's be
systematic.
2018-05-24 17:01:57 +02:00
Lennart Poettering c385b10a13
Merge pull request #8993 from keszybz/sd-resolve-coverity-and-related-fixes
sd-resolve coverity and related fixes
2018-05-18 20:30:12 +02:00
Zbigniew Jędrzejewski-Szmek 627d2bac24 fs-util,test: add helper to remove tempfiles
This simplifies the use of tempfiles in tests and fixes "leaked"
temporary files in test-fileio, test-catalog, test-conf-parser.

Not the whole tree is converted.
2018-05-18 20:11:31 +02:00
Zbigniew Jędrzejewski-Szmek dcd6361ea1 tree-wide: do not wrap assert_se in extra parentheses
We were inconsitently using them in some cases, but in majority not.
Using assignment in assert_se is very common, not an exception like in
'if', so let's drop the extra parens everywhere.
2018-05-14 17:22:22 +02:00
Lennart Poettering 5d13a15b1d tree-wide: drop spurious newlines (#8764)
Double newlines (i.e. one empty lines) are great to structure code. But
let's avoid triple newlines (i.e. two empty lines), quadruple newlines,
quintuple newlines, …, that's just spurious whitespace.

It's an easy way to drop 121 lines of code, and keeps the coding style
of our sources a bit tigther.
2018-04-19 12:13:23 +02:00
Zbigniew Jędrzejewski-Szmek 11a1589223 tree-wide: drop license boilerplate
Files which are installed as-is (any .service and other unit files, .conf
files, .policy files, etc), are left as is. My assumption is that SPDX
identifiers are not yet that well known, so it's better to retain the
extended header to avoid any doubt.

I also kept any copyright lines. We can probably remove them, but it'd nice to
obtain explicit acks from all involved authors before doing that.
2018-04-06 18:58:55 +02:00
Evegeny Vereshchagin 9a92e25545 tests: close a leftover file descriptor in test-fileio
This should make it a bit easier to search for real file descriptor leaks.

```
$ valgrind --leak-check=full --track-fds=yes ./build/test-fileio
...
==29457==
==29457== FILE DESCRIPTORS: 4 open at exit.
==29457== Open file descriptor 3: /tmp/test-systemd_writing_tmpfile.lyV5Rc
==29457==    at 0x4B9AD9E: open (open.c:43)
==29457==    by 0x4B19B24: __gen_tempname (tempname.c:261)
==29457==    by 0x4BA5CC3: mkostemp64 (mkostemp64.c:32)
==29457==    by 0x48F739B: mkostemp_safe (fileio.c:1206)
==29457==    by 0x10D968: test_writing_tmpfile (test-fileio.c:620)
==29457==    by 0x10E930: main (test-fileio.c:767)
==29457==
```
2018-03-05 22:17:23 +00:00
Evegeny Vereshchagin 362973902c tests: stop using freopen in test-fileio
This helps get around a bug confusing `glibc` and making the test bail
out with the following error under `asan` on `x86`:

Fatal error: glibc detected an invalid stdio handle
Aborted (core dumped)

The bug has been reported in https://github.com/google/sanitizers/issues/778,
but it is unlikely to be fixed anytime soon.
2018-03-05 21:57:14 +00:00
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