Skip to content

Proc

Description

Opaque platform-dependent handle storing process information.

Usage example (Cross-references)

Usage examples (Cross-references)
    #define _DEFAULT_SOURCE
    
    #include <Misra/Sys/Proc.h>
    #include <Misra/Sys.h>
    #include <Misra/Std/Memory.h>
    #endif
    
    struct Proc {
        int  exit_code;
        bool completed;
        HANDLE              hStderrRead;
    #else
    #    error "Unsupported OS for Proc"
    #endif
    };
    #define WRITE_END 1
    
    Proc *ProcCreate(const char *filepath, char **argv, char **envp, Allocator *alloc) {
        if (!alloc) {
            LOG_FATAL("ProcCreate requires an allocator");
        close(stderr_pipe[WRITE_END]);
    
        Proc *proc = (Proc *)AllocatorAlloc(alloc, sizeof(Proc), true);
    
        if (!proc) {
        CloseHandle(hStderrWrite); // parent won't write to child's stderr, will read from it
    
        Proc *proc = (Proc *)AllocatorAlloc(alloc, sizeof(Proc), true);
    
        if (!proc) {
    }
    
    ProcStatus ProcWait(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
    }
    
    ProcStatus ProcWaitFor(Proc *proc, u64 timeout_ms) {
        if (!proc) {
            LOG_FATAL("Invalid arguments");
    }
    
    void ProcTerminate(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
    
    
    void ProcDestroy(Proc *proc, Allocator *alloc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
        CloseHandle(proc->pi.hProcess);
    #endif
        AllocatorFree(alloc, proc, sizeof(Proc));
    }
    }
    
    i32 ProcWriteToStdin(Proc *proc, Str *buf) {
        if (!proc || !buf) {
            LOG_FATAL("Invalid arguments");
    }
    
    i32 sys_proc_read_internal(Proc *proc, Str *buf, bool is_stdout) {
        if (!proc || !buf) {
            LOG_FATAL("Invalid argument");
    }
    
    i32 ProcReadFromStdout(Proc *proc, Str *buf) {
        return sys_proc_read_internal(proc, buf, /* is stdout*/ true);
    }
    }
    
    i32 ProcReadFromStderr(Proc *proc, Str *buf) {
        return sys_proc_read_internal(proc, buf, /* is stdout*/ false);
    }
    }
    
    i32 ProcGetId(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
    }
    
    i32 ProcIsRunning(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
    }
    
    i32 ProcGetExitCode(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
    
    #if MISRA_HAVE_SYS_PROC
    #    include <Misra/Sys/Proc.h>
    #endif
    /// FAILURE: NULL.
    ///
    Proc *ProcCreate(const char *path, char **argv, char **envp, Allocator *alloc);
    
    ///
    /// `SYS_PROC_STATUS_ERROR`.
    ///
    ProcStatus ProcWait(Proc *proc);
    
    ///
    /// waiting fails.
    ///
    ProcStatus ProcWaitFor(Proc *proc, u64 timeout);
    
    ///
    /// FAILURE: Abort with log message.
    ///
    void ProcTerminate(Proc *proc);
    
    ///
    /// FAILURE: Abort with log message.
    ///
    void ProcDestroy(Proc *proc, Allocator *alloc);
    
    ///
    /// FAILURE: Return -1
    ///
    i32 ProcWriteToStdin(Proc *proc, Str *buf);
    
    ///
    /// FAILURE: Abort with a log message.
    ///
    i32 ProcGetExitCode(Proc *proc);
    
    ///
    /// FAILURE: Return -1
    ///
    i32 ProcReadFromStdout(Proc *proc, Str *buf);
    
    ///
    /// FAILURE: Return -1
    ///
    i32 ProcReadFromStderr(Proc *proc, Str *buf);
    
    ///
    /// FAILURE: Returns -1
    ///
    i32 ProcGetId(Proc *proc);
    
    ///
    /// proc[in] : Child process handle.
    ///
    ProcStatus ProcGetStatus(Proc *proc);
    
    ///
    
        // create a new child process
        Proc *proc = ProcCreate(argv[1], argv + 1, envp, &alloc.base);
    
        // write something to it's stdout
        ELF_MACHINE_XTENSA       = 94,  /// Tensilica Xtensa Architecture
        ELF_MACHINE_VIDEOCORE    = 95,  /// Alphamosaic VideoCore
        ELF_MACHINE_TMM_GPP      = 96,  /// Thompson Multimedia General Purpose Proc
        ELF_MACHINE_NS32K        = 97,  /// National Semi. 32000
        ELF_MACHINE_TPC          = 98,  /// Tenor Network TPC
Last updated on