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