mirror of
https://github.com/Ed94/raddebugger.git
synced 2026-11-16 19:20:13 -08:00
process groups + pipe pair
This commit is contained in:
1 parent
c44e4a388e
commit
77bacd640a
5 files changed
+328
-4
No files matched your search
@@ -40,6 +40,13 @@ struct File
|
||||
U64 u64[1];
|
||||
};
|
||||
|
||||
typedef struct FilePair FilePair;
|
||||
struct FilePair
|
||||
{
|
||||
File read;
|
||||
File write;
|
||||
};
|
||||
|
||||
typedef struct FileMap FileMap;
|
||||
struct FileMap
|
||||
{
|
||||
@@ -70,6 +77,11 @@ internal String8 file_read_cstring(Arena *arena, File file, U64 off);
|
||||
//- rjf: files
|
||||
internal File file_open(AccessFlags flags, String8 path);
|
||||
internal void file_close(File file);
|
||||
internal FilePair file_pipe_make(B32 read_inherited, B32 write_inherited);
|
||||
internal U64 file_pipe_read(File file, void *out_data, U64 size);
|
||||
internal U64 file_pipe_write(File file, void *data, U64 size);
|
||||
internal U64 file_pipe_bytes_available(File file);
|
||||
internal B32 file_pipe_is_end(File file);
|
||||
internal U64 file_read(File file, Rng1U64 rng, void *out_data);
|
||||
#define file_read_struct(f, off, ptr) file_read((f), r1u64((off), (off)+sizeof(*(ptr))), (ptr))
|
||||
internal U64 file_write(File file, Rng1U64 rng, void *data);
|
||||
|
||||
@@ -25,6 +25,12 @@ struct Process
|
||||
U64 u64[1];
|
||||
};
|
||||
|
||||
typedef struct ProcessGroup ProcessGroup;
|
||||
struct ProcessGroup
|
||||
{
|
||||
U64 u64[1];
|
||||
};
|
||||
|
||||
typedef struct ProcessNode ProcessNode;
|
||||
struct ProcessNode
|
||||
{
|
||||
@@ -49,6 +55,8 @@ struct ProcessLaunchParams
|
||||
B32 inherit_env;
|
||||
B32 debug_subprocesses;
|
||||
B32 consoleless;
|
||||
B32 new_console;
|
||||
ProcessGroup process_group;
|
||||
File stdout_file;
|
||||
File stderr_file;
|
||||
File stdin_file;
|
||||
@@ -84,8 +92,15 @@ internal U32 get_process_start_time_unix(void);
|
||||
|
||||
internal Process process_launch(ProcessLaunchParams *params);
|
||||
internal U64 pid_from_process(Process process);
|
||||
internal B32 process_poll(Process process, U64 *exit_code_out);
|
||||
internal B32 process_is_active(Process process);
|
||||
internal B32 process_join(Process process, U64 endt_us, U64 *exit_code_out);
|
||||
internal void process_detach(Process process);
|
||||
internal B32 process_kill(Process process);
|
||||
internal B32 process_send_ctrl_c(Process process);
|
||||
|
||||
internal ProcessGroup process_group_make(B32 kill_on_close);
|
||||
internal B32 process_group_add(ProcessGroup group, Process process);
|
||||
internal void process_group_close(ProcessGroup group);
|
||||
|
||||
#endif // BASE_PROCESSES_H
|
||||
+150
-3
@@ -833,6 +833,51 @@ file_close(File file)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
internal FilePair
|
||||
file_pipe_make(B32 read_inherited, B32 write_inherited)
|
||||
{
|
||||
FilePair result = {0};
|
||||
int fds[2] = {0};
|
||||
if(pipe(fds) == 0)
|
||||
{
|
||||
if(!read_inherited) { fcntl(fds[0], F_SETFD, FD_CLOEXEC); }
|
||||
if(!write_inherited) { fcntl(fds[1], F_SETFD, FD_CLOEXEC); }
|
||||
result.read.u64[0] = fds[0];
|
||||
result.write.u64[0] = fds[1];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_pipe_read(File file, void *out_data, U64 size)
|
||||
{
|
||||
ssize_t read_size = LNX_RETRY_ON_EINTR(read((int)file.u64[0], out_data, size));
|
||||
return read_size > 0 ? (U64)read_size : 0;
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_pipe_write(File file, void *data, U64 size)
|
||||
{
|
||||
ssize_t write_size = LNX_RETRY_ON_EINTR(write((int)file.u64[0], data, size));
|
||||
return write_size > 0 ? (U64)write_size : 0;
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_pipe_bytes_available(File file)
|
||||
{
|
||||
int size = 0;
|
||||
B32 is_ok = !file_match(file, file_zero()) && ioctl((int)file.u64[0], FIONREAD, &size) == 0;
|
||||
return is_ok && size > 0 ? (U64)size : 0;
|
||||
}
|
||||
|
||||
internal B32
|
||||
file_pipe_is_end(File file)
|
||||
{
|
||||
struct pollfd poll_fd = { .fd = (int)file.u64[0], .events = POLLIN|POLLHUP };
|
||||
poll(&poll_fd, 1, 0);
|
||||
return !!(poll_fd.revents & (POLLHUP|POLLERR|POLLNVAL));
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_read(File file, Rng1U64 rng, void *out_data)
|
||||
{
|
||||
@@ -1248,13 +1293,43 @@ process_launch(ProcessLaunchParams *params)
|
||||
int chdir_code = posix_spawn_file_actions_addchdir_np(&file_actions, (char *)push_cstr(scratch.arena, params->path).str);
|
||||
Assert(chdir_code == 0);
|
||||
}
|
||||
int stdout_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stdout_file.u64[0], STDOUT_FILENO);
|
||||
int stderr_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stderr_file.u64[0], STDERR_FILENO);
|
||||
int stdin_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stdin_file.u64[0], STDIN_FILENO);
|
||||
if(!file_match(params->stdout_file, file_zero()))
|
||||
{
|
||||
int stdout_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stdout_file.u64[0], STDOUT_FILENO);
|
||||
Assert(stdout_code == 0);
|
||||
}
|
||||
if(!file_match(params->stderr_file, file_zero()))
|
||||
{
|
||||
int stderr_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stderr_file.u64[0], STDERR_FILENO);
|
||||
Assert(stderr_code == 0);
|
||||
}
|
||||
if(!file_match(params->stdin_file, file_zero()))
|
||||
{
|
||||
int stdin_code = posix_spawn_file_actions_adddup2(&file_actions, (int)params->stdin_file.u64[0], STDIN_FILENO);
|
||||
Assert(stdin_code == 0);
|
||||
}
|
||||
File std_files[] = { params->stdout_file, params->stderr_file, params->stdin_file };
|
||||
for EachIndex(i, ArrayCount(std_files))
|
||||
{
|
||||
int fd = (int)std_files[i].u64[0];
|
||||
B32 is_unique = !file_match(std_files[i], file_zero());
|
||||
for EachIndex(j, i) { is_unique &= !file_match(std_files[i], std_files[j]); }
|
||||
if(is_unique && fd > STDERR_FILENO)
|
||||
{
|
||||
int close_code = posix_spawn_file_actions_addclose(&file_actions, fd);
|
||||
Assert(close_code == 0);
|
||||
}
|
||||
}
|
||||
posix_spawnattr_t attr = {0};
|
||||
int attr_init_code = posix_spawnattr_init(&attr);
|
||||
if(attr_init_code == 0)
|
||||
{
|
||||
if(params->new_console || params->process_group.u64[0] != 0)
|
||||
{
|
||||
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETPGROUP);
|
||||
posix_spawnattr_setpgroup(&attr, 0);
|
||||
}
|
||||
|
||||
// package argv
|
||||
char **argv = push_array(scratch.arena, char *, params->cmd_line.node_count + 1);
|
||||
{
|
||||
@@ -1290,6 +1365,12 @@ process_launch(ProcessLaunchParams *params)
|
||||
if(spawn_code == 0)
|
||||
{
|
||||
handle.u64[0] = (U64)pid;
|
||||
if(params->process_group.u64[0] != 0 && !process_group_add(params->process_group, handle))
|
||||
{
|
||||
kill(pid, SIGKILL);
|
||||
waitpid(pid, 0, 0);
|
||||
MemoryZeroStruct(&handle);
|
||||
}
|
||||
}
|
||||
|
||||
// clean up attributes
|
||||
@@ -1310,6 +1391,27 @@ pid_from_process(Process process)
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_poll(Process process, U64 *exit_code_out)
|
||||
{
|
||||
if(process_match(process, process_zero())) { return 0; }
|
||||
siginfo_t info = {0};
|
||||
B32 result = waitid(P_PID, (pid_t)process.u64[0], &info, WEXITED|WNOHANG|WNOWAIT) == 0 && info.si_pid != 0;
|
||||
if(result && exit_code_out != 0)
|
||||
{
|
||||
*exit_code_out = info.si_code == CLD_EXITED ? (U64)info.si_status : (U64)info.si_status + 128;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_is_active(Process process)
|
||||
{
|
||||
B32 result = !process_match(process, process_zero()) &&
|
||||
!process_poll(process, 0) && kill((pid_t)process.u64[0], 0) == 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_join(Process process, U64 endt_us, U64 *exit_code_out)
|
||||
{
|
||||
@@ -1359,6 +1461,51 @@ process_kill(Process process)
|
||||
return is_killed;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_send_ctrl_c(Process process)
|
||||
{
|
||||
B32 result = !process_match(process, process_zero()) && kill((pid_t)process.u64[0], SIGINT) == 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct LNX_ProcessGroup LNX_ProcessGroup;
|
||||
struct LNX_ProcessGroup
|
||||
{
|
||||
pid_t pid;
|
||||
B32 kill_on_close;
|
||||
};
|
||||
|
||||
internal ProcessGroup
|
||||
process_group_make(B32 kill_on_close)
|
||||
{
|
||||
LNX_ProcessGroup *group = malloc(sizeof(*group));
|
||||
if(group != 0) { *group = (LNX_ProcessGroup){ .kill_on_close = kill_on_close }; }
|
||||
return (ProcessGroup){ .u64[0] = (U64)group };
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_group_add(ProcessGroup group, Process process)
|
||||
{
|
||||
LNX_ProcessGroup *lnx_group = (LNX_ProcessGroup *)group.u64[0];
|
||||
if(lnx_group == 0 || lnx_group->pid != 0 || process_match(process, process_zero())) { return 0; }
|
||||
lnx_group->pid = (pid_t)process.u64[0];
|
||||
return 1;
|
||||
}
|
||||
|
||||
internal void
|
||||
process_group_close(ProcessGroup group)
|
||||
{
|
||||
LNX_ProcessGroup *lnx_group = (LNX_ProcessGroup *)group.u64[0];
|
||||
if(lnx_group != 0)
|
||||
{
|
||||
if(lnx_group->kill_on_close && lnx_group->pid != 0)
|
||||
{
|
||||
kill(-lnx_group->pid, SIGKILL);
|
||||
}
|
||||
free(lnx_group);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Dynamically-Loaded Libraries
|
||||
|
||||
|
||||
@@ -15,11 +15,13 @@
|
||||
#include <fcntl.h>
|
||||
#include <features.h>
|
||||
#include <linux/limits.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <signal.h>
|
||||
#include <spawn.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/random.h>
|
||||
#include <sys/sendfile.h>
|
||||
|
||||
+149
-1
@@ -805,6 +805,62 @@ file_close(File file)
|
||||
(void)result;
|
||||
}
|
||||
|
||||
internal FilePair
|
||||
file_pipe_make(B32 read_inherited, B32 write_inherited)
|
||||
{
|
||||
FilePair result = {0};
|
||||
SECURITY_ATTRIBUTES attributes = { .nLength = sizeof(attributes), .bInheritHandle = TRUE };
|
||||
HANDLE read = 0;
|
||||
HANDLE write = 0;
|
||||
if(CreatePipe(&read, &write, &attributes, 0) &&
|
||||
(read_inherited || SetHandleInformation(read, HANDLE_FLAG_INHERIT, 0)) &&
|
||||
(write_inherited || SetHandleInformation(write, HANDLE_FLAG_INHERIT, 0)))
|
||||
{
|
||||
result.read.u64[0] = (U64)read;
|
||||
result.write.u64[0] = (U64)write;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(read != 0) { CloseHandle(read); }
|
||||
if(write != 0) { CloseHandle(write); }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_pipe_read(File file, void *out_data, U64 size)
|
||||
{
|
||||
DWORD read = 0;
|
||||
B32 is_ok = !file_match(file, file_zero()) &&
|
||||
ReadFile((HANDLE)file.u64[0], out_data, safe_cast_u32(size), &read, 0);
|
||||
return is_ok ? read : 0;
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_pipe_write(File file, void *data, U64 size)
|
||||
{
|
||||
DWORD written = 0;
|
||||
B32 is_ok = !file_match(file, file_zero()) &&
|
||||
WriteFile((HANDLE)file.u64[0], data, safe_cast_u32(size), &written, 0);
|
||||
return is_ok ? written : 0;
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_pipe_bytes_available(File file)
|
||||
{
|
||||
DWORD available = 0;
|
||||
B32 is_ok = !file_match(file, file_zero()) &&
|
||||
PeekNamedPipe((HANDLE)file.u64[0], 0, 0, 0, &available, 0);
|
||||
return is_ok ? available : 0;
|
||||
}
|
||||
|
||||
internal B32
|
||||
file_pipe_is_end(File file)
|
||||
{
|
||||
DWORD available = 0;
|
||||
return !PeekNamedPipe((HANDLE)file.u64[0], 0, 0, 0, &available, 0);
|
||||
}
|
||||
|
||||
internal U64
|
||||
file_read(File file, Rng1U64 rng, void *out_data)
|
||||
{
|
||||
@@ -1391,6 +1447,14 @@ process_launch(ProcessLaunchParams *params)
|
||||
{
|
||||
creation_flags |= CREATE_NO_WINDOW;
|
||||
}
|
||||
if(params->new_console)
|
||||
{
|
||||
creation_flags |= CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP;
|
||||
}
|
||||
if(params->process_group.u64[0] != 0)
|
||||
{
|
||||
creation_flags |= CREATE_SUSPENDED;
|
||||
}
|
||||
|
||||
//- rjf: launch
|
||||
BOOL inherit_handles = 0;
|
||||
@@ -1419,7 +1483,19 @@ process_launch(ProcessLaunchParams *params)
|
||||
PROCESS_INFORMATION process_info = {0};
|
||||
if(CreateProcessW(0, (WCHAR*)cmd16.str, 0, 0, inherit_handles, creation_flags, use_null_env_arg ? 0 : (WCHAR*)env16.str, (WCHAR*)dir16.str, &startup_info, &process_info))
|
||||
{
|
||||
result.u64[0] = (U64)process_info.hProcess;
|
||||
B32 group_is_ok = params->process_group.u64[0] == 0 ||
|
||||
AssignProcessToJobObject((HANDLE)params->process_group.u64[0], process_info.hProcess);
|
||||
B32 resume_is_ok = params->process_group.u64[0] == 0 || ResumeThread(process_info.hThread) != (DWORD)-1;
|
||||
if(group_is_ok && resume_is_ok)
|
||||
{
|
||||
result.u64[0] = (U64)process_info.hProcess;
|
||||
}
|
||||
else
|
||||
{
|
||||
TerminateProcess(process_info.hProcess, 999);
|
||||
WaitForSingleObject(process_info.hProcess, INFINITE);
|
||||
CloseHandle(process_info.hProcess);
|
||||
}
|
||||
CloseHandle(process_info.hThread);
|
||||
}
|
||||
|
||||
@@ -1435,6 +1511,25 @@ pid_from_process(Process process)
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_poll(Process process, U64 *exit_code_out)
|
||||
{
|
||||
if(process_match(process, process_zero()) || WaitForSingleObject((HANDLE)process.u64[0], 0) != WAIT_OBJECT_0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
DWORD exit_code = 0;
|
||||
B32 result = GetExitCodeProcess((HANDLE)process.u64[0], &exit_code);
|
||||
if(result && exit_code_out != 0) { *exit_code_out = exit_code; }
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_is_active(Process process)
|
||||
{
|
||||
return !process_match(process, process_zero()) && !process_poll(process, 0);
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_join(Process process, U64 endt_us, U64 *exit_code_out)
|
||||
{
|
||||
@@ -1472,6 +1567,59 @@ process_kill(Process process)
|
||||
return was_terminated;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_send_ctrl_c(Process process)
|
||||
{
|
||||
B32 result = 0;
|
||||
if(process_match(process, process_zero())) { return 0; }
|
||||
B32 had_console = GetConsoleCP() != 0;
|
||||
if(had_console && !FreeConsole()) { return 0; }
|
||||
if(AttachConsole(safe_cast_u32(pid_from_process(process)))) {
|
||||
SetConsoleCtrlHandler(0, TRUE);
|
||||
result = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) != 0;
|
||||
if(result) { Sleep(10); }
|
||||
}
|
||||
FreeConsole();
|
||||
if(had_console) { AttachConsole(ATTACH_PARENT_PROCESS); }
|
||||
SetConsoleCtrlHandler(0, FALSE);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal ProcessGroup
|
||||
process_group_make(B32 kill_on_close)
|
||||
{
|
||||
ProcessGroup result = {0};
|
||||
HANDLE handle = CreateJobObjectW(0, 0);
|
||||
if(handle != 0)
|
||||
{
|
||||
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits = {0};
|
||||
if(kill_on_close) { limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; }
|
||||
if(SetInformationJobObject(handle, JobObjectExtendedLimitInformation, &limits, sizeof(limits)))
|
||||
{
|
||||
result.u64[0] = (U64)handle;
|
||||
}
|
||||
else
|
||||
{
|
||||
CloseHandle(handle);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
process_group_add(ProcessGroup group, Process process)
|
||||
{
|
||||
B32 result = group.u64[0] != 0 && !process_match(process, process_zero()) &&
|
||||
AssignProcessToJobObject((HANDLE)group.u64[0], (HANDLE)process.u64[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void
|
||||
process_group_close(ProcessGroup group)
|
||||
{
|
||||
if(group.u64[0] != 0) { CloseHandle((HANDLE)group.u64[0]); }
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Dynamically-Loaded Libraries
|
||||
|
||||
|
||||
Reference in new issue
Block a user