Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed Jul 1, 2016
1 parent 663a84a commit bb0df4d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
17 changes: 8 additions & 9 deletions fsev.h → backend/devfsev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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

Expand Down
57 changes: 29 additions & 28 deletions backend/fanotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions backend/kdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ 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;
}

static bool fm_end(FileMonitor *fm) {
return kdebug_stop ();
return false;
}

FileMonitorBackend fmb_kdebug = {
Expand Down
7 changes: 6 additions & 1 deletion backend/kdebug/kdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ static char *parse_access(int arg2) {
}
#endif

#if WITH_MAIN
static char *parse_openarg(int arg2) {
static char mode[7];

Expand All @@ -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;
Expand Down Expand Up @@ -623,17 +625,20 @@ 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");
eprintf ("\n%s will handle a maximum list of %d pids.\n\n", myname, MAX_PIDS);
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));
}

Expand Down
2 changes: 1 addition & 1 deletion fsmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <stdint.h>
#include <signal.h>
#include "fsev.h"
#include "backend/devfsev.h"
#include "util.h"

#define eprintf(x,y...) fprintf(stderr,x,##y)
Expand Down

0 comments on commit bb0df4d

Please sign in to comment.