2018-03-03 14:23:13 +03:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
2018-05-01 11:12:56 +03:00
|
|
|
* Copyright (C) 2013-2018 K. Lange
|
2018-03-03 14:23:13 +03:00
|
|
|
*
|
|
|
|
* ps
|
|
|
|
*
|
|
|
|
* print a list of running processes
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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>
|
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;
|
2018-05-09 06:10:14 +03:00
|
|
|
static int collect_commandline = 0;
|
|
|
|
|
2018-09-14 15:11:26 +03:00
|
|
|
static int widths[] = {3,3,4,3,3,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;
|
2018-05-09 06:10:14 +03:00
|
|
|
char * process;
|
|
|
|
char * command_line;
|
|
|
|
};
|
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();
|
|
|
|
}
|
|
|
|
|
2018-05-09 06:10:14 +03:00
|
|
|
struct process * process_entry(struct dirent *dent) {
|
|
|
|
char tmp[256];
|
2018-03-03 14:23:13 +03:00
|
|
|
FILE * f;
|
|
|
|
char line[LINE_LEN];
|
|
|
|
|
2018-09-14 15:11:26 +03:00
|
|
|
int pid = 0, uid = 0, tgid = 0, mem = 0, shm = 0, vsz = 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);
|
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) {
|
|
|
|
if (tgid != pid) 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;
|
2018-05-09 06:10:14 +03:00
|
|
|
out->process = strdup(name);
|
|
|
|
out->command_line = NULL;
|
2018-03-03 14:23:13 +03:00
|
|
|
|
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;
|
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");
|
|
|
|
}
|
2018-09-14 14:40:05 +03:00
|
|
|
if (show_mem) {
|
2018-09-14 15:11:26 +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
|
|
|
}
|
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);
|
|
|
|
}
|
2018-09-14 14:40:05 +03:00
|
|
|
if (show_mem) {
|
2018-09-14 15:11:26 +03:00
|
|
|
char tmp[6];
|
|
|
|
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
|
|
|
}
|
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 */
|
2018-05-09 06:10:14 +03:00
|
|
|
char c;
|
|
|
|
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;
|
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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vim: tabstop=4
|
|
|
|
* vim: shiftwidth=4
|
|
|
|
* vim: noexpandtab
|
|
|
|
*/
|