xref: /illumos-gate/usr/src/test/os-tests/tests/uccid/excl-nonblock.c (revision 63f91fbc3c024870d86dc3332a4a0080fb29bc40)
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 if we've grabbed an exclusive lock, another thread fails to grab
18  * it as a non-blocking lock.
19  */
20 
21 #include <err.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <strings.h>
27 #include <unistd.h>
28 #include <sys/debug.h>
29 #include <thread.h>
30 #include <errno.h>
31 
32 #include <sys/usb/clients/ccid/uccid.h>
33 
34 void *
35 nonblock_thread(void *arg)
36 {
37 	uccid_cmd_txn_begin_t begin;
38 	int ret;
39 	int fd = (uintptr_t)arg;
40 
41 
42 	bzero(&begin, sizeof (begin));
43 
44 	begin.uct_version = UCCID_CURRENT_VERSION;
45 	begin.uct_flags = UCCID_TXN_DONT_BLOCK;
46 
47 	ret = ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin);
48 	VERIFY3S(ret, ==, -1);
49 	VERIFY3S(errno, ==, EBUSY);
50 
51 	return (NULL);
52 }
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	int fda, fdb;
58 	uccid_cmd_txn_begin_t begin;
59 	uccid_cmd_txn_end_t end;
60 	thread_t thr;
61 
62 	if (argc != 2) {
63 		errx(EXIT_FAILURE, "missing required ccid path");
64 	}
65 
66 	if ((fda = open(argv[1], O_RDWR)) < 0) {
67 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
68 	}
69 
70 	if ((fdb = open(argv[1], O_RDWR)) < 0) {
71 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
72 	}
73 
74 	bzero(&begin, sizeof (begin));
75 	bzero(&end, sizeof (end));
76 
77 	begin.uct_version = UCCID_CURRENT_VERSION;
78 	end.uct_version = UCCID_CURRENT_VERSION;
79 	end.uct_flags = UCCID_TXN_END_RELEASE;
80 
81 	if (ioctl(fda, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
82 		err(EXIT_FAILURE, "failed to issue begin ioctl");
83 	}
84 
85 	if (thr_create(NULL, 0, nonblock_thread, (void *)(uintptr_t)fdb, 0,
86 	    &thr) != 0) {
87 		err(EXIT_FAILURE, "failed to create thread");
88 	}
89 
90 	if (thr_join(thr, NULL, NULL) != 0) {
91 		err(EXIT_FAILURE, "failed to join therad");
92 	}
93 
94 	if (ioctl(fda, UCCID_CMD_TXN_END, &end) != 0) {
95 		err(EXIT_FAILURE, "failed to issue end ioctl");
96 	}
97 
98 	return (0);
99 }
100