Path: blob/main/share/examples/kld/cdev/test/testcdev.c
39553 views
/* 08 Nov 1998*/1/*-2* testmisc.c3*4* Test program to call the sample loaded kld device driver.5*6* 05 Jun 93 Rajesh Vaidheeswarran Original7*8*9* SPDX-License-Identifier: BSD-4-Clause10*11* Copyright (c) 1993 Rajesh Vaidheeswarran.12* All rights reserved.13*14* Redistribution and use in source and binary forms, with or without15* modification, are permitted provided that the following conditions16* are met:17* 1. Redistributions of source code must retain the above copyright18* notice, this list of conditions and the following disclaimer.19* 2. Redistributions in binary form must reproduce the above copyright20* notice, this list of conditions and the following disclaimer in the21* documentation and/or other materials provided with the distribution.22* 3. All advertising materials mentioning features or use of this software23* must display the following acknowledgement:24* This product includes software developed by Rajesh Vaidheeswarran.25* 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote26* products derived from this software without specific prior written27* permission.28*29* THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY30* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE31* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE32* ARE DISCLAIMED. IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE33* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL34* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS35* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)36* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT37* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY38* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39* SUCH DAMAGE.40*41* Copyright (c) 1993 Terrence R. Lambert.42* All rights reserved.43*44* Redistribution and use in source and binary forms, with or without45* modification, are permitted provided that the following conditions46* are met:47* 1. Redistributions of source code must retain the above copyright48* notice, this list of conditions and the following disclaimer.49* 2. Redistributions in binary form must reproduce the above copyright50* notice, this list of conditions and the following disclaimer in the51* documentation and/or other materials provided with the distribution.52* 3. All advertising materials mentioning features or use of this software53* must display the following acknowledgement:54* This product includes software developed by Terrence R. Lambert.55* 4. The name Terrence R. Lambert may not be used to endorse or promote56* products derived from this software without specific prior written57* permission.58*59* THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY60* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE61* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE62* ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE63* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL64* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS65* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)66* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT67* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY68* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF69* SUCH DAMAGE.70*71*/72#include <sys/types.h>73#include <sys/ioccom.h>7475#include <stdio.h>76#include <stdlib.h>77#include <fcntl.h>78#include <paths.h>79#include <string.h>80#include <unistd.h>8182#define CDEV_IOCTL1 _IOR('C', 1, u_int)83#define CDEV_DEVICE "cdev"8485static char writestr[] = "Hello kernel!";86static char buf[512+1];8788int89main(int argc __unused, char *argv[] __unused)90{91int kernel_fd;92int one;93int len;9495if ((kernel_fd = open("/dev/" CDEV_DEVICE, O_RDWR)) == -1) {96perror("/dev/" CDEV_DEVICE);97exit(1);98}99100/* Send ioctl */101if (ioctl(kernel_fd, CDEV_IOCTL1, &one) == -1) {102perror("CDEV_IOCTL1");103} else {104printf( "Sent ioctl CDEV_IOCTL1 to device %s%s\n", _PATH_DEV, CDEV_DEVICE);105}106107len = strlen(writestr) + 1;108109/* Write operation */110if (write(kernel_fd, writestr, len) == -1) {111perror("write()");112} else {113printf("Written \"%s\" string to device /dev/" CDEV_DEVICE "\n", writestr);114}115116/* Read operation */117if (read(kernel_fd, buf, len) == -1) {118perror("read()");119} else {120printf("Read \"%s\" string from device /dev/" CDEV_DEVICE "\n", buf);121}122123exit(0);124}125126127