Browse Source

Cope with unknown pids in various send msg commands without terminal failure

master
Con Kolivas 10 years ago
parent
commit
646d4a9560
  1. 60
      src/ckpool.c

60
src/ckpool.c

@ -237,7 +237,25 @@ static int pid_wait(const pid_t pid, const int ms)
return ret; return ret;
} }
static int send_procmsg(const proc_instance_t *pi, const char *buf) static int get_proc_pid(const proc_instance_t *pi)
{
int ret, pid = 0;
char path[256];
FILE *fp;
sprintf(path, "%s%s.pid", pi->ckp->socket_dir, pi->processname);
fp = fopen(path, "re");
if (!fp)
goto out;
ret = fscanf(fp, "%d", &pid);
if (ret < 1)
pid = 0;
fclose(fp);
out:
return pid;
}
static int send_procmsg(proc_instance_t *pi, const char *buf)
{ {
char *path = pi->us.path; char *path = pi->us.path;
int ret = -1; int ret = -1;
@ -251,6 +269,12 @@ static int send_procmsg(const proc_instance_t *pi, const char *buf)
LOGERR("Attempted to send null message to socket %s in send_proc", path); LOGERR("Attempted to send null message to socket %s in send_proc", path);
goto out; goto out;
} }
if (unlikely(!pi->pid)) {
pi->pid = get_proc_pid(pi);
if (!pi->pid)
goto out;
}
if (unlikely(kill_pid(pi->pid, 0))) { if (unlikely(kill_pid(pi->pid, 0))) {
LOGALERT("Attempting to send message %s to dead process %s", buf, pi->processname); LOGALERT("Attempting to send message %s to dead process %s", buf, pi->processname);
goto out; goto out;
@ -474,24 +498,6 @@ out:
static void childsighandler(const int sig); static void childsighandler(const int sig);
static int get_proc_pid(const proc_instance_t *pi)
{
int ret, pid = 0;
char path[256];
FILE *fp;
sprintf(path, "%s%s.pid", pi->ckp->socket_dir, pi->processname);
fp = fopen(path, "re");
if (!fp)
goto out;
ret = fscanf(fp, "%d", &pid);
if (ret < 1)
pid = 0;
fclose(fp);
out:
return pid;
}
struct proc_message { struct proc_message {
proc_instance_t *pi; proc_instance_t *pi;
char *msg; char *msg;
@ -528,10 +534,16 @@ void *async_send_proc(void *arg)
} }
/* At startup the pid fields are not set up before some processes are /* At startup the pid fields are not set up before some processes are
* forked so they never inherit them. */ * forked so they never inherit them. */
if (unlikely(!pi->pid)) if (unlikely(!pi->pid)) {
pi->pid = get_proc_pid(pi); pi->pid = get_proc_pid(pi);
if (unlikely(kill_pid(pi->pid, 0))) { if (!pi->pid) {
LOGALERT("Attempting to send message %s to non existent process %s", msg, pi->processname); LOGALERT("Attempting to send message %s to non existent process %s", msg, pi->processname);
goto out_nofail;
}
}
if (unlikely(kill_pid(pi->pid, 0))) {
LOGALERT("Attempting to send message %s to non existent process %s pid %d",
msg, pi->processname, pi->pid);
goto out; goto out;
} }
sockd = open_unix_client(path); sockd = open_unix_client(path);
@ -551,6 +563,7 @@ out:
LOGERR("Failure in send_proc from %s %s:%d", file, func, line); LOGERR("Failure in send_proc from %s %s:%d", file, func, line);
childsighandler(15); childsighandler(15);
} }
out_nofail:
free(msg); free(msg);
free(pm); free(pm);
return NULL; return NULL;
@ -586,8 +599,11 @@ char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, co
LOGERR("Attempted to send null message to socket %s in send_proc", path); LOGERR("Attempted to send null message to socket %s in send_proc", path);
goto out; goto out;
} }
if (unlikely(!pi->pid)) if (unlikely(!pi->pid)) {
pi->pid = get_proc_pid(pi); pi->pid = get_proc_pid(pi);
if (!pi->pid)
goto out;
}
if (unlikely(kill_pid(pi->pid, 0))) { if (unlikely(kill_pid(pi->pid, 0))) {
/* Reset the pid value in case we are still looking for an old /* Reset the pid value in case we are still looking for an old
* process */ * process */

Loading…
Cancel
Save