plugins: refactor, break out inotify watcher to src/iwatch.[ch]

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-02-23 20:26:38 +01:00
1 parent 8381976f05
commit 8dc1ad00d5
4 files changed
+225 -112

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ finit_SOURCES = api.c cgroup.c cgroup.h \
exec.c finit.c finit.h \
getty.c stty.c \
helpers.c helpers.h \
iwatch.c iwatch.h \
log.c log.h \
mdadm.c mount.c \
pid.c pid.h \
+118
View File
@@ -0,0 +1,118 @@
#include <errno.h>
#include "finit.h"
#include "iwatch.h"
#include "log.h"
int iwatch_init(struct iwatch *iw)
{
if (!iw) {
errno = EINVAL;
return -1;
}
TAILQ_INIT(&iw->iwp_list);
iw->fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (iw->fd < 0) {
_pe("inotify_init()");
return -1;
}
return iw->fd;
}
void iwatch_exit(struct iwatch *iw)
{
struct iwatch_path *iwp, *tmp;
TAILQ_FOREACH_SAFE(iwp, &iw->iwp_list, link, tmp) {
TAILQ_REMOVE(&iw->iwp_list, iwp, link);
inotify_rm_watch(iw->fd, iwp->wd);
free(iwp->path);
free(iwp);
}
close(iw->fd);
}
int iwatch_add(struct iwatch *iw, char *file, uint32_t mask)
{
struct iwatch_path *iwp;
char *path;
int wd;
_d("Adding new watcher for path %s", file);
path = strdup(file);
if (!path) {
_pe("Out of memory");
return -1;
}
wd = inotify_add_watch(iw->fd, path, IWATCH_MASK | mask);
if (wd < 0) {
_pe("inotify_add_watch(%s)", path);
free(path);
return -1;
}
iwp = malloc(sizeof(struct iwatch_path));
if (!iwp) {
_pe("Failed allocating new `struct iwatch_path`");
inotify_rm_watch(iw->fd, wd);
free(path);
return -1;
}
iwp->path = path;
iwp->wd = wd;
TAILQ_INSERT_HEAD(&iw->iwp_list, iwp, link);
return 0;
}
int iwatch_del(struct iwatch *iw, struct iwatch_path *iwp)
{
_d("Removing watcher for removed path %s", iwp->path);
TAILQ_REMOVE(&iw->iwp_list, iwp, link);
inotify_rm_watch(iw->fd, iwp->wd);
free(iwp->path);
free(iwp);
return 0;
}
struct iwatch_path *iwatch_find_by_wd(struct iwatch *iw, int wd)
{
struct iwatch_path *iwp;
TAILQ_FOREACH(iwp, &iw->iwp_list, link) {
if (iwp->wd == wd)
return iwp;
}
return NULL;
}
struct iwatch_path *iwatch_find_by_path(struct iwatch *iw, const char *path)
{
struct iwatch_path *iwp;
TAILQ_FOREACH(iwp, &iw->iwp_list, link) {
if (!strcmp(iwp->path, path))
return iwp;
}
return NULL;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+64
View File
@@ -0,0 +1,64 @@
/* inotify watcher for files or directories
*
* Copyright (c) 2015-2016 Tobias Waldekranz <tobias@waldekranz.com>
* Copyright (c) 2016-2021 Joachim Wiberg <troglobit@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef FINIT_IWATCH_H_
#define FINIT_IWATCH_H_
#include <stdint.h>
#include <lite/queue.h>
#include <sys/inotify.h>
/* Monitors changes to both directories and files by default */
#define IWATCH_MASK (IN_CREATE | IN_DELETE | IN_MODIFY | IN_ATTRIB | IN_MOVE)
struct iwatch_path {
TAILQ_ENTRY(iwatch_path) link;
char *path;
int wd;
};
struct iwatch {
int fd;
TAILQ_HEAD(, iwatch_path) iwp_list;
};
int iwatch_init (struct iwatch *iw);
void iwatch_exit (struct iwatch *iw);
int iwatch_add (struct iwatch *iw, char *file, uint32_t mask);
int iwatch_del (struct iwatch *iw, struct iwatch_path *iwp);
struct iwatch_path *iwatch_find_by_wd (struct iwatch *iw, int wd);
struct iwatch_path *iwatch_find_by_path (struct iwatch *iw, const char *path);
#endif /* FINIT_IWATCH_H_ */
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/