Line discipline for carriage return + ps/kill kshell commands
This commit is contained in:
+68
-9
@@ -13,6 +13,7 @@
|
||||
#include <kernel.h>
|
||||
#include <time/date.h>
|
||||
#include <mem/kheap.h>
|
||||
#include <sched/process.h>
|
||||
|
||||
__attribute__((noinline))
|
||||
void smash_it()
|
||||
@@ -23,6 +24,52 @@ void smash_it()
|
||||
}
|
||||
}
|
||||
|
||||
extern struct process* processes_list;
|
||||
|
||||
void ps()
|
||||
{
|
||||
printf("pid\tname\tstatus\n");
|
||||
struct process* curr = processes_list;
|
||||
while (curr != NULL) {
|
||||
char* status;
|
||||
switch (curr->status) {
|
||||
case READY: status = "READY"; break;
|
||||
case RUNNING: status = "RUNNING"; break;
|
||||
case DEAD: status = "DEAD"; break;
|
||||
default: status = "N/A"; break;
|
||||
}
|
||||
printf("%u\t%s\t%s\n", curr->pid, curr->name, status);
|
||||
if (curr->next != NULL) {
|
||||
curr = curr->next;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void kill()
|
||||
{
|
||||
char input_buf[11] = {0};
|
||||
printf("pid> ");
|
||||
keyboard_getline(input_buf, 10);
|
||||
int pid = atoi(input_buf);
|
||||
|
||||
struct process* curr = processes_list;
|
||||
while (curr != NULL) {
|
||||
if (curr->pid == (size_t)pid) {
|
||||
curr->status = DEAD; // equivalent of SIGKILL (no clean termination like SIGTERM)
|
||||
printf("killed %d\n", pid);
|
||||
break;
|
||||
}
|
||||
if (curr->next != NULL) {
|
||||
curr = curr->next;
|
||||
} else {
|
||||
printf("couldn't find process with PID %d\n", pid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* pedicel_main - Kernel shell main function
|
||||
* @arg: argument (optional)
|
||||
@@ -44,15 +91,17 @@ void pedicel_main(void* arg)
|
||||
|
||||
if (strncmp(input_buf, "help", 4) == 0) {
|
||||
printf("++ shell builtins ++\r\n\r\n"
|
||||
"\tclear - clear the screen\r\n"
|
||||
"\tpanic - trigger a test panic\r\n"
|
||||
"\tsyscall - trigger int 0x80\r\n"
|
||||
"\tpf - trigger a page fault\r\n"
|
||||
"\tnow - get current date\r\n"
|
||||
"\tsmash - smash the stack\r\n"
|
||||
"\tmem - get used heap info\r\n"
|
||||
"\tload - load an user executable\r\n"
|
||||
"\tlist - list initfs.tar contents\r\n");
|
||||
"\tclear - clear the screen\n"
|
||||
"\tpanic - trigger a test panic\n"
|
||||
"\tsyscall - trigger int 0x80\n"
|
||||
"\tpf - trigger a page fault\n"
|
||||
"\tnow - get current date\n"
|
||||
"\tsmash - smash the stack\n"
|
||||
"\tmem - get used heap info\n"
|
||||
"\tload - load an user executable\n"
|
||||
"\tlist - list initfs.tar contents\n"
|
||||
"\tps - list running processes\n"
|
||||
"\tkill - kill a running process by PID\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -107,6 +156,16 @@ void pedicel_main(void* arg)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strncmp(input_buf, "ps", 2) == 0) {
|
||||
ps();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strncmp(input_buf, "kill", 4) == 0) {
|
||||
kill();
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("%s: command not found\r\n", input_buf);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user