Use sockets instead of pipes to allow bidirectional communication. - geomyidae - A small C-based gopherd. (gopher://bitreich.org/1/scm/geomyidae)
git clone git://r-36.net/geomyidae
Log
Files
Refs
README
LICENSE
---
commit 3a9cbc78b4777d2dab54bca974f65c708ffc61a5
parent 5359e24388be689acc073af1739af4c932a57915
Author: Christoph Lohmann <20h@r-36.net>
Date:   Fri, 26 May 2023 06:28:28 +0200

Use sockets instead of pipes to allow bidirectional communication.

* In CGI it was already possible to completely speak two way with the
  client.
* In DCGI this allows the same behaviour as in CGI.
* For TLS this is a preparation for TLS support too.

Diffstat:
  M handlr.c                            |      14 +++++++-------
  M main.c                              |      16 ++++++++--------

2 files changed, 15 insertions(+), 15 deletions(-)
---
diff --git a/handlr.c b/handlr.c
@@ -192,10 +192,10 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
         char *p, *path, *ln = NULL;
         size_t linesiz = 0;
         ssize_t n;
-        int outpipe[2], ret = 0;
+        int outsocks[2], ret = 0;
         Elems *el;
 
-        if (pipe(outpipe) < 0)
+        if (socketpair(AF_LOCAL, SOCK_STREAM, 0, outsocks) < 0)
                 return;
 
         path = xstrdup(file);
@@ -218,8 +218,8 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
         while (dup2(sock, 2) < 0 && errno == EINTR);
         switch (fork()) {
         case 0:
-                while (dup2(outpipe[1], 1) < 0 && errno == EINTR);
-                close(outpipe[0]);
+                while (dup2(outsocks[1], 1) < 0 && errno == EINTR);
+                close(outsocks[0]);
                 if (path != NULL) {
                         if (chdir(path) < 0)
                                 break;
@@ -239,11 +239,11 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
                 break;
         default:
                 while (dup2(sock, 1) < 0 && errno == EINTR);
-                close(outpipe[1]);
+                close(outsocks[1]);
 
-                if (!(fp = fdopen(outpipe[0], "r"))) {
+                if (!(fp = fdopen(outsocks[0], "r"))) {
                         perror("fdopen");
-                        close(outpipe[0]);
+                        close(outsocks[0]);
                         break;
                 }
 
diff --git a/main.c b/main.c
@@ -551,7 +551,7 @@ main(int argc, char *argv[])
             nlfdret, *lfdret, listfd, maxlfd, istls = 0,
             dotls = 0, dohaproxy = 0, tcpver = -1, haret = 0,
 #ifdef ENABLE_TLS
-            tlspipe[2], shufbuf[1025],
+            tlssocks[2], shufbuf[1025],
             shuflen, wlen, shufpos,
 #endif /* ENABLE_TLS */
             maxrecv, retl,
@@ -1035,23 +1035,23 @@ read_selector_again:
 
 #ifdef ENABLE_TLS
                         if (istls) {
-                                if (pipe(tlspipe) < 0) {
-                                        perror("tls_pipe");
+                                if (socketpair(AF_LOCAL, SOCK_STREAM, 0, tlssocks) < 0) {
+                                        perror("tls_socketpair");
                                         return 1;
                                 }
 
                                 switch(fork()) {
                                 case 0:
-                                        sock = tlspipe[1];
-                                        close(tlspipe[0]);
+                                        sock = tlssocks[1];
+                                        close(tlssocks[0]);
                                         break;
                                 case -1:
                                         perror("fork");
                                         return 1;
                                 default:
-                                        close(tlspipe[1]);
+                                        close(tlssocks[1]);
                                         do {
-                                                shuflen = read(tlspipe[0], shufbuf, sizeof(shufbuf)-1);
+                                                shuflen = read(tlssocks[0], shufbuf, sizeof(shufbuf)-1);
                                                 if (shuflen == -1 && errno == EINTR)
                                                         continue;
                                                 for (shufpos = 0; shufpos < shuflen; shufpos += wlen) {
@@ -1065,7 +1065,7 @@ read_selector_again:
 
                                         tls_close(tlsclientctx);
                                         tls_free(tlsclientctx);
-                                        close(tlspipe[0]);
+                                        close(tlssocks[0]);
 
                                         waitforpendingbytes(sock);
                                         shutdown(sock, SHUT_RDWR);