summaryrefslogtreecommitdiff
path: root/dialects/aix/dmnt.c
blob: ef1d61c79a864a9046958fdc060073c941bad9de (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * dmnt.c - AIX mount support functions for lsof
 */


/*
 * Copyright 1994 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.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: dmnt.c,v 1.13 2005/08/08 19:46:38 abe Exp $";
#endif


#include "lsof.h"


/*
 * Local static definitions
 */

static struct mounts *Lmi = (struct mounts *)NULL;	/* local mount info */
static int Lmist = 0;					/* Lmi status */


/*
 * readmnt() - read mount table
 */

struct mounts *
readmnt()
{
	char *dir, *fs, *h, *ln, *ty;
	char *dn = (char *)NULL;
	struct mounts *mtp;
	int nm;
	struct stat sb;
	MALLOC_S sz;
	struct vmount *v;
	struct vmount *vt = (struct vmount *)NULL;

	if (Lmi || Lmist)
	    return(Lmi);
/*
 * Read the table of vmount structures.
 */
	for (sz = sizeof(struct vmount);;) {
	    if (!(vt = (struct vmount *)malloc(sz))) {
		(void) fprintf(stderr, "%s: no space for vmount table\n", Pn);
		return(0);
	    }
	    nm = mntctl(MCTL_QUERY, sz, (unsigned char *)vt);
	    if (nm > 0) {
		if (vt->vmt_revision != VMT_REVISION) {
		    (void) fprintf(stderr,
			"%s: stale file system, rev %d != %d\n",
			Pn, vt->vmt_revision, VMT_REVISION);
		    return(0);
		}
		break;
	    }
	    if (nm == 0) {
		sz = (unsigned)vt->vmt_revision;
		(void) free((FREE_P *)vt);
	    } else {
		(void) fprintf(stderr, "%s: mntctl error: %s\n",
		    Pn, strerror(errno));
		return(0);
	    }
	}
/*
 * Scan the vmount structures and build Lmi.
 */
	for (v = vt; nm--; v = (struct vmount *)((char *)v + v->vmt_length)) {
	    dir = (char *)vmt2dataptr(v, VMT_STUB);
	    fs = (char *)vmt2dataptr(v, VMT_OBJECT);
	    h = (char *)vmt2dataptr(v, VMT_HOST);
            if (statsafely(dir, &sb)) {
		if (!Fwarn) {

		/*
		 * Issue stat() failure warning.
		 */
		    switch(v->vmt_gfstype) {

#if	defined(HAS_AFS)
		    case MNT_AFS:
			ty = "afs";
			break;
#endif	/* defined(HAS_AFS) */

#if	defined(MNT_AIX) && defined(MNT_J2) && MNT_AIX==MNT_J2
		    case MNT_AIX:
			ty = "jfs2";
			break;
#else	/* !defined(MNT_AIX) || !defined(MNT_J2) || MNT_AIX!=MNT_J2 */
# if	defined(MNT_AIX)
		    case MNT_AIX:
			ty = "oaix";
			break;
# endif	/* defined(MNT_AIX) */
# if	defined(MNT_J2)
		    case MNT_J2:
			ty = "jfs2";
			break;
# endif	/* defined(MNT_J2) */
#endif	/* defined(MNT_AIX) && defined(MNT_H2) && MNT_AIX==MNT_J2 */

		    case MNT_CDROM:
			ty = "cdrom";
			break;
		    case MNT_JFS:
			ty = "jfs";
			break;
		    case MNT_NFS:
			ty = "nfs";
			break;

#if	defined(MNT_NFS3)
		    case MNT_NFS3:
			ty = "nfs3";
			break;
#endif	/* defined(MNT_NFS3) */

#if	defined(HASPROCFS)
		    case MNT_PROCFS:
			ty = HASPROCFS;
			break;
#endif	/* defined(HASPROCFS) */

#if	defined(MNT_SANFS)
		    case MNT_SANFS:
			ty = "sanfs";
			break;
#endif	/* defined(MNT_SANFS) */

		    default:
			ty = "unknown";
		    }
		    (void) fprintf(stderr,
			"%s: WARNING: can't stat() %s file system %s\n",
			Pn, ty, dir);
		    (void) fprintf(stderr,
			"      Output information may be incomplete.\n");
		}
	    /*
	     * Assemble alternate device number and mode flags.
	     */
		(void) bzero((char *)&sb, sizeof(sb));
		if (v->vmt_flags & MNT_REMOTE) {

#if	AIXA<2
		    sb.st_dev = (dev_t)(SDEV_REMOTE | v->vmt_vfsnumber);
#else	/* AIXA>=2 */
		    sb.st_dev = (dev_t)(SDEV_REMOTE | (SDEV_REMOTE << 32)
			      |         v->vmt_vfsnumber);
#endif	/* AIXA<2 */

		} else {

#if	defined(HAS_AFS)
		    if (v->vmt_gfstype == MNT_AFS)
			sb.st_dev = AFSDEV;
		    else
#endif	/* defined(HAS_AFS) */

#if	AIXA>=2 && defined(HASPROCFS)
		    if (v->vmt_gfstype == MNT_PROCFS) {

		    /*
		     * !!!DEBUG!!!   !!!DEBUG!!!   !!!DEBUG!!!   !!!DEBUG!!!
		     *
		     * The following *hack* is required to make the vmount
		     * structure's device number match what stat(2)
		     * errnoneously returns on ia64 AIX 5L.
 		     *
		     * REMOVE THIS CODE WHEN STAT(2) IS FIXED!!!
		     */
			sb.st_dev = (dev_t)(v->vmt_fsid.val[0]
				  &         0x7fffffffffffffff);
		    /*
		     * !!!DEBUG!!!   !!!DEBUG!!!   !!!DEBUG!!!   !!!DEBUG!!!
 		     */

		    }
		    else
#endif	/* AIXA>=2 && defined(HASPROCFS) */

			sb.st_dev = (dev_t)v->vmt_fsid.val[0];
		}
		if (!Fwarn)
		    (void) fprintf(stderr,
			"      assuming \"dev=%#lx\" from mount table\n",
			sb.st_dev);
		sb.st_mode = S_IFDIR | 0777;
	    }
	/*
	 * Allocate space for the directory (mounted on) and resolve
	 * any symbolic links.
	 */
	    if (dn)
		(void) free((FREE_P *)dn);
	    if (!(dn = mkstrcpy(dir, (MALLOC_S *)NULL))) {

no_space_for_mount:

		(void) fprintf(stderr, "%s: no space for mount at %s (%s)\n",
		    Pn, fs, dir);
		Exit(1);
	    }
	    if (!(ln = Readlink(dn))) {
		if (!Fwarn) {
		    (void) fprintf(stderr,
			"      Output information may be incomplete.\n");
		}
		continue;
	    }
	    if (ln != dn) {
		(void) free((FREE_P *)dn);
		dn = ln;
	    }
	    if (*dn != '/')
		continue;
	/*
	 * Allocate a local mounts structure and fill the directory information.
	 */
	    if (!(mtp = (struct mounts *)malloc(
			(MALLOC_S)sizeof(struct mounts))))
		goto no_space_for_mount;
	    mtp->dir = dn;
	    dn = (char *)NULL;
	    mtp->dev = sb.st_dev;
	    mtp->inode = (INODETYPE)sb.st_ino;
	    mtp->mode = sb.st_mode;
	    mtp->rdev = sb.st_rdev;

#if	defined(HASFSTYPE)
	    mtp->fstype = sb.st_vfstype;
#endif	/* defined(HASFSTYPE) */

	    mtp->next = Lmi;
	/*
	 * Form the file system (mounted-on) device name.  Resolve any
	 * symbolic links.  Allocate space for the result and store it in
	 * the local mounts structure.
	 */
	    if (h && (v->vmt_flags & MNT_REMOTE)) {
		if (!(dn = mkstrcat(h, -1, *h ? ":" : "", 1, fs, -1,
				    (MALLOC_S *)NULL)))
		    goto no_space_for_mount;
	    } else {
		if (!(dn = mkstrcpy(fs, (MALLOC_S *)NULL)))
		    goto no_space_for_mount;
	    }
	    mtp->fsname = dn;
	    ln = Readlink(dn);
	    dn = (char *)NULL;
	/*
	 * Stat the file system (mounted-on) device name to get its modes.
	 * Set the modes to zero if the stat fails.  Add file system
	 * (mounted-on) device information to the local mountsstructure.
	 */
	    if (!ln || statsafely(ln, &sb))
		sb.st_mode = 0;
	    mtp->fsnmres = ln;
	    mtp->fs_mode = sb.st_mode;
	    Lmi = mtp;
        }
/*
 * Clean up and return local mount info table address.
 */
	if (dn)
	    (void) free((FREE_P *)dn);
	if (vt)
	    (void) free((FREE_P *)vt);
	Lmist = 1;
	return(Lmi);
}