void display_process(struct p_tab *pp, int i) { int j; if (outflags & OF_F) fputs("0 ", stdout); if (outflags & OF_S) { putchar(mapstat(pp->p_status)); putchar(' '); } if (outflags & OF_UID) printf("%5d ", pp->p_uid); if (outflags & OF_PID) printf("%5d ", pp->p_pid); if (outflags & OF_PPID) printf("%5d ", ptab[ppid_slot[i]].p_tab.p_pid); if (outflags & OF_C) fputs(" 0 ", stdout); if (outflags & OF_PRI) printf("%3d ", pp->p_priority); if (outflags & OF_NI) printf("%3d ", pp->p_nice); if (outflags & OF_ADDR) fputs(" - ", stdout); if (outflags & OF_SZ) fputs(" - ", stdout); if (outflags & OF_WCHAN) { if (pp->p_status > 2) printf("%5d ", (unsigned int)pp->p_wait); else fputs(" - ", stdout); } /* We need to sort out the whole kernel and user handling of times in ptab verus udata here */ if (outflags & OF_STIME) fputs("00:00 ", stdout); /* FIXME */ if (outflags & OF_TTY) { if (!pp->p_tty) fputs(" ? ", stdout); else printf("%6s ", ttyshortname(pp->p_tty)); } if (outflags & OF_TIME) fputs("00:00:00 ", stdout); /* FIXME */ if (outflags & OF_CMD) { char name[9]; strncpy(name, pp->p_name, 8); name[8] = '\0'; for (j = 0; j < 8; ++j) { if (name[j] != 0) if (name[j] < ' ' || name[j] > 126) name[j] = '?'; } fputs(name, stdout); if (pp->p_status == P_ZOMBIE) fputs(" <defunct>", stdout); putchar('\n'); } }
int do_ps(void) { int i, j, uid, pfd, ptsize, nodesize; struct passwd *pwd; struct p_tab_buffer *ppbuf; struct p_tab *pp; static struct p_tab_buffer ptab[PTABSIZE]; static int ppid_slot[PTABSIZE]; static char name[10], uname[20]; uid = getuid(); if ((pfd = open("/dev/proc", O_RDONLY)) < 0) { perror("ps"); return 1; } if (ioctl(pfd, 2, (char *) &nodesize) != 0) { perror("ioctl"); close(pfd); return 1; } if (nodesize > sizeof(struct p_tab_buffer)) { fprintf(stderr, "kernel/user include mismatch.\n"); exit(1); } if (ioctl(pfd, 1, (char *) &ptsize) != 0) { perror("ioctl"); close(pfd); return 1; } if (ptsize > PTABSIZE) ptsize = PTABSIZE; for (i = 0; i < ptsize; ++i) { if (read(pfd, (char * ) &ptab[i], nodesize) != nodesize) { fprintf(stderr, "ps: error reading from /dev/proc\n"); close(pfd); return 1; } ppid_slot[i] = ptab[i].p_tab.p_pptr - ptab[0].p_tab.p_pptr; } close(pfd); if (!(flags & F_h)) { if (flags & F_n) printf(" PID\t PPID\t UID\tSTAT\tWCHAN\tALARM\tCOMMAND\n"); else printf("USER\t PID\t PPID\tSTAT\tWCHAN\tALARM\tCOMMAND\n"); } for (ppbuf = ptab, i = 0; i < ptsize; ++i, ++ppbuf) { pp = &ppbuf->p_tab; if (pp->p_status == 0) continue; if ((flags & F_r) && (pp->p_status != P_RUNNING && pp->p_status != P_READY)) continue; if (!(flags & F_a) && (pp->p_uid != uid)) continue; strncpy(name, pp->p_name, 8); name[8] = '\0'; for (j = 0; j < 8; ++j) { if (name[j] != 0) if (name[j] < ' ') name[j] = '?'; } if (flags & F_n) { printf("%5d\t%5d\t%5d\t%s\t%04x\t%-5d\t%s\n", pp->p_pid, ptab[ppid_slot[i]].p_tab.p_pid, pp->p_uid, mapstat(pp->p_status), pp->p_wait, pp->p_alarm, name); } else { pwd = getpwuid(pp->p_uid); if (pwd) strcpy(uname, pwd->pw_name); else sprintf(uname, "%d", pp->p_uid); printf("%s\t%5d\t%5d\t%s\t%04x\t%-5d\t%s\n", uname, pp->p_pid, ptab[ppid_slot[i]].p_tab.p_pid, mapstat(pp->p_status), pp->p_wait, pp->p_alarm, name); } } return 0; }
int do_test(char *input_buf) { int argc, seal_argc; char **argv, **argv_array; char *cmd; int i, bufsize = 512; char str_buf[512]; utf8string str; uid_t uid; gid_t gid; int stat; argv = 0; if (parse_input_line(input_buf, &argc, &argv) == 0) { printf(gettext("\n")); return (1); } /* * remember argv_array address, which is memory calloc'd by * parse_input_line, so it can be free'd at the end of the loop. */ argv_array = argv; if (argc < 1) { usage(); free(argv_array); return (0); } cmd = argv[0]; if (strcmp(cmd, "str2uid") == 0) { if (argc < 2) { usage(); free(argv_array); return (0); } str.utf8string_val = argv[1]; str.utf8string_len = strlen(argv[1]); stat = nfs_idmap_str_uid(&str, &uid); printf(gettext("%d stat=%s \n"), uid, mapstat(stat)); } else if (strcmp(cmd, "str2gid") == 0) { if (argc < 2) { usage(); free(argv_array); return (0); } str.utf8string_val = argv[1]; str.utf8string_len = strlen(argv[1]); stat = nfs_idmap_str_gid(&str, &gid); printf(gettext("%d stat=%s \n"), gid, mapstat(stat)); } else if (strcmp(cmd, "uid2str") == 0) { if (argc < 2) { usage(); free(argv_array); return (0); } uid = atoi(argv[1]); bzero(str_buf, bufsize); str.utf8string_val = str_buf; stat = nfs_idmap_uid_str(uid, &str); printf(gettext("%s stat=%s\n"), str.utf8string_val, mapstat(stat)); } else if (strcmp(cmd, "gid2str") == 0) { if (argc < 2) { usage(); free(argv_array); return (0); } gid = atoi(argv[1]); bzero(str_buf, bufsize); str.utf8string_val = str_buf; stat = nfs_idmap_gid_str(gid, &str); printf(gettext("%s stat=%s\n"), str.utf8string_val, mapstat(stat)); } else if (strcmp(cmd, "echo") == 0) { for (i = 1; i < argc; i++) printf("%s ", argv[i]); printf("\n"); } else if (strcmp(cmd, "exit") == 0 || strcmp(cmd, "quit") == 0) { printf(gettext("\n")); free(argv_array); return (1); } else usage(); /* free argv array */ free(argv_array); return (0); }
void display_process(struct p_tab *pp, int i) { int j; if (outflags & OF_F) fputs("0 ", stdout); if (outflags & OF_S) { putchar(mapstat(pp->p_status)); putchar(' '); } if (outflags & OF_UID) printf("%5d ", pp->p_uid); if (outflags & OF_PID) printf("%5d ", pp->p_pid); if (outflags & OF_PPID) printf("%5d ", ptab[ppid_slot[i]].p_tab.p_pid); if (outflags & OF_C) fputs(" 0 ", stdout); if (outflags & OF_PRI) printf("%3d ", pp->p_priority); if (outflags & OF_NI) printf("%3d ", pp->p_nice); if (outflags & OF_ADDR) fputs(" - ", stdout); if (outflags & OF_SZ) fputs(" - ", stdout); if (outflags & OF_WCHAN) { if (pp->p_status > 2) printf("%5d ", (unsigned int)pp->p_wait); else fputs(" - ", stdout); } /* We need to sort out the whole kernel and user handling of times in ptab verus udata here */ if (outflags & OF_STIME) { uint32_t t; t = time(NULL) - pp->p_stime; if ((t - pp->p_stime) > 86400) printf("%02day ", t / 86400); else { struct tm *tm; time_t x = pp->p_stime; tm = localtime(&x); printf("%02d:%02d ", tm->tm_hour, tm->tm_min); } } if (outflags & OF_TTY) { if (!pp->p_tty) fputs(" ? ", stdout); else printf("%6s ", ttyshortname(pp->p_tty)); } if (outflags & OF_TIME) { /* cstime/cutime or ctime/utime ? */ uint32_t c = pp->p_cstime + pp->p_cutime; uint8_t secs = c % 60; uint8_t mins; c -= secs; c /= 60; mins = c % 60; c -= mins; c /= 60; /* What to do if we exceed 99 hours ? */ printf("%02d:%02d:%02d ", c, mins, secs); } if (outflags & OF_CMD) { char name[9]; strncpy(name, pp->p_name, 8); name[8] = '\0'; for (j = 0; j < 8; ++j) { if (name[j] != 0) if (name[j] < ' ' || name[j] > 126) name[j] = '?'; } fputs(name, stdout); if (pp->p_status == P_ZOMBIE) fputs(" <defunct>", stdout); putchar('\n'); } }