xref: /xnu-11417.140.69/tests/skywalk/skt_closenfd.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 2016-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <uuid/uuid.h>
34 #include <unistd.h>
35 #include "skywalk_test_driver.h"
36 #include "skywalk_test_common.h"
37 
38 static int
skt_closenfd_main(int argc,char * argv[])39 skt_closenfd_main(int argc, char *argv[])
40 {
41 	int error;
42 	nexus_controller_t ncd;
43 	int nfd;
44 
45 	ncd = os_nexus_controller_create();
46 	assert(ncd);
47 
48 	nfd = os_nexus_controller_get_fd(ncd);
49 	assert(nfd != -1);
50 
51 	error = close(nfd); // expect guarded fd fail
52 	SKTC_ASSERT_ERR(!error);
53 
54 	os_nexus_controller_destroy(ncd);
55 
56 	return 1; // should not reach
57 }
58 
59 struct skywalk_test skt_closenfd = {
60 	"closenfd", "test closing guarded nexus fd",
61 	SK_FEATURE_SKYWALK,
62 	skt_closenfd_main, { NULL }, NULL, NULL,
63 	0x4000000100000000, 0xFFFFFFFF,
64 };
65 
66 
67 /****************************************************************/
68 
69 static int
skt_writenfd_main(int argc,char * argv[])70 skt_writenfd_main(int argc, char *argv[])
71 {
72 	nexus_controller_t ncd;
73 	int nfd;
74 	char buf[100] = { 0 };
75 	ssize_t ret;
76 
77 	ncd = os_nexus_controller_create();
78 	assert(ncd);
79 
80 	nfd = os_nexus_controller_get_fd(ncd);
81 	assert(nfd != -1);
82 
83 	ret = write(nfd, buf, sizeof(buf));
84 	assert(ret == -1);
85 	assert(errno == EBADF);
86 
87 	os_nexus_controller_destroy(ncd);
88 
89 	return 0;
90 }
91 
92 struct skywalk_test skt_writenfd = {
93 	"writenfd", "test writing to a guarded nexus fd",
94 	SK_FEATURE_SKYWALK,
95 	skt_writenfd_main, { NULL }, NULL, NULL, 0x9c00003, 0,
96 };
97 
98 /****************************************************************/
99 
100 static int
skt_readnfd_main(int argc,char * argv[])101 skt_readnfd_main(int argc, char *argv[])
102 {
103 	nexus_controller_t ncd;
104 	int nfd;
105 	char buf[100];
106 	ssize_t ret;
107 
108 	ncd = os_nexus_controller_create();
109 	assert(ncd);
110 
111 	nfd = os_nexus_controller_get_fd(ncd);
112 	assert(nfd != -1);
113 
114 	ret = read(nfd, buf, sizeof(buf));
115 	assert(ret == -1);
116 	assert(errno == ENXIO);
117 
118 	os_nexus_controller_destroy(ncd);
119 
120 	return 0;
121 }
122 
123 struct skywalk_test skt_readnfd = {
124 	"readnfd", "test reading from a guarded nexus fd",
125 	SK_FEATURE_SKYWALK,
126 	skt_readnfd_main, { NULL }, NULL, NULL,
127 };
128 
129 /****************************************************************/
130