Skip to content

Commit

Permalink
Filter some chars in filenames in JSON mode
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed May 25, 2016
1 parent 7aa537b commit 5b14f83
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
6 changes: 4 additions & 2 deletions backend/devfsev.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ static bool fm_loop (FileMonitor *fm, FileMonitorCallback cb) {
ev.type = fme->type;
ev.pid = fme->val.u32;
ev.ppid = 0;
ev.proc = getProcName (ev.pid, &ev.ppid);
ev.proc = get_proc_name (ev.pid, &ev.ppid);
ev.file = (const char *)buf + buf_idx + sizeof (FMEventStruct);
}
/* parse data packet */
arg_len = parse_event (&ev, fme);
if (arg_len == -1) {
if (ev.pid && ev.type != -1 && cb) cb (fm, &ev);
if (ev.pid && ev.type != -1 && cb) {
cb (fm, &ev);
}
fsevent_free (&ev);
arg_len = 2;
} else if (arg_len < 1) {
Expand Down
2 changes: 1 addition & 1 deletion backend/inotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static bool parseFaEvent(FileMonitor *fm, struct fanotify_event_metadata *metada

ev->file = path;
ev->pid = metadata->pid;
ev->proc = getProcName (ev->pid, &ev->ppid);
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;
Expand Down
10 changes: 8 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ static bool callback(FileMonitor *fm, FileMonitorEvent *ev) {
return false;
}
if (fm->json) {
char *filename = fmu_jsonfilter (ev->file);
printf ("%s{\"filename\":\"%s\",\"pid\":%d,"
"\"uid\":%d,\"gid\":%d,",
firstnode? "":",", ev->file, ev->pid, ev->uid, ev->gid);
firstnode? "":",", filename, ev->pid, ev->uid, ev->gid);
firstnode = false;
free (filename);
if (ev->inode) {
printf ("\"inode\":%d,", ev->inode);
}
Expand All @@ -97,10 +99,14 @@ static bool callback(FileMonitor *fm, FileMonitorEvent *ev) {
printf ("\"ppid\":%d,", ev->ppid);
}
if (ev->proc && *ev->proc) {
char *proc = fmu_jsonfilter (ev->proc);
printf ("\"proc\":\"%s\",", ev->proc);
free (proc);
}
if (ev->newfile && *ev->newfile) {
printf ("\"newfile\":\"%s\",", ev->newfile);
char *filename = fmu_jsonfilter (ev->newfile);
printf ("\"newfile\":\"%s\",", filename);
free (filename);
}
printf ("\"type\":\"%s\"}", fm_typestr (ev->type));
} else {
Expand Down
21 changes: 20 additions & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const char *fm_colorstr(int type) {
return (type >= 0 && type < FSE_MAX_EVENTS)? colors[type]: "";
}

const char *getProcName(int pid, int *ppid) {
const char *get_proc_name(int pid, int *ppid) {
static char path[PATH_MAX] = {0};
#if __APPLE__
struct kinfo_proc * kinfo = (struct kinfo_proc*)&path;
Expand Down Expand Up @@ -193,3 +193,22 @@ bool copy_file(const char *src, const char *dst) {
(void) close (fd_dst);
return true;
}

static bool isPrintable(const char ch) {
if (ch == '"' || ch == '\\') {
return false;
}
return IS_PRINTABLE (ch);
}

char *fmu_jsonfilter(const char *s) {
char *r, *R = strdup (s);
for (r = R; *r; ) {
if (isPrintable (*r)) {
r++;
} else {
memmove (r, r + 1, strlen (r) + 1);
}
}
return R;
}
6 changes: 5 additions & 1 deletion util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

#include <stdbool.h>

#define IS_PRINTABLE(x) (x>=' ' && x<='~')

char *fmu_jsonfilter(const char *s);
const char *fm_argstr(int type);
const char *fm_typestr(int type);
const char *fm_colorstr(int type);
void hexdump(const uint8_t *buf, unsigned int len, int w);
const char * getProcName(int pid, int *ppid);
const char * get_proc_name(int pid, int *ppid);
bool is_directory (const char *str);
bool copy_file(const char *src, const char *dst);

Expand Down Expand Up @@ -42,4 +45,5 @@ bool copy_file(const char *src, const char *dst);
#define Color_BCYAN "\x1b[1;36m"
#define Color_BBLUE "\x1b[1;34m"
#define Color_BGRAY "\x1b[1;38m"

#endif

0 comments on commit 5b14f83

Please sign in to comment.