Six live tests carried private copies of the same daemon harness: the
private XDG_RUNTIME_DIR, the fork/exec with captured stderr, the
readiness waits, the SIGTERM and SIGKILL paths, the IBus address file
reader, the socket connect, and the IPC probe. Three of them also
carried the same libdbus helpers.
Extract one implementation into tests/live.c and, so that the binaries
that do not link dbus-1 keep not linking it, the D-Bus half into
tests/livebus.c. Both are compiled into each binary the way ../ipc.c
already is. gtk_live_test keeps its own fake-server harness and only
takes fail, nowms and leftms; xim_live_test keeps its own Xvfb and
process-group daemon spawn, which must inherit DISPLAY, and takes the
temporary directory, timing and cleanup halves.
The copies had drifted; the harness keeps the stricter behaviour.
- nowms reports a broken clock (-1) instead of pretending it read
zero, and leftms turns that into an expired deadline, so a loop
ends in a timeout failure rather than spinning. livesetup checks
the clock once up front, as daemon_restart_test did.
- Timeouts compare with <= 0, not == 0.
- readuntil keeps the three-way result (complete, peer closed, error)
from ipc_live_test and daemon_restart_test rather than folding
peer closure into ECONNRESET.
- The daemon's stdout and stderr are both captured, and a failing
setenv is reported, for every daemon; daemon_failure_test captured
stderr only and said nothing about setenv.
- The child keeps daemon_restart_test's careful redirect that also
works when the pipe lands on fd 1 or 2.
- parseaddress requires a positive declared PID.
- killdaemon reports ECHILD after SIGKILL as a failure; one copy
accepted it. It now reaps with a blocking waitpid, which SIGKILL
guarantees will return, instead of daemon_restart_test's polled
wait with its own timeout diagnostic.
- stopdaemon tolerates ESRCH on SIGTERM, a benign race two copies
reported as an error.
- liveclean removes the socket and address files and then rmdirs
each directory, reporting leftovers, rather than deleting the
temporary root recursively.
- ibus_live_test now waits for the IPC socket and the address file
by polling, dropping its inotify variant; it asserted only the
address file before.
139 lines
2.9 KiB
C
139 lines
2.9 KiB
C
#define _GNU_SOURCE
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <poll.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
#include "live.h"
|
|
|
|
enum
|
|
{
|
|
Failtimeout = 4000,
|
|
};
|
|
|
|
static int
|
|
waitfailed(Daemon *d, char *badmap)
|
|
{
|
|
struct pollfd pfd;
|
|
int n, status;
|
|
int64_t deadline;
|
|
|
|
status = 0;
|
|
deadline = nowms() + Failtimeout;
|
|
for(;;){
|
|
n = waitpid(d->pid, &status, WNOHANG);
|
|
if(n == d->pid){
|
|
d->pid = -1;
|
|
readerrors(d);
|
|
break;
|
|
}
|
|
if(n < 0 && errno == EINTR)
|
|
continue;
|
|
if(n < 0){
|
|
if(errno == ECHILD)
|
|
d->pid = -1;
|
|
return fail("waitpid: %s", strerror(errno));
|
|
}
|
|
n = leftms(deadline);
|
|
if(n == 0)
|
|
return fail("daemon did not exit after map initialization failure");
|
|
pfd.fd = d->errfd;
|
|
pfd.events = POLLIN|POLLHUP;
|
|
pfd.revents = 0;
|
|
n = poll(&pfd, 1, n);
|
|
if(n < 0 && errno == EINTR)
|
|
continue;
|
|
if(n < 0)
|
|
return fail("poll daemon: %s", strerror(errno));
|
|
if(n > 0)
|
|
readerrors(d);
|
|
}
|
|
if(!WIFEXITED(status) || WEXITSTATUS(status) == 0)
|
|
return fail("daemon map failure had wait status %#x", status);
|
|
if(strstr(d->err, "can't open") == NULL || strstr(d->err, badmap) == NULL)
|
|
return fail("daemon did not report the missing map directory");
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
emptydir(char *path)
|
|
{
|
|
DIR *dir;
|
|
struct dirent *de;
|
|
int empty;
|
|
|
|
dir = opendir(path);
|
|
if(dir == NULL)
|
|
return fail("open %s: %s", path, strerror(errno));
|
|
empty = 1;
|
|
errno = 0;
|
|
while((de = readdir(dir)) != NULL)
|
|
if(strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0){
|
|
fail("unexpected endpoint %s/%s", path, de->d_name);
|
|
empty = 0;
|
|
}
|
|
if(errno != 0){
|
|
fail("read %s: %s", path, strerror(errno));
|
|
empty = 0;
|
|
}
|
|
if(closedir(dir) < 0){
|
|
fail("close %s: %s", path, strerror(errno));
|
|
empty = 0;
|
|
}
|
|
return empty;
|
|
}
|
|
|
|
static int
|
|
checkendpoints(Live *l)
|
|
{
|
|
struct stat st;
|
|
|
|
if(lstat(l->socket, &st) == 0)
|
|
return fail("IPC endpoint exists after failed startup: %s", l->socket);
|
|
if(errno != ENOENT)
|
|
return fail("lstat IPC endpoint: %s", strerror(errno));
|
|
return emptydir(l->runtime) && emptydir(l->bus);
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
Daemon daemon;
|
|
Live live;
|
|
char badmap[320];
|
|
int ok;
|
|
|
|
testname = "daemon_failure_test";
|
|
if(argc != 2){
|
|
fprintf(stderr, "usage: daemon_failure_test strans\n");
|
|
return 2;
|
|
}
|
|
daemoninit(&daemon, "daemon");
|
|
ok = livesetup(&live, "failure");
|
|
if(ok && snprintf(badmap, sizeof badmap, "%s/missing-map", live.root)
|
|
>= (int)sizeof badmap)
|
|
ok = fail("temporary path is too long");
|
|
if(ok)
|
|
ok = startdaemon(&live, &daemon, argv[1], badmap);
|
|
if(ok)
|
|
ok = waitfailed(&daemon, badmap) && checkendpoints(&live);
|
|
if(!killdaemon(&daemon, NULL))
|
|
ok = 0;
|
|
if(!closeerrors(&daemon))
|
|
ok = 0;
|
|
if(!liveclean(&live))
|
|
ok = 0;
|
|
if(!ok){
|
|
showerrors(&daemon);
|
|
return 1;
|
|
}
|
|
printf("failed daemon startup leaves no endpoints: ok\n");
|
|
return 0;
|
|
}
|