Bug fixes and select on FIFOs in wcgi - doesn't really work!
[egate.git] / www / wcgi.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  * $Log: wcgi.c,v $
44  * Revision 1.4  1995/11/02 16:35:37  adam
45  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
46  *
47  * Revision 1.3  1995/10/31  16:56:24  adam
48  * Record presentation.
49  *
50  * Revision 1.2  1995/10/23  16:55:36  adam
51  * A lot of changes - really.
52  *
53  * Revision 1.1  1995/10/20  11:49:25  adam
54  * First version of www gateway.
55  *
56  */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <unistd.h>
61 #include <fcntl.h>
62 #include <sys/stat.h>
63 #include <sys/time.h>
64 #include <sys/types.h>
65 #ifdef AIX
66 #include <sys/select.h>
67 #endif
68
69 #define DEADSTRING "Your database server has terminated. To reactivate \
70 the server, please reload the server's 'front page'."
71
72 #include "wproto.h"
73
74 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
75
76 static char *prog = "cgi";
77
78 static char serverp[256] = {'\0'};
79
80 static void fatal(char *p)
81 {
82     printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
83     printf("<BODY>%s</BODY>\n", p);
84     if (*serverp)
85         unlink(serverp);
86     exit(0);
87 }
88
89 static int spawn(char *sprog)
90 {
91     int r;
92     char path[256];
93
94     sprintf(path, "%s/%s", CGIDIR, sprog);
95     switch(r = fork())
96     {
97         case -1: 
98             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); 
99             exit(1);
100         case 0: 
101             close (0);
102             close (1);
103             gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
104             execl (path, sprog, 0); 
105             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
106             exit(0);
107         default: 
108             return r;
109     }
110 }
111
112
113 /*
114  * NOTE: In the (perhaps odd) terminology used within this software,
115  * the 'server' is the present program, which is executed by the httpd
116  * server. The 'client' is the process running outside.
117  * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
118  */
119 int main()
120 {
121     char clientp[256], tmp[256], *path_info, *p, *operation, *t;
122     char combuf[COMBUF];
123     int linein = -1, lineout, data, childpid;
124
125     gw_log_init ("egw");
126     gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
127     gw_log_level (GW_LOG_ALL);
128     gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
129
130     sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
131     if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
132     {
133         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
134         fatal("Internal error in server.");
135     }
136     sprintf(serverp, "%s/srv%d", tmp, getpid());
137     if (access(serverp, R_OK|W_OK) == 0)
138     {
139         if (unlink(serverp) < 0)
140         {
141             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
142                     "Failed to delete stale fifo.");
143             fatal("Internal error in server.");
144         }
145         else
146             gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
147     }
148     if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
149     {
150         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
151         fatal("Internal error in server.");
152     }
153     if (!(path_info = getenv("PATH_INFO")))
154     {
155         gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
156         fatal("Internal error in server.");
157     }
158     operation = ++path_info;
159     while (*path_info && *path_info != '/')
160         path_info++;
161     if (*path_info)
162         *(path_info++) = '\0';
163     if ((childpid = atoi(operation)) <= 0)
164     {
165         childpid = spawn(operation);
166         /* synchronize with client. */
167         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client.");
168         if ((linein = open(serverp, O_RDONLY)) < 0)
169         {
170             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
171             fatal("Internal error in server.");
172         }
173         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
174         {
175             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
176             fatal("Internal error in server");
177         }
178         gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
179     }
180     sprintf(clientp, "%s/clt%d", tmp, childpid);
181     gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
182     if ((lineout = open(clientp, O_WRONLY)) < 0)
183     {
184         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
185         fatal(DEADSTRING);
186     }
187     gw_log (GW_LOG_DEBUG, prog, "Decoding user data.");
188     p = combuf + sizeof(data);
189     strcpy(p, serverp);
190     p += strlen(p) + 1;
191     strcpy(p, path_info);
192     p += strlen(p) + 1;
193     *(p++) = '\0';               /* no envvars tranferred at present */
194     if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
195     {
196         if (read(0, p, data) < data)
197         {
198             gw_log (GW_LOG_FATAL, prog, "Failed to read input.");
199             fatal("Internal error in server.");
200         }
201     }
202     p += data;
203     *(p++) = '\0';
204     data = (p - combuf);
205     memcpy(combuf, &data, sizeof(data));
206     gw_log (GW_LOG_DEBUG, prog, "Writing data.");
207     if (write(lineout, combuf, data) < data)
208     {
209         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
210         fatal("Internal server error.");
211     }
212     if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
213     {
214         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
215         fatal("Internal error in server.");
216     }
217     gw_log (GW_LOG_DEBUG, prog, "Reading response.");
218
219 #if 0
220     while ((data = read(linein, combuf, COMBUF)) > 0)
221     {
222         gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
223         if (write(1, combuf, data) < data)
224         {
225             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
226             exit (1);
227         }
228     }
229 #else
230 #  if 1
231     fcntl (linein, F_SETFL, O_NONBLOCK);
232 #  endif
233     while (1)
234     {
235         fd_set s_input;
236         struct timeval t;
237         int r;
238     
239         t.tv_sec = 5;
240         t.tv_usec = 0;
241         FD_ZERO(&s_input);
242         FD_SET(linein, &s_input);
243 #  if 0
244         FD_SET(1, &s_output);
245 #  endif
246         gw_log (GW_LOG_DEBUG, prog, "select");
247         r = select (linein + 1, &s_input, NULL, NULL, NULL);
248         if (r < 0)
249         {
250             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
251             exit(1);
252         }
253         if (r == 0) 
254         {
255             gw_log (GW_LOG_DEBUG, prog, "poll");
256             if ((data = read (linein, combuf, COMBUF)) > 0)
257             {
258                 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
259                 if (write(1, combuf, data) < data)
260                 {
261                     gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
262                     exit (1);
263                 }
264             }
265             else if (data == -1)
266             {
267                 gw_log (GW_LOG_DEBUG, prog, "No data");
268             }
269             else
270             {
271                 gw_log (GW_LOG_DEBUG, prog, "No more");
272                 break;
273             }
274             continue;
275         }
276         if (FD_ISSET (linein, &s_input))
277         {
278             data = read (linein, combuf, COMBUF);
279             if (data == 0)
280                 break;
281             else if (data < 0)
282             {
283                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
284                 exit (1);
285             }
286             gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
287             if (write(1, combuf, data) < data)
288             {
289                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
290                 exit (1);
291             }
292         }
293         if (FD_ISSET (1, &s_input))
294         {
295             gw_log (GW_LOG_DEBUG, prog, "stdout closed");
296             break;
297         }
298     }
299 #endif
300     if (data < 0)
301     {
302         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
303         exit (1);
304     }
305     gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");
306     close(linein);
307     unlink(serverp);
308     close(lineout);
309     return 0;
310 }