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)
80 envblk = NULL; /* unused */
82 if ((pid = vfork()) == -1) {
85 elog(E_LOG, c, "redirect: fork: %s", strerror(errno));
87 } else if (pid == 0) {
93 (void) closesocket(sock);
95 /* If error file is specified, send errors there */
96 if (c->ctx->error_log)
97 (void) dup2(fileno(c->ctx->error_log), 2);
99 /* Execute CGI program */
100 if (c->ctx->cgi_interpreter == NULL) {
101 (void) execle(prog, prog, NULL, envp);
102 elog(E_FATAL, c, "redirect: exec(%s)", prog);
104 (void) execle(c->ctx->cgi_interpreter,
105 c->ctx->cgi_interpreter, prog, NULL, envp);
106 elog(E_FATAL, c, "redirect: exec(%s %s)",
107 c->ctx->cgi_interpreter, prog);
118 (void) closesocket(sock);