diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2022-06-24 12:17:29 +1000 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2022-07-06 08:10:55 +0200 |
commit | dab2c6ee1f308af001dd4f57a13ec16e765f930b (patch) | |
tree | 4ba6045db5093293362c1697a3410574ea2aad7c /lineread.h | |
parent | 20c418f1f911f8a6c75b3d83fdab52ee4840640d (diff) | |
download | passt-dab2c6ee1f308af001dd4f57a13ec16e765f930b.tar passt-dab2c6ee1f308af001dd4f57a13ec16e765f930b.tar.gz passt-dab2c6ee1f308af001dd4f57a13ec16e765f930b.tar.bz2 passt-dab2c6ee1f308af001dd4f57a13ec16e765f930b.tar.lz passt-dab2c6ee1f308af001dd4f57a13ec16e765f930b.tar.xz passt-dab2c6ee1f308af001dd4f57a13ec16e765f930b.tar.zst passt-dab2c6ee1f308af001dd4f57a13ec16e765f930b.zip |
Add cleaner line-by-line reading primitives
Two places in passt need to read files line by line (one parsing
resolv.conf, the other parsing /proc/net/*. They can't use fgets()
because in glibc that can allocate memory. Instead they use an
implementation line_read() in util.c. This has some problems:
* It has two completely separate modes of operation, one buffering
and one not, the relation between these and how they're activated
is subtle and confusing
* At least in non-buffered mode, it will mishandle an empty line,
folding them onto the start of the next non-empty line
* In non-buffered mode it will use lseek() which prevents using this
on non-regular files (we don't need that at present, but it's a
surprising limitation)
* It has a lot of difficult to read pointer mangling
Add a new cleaner implementation of allocation-free line-by-line
reading in lineread.c. This one always buffers, using a state
structure to keep track of what we need. This is larger than I'd
like, but it turns out handling all the edge cases of line-by-line
reading in C is surprisingly hard.
This just adds the code, subsequent patches will change the existing
users of line_read() to the new implementation.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'lineread.h')
-rw-r--r-- | lineread.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lineread.h b/lineread.h new file mode 100644 index 0000000..b6faff5 --- /dev/null +++ b/lineread.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * Copyright Red Hat + * Author: David Gibson <david@gibson.dropbear.id.au> + */ + +#ifndef LINEREAD_H +#define LINEREAD_H + +#define LINEREAD_BUFFER_SIZE 8192 + +/** + * struct lineread - Line reader state + * @fd: File descriptor lines are read from + * @next_line: Offset in @buf of the start of the first line not yet + * returned by lineread_get() + * @count: Number of bytes in @buf read from the file, but not yet + * returned by lineread_get() + * @buf: Buffer storing data read from file. + */ +struct lineread { + int fd; int next_line; + int count; + + /* One extra byte for possible trailing \0 */ + char buf[LINEREAD_BUFFER_SIZE+1]; +}; + +void lineread_init(struct lineread *lr, int fd); +int lineread_get(struct lineread *lr, char **line); + +#endif /* _LINEREAD_H */ |