1 /* @(#)checkmount.c	1.21 21/07/23 Copyright 1991-2021 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)checkmount.c	1.21 21/07/23 Copyright 1991-2021 J. Schilling";
6 #endif
7 /*
8  *	Check if disk or part of disk is mounted
9  *
10  *	Copyright (c) 1991-2021 J. Schilling
11  *
12  *	XXX #ifdef HAVE_DKIO ist vorerst nur ein Hack
13  */
14 /*
15  * The contents of this file are subject to the terms of the
16  * Common Development and Distribution License, Version 1.0 only
17  * (the "License").  You may not use this file except in compliance
18  * with the License.
19  *
20  * See the file CDDL.Schily.txt in this distribution for details.
21  * A copy of the CDDL is also available via the Internet at
22  * http://www.opensource.org/licenses/cddl1.txt
23  *
24  * When distributing Covered Code, include this CDDL HEADER in each
25  * file and include the License file CDDL.Schily.txt from this distribution.
26  */
27 
28 #include <schily/stdio.h>
29 #include <schily/unistd.h>
30 #include <schily/fcntl.h>
31 #include <schily/stat.h>
32 #include "dsklabel.h"
33 #include <schily/device.h>
34 #include <schily/standard.h>
35 #include <schily/schily.h>
36 #ifdef	HAVE_SYS_MNTTAB_H
37 #include <sys/mnttab.h>		/* Before sys/mntent.h for SCO UnixWare */
38 #endif
39 #ifdef	HAVE_SYS_MNTENT_H
40 #include <sys/mntent.h>
41 #endif
42 
43 #ifdef	HAVE_MNTENT_H
44 #include <mntent.h>
45 #ifndef	MNTTAB
46 #	define	MNTTAB	MOUNTED
47 #endif
48 #define	mnt_special	mnt_fsname
49 #endif
50 
51 #include "fmt.h"
52 #include "map.h"
53 
54 extern	int	autoformat;
55 
56 extern	struct	dk_label *d_label;
57 
58 EXPORT	BOOL	checkmount __PR((int, int, int, long, long));
59 
60 EXPORT BOOL
checkmount(scsibus,target,lun,start,end)61 checkmount(scsibus, target, lun, start, end)
62 	int	scsibus;
63 	int	target;
64 	int	lun;
65 	long	start;
66 	long	end;
67 {
68 #ifndef	HAVE_DKIO
69 	return (FALSE);
70 #else
71 	FILE	*mf;
72 	int	f;
73 	struct stat sb;
74 	struct dk_conf	conf;
75 	scgdrv	*scgmap;
76 #ifdef	SVR4
77 	struct	mnttab	_mnt;
78 	struct	mnttab	*mnt = &_mnt;
79 #else
80 	struct	mntent	*mnt;
81 #endif
82 	char	*dname;
83 	char	cdisk[128];
84 	char	rdisk[128];
85 	int	part;
86 	long	pstart;
87 	long	pend;
88 	BOOL	found = FALSE;
89 
90 	dname = diskname(maptodisk(scsibus, target, lun));
91 
92 	/*
93 	 * Open the mount table.
94 	 */
95 	mf = fopen(MNTTAB, "r");
96 	if (mf == NULL) {
97 		errmsgno(-1, "WARNING: Cannot open mount table.\n");
98 		return (found);
99 	}
100 #ifdef	SVR4
101 	while (getmntent(mf, mnt) != -1) {
102 #else
103 	while ((mnt = getmntent(mf)) != NULL) {
104 #endif
105 
106 /*		printf("testing: %s\n", mnt->mnt_special);*/
107 
108 		if (sscanf(mnt->mnt_special, "/dev/%s", cdisk) != 1)
109 			continue;
110 
111 		strcatl(rdisk, "/dev/r", cdisk, (char *)NULL);
112 
113 		if ((f = open(rdisk, O_RDONLY|O_NDELAY)) < 0)
114 			continue;
115 		if (fstat(f, &sb) < 0) {
116 			close(f);
117 			continue;
118 		}
119 		if ((sb.st_mode & S_IFMT) != S_IFCHR) {
120 			close(f);
121 			continue;
122 		}
123 		if (ioctl(f, DKIOCGCONF, &conf) < 0) {
124 			close(f);
125 			continue;
126 		}
127 		close(f);
128 
129 		scgmap = scg_getdrv(scsibus);
130 		if (scgmap->scg_cunit != conf.dkc_cnum ||
131 		    scgmap->scg_caddr != conf.dkc_addr ||
132 				!streql(scgmap->scg_cname, conf.dkc_cname))
133 			continue;
134 
135 		if (conf.dkc_slave != target*8 + lun)
136 			continue;
137 
138 		printf("disk: %s %s %s\n", dname, cdisk, mnt->mnt_special);
139 
140 		if (start < 0) {
141 			found = TRUE;
142 			break;
143 		}
144 		part = PART(sb.st_rdev);
145 		/*
146 		 * Cannot use struct dk_map * because starting with
147 		 * Solaris 9 there are several different versions of this
148 		 * structure.
149 		 */
150 		pstart = d_label->dkl_map[part].dkl_cylno *
151 				d_label->dkl_nhead * d_label->dkl_nsect;
152 
153 		pend = pstart + d_label->dkl_map[part].dkl_nblk;
154 printf("pstart: %ld pend: %ld\n", pstart, pend);
155 		if ((start >= pend) || (end < pstart))
156 			continue;
157 
158 		printf("disk part: %s %s %s\n", dname, cdisk, mnt->mnt_special);
159 
160 		found = TRUE;
161 		break;
162 	}
163 	/*
164 	 * Close the mount table.
165 	 */
166 	(void) fclose(mf);
167 
168 	if (autoformat && found)
169 		comerrno(-1, "Cannot format mounted disks.\n");
170 
171 	return (found);
172 #endif
173 }
174