1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
* Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
* All rights reserved
*
* "THE BEER-WARE LICENSE" (Revision 42):
* Sergey Lyubka wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
*/
#include "defs.h"
void
set_close_on_exec(int fd)
{
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
}
int
my_stat(const char *path, struct stat *stp)
{
return (stat(path, stp));
}
int
my_open(const char *path, int flags, int mode)
{
return (open(path, flags, mode));
}
int
my_remove(const char *path)
{
return (remove(path));
}
int
my_rename(const char *path1, const char *path2)
{
return (rename(path1, path2));
}
int
my_mkdir(const char *path, int mode)
{
return (mkdir(path, mode));
}
char *
my_getcwd(char *buffer, int maxlen)
{
return (getcwd(buffer, maxlen));
}
int
set_non_blocking_mode(int fd)
{
int ret = -1;
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
DBG(("nonblock: fcntl(F_GETFL): %d", ERRNO));
} else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) {
DBG(("nonblock: fcntl(F_SETFL): %d", ERRNO));
} else {
ret = 0; /* Success */
}
return (ret);
}
#ifndef NO_CGI
int
spawn_process(struct conn *c, const char *prog, char *envblk,
char *envp[], int sock, const char *dir)
{
int ret;
pid_t pid;
envblk = NULL; /* unused */
if ((pid = vfork()) == -1) {
ret = -1;
elog(E_LOG, c, "redirect: fork: %s", strerror(errno));
} else if (pid == 0) {
/* Child */
(void) chdir(dir);
(void) dup2(sock, 0);
(void) dup2(sock, 1);
(void) closesocket(sock);
/* If error file is specified, send errors there */
if (c->ctx->error_log)
(void) dup2(fileno(c->ctx->error_log), 2);
/* Execute CGI program */
if (c->ctx->cgi_interpreter == NULL) {
(void) execle(prog, prog, NULL, envp);
elog(E_FATAL, c, "redirect: exec(%s)", prog);
} else {
(void) execle(c->ctx->cgi_interpreter,
c->ctx->cgi_interpreter, prog, NULL, envp);
elog(E_FATAL, c, "redirect: exec(%s %s)",
c->ctx->cgi_interpreter, prog);
}
/* UNREACHED */
ret = -1;
exit(EXIT_FAILURE);
} else {
/* Parent */
ret = 0;
(void) closesocket(sock);
}
return (ret);
}
#endif /* !NO_CGI */
|