add stuff needed for process control

This commit is contained in:
Colin Davidson
2024-08-25 03:57:59 -07:00
parent d410281766
commit b396280df7
2 changed files with 87 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ import "core:strings"
import "core:c"
Handle :: distinct i32
Pid :: distinct i32
File_Time :: distinct u64
INVALID_HANDLE :: ~Handle(0)
@@ -584,6 +585,8 @@ F_GETPATH :: 50 // return the full path of the fd
foreign libc {
@(link_name="__error") __error :: proc() -> ^c.int ---
@(link_name="posix_spawn") _unix_posix_spawn :: proc(pid: ^Pid, path: cstring, file_actions: rawptr, attrp: rawptr, argv: [^]cstring, envp: [^]cstring) -> c.int ---
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, #c_vararg mode: ..u16) -> Handle ---
@(link_name="close") _unix_close :: proc(handle: Handle) -> c.int ---
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: c.size_t) -> int ---
@@ -677,6 +680,28 @@ get_last_error_string :: proc() -> string {
return string(_darwin_string_error(__error()^))
}
posix_spawn :: proc(path: string, args: []string, envs: []string, file_actions: rawptr, attributes: rawptr) -> (Pid, Error) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
args_cstrs := make([]cstring, len(args) + 2, context.temp_allocator)
args_cstrs[0] = strings.clone_to_cstring(path, context.temp_allocator)
for i := 0; i < len(args); i += 1 {
args_cstrs[i+1] = strings.clone_to_cstring(args[i], context.temp_allocator)
}
envs_cstrs := make([]cstring, len(envs) + 1, context.temp_allocator)
for i := 0; i < len(envs); i += 1 {
envs_cstrs[i] = strings.clone_to_cstring(envs[i], context.temp_allocator)
}
child_pid: Pid
status := _unix_posix_spawn(&child_pid, path_cstr, file_actions, attributes, raw_data(args_cstrs), raw_data(envs_cstrs))
if status != 0 {
return 0, get_last_error()
}
return child_pid, nil
}
@(require_results)
open :: proc(path: string, flags: int = O_RDWR, mode: int = 0) -> (handle: Handle, err: Error) {

View File

@@ -1,6 +1,6 @@
package darwin
foreign import pthread "system:System.framework"
foreign import mach "system:System.framework"
import "core:c"
@@ -8,15 +8,74 @@ import "core:c"
// However all other sync primitives are aligned for robustness.
// I cannot currently align these though.
// See core/sys/unix/pthread_linux.odin/pthread_t.
task_t :: distinct u64
mach_port_t :: distinct i32
task_t :: mach_port_t
semaphore_t :: distinct u64
kern_return_t :: distinct u64
thread_act_t :: distinct u64
MACH_MSG_PORT_DESCRIPTOR :: 0
MACH_SEND_MSG :: 0x00000001
MACH_RCV_MSG :: 0x00000002
MACH_SEND_TIMEOUT :: 0x00000010
MACH_RCV_TIMEOUT :: 0x00000100
MACH_MSG_TYPE_COPY_SEND :: 19
MACH_MSG_TYPE_MAKE_SEND :: 20
MACH_MSGH_BITS_COMPLEX :: 0x80000000
MACH_PORT_RIGHT_SEND :: 0
MACH_PORT_RIGHT_RECEIVE :: 1
TASK_BOOTSTRAP_PORT :: 4
mach_msg_option_t :: distinct i32
name_t :: distinct cstring
mach_msg_port_descriptor_t :: struct {
name: mach_port_t,
_: u32,
extra: bit_field u32 {
_: u32 | 16,
disposition: u32 | 8,
type: u32 | 8,
},
}
mach_msg_header_t :: struct {
msgh_bits: u32,
msgh_size: u32,
msgh_remote_port: mach_port_t,
msgh_local_port: mach_port_t,
msgh_voucher_port: u32,
msgh_id: i32,
}
mach_msg_body_t :: struct {
msgh_descriptor_count: u32,
}
mach_msg_trailer_t :: struct {
msgh_trailer_type: u32,
msgh_trailer_size: u32,
}
@(default_calling_convention="c")
foreign pthread {
foreign mach {
mach_task_self :: proc() -> task_t ---
mach_msg :: proc(header: rawptr, option: mach_msg_option_t, send_size: u32, receive_limit: u32, receive_name: mach_port_t, timeout: u32, notify: mach_port_t) -> kern_return_t ---
mach_msg_send :: proc(header: rawptr) -> kern_return_t ---
mach_port_allocate :: proc(task: task_t, right: u32, name: rawptr) -> kern_return_t ---
mach_port_deallocate :: proc(task: task_t, name: u32) -> kern_return_t ---
mach_port_extract_right :: proc(task: task_t, name: u32, msgt_name: u32, poly: ^mach_port_t, poly_poly: ^mach_port_t) -> kern_return_t ---
task_get_special_port :: proc(task: task_t, port: i32, special_port: ^mach_port_t) -> kern_return_t ---
bootstrap_register2 :: proc(bp: mach_port_t, service_name: name_t, sp: mach_port_t, flags: u64) -> kern_return_t ---
bootstrap_look_up :: proc(bp: mach_port_t, service_name: name_t, sp: ^mach_port_t) -> kern_return_t ---
semaphore_create :: proc(task: task_t, semaphore: ^semaphore_t, policy, value: c.int) -> kern_return_t ---
semaphore_destroy :: proc(task: task_t, semaphore: semaphore_t) -> kern_return_t ---