diff options
author | Lennart Poettering <lennart@poettering.net> | 2020-04-13 10:09:44 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2020-04-13 11:26:49 +0200 |
commit | 14f594b995bbaea85456a4c26e5c07446a4c446e (patch) | |
tree | 0ab2b1d668ea6d386167b2cc1a6104213f50562b /src/shared/conf-parser.c | |
parent | Merge pull request #15411 from ssahani/systemd-issue-15375 (diff) | |
download | systemd-14f594b995bbaea85456a4c26e5c07446a4c446e.tar.xz systemd-14f594b995bbaea85456a4c26e5c07446a4c446e.zip |
fileio: fileno() can realistically return -1
An stdio FILE* stream usually refers to something with a file
descriptor, but that's just "usually". It doesn't have to, when taking
fmemopen() and similar into account. Most of our calls to fileno()
assumed the call couldn't fail. In most cases this was correct, but in
some cases where we didn't know whether we work on files or memory we'd
use the returned fd as if it was unconditionally valid while it wasn't,
and passed it to a multitude of kernel syscalls. Let's fix that, and do
something reasonably smart when encountering this case.
(Running test-fileio with this patch applied will remove tons of ioctl()
calls on -1).
Diffstat (limited to 'src/shared/conf-parser.c')
-rw-r--r-- | src/shared/conf-parser.c | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 657df0a517..3ba33606fb 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -294,7 +294,7 @@ int config_parse(const char *unit, _cleanup_fclose_ FILE *ours = NULL; unsigned line = 0, section_line = 0; bool section_ignored = false, bom_seen = false; - int r; + int r, fd; assert(filename); assert(lookup); @@ -311,7 +311,9 @@ int config_parse(const char *unit, } } - fd_warn_permissions(filename, fileno(f)); + fd = fileno(f); + if (fd >= 0) /* stream might not have an fd, let's be careful hence */ + fd_warn_permissions(filename, fd); for (;;) { _cleanup_free_ char *buf = NULL; |