Skip to content

Commit

Permalink
Truncate overly long --command matches
Browse files Browse the repository at this point in the history
/proc/pid/comm is at most 15 characters (plus NUL), so specifying a
longer `--command` value will never match. Truncate it to the maximum
and warn about it.

Note that the manpage speaks about `TASK_COMM_LEN`, but that isn't
defined anywhere. So define it ourselves.

Fixes #33

[1] https://man7.org/linux/man-pages/man5/proc_pid_comm.5.html
  • Loading branch information
martinpitt committed Jan 19, 2025
1 parent 76563b9 commit 5b80e6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 11 additions & 1 deletion fatrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand Down
14 changes: 13 additions & 1 deletion tests/fatrace-comm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mkdir -m 777 tmp
trap "rm -rf tmp" EXIT INT QUIT PIPE

LOG="$AUTOPKGTEST_TMP/fatrace.log"
echo "starting fatrace..."
echo "starting fatrace --command touch..."
fatrace --current-mount --command touch -s 2 -o $LOG &
sleep 1

Expand All @@ -32,6 +32,18 @@ if grep -Eq "notme|^dd" $LOG; then
((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
Expand Down

0 comments on commit 5b80e6c

Please sign in to comment.