Commit 1d5b03eb authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Improve vtc_wait4() so it can also handle vtc_process' needs.

parent 5823ee99
......@@ -138,7 +138,7 @@ void b64_settings(const struct http *hp, const char *s);
struct vsb *vtc_hex_to_bin(struct vtclog *vl, const char *arg);
void vtc_expect(struct vtclog *, const char *, const char *, const char *,
const char *, const char *);
void vtc_wait4(struct vtclog *, long, int, int);
void vtc_wait4(struct vtclog *, long, int, int, int);
/* vtc_term.c */
struct term *Term_New(struct vtclog *, int, int);
......
......@@ -32,8 +32,6 @@
#include "config.h"
#include <sys/ioctl.h> // Linux: struct winsize
#include <sys/resource.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
......@@ -85,7 +83,6 @@ struct process {
pthread_mutex_t mtx;
pthread_t tp;
unsigned hasthread;
int status;
struct term *term;
int lin;
......@@ -247,10 +244,9 @@ static void *
process_thread(void *priv)
{
struct process *p;
struct rusage ru;
struct vev_root *evb;
struct vev *ev;
int core, sig, ext, r;
int r;
CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
......@@ -293,8 +289,8 @@ process_thread(void *priv)
vtc_fatal(p->vl, "VEV_Once() = %d, error %s", r,
strerror(errno));
r = wait4(p->pid, &p->status, 0, &ru);
vtc_wait4(p->vl, p->pid,
p->expect_exit, p->expect_signal, p->allow_core);
closefd(&p->f_stdout);
closefd(&p->f_stderr);
......@@ -304,35 +300,7 @@ process_thread(void *priv)
macro_undef(p->vl, p->name, "pid");
p->pid = -1;
vtc_log(p->vl, 2, "R 0x%04x Status: %04x (u %.6f s %.6f)",
r, p->status,
ru.ru_utime.tv_sec + 1e-6 * ru.ru_utime.tv_usec,
ru.ru_stime.tv_sec + 1e-6 * ru.ru_stime.tv_usec
);
AZ(pthread_mutex_unlock(&p->mtx));
sig = WTERMSIG(p->status);
ext = WEXITSTATUS(p->status);
#ifdef WCOREDUMP
core = WCOREDUMP(p->status);
vtc_log(p->vl, 2, "Exit code: %04x sig %d exit %d core %d",
p->status, sig, ext, core);
#else
core = 0;
vtc_log(p->vl, 2, "Exit code: %04x sig %d exit %d",
p->status, sig, ext);
#endif
if (core && !p->allow_core)
vtc_fatal(p->vl, "Core dump");
if (p->expect_signal >= 0 && sig != p->expect_signal)
vtc_fatal(p->vl, "Expected signal %d got %d",
p->expect_signal, sig);
else if (sig != 0 && sig != -p->expect_signal)
vtc_fatal(p->vl, "Expected signal %d got %d",
-p->expect_signal, sig);
if (ext != p->expect_exit)
vtc_fatal(p->vl, "Expected exit %d got %d",
p->expect_exit, ext);
VEV_Destroy(&evb);
if (p->log == 1) {
......
......@@ -132,8 +132,22 @@ vtc_expect(struct vtclog *vl,
olhs, lhs, cmp, rhs);
}
/**********************************************************************
* Wait for a subprocess.
*
* if expect_signal > 0, the process must die on that signal.
* if expect_signal < 0, dying on that signal is allowed, but not required.
* if allow_core > 0, a coredump is allowed, but not required.
* otherwise, the process must die on exit(expect_status)
*/
#ifndef WCOREDUMP
# define WCOREDUMP(s) (-1)
#endif
void
vtc_wait4(struct vtclog *vl, long pid, int expect_status, int expect_signal)
vtc_wait4(struct vtclog *vl, long pid,
int expect_status, int expect_signal, int allow_core)
{
int status, r;
struct rusage ru;
......@@ -148,20 +162,21 @@ vtc_wait4(struct vtclog *vl, long pid, int expect_status, int expect_signal)
ru.ru_stime.tv_sec + 1e-6 * ru.ru_stime.tv_usec
);
if (WIFEXITED(status) && (WEXITSTATUS(status) == expect_status))
if (WIFEXITED(status) && expect_signal <= 0 &&
(WEXITSTATUS(status) == expect_status))
return;
if (WIFSIGNALED(status) && (WTERMSIG(status) == expect_signal))
if (expect_signal < 0)
expect_signal = -expect_signal;
if (WIFSIGNALED(status) && WCOREDUMP(status) <= allow_core &&
WTERMSIG(status) == expect_signal)
return;
#ifdef WCOREDUMP
vtc_fatal(vl, "Bad exit code: 0x%04x exit 0x%x signal %d core %d",
vtc_log(vl, 1, "Expected exit: 0x%x signal: %d core: %d",
expect_status, expect_signal, allow_core);
vtc_fatal(vl, "Bad exit status: 0x%04x exit 0x%x signal %d core %d",
status,
WEXITSTATUS(status),
WIFSIGNALED(status) ? WTERMSIG(status) : 0,
WCOREDUMP(status));
#else
vtc_fatal(vl, "Bad exit code: 0x%04x exit 0x%x signal %d",
status,
WEXITSTATUS(status),
WIFSIGNALED(status) ? WTERMSIG(status) : 0);
#endif
}
......@@ -647,7 +647,7 @@ varnish_cleanup(struct varnish *v)
/* Pick up the VSL thread */
AZ(pthread_join(v->tp_vsl, &p));
vtc_wait4(v->vl, v->pid, v->expect_exit, 0);
vtc_wait4(v->vl, v->pid, v->expect_exit, 0, 0);
v->pid = 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment