1 /*
2 *
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31
32 /*
33
34 opentest.c
35
36 tests open, close call
37
38 */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <sys/wait.h>
46 #include <signal.h>
47 #include <string.h>
48
49 #include "fusd.h"
50
51 void done(int signo)
52 {
53 int status = 0;
54 pid_t pid = waitpid(-1, &status, WNOHANG);
55
56 if (pid <= 0)
57 fprintf(stderr, "child error\n");
58 else if (!WIFEXITED(status))
59 fprintf(stderr, "child died of signal %d\n", WTERMSIG(status));
60 else if (WEXITSTATUS(status))
61 fprintf(stderr, "TEST FAILED - exit status %d\n", WEXITSTATUS(status));
62 else {
63 fprintf(stderr, "TEST PASSED\n");
64 exit(0);
65 }
66
67 exit(1);
68 }
69
70
71 int testnums[5] = {12,34,56,78,0};
72 int counter = 0;
73
74 int _open(struct fusd_file_info *file)
75 {
76 if (0 && (counter == 0)) {
77 fusd_return(file, -testnums[0]);
78 counter++;
79 return -FUSD_NOREPLY;
80 }
81
82 else
83 return -testnums[counter++];
84 }
85
86
87 int _close(struct fusd_file_info *file)
88 {
89 if (counter == 5) {
90 fusd_return(file, -testnums[0]);
91 counter++;
92 return -FUSD_NOREPLY;
93 }
94
95 else
96 return -testnums[counter++ - 5];
97 }
98
99
100
101 int main(int argc, char *argv[])
102 {
103 int f;
104
105 if (argc != 2) {
106 fprintf(stderr, "usage: %s <device-to-create>\n", argv[0]);
107 exit(0);
108 }
109
110 if (strncmp(argv[1], "/dev/", 5)) {
111 fprintf(stderr, "devname must start with /dev/\n");
112 exit(0);
113 }
114
115 /* fork into client and server */
116 f = fork();
117 if (f<0) {
118 perror("Fork failed");
119 exit(1);
120 }
121
122 /* server */
123 if (f) {
124 // init function table
125 fusd_file_operations_t
126 fops = {
127 open: _open,
128 close: _close
129 };
130
131 int fd;
132 if ((fd = fusd_register(argv[1], 0666, 0, &fops)) < 0) {
133 perror("Register failed");
134 }
135
136 else {
137 signal(SIGCHLD, done);
138 fusd_run();
139 }
140 }
141
142 /* client */
143 else {
144 int i;
145 int fd;
146 int error = 0;
147 int j;
148
149 sleep(1);
150
151 /* opens */
152 for (i=0; i<5; i++) {
153 fd = open(argv[1], O_RDWR);
154
155 if (i != 4) {
156 fprintf(stderr, "Open call returned errno %d (%m), should be %d.\n",
157 errno, testnums[i]);
158 if (errno != testnums[i])
159 error = 1;
160 }
161 else {
162 fprintf(stderr, "Open call return fd = %d, should be nonnegative.\n", fd);
163 if (fd < 0) error = 1;
164 }
165
166 }
167
168 /* close */
169 j = close(fd);
170 fprintf(stderr, "Close call returned %d, should be 0.\n", j);
171 if (j != 0) error = 1;
172
173 close(fd);
174 fprintf(stderr, "Close call returned %d, should be 9.\n", errno);
175 if (errno != 9) error = 1;
176
177 if (error)
178 exit(1);
179 else
180 exit(0);
181 }
182
183 exit(0);
184 }
185
186
187
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.