diff --git a/fsev.h b/backend/devfsev.h similarity index 94% rename from fsev.h rename to backend/devfsev.h index c0b8091..004a014 100644 --- a/fsev.h +++ b/backend/devfsev.h @@ -2,7 +2,6 @@ #define INCLUDE_FM_FSEV_H /* - TODO: move this into backend/devfsev.h TODO: this stuff must be refactorized to not be fsev-specific TODO: because it is used from non-devfsev backends */ @@ -94,19 +93,19 @@ typedef struct kfs_event_arg { #if __LP64__ typedef struct fsevent_clone_args { - int8_t *event_list; - int32_t num_events; - int32_t event_queue_depth; + int8_t *event_list; + int32_t num_events; + int32_t event_queue_depth; int32_t *fd; } fsevent_clone_args; #else typedef struct fsevent_clone_args { - int8_t *event_list; - int32_t pad1; - int32_t num_events; - int32_t event_queue_depth; + int8_t *event_list; + int32_t pad1; + int32_t num_events; + int32_t event_queue_depth; int32_t *fd; - int32_t pad2; + int32_t pad2; } fsevent_clone_args; #endif diff --git a/backend/fanotify.c b/backend/fanotify.c index c94c6a5..837dffe 100644 --- a/backend/fanotify.c +++ b/backend/fanotify.c @@ -69,12 +69,12 @@ static void usr1_handler(int sig __attribute__((unused)), } static int handle_perm(int fan_fd, struct fanotify_event_metadata *metadata) { - struct fanotify_response response_struct; - int ret; - response_struct.fd = metadata->fd; - response_struct.response = FAN_ALLOW; - ret = write (fan_fd, &response_struct, sizeof(response_struct)); - return (ret<0)? ret: 0; + struct fanotify_response response_struct = { + .fd = metadata->fd, + .response = FAN_ALLOW + } + const int ret = write (fan_fd, &response_struct, sizeof (response_struct)); + return (ret < 0) ? ret : 0; } static bool parseFaEvent(FileMonitor *fm, struct fanotify_event_metadata *metadata, FileMonitorEvent *ev) { @@ -89,16 +89,21 @@ static bool parseFaEvent(FileMonitor *fm, struct fanotify_event_metadata *metada } path[path_len] = '\0'; //printf ("%s:", path); - } else strcpy (path, "."); - + } else { + strcpy (path, "."); + } ev->file = path; ev->pid = metadata->pid; ev->proc = get_proc_name (ev->pid, &ev->ppid); if (metadata->mask & FAN_ACCESS) { ev->type = FSE_STAT_CHANGED; } - if (metadata->mask & FAN_OPEN) ev->type = FSE_OPEN; - if (metadata->mask & FAN_MODIFY) ev->type = FSE_CONTENT_MODIFIED; + if (metadata->mask & FAN_OPEN) { + ev->type = FSE_OPEN; + } + if (metadata->mask & FAN_MODIFY) { + ev->type = FSE_CONTENT_MODIFIED; + } if (metadata->mask & FAN_CLOSE) { if (metadata->mask & FAN_CLOSE_WRITE) { ev->type = FSE_CREATE_FILE; // create @@ -147,12 +152,16 @@ static bool fm_loop (FileMonitor *fm, FileMonitorCallback cb) { eprintf ("Kernel fanotify version too old\n"); goto fail; } - if (!parseFaEvent (fm, metadata, &ev)) + if (!parseFaEvent (fm, metadata, &ev)) { goto fail; - if (ev.type != -1) cb (fm, &ev); + } + if (ev.type != -1) { + cb (fm, &ev); + } memset (&ev, 0, sizeof (ev)); - if (metadata->fd >= 0 && close (metadata->fd) != 0) + if (metadata->fd >= 0 && close (metadata->fd) != 0) { goto fail; + } metadata = FAN_EVENT_NEXT (metadata, len); } while (select (fan_fd + 1, &rfds, NULL, NULL, NULL) < 0) { @@ -180,39 +189,31 @@ static bool fm_begin (FileMonitor *fm) { sa.sa_flags = SA_SIGINFO | SA_RESTART; sigemptyset (&sa.sa_mask); sa.sa_sigaction = usr1_handler; - if (sigaction (SIGUSR1, &sa, NULL) == -1) { eprintf ("Cannot set SIGUSR1 signal handler\n"); - goto fail; + return false; } - fan_mask |= FAN_ONDIR; fan_mask |= FAN_EVENT_ON_CHILD; mark_flags |= FAN_MARK_MOUNT; // walk into subdirectories - init_flags |= (fan_mask & FAN_ALL_PERM_EVENTS) ? FAN_CLASS_CONTENT : FAN_CLASS_NOTIF; - if (!fm->root) { fm->root = "/"; } - fan_fd = fanotify_init (init_flags, O_RDONLY); // | O_LARGEFILE); - if (fan_fd < 0) - goto fail; - + if (fan_fd < 0) { + perror ("fanotify_init"); + return false; + } if (fanotify_mark (fan_fd, mark_flags, fan_mask, AT_FDCWD, fm->root) != 0) { perror ("fanotify_mark"); - return -1; + return false; } - FD_ZERO (&rfds); FD_SET (fan_fd, &rfds); - return 1; -fail: - perror ("fanotify"); - return 0; + return true; } static bool fm_end (FileMonitor *fm) { diff --git a/backend/kdebug.c b/backend/kdebug.c index 8ee724f..f0fbaf6 100644 --- a/backend/kdebug.c +++ b/backend/kdebug.c @@ -25,7 +25,7 @@ static bool fm_loop (FileMonitor *fm, FileMonitorCallback cb) { kdebug_env (fm, cb); for (; fm->running; ) { /* read events, run callback */ - bool rc = kdebug_loop_once (); + (void) kdebug_loop_once (); fflush (stdout); } return true; @@ -33,7 +33,6 @@ static bool fm_loop (FileMonitor *fm, FileMonitorCallback cb) { static bool fm_end(FileMonitor *fm) { return kdebug_stop (); - return false; } FileMonitorBackend fmb_kdebug = { diff --git a/backend/kdebug/kdebug.c b/backend/kdebug/kdebug.c index 9a099bc..1f33156 100755 --- a/backend/kdebug/kdebug.c +++ b/backend/kdebug/kdebug.c @@ -534,6 +534,7 @@ static char *parse_access(int arg2) { } #endif +#if WITH_MAIN static char *parse_openarg(int arg2) { static char mode[7]; @@ -554,6 +555,7 @@ static char *parse_openarg(int arg2) { if (arg2 & O_EXCL) mode[5] = 'e'; return mode; } +#endif static void set_pidcheck(int pid, int on_off) { kd_regtype kr; @@ -623,6 +625,7 @@ bool kdebug_stop() { return true; } +#if WITH_MAIN static void exit_usage(const char *myname) { eprintf ("Usage: %s [-e] [pid | cmd [pid | cmd] ...]\n", myname); eprintf (" -e exclude the specified list of pids from the sample\n"); @@ -630,10 +633,12 @@ static void exit_usage(const char *myname) { eprintf ("By default (no options) the following processes are excluded from the output:\n"); eprintf ("fs_usage, Terminal, telnetd, sshd, rlogind, tcsh, csh, sh\n\n"); } +#endif int filemgr_index(type) { - if (type & 0x10000) + if (type & 0x10000) { return (((type >> 2) & 0x3fff) + 256); + } return (((type >> 2) & 0x3fff)); } diff --git a/fsmon.h b/fsmon.h index 310e031..3576281 100644 --- a/fsmon.h +++ b/fsmon.h @@ -5,7 +5,7 @@ #include #include -#include "fsev.h" +#include "backend/devfsev.h" #include "util.h" #define eprintf(x,y...) fprintf(stderr,x,##y)