Changeset View
Changeset View
Standalone View
Standalone View
ksysguardd/Linux/ProcessList.c
Show First 20 Lines • Show All 187 Lines • ▼ Show 20 Line(s) | 111 | typedef struct { | |||
---|---|---|---|---|---|
188 | char cmdline[ 256 ]; | 188 | char cmdline[ 256 ]; | ||
189 | 189 | | |||
190 | /** The login name of the user that owns this process */ | 190 | /** The login name of the user that owns this process */ | ||
191 | char userName[ 32 ]; | 191 | char userName[ 32 ]; | ||
192 | 192 | | |||
193 | /** NoNewPrivileges: task can't gain higher privileges via setuid etc. */ | 193 | /** NoNewPrivileges: task can't gain higher privileges via setuid etc. */ | ||
194 | int noNewPrivileges; | 194 | int noNewPrivileges; | ||
195 | 195 | | |||
196 | /** cgroup: Linux process control group */ | ||||
197 | char *cGroup; | ||||
198 | | ||||
196 | } ProcessInfo; | 199 | } ProcessInfo; | ||
197 | 200 | | |||
198 | void getIOnice( int pid, ProcessInfo *ps ); | 201 | void getIOnice( int pid, ProcessInfo *ps ); | ||
199 | void ioniceProcess( const char* cmd ); | 202 | void ioniceProcess( const char* cmd ); | ||
200 | 203 | | |||
201 | static unsigned ProcessCount; | 204 | static unsigned ProcessCount; | ||
202 | static DIR* procDir; | 205 | static DIR* procDir; | ||
203 | static void validateStr( char* str ) | 206 | static void validateStr( char* str ) | ||
▲ Show 20 Lines • Show All 194 Lines • ▼ Show 20 Line(s) | 223 | { | |||
398 | /* find out user name with the process uid */ | 401 | /* find out user name with the process uid */ | ||
399 | uName = getCachedPWUID( ps->uid ); | 402 | uName = getCachedPWUID( ps->uid ); | ||
400 | strncpy( ps->userName, uName, sizeof( ps->userName ) - 1 ); | 403 | strncpy( ps->userName, uName, sizeof( ps->userName ) - 1 ); | ||
401 | ps->userName[ sizeof( ps->userName ) - 1 ] = '\0'; | 404 | ps->userName[ sizeof( ps->userName ) - 1 ] = '\0'; | ||
402 | validateStr( ps->userName ); | 405 | validateStr( ps->userName ); | ||
403 | 406 | | |||
404 | getIOnice(pid, ps); | 407 | getIOnice(pid, ps); | ||
405 | 408 | | |||
409 | /* CGroups */ | ||||
410 | snprintf( buf, BUFSIZE - 1, "/proc/%d/cgroup", pid ); | ||||
411 | buf[ BUFSIZE - 1 ] = '\0'; | ||||
412 | ps->cGroup = NULL; | ||||
413 | if ( ( fd = fopen( buf, "r" ) ) != 0 ) { | ||||
414 | do { | ||||
415 | fgets( buf, BUFSIZE, fd ); | ||||
416 | } while ( !feof( fd ) && buf[0] != '0' ); | ||||
417 | fclose ( fd ); | ||||
418 | | ||||
419 | if ( buf[0] == '0' && buf[1] == ':' && buf[2] == ':' ) { | ||||
420 | while (buf[strlen(buf) - 1] == '\n') | ||||
421 | buf[strlen(buf) - 1] = '\0'; /* trim trailing newlines */ | ||||
anthonyfieroniUnsubmitted Done anthonyfieroni: ```
for (int i = strlen(buf) - 1; buf[i] == '\n'; i++)
buf[i] = '\0';
``` | |||||
anthonyfieroni: I made mistake i mean `i--` we start from last. | |||||
topimiettinen: I also added a check for i not being negative. | |||||
422 | ps->cGroup = strdup( &buf[3] ); | ||||
anthonyfieroni: strdup allocates memory i don't see where you free it. | |||||
423 | } | ||||
424 | } | ||||
425 | | ||||
406 | return true; | 426 | return true; | ||
407 | } | 427 | } | ||
408 | 428 | | |||
409 | void printProcessList( const char* cmd) | 429 | void printProcessList( const char* cmd) | ||
410 | { | 430 | { | ||
411 | (void)cmd; | 431 | (void)cmd; | ||
412 | struct dirent* entry; | 432 | struct dirent* entry; | ||
413 | 433 | | |||
414 | ProcessInfo ps; | 434 | ProcessInfo ps; | ||
415 | ProcessCount = 0; | 435 | ProcessCount = 0; | ||
416 | rewinddir(procDir); | 436 | rewinddir(procDir); | ||
417 | while ( ( entry = readdir( procDir ) ) ) { | 437 | while ( ( entry = readdir( procDir ) ) ) { | ||
418 | if ( isdigit( entry->d_name[ 0 ] ) ) { | 438 | if ( isdigit( entry->d_name[ 0 ] ) ) { | ||
419 | long pid; | 439 | long pid; | ||
420 | pid = atol( entry->d_name ); | 440 | pid = atol( entry->d_name ); | ||
421 | if(getProcess( pid, &ps )) /* Print out the details of the process. Because of a stupid bug in kde3 ksysguard, make sure cmdline and tty are not empty */ | 441 | if(getProcess( pid, &ps )) /* Print out the details of the process. Because of a stupid bug in kde3 ksysguard, make sure cmdline and tty are not empty */ | ||
422 | output( "%s\t%ld\t%ld\t%lu\t%lu\t%s\t%lu\t%lu\t%d\t%lu\t%lu\t%lu\t%s\t%ld\t%s\t%s\t%d\t%d\t%d\n", | 442 | output( "%s\t%ld\t%ld\t%lu\t%lu\t%s\t%lu\t%lu\t%d\t%lu\t%lu\t%lu\t%s\t%ld\t%s\t%s\t%d\t%d\t%d\t%s\n", | ||
423 | ps.name, pid, (long)ps.ppid, | 443 | ps.name, pid, (long)ps.ppid, | ||
424 | (long)ps.uid, (long)ps.gid, ps.status, ps.userTime, | 444 | (long)ps.uid, (long)ps.gid, ps.status, ps.userTime, | ||
425 | ps.sysTime, ps.niceLevel, ps.vmSize, ps.vmRss, ps.vmURss, | 445 | ps.sysTime, ps.niceLevel, ps.vmSize, ps.vmRss, ps.vmURss, | ||
426 | (ps.userName[0]==0)?" ":ps.userName, (long)ps.tracerpid, | 446 | (ps.userName[0]==0)?" ":ps.userName, (long)ps.tracerpid, | ||
427 | (ps.tty[0]==0)?" ":ps.tty, (ps.cmdline[0]==0)?" ":ps.cmdline, | 447 | (ps.tty[0]==0)?" ":ps.tty, (ps.cmdline[0]==0)?" ":ps.cmdline, | ||
428 | ps.ioPriorityClass, ps.ioPriority, ps.noNewPrivileges | 448 | ps.ioPriorityClass, ps.ioPriority, ps.noNewPrivileges, ps.cGroup | ||
429 | ); | 449 | ); | ||
430 | } | 450 | } | ||
431 | } | 451 | } | ||
432 | output( "\n" ); | 452 | output( "\n" ); | ||
433 | return; | 453 | return; | ||
434 | } | 454 | } | ||
435 | 455 | | |||
436 | void getIOnice( int pid, ProcessInfo *ps ) { | 456 | void getIOnice( int pid, ProcessInfo *ps ) { | ||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Line(s) | 502 | { | |||
491 | 511 | | |||
492 | exitPWUIDCache(); | 512 | exitPWUIDCache(); | ||
493 | } | 513 | } | ||
494 | 514 | | |||
495 | void printProcessListInfo( const char* cmd ) | 515 | void printProcessListInfo( const char* cmd ) | ||
496 | { | 516 | { | ||
497 | (void)cmd; | 517 | (void)cmd; | ||
498 | output( "Name\tPID\tPPID\tUID\tGID\tStatus\tUser Time\tSystem Time\tNice\tVmSize" | 518 | output( "Name\tPID\tPPID\tUID\tGID\tStatus\tUser Time\tSystem Time\tNice\tVmSize" | ||
499 | "\tVmRss\tVmURss\tLogin\tTracerPID\tTTY\tCommand\tIO Priority Class\tIO Priority\tNNP\n" ); | 519 | "\tVmRss\tVmURss\tLogin\tTracerPID\tTTY\tCommand\tIO Priority Class\tIO Priority\tNNP\tCGroup\n" ); | ||
500 | output( "s\td\td\td\td\tS\td\td\td\tD\tD\tD\ts\td\ts\ts\td\td\td\n" ); | 520 | output( "s\td\td\td\td\tS\td\td\td\tD\tD\tD\ts\td\ts\ts\td\td\td\ts\n" ); | ||
501 | } | 521 | } | ||
502 | 522 | | |||
503 | void printProcessCount( const char* cmd ) | 523 | void printProcessCount( const char* cmd ) | ||
504 | { | 524 | { | ||
505 | (void)cmd; | 525 | (void)cmd; | ||
506 | struct dirent* entry; | 526 | struct dirent* entry; | ||
507 | ProcessCount = 0; | 527 | ProcessCount = 0; | ||
508 | rewinddir(procDir); | 528 | rewinddir(procDir); | ||
▲ Show 20 Lines • Show All 179 Lines • Show Last 20 Lines |