xref: /illumos-gate/usr/src/test/os-tests/tests/uccid/excl-close.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 a child grabs an exclusive lock and calls exit, we can grab it
18  * again.
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 <errno.h>
29 #include <sys/wait.h>
30 
31 #include <sys/usb/clients/ccid/uccid.h>
32 
33 int
34 main(int argc, char *argv[])
35 {
36 	int fd, estat;
37 	pid_t pid;
38 	uccid_cmd_txn_begin_t begin;
39 
40 	if (argc != 2) {
41 		errx(EXIT_FAILURE, "missing required ccid path");
42 	}
43 
44 	bzero(&begin, sizeof (begin));
45 	begin.uct_version = UCCID_CURRENT_VERSION;
46 
47 	pid = fork();
48 	if (pid == 0) {
49 		fd = open(argv[1], O_RDWR);
50 		if (fd < 0) {
51 			err(EXIT_FAILURE, "failed to open %s", argv[1]);
52 		}
53 
54 		if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
55 			err(EXIT_FAILURE, "failed to issue begin ioctl");
56 		}
57 
58 		_exit(0);
59 	}
60 
61 	estat = -1;
62 	if (waitpid(pid, &estat, 0) == -1) {
63 		err(EXIT_FAILURE, "failed to wait for pid %d", pid);
64 	}
65 
66 	if (estat != 0) {
67 		errx(EXIT_FAILURE, "child exited with non-zero value (%d)",
68 		    estat);
69 	}
70 
71 	fd = open(argv[1], O_RDWR);
72 	if (fd < 0) {
73 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
74 	}
75 
76 	if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
77 		err(EXIT_FAILURE, "failed to issue begin ioctl");
78 	}
79 
80 	return (0);
81 }
82