Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate overly long --command matches #35

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 54 additions & 0 deletions tests/fatrace-comm
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down