<commit_before>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

#include <lua.h>
#include <lauxlib.h>

static int run(lua_State *L) {
	
	// check arguments
	// args: (argv...)
	
	int argc = lua_gettop(L);
	int i = 1;
	

	// argv
	
	if(argc < i) {
		return luaL_error(L, "No command provided to run.");
	}
	
	int argvStart = i;
	for(; i <= argc; i++) {
		luaL_checkstring(L, i);
	}
	
	// OK, sanity check done, prepare child
	
	int result = fork();
	
	if(result == -1) {
		return luaL_error(L, "Couldn't fork");
	}

	if(result != 0) {
		// we are still the supervisor process, return the child PID
		lua_pushinteger(L, result);
		return 1;
	}
	
	// prepare to exec the child process
	
	//typedef char * ConstString;
	const char ** argv = malloc((argc + 1) * sizeof(char*));
	
	for(i = argvStart; i <= argc; i++) {
		argv[i-1] = luaL_checkstring(L, i);
	}
	argv[argc] = NULL;
	
	execvp(argv[0], (char **) argv);
	
	perror("execvp");
	
	exit(1);
}

static int waitChildren(lua_State *L) {
	
	lua_newtable(L);
	
	int status;
	while(1) {
		int result = waitpid(-1, &status, WNOHANG);
		
		if(result <= 0) break;
		
		// TODO: distinguish between exits, signal kills, etc
		lua_pushinteger(L, result); // pid
		lua_pushinteger(L, WEXITSTATUS(status));<commit_msg>Add a malloc sanity-check to poll.c<commit_after>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

#include <lua.h>
#include <lauxlib.h>

static int run(lua_State *L) {
	
	// check arguments
	// args: (argv...)
	
	int argc = lua_gettop(L);
	int i = 1;
	

	// argv
	
	if(argc < i) {
		return luaL_error(L, "No command provided to run.");
	}
	
	int argvStart = i;
	for(; i <= argc; i++) {
		luaL_checkstring(L, i);
	}
	
	// OK, sanity check done, prepare child
	
	int result = fork();
	
	if(result == -1) {
		return luaL_error(L, "Couldn't fork");
	}

	if(result != 0) {
		// we are still the supervisor process, return the child PID
		lua_pushinteger(L, result);
		return 1;
	}
	
	// we are the child, set up enviroment
	
	// set user (if able to)
	
	// prepare to exec the child process
	
	const char ** argv = malloc((argc + 1) * sizeof(char*));
	if(argv == NULL) {
		perror("malloc");
		exit(1);
	}
	
	for(i = argvStart; i <= argc; i++) {
		argv[i-1] = luaL_checkstring(L, i);
	}
	argv[argc] = NULL;
	
	execvp(argv[0], (char **) argv);
	
	perror("execvp");
	
	exit(1);
}

static int waitChildren(lua_State *L) {
	
	lua_newtable(L);
	
	int status;
	while(1) {
		int result = waitpid(-1, &status, WNOHANG);
		
		if(result <= 0) break;
		
		// TODO: distinguish between exits, signal kills, etc
		lua_pushinteger(L, result); // pid
		lua_pushinteger(L, WEXITSTATUS(status));
{'fraction_non_alphanumeric': 0.1666039864610756, 'fraction_numerical': 0.008649868371568259, 'mean_word_length': 3.169278996865204, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7507308', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1013, 'n_words': 283}