mirror of
https://github.com/Leonmmcoset/cleonos.git
synced 2026-04-24 19:34:01 +00:00
更好的进程系统
This commit is contained in:
96
cleonos/c/apps/bg_main.c
Normal file
96
cleonos/c/apps/bg_main.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "cmd_runtime.h"
|
||||
|
||||
static int ush_cmd_bg(const ush_state *sh, const char *arg) {
|
||||
char target[USH_PATH_MAX];
|
||||
char argv_line[USH_ARG_MAX];
|
||||
char env_line[USH_PATH_MAX + 32ULL];
|
||||
const char *rest = "";
|
||||
char path[USH_PATH_MAX];
|
||||
u64 pid;
|
||||
|
||||
if (sh == (const ush_state *)0 || arg == (const char *)0 || arg[0] == '\0') {
|
||||
ush_writeln("bg: usage bg <path|name> [args...]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ush_split_first_and_rest(arg, target, (u64)sizeof(target), &rest) == 0) {
|
||||
ush_writeln("bg: usage bg <path|name> [args...]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
argv_line[0] = '\0';
|
||||
if (rest != (const char *)0 && rest[0] != '\0') {
|
||||
ush_copy(argv_line, (u64)sizeof(argv_line), rest);
|
||||
}
|
||||
|
||||
if (ush_resolve_exec_path(sh, target, path, (u64)sizeof(path)) == 0) {
|
||||
ush_writeln("bg: invalid target");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ush_path_is_under_system(path) != 0) {
|
||||
ush_writeln("bg: /system/*.elf is kernel-mode (KELF), not user-exec");
|
||||
return 0;
|
||||
}
|
||||
|
||||
env_line[0] = '\0';
|
||||
ush_copy(env_line, (u64)sizeof(env_line), "PWD=");
|
||||
ush_copy(env_line + 4, (u64)(sizeof(env_line) - 4ULL), sh->cwd);
|
||||
|
||||
pid = cleonos_sys_spawn_pathv(path, argv_line, env_line);
|
||||
|
||||
if (pid == (u64)-1) {
|
||||
ush_writeln("bg: request failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ush_write("[");
|
||||
ush_write_hex_u64(pid);
|
||||
ush_write("] ");
|
||||
ush_writeln(path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cleonos_app_main(void) {
|
||||
ush_cmd_ctx ctx;
|
||||
ush_cmd_ret ret;
|
||||
ush_state sh;
|
||||
char initial_cwd[USH_PATH_MAX];
|
||||
int has_context = 0;
|
||||
int success = 0;
|
||||
const char *arg = "";
|
||||
|
||||
ush_zero(&ctx, (u64)sizeof(ctx));
|
||||
ush_zero(&ret, (u64)sizeof(ret));
|
||||
ush_init_state(&sh);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
|
||||
if (ush_command_ctx_read(&ctx) != 0) {
|
||||
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "bg") != 0) {
|
||||
has_context = 1;
|
||||
arg = ctx.arg;
|
||||
if (ctx.cwd[0] == '/') {
|
||||
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success = ush_cmd_bg(&sh, arg);
|
||||
|
||||
if (has_context != 0) {
|
||||
if (ush_streq(sh.cwd, initial_cwd) == 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_CWD;
|
||||
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
|
||||
}
|
||||
|
||||
if (sh.exit_requested != 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_EXIT;
|
||||
ret.exit_code = sh.exit_code;
|
||||
}
|
||||
|
||||
(void)ush_command_ret_write(&ret);
|
||||
}
|
||||
|
||||
return (success != 0) ? 0 : 1;
|
||||
}
|
||||
163
cleonos/c/apps/fg_main.c
Normal file
163
cleonos/c/apps/fg_main.c
Normal file
@@ -0,0 +1,163 @@
|
||||
#include "cmd_runtime.h"
|
||||
|
||||
static int ush_fg_is_user_path(const char *path) {
|
||||
if (path == (const char *)0 || path[0] != '/') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (path[1] == 's' && path[2] == 'y' && path[3] == 's' && path[4] == 't' && path[5] == 'e' && path[6] == 'm' &&
|
||||
(path[7] == '/' || path[7] == '\0')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (path[1] == 'd' && path[2] == 'r' && path[3] == 'i' && path[4] == 'v' && path[5] == 'e' && path[6] == 'r' &&
|
||||
(path[7] == '/' || path[7] == '\0')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ush_fg_pick_latest_job(u64 *out_pid) {
|
||||
u64 proc_count;
|
||||
u64 tty_active;
|
||||
u64 i;
|
||||
u64 best = 0ULL;
|
||||
|
||||
if (out_pid == (u64 *)0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*out_pid = 0ULL;
|
||||
proc_count = cleonos_sys_proc_count();
|
||||
tty_active = cleonos_sys_tty_active();
|
||||
|
||||
for (i = 0ULL; i < proc_count; i++) {
|
||||
u64 pid = 0ULL;
|
||||
cleonos_proc_snapshot snap;
|
||||
|
||||
if (cleonos_sys_proc_pid_at(i, &pid) == 0ULL || pid == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cleonos_sys_proc_snapshot(pid, &snap, (u64)sizeof(snap)) == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (snap.tty_index != tty_active) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ush_fg_is_user_path(snap.path) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (snap.state != CLEONOS_PROC_STATE_PENDING && snap.state != CLEONOS_PROC_STATE_RUNNING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (best == 0ULL || snap.pid > best) {
|
||||
best = snap.pid;
|
||||
}
|
||||
}
|
||||
|
||||
if (best == 0ULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*out_pid = best;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ush_fg_wait_pid(u64 pid) {
|
||||
for (;;) {
|
||||
u64 status = (u64)-1;
|
||||
u64 wait_ret = cleonos_sys_wait_pid(pid, &status);
|
||||
|
||||
if (wait_ret == (u64)-1) {
|
||||
ush_writeln("fg: pid not found");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (wait_ret == 1ULL) {
|
||||
ush_write("fg: done [");
|
||||
ush_write_hex_u64(pid);
|
||||
ush_writeln("]");
|
||||
if ((status & (1ULL << 63)) != 0ULL) {
|
||||
ush_print_kv_hex(" SIGNAL", status & 0xFFULL);
|
||||
ush_print_kv_hex(" VECTOR", (status >> 8) & 0xFFULL);
|
||||
ush_print_kv_hex(" ERROR", (status >> 16) & 0xFFFFULL);
|
||||
} else {
|
||||
ush_print_kv_hex(" STATUS", status);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
(void)cleonos_sys_sleep_ticks(1ULL);
|
||||
}
|
||||
}
|
||||
|
||||
static int ush_cmd_fg(const char *arg) {
|
||||
u64 pid = 0ULL;
|
||||
|
||||
if (arg != (const char *)0 && arg[0] != '\0') {
|
||||
if (ush_parse_u64_dec(arg, &pid) == 0 || pid == 0ULL) {
|
||||
ush_writeln("fg: usage fg [pid]");
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (ush_fg_pick_latest_job(&pid) == 0) {
|
||||
ush_writeln("fg: no active background job");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ush_write("fg: waiting [");
|
||||
ush_write_hex_u64(pid);
|
||||
ush_writeln("]");
|
||||
return ush_fg_wait_pid(pid);
|
||||
}
|
||||
|
||||
int cleonos_app_main(void) {
|
||||
ush_cmd_ctx ctx;
|
||||
ush_cmd_ret ret;
|
||||
ush_state sh;
|
||||
char initial_cwd[USH_PATH_MAX];
|
||||
int has_context = 0;
|
||||
int success = 0;
|
||||
const char *arg = "";
|
||||
|
||||
ush_zero(&ctx, (u64)sizeof(ctx));
|
||||
ush_zero(&ret, (u64)sizeof(ret));
|
||||
ush_init_state(&sh);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
|
||||
if (ush_command_ctx_read(&ctx) != 0) {
|
||||
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "fg") != 0) {
|
||||
has_context = 1;
|
||||
arg = ctx.arg;
|
||||
if (ctx.cwd[0] == '/') {
|
||||
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success = ush_cmd_fg(arg);
|
||||
|
||||
if (has_context != 0) {
|
||||
if (ush_streq(sh.cwd, initial_cwd) == 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_CWD;
|
||||
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
|
||||
}
|
||||
|
||||
if (sh.exit_requested != 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_EXIT;
|
||||
ret.exit_code = sh.exit_code;
|
||||
}
|
||||
|
||||
(void)ush_command_ret_write(&ret);
|
||||
}
|
||||
|
||||
return (success != 0) ? 0 : 1;
|
||||
}
|
||||
@@ -27,8 +27,10 @@ static int ush_cmd_help(void) {
|
||||
ush_writeln(" mv <src> <dst> (/temp only)");
|
||||
ush_writeln(" rm <path> (/temp only)");
|
||||
ush_writeln(" pid");
|
||||
ush_writeln(" spawn <path|name> [args...]");
|
||||
ush_writeln(" wait <pid>");
|
||||
ush_writeln(" spawn <path|name> [args...] / bg <path|name> [args...]");
|
||||
ush_writeln(" wait <pid> / fg [pid]");
|
||||
ush_writeln(" kill <pid> [signal]");
|
||||
ush_writeln(" jobs [-a] / ps [-a] [-u] / top [--once] [-n loops] [-d ticks]");
|
||||
ush_writeln(" sleep <ticks>");
|
||||
ush_writeln(" yield");
|
||||
ush_writeln(" shutdown / restart");
|
||||
|
||||
142
cleonos/c/apps/jobs_main.c
Normal file
142
cleonos/c/apps/jobs_main.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#include "cmd_runtime.h"
|
||||
|
||||
static int ush_jobs_is_user_path(const char *path) {
|
||||
if (path == (const char *)0 || path[0] != '/') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (path[1] == 's' && path[2] == 'y' && path[3] == 's' && path[4] == 't' && path[5] == 'e' && path[6] == 'm' &&
|
||||
(path[7] == '/' || path[7] == '\0')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (path[1] == 'd' && path[2] == 'r' && path[3] == 'i' && path[4] == 'v' && path[5] == 'e' && path[6] == 'r' &&
|
||||
(path[7] == '/' || path[7] == '\0')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *ush_jobs_state_name(u64 state) {
|
||||
if (state == CLEONOS_PROC_STATE_PENDING) {
|
||||
return "PENDING";
|
||||
}
|
||||
if (state == CLEONOS_PROC_STATE_RUNNING) {
|
||||
return "RUNNING";
|
||||
}
|
||||
if (state == CLEONOS_PROC_STATE_EXITED) {
|
||||
return "EXITED ";
|
||||
}
|
||||
return "UNUSED ";
|
||||
}
|
||||
|
||||
static int ush_cmd_jobs(const char *arg) {
|
||||
u64 proc_count;
|
||||
u64 tty_active = cleonos_sys_tty_active();
|
||||
u64 i;
|
||||
u64 shown = 0ULL;
|
||||
int include_exited = 0;
|
||||
|
||||
if (arg != (const char *)0 && arg[0] != '\0') {
|
||||
if (ush_streq(arg, "-a") != 0 || ush_streq(arg, "--all") != 0) {
|
||||
include_exited = 1;
|
||||
} else {
|
||||
ush_writeln("jobs: usage jobs [-a|--all]");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
proc_count = cleonos_sys_proc_count();
|
||||
ush_writeln("jobs:");
|
||||
|
||||
for (i = 0ULL; i < proc_count; i++) {
|
||||
u64 pid = 0ULL;
|
||||
cleonos_proc_snapshot snap;
|
||||
const char *state_name;
|
||||
|
||||
if (cleonos_sys_proc_pid_at(i, &pid) == 0ULL || pid == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cleonos_sys_proc_snapshot(pid, &snap, (u64)sizeof(snap)) == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ush_jobs_is_user_path(snap.path) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (snap.tty_index != tty_active) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (include_exited == 0 && snap.state == CLEONOS_PROC_STATE_EXITED) {
|
||||
continue;
|
||||
}
|
||||
|
||||
state_name = ush_jobs_state_name(snap.state);
|
||||
ush_write("[");
|
||||
ush_write_hex_u64(snap.pid);
|
||||
ush_write("] ");
|
||||
ush_write(state_name);
|
||||
ush_write(" ");
|
||||
ush_write(snap.path);
|
||||
if (snap.state == CLEONOS_PROC_STATE_EXITED) {
|
||||
ush_write(" status=");
|
||||
ush_write_hex_u64(snap.exit_status);
|
||||
}
|
||||
ush_write_char('\n');
|
||||
shown++;
|
||||
}
|
||||
|
||||
if (shown == 0ULL) {
|
||||
ush_writeln("(no jobs)");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cleonos_app_main(void) {
|
||||
ush_cmd_ctx ctx;
|
||||
ush_cmd_ret ret;
|
||||
ush_state sh;
|
||||
char initial_cwd[USH_PATH_MAX];
|
||||
int has_context = 0;
|
||||
int success = 0;
|
||||
const char *arg = "";
|
||||
|
||||
ush_zero(&ctx, (u64)sizeof(ctx));
|
||||
ush_zero(&ret, (u64)sizeof(ret));
|
||||
ush_init_state(&sh);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
|
||||
if (ush_command_ctx_read(&ctx) != 0) {
|
||||
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "jobs") != 0) {
|
||||
has_context = 1;
|
||||
arg = ctx.arg;
|
||||
if (ctx.cwd[0] == '/') {
|
||||
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success = ush_cmd_jobs(arg);
|
||||
|
||||
if (has_context != 0) {
|
||||
if (ush_streq(sh.cwd, initial_cwd) == 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_CWD;
|
||||
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
|
||||
}
|
||||
|
||||
if (sh.exit_requested != 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_EXIT;
|
||||
ret.exit_code = sh.exit_code;
|
||||
}
|
||||
|
||||
(void)ush_command_ret_write(&ret);
|
||||
}
|
||||
|
||||
return (success != 0) ? 0 : 1;
|
||||
}
|
||||
94
cleonos/c/apps/kill_main.c
Normal file
94
cleonos/c/apps/kill_main.c
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "cmd_runtime.h"
|
||||
|
||||
static int ush_cmd_kill(const char *arg) {
|
||||
char pid_text[USH_PATH_MAX];
|
||||
const char *rest = "";
|
||||
u64 pid;
|
||||
u64 signal = 15ULL;
|
||||
u64 ret;
|
||||
|
||||
if (arg == (const char *)0 || arg[0] == '\0') {
|
||||
ush_writeln("kill: usage kill <pid> [signal]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ush_split_first_and_rest(arg, pid_text, (u64)sizeof(pid_text), &rest) == 0) {
|
||||
ush_writeln("kill: usage kill <pid> [signal]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ush_parse_u64_dec(pid_text, &pid) == 0 || pid == 0ULL) {
|
||||
ush_writeln("kill: invalid pid");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (rest != (const char *)0 && rest[0] != '\0') {
|
||||
if (ush_parse_u64_dec(rest, &signal) == 0 || signal > 255ULL) {
|
||||
ush_writeln("kill: invalid signal");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ret = cleonos_sys_proc_kill(pid, signal);
|
||||
|
||||
if (ret == (u64)-1) {
|
||||
ush_writeln("kill: pid not found");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ret == 0ULL) {
|
||||
ush_writeln("kill: target cannot be terminated right now");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ush_write("kill: sent signal ");
|
||||
ush_write_hex_u64(signal);
|
||||
ush_write(" to ");
|
||||
ush_write_hex_u64(pid);
|
||||
ush_write_char('\n');
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cleonos_app_main(void) {
|
||||
ush_cmd_ctx ctx;
|
||||
ush_cmd_ret ret;
|
||||
ush_state sh;
|
||||
char initial_cwd[USH_PATH_MAX];
|
||||
int has_context = 0;
|
||||
int success = 0;
|
||||
const char *arg = "";
|
||||
|
||||
ush_zero(&ctx, (u64)sizeof(ctx));
|
||||
ush_zero(&ret, (u64)sizeof(ret));
|
||||
ush_init_state(&sh);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
|
||||
if (ush_command_ctx_read(&ctx) != 0) {
|
||||
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "kill") != 0) {
|
||||
has_context = 1;
|
||||
arg = ctx.arg;
|
||||
if (ctx.cwd[0] == '/') {
|
||||
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success = ush_cmd_kill(arg);
|
||||
|
||||
if (has_context != 0) {
|
||||
if (ush_streq(sh.cwd, initial_cwd) == 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_CWD;
|
||||
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
|
||||
}
|
||||
|
||||
if (sh.exit_requested != 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_EXIT;
|
||||
ret.exit_code = sh.exit_code;
|
||||
}
|
||||
|
||||
(void)ush_command_ret_write(&ret);
|
||||
}
|
||||
|
||||
return (success != 0) ? 0 : 1;
|
||||
}
|
||||
191
cleonos/c/apps/ps_main.c
Normal file
191
cleonos/c/apps/ps_main.c
Normal file
@@ -0,0 +1,191 @@
|
||||
#include "cmd_runtime.h"
|
||||
|
||||
static int ush_ps_is_user_path(const char *path) {
|
||||
if (path == (const char *)0 || path[0] != '/') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (path[1] == 's' && path[2] == 'y' && path[3] == 's' && path[4] == 't' && path[5] == 'e' && path[6] == 'm' &&
|
||||
(path[7] == '/' || path[7] == '\0')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (path[1] == 'd' && path[2] == 'r' && path[3] == 'i' && path[4] == 'v' && path[5] == 'e' && path[6] == 'r' &&
|
||||
(path[7] == '/' || path[7] == '\0')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *ush_ps_state_name(u64 state) {
|
||||
if (state == CLEONOS_PROC_STATE_PENDING) {
|
||||
return "PEND";
|
||||
}
|
||||
if (state == CLEONOS_PROC_STATE_RUNNING) {
|
||||
return "RUN ";
|
||||
}
|
||||
if (state == CLEONOS_PROC_STATE_EXITED) {
|
||||
return "EXIT";
|
||||
}
|
||||
return "UNKN";
|
||||
}
|
||||
|
||||
static int ush_ps_next_token(const char **io_cursor, char *out, u64 out_size) {
|
||||
const char *p;
|
||||
u64 n = 0ULL;
|
||||
|
||||
if (io_cursor == (const char **)0 || out == (char *)0 || out_size == 0ULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
out[0] = '\0';
|
||||
p = *io_cursor;
|
||||
|
||||
if (p == (const char *)0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (*p != '\0' && ush_is_space(*p) != 0) {
|
||||
p++;
|
||||
}
|
||||
|
||||
if (*p == '\0') {
|
||||
*io_cursor = p;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (*p != '\0' && ush_is_space(*p) == 0) {
|
||||
if (n + 1ULL < out_size) {
|
||||
out[n++] = *p;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
|
||||
out[n] = '\0';
|
||||
*io_cursor = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ush_ps_print_one(const cleonos_proc_snapshot *snap) {
|
||||
if (snap == (const cleonos_proc_snapshot *)0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ush_write("PID=");
|
||||
ush_write_hex_u64(snap->pid);
|
||||
ush_write(" PPID=");
|
||||
ush_write_hex_u64(snap->ppid);
|
||||
ush_write(" ST=");
|
||||
ush_write(ush_ps_state_name(snap->state));
|
||||
ush_write(" TTY=");
|
||||
ush_write_hex_u64(snap->tty_index);
|
||||
ush_write(" RT=");
|
||||
ush_write_hex_u64(snap->runtime_ticks);
|
||||
ush_write(" MEM=");
|
||||
ush_write_hex_u64(snap->mem_bytes);
|
||||
if (snap->state == CLEONOS_PROC_STATE_EXITED) {
|
||||
ush_write(" EXIT=");
|
||||
ush_write_hex_u64(snap->exit_status);
|
||||
}
|
||||
ush_write(" PATH=");
|
||||
ush_writeln(snap->path);
|
||||
}
|
||||
|
||||
static int ush_cmd_ps(const char *arg) {
|
||||
u64 proc_count;
|
||||
u64 i;
|
||||
u64 shown = 0ULL;
|
||||
int include_exited = 0;
|
||||
int only_user = 0;
|
||||
const char *cursor = arg;
|
||||
char token[USH_PATH_MAX];
|
||||
|
||||
while (ush_ps_next_token(&cursor, token, (u64)sizeof(token)) != 0) {
|
||||
if (ush_streq(token, "-a") != 0 || ush_streq(token, "--all") != 0) {
|
||||
include_exited = 1;
|
||||
} else if (ush_streq(token, "-u") != 0 || ush_streq(token, "--user") != 0) {
|
||||
only_user = 1;
|
||||
} else {
|
||||
ush_writeln("ps: usage ps [-a|--all] [-u|--user]");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
proc_count = cleonos_sys_proc_count();
|
||||
ush_writeln("ps:");
|
||||
|
||||
for (i = 0ULL; i < proc_count; i++) {
|
||||
u64 pid = 0ULL;
|
||||
cleonos_proc_snapshot snap;
|
||||
|
||||
if (cleonos_sys_proc_pid_at(i, &pid) == 0ULL || pid == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cleonos_sys_proc_snapshot(pid, &snap, (u64)sizeof(snap)) == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (include_exited == 0 && snap.state == CLEONOS_PROC_STATE_EXITED) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (only_user != 0 && ush_ps_is_user_path(snap.path) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ush_ps_print_one(&snap);
|
||||
shown++;
|
||||
}
|
||||
|
||||
if (shown == 0ULL) {
|
||||
ush_writeln("(no process)");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cleonos_app_main(void) {
|
||||
ush_cmd_ctx ctx;
|
||||
ush_cmd_ret ret;
|
||||
ush_state sh;
|
||||
char initial_cwd[USH_PATH_MAX];
|
||||
int has_context = 0;
|
||||
int success = 0;
|
||||
const char *arg = "";
|
||||
|
||||
ush_zero(&ctx, (u64)sizeof(ctx));
|
||||
ush_zero(&ret, (u64)sizeof(ret));
|
||||
ush_init_state(&sh);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
|
||||
if (ush_command_ctx_read(&ctx) != 0) {
|
||||
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "ps") != 0) {
|
||||
has_context = 1;
|
||||
arg = ctx.arg;
|
||||
if (ctx.cwd[0] == '/') {
|
||||
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success = ush_cmd_ps(arg);
|
||||
|
||||
if (has_context != 0) {
|
||||
if (ush_streq(sh.cwd, initial_cwd) == 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_CWD;
|
||||
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
|
||||
}
|
||||
|
||||
if (sh.exit_requested != 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_EXIT;
|
||||
ret.exit_code = sh.exit_code;
|
||||
}
|
||||
|
||||
(void)ush_command_ret_write(&ret);
|
||||
}
|
||||
|
||||
return (success != 0) ? 0 : 1;
|
||||
}
|
||||
@@ -152,8 +152,10 @@ static int ush_cmd_help(void) {
|
||||
ush_writeln(" mv <src> <dst> (/temp only)");
|
||||
ush_writeln(" rm <path> (/temp only)");
|
||||
ush_writeln(" pid");
|
||||
ush_writeln(" spawn <path|name> [args...]");
|
||||
ush_writeln(" wait <pid>");
|
||||
ush_writeln(" spawn <path|name> [args...] / bg <path|name> [args...]");
|
||||
ush_writeln(" wait <pid> / fg [pid]");
|
||||
ush_writeln(" kill <pid> [signal]");
|
||||
ush_writeln(" jobs [-a] / ps [-a] [-u] / top [--once] [-n loops] [-d ticks]");
|
||||
ush_writeln(" sleep <ticks>");
|
||||
ush_writeln(" yield");
|
||||
ush_writeln(" shutdown / restart");
|
||||
|
||||
236
cleonos/c/apps/top_main.c
Normal file
236
cleonos/c/apps/top_main.c
Normal file
@@ -0,0 +1,236 @@
|
||||
#include "cmd_runtime.h"
|
||||
|
||||
static const char *ush_top_state_name(u64 state) {
|
||||
if (state == CLEONOS_PROC_STATE_PENDING) {
|
||||
return "PEND";
|
||||
}
|
||||
if (state == CLEONOS_PROC_STATE_RUNNING) {
|
||||
return "RUN ";
|
||||
}
|
||||
if (state == CLEONOS_PROC_STATE_EXITED) {
|
||||
return "EXIT";
|
||||
}
|
||||
return "UNKN";
|
||||
}
|
||||
|
||||
static int ush_top_next_token(const char **io_cursor, char *out, u64 out_size) {
|
||||
const char *p;
|
||||
u64 n = 0ULL;
|
||||
|
||||
if (io_cursor == (const char **)0 || out == (char *)0 || out_size == 0ULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
out[0] = '\0';
|
||||
p = *io_cursor;
|
||||
|
||||
if (p == (const char *)0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (*p != '\0' && ush_is_space(*p) != 0) {
|
||||
p++;
|
||||
}
|
||||
|
||||
if (*p == '\0') {
|
||||
*io_cursor = p;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (*p != '\0' && ush_is_space(*p) == 0) {
|
||||
if (n + 1ULL < out_size) {
|
||||
out[n++] = *p;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
|
||||
out[n] = '\0';
|
||||
*io_cursor = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ush_top_parse(const char *arg, u64 *out_loops, u64 *out_delay) {
|
||||
const char *cursor = arg;
|
||||
char token[USH_PATH_MAX];
|
||||
u64 loops = 0ULL;
|
||||
u64 delay = 5ULL;
|
||||
|
||||
if (out_loops == (u64 *)0 || out_delay == (u64 *)0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (ush_top_next_token(&cursor, token, (u64)sizeof(token)) != 0) {
|
||||
if (ush_streq(token, "--once") != 0) {
|
||||
loops = 1ULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ush_streq(token, "-n") != 0) {
|
||||
if (ush_top_next_token(&cursor, token, (u64)sizeof(token)) == 0 || ush_parse_u64_dec(token, &loops) == 0) {
|
||||
return 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ush_streq(token, "-d") != 0) {
|
||||
if (ush_top_next_token(&cursor, token, (u64)sizeof(token)) == 0 || ush_parse_u64_dec(token, &delay) == 0) {
|
||||
return 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
*out_loops = loops;
|
||||
*out_delay = delay;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ush_top_render_frame(u64 frame_index, u64 delay_ticks) {
|
||||
u64 proc_count = cleonos_sys_proc_count();
|
||||
u64 i;
|
||||
u64 shown = 0ULL;
|
||||
|
||||
ush_write("\x1B[2J\x1B[H");
|
||||
ush_write("top frame=");
|
||||
ush_write_hex_u64(frame_index);
|
||||
ush_write(" ticks=");
|
||||
ush_write_hex_u64(cleonos_sys_timer_ticks());
|
||||
ush_write(" delay=");
|
||||
ush_write_hex_u64(delay_ticks);
|
||||
ush_write_char('\n');
|
||||
ush_writeln("PID ST TTY RTICKS MEM PATH");
|
||||
|
||||
for (i = 0ULL; i < proc_count; i++) {
|
||||
u64 pid = 0ULL;
|
||||
cleonos_proc_snapshot snap;
|
||||
|
||||
if (cleonos_sys_proc_pid_at(i, &pid) == 0ULL || pid == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cleonos_sys_proc_snapshot(pid, &snap, (u64)sizeof(snap)) == 0ULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (snap.state != CLEONOS_PROC_STATE_PENDING && snap.state != CLEONOS_PROC_STATE_RUNNING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ush_write_hex_u64(snap.pid);
|
||||
ush_write(" ");
|
||||
ush_write(ush_top_state_name(snap.state));
|
||||
ush_write(" ");
|
||||
ush_write_hex_u64(snap.tty_index);
|
||||
ush_write(" ");
|
||||
ush_write_hex_u64(snap.runtime_ticks);
|
||||
ush_write(" ");
|
||||
ush_write_hex_u64(snap.mem_bytes);
|
||||
ush_write(" ");
|
||||
ush_writeln(snap.path);
|
||||
shown++;
|
||||
}
|
||||
|
||||
if (shown == 0ULL) {
|
||||
ush_writeln("(no active process)");
|
||||
}
|
||||
|
||||
ush_writeln("");
|
||||
ush_writeln("press q to quit");
|
||||
}
|
||||
|
||||
static int ush_top_sleep_or_quit(u64 delay_ticks) {
|
||||
u64 i;
|
||||
|
||||
if (delay_ticks == 0ULL) {
|
||||
delay_ticks = 1ULL;
|
||||
}
|
||||
|
||||
for (i = 0ULL; i < delay_ticks; i++) {
|
||||
u64 ch = cleonos_sys_kbd_get_char();
|
||||
|
||||
if (ch != (u64)-1) {
|
||||
char c = (char)(ch & 0xFFULL);
|
||||
|
||||
if (c == 'q' || c == 'Q') {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
(void)cleonos_sys_sleep_ticks(1ULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ush_cmd_top(const char *arg) {
|
||||
u64 loops;
|
||||
u64 delay_ticks;
|
||||
u64 frame = 0ULL;
|
||||
|
||||
if (ush_top_parse(arg, &loops, &delay_ticks) == 0) {
|
||||
ush_writeln("top: usage top [--once] [-n loops] [-d ticks]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
frame++;
|
||||
ush_top_render_frame(frame, delay_ticks);
|
||||
|
||||
if (loops != 0ULL && frame >= loops) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ush_top_sleep_or_quit(delay_ticks) != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ush_write("\x1B[0m");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cleonos_app_main(void) {
|
||||
ush_cmd_ctx ctx;
|
||||
ush_cmd_ret ret;
|
||||
ush_state sh;
|
||||
char initial_cwd[USH_PATH_MAX];
|
||||
int has_context = 0;
|
||||
int success = 0;
|
||||
const char *arg = "";
|
||||
|
||||
ush_zero(&ctx, (u64)sizeof(ctx));
|
||||
ush_zero(&ret, (u64)sizeof(ret));
|
||||
ush_init_state(&sh);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
|
||||
if (ush_command_ctx_read(&ctx) != 0) {
|
||||
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "top") != 0) {
|
||||
has_context = 1;
|
||||
arg = ctx.arg;
|
||||
if (ctx.cwd[0] == '/') {
|
||||
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
|
||||
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success = ush_cmd_top(arg);
|
||||
|
||||
if (has_context != 0) {
|
||||
if (ush_streq(sh.cwd, initial_cwd) == 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_CWD;
|
||||
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
|
||||
}
|
||||
|
||||
if (sh.exit_requested != 0) {
|
||||
ret.flags |= USH_CMD_RET_FLAG_EXIT;
|
||||
ret.exit_code = sh.exit_code;
|
||||
}
|
||||
|
||||
(void)ush_command_ret_write(&ret);
|
||||
}
|
||||
|
||||
return (success != 0) ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user