diff --git a/ce/ce.c b/ce/ce.c index 5d8c771..70c485a 100644 --- a/ce/ce.c +++ b/ce/ce.c @@ -3,13 +3,14 @@ #include "gapbuffer.h" #include "interp.h" #include "mprintf.h" -#include "self.h" #include "strbuf.h" #include +#include #include #include #include #include +#include #include #include #include @@ -63,8 +64,7 @@ static void exec_line (const char* line) { } void app_main (void) { - e_pid = get_exec_pid (); - e_pgid = get_procgroup (e_pid); + libprocess_self_init (); struct edit_line edit_line; diff --git a/ce/edit.c b/ce/edit.c index 6f61caa..7355972 100644 --- a/ce/edit.c +++ b/ce/edit.c @@ -2,7 +2,6 @@ #include "arena_alloc.h" #include "gapbuffer.h" #include "mprintf.h" -#include "self.h" #include "walloc.h" #include #include @@ -11,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -231,7 +231,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si (int)(editor.cursor.line - editor.row_offset) + 1, (int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width); - mail_send (e_pgid, backbuffer, bbptr - backbuffer); + mail_send (process_get_exec_pgid (), backbuffer, bbptr - backbuffer); uint8_t ch = 0; mail_receive (&ch, 1); diff --git a/ce/interp.c b/ce/interp.c index e7eafd2..b398951 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -4,7 +4,6 @@ #include "edit.h" #include "mprintf.h" #include "parser.h" -#include "self.h" #include #include #include @@ -16,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -160,7 +160,7 @@ static void cat (struct context* context, char** file_paths, size_t files_count) return; } - mail_send (e_pgid, buffer, chunk_size); + mail_send (process_get_exec_pgid (), buffer, chunk_size); } if (rem > 0) { @@ -170,7 +170,7 @@ static void cat (struct context* context, char** file_paths, size_t files_count) return; } - mail_send (e_pgid, buffer, rem); + mail_send (process_get_exec_pgid (), buffer, rem); } filereader_fini (&fr); @@ -357,7 +357,7 @@ static void cmd_collect_proc (void* arg) { char recv[RECV_MAX]; memset (recv, 0, sizeof (recv)); if (mail_receive_nonblock (&recv, sizeof (recv) - 1) == ST_OK) { - mail_send (e_pgid, recv, strlen (recv)); + mail_send (process_get_exec_pgid (), recv, strlen (recv)); } } } diff --git a/ce/mprintf.c b/ce/mprintf.c index e0c697f..a58fb9e 100644 --- a/ce/mprintf.c +++ b/ce/mprintf.c @@ -1,7 +1,7 @@ #include "mprintf.h" -#include "self.h" #include #include +#include #include #include #include @@ -22,6 +22,6 @@ void mprintf (const char* fmt, ...) { va_end (args); - mail_send (e_pgid, buf, len); + mail_send (process_get_exec_pgid (), buf, len); free (buf); } diff --git a/ce/self.c b/ce/self.c deleted file mode 100644 index d29bb90..0000000 --- a/ce/self.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "self.h" - -int e_pid; -int e_pgid; diff --git a/ce/self.h b/ce/self.h deleted file mode 100644 index dd443f5..0000000 --- a/ce/self.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef _SELF_H -#define _SELF_H - -extern int e_pid; -extern int e_pgid; - -#endif // _SELF_H diff --git a/ce/src.mk b/ce/src.mk index 7fe6778..d4e3bee 100644 --- a/ce/src.mk +++ b/ce/src.mk @@ -5,7 +5,6 @@ c += ce.c \ parser.c \ interp.c \ mprintf.c \ - self.c \ gapbuffer.c \ edit.c @@ -16,6 +15,5 @@ o += ce.o \ parser.o \ interp.o \ mprintf.o \ - self.o \ gapbuffer.o \ edit.o diff --git a/include/syscall_defs.h b/include/syscall_defs.h index c5c483d..f842d50 100644 --- a/include/syscall_defs.h +++ b/include/syscall_defs.h @@ -35,5 +35,6 @@ #define SYS_EXEC_PARTIAL 32 #define SYS_EXEC_PARTIAL_FINI 33 #define SYS_MAIL_RECEIVE_NONBLOCK 34 +#define SYS_GET_SELF_PID 35 #endif // _M_SYSCALL_DEFS_H diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index d7689fc..9ecfac1 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -1022,6 +1022,17 @@ DEFINE_SYSCALL (sys_env_get) { return SYSRESULT (proc_env_set (target_procgroup, key, buffer, size)); } +/* int get_self_pid (void) */ +DEFINE_SYSCALL (sys_get_self_pid) { + uint64_t fp; + + spin_lock (&proc->lock, &fp); + int pid = proc->pid; + spin_unlock (&proc->lock, fp); + + return SYSRESULT (pid); +} + static syscall_handler_func_t handler_table[] = { [SYS_QUIT] = &sys_quit, [SYS_TEST] = &sys_test, @@ -1057,6 +1068,7 @@ static syscall_handler_func_t handler_table[] = { [SYS_EXEC_PARTIAL] = &sys_exec_partial, [SYS_EXEC_PARTIAL_FINI] = &sys_exec_partial_fini, [SYS_MAIL_RECEIVE_NONBLOCK] = &sys_mail_receive_nonblock, + [SYS_GET_SELF_PID] = &sys_get_self_pid, }; syscall_handler_func_t syscall_find_handler (int syscall_num) { diff --git a/libprocess/process_self.c b/libprocess/process_self.c new file mode 100644 index 0000000..efce891 --- /dev/null +++ b/libprocess/process_self.c @@ -0,0 +1,22 @@ +#include +#include + +static int e_pid; +static int e_pgid; +static int pid; +static int pgid; + +void libprocess_self_init (void) { + e_pid = get_exec_pid (); + e_pgid = get_procgroup (e_pid); + pid = get_self_pid (); + pgid = get_procgroup (pid); +} + +int process_get_exec_pid (void) { return e_pid; } + +int process_get_exec_pgid (void) { return e_pgid; } + +int process_get_pid (void) { return pid; } + +int process_get_pgid (void) { return pgid; } diff --git a/libprocess/process_self.h b/libprocess/process_self.h new file mode 100644 index 0000000..6e21e7e --- /dev/null +++ b/libprocess/process_self.h @@ -0,0 +1,14 @@ +#ifndef _LIBPROCESS_PROCESS_SELF_H +#define _LIBPROCESS_PROCESS_SELF_H + +void libprocess_self_init (void); + +int process_get_exec_pid (void); + +int process_get_exec_pgid (void); + +int process_get_pid (void); + +int process_get_pgid (void); + +#endif // _LIBPROCESS_PROCESS_SELF_H diff --git a/libprocess/src.mk b/libprocess/src.mk index 57d57e2..3e04574 100644 --- a/libprocess/src.mk +++ b/libprocess/src.mk @@ -1,5 +1,7 @@ include $(platform)/src.mk -c += process.c +c += process.c \ + process_self.c -o += process.o +o += process.o \ + process_self.o diff --git a/libsystem/system.c b/libsystem/system.c index 224d37c..83d4bbe 100644 --- a/libsystem/system.c +++ b/libsystem/system.c @@ -104,3 +104,5 @@ int exec_partial (const char* volume, const char* path) { } int exec_partial_fini (int pid) { return (int)do_syscall (SYS_EXEC_PARTIAL_FINI, pid); } + +int get_self_pid (void) { return (int)do_syscall (SYS_GET_SELF_PID, 0); } diff --git a/libsystem/system.h b/libsystem/system.h index fef707b..8712d15 100644 --- a/libsystem/system.h +++ b/libsystem/system.h @@ -108,4 +108,7 @@ int exec_partial (const char* volume, const char* path); /* Finish process for execution - run it! */ int exec_partial_fini (int pid); +/* get this process' PID */ +int get_self_pid (void); + #endif // _LIBMSL_M_SYSTEM_H