1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-07 18:27:04 +03:00

tmpfiles: modernize load_unix_sockets() a bit

Let's log in case of error.

Let's use read_line() instead of a fixed-size buffer

Let's make use of set_free_free()'s return value.
This commit is contained in:
Lennart Poettering 2018-01-10 17:28:03 +01:00
parent dfd14786b5
commit f1ff734fad

View File

@ -375,35 +375,47 @@ static struct Item* find_glob(OrderedHashmap *h, const char *match) {
static void load_unix_sockets(void) { static void load_unix_sockets(void) {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
char line[LINE_MAX]; int r;
if (unix_sockets) if (unix_sockets)
return; return;
/* We maintain a cache of the sockets we found in /* We maintain a cache of the sockets we found in /proc/net/unix to speed things up a little. */
* /proc/net/unix to speed things up a little. */
unix_sockets = set_new(&string_hash_ops); unix_sockets = set_new(&string_hash_ops);
if (!unix_sockets) if (!unix_sockets)
return; return;
f = fopen("/proc/net/unix", "re"); f = fopen("/proc/net/unix", "re");
if (!f) if (!f) {
return; log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
"Failed to open /proc/net/unix, ignoring: %m");
goto fail;
}
/* Skip header */ /* Skip header */
if (!fgets(line, sizeof(line), f)) r = read_line(f, LONG_LINE_MAX, NULL);
if (r < 0) {
log_warning_errno(r, "Failed to skip /proc/net/unix header line: %m");
goto fail; goto fail;
}
if (r == 0) {
log_warning("Premature end of file reading /proc/net/unix.");
goto fail;
}
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL;
char *p, *s; char *p, *s;
int k;
if (!fgets(line, sizeof(line), f)) r = read_line(f, LONG_LINE_MAX, &line);
if (r < 0) {
log_warning_errno(r, "Failed to read /proc/net/unix line, ignoring: %m");
goto fail;
}
if (r == 0) /* EOF */
break; break;
truncate_nl(line);
p = strchr(line, ':'); p = strchr(line, ':');
if (!p) if (!p)
continue; continue;
@ -420,21 +432,24 @@ static void load_unix_sockets(void) {
continue; continue;
s = strdup(p); s = strdup(p);
if (!s) if (!s) {
log_oom();
goto fail; goto fail;
}
path_kill_slashes(s); path_kill_slashes(s);
k = set_consume(unix_sockets, s); r = set_consume(unix_sockets, s);
if (k < 0 && k != -EEXIST) if (r < 0 && r != -EEXIST) {
log_warning_errno(r, "Failed to add AF_UNIX socket to set, ignoring: %m");
goto fail; goto fail;
} }
}
return; return;
fail: fail:
set_free_free(unix_sockets); unix_sockets = set_free_free(unix_sockets);
unix_sockets = NULL;
} }
static bool unix_socket_alive(const char *fn) { static bool unix_socket_alive(const char *fn) {