libobs: Add return code to process pipe destroy

This commit is contained in:
jp9000 2015-06-08 11:30:51 -07:00
parent 43956388b2
commit 6587b18d59
3 changed files with 29 additions and 7 deletions

View File

@ -15,6 +15,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h>
#include "bmem.h" #include "bmem.h"
#include "pipe.h" #include "pipe.h"
@ -46,12 +47,18 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
return out; return out;
} }
void os_process_pipe_destroy(os_process_pipe_t *pp) int os_process_pipe_destroy(os_process_pipe_t *pp)
{ {
int ret = 0;
if (pp) { if (pp) {
pclose(pp->file); int status = pclose(pp->file);
if (WIFEXITED(status))
ret = (int)(char)WEXITSTATUS(status);
bfree(pp); bfree(pp);
} }
return ret;
} }
size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len) size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)

View File

@ -24,6 +24,7 @@
struct os_process_pipe { struct os_process_pipe {
bool read_pipe; bool read_pipe;
HANDLE handle; HANDLE handle;
HANDLE process;
}; };
static bool create_pipe(HANDLE *input, HANDLE *output) static bool create_pipe(HANDLE *input, HANDLE *output)
@ -41,7 +42,7 @@ static bool create_pipe(HANDLE *input, HANDLE *output)
} }
static inline bool create_proccess(const char *cmd_line, HANDLE stdin_handle, static inline bool create_proccess(const char *cmd_line, HANDLE stdin_handle,
HANDLE stdout_handle) HANDLE stdout_handle, HANDLE *process)
{ {
PROCESS_INFORMATION pi = {0}; PROCESS_INFORMATION pi = {0};
wchar_t *cmd_line_w = NULL; wchar_t *cmd_line_w = NULL;
@ -59,7 +60,7 @@ static inline bool create_proccess(const char *cmd_line, HANDLE stdin_handle,
CREATE_NO_WINDOW, NULL, NULL, &si, &pi); CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
if (success) { if (success) {
CloseHandle(pi.hProcess); *process = pi.hProcess;
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
} }
@ -74,6 +75,7 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
{ {
os_process_pipe_t *pp = NULL; os_process_pipe_t *pp = NULL;
bool read_pipe; bool read_pipe;
HANDLE process;
HANDLE output; HANDLE output;
HANDLE input; HANDLE input;
bool success; bool success;
@ -97,7 +99,7 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
} }
success = create_proccess(cmd_line, read_pipe ? NULL : input, success = create_proccess(cmd_line, read_pipe ? NULL : input,
read_pipe ? output : NULL); read_pipe ? output : NULL, &process);
if (!success) { if (!success) {
goto error; goto error;
} }
@ -105,6 +107,7 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
pp = bmalloc(sizeof(*pp)); pp = bmalloc(sizeof(*pp));
pp->handle = read_pipe ? input : output; pp->handle = read_pipe ? input : output;
pp->read_pipe = read_pipe; pp->read_pipe = read_pipe;
pp->process = process;
CloseHandle(read_pipe ? output : input); CloseHandle(read_pipe ? output : input);
return pp; return pp;
@ -115,12 +118,24 @@ error:
return NULL; return NULL;
} }
void os_process_pipe_destroy(os_process_pipe_t *pp) int os_process_pipe_destroy(os_process_pipe_t *pp)
{ {
int ret = 0;
if (pp) { if (pp) {
DWORD code;
CloseHandle(pp->handle); CloseHandle(pp->handle);
WaitForSingleObject(pp->process, INFINITE);
if (GetExitCodeProcess(pp->process, &code))
ret = (int)code;
CloseHandle(pp->process);
bfree(pp); bfree(pp);
} }
return ret;
} }
size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len) size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)

View File

@ -23,7 +23,7 @@ typedef struct os_process_pipe os_process_pipe_t;
EXPORT os_process_pipe_t *os_process_pipe_create(const char *cmd_line, EXPORT os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
const char *type); const char *type);
EXPORT void os_process_pipe_destroy(os_process_pipe_t *pp); EXPORT int os_process_pipe_destroy(os_process_pipe_t *pp);
EXPORT size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, EXPORT size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data,
size_t len); size_t len);