mirror of
https://github.com/troglobit/finit.git
synced 2026-10-15 10:29:35 +00:00
plugins: refactor, break out inotify watcher to src/iwatch.[ch]
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
8381976f05
commit
8dc1ad00d5
4 files changed
+225
-112
No files matched your search
+42
-112
@@ -25,8 +25,6 @@
|
||||
#include <glob.h>
|
||||
#include <limits.h>
|
||||
#include <paths.h>
|
||||
#include <lite/queue.h>
|
||||
#include <sys/inotify.h>
|
||||
|
||||
#include "finit.h"
|
||||
#include "cond.h"
|
||||
@@ -34,28 +32,15 @@
|
||||
#include "pid.h"
|
||||
#include "plugin.h"
|
||||
#include "service.h"
|
||||
|
||||
struct iwatch_path {
|
||||
TAILQ_ENTRY(iwatch_path) link;
|
||||
char *path;
|
||||
int wd;
|
||||
};
|
||||
|
||||
struct iwatch {
|
||||
int fd;
|
||||
TAILQ_HEAD(, iwatch_path) iwp_list;
|
||||
};
|
||||
#include "iwatch.h"
|
||||
|
||||
static struct iwatch iw_pidfile;
|
||||
|
||||
static int iwatch_add(struct iwatch *iw, char *path)
|
||||
{
|
||||
struct iwatch_path *iwp;
|
||||
uint32_t mask = IN_ONLYDIR | IN_CREATE | IN_ATTRIB | IN_DELETE | IN_MODIFY | IN_MOVED_TO;
|
||||
char *ptr;
|
||||
int wd;
|
||||
|
||||
_d("pidfile: Adding new watcher for path %s", path);
|
||||
static int pidfile_add_path(struct iwatch *iw, char *path)
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
ptr = strstr(path, "run/");
|
||||
if (ptr) {
|
||||
char *slash;
|
||||
@@ -66,44 +51,16 @@ static int iwatch_add(struct iwatch *iw, char *path)
|
||||
ptr = slash++;
|
||||
slash = strchr(ptr, '/');
|
||||
if (slash) {
|
||||
_d("pidfile: Path too deep, skipping.");
|
||||
_d("Path too deep, skipping.");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wd = inotify_add_watch(iw->fd, path, mask);
|
||||
if (wd < 0) {
|
||||
_pe("inotify_add_watch()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
iwp = malloc(sizeof(struct iwatch_path));
|
||||
if (!iwp) {
|
||||
_pe("Failed allocating new `struct iwatch_path`");
|
||||
inotify_rm_watch(iw->fd, wd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
iwp->path = path;
|
||||
iwp->wd = wd;
|
||||
TAILQ_INSERT_HEAD(&iw->iwp_list, iwp, link);
|
||||
|
||||
return 0;
|
||||
return iwatch_add(iw, path, IN_ONLYDIR);
|
||||
}
|
||||
|
||||
static int iwatch_del(struct iwatch *iw, struct iwatch_path *iwp)
|
||||
{
|
||||
_d("pidfile: 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;
|
||||
}
|
||||
|
||||
static void update_conds(char *dir, char *name, uint32_t mask)
|
||||
static void pidfile_update_conds(char *dir, char *name, uint32_t mask)
|
||||
{
|
||||
char cond[MAX_COND_LEN];
|
||||
char fn[PATH_MAX];
|
||||
@@ -114,11 +71,11 @@ static void update_conds(char *dir, char *name, uint32_t mask)
|
||||
|
||||
svc = svc_find_by_pidfile(fn);
|
||||
if (!svc) {
|
||||
_d("pidfile: No matching svc for %s", fn);
|
||||
_d("No matching svc for %s", fn);
|
||||
return;
|
||||
}
|
||||
|
||||
_d("pidfile: Found svc %s for %s with pid %d", svc->name, fn, svc->pid);
|
||||
_d("Found svc %s for %s with pid %d", svc->name, fn, svc->pid);
|
||||
|
||||
mkcond(svc, cond, sizeof(cond));
|
||||
if (mask & (IN_CREATE | IN_ATTRIB | IN_MODIFY | IN_MOVED_TO)) {
|
||||
@@ -138,7 +95,7 @@ static void update_conds(char *dir, char *name, uint32_t mask)
|
||||
}
|
||||
|
||||
/* synthesize events in case of new run dirs */
|
||||
static void scan_for_pidfiles(struct iwatch *iw, char *dir, int len)
|
||||
static void pidfile_scandir(struct iwatch *iw, char *dir, int len)
|
||||
{
|
||||
glob_t gl;
|
||||
size_t i;
|
||||
@@ -157,47 +114,33 @@ static void scan_for_pidfiles(struct iwatch *iw, char *dir, int len)
|
||||
|
||||
for (i = 0; i < gl.gl_pathc; i++) {
|
||||
_d("scan found %s", gl.gl_pathv[i]);
|
||||
update_conds(dir, gl.gl_pathv[i], IN_CREATE);
|
||||
pidfile_update_conds(dir, gl.gl_pathv[i], IN_CREATE);
|
||||
}
|
||||
globfree(&gl);
|
||||
}
|
||||
|
||||
static void handle_dir(struct iwatch *iw, struct iwatch_path *iwp, char *name, int mask)
|
||||
/*
|
||||
* create/remove sub-directory in monitored directory
|
||||
*/
|
||||
static void pidfile_handle_dir(struct iwatch *iw, char *dir, char *name, int mask)
|
||||
{
|
||||
char *path;
|
||||
int plen;
|
||||
int exist = 0;
|
||||
char path[strlen(dir) + strlen(name) + 2];
|
||||
struct iwatch_path *iwp;
|
||||
|
||||
plen = snprintf(NULL, 0, "%s/%s", iwp->path, name) + 1;
|
||||
path = malloc(plen);
|
||||
if (!path) {
|
||||
_pe("Failed allocating path buffer for watcher");
|
||||
return;
|
||||
}
|
||||
snprintf(path, plen, "%s/%s", iwp->path, name);
|
||||
_d("pidfile: path is %s", path);
|
||||
snprintf(path, sizeof(path), "%s/%s", dir, name);
|
||||
_d("path: %s", path);
|
||||
|
||||
TAILQ_FOREACH(iwp, &iw->iwp_list, link) {
|
||||
if (!strcmp(iwp->path, path)) {
|
||||
exist = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
iwp = iwatch_find_by_path(iw, path);
|
||||
|
||||
if (mask & IN_CREATE) {
|
||||
if (!exist) {
|
||||
int rc = iwatch_add(iw, path);
|
||||
|
||||
scan_for_pidfiles(iw, path, plen);
|
||||
if (!rc)
|
||||
return;
|
||||
if (!iwp) {
|
||||
pidfile_add_path(iw, path);
|
||||
pidfile_scandir(iw, path, sizeof(path));
|
||||
}
|
||||
} else if (mask & IN_DELETE) {
|
||||
if (exist)
|
||||
if (iwp)
|
||||
iwatch_del(&iw_pidfile, iwp);
|
||||
}
|
||||
|
||||
free(path);
|
||||
}
|
||||
|
||||
static void pidfile_callback(void *arg, int fd, int events)
|
||||
@@ -213,14 +156,14 @@ static void pidfile_callback(void *arg, int fd, int events)
|
||||
return;
|
||||
}
|
||||
|
||||
_d("pidfile: Entering ... reading %zu bytes into ev_buf[]", buflen - 1);
|
||||
_d("Entering ... reading %zu bytes into ev_buf[]", buflen - 1);
|
||||
sz = read(fd, buf, buflen - 1);
|
||||
if (sz <= 0) {
|
||||
_pe("invalid inotify event");
|
||||
goto done;
|
||||
}
|
||||
buf[sz] = 0;
|
||||
_d("pidfile: Read %zd bytes, processing ...", sz);
|
||||
_d("Read %zd bytes, processing ...", sz);
|
||||
|
||||
off = 0;
|
||||
for (off = 0; off < sz; off += sizeof(*ev) + ev->len) {
|
||||
@@ -228,18 +171,17 @@ static void pidfile_callback(void *arg, int fd, int events)
|
||||
|
||||
ev = (struct inotify_event *)&buf[off];
|
||||
|
||||
_d("pidfile: path %s, event: 0x%08x", ev->name, ev->mask);
|
||||
_d("path %s, event: 0x%08x", ev->name, ev->mask);
|
||||
if (!ev->mask)
|
||||
continue;
|
||||
|
||||
/* Find base path for this event */
|
||||
TAILQ_FOREACH(iwp, &iw_pidfile.iwp_list, link) {
|
||||
if (iwp->wd == ev->wd)
|
||||
break;
|
||||
}
|
||||
iwp = iwatch_find_by_wd(&iw_pidfile, ev->wd);
|
||||
if (!iwp)
|
||||
continue;
|
||||
|
||||
if (ev->mask & IN_ISDIR) {
|
||||
handle_dir(&iw_pidfile, iwp, ev->name, ev->mask);
|
||||
pidfile_handle_dir(&iw_pidfile, iwp->path, ev->name, ev->mask);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -249,7 +191,7 @@ static void pidfile_callback(void *arg, int fd, int events)
|
||||
}
|
||||
|
||||
if (ev->mask & (IN_CREATE | IN_ATTRIB | IN_MODIFY | IN_MOVED_TO))
|
||||
update_conds(iwp->path, ev->name, ev->mask);
|
||||
pidfile_update_conds(iwp->path, ev->name, ev->mask);
|
||||
}
|
||||
done:
|
||||
free(buf);
|
||||
@@ -305,14 +247,10 @@ static void pidfile_init(void *arg)
|
||||
return;
|
||||
}
|
||||
|
||||
TAILQ_INIT(&iw_pidfile.iwp_list);
|
||||
if (iwatch_add(&iw_pidfile, path))
|
||||
close(iw_pidfile.fd);
|
||||
_d("pidfile monitor active");
|
||||
if (pidfile_add_path(&iw_pidfile, path))
|
||||
iwatch_exit(&iw_pidfile);
|
||||
}
|
||||
|
||||
static struct iwatch iw_pidfile;
|
||||
|
||||
/*
|
||||
* When performing an `initctl reload` with one (unchanged) service
|
||||
* depending on, e.g. `net/iface/lo`, its condition will not be set
|
||||
@@ -340,27 +278,19 @@ static plugin_t plugin = {
|
||||
|
||||
PLUGIN_INIT(plugin_init)
|
||||
{
|
||||
iw_pidfile.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||
if (iw_pidfile.fd < 0) {
|
||||
_pe("inotify_init()");
|
||||
return;
|
||||
}
|
||||
int fd;
|
||||
|
||||
plugin.io.fd = iw_pidfile.fd;
|
||||
fd = iwatch_init(&iw_pidfile);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
plugin.io.fd = fd;
|
||||
plugin_register(&plugin);
|
||||
}
|
||||
|
||||
PLUGIN_EXIT(plugin_exit)
|
||||
{
|
||||
struct iwatch_path *iwp, *tmp;
|
||||
|
||||
TAILQ_FOREACH_SAFE(iwp, &iw_pidfile.iwp_list, link, tmp) {
|
||||
inotify_rm_watch(iw_pidfile.fd, iwp->wd);
|
||||
free(iwp->path);
|
||||
free(iwp);
|
||||
}
|
||||
close(iw_pidfile.fd);
|
||||
|
||||
iwatch_exit(&iw_pidfile);
|
||||
plugin_unregister(&plugin);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user