2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
14 set_close_on_exec(int fd)
16 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
20 my_stat(const char *path, struct stat *stp)
22 return (stat(path, stp));
26 my_open(const char *path, int flags, int mode)
28 return (open(path, flags, mode));
32 my_remove(const char *path)
34 return (remove(path));
38 my_rename(const char *path1, const char *path2)
40 return (rename(path1, path2));
44 my_mkdir(const char *path, int mode)
46 return (mkdir(path, mode));
50 my_getcwd(char *buffer, int maxlen)
52 return (getcwd(buffer, maxlen));
56 set_non_blocking_mode(int fd)
61 if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
62 DBG(("nonblock: fcntl(F_GETFL): %d", ERRNO));
63 } else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) {
64 DBG(("nonblock: fcntl(F_SETFL): %d", ERRNO));
66 ret = 0; /* Success */
74 spawn_process(struct conn *c, const char *prog, char *envblk,
75 char *envp[], int sock, const char *dir)
79 const char *p, *interp = c->ctx->options[OPT_CGI_INTERPRETER];
81 envblk = NULL; /* unused */
83 if ((pid = vfork()) == -1) {
86 elog(E_LOG, c, "redirect: fork: %s", strerror(errno));
88 } else if (pid == 0) {
95 (void) closesocket(sock);
97 /* If error file is specified, send errors there */
98 if (c->ctx->error_log)
99 (void) dup2(fileno(c->ctx->error_log), 2);
101 if ((p = strrchr(prog, '/')) != NULL)
106 /* Execute CGI program */
107 if (interp == NULL) {
108 (void) execle(p, p, NULL, envp);
109 elog(E_FATAL, c, "redirect: exec(%s)", prog);
111 (void) execle(interp, interp, p, NULL, envp);
112 elog(E_FATAL, c, "redirect: exec(%s %s)",
123 (void) closesocket(sock);