1/* 2/* 3 * 4 * Copyright (c) International Business Machines Corp., 2001 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 14 * the GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * 20 21 * This is the main of your user space test program, 22 * which will open the correct kernel module, find the 23 * file descriptor value and use that value to make 24 * ioctl calls to the system 25 * 26 * Use the ki_generic and other ki_testname functions 27 * to abstract the calls from the main 28 * 29 * author: Sean Ruyle 30 * date: 06/11/2003 31 * 32 * update: Marty Ridgeway 33 * date: 09/02/2003 34 * 35 */ 36 37#include <stdio.h> 38#include <stdlib.h> 39#include <errno.h> 40#include <sys/stat.h> 41#include <sys/ioctl.h> 42#include <fcntl.h> 43#include <linux/kernel.h> 44#include <linux/errno.h> 45 46#include "user_tmod.h" 47#include "../kernel_space/tmod.h" 48 49static int tmod_fd = -1; /* file descriptor */ 50 51int tmodopen() 52{ 53 54 dev_t devt; 55 struct stat st; 56 int rc = 0; 57 58 devt = makedev(TMOD_MAJOR, 0); 59 60 if (rc) { 61 if (errno == ENOENT) { 62 /* dev node does not exist. */ 63 rc = mkdir(DEVICE_NAME, (S_IFDIR | S_IRWXU | 64 S_IRGRP | S_IXGRP | 65 S_IROTH | S_IXOTH)); 66 } else { 67 printf 68 ("ERROR: Problem with Base dev directory. Error code from stat() is %d\n\n", 69 errno); 70 } 71 72 } else { 73 if (!(st.st_mode & S_IFDIR)) { 74 rc = unlink(DEVICE_NAME); 75 if (!rc) { 76 rc = mkdir(DEVICE_NAME, (S_IFDIR | S_IRWXU | 77 S_IRGRP | S_IXGRP | 78 S_IROTH | S_IXOTH)); 79 } 80 } 81 } 82 83 /* 84 * Check for the /dev/tmod node, and create if it does not 85 * exist. 86 */ 87 rc = stat(DEVICE_NAME, &st); 88 if (rc) { 89 if (errno == ENOENT) { 90 /* dev node does not exist */ 91 rc = mknod(DEVICE_NAME, 92 (S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | 93 S_IWGRP), devt); 94 } else { 95 printf 96 ("ERROR:Problem with tbase device node directory. Error code form stat() is %d\n\n", 97 errno); 98 } 99 100 } else { 101 /* 102 * /dev/tbase CHR device exists. Check to make sure it is for a 103 * block device and that it has the right major and minor. 104 */ 105 if ((!(st.st_mode & S_IFCHR)) || (st.st_rdev != devt)) { 106 107 /* Recreate the dev node. */ 108 rc = unlink(DEVICE_NAME); 109 if (!rc) { 110 rc = mknod(DEVICE_NAME, 111 (S_IFCHR | S_IRUSR | S_IWUSR | 112 S_IRGRP | S_IWGRP), devt); 113 } 114 } 115 } 116 117 tmod_fd = open(DEVICE_NAME, O_RDWR); 118 119 if (tmod_fd < 0) { 120 printf("ERROR: Open of device %s failed %d errno = %d\n", 121 DEVICE_NAME, tmod_fd, errno); 122 return errno; 123 } else { 124 printf("Device opened successfully \n"); 125 return 0; 126 } 127 128} 129 130int tmodclose() 131{ 132 133 if (tmod_fd != -1) { 134 close(tmod_fd); 135 tmod_fd = -1; 136 } 137 138 return 0; 139} 140 141int main() 142{ 143 int rc; 144 145 /* open the module */ 146 rc = tmodopen(); 147 if (rc) { 148 printf("Test MOD Driver may not be loaded\n"); 149 exit(1); 150 } 151 152 /* make test calls */ 153 if (ki_generic(tmod_fd, LTP_OPTION1)) 154 printf("Failed on option 1 test\n"); 155 else 156 printf("Success on option 1 test\n"); 157 158 /* close the module */ 159 rc = tmodclose(); 160 if (rc) { 161 printf("Test MOD Driver may not be closed\n"); 162 exit(1); 163 } 164 165 return 0; 166} 167