2021-11-26 06:41:56 +03:00
|
|
|
/**
|
|
|
|
* @brief Print a list of running processes.
|
2018-03-03 14:23:13 +03:00
|
|
|
*
|
2021-11-26 06:41:56 +03:00
|
|
|
* The listed processes are limited to ones owned by the current
|
|
|
|
* user and are listed in PID order. Various options allow for
|
|
|
|
* threads to be shown separately, extra information to be
|
|
|
|
* included in the output, etc.
|
2018-03-03 14:23:13 +03:00
|
|
|
*
|
2021-11-26 06:41:56 +03:00
|
|
|
* @copyright
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2013-2021 K. Lange
|
2018-03-03 14:23:13 +03:00
|
|
|
*/
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <toaru/list.h>
|
2021-09-03 05:43:36 +03:00
|
|
|
#include <toaru/hashmap.h>
|
2018-03-03 14:23:13 +03:00
|
|
|
|
|
|
|
#define LINE_LEN 4096
|
|
|
|
|
|
|
|
static int show_all = 0;
|
2018-05-09 06:10:14 +03:00
|
|
|
static int show_threads = 0;
|
|
|
|
static int show_username = 0;
|
2018-09-14 14:40:05 +03:00
|
|
|
static int show_mem = 0;
|
2021-09-03 04:17:10 +03:00
|
|
|
static int show_cpu = 0;
|
2022-04-18 04:06:10 +03:00
|
|
|
static int show_time = 0;
|
2018-05-09 06:10:14 +03:00
|
|
|
static int collect_commandline = 0;
|
|
|
|
|
2022-04-18 04:06:10 +03:00
|
|
|
static int widths[] = {3,3,4,3,3,4,4,4};
|
2018-05-09 06:10:14 +03:00
|
|
|
|
|
|
|
struct process {
|
|
|
|
int uid;
|
|
|
|
int pid;
|
|
|
|
int tid;
|
2018-09-14 14:40:05 +03:00
|
|
|
int mem;
|
2018-09-14 15:11:26 +03:00
|
|
|
int vsz;
|
2018-09-14 14:44:57 +03:00
|
|
|
int shm;
|
2021-09-03 04:17:10 +03:00
|
|
|
int cpu;
|
2022-04-18 04:06:10 +03:00
|
|
|
unsigned long time;
|
2018-05-09 06:10:14 +03:00
|
|
|
char * process;
|
|
|
|
char * command_line;
|
|
|
|
};
|
2018-03-03 14:23:13 +03:00
|
|
|
|
2021-09-03 05:43:36 +03:00
|
|
|
static hashmap_t * process_ents = NULL;
|
|
|
|
|
2018-03-03 14:23:13 +03:00
|
|
|
void print_username(int uid) {
|
|
|
|
struct passwd * p = getpwuid(uid);
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
printf("%-8s", p->pw_name);
|
|
|
|
} else {
|
|
|
|
printf("%-8d", uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:43:36 +03:00
|
|
|
struct process * process_from_pid(pid_t pid) {
|
|
|
|
return hashmap_get(process_ents, (void*)(uintptr_t)pid);
|
|
|
|
}
|
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
struct process * process_entry(struct dirent *dent) {
|
2021-05-31 04:47:02 +03:00
|
|
|
char tmp[300];
|
2018-03-03 14:23:13 +03:00
|
|
|
FILE * f;
|
|
|
|
char line[LINE_LEN];
|
|
|
|
|
2021-09-03 04:17:10 +03:00
|
|
|
int pid = 0, uid = 0, tgid = 0, mem = 0, shm = 0, vsz = 0, cpu = 0;
|
2022-04-18 04:06:10 +03:00
|
|
|
unsigned long ttime = 0;
|
2018-03-03 14:23:13 +03:00
|
|
|
char name[100];
|
|
|
|
|
|
|
|
sprintf(tmp, "/proc/%s/status", dent->d_name);
|
|
|
|
f = fopen(tmp, "r");
|
|
|
|
|
|
|
|
if (!f) {
|
2018-05-09 06:10:14 +03:00
|
|
|
return NULL;
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
line[0] = 0;
|
|
|
|
|
|
|
|
while (fgets(line, LINE_LEN, f) != NULL) {
|
|
|
|
char * n = strstr(line,"\n");
|
|
|
|
if (n) { *n = '\0'; }
|
|
|
|
char * tab = strstr(line,"\t");
|
|
|
|
if (tab) {
|
|
|
|
*tab = '\0';
|
|
|
|
tab++;
|
|
|
|
}
|
|
|
|
if (strstr(line, "Pid:") == line) {
|
|
|
|
pid = atoi(tab);
|
|
|
|
} else if (strstr(line, "Uid:") == line) {
|
|
|
|
uid = atoi(tab);
|
|
|
|
} else if (strstr(line, "Tgid:") == line) {
|
|
|
|
tgid = atoi(tab);
|
|
|
|
} else if (strstr(line, "Name:") == line) {
|
|
|
|
strcpy(name, tab);
|
2018-09-14 14:40:05 +03:00
|
|
|
} else if (strstr(line, "VmSize:") == line) {
|
2018-09-14 15:11:26 +03:00
|
|
|
vsz = atoi(tab);
|
2018-09-14 14:44:57 +03:00
|
|
|
} else if (strstr(line, "RssShmem:") == line) {
|
|
|
|
shm = atoi(tab);
|
2018-09-14 15:11:26 +03:00
|
|
|
} else if (strstr(line, "MemPermille:") == line) {
|
|
|
|
mem = atoi(tab);
|
2021-09-03 04:17:10 +03:00
|
|
|
} else if (strstr(line, "CpuPermille:") == line) {
|
|
|
|
cpu = atoi(tab);
|
2022-04-18 04:06:10 +03:00
|
|
|
} else if (strstr(line, "TotalTime:") == line) {
|
|
|
|
ttime = strtoul(tab,NULL,0);
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
if (!show_all) {
|
|
|
|
/* Filter not ours */
|
|
|
|
if (uid != getuid()) return NULL;
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
if (!show_threads) {
|
2021-09-03 05:43:36 +03:00
|
|
|
if (tgid != pid) {
|
|
|
|
/* Add this thread's CPU usage to the parent */
|
|
|
|
struct process * parent = process_from_pid(tgid);
|
|
|
|
if (parent) {
|
|
|
|
parent->cpu += cpu;
|
2022-04-18 04:06:10 +03:00
|
|
|
parent->time += ttime;
|
2021-09-03 05:43:36 +03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
struct process * out = malloc(sizeof(struct process));
|
|
|
|
out->uid = uid;
|
|
|
|
out->pid = tgid;
|
|
|
|
out->tid = pid;
|
2018-09-14 14:40:05 +03:00
|
|
|
out->mem = mem;
|
2018-09-14 14:44:57 +03:00
|
|
|
out->shm = shm;
|
2018-09-14 15:11:26 +03:00
|
|
|
out->vsz = vsz;
|
2021-09-03 04:17:10 +03:00
|
|
|
out->cpu = cpu;
|
2022-04-18 04:06:10 +03:00
|
|
|
out->time = ttime;
|
2018-05-09 06:10:14 +03:00
|
|
|
out->process = strdup(name);
|
|
|
|
out->command_line = NULL;
|
2018-03-03 14:23:13 +03:00
|
|
|
|
2021-09-03 05:43:36 +03:00
|
|
|
hashmap_set(process_ents, (void*)(uintptr_t)pid, out);
|
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
char garbage[1024];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if ((len = sprintf(garbage, "%d", out->pid)) > widths[0]) widths[0] = len;
|
|
|
|
if ((len = sprintf(garbage, "%d", out->tid)) > widths[1]) widths[1] = len;
|
2018-09-14 15:11:26 +03:00
|
|
|
if ((len = sprintf(garbage, "%d", out->vsz)) > widths[3]) widths[3] = len;
|
2018-09-14 14:44:57 +03:00
|
|
|
if ((len = sprintf(garbage, "%d", out->shm)) > widths[4]) widths[4] = len;
|
2018-09-14 15:11:26 +03:00
|
|
|
if ((len = sprintf(garbage, "%d.%01d", out->mem / 10, out->mem % 10)) > widths[5]) widths[5] = len;
|
2021-09-03 04:17:10 +03:00
|
|
|
if ((len = sprintf(garbage, "%d.%01d", out->cpu / 10, out->cpu % 10)) > widths[6]) widths[6] = len;
|
2022-04-18 04:18:01 +03:00
|
|
|
if ((len = sprintf(garbage, "%lu:%02lu.%02lu",
|
2022-04-18 04:06:10 +03:00
|
|
|
(out->time / (1000000UL * 60 * 60)),
|
|
|
|
(out->time / (1000000UL * 60)) % 60,
|
|
|
|
(out->time / (1000000UL)) % 60)) > widths[7]) widths[7] = len;
|
2018-05-09 06:10:14 +03:00
|
|
|
|
|
|
|
struct passwd * p = getpwuid(out->uid);
|
|
|
|
if (p) {
|
|
|
|
if ((len = strlen(p->pw_name)) > widths[2]) widths[2] = len;
|
|
|
|
} else {
|
|
|
|
if ((len = sprintf(garbage, "%d", out->uid)) > widths[2]) widths[2] = len;
|
|
|
|
}
|
|
|
|
endpwent();
|
|
|
|
|
|
|
|
if (collect_commandline) {
|
|
|
|
sprintf(tmp, "/proc/%s/cmdline", dent->d_name);
|
|
|
|
f = fopen(tmp, "r");
|
|
|
|
char foo[1024];
|
|
|
|
int s = fread(foo, 1, 1024, f);
|
|
|
|
if (s > 0) {
|
|
|
|
out->command_line = malloc(s + 1);
|
|
|
|
memset(out->command_line, 0, s + 1);
|
|
|
|
memcpy(out->command_line, foo, s);
|
|
|
|
|
|
|
|
for (int i = 0; i < s; ++i) {
|
|
|
|
if (out->command_line[i] == 30) {
|
|
|
|
out->command_line[i] = ' ';
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 14:23:13 +03:00
|
|
|
|
|
|
|
}
|
2018-05-09 06:10:14 +03:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_header(void) {
|
|
|
|
if (show_username) {
|
|
|
|
printf("%-*s ", widths[2], "USER");
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
2018-05-09 06:10:14 +03:00
|
|
|
printf("%*s ", widths[0], "PID");
|
2018-05-09 06:12:50 +03:00
|
|
|
if (show_threads) {
|
|
|
|
printf("%*s ", widths[1], "TID");
|
|
|
|
}
|
2021-09-03 04:17:10 +03:00
|
|
|
if (show_cpu) {
|
|
|
|
printf("%*s ", widths[6], "%CPU");
|
|
|
|
}
|
2018-09-14 14:40:05 +03:00
|
|
|
if (show_mem) {
|
2021-09-03 04:17:10 +03:00
|
|
|
printf("%*s ", widths[5], "%MEM");
|
2018-09-14 14:40:05 +03:00
|
|
|
printf("%*s ", widths[3], "VSZ");
|
2018-09-14 14:44:57 +03:00
|
|
|
printf("%*s ", widths[4], "SHM");
|
2018-09-14 14:40:05 +03:00
|
|
|
}
|
2022-04-18 04:06:10 +03:00
|
|
|
if (show_time) {
|
|
|
|
printf("%*s ", widths[7], "TIME");
|
|
|
|
}
|
2018-05-09 06:10:14 +03:00
|
|
|
printf("CMD\n");
|
|
|
|
}
|
2018-03-03 14:23:13 +03:00
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
void print_entry(struct process * out) {
|
|
|
|
if (show_username) {
|
|
|
|
struct passwd * p = getpwuid(out->uid);
|
|
|
|
if (p) {
|
|
|
|
printf("%-*s ", widths[2], p->pw_name);
|
|
|
|
} else {
|
|
|
|
printf("%-*d ", widths[2], out->uid);
|
|
|
|
}
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
printf("%*d ", widths[0], out->pid);
|
2018-05-09 06:12:50 +03:00
|
|
|
if (show_threads) {
|
|
|
|
printf("%*d ", widths[1], out->tid);
|
|
|
|
}
|
2021-09-03 04:17:10 +03:00
|
|
|
if (show_cpu) {
|
2021-09-03 05:43:36 +03:00
|
|
|
char tmp[10];
|
2021-09-03 04:17:10 +03:00
|
|
|
sprintf(tmp, "%*d.%01d", widths[6]-2, out->cpu / 10, out->cpu % 10);
|
|
|
|
printf("%*s ", widths[6], tmp);
|
|
|
|
}
|
2018-09-14 14:40:05 +03:00
|
|
|
if (show_mem) {
|
2021-09-03 05:43:36 +03:00
|
|
|
char tmp[10];
|
2018-09-14 15:11:26 +03:00
|
|
|
sprintf(tmp, "%*d.%01d", widths[5]-2, out->mem / 10, out->mem % 10);
|
|
|
|
printf("%*s ", widths[5], tmp);
|
|
|
|
printf("%*d ", widths[3], out->vsz);
|
2018-09-14 14:44:57 +03:00
|
|
|
printf("%*d ", widths[4], out->shm);
|
2018-09-14 14:40:05 +03:00
|
|
|
}
|
2022-04-18 04:06:10 +03:00
|
|
|
if (show_time) {
|
|
|
|
char tmp[30];
|
2022-04-18 04:18:01 +03:00
|
|
|
sprintf(tmp, "%lu:%02lu.%02lu",
|
2022-04-18 04:06:10 +03:00
|
|
|
(out->time / (1000000UL * 60 * 60)),
|
|
|
|
(out->time / (1000000UL * 60)) % 60,
|
|
|
|
(out->time / (1000000UL)) % 60);
|
|
|
|
|
|
|
|
printf("%*s ", widths[7], tmp);
|
|
|
|
}
|
2018-05-09 06:10:14 +03:00
|
|
|
if (out->command_line) {
|
|
|
|
printf("%s\n", out->command_line);
|
2018-03-03 14:23:13 +03:00
|
|
|
} else {
|
2018-05-09 06:10:14 +03:00
|
|
|
printf("%s\n", out->process);
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_usage(int argc, char * argv[]) {
|
|
|
|
printf(
|
|
|
|
"ps - list running processes\n"
|
|
|
|
"\n"
|
|
|
|
"usage: %s [-A] [format]\n"
|
|
|
|
"\n"
|
2018-05-09 06:10:14 +03:00
|
|
|
" -A \033[3mshow other users' processes\033[0m\n"
|
|
|
|
" -T \033[3mshow threads\033[0m\n"
|
2018-03-03 14:23:13 +03:00
|
|
|
" -? \033[3mshow this help text\033[0m\n"
|
2018-09-14 05:12:33 +03:00
|
|
|
"\n"
|
|
|
|
" [format] supports some BSD options:\n"
|
|
|
|
"\n"
|
|
|
|
" a \033[3mshow full command line\033[0m\n"
|
|
|
|
" u \033[3muse 'user-oriented' format\033[0m\n"
|
2018-03-03 14:23:13 +03:00
|
|
|
"\n", argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char * argv[]) {
|
|
|
|
|
|
|
|
/* Parse arguments */
|
2022-01-23 04:36:46 +03:00
|
|
|
int c;
|
2018-05-09 06:10:14 +03:00
|
|
|
while ((c = getopt(argc, argv, "AT?")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'A':
|
|
|
|
show_all = 1;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
show_threads = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
show_usage(argc, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 14:23:13 +03:00
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
if (optind < argc) {
|
|
|
|
char * show = argv[optind];
|
|
|
|
while (*show) {
|
|
|
|
switch (*show) {
|
|
|
|
case 'u':
|
|
|
|
show_username = 1;
|
2018-09-14 14:40:05 +03:00
|
|
|
show_mem = 1;
|
2021-09-03 04:17:10 +03:00
|
|
|
show_cpu = 1;
|
2022-04-18 04:06:10 +03:00
|
|
|
show_time = 1;
|
2018-05-09 06:10:14 +03:00
|
|
|
// fallthrough
|
|
|
|
case 'a':
|
|
|
|
collect_commandline = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
2018-05-09 06:10:14 +03:00
|
|
|
show++;
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the directory */
|
|
|
|
DIR * dirp = opendir("/proc");
|
|
|
|
|
|
|
|
/* Read the entries in the directory */
|
|
|
|
list_t * ents_list = list_create();
|
|
|
|
|
2021-09-03 05:43:36 +03:00
|
|
|
process_ents = hashmap_create_int(10);
|
|
|
|
|
2018-03-03 14:23:13 +03:00
|
|
|
struct dirent * ent = readdir(dirp);
|
|
|
|
while (ent != NULL) {
|
|
|
|
if (ent->d_name[0] >= '0' && ent->d_name[0] <= '9') {
|
2018-05-09 06:10:14 +03:00
|
|
|
struct process * p = process_entry(ent);
|
|
|
|
if (p) {
|
|
|
|
list_insert(ents_list, (void *)p);
|
|
|
|
}
|
2018-03-03 14:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ent = readdir(dirp);
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
print_header();
|
2018-03-03 14:23:13 +03:00
|
|
|
foreach(entry, ents_list) {
|
|
|
|
print_entry(entry->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|