[BusyBox] [httpd] use socketpair instead of two pipes for CGI
Rainer Weikusat
rainer.weikusat at sncag.com
Thu Feb 17 18:02:12 UTC 2005
This is a patch that changes to sendCgi function in httpd.c so that it
uses a pair of connected AF_UNIX stream sockets instead of two pipes
for communicating with CGI scripts (504 bytes code size reduction for
ARM).
Index: busybox/networking/httpd.c
===================================================================
RCS file: /data/repo/busybox/networking/httpd.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 httpd.c
--- busybox/networking/httpd.c 30 Nov 2004 16:02:32 -0000 1.1.1.1
+++ busybox/networking/httpd.c 17 Feb 2005 17:57:29 -0000
@@ -1069,22 +1069,17 @@
const char *request, int bodyLen, const char *cookie,
const char *content_type)
{
- int fromCgi[2]; /* pipe for reading data from CGI */
- int toCgi[2]; /* pipe for sending data to CGI */
-
+ int fds[2];
+#define cgi_fd fds[1]
+#define server_fd fds[0]
+
static char * argp[] = { 0, 0 };
int pid = 0;
- int inFd;
- int outFd;
int firstLine = 1;
do {
- if (pipe(fromCgi) != 0) {
- break;
- }
- if (pipe(toCgi) != 0) {
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0)
break;
- }
pid = fork();
if (pid < 0) {
@@ -1101,21 +1096,16 @@
if(purl == NULL)
_exit(242);
- inFd = toCgi[0];
- outFd = fromCgi[1];
-
- dup2(inFd, 0); // replace stdin with the pipe
- dup2(outFd, 1); // replace stdout with the pipe
+ dup2(cgi_fd, 0);
+ dup2(cgi_fd, 1);
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
if (!config->debugHttpd)
#endif
- dup2(outFd, 2); // replace stderr with the pipe
+ dup2(cgi_fd, 2); // replace stderr with the pipe
- close(toCgi[0]);
- close(toCgi[1]);
- close(fromCgi[0]);
- close(fromCgi[1]);
+ close(cgi_fd);
+ close(server_fd);
/*
* Find PATH_INFO.
@@ -1205,10 +1195,7 @@
int status;
size_t post_readed_size = 0, post_readed_idx = 0;
- inFd = fromCgi[0];
- outFd = toCgi[1];
- close(fromCgi[1]);
- close(toCgi[0]);
+ close(cgi_fd);
signal(SIGPIPE, SIG_IGN);
while (1) {
@@ -1220,10 +1207,10 @@
FD_ZERO(&readSet);
FD_ZERO(&writeSet);
- FD_SET(inFd, &readSet);
+ FD_SET(server_fd, &readSet);
if(bodyLen > 0 || post_readed_size > 0) {
- FD_SET(outFd, &writeSet);
- nfound = outFd > inFd ? outFd : inFd;
+ FD_SET(server_fd, &writeSet);
+ nfound = server_fd;
if(post_readed_size == 0) {
FD_SET(a_c_r, &readSet);
if(nfound < a_c_r)
@@ -1232,16 +1219,13 @@
/* Now wait on the set of sockets! */
nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
} else {
- if(!bodyLen) {
- close(outFd);
- bodyLen = -1;
- }
- nfound = select(inFd + 1, &readSet, 0, 0, NULL);
+ if (!bodyLen) bodyLen = -1;
+ nfound = select(server_fd + 1, &readSet, 0, 0, NULL);
}
if (nfound <= 0) {
if (waitpid(pid, &status, WNOHANG) > 0) {
- close(inFd);
+ close(server_fd);
#ifdef DEBUG
if (config->debugHttpd) {
if (WIFEXITED(status))
@@ -1252,8 +1236,8 @@
#endif
break;
}
- } else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
- count = bb_full_write(outFd, wbuf + post_readed_idx, post_readed_size);
+ } else if(post_readed_size > 0 && FD_ISSET(server_fd, &writeSet)) {
+ count = bb_full_write(server_fd, wbuf + post_readed_idx, post_readed_size);
if(count > 0) {
post_readed_size -= count;
post_readed_idx += count;
@@ -1270,7 +1254,7 @@
bodyLen = 0; /* closed */
}
}
- if(FD_ISSET(inFd, &readSet)) {
+ if(FD_ISSET(server_fd, &readSet)) {
int s = a_c_w;
char *rbuf = config->buf;
@@ -1284,7 +1268,7 @@
#endif
// There is something to read
- count = safe_read(inFd, rbuf, PIPESIZE);
+ count = safe_read(server_fd, rbuf, PIPESIZE);
if (count == 0)
break; /* closed */
if (count > 0) {
@@ -1311,6 +1295,9 @@
}
}
return 0;
+
+#undef cgi_fd
+#undef server_fd
}
#endif /* CONFIG_FEATURE_HTTPD_CGI */
More information about the busybox
mailing list