Source

spiffy-cgi-handlers / fork-exec.c

/* We need this helper to ensure that the fork/exec is atomic because, unlike
 * pthreads, srfi-18 threads are not killed off in the child.
 *
 * based on mod_fastcgi.c from lighttpd-1.4.31
 */

#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "fastcgi.h"
#include "fork-exec.h"

void* create_string_list(int n) {
	if (n > 0) {
		return calloc(n+1, sizeof(char*)); /* Extra one for NULL */
	} else {
		return NULL;
	}

}

void free_string_list(void* pv, int n) {
	int i;
	char** p = (char**)(pv);

	for (i = 0; i <= n; i++) { /* Free the extra one incase it got used. */
		if (p[i]) free(p[i]);
	}
	free(p);
}

void insert_string(void* lv, int n, char* s) {
	char** l = (char**)(lv);

	l[n] = strdup((const char*)(s));
}

void inspect_string_list(void* lv, int n) {
	int i;
	char** l = (char**)(lv);

	for (i = 0; i < n; i++) {
		printf("%d: <%s>\n", i, l[i]);
	}
	if (l[n])
		printf("Not NULL terminated!\n");
	else
		printf("NULL terminated!\n");

}

int fork_exec (int fcgi_fd, void* argsv, void* envv)  {
	pid_t child;
	char** args = (char**)(argsv);
	char** env  = (char**)(envv);

	switch ((child = fork())) {
		case 0: { /* Child */
				size_t i = 0;

				if (fcgi_fd != FCGI_LISTENSOCK_FILENO) {
					close(FCGI_LISTENSOCK_FILENO);
					dup2(fcgi_fd, FCGI_LISTENSOCK_FILENO);
					close(fcgi_fd);
				}

				for (i = 3; i < 256; i++) {
					close(i);
				}

				/* reset signals */
#ifdef SIGTTOU
				signal(SIGTTOU, SIG_DFL);
#endif
#ifdef SIGTTIN
				signal(SIGTTIN, SIG_DFL);
#endif
#ifdef SIGTSTP
				signal(SIGTSTP, SIG_DFL);
#endif
				signal(SIGHUP, SIG_DFL);
				signal(SIGPIPE, SIG_DFL);
				signal(SIGUSR1, SIG_DFL);

				errno = 0;
				execve(args[0], args, env); /* args[0] might not be a basename! */
				exit(errno);

				break;
			}
		case -1: { /* Error */
				 return 0;
			 }
		default: { /* Parent */
				 return child;
			 }
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.