1*a61ed2ceSHans Rosenfeld /*
2*a61ed2ceSHans Rosenfeld  * This file and its contents are supplied under the terms of the
3*a61ed2ceSHans Rosenfeld  * Common Development and Distribution License ("CDDL"), version 1.0.
4*a61ed2ceSHans Rosenfeld  * You may only use this file in accordance with the terms of version
5*a61ed2ceSHans Rosenfeld  * 1.0 of the CDDL.
6*a61ed2ceSHans Rosenfeld  *
7*a61ed2ceSHans Rosenfeld  * A full copy of the text of the CDDL should have accompanied this
8*a61ed2ceSHans Rosenfeld  * source.  A copy of the CDDL is also available via the Internet at
9*a61ed2ceSHans Rosenfeld  * http://www.illumos.org/license/CDDL.
10*a61ed2ceSHans Rosenfeld  */
11*a61ed2ceSHans Rosenfeld 
12*a61ed2ceSHans Rosenfeld /*
13*a61ed2ceSHans Rosenfeld  * Copyright 2019, Joyent, Inc.
14*a61ed2ceSHans Rosenfeld  */
15*a61ed2ceSHans Rosenfeld 
16*a61ed2ceSHans Rosenfeld /*
17*a61ed2ceSHans Rosenfeld  * Verify that we can grab a basic exclusive lock and then if we try to get
18*a61ed2ceSHans Rosenfeld  * another lock it fails. Regardless of whether we do so through open(2) or
19*a61ed2ceSHans Rosenfeld  * ioctl(2).
20*a61ed2ceSHans Rosenfeld  */
21*a61ed2ceSHans Rosenfeld 
22*a61ed2ceSHans Rosenfeld #include <err.h>
23*a61ed2ceSHans Rosenfeld #include <stdlib.h>
24*a61ed2ceSHans Rosenfeld #include <sys/types.h>
25*a61ed2ceSHans Rosenfeld #include <sys/stat.h>
26*a61ed2ceSHans Rosenfeld #include <fcntl.h>
27*a61ed2ceSHans Rosenfeld #include <strings.h>
28*a61ed2ceSHans Rosenfeld #include <unistd.h>
29*a61ed2ceSHans Rosenfeld #include <errno.h>
30*a61ed2ceSHans Rosenfeld #include <sys/debug.h>
31*a61ed2ceSHans Rosenfeld 
32*a61ed2ceSHans Rosenfeld #include <sys/usb/clients/ccid/uccid.h>
33*a61ed2ceSHans Rosenfeld 
34*a61ed2ceSHans Rosenfeld int
main(int argc,char * argv[])35*a61ed2ceSHans Rosenfeld main(int argc, char *argv[])
36*a61ed2ceSHans Rosenfeld {
37*a61ed2ceSHans Rosenfeld 	int fd, ret;
38*a61ed2ceSHans Rosenfeld 	uccid_cmd_txn_begin_t begin;
39*a61ed2ceSHans Rosenfeld 	uccid_cmd_txn_end_t end;
40*a61ed2ceSHans Rosenfeld 
41*a61ed2ceSHans Rosenfeld 	if (argc != 2) {
42*a61ed2ceSHans Rosenfeld 		errx(EXIT_FAILURE, "missing required ccid path");
43*a61ed2ceSHans Rosenfeld 	}
44*a61ed2ceSHans Rosenfeld 
45*a61ed2ceSHans Rosenfeld 	if ((fd = open(argv[1], O_RDWR)) < 0) {
46*a61ed2ceSHans Rosenfeld 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
47*a61ed2ceSHans Rosenfeld 	}
48*a61ed2ceSHans Rosenfeld 
49*a61ed2ceSHans Rosenfeld 	bzero(&begin, sizeof (begin));
50*a61ed2ceSHans Rosenfeld 	bzero(&end, sizeof (end));
51*a61ed2ceSHans Rosenfeld 
52*a61ed2ceSHans Rosenfeld 	begin.uct_version = UCCID_CURRENT_VERSION;
53*a61ed2ceSHans Rosenfeld 	end.uct_version = UCCID_CURRENT_VERSION;
54*a61ed2ceSHans Rosenfeld 	end.uct_flags = UCCID_TXN_END_RELEASE;
55*a61ed2ceSHans Rosenfeld 
56*a61ed2ceSHans Rosenfeld 	if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
57*a61ed2ceSHans Rosenfeld 		err(EXIT_FAILURE, "failed to issue begin ioctl");
58*a61ed2ceSHans Rosenfeld 	}
59*a61ed2ceSHans Rosenfeld 
60*a61ed2ceSHans Rosenfeld 	ret = ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin);
61*a61ed2ceSHans Rosenfeld 	VERIFY3S(ret, ==, -1);
62*a61ed2ceSHans Rosenfeld 	VERIFY3S(errno, ==, EEXIST);
63*a61ed2ceSHans Rosenfeld 
64*a61ed2ceSHans Rosenfeld 	if (ioctl(fd, UCCID_CMD_TXN_END, &end) != 0) {
65*a61ed2ceSHans Rosenfeld 		err(EXIT_FAILURE, "failed to issue end ioctl");
66*a61ed2ceSHans Rosenfeld 	}
67*a61ed2ceSHans Rosenfeld 
68*a61ed2ceSHans Rosenfeld 	VERIFY0(close(fd));
69*a61ed2ceSHans Rosenfeld 
70*a61ed2ceSHans Rosenfeld 	if ((fd = open(argv[1], O_RDWR)) < 0) {
71*a61ed2ceSHans Rosenfeld 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
72*a61ed2ceSHans Rosenfeld 	}
73*a61ed2ceSHans Rosenfeld 
74*a61ed2ceSHans Rosenfeld 	ret = ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin);
75*a61ed2ceSHans Rosenfeld 	VERIFY0(ret);
76*a61ed2ceSHans Rosenfeld 
77*a61ed2ceSHans Rosenfeld 	ret = ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin);
78*a61ed2ceSHans Rosenfeld 	VERIFY3S(ret, ==, -1);
79*a61ed2ceSHans Rosenfeld 	VERIFY3S(errno, ==, EEXIST);
80*a61ed2ceSHans Rosenfeld 
81*a61ed2ceSHans Rosenfeld 	VERIFY0(close(fd));
82*a61ed2ceSHans Rosenfeld 
83*a61ed2ceSHans Rosenfeld 	return (0);
84*a61ed2ceSHans Rosenfeld }