Browse Source

[os2/process]: Fill in basic functions

flysand7 1 year ago
parent
commit
2495f1c39a
2 changed files with 124 additions and 8 deletions
  1. 78 8
      core/os/os2/process.odin
  2. 46 0
      core/os/os2/process_windows.odin

+ 78 - 8
core/os/os2/process.odin

@@ -3,35 +3,105 @@ package os2
 import "core:sync"
 import "core:sync"
 import "core:time"
 import "core:time"
 import "base:runtime"
 import "base:runtime"
+import "core:strings"
 
 
-args: []string
+/*
+	Arguments to the current process.
 
 
+	See `get_args()` for description of the slice.
+*/
+args := get_args()
+
+/*
+	Obtain the process argument array from the OS.
+
+	Slice, containing arguments to the current process. Each element of the
+	slice contains a single argument. The first element of the slice would
+	typically is the path to the currently running executable.
+*/
+get_args :: proc() -> []string {
+	args := make([]string, len(runtime.args__), allocator = context.allocator)
+	for rt_arg, i in runtime.args__ {
+		args[i] = cast(string) rt_arg
+	}
+	return args[:]
+}
+
+/*
+	Exit the current process.
+*/
 exit :: proc "contextless" (code: int) -> ! {
 exit :: proc "contextless" (code: int) -> ! {
-	runtime.trap()
+	_exit(code)
 }
 }
 
 
+/*
+	Obtain the UID of the current process.
+
+	**Note(windows)**: Windows doesn't follow the posix permissions model, so
+	the function simply returns -1.
+*/
 get_uid :: proc() -> int {
 get_uid :: proc() -> int {
-	return -1
+	return _get_uid()
 }
 }
 
 
+/*
+	Obtain the effective UID of the current process.
+
+	The effective UID is typically the same as the UID of the process. In case
+	the process was run by a user with elevated permissions, the process may
+	lower the privilege to perform some tasks without privilege. In these cases
+	the real UID of the process and the effective UID are different.
+	
+	**Note(windows)**: Windows doesn't follow the posix permissions model, so
+	the function simply returns -1.
+*/
 get_euid :: proc() -> int {
 get_euid :: proc() -> int {
-	return -1
+	return _get_euid()
 }
 }
 
 
+/*
+	Obtain the GID of the current process.
+	
+	**Note(windows)**: Windows doesn't follow the posix permissions model, so
+	the function simply returns -1.
+*/
 get_gid :: proc() -> int {
 get_gid :: proc() -> int {
-	return -1
+	return _get_gid()
 }
 }
 
 
+/*
+	Obtain the effective GID of the current process.
+	
+	The effective GID is typically the same as the GID of the process. In case
+	the process was run by a user with elevated permissions, the process may
+	lower the privilege to perform some tasks without privilege. In these cases
+	the real GID of the process and the effective GID are different.
+
+	**Note(windows)**: Windows doesn't follow the posix permissions model, so
+	the function simply returns -1.
+*/
 get_egid :: proc() -> int {
 get_egid :: proc() -> int {
-	return -1
+	return _get_egid()
 }
 }
 
 
+/*
+	Obtain the ID of the current process.
+*/
 get_pid :: proc() -> int {
 get_pid :: proc() -> int {
-	return -1
+	return _get_pid()
 }
 }
 
 
+/*
+	Obtain the ID of the parent process.
+
+	**Note(windows)**: Windows does not mantain strong relationships between
+	parent and child processes. This function returns the ID of the process
+	that has created the current process. In case the parent has died, the ID
+	returned by this function can identify a non-existent or a different
+	process.
+*/
 get_ppid :: proc() -> int {
 get_ppid :: proc() -> int {
-	return -1
+	return _get_ppid()
 }
 }
 
 
 
 

+ 46 - 0
core/os/os2/process_windows.odin

@@ -0,0 +1,46 @@
+//+build windows
+package os2
+
+import "core:sys/windows"
+
+_exit :: proc "contextless" (code: int) -> ! {
+	windows.ExitProcess(u32(code))
+}
+
+_get_uid :: proc() -> int {
+	return -1
+}
+
+_get_euid :: proc() -> int {
+	return -1
+}
+
+_get_gid :: proc() -> int {
+	return -1
+}
+
+_get_egid :: proc() -> int {
+	return -1
+}
+
+_get_pid :: proc() -> int {
+	return cast(int) windows.GetCurrentProcessId()
+}
+
+_get_ppid :: proc() -> int {
+	our_pid := windows.GetCurrentProcessId()
+	snap := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
+	if snap == windows.INVALID_HANDLE_VALUE {
+		return -1
+	}
+	defer windows.CloseHandle(snap)
+	entry := windows.PROCESSENTRY32W { dwSize = size_of(windows.PROCESSENTRY32W) }
+	status := windows.Process32FirstW(snap, &entry)
+	for status {
+		if entry.th32ProcessID == our_pid {
+			return cast(int) entry.th32ParentProcessID
+		}
+		status = windows.Process32NextW(snap, &entry)
+	}
+	return -1
+}