1*4c87aefeSPatrick Mooney /*
2*4c87aefeSPatrick Mooney  * This file and its contents are supplied under the terms of the
3*4c87aefeSPatrick Mooney  * Common Development and Distribution License ("CDDL"), version 1.0.
4*4c87aefeSPatrick Mooney  * You may only use this file in accordance with the terms of version
5*4c87aefeSPatrick Mooney  * 1.0 of the CDDL.
6*4c87aefeSPatrick Mooney  *
7*4c87aefeSPatrick Mooney  * A full copy of the text of the CDDL should have accompanied this
8*4c87aefeSPatrick Mooney  * source.  A copy of the CDDL is also available via the Internet at
9*4c87aefeSPatrick Mooney  * http://www.illumos.org/license/CDDL.
10*4c87aefeSPatrick Mooney  */
11*4c87aefeSPatrick Mooney 
12*4c87aefeSPatrick Mooney /*
13*4c87aefeSPatrick Mooney  * Copyright 2018 Joyent, Inc.
14*4c87aefeSPatrick Mooney  */
15*4c87aefeSPatrick Mooney 
16*4c87aefeSPatrick Mooney #include <pthread.h>
17*4c87aefeSPatrick Mooney #include <signal.h>
18*4c87aefeSPatrick Mooney #include <strings.h>
19*4c87aefeSPatrick Mooney #include <unistd.h>
20*4c87aefeSPatrick Mooney 
21*4c87aefeSPatrick Mooney #include "testlib.h"
22*4c87aefeSPatrick Mooney #include "mevent.h"
23*4c87aefeSPatrick Mooney 
24*4c87aefeSPatrick Mooney const char *testlib_prog;
25*4c87aefeSPatrick Mooney boolean_t testlib_verbose;
26*4c87aefeSPatrick Mooney 
27*4c87aefeSPatrick Mooney static void
timed_out(int signo)28*4c87aefeSPatrick Mooney timed_out(int signo)
29*4c87aefeSPatrick Mooney {
30*4c87aefeSPatrick Mooney 	ASSERT_INT_EQ(("timeout signal"), signo, SIGALRM);
31*4c87aefeSPatrick Mooney 
32*4c87aefeSPatrick Mooney 	FAIL(("Timed out"));
33*4c87aefeSPatrick Mooney }
34*4c87aefeSPatrick Mooney 
35*4c87aefeSPatrick Mooney void
start_test(const char * argv0,uint32_t timeout)36*4c87aefeSPatrick Mooney start_test(const char *argv0, uint32_t timeout)
37*4c87aefeSPatrick Mooney {
38*4c87aefeSPatrick Mooney 	char *val;
39*4c87aefeSPatrick Mooney 
40*4c87aefeSPatrick Mooney 	testlib_prog = strrchr(argv0, '/');
41*4c87aefeSPatrick Mooney 	if (testlib_prog == NULL) {
42*4c87aefeSPatrick Mooney 		testlib_prog = argv0;
43*4c87aefeSPatrick Mooney 	} else {
44*4c87aefeSPatrick Mooney 		testlib_prog++;
45*4c87aefeSPatrick Mooney 	}
46*4c87aefeSPatrick Mooney 
47*4c87aefeSPatrick Mooney 	testlib_verbose = ((val = getenv("TEST_VERBOSE")) != NULL) &&
48*4c87aefeSPatrick Mooney 	    val[0] != '\0';
49*4c87aefeSPatrick Mooney 
50*4c87aefeSPatrick Mooney 	signal(SIGALRM, timed_out);
51*4c87aefeSPatrick Mooney 	alarm(timeout);
52*4c87aefeSPatrick Mooney }
53*4c87aefeSPatrick Mooney 
54*4c87aefeSPatrick Mooney /* ARGSUSED */
55*4c87aefeSPatrick Mooney static void *
event_thread(void * arg)56*4c87aefeSPatrick Mooney event_thread(void *arg)
57*4c87aefeSPatrick Mooney {
58*4c87aefeSPatrick Mooney 	mevent_dispatch();
59*4c87aefeSPatrick Mooney 	return (NULL);
60*4c87aefeSPatrick Mooney }
61*4c87aefeSPatrick Mooney 
62*4c87aefeSPatrick Mooney void
start_event_thread(void)63*4c87aefeSPatrick Mooney start_event_thread(void)
64*4c87aefeSPatrick Mooney {
65*4c87aefeSPatrick Mooney 	pthread_t tid;
66*4c87aefeSPatrick Mooney 
67*4c87aefeSPatrick Mooney 	if (pthread_create(&tid, NULL, event_thread, NULL) != 0) {
68*4c87aefeSPatrick Mooney 		FAIL_ERRNO("pthread_create");
69*4c87aefeSPatrick Mooney 	}
70*4c87aefeSPatrick Mooney }
71