X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=www%2Fwcgi.c;h=f6ae6432a063bcd5896b72034516a9163824618c;hb=d1fc0ff84894bba24f827a81597717e9f69b6c06;hp=96536424dde111fcc4e90702d2dddd77f7a22e93;hpb=129b920a3019c8cfcb8ed37b20b1b49fd557b100;p=egate.git diff --git a/www/wcgi.c b/www/wcgi.c index 9653642..f6ae643 100644 --- a/www/wcgi.c +++ b/www/wcgi.c @@ -41,7 +41,22 @@ * USE OR PERFORMANCE OF THIS SOFTWARE. * * $Log: wcgi.c,v $ - * Revision 1.12 1996/01/09 10:46:49 adam + * Revision 1.15 1996/01/26 09:02:20 adam + * Open of client FIFO called with O_NDELAY when reconnecting to shell + * in order to prevent serious lock if previous shell died without + * unlinking client FIFO. + * + * Revision 1.14 1996/01/12 13:08:06 adam + * CGI script passes name of lock file to the shell. The server will not close + * the response FIFO until this file becomes unlocked. This method handles + * cancel operations much better. + * + * Revision 1.13 1996/01/12 10:05:17 adam + * If script name ends with ';' HTTP/GET/Expires will be defined. + * The cgi interface only reads final handshake if response from + * server (shell) was zero-terminated [If it isn't it probably died]. + * + * Revision 1.12 1996/01/09 10:46:49 adam * New defines: LOGDIR/EGWDIR/CGIDIR set in Makefile. * * Revision 1.11 1996/01/08 08:42:19 adam @@ -106,6 +121,7 @@ the server, please reload the server's 'front page'." static char *prog = "cgi"; static char serverp[256] = {'\0'}; +static char serverf[256] = {'\0'}; static GW_DB gw_db = NULL; static void fatal(char *p) @@ -115,7 +131,9 @@ static void fatal(char *p) if (gw_db) gw_db_close (gw_db); if (*serverp) - unlink(serverp); + unlink (serverp); + if (*serverf) + unlink (serverf); exit(0); } @@ -167,20 +185,24 @@ int main() { char clientp[256], tmp[256], *path_info, *p, *operation, *t; char combuf[COMBUF]; + int serverf_fd = -1; int linein = -1, lineout, data, gw_id; chdir ("/usr/local/etc/httpd/cgi-bin"); gw_log_init ("egw"); gw_log_file (GW_LOG_ALL, LOGDIR "/egwcgi_log"); gw_log_level (GW_LOG_ALL); + gw_log_session (getpid()); gw_log (GW_LOG_STAT, prog, "Europagate www cgi server"); + /* Create fifo directory if it doesn't exist already */ sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR); if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0) { gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp); fatal("Internal error in server."); } + /* Delete server FIFO if it does exist */ sprintf(serverp, "%s/srv%d", tmp, getpid()); if (access(serverp, R_OK|W_OK) == 0) { @@ -193,34 +215,62 @@ int main() else gw_log (GW_LOG_WARN, prog, "Removed stale server fifo."); } + /* Make server FIFO */ if (mkfifo(serverp, 0666 | S_IFIFO) < 0) { gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp); fatal("Internal error in server."); } + /* The httpd server must pass PATH_INFO */ if (!(path_info = getenv("PATH_INFO"))) { gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO."); fatal("Internal error in server."); } + /* Create lock file that ensures the server (shell) doesn't */ + /* terminate before we have read the whole response */ + sprintf (serverf, "%s/srf%d", tmp, getpid ()); + gw_log (GW_LOG_DEBUG, prog, "open w %s", serverf); + serverf_fd = open (serverf, O_WRONLY|O_CREAT|O_TRUNC, 0666); + if (serverf_fd == -1) + { + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open (%s)", serverf); + fatal("Internal error in server."); + } + else + { + struct flock area; + area.l_type = F_WRLCK; + area.l_whence = SEEK_SET; + area.l_start = 0L; + area.l_len = 0L; + fcntl (serverf_fd, F_SETLK, &area); + } + /* Read first part of path_info. Either it is a numeric session id */ + /* or it is the name of a backend shell. */ operation = ++path_info; while (*path_info && *path_info != '/') path_info++; if (*path_info) *(path_info++) = '\0'; + gw_log (GW_LOG_DEBUG, prog, "www.db open"); if (!(gw_db = gw_db_open (EGWDIR "/www.db", 1, 1))) { gw_log (GW_LOG_FATAL, prog, "gw_db_open"); exit (1); } + gw_log (GW_LOG_DEBUG, prog, "www.db ok"); + /* Is operation a backend shell (new session) ? */ if ((gw_id = atoi(operation)) <= 0) { int r; char gw_id_str[16]; + /* Get new unique id */ gw_id = gw_db_seq_no (gw_db); sprintf (gw_id_str, "%d", gw_id); - + + /* Spawn backend shell (server) */ spawn(operation, gw_id); r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1, operation, strlen(operation)+1); @@ -233,7 +283,7 @@ int main() gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client"); if ((linein = open(serverp, O_RDONLY)) < 0) { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp); + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open r %s", serverp); fatal("Internal error in server."); } if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK")) @@ -242,10 +292,23 @@ int main() fatal("Internal error in server"); } gw_log (GW_LOG_DEBUG, prog, "Synchronized"); + sprintf(clientp, "%s/clt%d", tmp, gw_id); + gw_log (GW_LOG_DEBUG, prog, "open w %s", clientp); + lineout = open (clientp, O_WRONLY); } - sprintf(clientp, "%s/clt%d", tmp, gw_id); - gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp); - if ((lineout = open(clientp, O_WRONLY)) < 0) + else /* A session is continued */ + { + sprintf(clientp, "%s/clt%d", tmp, gw_id); + gw_log (GW_LOG_DEBUG, prog, "open w|n %s", clientp); + /* Open the FIFO in O_NDELAY-mode: This prevents blocking */ + /* even though the shell died without unlinking the FIFO */ + /* On the other hand, if the shell is running, it will never */ + /* close this FIFO */ + lineout = open (clientp, O_WRONLY|O_NDELAY); + } + /* If open of clientp failed, the shell is not running, so we */ + /* invoke it again */ + if (lineout < 0) { char gw_id_str[16]; void *sprog; @@ -260,7 +323,7 @@ int main() gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str); fatal("Internal error in server"); } - gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp); + gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open r %s restart", clientp); spawn (sprog, gw_id); gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client"); if ((linein = open(serverp, O_RDONLY)) < 0) @@ -283,9 +346,11 @@ int main() gw_db_close (gw_db); gw_log (GW_LOG_DEBUG, prog, "Decoding user data"); p = combuf + sizeof(data); - strcpy(p, serverp); - p += strlen(p) + 1; - strcpy(p, path_info); + strcpy (p, serverp); + p += strlen (p) + 1; + strcpy (p, serverf); + p += strlen (p) + 1; + strcpy (p, path_info); gw_log (GW_LOG_DEBUG, prog, "P:%s", p); p += strlen(p) + 1; *(p++) = '\0'; /* no envvars tranferred at present */ @@ -327,7 +392,7 @@ int main() } if (linein < 0) { - gw_log (GW_LOG_DEBUG, prog, "open %s", serverp); + gw_log (GW_LOG_DEBUG, prog, "open r %s", serverp); if ((linein = open(serverp, O_RDONLY)) < 0) { gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp); @@ -336,7 +401,6 @@ int main() } gw_log (GW_LOG_DEBUG, prog, "Reading response"); -#if 1 while ((data = read(linein, combuf, COMBUF)) > 0) { gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data); @@ -355,93 +419,21 @@ int main() } if (data > 0) { - --data; - if (write(1, combuf, data) < data) + if (close (serverf_fd)) { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); - exit (1); + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "close %s", serverf); } - } -#else -# if 1 - fcntl (linein, F_SETFL, O_NONBLOCK); -# endif - while (1) - { - fd_set s_input; - struct timeval t; - int r, eof_flag = 0; - - t.tv_sec = 10; - t.tv_usec = 0; - FD_ZERO(&s_input); - FD_SET(linein, &s_input); -# if 0 - FD_SET(1, &s_input); -# endif - gw_log (GW_LOG_DEBUG, prog, "select"); - r = select (linein + 1, &s_input, NULL, NULL, &t); - if (r < 0) + if (--data > 0) { - gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select"); - exit(1); - } - if (r == 0 || FD_ISSET (linein, &s_input)) - { - if (r == 0) - gw_log (GW_LOG_DEBUG, prog, "poll"); - if ((data = read (linein, combuf, COMBUF)) > 0) - { - if (combuf[data-1] == '\0') - { - --data; - eof_flag = 1; - } - gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data); - if (data > 0 && write(1, combuf, data) < data) - { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); - exit (1); - } - } - else if (data == -1) - { - if (r > 0) - { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read"); - exit (1); - } - gw_log (GW_LOG_DEBUG, prog, "poll read"); - } - else - break; - } - if (eof_flag) - break; - if (r > 0 && FD_ISSET (1, &s_input)) - { - data = read (1, combuf, COMBUF); - if (data == -1) - { - gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed"); - break; - } - if (data == 0) + if (write(1, combuf, data) < data) { - gw_log (GW_LOG_DEBUG, prog, "stdout closed"); - break; + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); + exit (1); } - gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data); } } -#endif - -#if 1 - gw_log (GW_LOG_DEBUG, prog, "writing ack"); - if (write(lineout, "OK", 2) < 2) - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); -#endif gw_log (GW_LOG_DEBUG, prog, "Cleaning up."); + unlink (serverf); close(linein); unlink(serverp); close(lineout);