Compare commits

...

2 Commits

Author SHA1 Message Date
01564cb905 libc: Adapt libc to getprocid() + add getppid() 2022-10-18 17:36:33 +02:00
52d391507d Kernel: Rename the getpid() syscall to getprocid()
Now, we have one single system call to fetch all sorts of identifiers:
PID, PPID, UID, GID; EUID, EGID, and more...
2022-10-18 17:36:17 +02:00
11 changed files with 58 additions and 15 deletions

View File

@ -64,7 +64,7 @@ int main()
printf("Welcome to Luna!\n"); printf("Welcome to Luna!\n");
printf("Running as PID %ld\n\n", getpid()); printf("Running as PID %ld, PPID %ld\n\n", getpid(), getppid());
sleep(1); sleep(1);
@ -163,7 +163,7 @@ int main()
if (child == 0) if (child == 0)
{ {
msleep(500); msleep(500);
printf("I am the child, who is my parent?\n"); printf("I am the child (PID %ld), my parent is PID %ld!!\n", getpid(), getppid());
execv("/bin/sym", NULL); execv("/bin/sym", NULL);
perror("execv"); perror("execv");
return 1; return 1;

View File

@ -7,7 +7,7 @@
#define SYS_sleep 2 #define SYS_sleep 2
#define SYS_write 3 #define SYS_write 3
#define SYS_paint 4 #define SYS_paint 4
#define SYS_getpid 5 #define SYS_getprocid 5
#define SYS_mmap 6 #define SYS_mmap 6
#define SYS_munmap 7 #define SYS_munmap 7
#define SYS_open 8 #define SYS_open 8
@ -33,7 +33,7 @@ void sys_yield(Context* context);
void sys_sleep(Context* context, uint64_t ms); void sys_sleep(Context* context, uint64_t ms);
void sys_write(Context* context, int fd, size_t size, const char* addr); void sys_write(Context* context, int fd, size_t size, const char* addr);
void sys_paint(Context* context, uint64_t x, uint64_t y, uint64_t w, uint64_t h, uint64_t col); void sys_paint(Context* context, uint64_t x, uint64_t y, uint64_t w, uint64_t h, uint64_t col);
void sys_getpid(Context* context); void sys_getprocid(Context* context, int field);
void sys_mmap(Context* context, void* address, size_t size, int prot); void sys_mmap(Context* context, void* address, size_t size, int prot);
void sys_munmap(Context* context, void* address, size_t size); void sys_munmap(Context* context, void* address, size_t size);
void sys_open(Context* context, const char* filename, int flags); void sys_open(Context* context, const char* filename, int flags);

View File

@ -16,7 +16,7 @@ void Syscall::entry(Context* context)
case SYS_sleep: sys_sleep(context, context->rdi); break; case SYS_sleep: sys_sleep(context, context->rdi); break;
case SYS_write: sys_write(context, (int)context->rdi, context->rsi, (const char*)context->rdx); break; case SYS_write: sys_write(context, (int)context->rdi, context->rsi, (const char*)context->rdx); break;
case SYS_paint: sys_paint(context, context->rdi, context->rsi, context->rdx, context->r10, context->r8); break; case SYS_paint: sys_paint(context, context->rdi, context->rsi, context->rdx, context->r10, context->r8); break;
case SYS_getpid: sys_getpid(context); break; case SYS_getprocid: sys_getprocid(context, (int)context->rdi); break;
case SYS_mmap: sys_mmap(context, (void*)context->rdi, context->rsi, (int)context->rdx); break; case SYS_mmap: sys_mmap(context, (void*)context->rdi, context->rsi, (int)context->rdx); break;
case SYS_munmap: sys_munmap(context, (void*)context->rdi, context->rsi); break; case SYS_munmap: sys_munmap(context, (void*)context->rdi, context->rsi); break;
case SYS_open: sys_open(context, (const char*)context->rdi, (int)context->rsi); break; case SYS_open: sys_open(context, (const char*)context->rdi, (int)context->rsi); break;

24
kernel/src/sys/id.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "errno.h"
#include "thread/Scheduler.h"
#define ID_PID 0
#define ID_PPID 1
void sys_getprocid(Context* context, int field)
{
if (field == ID_PID)
{
context->rax = Scheduler::current_task()->id;
return;
}
else if (field == ID_PPID)
{
context->rax = Scheduler::current_task()->ppid;
return;
}
else
{
context->rax = -EINVAL;
return;
}
}

View File

@ -19,8 +19,3 @@ void sys_sleep(Context* context, uint64_t ms)
task->state = task->Sleeping; task->state = task->Sleeping;
Scheduler::task_yield(context); Scheduler::task_yield(context);
} }
void sys_getpid(Context* context)
{
context->rax = Scheduler::current_task()->id;
}

