xref: /illumos-gate/usr/src/test/os-tests/tests/uccid/excl-loop.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 we can grab a basic exclusive lock and then if we try to get
18  * another lock it fails. Regardless of whether we do so through open(2) or
19  * ioctl(2).
20  */
21 
22 #include <err.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <strings.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/debug.h>
31 
32 #include <sys/usb/clients/ccid/uccid.h>
33 
34 int
35 main(int argc, char *argv[])
36 {
37 	int fd, ret;
38 	uccid_cmd_txn_begin_t begin;
39 	uccid_cmd_txn_end_t end;
40 
41 	if (argc != 2) {
42 		errx(EXIT_FAILURE, "missing required ccid path");
43 	}
44 
45 	if ((fd = open(argv[1], O_RDWR)) < 0) {
46 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
47 	}
48 
49 	bzero(&begin, sizeof (begin));
50 	bzero(&end, sizeof (end));
51 
52 	begin.uct_version = UCCID_CURRENT_VERSION;
53 	end.uct_version = UCCID_CURRENT_VERSION;
54 	end.uct_flags = UCCID_TXN_END_RELEASE;
55 
56 	if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
57 		err(EXIT_FAILURE, "failed to issue begin ioctl");
58 	}
59 
60 	ret = ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin);
61 	VERIFY3S(ret, ==, -1);
62 	VERIFY3S(errno, ==, EEXIST);
63 
64 	if (ioctl(fd, UCCID_CMD_TXN_END, &end) != 0) {
65 		err(EXIT_FAILURE, "failed to issue end ioctl");
66 	}
67 
68 	VERIFY0(close(fd));
69 
70 	if ((fd = open(argv[1], O_RDWR)) < 0) {
71 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
72 	}
73 
74 	ret = ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin);
75 	VERIFY0(ret);
76 
77 	ret = ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin);
78 	VERIFY3S(ret, ==, -1);
79 	VERIFY3S(errno, ==, EEXIST);
80 
81 	VERIFY0(close(fd));
82 
83 	return (0);
84 }
85