static void master_delete_child(MASTER_PROC *proc) { MASTER_SERV *serv; /* * Undo the things that master_spawn did. Stop the process if it still * exists, and remove it from the lookup tables. Update the number of * available processes. */ serv = proc->serv; serv->total_proc--; if (proc->avail == MASTER_STAT_AVAIL) master_avail_less(serv, proc); else master_avail_listen(serv); binhash_delete(master_child_table, (void *) &proc->pid, sizeof(proc->pid), (void (*) (void *)) 0); myfree((void *) proc); }
static void master_status_event(int event, char *context) { const char *myname = "master_status_event"; MASTER_SERV *serv = (MASTER_SERV *) context; MASTER_STATUS stat; MASTER_PROC *proc; MASTER_PID pid; int n; if (event == 0) /* XXX Can this happen? */ return; /* * We always keep the child end of the status pipe open, so an EOF read * condition means that we're seriously confused. We use non-blocking * reads so that we don't get stuck when someone sends a partial message. * Messages are short, so a partial read means someone wrote less than a * whole status message. Hopefully the next read will be in sync again... * We use a global child process status table because when a child dies * only its pid is known - we do not know what service it came from. */ switch (n = read(serv->status_fd[0], (char *) &stat, sizeof(stat))) { case -1: msg_warn("%s: read: %m", myname); return; case 0: msg_panic("%s: read EOF status", myname); /* NOTREACHED */ default: msg_warn("service %s(%s): child (pid %d) sent partial status update (%d bytes)", serv->ext_name, serv->name, stat.pid, n); return; case sizeof(stat): pid = stat.pid; if (msg_verbose) msg_info("%s: pid %d gen %u avail %d", myname, stat.pid, stat.gen, stat.avail); } /* * Sanity checks. Do not freak out when the child sends garbage because * it is confused or for other reasons. However, be sure to freak out * when our own data structures are inconsistent. A process not found * condition can happen when we reap a process before receiving its * status update, so this is not an error. */ if ((proc = (MASTER_PROC *) binhash_find(master_child_table, (char *) &pid, sizeof(pid))) == 0) { if (msg_verbose) msg_info("%s: process id not found: %d", myname, stat.pid); return; } if (proc->gen != stat.gen) { msg_info("ignoring status update from child pid %d generation %u", pid, stat.gen); return; } if (proc->serv != serv) msg_panic("%s: pointer corruption: %p != %p", myname, (void *) proc->serv, (void *) serv); /* * Update our idea of the child process status. Allow redundant status * updates, because different types of events may be processed out of * order. Otherwise, warn about weird status updates but do not take * action. It's all gossip after all. */ if (proc->avail == stat.avail) return; switch (stat.avail) { case MASTER_STAT_AVAIL: proc->use_count++; master_avail_more(serv, proc); break; case MASTER_STAT_TAKEN: master_avail_less(serv, proc); break; default: msg_warn("%s: ignoring unknown status: %d allegedly from pid: %d", myname, stat.pid, stat.avail); break; } }