1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci#include <stdio.h> 21f08c3bdfSopenharmony_ci#include <stdlib.h> 22f08c3bdfSopenharmony_ci#include <unistd.h> 23f08c3bdfSopenharmony_ci#include <string.h> 24f08c3bdfSopenharmony_ci#include <ctype.h> 25f08c3bdfSopenharmony_ci#include <errno.h> 26f08c3bdfSopenharmony_ci#include <sys/types.h> 27f08c3bdfSopenharmony_ci#include <sys/stat.h> 28f08c3bdfSopenharmony_ci#include <fcntl.h> 29f08c3bdfSopenharmony_ci#include <sys/ioctl.h> 30f08c3bdfSopenharmony_ci#include <linux/kd.h> 31f08c3bdfSopenharmony_ci#include <linux/errno.h> 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci#include "includeTest.h" 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ciint open_block_device(void); 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ciint block_dev_handle = 0; /* handle to INC test block device */ 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ciint main(int argc, char **argv) 40f08c3bdfSopenharmony_ci{ 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci incdev_cmd_t cmd = { 0, 0 }; 43f08c3bdfSopenharmony_ci int rc; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci rc = open_block_device(); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci if (!rc) { 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci block_dev_handle = open(DEVICE_NAME, O_RDWR); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci if (block_dev_handle < 0) { 52f08c3bdfSopenharmony_ci printf 53f08c3bdfSopenharmony_ci ("ERROR: Open of device %s failed %d errno = %d\n", 54f08c3bdfSopenharmony_ci DEVICE_NAME, block_dev_handle, errno); 55f08c3bdfSopenharmony_ci } else { 56f08c3bdfSopenharmony_ci rc = ioctl(block_dev_handle, INCDEV_CMD, &cmd); 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci printf("return from ioctl %d \n", rc); 59f08c3bdfSopenharmony_ci } 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci } else { 62f08c3bdfSopenharmony_ci printf("ERROR: Create/open block device failed\n"); 63f08c3bdfSopenharmony_ci } 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci return 0; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci printf("Block Include Test complete.\n"); 68f08c3bdfSopenharmony_ci} 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ciint open_block_device() 71f08c3bdfSopenharmony_ci{ 72f08c3bdfSopenharmony_ci dev_t devt; 73f08c3bdfSopenharmony_ci struct stat statbuf; 74f08c3bdfSopenharmony_ci int rc; 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci if (block_dev_handle == 0) { 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci /* check for the /dev/ subdir, and create if it does not exist. 79f08c3bdfSopenharmony_ci * 80f08c3bdfSopenharmony_ci * If devfs is running and mounted on /dev, these checks will all pass, 81f08c3bdfSopenharmony_ci * so a new node will not be created. 82f08c3bdfSopenharmony_ci */ 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_ci devt = makedev(INCLUDEMAJOR, 0); 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci rc = stat(INCLUDE_DEVICE_PATH, &statbuf); 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ci if (rc) { 89f08c3bdfSopenharmony_ci if (errno == ENOENT) { 90f08c3bdfSopenharmony_ci /* dev node does not exist. */ 91f08c3bdfSopenharmony_ci rc = mkdir(INCLUDE_DEVICE_PATH, 92f08c3bdfSopenharmony_ci (S_IFDIR | S_IRWXU | S_IRGRP | 93f08c3bdfSopenharmony_ci S_IXGRP | S_IROTH | S_IXOTH)); 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci } else { 96f08c3bdfSopenharmony_ci printf("ERROR: Problem with INC dev directory. Error code from stat( 97f08c3bdfSopenharmony_ci ) is %d\n\n", errno); 98f08c3bdfSopenharmony_ci } 99f08c3bdfSopenharmony_ci } else { 100f08c3bdfSopenharmony_ci if (!(statbuf.st_mode & S_IFDIR)) { 101f08c3bdfSopenharmony_ci rc = unlink(INCLUDE_DEVICE_PATH); 102f08c3bdfSopenharmony_ci if (!rc) { 103f08c3bdfSopenharmony_ci rc = mkdir(INCLUDE_DEVICE_PATH, 104f08c3bdfSopenharmony_ci (S_IFDIR | S_IRWXU | S_IRGRP 105f08c3bdfSopenharmony_ci | S_IXGRP | S_IROTH | 106f08c3bdfSopenharmony_ci S_IXOTH)); 107f08c3bdfSopenharmony_ci } 108f08c3bdfSopenharmony_ci } 109f08c3bdfSopenharmony_ci } 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci /* 112f08c3bdfSopenharmony_ci * Check for the block_device node, and create if it does not 113f08c3bdfSopenharmony_ci * exist. 114f08c3bdfSopenharmony_ci */ 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_ci rc = stat(DEVICE_NAME, &statbuf); 117f08c3bdfSopenharmony_ci if (rc) { 118f08c3bdfSopenharmony_ci if (errno == ENOENT) { 119f08c3bdfSopenharmony_ci /* dev node does not exist */ 120f08c3bdfSopenharmony_ci rc = mknod(DEVICE_NAME, 121f08c3bdfSopenharmony_ci (S_IFBLK | S_IRUSR | S_IWUSR | 122f08c3bdfSopenharmony_ci S_IRGRP | S_IWGRP), devt); 123f08c3bdfSopenharmony_ci } else { 124f08c3bdfSopenharmony_ci printf 125f08c3bdfSopenharmony_ci ("ERROR:Problem with block device node directory. Error code form stat() is %d\n\n", 126f08c3bdfSopenharmony_ci errno); 127f08c3bdfSopenharmony_ci } 128f08c3bdfSopenharmony_ci 129f08c3bdfSopenharmony_ci } else { 130f08c3bdfSopenharmony_ci /* 131f08c3bdfSopenharmony_ci * Device exists. Check to make sure it is for a 132f08c3bdfSopenharmony_ci * block device and that it has the right major and minor. 133f08c3bdfSopenharmony_ci */ 134f08c3bdfSopenharmony_ci 135f08c3bdfSopenharmony_ci if ((!(statbuf.st_mode & S_IFBLK)) || 136f08c3bdfSopenharmony_ci (statbuf.st_rdev != devt)) { 137f08c3bdfSopenharmony_ci /* Recreate the dev node. */ 138f08c3bdfSopenharmony_ci rc = unlink(DEVICE_NAME); 139f08c3bdfSopenharmony_ci if (!rc) { 140f08c3bdfSopenharmony_ci rc = mknod(DEVICE_NAME, 141f08c3bdfSopenharmony_ci (S_IFBLK | S_IRUSR | S_IWUSR 142f08c3bdfSopenharmony_ci | S_IRGRP | S_IWGRP), devt); 143f08c3bdfSopenharmony_ci } 144f08c3bdfSopenharmony_ci } 145f08c3bdfSopenharmony_ci } 146f08c3bdfSopenharmony_ci 147f08c3bdfSopenharmony_ci } 148f08c3bdfSopenharmony_ci return rc; 149f08c3bdfSopenharmony_ci} 150