extern void remove_helper(WAITFORHELPER *ptr)
{
int i, found;
if (ptr == NULL)
return;
kill (ptr->pid, SIGTERM); /* just in case child is out there */
do_free (ptr->file);
do_free (ptr->cachefile);
/* DONT free the ptr->node, it's part of the datastruct */
for (i=0, found = 0; !found && i < FD_SETSIZE; ) {
if (conntab[i].c_hptr == ptr)
found = 1;
else
i++;
}
free (ptr);
if (found)
conntab[i].c_hptr = NULL;
return;
}
int next_id;
/* increments next id in memory + on disk */
/* assumes that get_nextid() has been called already */
static void
inc_nextid(void)
{
char line[20];
int dfd;
if (pid == 0) {
fprintf (stderr, "Funny, wait3 returned pid 0!\n");
return; /* don't know if there's a connect out there waiting */
}
else if (pid < 0) {
fprintf (stderr, "wait3 returned %d\n", pid);
perror ("wait3");
return; /* don't know if there's a connect out there waiting */
}
if ((c=find_helper_conn (pid)) < 0) {
if (pid != savedatastruct_pid)
fprintf (stderr, "Unknown what connection %d went with\n", pid);
else {
savedatastruct_pid = 0;
fprintf(stderr, "save_datastruct child.\n");
}
return;
}
else
fprintf (stderr, "Goes with conn #%d, sock %d\n", c, conntab[c].c_socket);
hptr = conntab[c].c_hptr;
/* Assumption: c_hptr->node still points to a valid node in a1[] */
/* if child exited with non-zero status, must be an error */
if (WEXITSTATUS(status) != 0) {
child_punt((hptr->node)->gophertype, (hptr->node)->nodeid,
conntab[c].c_socket, conntab[c].c_output_fmt);
unlink (hptr->file);
return;
}
childfile = fopen (hptr->file,"r");
if (childfile == NULL) { /* File wasn't there ??? */
fprintf (stderr, "Could not open child's output file\n");
perror (hptr->file);
child_punt ((hptr->node)->gophertype, (hptr->node)->nodeid,
conntab[c].c_socket, conntab[c].c_output_fmt);
return;
}
else {
filename2 = tempnam(CACHEDIR, "convert");
/* has to be the same dir as the cache because rename() needs that */
switch ((hptr->node)->gophertype) {
case GOPHTYP_IMAGE:
case GOPHTYP_GIF:
/* No conversion is needed for GIF files */
fclose (childfile);
rename (hptr->file, hptr->cachefile);
nio_send_file(conntab[c].c_socket, hptr->cachefile,
hptr->startpoint, hptr->requested, 0);
remove_helper(hptr);
do_free(filename2);
break;
case GOPHTYP_TEXT:
conv_textfile (childfile, filename2);
rename (filename2, hptr->cachefile);
nio_send_file(conntab[c].c_socket, hptr->cachefile,
hptr->startpoint, hptr->requested, 0);
fclose (childfile); /* conv_... did NOT close file */
unlink (hptr->file); /* remove child's file */
remove_helper(hptr);
do_free (filename2);
break;
case GOPHTYP_MENU:
case GOPHTYP_SEARCH:
if (!create_cached_nlist (childfile, filename2)) {
child_punt((hptr->node)->gophertype,
(hptr->node)->nodeid,
conntab[c].c_socket, conntab[c].c_output_fmt);
}
else {
rename (filename2, hptr->cachefile);
send_cached_nlist (hptr->node, conntab[c].c_socket, hptr->cachefile,
conntab[c].c_output_fmt);
}
fclose (childfile);
unlink (hptr->file); /* remove child's file from the filesystem */
remove_helper(hptr); /* remove the buf associated with child */
do_free(filename2);
break;