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