summaryrefslogtreecommitdiff
path: root/lib/lkud.c
blob: 268af4f38c158ae18434a21577a054b967721774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * lkud.c -- device lookup functions for lsof library
 */


/*
 * Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
 * 47907.  All rights reserved.
 *
 * Written by Victor A. Abell
 *
 * This software is not subject to any license of the American Telephone
 * and Telegraph Company or the Regents of the University of California.
 *
 * Permission is granted to anyone to use this software for any purpose on
 * any computer system, and to alter it and redistribute it freely, subject
 * to the following restrictions:
 *
 * 1. Neither the authors nor Purdue University are responsible for any
 *    consequences of the use of this software.
 *
 * 2. The origin of this software must not be misrepresented, either by
 *    explicit claim or by omission.  Credit to the authors and Purdue
 *    University must appear in documentation and sources.
 *
 * 3. Altered versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 *
 * 4. This notice may not be removed or altered.
 */


/*
 * lkud.c -- lookup device
 *
 * The caller may define:
 *
 *	HASBLKDEV	to activate block device lookup
 */


#include "../machine.h"

#if	defined(HASBLKDEV) || defined(USE_LIB_LKUPDEV)

# if	!defined(lint)
static char copyright[] =
"@(#) Copyright 1997 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: lkud.c,v 1.7 2008/10/21 16:12:36 abe Exp $";
# endif	/* !defined(lint) */

#include "../lsof.h"

#else	/* !defined(HASBLKDEV) && !defined(USE_LIB_LKUPDEV) */
char lkud_d1[] = "d"; char *lkud_d2 = lkud_d1;
#endif	/* defined(HASBLKDEV) || defined(USE_LIB_LKUPDEV) */



#if	defined(HASBLKDEV)
/*
 * lkupbdev() - look up a block device
 */

struct l_dev *
lkupbdev(dev, rdev, i, r)
	dev_t *dev;			/* pointer to device number */
	dev_t *rdev;			/* pointer to raw device number */
	int i;				/* inode match status */
	int r;				/* if 1, rebuild the device cache with
					 * rereaddev() when no match is found
					 * and HASDCACHE is defined and
					 * DCunsafe is one */
{
	INODETYPE inode = (INODETYPE)0;
	int low, hi, mid;
	struct l_dev *dp;
	int ty = 0;

	if (*dev != DevDev)
	    return((struct l_dev *)NULL);
	readdev(0);
	if (i) {
	    inode = Lf->inode;
	    ty = Lf->inp_ty;
	}
/*
 * Search block device table for match.
 */

# if	defined(HASDCACHE)

lkupbdev_again:

# endif	/* defined(HASDCACHE) */

	low = mid = 0;
	hi = BNdev - 1;
	while (low <= hi) {
	    mid = (low + hi) / 2;
	    dp = BSdev[mid];
	    if (*rdev < dp->rdev)
		hi = mid - 1;
	    else if (*rdev > dp->rdev)
		low = mid + 1;
	    else {
		if ((i == 0) || (ty != 1) || (inode == dp->inode)) {

# if	defined(HASDCACHE)
		    if (DCunsafe && !dp->v && !vfy_dev(dp))
			goto lkupbdev_again;
# endif	/* defined(HASDCACHE) */

		    return(dp);
		}
		if (inode < dp->inode)
		    hi = mid - 1;
		else
		    low = mid + 1;
	    }
	}

# if	defined(HASDCACHE)
	if (DCunsafe && r) {
	    (void) rereaddev();
	    goto lkupbdev_again;
	}
# endif	/* defined(HASDCACHE) */

	return((struct l_dev *)NULL);
}
#endif	/* defined(HASBLKDEV) */


#if	defined(USE_LIB_LKUPDEV)
/*
 * lkupdev() - look up a character device
 */

struct l_dev *
lkupdev(dev, rdev, i, r)
	dev_t *dev;			/* pointer to device number */
	dev_t *rdev;			/* pointer to raw device number */
	int i;				/* inode match status */
	int r;				/* if 1, rebuild the device cache with
					 * rereaddev() when no match is found
					 * and HASDCACHE is defined and
					 * DCunsafe is one */
{
	INODETYPE inode = (INODETYPE)0;
	int low, hi, mid;
	struct l_dev *dp;
	int ty = 0;

	if (*dev != DevDev)
	    return((struct l_dev *)NULL);
	readdev(0);
	if (i) {
	    inode = Lf->inode;
	    ty = Lf->inp_ty;
	}
/*
 * Search device table for match.
 */

# if	defined(HASDCACHE)

lkupdev_again:

# endif	/* defined(HASDCACHE) */

	low = mid = 0;
	hi = Ndev - 1;
	while (low <= hi) {
	    mid = (low + hi) / 2;
	    dp = Sdev[mid];
	    if (*rdev < dp->rdev)
		hi = mid - 1;
	    else if (*rdev > dp->rdev)
		low = mid + 1;
	    else {
		if ((i == 0) || (ty != 1) || (inode == dp->inode)) {

# if	defined(HASDCACHE)
		    if (DCunsafe && !dp->v && !vfy_dev(dp))
			goto lkupdev_again;
# endif	/* defined(HASDCACHE) */

		    return(dp);
		}
		if (inode < dp->inode)
		    hi = mid - 1;
		else
		    low = mid + 1;
	    }
	}

# if	defined(HASDCACHE)
	if (DCunsafe && r) {
	    (void) rereaddev();
	    goto lkupdev_again;
	}
# endif	/* defined(HASDCACHE) */

	return((struct l_dev *)NULL);
}
#endif	/* defined(USE_LIB_LKUPDEV) */