2024-07-10 19:59:28 +00:00
|
|
|
#include <fcntl.h>
|
2024-07-01 10:23:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2024-07-05 16:54:49 +00:00
|
|
|
#include <pwd.h>
|
2024-07-10 13:59:36 +00:00
|
|
|
#include <time.h>
|
2024-07-10 19:59:28 +00:00
|
|
|
#include <limits.h>
|
2024-07-10 13:59:36 +00:00
|
|
|
#include "proc_parser.h"
|
|
|
|
#include "human.h"
|
2024-07-01 10:23:00 +00:00
|
|
|
|
2024-07-10 19:59:28 +00:00
|
|
|
static char c_flag;
|
|
|
|
|
2024-07-10 13:59:36 +00:00
|
|
|
static int pscan(const pid_t pid) {
|
|
|
|
struct mu_proc proc;
|
|
|
|
if (mu_proc_parser("ps", pid, &proc))
|
2024-07-05 16:54:49 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-07-10 13:59:36 +00:00
|
|
|
/* Uid */
|
|
|
|
char *name = "unknow";
|
|
|
|
struct passwd *pw = getpwuid(proc.uid);
|
|
|
|
if (pw != NULL)
|
|
|
|
name = pw->pw_name;
|
2024-07-05 16:54:49 +00:00
|
|
|
|
2024-07-10 13:59:36 +00:00
|
|
|
/* Time */
|
|
|
|
unsigned int rtime = (proc.utime + proc.stime) / sysconf(_SC_CLK_TCK);
|
2024-07-01 10:23:00 +00:00
|
|
|
|
2024-07-10 13:59:36 +00:00
|
|
|
/* Print */
|
|
|
|
char virt[MU_HUMAN_BUF_SIZE + 1];
|
2024-07-10 19:59:28 +00:00
|
|
|
strcpy(virt, mu_humansize(proc.vsize, 1024));
|
2024-07-05 16:54:49 +00:00
|
|
|
|
2024-07-10 13:59:36 +00:00
|
|
|
char rss[MU_HUMAN_BUF_SIZE + 1];
|
2024-07-10 19:59:28 +00:00
|
|
|
strcpy(rss, mu_humansize(proc.vmrss * 1024, 1024));
|
2024-07-05 16:54:49 +00:00
|
|
|
|
2024-07-23 08:33:47 +00:00
|
|
|
char *prog = (c_flag) ? proc.cmdline : proc.cmd;
|
|
|
|
if (prog[0] == '\0')
|
|
|
|
return 0;
|
2024-07-10 19:59:28 +00:00
|
|
|
|
|
|
|
printf("%6d %8s %4ld %4ld %8s %8s %2c %02um:%02us %2s\n", proc.pid, name, proc.priority, proc.nice, virt, rss, proc.state, rtime / 60, rtime % 60, prog);
|
2024-07-01 10:23:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2024-07-10 19:59:28 +00:00
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, argv, "c")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'c':
|
|
|
|
c_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
puts("ps [c] [PID]\n\t-c Print cmdline instead of program name");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-07-01 10:23:00 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 16:54:49 +00:00
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
2024-07-01 10:23:00 +00:00
|
|
|
|
2024-07-10 13:59:36 +00:00
|
|
|
puts(" PID USER PRI NICE VIRT RSS S RTIME CMD");
|
2024-07-01 10:23:00 +00:00
|
|
|
|
|
|
|
int ret = 0;
|
2024-07-05 16:54:49 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
DIR *dp = opendir("/proc");
|
|
|
|
if (dp == NULL) {
|
|
|
|
fprintf(stderr, "ps: /proc: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *ep;
|
2024-07-10 13:59:36 +00:00
|
|
|
while ((ep = readdir(dp)) != NULL) {
|
|
|
|
pid_t pid = strtoul(ep->d_name, 0L, 10);
|
|
|
|
if (pid)
|
|
|
|
if (pscan(pid))
|
2024-07-05 16:54:49 +00:00
|
|
|
ret = 1;
|
2024-07-10 13:59:36 +00:00
|
|
|
}
|
2024-07-05 16:54:49 +00:00
|
|
|
|
|
|
|
closedir(dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2024-07-10 13:59:36 +00:00
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
pid_t pid = strtoul(argv[i], 0L, 10);
|
|
|
|
if (pid)
|
|
|
|
if (pscan(pid))
|
2024-07-05 16:54:49 +00:00
|
|
|
ret = 1;
|
2024-07-10 13:59:36 +00:00
|
|
|
}
|
2024-07-05 16:54:49 +00:00
|
|
|
}
|
2024-07-01 10:23:00 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|