plugins: pidfile: rename variables and local fns

- new namespace before breaking out to shared object
- use inotify naming fd -> wd

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-02-23 09:04:53 +01:00
1 parent f4a0b99df8
commit c0bbb0fc51
1 file changed
+52 -52
+52 -52
View File
@@ -35,25 +35,25 @@
#include "plugin.h" #include "plugin.h"
#include "service.h" #include "service.h"
struct wd_entry { struct iwatch_path {
TAILQ_ENTRY(wd_entry) link; TAILQ_ENTRY(iwatch_path) link;
char *path; char *path;
int fd; int wd;
}; };
struct context { struct iwatch {
int fd; int fd;
TAILQ_HEAD(, wd_entry) wd_list; TAILQ_HEAD(, iwatch_path) iwp_list;
}; };
static struct context pidfile_ctx; static struct iwatch iw_pidfile;
static int watcher_add(struct context *ctx, char *path) static int iwatch_add(struct iwatch *iw, char *path)
{ {
struct wd_entry *wd; struct iwatch_path *iwp;
uint32_t mask = IN_ONLYDIR | IN_CREATE | IN_ATTRIB | IN_DELETE | IN_MODIFY | IN_MOVED_TO; uint32_t mask = IN_ONLYDIR | IN_CREATE | IN_ATTRIB | IN_DELETE | IN_MODIFY | IN_MOVED_TO;
char *ptr; char *ptr;
int fd; int wd;
_d("pidfile: Adding new watcher for path %s", path); _d("pidfile: Adding new watcher for path %s", path);
ptr = strstr(path, "run/"); ptr = strstr(path, "run/");
@@ -72,33 +72,33 @@ static int watcher_add(struct context *ctx, char *path)
} }
} }
fd = inotify_add_watch(ctx->fd, path, mask); wd = inotify_add_watch(iw->fd, path, mask);
if (fd < 0) { if (wd < 0) {
_pe("inotify_add_watch()"); _pe("inotify_add_watch()");
return -1; return -1;
} }
wd = malloc(sizeof(struct wd_entry)); iwp = malloc(sizeof(struct iwatch_path));
if (!wd) { if (!iwp) {
_pe("Failed allocating new `struct wd_entry`"); _pe("Failed allocating new `struct iwatch_path`");
inotify_rm_watch(ctx->fd, fd); inotify_rm_watch(iw->fd, wd);
return -1; return -1;
} }
wd->path = path; iwp->path = path;
wd->fd = fd; iwp->wd = wd;
TAILQ_INSERT_HEAD(&ctx->wd_list, wd, link); TAILQ_INSERT_HEAD(&iw->iwp_list, iwp, link);
return 0; return 0;
} }
static int watcher_del(struct context *ctx, struct wd_entry *wde) static int iwatch_del(struct iwatch *iw, struct iwatch_path *iwp)
{ {
_d("pidfile: Removing watcher for removed path %s", wde->path); _d("pidfile: Removing watcher for removed path %s", iwp->path);
TAILQ_REMOVE(&ctx->wd_list, wde, link); TAILQ_REMOVE(&iw->iwp_list, iwp, link);
inotify_rm_watch(ctx->fd, wde->fd); inotify_rm_watch(iw->fd, iwp->wd);
free(wde->path); free(iwp->path);
free(wde); free(iwp);
return 0; return 0;
} }
@@ -138,7 +138,7 @@ static void update_conds(char *dir, char *name, uint32_t mask)
} }
/* synthesize events in case of new run dirs */ /* synthesize events in case of new run dirs */
static void scan_for_pidfiles(struct context *ctx, char *dir, int len) static void scan_for_pidfiles(struct iwatch *iw, char *dir, int len)
{ {
glob_t gl; glob_t gl;
size_t i; size_t i;
@@ -162,23 +162,23 @@ static void scan_for_pidfiles(struct context *ctx, char *dir, int len)
globfree(&gl); globfree(&gl);
} }
static void handle_dir(struct context *ctx, struct wd_entry *wde, char *name, int mask) static void handle_dir(struct iwatch *iw, struct iwatch_path *iwp, char *name, int mask)
{ {
char *path; char *path;
int plen; int plen;
int exist = 0; int exist = 0;
plen = snprintf(NULL, 0, "%s/%s", wde->path, name) + 1; plen = snprintf(NULL, 0, "%s/%s", iwp->path, name) + 1;
path = malloc(plen); path = malloc(plen);
if (!path) { if (!path) {
_pe("Failed allocating path buffer for watcher"); _pe("Failed allocating path buffer for watcher");
return; return;
} }
snprintf(path, plen, "%s/%s", wde->path, name); snprintf(path, plen, "%s/%s", iwp->path, name);
_d("pidfile: path is %s", path); _d("pidfile: path is %s", path);
TAILQ_FOREACH(wde, &ctx->wd_list, link) { TAILQ_FOREACH(iwp, &iw->iwp_list, link) {
if (!strcmp(wde->path, path)) { if (!strcmp(iwp->path, path)) {
exist = 1; exist = 1;
break; break;
} }
@@ -186,15 +186,15 @@ static void handle_dir(struct context *ctx, struct wd_entry *wde, char *name, in
if (mask & IN_CREATE) { if (mask & IN_CREATE) {
if (!exist) { if (!exist) {
int rc = watcher_add(ctx, path); int rc = iwatch_add(iw, path);
scan_for_pidfiles(ctx, path, plen); scan_for_pidfiles(iw, path, plen);
if (!rc) if (!rc)
return; return;
} }
} else if (mask & IN_DELETE) { } else if (mask & IN_DELETE) {
if (exist) if (exist)
watcher_del(&pidfile_ctx, wde); iwatch_del(&iw_pidfile, iwp);
} }
free(path); free(path);
@@ -224,7 +224,7 @@ static void pidfile_callback(void *arg, int fd, int events)
off = 0; off = 0;
for (off = 0; off < sz; off += sizeof(*ev) + ev->len) { for (off = 0; off < sz; off += sizeof(*ev) + ev->len) {
struct wd_entry *wde; struct iwatch_path *iwp;
ev = (struct inotify_event *)&buf[off]; ev = (struct inotify_event *)&buf[off];
@@ -233,23 +233,23 @@ static void pidfile_callback(void *arg, int fd, int events)
continue; continue;
/* Find base path for this event */ /* Find base path for this event */
TAILQ_FOREACH(wde, &pidfile_ctx.wd_list, link) { TAILQ_FOREACH(iwp, &iw_pidfile.iwp_list, link) {
if (wde->fd == ev->wd) if (iwp->wd == ev->wd)
break; break;
} }
if (ev->mask & IN_ISDIR) { if (ev->mask & IN_ISDIR) {
handle_dir(&pidfile_ctx, wde, ev->name, ev->mask); handle_dir(&iw_pidfile, iwp, ev->name, ev->mask);
continue; continue;
} }
if (ev->mask & IN_DELETE) { if (ev->mask & IN_DELETE) {
_d("pidfile %s/%s removed ...", wde->path, ev->name); _d("pidfile %s/%s removed ...", iwp->path, ev->name);
continue; continue;
} }
if (ev->mask & (IN_CREATE | IN_ATTRIB | IN_MODIFY | IN_MOVED_TO)) if (ev->mask & (IN_CREATE | IN_ATTRIB | IN_MODIFY | IN_MOVED_TO))
update_conds(wde->path, ev->name, ev->mask); update_conds(iwp->path, ev->name, ev->mask);
} }
done: done:
free(buf); free(buf);
@@ -305,13 +305,13 @@ static void pidfile_init(void *arg)
return; return;
} }
TAILQ_INIT(&pidfile_ctx.wd_list); TAILQ_INIT(&iw_pidfile.iwp_list);
if (watcher_add(&pidfile_ctx, path)) if (iwatch_add(&iw_pidfile, path))
close(pidfile_ctx.fd); close(iw_pidfile.fd);
_d("pidfile monitor active"); _d("pidfile monitor active");
} }
static struct context pidfile_ctx; static struct iwatch iw_pidfile;
/* /*
* When performing an `initctl reload` with one (unchanged) service * When performing an `initctl reload` with one (unchanged) service
@@ -340,26 +340,26 @@ static plugin_t plugin = {
PLUGIN_INIT(plugin_init) PLUGIN_INIT(plugin_init)
{ {
pidfile_ctx.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); iw_pidfile.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (pidfile_ctx.fd < 0) { if (iw_pidfile.fd < 0) {
_pe("inotify_init()"); _pe("inotify_init()");
return; return;
} }
plugin.io.fd = pidfile_ctx.fd; plugin.io.fd = iw_pidfile.fd;
plugin_register(&plugin); plugin_register(&plugin);
} }
PLUGIN_EXIT(plugin_exit) PLUGIN_EXIT(plugin_exit)
{ {
struct wd_entry *wde, *tmp; struct iwatch_path *iwp, *tmp;
TAILQ_FOREACH_SAFE(wde, &pidfile_ctx.wd_list, link, tmp) { TAILQ_FOREACH_SAFE(iwp, &iw_pidfile.iwp_list, link, tmp) {
inotify_rm_watch(pidfile_ctx.fd, wde->fd); inotify_rm_watch(iw_pidfile.fd, iwp->wd);
free(wde->path); free(iwp->path);
free(wde); free(iwp);
} }
close(pidfile_ctx.fd); close(iw_pidfile.fd);
plugin_unregister(&plugin); plugin_unregister(&plugin);
} }