Compare commits

...

4 Commits

Author SHA1 Message Date
727e227b09 apps: Add ps (uses pstat) 2022-10-22 14:26:59 +02:00
6ac57a2f5c libc: Add support for pstat() 2022-10-22 14:26:42 +02:00
bcdcfc4b45 Kernel: Add a pstat() system call
Not part of C or POSIX, but since there is no procfs right now, I thought it would be nice to have an interface to query process information.
It works like this: you pass the process ID and a pointer to a struct pstat (can be null).
If the process ID is -1, the kernel picks the process with the highest PID.
Then, if the pointer to a pstat struct is not null, the kernel fills it in with the process's information, and returns the process's PID.
2022-10-22 14:26:29 +02:00
31e0f0efed uptime: Use fgets() 2022-10-22 14:23:34 +02:00
11 changed files with 162 additions and 42 deletions

View File

@ -1,4 +1,4 @@
APPS := init sym sh crash uname uptime hello block
APPS := init sym sh crash uname uptime hello ps
APPS_DIR := $(LUNA_ROOT)/apps
APPS_SRC := $(APPS_DIR)/src

View File

@ -1,34 +0,0 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int fd = open("/dev/kbd", O_RDONLY | O_NONBLOCK);
if (fd < 0)
{
perror("open");
return 1;
}
FILE* fp = fdopen(fd, "r");
if (!fp)
{
perror("fdopen");
close(fd);
return 1;
}
char buf[32];
fread(buf, sizeof(buf) - 1, 1, fp);
if (ferror(fp))
{
perror("fread");
fclose(fp);
return 1;
}
fclose(fp);
return 0;
}

44
apps/src/ps.c Normal file
View File

@ -0,0 +1,44 @@
#include <errno.h>
#include <luna/pstat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
pid_t get_current_max_threads()
{
pid_t result = pstat(-1, NULL);
if (result < 0)
{
perror("pstat(-1)");
exit(1);
}
return result;
}
void display_process(struct pstat* pstatbuf)
{
printf("%ld %ld %s %s (%ld ms)\n", pstatbuf->pt_pid, pstatbuf->pt_ppid, pstatbuf->pt_name, pstname(pstatbuf),
pstatbuf->pt_time);
}
int try_pstat(pid_t pid, struct pstat* pstatbuf)
{
pid_t result = pstat(pid, pstatbuf);
if (result < 0)
{
if (errno == ESRCH) return 0;
perror("pstat");
exit(1);
}
return 1;
}
int main()
{
struct pstat pst;
pid_t max = get_current_max_threads();
for (pid_t pid = 0; pid <= max; pid++)
{
if (try_pstat(pid, &pst)) { display_process(&pst); }
}
}

View File

@ -10,17 +10,15 @@ int main()
return 1;
}
char buf[4096];
size_t nread = fread(buf, sizeof(buf) - 1, 1, fp);
char buf[32];
fgets(buf, sizeof(buf), fp);
if (ferror(fp))
{
perror("fread");
perror("fgets");
return 1;
}
buf[nread] = 0; // null terminate it :)
long ms_uptime = atol(buf);
printf("up for %ld seconds\n", ms_uptime / 1000);

View File

@ -23,8 +23,10 @@
#define SYS_waitpid 18
#define SYS_access 19
#define SYS_fstat 20
#define SYS_pstat 21
struct stat;
struct pstat;
namespace Syscall
{
@ -52,3 +54,4 @@ void sys_fork(Context* context);
void sys_waitpid(Context* context, long pid, int* wstatus, int options);
void sys_access(Context* context, const char* path, int amode);
void sys_fstat(Context* context, int fd, struct stat* buf);
void sys_pstat(Context* context, long pid, struct pstat* buf);

View File

@ -32,6 +32,7 @@ void Syscall::entry(Context* context)
case SYS_waitpid: sys_waitpid(context, (long)context->rdi, (int*)context->rsi, (int)context->rdx); break;
case SYS_access: sys_access(context, (const char*)context->rdi, (int)context->rsi); break;
case SYS_fstat: sys_fstat(context, (int)context->rdi, (struct stat*)context->rsi); break;
case SYS_pstat: sys_pstat(context, (long)context->rdi, (struct pstat*)context->rsi); break;
default: context->rax = -ENOSYS; break;
}
VMM::exit_syscall_context();

