Skip to content

Proc

Description

Process handle. Layout is platform-conditional. Stack-declare with ProcInit(path, argv, envp); don’t poke fields directly. The _pid (POSIX) or _pi.hProcess (Windows) being zero / NULL means the init failed – check with ProcOk(&p).

Usage example (Cross-references)

Usage examples (Cross-references)
    
    #if FEATURE_SYS_PROC
    #    include <Misra/Sys/Proc.h>
    #endif
        int   _stderr_fd; // read here from child error output
    #endif
        } Proc;
    
        ///
        /// TAGS: Sys, Proc, Spawn
        ///
        Proc proc_init(Zstr path, char **argv, char **envp, Allocator *alloc);
    
    #define ProcInit(...)                       OVERLOAD(ProcInit, __VA_ARGS__)
        /// TAGS: Sys, Proc, Deinit
        ///
        void ProcDeinit(Proc *p);
    
        ///
        /// TAGS: Proc, Query, State
        ///
        static inline bool ProcOk(const Proc *p) {
            if (!p) {
                return false;
        /// TAGS: Proc, Wait, API
        ///
        ProcStatus ProcWait(Proc *proc);
    
        ///
        /// TAGS: Proc, Wait, Timeout
        ///
        ProcStatus ProcWaitFor(Proc *proc, u64 timeout_ms);
    
        ///
        /// TAGS: Proc, Terminate, API
        ///
        void ProcTerminate(Proc *proc);
    
        ///
        /// TAGS: Proc, Write, Stdio
        ///
        i32 ProcWriteToStdin(Proc *proc, const Str *buf);
    
        ///
        /// TAGS: Proc, Get, ExitCode
        ///
        i32 ProcGetExitCode(Proc *proc);
    
        ///
        /// TAGS: Proc, Read, Stdio
        ///
        i32 ProcReadFromStdout(Proc *proc, Str *buf);
    
        ///
        /// TAGS: Proc, Read, Stdio
        ///
        i32 ProcReadFromStderr(Proc *proc, Str *buf);
    
        ///
        /// TAGS: Proc, Get, Identity
        ///
        i32 ProcGetId(Proc *proc);
    
        ///
        /// TAGS: Proc, Get, Status
        ///
        ProcStatus ProcGetStatus(Proc *proc);
    
        ///
    /// System functions for cross-platform process creation and interaction
    
    #include <Misra/Sys/Proc.h>
    #include <Misra/Std/Log.h>
    #include <Misra/Std/Memory.h>
    #define WRITE_END 1
    
    Proc proc_init(Zstr filepath, char **argv, char **envp, Allocator *alloc) {
        Proc proc = {0};
    #if PLATFORM_UNIX
    
    Proc proc_init(Zstr filepath, char **argv, char **envp, Allocator *alloc) {
        Proc proc = {0};
    #if PLATFORM_UNIX
        (void)alloc; // POSIX path doesn't need an allocator
    }
    
    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 ProcDeinit(Proc *proc) {
        if (!proc) {
            return;
    }
    
    i32 ProcWriteToStdin(Proc *proc, const Str *buf) {
        if (!proc || !buf) {
            LOG_FATAL("Invalid arguments");
    }
    
    static i32 proc_read_internal(Proc *proc, Str *buf, bool is_stdout) {
        if (!proc || !buf) {
            LOG_FATAL("Invalid argument");
    }
    
    i32 ProcReadFromStdout(Proc *proc, Str *buf) {
        return proc_read_internal(proc, buf, /* is stdout*/ true);
    }
    }
    
    i32 ProcReadFromStderr(Proc *proc, Str *buf) {
        return proc_read_internal(proc, buf, /* is stdout*/ false);
    }
    }
    
    i32 ProcGetId(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
    }
    
    ProcStatus ProcGetStatus(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
    }
    
    i32 ProcGetExitCode(Proc *proc) {
        if (!proc) {
            LOG_FATAL("Invalid argument");
Last updated on