13889d936787e03cdf934a7753a284e5949e12e88mridge/* 23889d936787e03cdf934a7753a284e5949e12e88mridge * 33889d936787e03cdf934a7753a284e5949e12e88mridge * Copyright (c) International Business Machines Corp., 2001 43889d936787e03cdf934a7753a284e5949e12e88mridge * 53889d936787e03cdf934a7753a284e5949e12e88mridge * This program is free software; you can redistribute it and/or modify 63889d936787e03cdf934a7753a284e5949e12e88mridge * it under the terms of the GNU General Public License as published by 73889d936787e03cdf934a7753a284e5949e12e88mridge * the Free Software Foundation; either version 2 of the License, or 83889d936787e03cdf934a7753a284e5949e12e88mridge * (at your option) any later version. 93889d936787e03cdf934a7753a284e5949e12e88mridge * 103889d936787e03cdf934a7753a284e5949e12e88mridge * This program is distributed in the hope that it will be useful, 113889d936787e03cdf934a7753a284e5949e12e88mridge * but WITHOUT ANY WARRANTY; without even the implied warranty of 123889d936787e03cdf934a7753a284e5949e12e88mridge * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 133889d936787e03cdf934a7753a284e5949e12e88mridge * the GNU General Public License for more details. 143889d936787e03cdf934a7753a284e5949e12e88mridge * 153889d936787e03cdf934a7753a284e5949e12e88mridge * You should have received a copy of the GNU General Public License 163889d936787e03cdf934a7753a284e5949e12e88mridge * along with this program; if not, write to the Free Software 174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 183889d936787e03cdf934a7753a284e5949e12e88mridge */ 193889d936787e03cdf934a7753a284e5949e12e88mridge 204bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak#include <stdio.h> 21cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <stdlib.h> 22cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <unistd.h> 23cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <string.h> 24cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <ctype.h> 25cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <errno.h> 26cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <sys/types.h> 27cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <sys/stat.h> 28cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <fcntl.h> 29cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <sys/ioctl.h> 30cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <linux/kd.h> 31cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include <linux/errno.h> 32cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 33cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge#include "includeTest.h" 34cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 35cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridgeint open_block_device(void); 36cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 37354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint block_dev_handle = 0; /* handle to INC test block device */ 38cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 39cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridgeint main(int argc, char **argv) 40cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge{ 41cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 42354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao incdev_cmd_t cmd = { 0, 0 }; 43354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao int rc; 44cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 45354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = open_block_device(); 46cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 47354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (!rc) { 48cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 49354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao block_dev_handle = open(DEVICE_NAME, O_RDWR); 50cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 51354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (block_dev_handle < 0) { 52354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao printf 53354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao ("ERROR: Open of device %s failed %d errno = %d\n", 54354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao DEVICE_NAME, block_dev_handle, errno); 55354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } else { 56354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = ioctl(block_dev_handle, INCDEV_CMD, &cmd); 57cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 58354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao printf("return from ioctl %d \n", rc); 59354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 60cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 61354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } else { 62354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao printf("ERROR: Create/open block device failed\n"); 63354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 64cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 65354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao return 0; 66cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 67354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao printf("Block Include Test complete.\n"); 68cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge} 69cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 70cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridgeint open_block_device() 71cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge{ 72354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao dev_t devt; 73354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao struct stat statbuf; 74354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao int rc; 75cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 76354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (block_dev_handle == 0) { 77cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 78354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao /* check for the /dev/ subdir, and create if it does not exist. 79354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao * 80354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao * If devfs is running and mounted on /dev, these checks will all pass, 81354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao * so a new node will not be created. 82354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao */ 83cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 84354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao devt = makedev(INCLUDEMAJOR, 0); 85cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 86354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = stat(INCLUDE_DEVICE_PATH, &statbuf); 87cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge 88354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (rc) { 89354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (errno == ENOENT) { 90354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao /* dev node does not exist. */ 91354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = mkdir(INCLUDE_DEVICE_PATH, 92354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao (S_IFDIR | S_IRWXU | S_IRGRP | 93354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao S_IXGRP | S_IROTH | S_IXOTH)); 942c28215423293e443469a07ae7011135d058b671Garrett Cooper 95354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } else { 96354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao printf("ERROR: Problem with INC dev directory. Error code from stat( 97cbf8c6fa52b2b44505a2ef68e1b4d2523492962emridge ) is %d\n\n", errno); 98354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 99354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } else { 100354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (!(statbuf.st_mode & S_IFDIR)) { 101354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = unlink(INCLUDE_DEVICE_PATH); 102354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (!rc) { 103354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = mkdir(INCLUDE_DEVICE_PATH, 104354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao (S_IFDIR | S_IRWXU | S_IRGRP 105354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao | S_IXGRP | S_IROTH | 106354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao S_IXOTH)); 107354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 108354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 109354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 110354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 111354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao /* 112354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao * Check for the block_device node, and create if it does not 113354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao * exist. 114354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao */ 115354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 116354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = stat(DEVICE_NAME, &statbuf); 117354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (rc) { 118354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (errno == ENOENT) { 119354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao /* dev node does not exist */ 120354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = mknod(DEVICE_NAME, 121354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao (S_IFBLK | S_IRUSR | S_IWUSR | 122354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao S_IRGRP | S_IWGRP), devt); 123354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } else { 124354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao printf 125354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao ("ERROR:Problem with block device node directory. Error code form stat() is %d\n\n", 126354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao errno); 127354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 128354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 129354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } else { 130354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao /* 131354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao * Device exists. Check to make sure it is for a 132354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao * block device and that it has the right major and minor. 133354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao */ 134354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 135354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if ((!(statbuf.st_mode & S_IFBLK)) || 136354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao (statbuf.st_rdev != devt)) { 137354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao /* Recreate the dev node. */ 138354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = unlink(DEVICE_NAME); 139354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if (!rc) { 140354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao rc = mknod(DEVICE_NAME, 141354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao (S_IFBLK | S_IRUSR | S_IWUSR 142354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao | S_IRGRP | S_IWGRP), devt); 143354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 144354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 145354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 146354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 147354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 148354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao return rc; 149ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman} 150