View File

@ -374,10 +374,10 @@ void Scheduler::task_tick(Context* context)
ASSERT(Interrupts::is_in_handler());
Interrupts::disable();
sched_decrement_sleep_times();
if (sched_current_task->id == 0) return task_yield(context);
sched_current_task->task_time -= frequency;
sched_current_task->cpu_time += frequency;
if (sched_current_task->task_time < 0)
if (sched_current_task->id == 0) return task_yield(context);
if (sched_current_task->task_time <= 0)
{
sched_current_task->task_time = 0;
task_yield(context);
@ -514,3 +514,49 @@ void sys_waitpid(Context* context, long pid, int* wstatus,
child->state = child->Exited;
context->rax = (long)child->id;
}
struct pstat
{
long pt_pid;
long pt_ppid;
char pt_name[128];
int pt_state;
long pt_time;
};
void sys_pstat(Context* context, long pid, struct pstat* buf)
{
Task* task;
if (pid == -1) task = Scheduler::find_by_pid(free_tid - 1);
else if (pid == 0)
task = &idle_task;
else
task = Scheduler::find_by_pid(pid);
if (!task)
{
context->rax = -ESRCH;
return;
}
if (task->state == task->Exited) // we're just waiting for the reaper to reap it
{
context->rax = -ESRCH;
return;
}
if (buf)
{
struct pstat* kpstat = obtain_user_ref(buf);
if (!kpstat)
{
context->rax = -EFAULT;
return;
}
kpstat->pt_pid = task->id;
kpstat->pt_ppid = task->ppid;
kpstat->pt_state = (int)task->state;
kpstat->pt_time = (long)task->cpu_time;
strlcpy(kpstat->pt_name, task->name, sizeof(kpstat->pt_name));
release_user_ref(kpstat);
}
context->rax = task->id;
return;
}

View File

@ -0,0 +1,36 @@
#ifndef _LUNA_PSTAT_H
#define _LUNA_PSTAT_H
#include <sys/types.h>
struct pstat
{
pid_t pt_pid;
pid_t pt_ppid;
char pt_name[128];
int pt_state;
pid_t pt_time;
};
#define PT_IDLE 0
#define PT_RUNNING 1
#define PT_SLEEPING 2
#define PT_ZOMBIE 3
#define PT_WAITING 4
#ifdef __cplusplus
extern "C"
{
#endif
/* Returns information about the process with PID pid in buf, if buf is non-null. Luna-specific. */
pid_t pstat(pid_t pid, struct pstat* buf);
/* Returns a string representation of the process state in buf. */
const char* pstname(struct pstat* buf);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -22,5 +22,6 @@
#define SYS_waitpid 18
#define SYS_access 19
#define SYS_fstat 20
#define SYS_pstat 21
#endif

View File

@ -0,0 +1,24 @@
#include <luna/pstat.h>
#include <sys/syscall.h>
#include <unistd.h>
extern "C"
{
pid_t pstat(pid_t pid, struct pstat* buf)
{
return syscall(SYS_pstat, pid, buf);
}
const char* pstname(struct pstat* buf)
{
switch (buf->pt_state)
{
case PT_IDLE: return "Idle";
case PT_RUNNING: return "Running";
case PT_SLEEPING: return "Sleeping";
case PT_WAITING: return "Waiting";
case PT_ZOMBIE: return "Zombie";
default: return "Unknown";
}
}
}

View File

@ -24,6 +24,7 @@ extern "C" long syscall(long number, ...)
case SYS_munmap:
case SYS_access:
case SYS_fstat:
case SYS_pstat:
case SYS_open: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);