1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
/*
* display.c - display ps output
* Copyright 1998-2003 by Albert Cahalan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <grp.h>
#include <locale.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include "../proc/alloc.h"
#include "../proc/readproc.h"
#include "../proc/sig.h"
#include "../proc/sysinfo.h"
#include "../proc/version.h"
#include "../proc/wchan.h"
#include "../include/fileutils.h"
#include "common.h"
#ifndef SIGCHLD
#define SIGCHLD SIGCLD
#endif
char *myname;
/* just reports a crash */
static void signal_handler(int signo){
if(signo==SIGPIPE) _exit(0); /* "ps | head" will cause this */
/* fprintf() is not reentrant, but we _exit() anyway */
fprintf(stderr,
_("Signal %d (%s) caught by %s (%s).\n"),
signo,
signal_number_to_name(signo),
myname,
procps_version
);
catastrophic_failure(__FILE__, __LINE__, _("please report this bug"));
}
/////////////////////////////////////////////////////////////////////////////////////
#undef DEBUG
#ifdef DEBUG
void init_stack_trace(char *prog_name);
#include <ctype.h>
void hex_dump(void *vp){
char *charlist;
int i = 0;
int line = 45;
char *cp = (char *)vp;
while(line--){
printf("%8lx ", (unsigned long)cp);
charlist = cp;
cp += 16;
for(i=0; i<16; i++){
if((charlist[i]>31) && (charlist[i]<127)){
printf("%c", charlist[i]);
}else{
printf(".");
}
}
printf(" ");
for(i=0; i<16; i++) printf(" %2x",(unsigned int)((unsigned char)(charlist[i])));
printf("\n");
i=0;
}
}
static void show_tgid(char *s, int n, sel_union *data){
printf("%s ", s);
while(--n){
printf("%d,", data[n].tgid);
}
printf("%d\n", data[0].tgid);
}
static void show_uid(char *s, int n, sel_union *data){
struct passwd *pw_data;
printf("%s ", s);
while(--n){
pw_data = getpwuid(data[n].uid);
if(pw_data) printf("%s,", pw_data->pw_name);
else printf("%d,", data[n].uid);
}
pw_data = getpwuid(data[n].uid);
if(pw_data) printf("%s\n", pw_data->pw_name);
else printf("%d\n", data[n].uid);
}
static void show_gid(char *s, int n, sel_union *data){
struct group *gr_data;
printf("%s ", s);
while(--n){
gr_data = getgrgid(data[n].gid);
if(gr_data) printf("%s,", gr_data->gr_name);
else printf("%d,", data[n].gid);
}
gr_data = getgrgid(data[n].gid);
if(gr_data) printf("%s\n", gr_data->gr_name);
else printf("%d\n", data[n].gid);
}
static void show_tty(char *s, int n, sel_union *data){
printf("%s ", s);
while(--n){
printf("%d:%d,", (int)major(data[n].tty), (int)minor(data[n].tty));
}
printf("%d:%d\n", (int)major(data[n].tty), (int)minor(data[n].tty));
}
static void show_cmd(char *s, int n, sel_union *data){
printf("%s ", s);
while(--n){
printf("%.8s,", data[n].cmd);
}
printf("%.8s\n", data[0].cmd);
}
static void arg_show(void){
selection_node *walk = selection_list;
while(walk){
switch(walk->typecode){
case SEL_RUID: show_uid("RUID", walk->n, walk->u); break;
case SEL_EUID: show_uid("EUID", walk->n, walk->u); break;
case SEL_SUID: show_uid("SUID", walk->n, walk->u); break;
case SEL_FUID: show_uid("FUID", walk->n, walk->u); break;
case SEL_RGID: show_gid("RGID", walk->n, walk->u); break;
case SEL_EGID: show_gid("EGID", walk->n, walk->u); break;
case SEL_SGID: show_gid("SGID", walk->n, walk->u); break;
case SEL_FGID: show_gid("FGID", walk->n, walk->u); break;
case SEL_PGRP: show_pid("PGRP", walk->n, walk->u); break;
case SEL_PID : show_pid("PID ", walk->n, walk->u); break;
case SEL_PPID: show_pid("PPID", walk->n, walk->u); break;
case SEL_TTY : show_tty("TTY ", walk->n, walk->u); break;
case SEL_SESS: show_pid("SESS", walk->n, walk->u); break;
case SEL_COMM: show_cmd("COMM", walk->n, walk->u); break;
default: printf("Garbage typecode value!\n");
}
walk = walk->next;
}
}
#endif
//////////////////////////////////////////////////////////////////////////
/***** check the header */
/* Unix98: must not print empty header */
static void check_headers(void){
format_node *walk = format_list;
int head_normal = 0;
if(header_type==HEAD_MULTI){
header_gap = screen_rows-1; /* true BSD */
return;
}
if(header_type==HEAD_NONE){
lines_to_next_header = -1; /* old Linux */
return;
}
while(walk){
if(!*(walk->name)){
walk = walk->next;
continue;
}
if(walk->pr){
head_normal++;
walk = walk->next;
continue;
}
walk = walk->next;
}
if(!head_normal) lines_to_next_header = -1; /* how UNIX does --noheader */
}
/***** check sort needs */
/* see what files need to be read, etc. */
static unsigned check_sort_needs(sort_node *walk){
unsigned needs = 0;
while(walk){
needs |= walk->need;
walk = walk->next;
}
return needs;
}
/***** check needs */
/* see what files need to be read, etc. */
static unsigned collect_format_needs(format_node *walk){
unsigned needs = 0;
while(walk){
needs |= walk->need;
walk = walk->next;
}
return needs;
}
static format_node *proc_format_list;
static format_node *task_format_list;
static unsigned needs_for_threads;
static unsigned needs_for_sort;
static unsigned proc_format_needs;
static unsigned task_format_needs;
#define needs_for_format (proc_format_needs|task_format_needs)
#define PROC_ONLY_FLAGS (PROC_FILLENV|PROC_FILLARG|PROC_FILLCOM|PROC_FILLMEM|PROC_FILLCGROUP)
/***** munge lists and determine openproc() flags */
static void lists_and_needs(void){
check_headers();
// only care about the difference when showing both
if(thread_flags & TF_show_both){
format_node pfn, tfn; // junk, to handle special case at begin of list
format_node *walk = format_list;
format_node *p_end = &pfn;
format_node *t_end = &tfn;
while(walk){
format_node *new = malloc(sizeof(format_node));
memcpy(new,walk,sizeof(format_node));
p_end->next = walk;
t_end->next = new;
p_end = walk;
t_end = new;
switch(walk->flags & CF_PRINT_MASK){
case CF_PRINT_THREAD_ONLY:
p_end->pr = pr_nop;
p_end->need = 0;
break;
case CF_PRINT_PROCESS_ONLY:
t_end->pr = pr_nop;
t_end->need = 0;
break;
default:
catastrophic_failure(__FILE__, __LINE__, _("please report this bug"));
// FALL THROUGH
case CF_PRINT_AS_NEEDED:
case CF_PRINT_EVERY_TIME:
break;
}
walk = walk->next;
}
t_end->next = NULL;
p_end->next = NULL;
proc_format_list = pfn.next;
task_format_list = tfn.next;
}else{
proc_format_list = format_list;
task_format_list = format_list;
}
proc_format_needs = collect_format_needs(proc_format_list);
task_format_needs = collect_format_needs(task_format_list);
needs_for_sort = check_sort_needs(sort_list);
// move process-only flags to the process
proc_format_needs |= (task_format_needs &~ PROC_ONLY_FLAGS);
task_format_needs &= ~PROC_ONLY_FLAGS;
if(bsd_c_option){
proc_format_needs &= ~PROC_FILLARG;
needs_for_sort &= ~PROC_FILLARG;
}
if(!unix_f_option){
proc_format_needs &= ~PROC_FILLCOM;
needs_for_sort &= ~PROC_FILLCOM;
}
// convert ARG to COM as a standard
if(proc_format_needs & PROC_FILLARG){
proc_format_needs |= (PROC_FILLCOM | PROC_EDITCMDLCVT);
proc_format_needs &= ~PROC_FILLARG;
}
if(bsd_e_option){
if(proc_format_needs&PROC_FILLCOM) proc_format_needs |= PROC_FILLENV;
}
/* FIXME broken filthy hack -- got to unify some stuff here */
if( ( (proc_format_needs|task_format_needs|needs_for_sort) & PROC_FILLWCHAN) && !wchan_is_number)
if (open_psdb(namelist_file)) wchan_is_number = 1;
if(thread_flags&TF_loose_tasks) needs_for_threads |= PROC_LOOSE_TASKS;
}
//////////////////////////////////////////////////////////////////////////
/***** fill in %CPU; not in libproc because of include_dead_children */
/* Note: for sorting, not display, so 0..0x7fffffff would be OK */
static int want_this_proc_pcpu(proc_t *buf){
unsigned long long used_jiffies;
unsigned long pcpu = 0;
unsigned long long seconds;
if(!want_this_proc(buf)) return 0;
used_jiffies = buf->utime + buf->stime;
if(include_dead_children) used_jiffies += (buf->cutime + buf->cstime);
seconds = seconds_since_boot - buf->start_time / Hertz;
if(seconds) pcpu = (used_jiffies * 1000ULL / Hertz) / seconds;
buf->pcpu = pcpu; // fits in an int, summing children on 128 CPUs
return 1;
}
/***** just display */
static void simple_spew(void){
static proc_t buf, buf2; // static avoids memset
PROCTAB* ptp;
ptp = openproc(needs_for_format | needs_for_sort | needs_for_select | needs_for_threads);
if(!ptp) {
fprintf(stderr, _("error: can not access /proc\n"));
exit(1);
}
switch(thread_flags & (TF_show_proc|TF_loose_tasks|TF_show_task)){
case TF_show_proc: // normal non-thread output
while(readproc(ptp,&buf)){
if(want_this_proc(&buf)){
show_one_proc(&buf, proc_format_list);
}
}
break;
case TF_show_proc|TF_loose_tasks: // H option
while(readproc(ptp,&buf)){
// must still have the process allocated
while(readtask(ptp,&buf,&buf2)){
if(!want_this_proc(&buf)) continue;
show_one_proc(&buf2, task_format_list);
}
}
break;
case TF_show_proc|TF_show_task: // m and -m options
while(readproc(ptp,&buf)){
if(want_this_proc(&buf)){
show_one_proc(&buf, proc_format_list);
// must still have the process allocated
while(readtask(ptp,&buf,&buf2)) show_one_proc(&buf2, task_format_list);
}
}
break;
case TF_show_task: // -L and -T options
while(readproc(ptp,&buf)){
if(want_this_proc(&buf)){
// must still have the process allocated
while(readtask(ptp,&buf,&buf2)) show_one_proc(&buf2, task_format_list);
}
}
break;
}
closeproc(ptp);
}
/***** forest output requires sorting by ppid; add start_time by default */
static void prep_forest_sort(void){
sort_node *tmp_list = sort_list;
const format_struct *incoming;
if(!sort_list) { /* assume start time order */
incoming = search_format_array("start_time");
if(!incoming) { fprintf(stderr, _("could not find start_time\n")); exit(1); }
tmp_list = malloc(sizeof(sort_node));
tmp_list->reverse = 0;
tmp_list->typecode = '?'; /* what was this for? */
tmp_list->sr = incoming->sr;
tmp_list->need = incoming->need;
tmp_list->next = sort_list;
sort_list = tmp_list;
}
/* this is required for the forest option */
incoming = search_format_array("ppid");
if(!incoming) { fprintf(stderr, _("could not find ppid\n")); exit(1); }
tmp_list = malloc(sizeof(sort_node));
tmp_list->reverse = 0;
tmp_list->typecode = '?'; /* what was this for? */
tmp_list->sr = incoming->sr;
tmp_list->need = incoming->need;
tmp_list->next = sort_list;
sort_list = tmp_list;
}
/* we rely on the POSIX requirement for zeroed memory */
//static proc_t *processes[98*1024]; // FIXME
static proc_t **processes;
/***** compare function for qsort */
static int compare_two_procs(const void *a, const void *b){
sort_node *tmp_list = sort_list;
while(tmp_list){
int result;
result = (*tmp_list->sr)(*(const proc_t *const*)a, *(const proc_t *const*)b);
if(result) return (tmp_list->reverse) ? -result : result;
tmp_list = tmp_list->next;
}
return 0; /* no conclusion */
}
/***** show pre-sorted array of process pointers */
static void show_proc_array(PROCTAB *restrict ptp, int n){
proc_t **p = processes;
while(n--){
if(thread_flags & TF_show_proc) show_one_proc(*p, proc_format_list);
if(thread_flags & TF_show_task){
static proc_t buf2; // static avoids memset
// must still have the process allocated
while(readtask(ptp,*p,&buf2)) show_one_proc(&buf2, task_format_list);
}
p++;
}
}
/***** show tree */
/* this needs some optimization work */
#define ADOPTED(x) 1
static void show_tree(const int self, const int n, const int level, const int have_sibling){
int i = 0;
if(level){
/* add prefix of "+" or "L" */
if(have_sibling) forest_prefix[level-1] = '+';
else forest_prefix[level-1] = 'L';
forest_prefix[level] = '\0';
}
show_one_proc(processes[self],format_list); /* first show self */
for(;;){ /* look for children */
if(i >= n) return; /* no children */
if(processes[i]->ppid == processes[self]->XXXID) break;
i++;
}
if(level){
/* change our prefix to "|" or " " for the children */
if(have_sibling) forest_prefix[level-1] = '|';
else forest_prefix[level-1] = ' ';
forest_prefix[level] = '\0';
}
for(;;){
int self_pid;
int more_children = 1;
if(i >= n) break; /* over the edge */
self_pid=processes[self]->XXXID;
if(i+1 >= n)
more_children = 0;
else
if(processes[i+1]->ppid != self_pid) more_children = 0;
if(self_pid==1 && ADOPTED(processes[i]) && forest_type!='u')
show_tree(i++, n, level, more_children);
else
show_tree(i++, n, level+1, more_children);
if(!more_children) break;
}
/* chop prefix that children added -- do we need this? */
forest_prefix[level] = '\0';
// memset(processes[self], '$', sizeof(proc_t)); /* debug */
}
/***** show forest */
static void show_forest(const int n){
int i = n;
int j;
while(i--){ /* cover whole array looking for trees */
j = n;
while(j--){ /* search for parent: if none, i is a tree! */
if(processes[j]->XXXID == processes[i]->ppid) goto not_root;
}
show_tree(i,n,0,0);
not_root:
;
}
/* don't free the array because it takes time and ps will exit anyway */
}
#if 0
static int want_this_proc_nop(proc_t *dummy){
(void)dummy;
return 1;
}
#endif
/***** sorted or forest */
static void fancy_spew(void){
proc_data_t *pd = NULL;
PROCTAB *restrict ptp;
int n = 0; /* number of processes & index into array */
ptp = openproc(needs_for_format | needs_for_sort | needs_for_select | needs_for_threads);
if(!ptp) {
fprintf(stderr, _("error: can not access /proc\n"));
exit(1);
}
if(thread_flags & TF_loose_tasks){
pd = readproctab3(want_this_proc_pcpu, ptp);
}else{
pd = readproctab2(want_this_proc_pcpu, (void*)0xdeadbeaful, ptp);
}
n = pd->n;
processes = pd->tab;
if(!n) return; /* no processes */
if(forest_type) prep_forest_sort();
qsort(processes, n, sizeof(proc_t*), compare_two_procs);
if(forest_type) show_forest(n);
else show_proc_array(ptp,n);
closeproc(ptp);
}
/***** no comment */
int main(int argc, char *argv[]){
atexit(close_stdout);
myname = strrchr(*argv, '/');
if (myname) ++myname; else myname = *argv;
setlocale (LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
#ifdef DEBUG
init_stack_trace(argv[0]);
#else
do {
struct sigaction sa;
int i = 32;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler;
sigfillset(&sa.sa_mask);
while(i--) switch(i){
default:
sigaction(i,&sa,NULL);
case 0:
case SIGINT: /* ^C */
case SIGTSTP: /* ^Z */
case SIGTTOU: /* see stty(1) man page */
case SIGQUIT: /* ^\ */
case SIGPROF: /* profiling */
case SIGKILL: /* can not catch */
case SIGSTOP: /* can not catch */
case SIGWINCH: /* don't care if window size changes */
;
}
} while (0);
#endif
reset_global(); /* must be before parser */
arg_parse(argc,argv);
/* arg_show(); */
trace("screen is %ux%u\n",screen_cols,screen_rows);
/* printf("sizeof(proc_t) is %d.\n", sizeof(proc_t)); */
trace("======= ps output follows =======\n");
init_output(); /* must be between parser and output */
lists_and_needs();
if(forest_type || sort_list) fancy_spew(); /* sort or forest */
else simple_spew(); /* no sort, no forest */
show_one_proc((proc_t *)-1,format_list); /* no output yet? */
return 0;
}
|