From: Lennart Poettering Date: Tue, 5 Oct 2021 12:44:17 +0000 (+0200) Subject: basic: split out inotify-related calls from fs-util.h → inotify-util.h X-Git-Tag: v250-rc1~566^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e5fd717994a4935490a0a0b04599d0ccbc66855;p=thirdparty%2Fsystemd.git basic: split out inotify-related calls from fs-util.h → inotify-util.h --- diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index bf771f2e2fb..a83f35a4c98 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -682,31 +682,6 @@ int unlink_or_warn(const char *filename) { return 0; } -int inotify_add_watch_fd(int fd, int what, uint32_t mask) { - int wd; - - /* This is like inotify_add_watch(), except that the file to watch is not referenced by a path, but by an fd */ - wd = inotify_add_watch(fd, FORMAT_PROC_FD_PATH(what), mask); - if (wd < 0) - return -errno; - - return wd; -} - -int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask) { - int wd; - - wd = inotify_add_watch(fd, pathname, mask); - if (wd < 0) { - if (errno == ENOSPC) - return log_error_errno(errno, "Failed to add a watch for %s: inotify watch limit reached", pathname); - - return log_error_errno(errno, "Failed to add a watch for %s: %m", pathname); - } - - return wd; -} - bool unsafe_transition(const struct stat *a, const struct stat *b) { /* Returns true if the transition from a to b is safe, i.e. that we never transition from unprivileged to * privileged files or directories. Why bother? So that unprivileged code can't symlink to privileged files diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index 7120ecd99db..61a6a81bf3c 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -67,21 +66,6 @@ int var_tmp_dir(const char **ret); int unlink_or_warn(const char *filename); -#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1) - -#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \ - for ((e) = &buffer.ev; \ - (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \ - (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len)) - -union inotify_event_buffer { - struct inotify_event ev; - uint8_t raw[INOTIFY_EVENT_MAX]; -}; - -int inotify_add_watch_fd(int fd, int what, uint32_t mask); -int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask); - enum { CHASE_PREFIX_ROOT = 1 << 0, /* The specified path will be prefixed by the specified root before beginning the iteration */ CHASE_NONEXISTENT = 1 << 1, /* It's OK if the path doesn't actually exist. */ diff --git a/src/basic/inotify-util.c b/src/basic/inotify-util.c new file mode 100644 index 00000000000..848f8590fab --- /dev/null +++ b/src/basic/inotify-util.c @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "fd-util.h" +#include "inotify-util.h" + +int inotify_add_watch_fd(int fd, int what, uint32_t mask) { + int wd; + + /* This is like inotify_add_watch(), except that the file to watch is not referenced by a path, but by an fd */ + wd = inotify_add_watch(fd, FORMAT_PROC_FD_PATH(what), mask); + if (wd < 0) + return -errno; + + return wd; +} + +int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask) { + int wd; + + wd = inotify_add_watch(fd, pathname, mask); + if (wd < 0) { + if (errno == ENOSPC) + return log_error_errno(errno, "Failed to add a watch for %s: inotify watch limit reached", pathname); + + return log_error_errno(errno, "Failed to add a watch for %s: %m", pathname); + } + + return wd; +} diff --git a/src/basic/inotify-util.h b/src/basic/inotify-util.h new file mode 100644 index 00000000000..88af08688f3 --- /dev/null +++ b/src/basic/inotify-util.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include +#include +#include +#include + +#define INOTIFY_EVENT_MAX (offsetof(struct inotify_event, name) + NAME_MAX + 1) + +#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \ + for ((e) = &buffer.ev; \ + (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \ + (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len)) + +union inotify_event_buffer { + struct inotify_event ev; + uint8_t raw[INOTIFY_EVENT_MAX]; +}; + +int inotify_add_watch_fd(int fd, int what, uint32_t mask); +int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask); diff --git a/src/basic/meson.build b/src/basic/meson.build index ac084ce60a3..0d8100a3913 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -72,6 +72,8 @@ basic_sources = files(''' hostname-util.h in-addr-util.c in-addr-util.h + inotify-util.c + inotify-util.h io-util.c io-util.h khash.c diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index d769423d6e9..33743f8c4d2 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -26,6 +26,7 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "inotify-util.h" #include "io-util.h" #include "log.h" #include "macro.h" diff --git a/src/core/cgroup.c b/src/core/cgroup.c index c19454e8bdd..a5770c03328 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -18,8 +18,8 @@ #include "cgroup.h" #include "fd-util.h" #include "fileio.h" -#include "fs-util.h" #include "in-addr-prefix-util.h" +#include "inotify-util.h" #include "io-util.h" #include "ip-protocol-list.h" #include "limits-util.h" diff --git a/src/core/manager.c b/src/core/manager.c index 2e7db509d1f..0b2e29ae148 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -44,9 +44,9 @@ #include "exit-status.h" #include "fd-util.h" #include "fileio.h" -#include "fs-util.h" #include "generator-setup.h" #include "hashmap.h" +#include "inotify-util.h" #include "install.h" #include "io-util.h" #include "label.h" diff --git a/src/core/path.c b/src/core/path.c index 693636b0ee4..3dd0206e7a8 100644 --- a/src/core/path.c +++ b/src/core/path.c @@ -11,8 +11,8 @@ #include "dbus-unit.h" #include "escape.h" #include "fd-util.h" -#include "fs-util.h" #include "glob-util.h" +#include "inotify-util.h" #include "macro.h" #include "mkdir.h" #include "path.h" diff --git a/src/libsystemd/sd-event/event-source.h b/src/libsystemd/sd-event/event-source.h index d2dc21470e9..08b409fef18 100644 --- a/src/libsystemd/sd-event/event-source.h +++ b/src/libsystemd/sd-event/event-source.h @@ -7,8 +7,8 @@ #include "sd-event.h" -#include "fs-util.h" #include "hashmap.h" +#include "inotify-util.h" #include "list.h" #include "prioq.h" #include "ratelimit.h" diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c index fbd2fb38bbf..8b7415f0db4 100644 --- a/src/libsystemd/sd-journal/sd-journal.c +++ b/src/libsystemd/sd-journal/sd-journal.c @@ -25,6 +25,7 @@ #include "hashmap.h" #include "hostname-util.h" #include "id128-util.h" +#include "inotify-util.h" #include "io-util.h" #include "journal-def.h" #include "journal-file.h" diff --git a/src/libsystemd/sd-network/sd-network.c b/src/libsystemd/sd-network/sd-network.c index ee93dae9e3a..dc48e918378 100644 --- a/src/libsystemd/sd-network/sd-network.c +++ b/src/libsystemd/sd-network/sd-network.c @@ -10,6 +10,7 @@ #include "env-file.h" #include "fd-util.h" #include "fs-util.h" +#include "inotify-util.h" #include "macro.h" #include "parse-util.h" #include "stdio-util.h" diff --git a/src/timesync/wait-sync.c b/src/timesync/wait-sync.c index 892746d5ee6..606b0160a41 100644 --- a/src/timesync/wait-sync.c +++ b/src/timesync/wait-sync.c @@ -14,7 +14,7 @@ #include "sd-event.h" #include "fd-util.h" -#include "fs-util.h" +#include "inotify-util.h" #include "main-func.h" #include "signal-util.h" #include "time-util.h" diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 839df8d6d29..37cfd8bb72c 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -24,8 +24,8 @@ #include "exit-status.h" #include "fd-util.h" #include "fileio.h" -#include "fs-util.h" #include "hashmap.h" +#include "inotify-util.h" #include "io-util.h" #include "macro.h" #include "main-func.h" diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 6df580d629a..b73f4776e70 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -40,6 +40,7 @@ #include "format-util.h" #include "fs-util.h" #include "hashmap.h" +#include "inotify-util.h" #include "io-util.h" #include "limits-util.h" #include "list.h"