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