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 2019, Joyent, Inc.
14  */
15 
16 /*
17  * Verify that closing a transaction while polling generates POLLERR.
18  */
19 
20 #include <err.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <strings.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <sys/debug.h>
29 #include <poll.h>
30 #include <port.h>
31 
32 #include <sys/usb/clients/ccid/uccid.h>
33 
34 int
main(int argc,char * argv[])35 main(int argc, char *argv[])
36 {
37 	int fd, port;
38 	uccid_cmd_txn_end_t end;
39 	uccid_cmd_txn_begin_t begin;
40 	port_event_t pe;
41 	timespec_t to;
42 
43 	if (argc != 2) {
44 		errx(EXIT_FAILURE, "missing required ccid path");
45 	}
46 
47 	if ((port = port_create()) == -1) {
48 		err(EXIT_FAILURE, "failed to create event port: %d",
49 		    port);
50 	}
51 
52 	if ((fd = open(argv[1], O_RDWR | O_EXCL)) < 0) {
53 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
54 	}
55 
56 	bzero(&begin, sizeof (begin));
57 	begin.uct_version = UCCID_CURRENT_VERSION;
58 	if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
59 		err(EXIT_FAILURE, "failed to issue begin ioctl");
60 	}
61 
62 	/*
63 	 * Do not poll for pollout here, since by default, after grabbing a
64 	 * transaction, the device is writeable.
65 	 */
66 	if (port_associate(port, PORT_SOURCE_FD, fd, POLLIN, NULL) != 0) {
67 		err(EXIT_FAILURE, "failed to associate");
68 	}
69 
70 	bzero(&end, sizeof (end));
71 	end.uct_version = UCCID_CURRENT_VERSION;
72 	end.uct_flags = UCCID_TXN_END_RELEASE;
73 
74 	if (ioctl(fd, UCCID_CMD_TXN_END, &end) != 0) {
75 		err(EXIT_FAILURE, "failed to issue end ioctl");
76 	}
77 
78 	bzero(&to, sizeof (timespec_t));
79 	if (port_get(port, &pe, &to) != 0) {
80 		err(EXIT_FAILURE, "failed to port_get()");
81 	}
82 
83 	VERIFY3S(pe.portev_source, ==, PORT_SOURCE_FD);
84 	VERIFY3S(pe.portev_object, ==, fd);
85 	VERIFY3S(pe.portev_events & POLLERR, !=, 0);
86 
87 	return (0);
88 }
89