From 1be55e04ce0e85ff40a3c99eb74031f0934414d2 Mon Sep 17 00:00:00 2001 From: Anton Anikin Date: Fri, 11 May 2018 11:10:24 +0800 Subject: [PATCH] Fix shell name detection --- src/hstr_utils.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/hstr_utils.c b/src/hstr_utils.c index bd1e1cc..c8a9ac9 100644 --- a/src/hstr_utils.c +++ b/src/hstr_utils.c @@ -148,8 +148,16 @@ char *get_shell_name_by_ppid(const int pid) { char* name = (char*)calloc(PID_BUFFER_SIZE,sizeof(char)); if(name){ - sprintf(name, "/proc/%d/cmdline",pid); + // First we should try to open /proc/[pid]/comm (since Linux 2.6.33) and only if it missing + // then use /proc/[pid]/cmdline. Second way have problems in some cases - when for example + // we open session with using "su - [username]". Then /proc/[pid]/cmdline will contains + // "-su" instead real shell name, which is present in the /proc/[pid]/comm file. + sprintf(name, "/proc/%d/comm",pid); FILE* f = fopen(name,"r"); + if (!f){ + sprintf(name, "/proc/%d/cmdline",pid); + f = fopen(name,"r"); + } if(f){ size_t size = fread(name, sizeof(char), PID_BUFFER_SIZE-1, f); if(size>0){