14c87aefeSPatrick Mooney /*
24c87aefeSPatrick Mooney  * This file and its contents are supplied under the terms of the
34c87aefeSPatrick Mooney  * Common Development and Distribution License ("CDDL"), version 1.0.
44c87aefeSPatrick Mooney  * You may only use this file in accordance with the terms of version
54c87aefeSPatrick Mooney  * 1.0 of the CDDL.
64c87aefeSPatrick Mooney  *
74c87aefeSPatrick Mooney  * A full copy of the text of the CDDL should have accompanied this
84c87aefeSPatrick Mooney  * source.  A copy of the CDDL is also available via the Internet at
94c87aefeSPatrick Mooney  * http://www.illumos.org/license/CDDL.
104c87aefeSPatrick Mooney  */
114c87aefeSPatrick Mooney 
124c87aefeSPatrick Mooney /*
134c87aefeSPatrick Mooney  * Copyright 2018 Joyent, Inc.
144c87aefeSPatrick Mooney  */
154c87aefeSPatrick Mooney 
164c87aefeSPatrick Mooney #include <pthread.h>
174c87aefeSPatrick Mooney #include <signal.h>
184c87aefeSPatrick Mooney #include <strings.h>
194c87aefeSPatrick Mooney #include <unistd.h>
204c87aefeSPatrick Mooney 
214c87aefeSPatrick Mooney #include "testlib.h"
224c87aefeSPatrick Mooney #include "mevent.h"
234c87aefeSPatrick Mooney 
244c87aefeSPatrick Mooney const char *testlib_prog;
254c87aefeSPatrick Mooney boolean_t testlib_verbose;
264c87aefeSPatrick Mooney 
274c87aefeSPatrick Mooney static void
timed_out(int signo)284c87aefeSPatrick Mooney timed_out(int signo)
294c87aefeSPatrick Mooney {
304c87aefeSPatrick Mooney 	ASSERT_INT_EQ(("timeout signal"), signo, SIGALRM);
314c87aefeSPatrick Mooney 
324c87aefeSPatrick Mooney 	FAIL(("Timed out"));
334c87aefeSPatrick Mooney }
344c87aefeSPatrick Mooney 
354c87aefeSPatrick Mooney void
start_test(const char * argv0,uint32_t timeout)364c87aefeSPatrick Mooney start_test(const char *argv0, uint32_t timeout)
374c87aefeSPatrick Mooney {
384c87aefeSPatrick Mooney 	char *val;
394c87aefeSPatrick Mooney 
404c87aefeSPatrick Mooney 	testlib_prog = strrchr(argv0, '/');
414c87aefeSPatrick Mooney 	if (testlib_prog == NULL) {
424c87aefeSPatrick Mooney 		testlib_prog = argv0;
434c87aefeSPatrick Mooney 	} else {
444c87aefeSPatrick Mooney 		testlib_prog++;
454c87aefeSPatrick Mooney 	}
464c87aefeSPatrick Mooney 
474c87aefeSPatrick Mooney 	testlib_verbose = ((val = getenv("TEST_VERBOSE")) != NULL) &&
484c87aefeSPatrick Mooney 	    val[0] != '\0';
494c87aefeSPatrick Mooney 
504c87aefeSPatrick Mooney 	signal(SIGALRM, timed_out);
514c87aefeSPatrick Mooney 	alarm(timeout);
524c87aefeSPatrick Mooney }
534c87aefeSPatrick Mooney 
544c87aefeSPatrick Mooney /* ARGSUSED */
554c87aefeSPatrick Mooney static void *
event_thread(void * arg)564c87aefeSPatrick Mooney event_thread(void *arg)
574c87aefeSPatrick Mooney {
584c87aefeSPatrick Mooney 	mevent_dispatch();
594c87aefeSPatrick Mooney 	return (NULL);
604c87aefeSPatrick Mooney }
614c87aefeSPatrick Mooney 
624c87aefeSPatrick Mooney void
start_event_thread(void)634c87aefeSPatrick Mooney start_event_thread(void)
644c87aefeSPatrick Mooney {
654c87aefeSPatrick Mooney 	pthread_t tid;
664c87aefeSPatrick Mooney 
674c87aefeSPatrick Mooney 	if (pthread_create(&tid, NULL, event_thread, NULL) != 0) {
684c87aefeSPatrick Mooney 		FAIL_ERRNO("pthread_create");
694c87aefeSPatrick Mooney 	}
704c87aefeSPatrick Mooney }