View File

@ -0,0 +1,7 @@
#ifndef _BITS_GETPROCID_H
#define _BITS_GETPROCID_H
#define ID_PID 0
#define ID_PPID 1
#endif

View File

@ -1,6 +1,7 @@
#ifndef _LUNA_H #ifndef _LUNA_H
#define _LUNA_H #define _LUNA_H
#include <bits/getprocid.h>
#include <bits/macros.h> #include <bits/macros.h>
#include <sys/types.h> #include <sys/types.h>
@ -9,6 +10,9 @@ extern "C"
{ {
#endif #endif
/* Returns a numeric identifier associated with the current process, depending on field. */
long getprocid(int field);
/* Sleeps for ms milliseconds. */ /* Sleeps for ms milliseconds. */
unsigned int msleep(unsigned int ms); unsigned int msleep(unsigned int ms);

View File

@ -6,7 +6,7 @@
#define SYS_sleep 2 #define SYS_sleep 2
#define SYS_write 3 #define SYS_write 3
#define SYS_paint 4 #define SYS_paint 4
#define SYS_getpid 5 #define SYS_getprocid 5
#define SYS_mmap 6 #define SYS_mmap 6
#define SYS_munmap 7 #define SYS_munmap 7
#define SYS_open 8 #define SYS_open 8

View File

@ -27,9 +27,12 @@ extern "C"
* the parent. */ * the parent. */
pid_t fork(void); pid_t fork(void);
/* Returns the current process's process ID. */ /* Returns the current process' process ID. */
pid_t getpid(void); pid_t getpid(void);
/* Returns the current process' parent's process ID. */
pid_t getppid(void);
/* Terminates the program with the status code status. */ /* Terminates the program with the status code status. */
__lc_noreturn void _exit(int status); __lc_noreturn void _exit(int status);

View File

@ -7,6 +7,11 @@
extern "C" extern "C"
{ {
long getprocid(int field)
{
return syscall(SYS_getprocid, field);
}
unsigned int msleep(unsigned int ms) unsigned int msleep(unsigned int ms)
{ {
return (unsigned int)syscall(SYS_sleep, ms); return (unsigned int)syscall(SYS_sleep, ms);

View File

@ -27,7 +27,12 @@ extern "C"
pid_t getpid(void) pid_t getpid(void)
{ {
return syscall(SYS_getpid); return getprocid(ID_PID);
}
pid_t getppid(void)
{
return getprocid(ID_PPID);
} }
long syscall(long number, ...) long syscall(long number, ...)
@ -40,9 +45,9 @@ extern "C"
{ {
case SYS_clock: case SYS_clock:
case SYS_yield: case SYS_yield:
case SYS_fork: case SYS_fork: result = __luna_syscall0(number); break;
case SYS_getpid: result = __luna_syscall0(number); break;
case SYS_exit: case SYS_exit:
case SYS_getprocid:
case SYS_close: case SYS_close:
case SYS_exec: case SYS_exec:
case SYS_mkdir: case SYS_mkdir: