xref: /freebsd/share/examples/kld/cdev/test/testcdev.c (revision 4f52dfbb)
1 /* 08 Nov 1998*/
2 /*-
3  * testmisc.c
4  *
5  * Test program to call the sample loaded kld device driver.
6  *
7  * 05 Jun 93	Rajesh Vaidheeswarran		Original
8  *
9  *
10  * SPDX-License-Identifier: BSD-4-Clause
11  *
12  * Copyright (c) 1993 Rajesh Vaidheeswarran.
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *      This product includes software developed by Rajesh Vaidheeswarran.
26  * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
27  *    products derived from this software without specific prior written
28  *    permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
31  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  * Copyright (c) 1993 Terrence R. Lambert.
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *      This product includes software developed by Terrence R. Lambert.
56  * 4. The name Terrence R. Lambert may not be used to endorse or promote
57  *    products derived from this software without specific prior written
58  *    permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
61  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *
73  * $FreeBSD$
74  */
75 #include <sys/types.h>
76 #include <sys/ioccom.h>
77 
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <fcntl.h>
81 #include <paths.h>
82 #include <string.h>
83 #include <unistd.h>
84 
85 #define CDEV_IOCTL1     _IOR('C', 1, u_int)
86 #define CDEV_DEVICE	"cdev"
87 
88 static char writestr[] = "Hello kernel!";
89 static char buf[512+1];
90 
91 int
92 main(int argc __unused, char *argv[] __unused)
93 {
94     int kernel_fd;
95     int one;
96     int len;
97 
98     if ((kernel_fd = open("/dev/" CDEV_DEVICE, O_RDWR)) == -1) {
99 	perror("/dev/" CDEV_DEVICE);
100 	exit(1);
101     }
102 
103     /* Send ioctl */
104     if (ioctl(kernel_fd, CDEV_IOCTL1, &one) == -1) {
105 	perror("CDEV_IOCTL1");
106     } else {
107 	printf( "Sent ioctl CDEV_IOCTL1 to device %s%s\n", _PATH_DEV, CDEV_DEVICE);
108     }
109 
110     len = strlen(writestr) + 1;
111 
112     /* Write operation */
113     if (write(kernel_fd, writestr, len) == -1) {
114 	perror("write()");
115     } else {
116 	printf("Written \"%s\" string to device /dev/" CDEV_DEVICE "\n", writestr);
117     }
118 
119     /* Read operation */
120     if (read(kernel_fd, buf, len) == -1) {
121 	perror("read()");
122     } else {
123 	printf("Read \"%s\" string from device /dev/" CDEV_DEVICE "\n", buf);
124     }
125 
126     exit(0);
127 }
128