diff options
author | Stefano Brivio <sbrivio@redhat.com> | 2021-10-15 22:39:17 +0200 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2021-10-16 00:49:33 +0200 |
commit | 1fd0c9b0e1f27098f9bf86e86285335e640317a5 (patch) | |
tree | fc0f069c77ae83e8c7225f4fbbdd0194a0dcfaea /util.c | |
parent | 14fe73e766206c71ab3c6d5ddc252435c36423bd (diff) | |
download | passt-1fd0c9b0e1f27098f9bf86e86285335e640317a5.tar passt-1fd0c9b0e1f27098f9bf86e86285335e640317a5.tar.gz passt-1fd0c9b0e1f27098f9bf86e86285335e640317a5.tar.bz2 passt-1fd0c9b0e1f27098f9bf86e86285335e640317a5.tar.lz passt-1fd0c9b0e1f27098f9bf86e86285335e640317a5.tar.xz passt-1fd0c9b0e1f27098f9bf86e86285335e640317a5.tar.zst passt-1fd0c9b0e1f27098f9bf86e86285335e640317a5.zip |
util, pasta: Don't read() and lseek() every single line in read_line()
...periodically checking bound ports becomes quite expensive
otherwise.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 27 |
1 files changed, 23 insertions, 4 deletions
@@ -368,8 +368,8 @@ int bitmap_isset(uint8_t *map, int bit) } /** - * line_read() - Same as fgets(), without using heap, a file instead of a stream - * @buf: Read buffer + * line_read() - Similar to fgets(), no heap usage, a file instead of a stream + * @buf: Read buffer: on non-empty string, use that instead of reading * @len: Maximum line length * @fd: File descriptor for reading * @@ -377,14 +377,31 @@ int bitmap_isset(uint8_t *map, int bit) */ char *line_read(char *buf, size_t len, int fd) { + int n, do_read = !*buf; char *p; - int n; + + if (!do_read) { + char *nl; + + buf[len - 1] = 0; + if (!strlen(buf)) + return NULL; + + p = buf + strlen(buf) + 1; + + if (!(nl = strchr(p, '\n'))) + return NULL; + *nl = 0; + + return memmove(buf, p, len - (p - buf)); + } n = read(fd, buf, --len); if (n <= 0) return NULL; buf[len] = 0; + if (!(p = strchr(buf, '\n'))) return buf; @@ -393,6 +410,7 @@ char *line_read(char *buf, size_t len, int fd) return buf; lseek(fd, (p - buf) - n + 1, SEEK_CUR); + return buf; } @@ -404,7 +422,7 @@ char *line_read(char *buf, size_t len, int fd) */ void procfs_scan_listen(char *name, uint8_t *map, uint8_t *exclude) { - char line[200], path[PATH_MAX]; + char line[BUFSIZ], path[PATH_MAX]; unsigned long port; unsigned int state; int fd; @@ -413,6 +431,7 @@ void procfs_scan_listen(char *name, uint8_t *map, uint8_t *exclude) if ((fd = open(path, O_RDONLY)) < 0) return; + *line = 0; line_read(line, sizeof(line), fd); while (line_read(line, sizeof(line), fd)) { if (sscanf(line, "%*u: %*x:%lx %*x:%*x %x", &port, &state) != 2) |