diff --git a/fatrace.c b/fatrace.c index f45607f..6064286 100644 --- a/fatrace.c +++ b/fatrace.c @@ -45,6 +45,11 @@ #define BUFSIZE 256*1024 +/* https://man7.org/linux/man-pages/man5/proc_pid_comm.5.html ; not defined in any include file */ +#ifndef TASK_COMM_LEN +#define TASK_COMM_LEN 16 +#endif + #define DEBUG 0 #if DEBUG #define debug(fmt, ...) fprintf (stderr, "DEBUG: " fmt "\n", ##__VA_ARGS__) @@ -231,7 +236,7 @@ print_event (const struct fanotify_event_metadata *data, { int event_fd = data->fd; static char printbuf[100]; - static char procname[100]; + static char procname[TASK_COMM_LEN]; static int procname_pid = -1; static char pathname[PATH_MAX]; bool got_procname = false; @@ -474,6 +479,11 @@ parse_args (int argc, char** argv) switch (c) { case 'C': option_comm = strdup (optarg); + /* see https://man7.org/linux/man-pages/man5/proc_pid_comm.5.html */ + if (strlen (option_comm) > TASK_COMM_LEN - 1) { + option_comm[TASK_COMM_LEN - 1] = '\0'; + warnx ("--command truncated to %i characters: %s", TASK_COMM_LEN - 1, option_comm); + } break; case 'c': diff --git a/tests/fatrace-comm b/tests/fatrace-comm new file mode 100755 index 0000000..00de5dc --- /dev/null +++ b/tests/fatrace-comm @@ -0,0 +1,54 @@ +#!/bin/sh +set -euC + +mkdir -m 777 tmp +trap "rm -rf tmp" EXIT INT QUIT PIPE + +LOG="$AUTOPKGTEST_TMP/fatrace.log" +echo "starting fatrace --command touch..." +fatrace --current-mount --command touch -s 2 -o $LOG & +sleep 1 + +echo "create files with different programs" +touch tmp/includeme +dd if=/dev/zero of=tmp/notme bs=1 count=1 + +echo "waiting for fatrace..." +wait + +echo "checking log..." +RC=0 +check_log() { + if ! grep -q "$1" $LOG; then + echo "$1 not found in log" >&2 + ((RC=RC+1)) + fi +} + +check_log "^touch([0-9]*).*includeme$" + +if grep -Eq "notme|^dd" $LOG; then + echo "notme found in log" >&2 + ((RC=RC+1)) +fi + +# exceeds TASK_COMM_LEN +rm $LOG +cp $(which touch) tmp/VeryLongTouchCommand +echo "starting fatrace --command VeryLongTouchCommand..." +fatrace --current-mount --command VeryLongTouchCommand -s 2 -o $LOG & +sleep 1 +tmp/VeryLongTouchCommand tmp/hello.txt +echo "waiting for fatrace..." +wait + +check_log "^VeryLongTouchCo([0-9]*).*hello.txt$" + +if [ $RC -ne 0 ]; then + echo "$RC checks failed -- log:" >&2 + echo "===================" >&2 + cat $LOG >&2 + echo "===================" >&2 +fi + +exit $RC diff --git a/tests/run b/tests/run index f4af9d4..fccf761 100755 --- a/tests/run +++ b/tests/run @@ -4,7 +4,7 @@ set -eu MYDIR=$(dirname $(readlink -f "$0")) export PATH=$(pwd):$PATH -for t in fatrace fatrace-currentmount fatrace-btrfs fatrace-user; do +for t in fatrace fatrace-currentmount fatrace-btrfs fatrace-user fatrace-comm; do export AUTOPKGTEST_TMP=$(mktemp -d) echo "===== $t ====" "$MYDIR"/$t