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 /*
174c87aefeSPatrick Mooney  *        Test:	read.requeue
184c87aefeSPatrick Mooney  *   Assertion: A sequence of writes turns into a sequence of events.
194c87aefeSPatrick Mooney  *
204c87aefeSPatrick Mooney  *    Strategy: 1. Create a pipe
214c87aefeSPatrick Mooney  *		2. Call mevent_add() to be notified of writes to the pipe.  The
224c87aefeSPatrick Mooney  *		   callback will signal a cv.
234c87aefeSPatrick Mooney  *		3. In a loop, write to the pipe then wait on the cv.
244c87aefeSPatrick Mooney  */
254c87aefeSPatrick Mooney 
264c87aefeSPatrick Mooney #include <errno.h>
274c87aefeSPatrick Mooney #include <fcntl.h>
284c87aefeSPatrick Mooney #include <pthread.h>
294c87aefeSPatrick Mooney #include <signal.h>
304c87aefeSPatrick Mooney #include <stdio.h>
314c87aefeSPatrick Mooney #include <stdlib.h>
324c87aefeSPatrick Mooney #include <strings.h>
334c87aefeSPatrick Mooney #include <unistd.h>
344c87aefeSPatrick Mooney 
354c87aefeSPatrick Mooney #include <sys/types.h>
364c87aefeSPatrick Mooney #include <sys/stat.h>
374c87aefeSPatrick Mooney 
384c87aefeSPatrick Mooney #include "testlib.h"
394c87aefeSPatrick Mooney #include "mevent.h"
404c87aefeSPatrick Mooney 
414c87aefeSPatrick Mooney static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
424c87aefeSPatrick Mooney static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
434c87aefeSPatrick Mooney 
444c87aefeSPatrick Mooney static char *cookie = "Chocolate chip with fudge stripes";
454c87aefeSPatrick Mooney 
464c87aefeSPatrick Mooney static void
munch(int fd,enum ev_type ev,void * arg)474c87aefeSPatrick Mooney munch(int fd, enum ev_type ev, void *arg)
484c87aefeSPatrick Mooney {
494c87aefeSPatrick Mooney 	static int i = 0;
504c87aefeSPatrick Mooney 	char buf[8] = { 0 };
514c87aefeSPatrick Mooney 	ssize_t nbytes;
524c87aefeSPatrick Mooney 
534c87aefeSPatrick Mooney 	ASSERT_INT_EQ(("bad event"), ev, EVF_READ);
544c87aefeSPatrick Mooney 	ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
554c87aefeSPatrick Mooney 
564c87aefeSPatrick Mooney 	if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
574c87aefeSPatrick Mooney 		ASSERT_INT64_EQ(("bad read: %s", strerror(errno)), nbytes, 1);
584c87aefeSPatrick Mooney 	}
594c87aefeSPatrick Mooney 	VERBOSE(("read %ld bytes '%s'", nbytes, buf));
604c87aefeSPatrick Mooney 
614c87aefeSPatrick Mooney 	ASSERT_INT64_EQ(("wanted a byte of cookie"), nbytes, 1);
624c87aefeSPatrick Mooney 
634c87aefeSPatrick Mooney 	ASSERT_CHAR_EQ(("bad byte %d of cookie", i), buf[0], cookie[i]);
644c87aefeSPatrick Mooney 
654c87aefeSPatrick Mooney 	pthread_mutex_lock(&mtx);
664c87aefeSPatrick Mooney 	pthread_cond_signal(&cv);
674c87aefeSPatrick Mooney 	VERBOSE(("wakeup"));
684c87aefeSPatrick Mooney 	pthread_mutex_unlock(&mtx);
694c87aefeSPatrick Mooney 
704c87aefeSPatrick Mooney 	i++;
714c87aefeSPatrick Mooney }
724c87aefeSPatrick Mooney 
734c87aefeSPatrick Mooney int
main(int argc,const char * argv[])744c87aefeSPatrick Mooney main(int argc, const char *argv[])
754c87aefeSPatrick Mooney {
764c87aefeSPatrick Mooney 	int pipefds[2];
774c87aefeSPatrick Mooney 	struct mevent *evp;
784c87aefeSPatrick Mooney 
794c87aefeSPatrick Mooney 	start_test(argv[0], 5);
804c87aefeSPatrick Mooney 	start_event_thread();
814c87aefeSPatrick Mooney 
824c87aefeSPatrick Mooney 	if (pipe(pipefds) != 0) {
834c87aefeSPatrick Mooney 		FAIL_ERRNO("pipe");
844c87aefeSPatrick Mooney 	}
854c87aefeSPatrick Mooney 	if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) != 0) {
864c87aefeSPatrick Mooney 		FAIL_ERRNO("set pipe nonblocking");
874c87aefeSPatrick Mooney 	}
884c87aefeSPatrick Mooney 
894c87aefeSPatrick Mooney 	evp = mevent_add(pipefds[0], EVF_READ, munch, cookie);
904c87aefeSPatrick Mooney 	ASSERT_PTR_NEQ(("mevent_add"), evp, NULL);
914c87aefeSPatrick Mooney 
924c87aefeSPatrick Mooney 	for (int i = 0; cookie[i] != '\0'; i++) {
934c87aefeSPatrick Mooney 		ssize_t written;
944c87aefeSPatrick Mooney 
954c87aefeSPatrick Mooney 		pthread_mutex_lock(&mtx);
964c87aefeSPatrick Mooney 		written = write(pipefds[1], cookie + i, 1);
974c87aefeSPatrick Mooney 		if (written < 0) {
984c87aefeSPatrick Mooney 			FAIL_ERRNO("bad write");
994c87aefeSPatrick Mooney 		}
1004c87aefeSPatrick Mooney 		ASSERT_INT64_EQ(("write byte %d of cookie", i), written, 1);
1014c87aefeSPatrick Mooney 
1024c87aefeSPatrick Mooney 		/* Wait for it to be read */
1034c87aefeSPatrick Mooney 		pthread_cond_wait(&cv, &mtx);
1044c87aefeSPatrick Mooney 		pthread_mutex_unlock(&mtx);
1054c87aefeSPatrick Mooney 	}
1064c87aefeSPatrick Mooney 
1074c87aefeSPatrick Mooney 	PASS();
1084c87aefeSPatrick Mooney }