#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
-#include <err.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
+#include <sys/resource.h>
#include <signal.h>
#include <assert.h>
+#include <ccan/err/err.h>
#include <ccan/time/time.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/failtest/failtest_proto.h>
/* If we're a child, this is the first call we did ourselves. */
static struct failtest_call *our_history_start = NULL;
/* For printing runtime with --trace. */
-static struct timeval start;
+static struct timeabs start;
/* Set when failtest_hook returns FAIL_PROBE */
static bool probing = false;
/* Table to track duplicates. */
static int move_fd_to_high(int fd)
{
int i;
+ struct rlimit lim;
+ int max;
- for (i = FD_SETSIZE - 1; i >= 0; i--) {
+ if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
+ max = lim.rlim_cur;
+ printf("Max is %i\n", max);
+ } else
+ max = FD_SETSIZE;
+
+ for (i = max - 1; i > fd; i--) {
if (fcntl(i, F_GETFL) == -1 && errno == EBADF) {
- if (dup2(fd, i) == -1)
- err(1, "Failed to dup fd %i to %i", fd, i);
+ if (dup2(fd, i) == -1) {
+ warn("Failed to dup fd %i to %i", fd, i);
+ continue;
+ }
close(fd);
return i;
}
{
struct failtest_call *i;
- while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
+ while ((i = tlist_top(&history, list)) != NULL)
free_call(i);
failtable_clear(&failtable);
/* But their program shouldn't leak, even on failure. */
if (!forced_cleanup && i->can_leak) {
+ char *p = failpath_string();
printf("Leak at %s:%u: --failpath=%s\n",
- i->file, i->line, failpath_string());
+ i->file, i->line, p);
+ free(p);
status = 1;
}
}
if (child == 0) {
traceindent++;
if (tracef) {
- struct timeval diff;
+ struct timerel diff;
const char *p;
char *failpath;
struct failtest_call *c;
- c = tlist_tail(&history, struct failtest_call, list);
- diff = time_sub(time_now(), start);
+ c = tlist_tail(&history, list);
+ diff = time_between(time_now(), start);
failpath = failpath_string();
p = strrchr(c->file, '/');
if (p)
p = c->file;
trace("%u->%u (%u.%02u): %s (%s:%u)\n",
getppid(), getpid(),
- (int)diff.tv_sec, (int)diff.tv_usec / 10000,
+ (int)diff.ts.tv_sec, (int)diff.ts.tv_nsec / 10000000,
failpath, p, c->line);
free(failpath);
}
/* From here on, we have to clean up! */
- our_history_start = tlist_tail(&history, struct failtest_call,
- list);
+ our_history_start = tlist_tail(&history, list);
close(control[0]);
close(output[0]);
/* Don't swallow stderr if we're tracing. */
call.closed = false;
if (call.flags & O_CREAT) {
call.mode = va_arg(ap, int);
- va_end(ap);
}
+ va_end(ap);
p = add_history(FAILTEST_OPEN, true, file, line, &call);
/* Avoid memory leak! */
if (p == &unrecorded_call)
return p->u.mmap.ret;
}
+/* Since OpenBSD can't handle adding args, we use this file and line.
+ * This will make all mmaps look the same, reducing coverage. */
+void *failtest_mmap_noloc(void *addr, size_t length, int prot, int flags,
+ int fd, off_t offset)
+{
+ return failtest_mmap(addr, length, prot, flags, fd, offset,
+ __FILE__, __LINE__);
+}
+
static void cleanup_pipe(struct pipe_call *call, bool restore)
{
trace("cleaning up pipe fd=%i%s,%i%s\n",
set_cleanup(p, cleanup_read, struct read_call);
}
}
- trace("%sread %s:%u fd %i %zu@%llu -> %i\n",
+ trace("%sread %s:%u fd %i %zu@%llu -> %zi\n",
is_pread ? "p" : "", file, line, fd, count, (long long)off,
p->u.read.ret);
errno = p->error;
else
p->u.write.ret = write(fd, buf, count);
}
- trace("%swrite %s:%i %zu@%llu on fd %i -> %i\n",
+ trace("%swrite %s:%i %zu@%llu on fd %i -> %zi\n",
p->u.write.is_pwrite ? "p" : "",
file, line, count, (long long)off, fd, p->u.write.ret);
errno = p->error;