1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-16 01:57:09 +00:00

Merge pull request #7 from alcazoid/master

Fix short process names in top
This commit is contained in:
Brenden Matthews 2013-04-09 16:43:17 -07:00
commit f0d0a03b8e

View File

@ -2584,8 +2584,10 @@ static void calc_io_each(void)
static void process_parse_stat(struct process *process)
{
char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
char cmdline[BUFFER_LEN] = { 0 }, cmdline_filename[BUFFER_LEN], cmdline_procname[BUFFER_LEN];
char tmpstr[BUFFER_LEN] = { 0 };
char state[4];
int ps;
int ps, cmdline_ps;
unsigned long user_time = 0;
unsigned long kernel_time = 0;
int rc;
@ -2596,6 +2598,7 @@ static void process_parse_stat(struct process *process)
struct stat process_stat;
snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
snprintf(cmdline_filename, sizeof(cmdline_filename), PROCFS_CMDLINE_TEMPLATE, process->pid);
ps = open(filename, O_RDONLY);
if (ps < 0) {
@ -2616,6 +2619,54 @@ static void process_parse_stat(struct process *process)
return;
}
/* Read /proc/<pid>/cmdline */
cmdline_ps = open(cmdline_filename, O_RDONLY);
if (cmdline_ps < 0) {
/* The process must have finished in the last few jiffies! */
return;
}
endl = read(cmdline_ps, cmdline, BUFFER_LEN - 1);
close(cmdline_ps);
if (endl < 0) {
return;
}
/* Some processes have null-separated arguments, let's fix it */
for(int i = 0; i < endl; i++)
if (cmdline[i] == 0)
cmdline[i] = ' ';
cmdline[endl] = 0;
/* We want to transform for example "/usr/bin/python program.py" to "python program.py"
* 1. search for first space
* 2. search for last / before first space
* 3. copy string from it's position */
char * space_ptr = strchr(cmdline, ' ');
if (space_ptr == NULL)
{
strcpy(tmpstr, cmdline);
}
else
{
long int space_pos = space_ptr - cmdline;
strncpy(tmpstr, cmdline, space_pos);
tmpstr[space_pos] = 0;
}
char * slash_ptr = strrchr(tmpstr, '/');
if (slash_ptr == NULL )
{
strncpy(cmdline_procname, cmdline, BUFFER_LEN);
}
else
{
long int slash_pos = slash_ptr - tmpstr;
strncpy(cmdline_procname, cmdline + slash_pos + 1, BUFFER_LEN - slash_pos);
cmdline_procname[BUFFER_LEN - slash_pos] = 0;
}
/* Extract cpu times from data in /proc filesystem */
lparen = strchr(line, '(');
rparen = strrchr(line, ')');
@ -2625,6 +2676,10 @@ static void process_parse_stat(struct process *process)
rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
strncpy(procname, lparen + 1, rc);
procname[rc] = '\0';
if (strlen(procname) < strlen(cmdline_procname))
strncpy(procname, cmdline_procname, strlen(cmdline_procname)+1);
rc = sscanf(rparen + 1, "%3s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
"%lu %*s %*s %*s %d %*s %*s %*s %llu %llu", state, &process->user_time,
&process->kernel_time, &nice_val, &process->vsize, &process->rss);