X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=www%2Fwcgi.c;h=b97bbd43c2fe610d7fe9eea59cb6f6d08007d1d4;hb=7f332a840baf336e34d612cdee0ef27901f0dd11;hp=4ab8aa056d31bf276ee38f8bc716c061fa766526;hpb=5414eafed41a60ef3625cfc6770184b36016186e;p=egate.git diff --git a/www/wcgi.c b/www/wcgi.c index 4ab8aa0..b97bbd4 100644 --- a/www/wcgi.c +++ b/www/wcgi.c @@ -41,7 +41,76 @@ * USE OR PERFORMANCE OF THIS SOFTWARE. * * $Log: wcgi.c,v $ - * Revision 1.2 1995/10/23 16:55:36 adam + * Revision 1.20 2001/02/26 14:32:36 adam + * Updated for YAZ 1.7. HTML output tidy up. Added LOC target. + * + * Revision 1.19 1997/01/24 13:13:10 adam + * Implemnted egw_source and added a "raw" option to the URL. + * Fixed a bug in the buffering system of wproto; the macro wo_putc could + * override memory if it was the first HTML generating function called. + * + * Revision 1.18 1996/03/14 11:48:37 adam + * New function egw_prog that returns name of shell. + * + * Revision 1.17 1996/02/26 10:36:15 adam + * Better error handling when (re)spawn of the shell fails. + * + * Revision 1.16 1996/02/12 10:10:29 adam + * Resource/config system used by the gateway. + * + * 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 + * Handles method GET. + * + * Revision 1.10 1996/01/05 16:21:20 adam + * Bug fix: shell (wproto) sometimes closed server FIFO before cgi + * program opened it - solution: cgi sends OK when response has been read. + * + * Revision 1.9 1995/12/20 16:31:33 adam + * Bug fix: shell might terminate even though new request was initiated + * by the cgi interface program. + * Work on more simple user interface and Europagate buttons. + * + * Revision 1.8 1995/11/08 16:14:35 adam + * Many improvements and bug fixes. + * First version that ran on dtbsun. + * + * Revision 1.7 1995/11/08 12:42:18 adam + * Added descriptive text field in target info. + * Added authentication field in target info. + * + * Revision 1.6 1995/11/06 17:44:22 adam + * State reestablised when shell restarts. History of previous + * result sets. + * + * Revision 1.5 1995/11/06 10:51:15 adam + * End of response marker in response from wsh/wproto to wcgi. + * Shells are respawned when necessary. + * + * Revision 1.4 1995/11/02 16:35:37 adam + * Bug fixes and select on FIFOs in wcgi - doesn't really work! + * + * Revision 1.3 1995/10/31 16:56:24 adam + * Record presentation. + * + * Revision 1.2 1995/10/23 16:55:36 adam * A lot of changes - really. * * Revision 1.1 1995/10/20 11:49:25 adam @@ -51,54 +120,82 @@ #include #include +#include #include -#include #include #include +#include +#include +#ifdef AIX +#include +#endif -#define DEADSTRING "Your database server has terminated. To reactivate \ -the server, please reload the server's 'front page'." - +#include +#include #include "wproto.h" -#define CGIDIR "/usr/local/etc/httpd/cgi-bin" - 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) { printf("Content-type: text/html\n\nServer Failure\n"); printf("%s\n", p); + if (gw_db) + gw_db_close (gw_db); if (*serverp) - unlink(serverp); + unlink (serverp); + if (*serverf) + unlink (serverf); exit(0); } -static int spawn(char *sprog) +static int spawn (char *sprog, int id) { - int r; + int r, fd; char path[256]; + char envstr[80]; - sprintf(path, "%s/%s", CGIDIR, sprog); + sprintf (envstr, "GWID=%d", id); + putenv (envstr); + sprintf(path, "%s/%s", EGWDIR, sprog); switch(r = fork()) { case -1: gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); - exit(1); + fatal ("Internal error in server"); case 0: close (0); close (1); + close (2); gw_log (GW_LOG_DEBUG, prog, "execl %s", path); execl (path, sprog, 0); gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path); - exit(0); + fd = open (serverp, O_WRONLY); + if (fd >= 0) + { + write (fd, "FA", 2); + close (fd); + } + exit(1); default: return r; } } +#if 0 +static void print_environ (void) +{ + extern char **environ; + int i; + + for (i = 0; environ[i]; i++) + gw_log (GW_LOG_DEBUG, prog, "e: %s", environ[i]); +} +#endif /* * NOTE: In the (perhaps odd) terminology used within this software, @@ -108,22 +205,40 @@ static int spawn(char *sprog) */ int main() { - char clientp[256], tmp[256], *path_info, *p, *operation, *t; + char clientp[256], *path_info, *p, *operation, *t; char combuf[COMBUF]; - int linein = -1, lineout, data, childpid; + const char *fifoDir; + GwRes cgiRes; + int serverf_fd = -1; + int linein = -1, lineout, data, gw_id; + if (chdir (EGWDIR)) + fatal ("egwcgi: Couldn't change directory to " EGWDIR); gw_log_init ("egw"); - gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log"); + gw_log_file (GW_LOG_ALL, "egwcgi_log"); +#if 0 gw_log_level (GW_LOG_ALL); +#endif + gw_log_session (getpid()); gw_log (GW_LOG_STAT, prog, "Europagate www cgi server"); + cgiRes = gw_res_init (); + gw_res_merge (cgiRes, "egw.res"); - sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR); - if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0) +#if 1 + gw_log_level (gw_log_mask_str ( + gw_res_get (cgiRes, "log.level", "default"))); +#endif + fifoDir = gw_res_get (cgiRes, "fifo.dir", "/tmp/egw"); + + /* Create fifo directory if it doesn't exist already */ + if (access(fifoDir, R_OK|W_OK) < 0 && mkdir(fifoDir, 0777) < 0) { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp); + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", + fifoDir); fatal("Internal error in server."); } - sprintf(serverp, "%s/srv%d", tmp, getpid()); + /* Delete server FIFO if it does exist */ + sprintf(serverp, "%s/srv%d", fifoDir, getpid()); if (access(serverp, R_OK|W_OK) == 0) { if (unlink(serverp) < 0) @@ -135,90 +250,225 @@ 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", fifoDir, 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'; - if ((childpid = atoi(operation)) <= 0) + gw_log (GW_LOG_DEBUG, prog, "www.db open"); + if (!(gw_db = gw_db_open (EGWDIR "/www.db", 1, 1))) { - childpid = spawn(operation); - /* synchronize with client. */ - gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client."); + 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); + if (r) + { + gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r); + gw_db_close (gw_db); + exit (1); + } + 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 server %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")) { - gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client."); + gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client"); fatal("Internal error in server"); } - gw_log (GW_LOG_DEBUG, prog, "Synchronized."); + gw_log (GW_LOG_DEBUG, prog, "Synchronized"); + sprintf(clientp, "%s/clt%d", fifoDir, gw_id); + gw_log (GW_LOG_DEBUG, prog, "open w %s", clientp); + lineout = open (clientp, O_WRONLY); } - sprintf(clientp, "%s/clt%d", tmp, childpid); - gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp); - if ((lineout = open(clientp, O_WRONLY)) < 0) + else /* A session is continued */ { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp); - fatal(DEADSTRING); + sprintf(clientp, "%s/clt%d", fifoDir, 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; + size_t sprog_size; + int r; + + sprintf (gw_id_str, "%d", gw_id); + r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1, + &sprog, &sprog_size); + if (r != 1) + { + 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 r %s restart", clientp); + spawn (sprog, gw_id); + 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); + fatal("Internal error in server"); + } + if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK")) + { + gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client."); + fatal("Internal error in server"); + } + gw_log (GW_LOG_DEBUG, prog, "Synchronized."); + if ((lineout = open(clientp, O_WRONLY)) < 0) + { + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp); + fatal("Internal error in server"); + } } - gw_log (GW_LOG_DEBUG, prog, "Decoding user data."); + 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_STAT, prog, "P:%s", p); p += strlen(p) + 1; *(p++) = '\0'; /* no envvars tranferred at present */ if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0) { - if (read(0, p, data) < data) - { - gw_log (GW_LOG_FATAL, prog, "Failed to read input."); - fatal("Internal error in server."); + int j, i = 0; + while (i < data) + { + j = read(0, p + i, data - i); + if (j == -1) + { + gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, + "Failed to read input"); + fatal("Internal error in server"); + } + else if (j == 0) + { + gw_log (GW_LOG_ERRNO, prog, "Failed to read input"); + fatal("Internal error in server"); + } + i += j; } } - p += data; - *(p++) = '\0'; + else if ((t = getenv("QUERY_STRING"))) + { + strcpy (p, t); + data = strlen(p); + } + p[data] = '\0'; + gw_log (GW_LOG_DEBUG, prog, "C:%s", p); + p += data+1; data = (p - combuf); memcpy(combuf, &data, sizeof(data)); - gw_log (GW_LOG_DEBUG, prog, "Writing data."); + gw_log (GW_LOG_DEBUG, prog, "Writing data"); if (write(lineout, combuf, data) < data) { gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); - fatal("Internal server error."); + fatal("Internal server error"); } - if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0) + if (linein < 0) { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp); - fatal("Internal error in server."); + 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); + fatal("Internal error in server"); + } } - gw_log (GW_LOG_DEBUG, prog, "Reading response."); + gw_log (GW_LOG_DEBUG, prog, "Reading response"); + while ((data = read(linein, combuf, COMBUF)) > 0) { - if (write(1, combuf, data) < data) - { - gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); - fatal("Internal server error."); - } + gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data); + if (combuf[data-1] == '\0') + break; + if (write(1, combuf, data) < data) + { + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); + exit (1); + } } if (data < 0) { gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read"); - fatal("Internal server error."); + exit (1); + } + if (data > 0) + { + if (close (serverf_fd)) + { + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "close %s", serverf); + } + if (--data > 0) + { + if (write(1, combuf, data) < data) + { + gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write"); + exit (1); + } + } } gw_log (GW_LOG_DEBUG, prog, "Cleaning up."); + unlink (serverf); close(linein); unlink(serverp); close(lineout);