basic: split out inotify-related calls from fs-util.h → inotify-util.h

This commit is contained in:
Lennart Poettering
2021-10-05 16:14:37 +02:00
parent d8e32c471f
commit 9e5fd71799
15 changed files with 63 additions and 47 deletions
-25
View File
@@ -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
-16
View File
@@ -6,7 +6,6 @@
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@@ -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. */
+29
View File
@@ -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;
}
+22
View File
@@ -0,0 +1,22 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <inttypes.h>
#include <limits.h>
#include <stddef.h>
#include <sys/inotify.h>
#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);
+2
View File
@@ -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
+1
View File
@@ -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"