From 33143b31800f3bfaaaea2ae857af9f01cd03fcf9 Mon Sep 17 00:00:00 2001 From: datenwolf <@datenwolf.net> Date: Tue, 18 Jan 2022 01:04:49 +0100 Subject: deal with empty cmdline properly --- pmon.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pmon.c b/pmon.c index b7a5efa..f693550 100644 --- a/pmon.c +++ b/pmon.c @@ -19,10 +19,12 @@ static char* pid_cmdline(pid_t const pid) snprintf(path, sizeof(path)-1, "/proc/%lu/cmdline", (long unsigned)pid); FILE *fil = fopen(path, "r"); if( !fil ){ return NULL; } - size_t cmdlen = 0; + size_t cmdbuflen = 0; char *cmdline = NULL; - getline(&cmdline, &cmdlen, fil); + ssize_t const cmdlen = getline(&cmdline, &cmdbuflen, fil); + fprintf(stderr, "cmdlen=%lu\n", (long unsigned)cmdlen); fclose(fil); + if( !cmdbuflen || 0 >= cmdlen ){ free(cmdline); cmdline = NULL; } return cmdline; } @@ -121,11 +123,13 @@ static int handle_proc_ev(int nl_sock) printf("set mcast listen ok\n"); break; case PROC_EVENT_FORK: - printf("fork: parent tid=%d pid=%d -> child tid=%d pid=%d\n", - nlcn_msg.proc_ev.event_data.fork.parent_pid, - nlcn_msg.proc_ev.event_data.fork.parent_tgid, - nlcn_msg.proc_ev.event_data.fork.child_pid, - nlcn_msg.proc_ev.event_data.fork.child_tgid); + cmdline = pid_cmdline(nlcn_msg.proc_ev.event_data.fork.parent_pid); + printf("fork: parent tid=%d pid=%d cmd='%s' -> child tid=%d pid=%d\n", + (int)nlcn_msg.proc_ev.event_data.fork.parent_pid, + (int)nlcn_msg.proc_ev.event_data.fork.parent_tgid, + cmdline, + (int)nlcn_msg.proc_ev.event_data.fork.child_pid, + (int)nlcn_msg.proc_ev.event_data.fork.child_tgid); break; case PROC_EVENT_EXEC: cmdline = pid_cmdline(nlcn_msg.proc_ev.event_data.exec.process_pid); -- cgit v1.2.3