Z-IO-0.1.6.0: cbits/hs_uv_process.c
/*
* Copyright (c) 2017-2019 Dong Han
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the authors or the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <hs_uv.h>
#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
//
// process
void hs_uv_exit_cb(uv_process_t* handle, int64_t exit_status, int term_signal){
HsInt slot = (HsInt)handle->data;
uv_loop_t* loop = handle->loop;
hs_loop_data* loop_data = loop->data;
loop_data->buffer_size_table[slot] = (HsInt)exit_status;
loop_data->event_queue[loop_data->event_counter] = slot; // push the slot to event queue
loop_data->event_counter += 1;
free_slot(loop_data, slot); // free the uv_process_t
}
HsInt hs_uv_spawn(uv_loop_t* loop
, uv_process_options_t* options
, const char* file
#if __GLASGOW_HASKELL__ < 810
, const StgMutArrPtrs* all_args_arr
, const HsInt args_len
, const StgMutArrPtrs* all_env_arr
, const HsInt env_len
#else
, const StgArrBytes** all_args
, const HsInt args_len
, const StgArrBytes** all_env
, const HsInt env_len
#endif
, const char* cwd
, uv_stdio_container_t* container){
#if __GLASGOW_HASKELL__ < 810
StgArrBytes **all_args = (StgArrBytes**)all_args_arr->payload;
StgArrBytes **all_env = (StgArrBytes**)all_env_arr->payload;
#endif
int r;
HsInt i;
char **args = NULL, **env = NULL;
hs_loop_data* loop_data = loop->data;
HsInt slot = alloc_slot(loop_data);
if (slot < 0) return UV_ENOMEM;
uv_process_t* handle =
(uv_process_t*)fetch_uv_struct(loop_data, slot);
handle->data = (void*)slot;
options->exit_cb = hs_uv_exit_cb;
options->file = file;
i = 0;
args = (char**)malloc(sizeof(char*)*(args_len + 2));
if (args == NULL) return UV_ENOMEM;
args[i++] = (char*)file;
while(i <= args_len) {
args[i] = (char*)all_args[i-1]->payload;
i++;
}
args[i] = NULL;
options->args = args;
i = 0;
// env_len == -1 for inherit from parent
if (env_len >= 0){
env = (char**)malloc(sizeof(char*)*(env_len+1));
if (env == NULL) return UV_ENOMEM;
while(i < env_len) {
env[i] = (char*)all_env[i]->payload;
i++;
}
env[i] = NULL;
}
options->env = env;
options->stdio_count = 3;
options->stdio = container;
options->cwd = cwd;
r = uv_spawn(loop, handle, options);
free(args);
if (env != NULL) free(env);
if (r < 0) {
free_slot(loop_data, slot); // free the uv_process_t, the callback won't fired
return (HsInt)r;
} else {
loop_data->buffer_size_table[slot] = (HsInt)handle->pid;
return slot;
}
}