File: store.c

package info (click to toggle)
lsof 4.37-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,244 kB
  • ctags: 5,398
  • sloc: ansic: 47,236; sh: 4,196; makefile: 1,232; perl: 616; awk: 214; asm: 2
file content (303 lines) | stat: -rw-r--r-- 10,521 bytes parent folder | download
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
303
/*
 * store.c - common global storage 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: store.c,v 1.14 98/08/18 08:07:35 abe Exp $";
#endif


#include "lsof.h"


/*
 * Global storage definitions
 */

#if	defined(HASBLKDEV)
struct l_dev *BDevtp = (struct l_dev *)NULL;
				/* block device table pointer */
int BNdev = 0;			/* number of entries in BDevtp[] */
struct l_dev **BSdev = (struct l_dev **)NULL;
				/* pointer to BDevtp[] pointers, sorted
				 * by device */
#endif	/* defined(HASBLKDEV) */

int CkPasswd = 0;		/* time to check /etc/passwd for change */
struct str_lst *Cmdl = (struct str_lst *)NULL;
				/* command names selected with -c */
int CmdColW;			/* COMMAND column width */

#if	defined(HASDCACHE)
unsigned DCcksum;		/* device cache file checksum */
int DCfd = -1;			/* device cache file descriptor */
FILE *DCfs = (FILE *)NULL;	/* stream pointer for DCfd */
char *DCpathArg = (char *)NULL;	/* device cache path from -D[b|r|u]<path> */
char *DCpath[] = {		/* device cache paths, indexed by DCpathX
				 *when it's >= 0 */
	(char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL
};
int DCpathX = -1;		/* device cache path index:
				 *	-1 = path not defined
				 *	 0 = defined via -D
				 *	 1 = defined via HASENVDC
				 *	 2 = defined via HASSYSDC
				 *	 3 = defined via HASPERSDC and
				 *	     HASPERSDCPATH */
int DCrebuilt = 0;		/* an unsafe device cache file has been
				 * rebuilt */
int DCstate = 3;		/* device cache state:
				 *	0 = ignore (-Di)
				 *	1 = build (-Db[path])
				 *	2 = read; don't rebuild (-Dr[path])
				 *	3 = update; read and rebuild if
				 *	    necessary (-Du[path])
				 */
int DCunsafe = 0;		/* device cache file is potentially unsafe,
				 * (The [cm]time check failed.) */
#endif	/* defined(HASDCACHE) */

int DChelp = 0;			/* -D? status */

int DevColW;			/* DEVICE column width */
struct l_dev *Devtp = (struct l_dev *)NULL;
				/* device table pointer */
int Fand = 0;			/* -a option status */
int Fblock = 0;			/* -b option status */
int FdColW;			/* FD column width */
int Ffilesys = 0;		/* -f option status:
				 *    0 = paths may be file systems
				 *    1 = paths are just files
				 *    2 = paths must be file systems */

#if	defined(HASNCACHE)
int Fncache = 1;		/* -C option status */
#endif	/* defined(HASNCACHE) */

int Ffield = 0;			/* -f and -F status */
int Fhelp = 0;			/* -h option status */
int Fhost = 1;			/* -H option status */
int Fnet = 0;			/* -i option status: 0==none, 1==find all,
				 * 2==some found*/
int Fnfs = 0;			/* -N option status: 0==none, 1==find all,
				 * 2==some found*/
int Foffset = 0;		/* -o option status */
int Fovhd = 0;			/* -O option status */
int Fport = 1;			/* -P option status */

#if	defined(HASPMAPENABLED)
int FportMap = 1;		/* +|-M option status */
#else	/* !defined(HASPMAPENABLED) */
int FportMap = 0;		/* +|-M option status */
#endif	/* defined(HASPMAPENABLED) */

int Fpgrp = 0;			/* -g option status */

#if	defined(HASPPID)
int Fppid = 0;			/* -R option status */
#endif	/* defined(HASPPID) */

int Fsize = 0;			/* -s option status */
int Ftcptpi = TCPTPI_STATE;	/* -T option status */
int Fterse = 0;			/* -t option status */
int Funix = 0;			/* -U option status */
int Futol = 1;			/* -l option status */
int Fverbose = 0;		/* -V option status */

#if	defined(WARNINGSTATE)
int Fwarn = 1;			/* +|-w option status */
#else	/* !defined(WARNINGSTATE) */
int Fwarn = 0;			/* +|-w option status */
#endif	/* defined(WARNINGSTATE) */

#if	defined(HASXOPT_VALUE)
int Fxopt = HASXOPT_VALUE;	/* -X option status */
#endif	/* defined(HASXOPT_VALUE) */

struct fd_lst *Fdl = (struct fd_lst *)NULL;
				/* file descriptors selected with -d */

struct fieldsel FieldSel[] = {
	{ LSOF_FID_ACCESS,	0,	LSOF_FNM_ACCESS	},	/*  0 */
	{ LSOF_FID_CMD,		0,	LSOF_FNM_CMD	},	/*  1 */
	{ LSOF_FID_DEVCH,	0,	LSOF_FNM_DEVCH	},	/*  2 */
	{ LSOF_FID_DEVN,	0,	LSOF_FNM_DEVN	},	/*  3 */
	{ LSOF_FID_FD,		0,	LSOF_FNM_FD	},	/*  4 */
	{ LSOF_FID_INODE,	0,	LSOF_FNM_INODE	},	/*  5 */
	{ LSOF_FID_LOCK,	0,	LSOF_FNM_LOCK	},	/*  6 */
	{ LSOF_FID_LOGIN,	0,	LSOF_FNM_LOGIN	},	/*  7 */
	{ LSOF_FID_MARK,	1,	LSOF_FNM_MARK	},	/*  8 */
	{ LSOF_FID_NAME,	0,	LSOF_FNM_NAME	},	/*  9 */
	{ LSOF_FID_OFFSET,	0,	LSOF_FNM_OFFSET	},	/* 10 */
	{ LSOF_FID_PID,		1,	LSOF_FNM_PID	},	/* 11 */
	{ LSOF_FID_PGRP,	0,	LSOF_FNM_PGRP	},	/* 12 */
	{ LSOF_FID_PROTO,	0,	LSOF_FNM_PROTO	},	/* 13 */
	{ LSOF_FID_PPID,	0,	LSOF_FNM_PPID	},	/* 14 */
	{ LSOF_FID_SIZE,	0,	LSOF_FNM_SIZE	},	/* 15 */
	{ LSOF_FID_STREAM,	0,	LSOF_FNM_STREAM	},	/* 16 */
	{ LSOF_FID_TYPE,	0,	LSOF_FNM_TYPE	},	/* 17 */
	{ LSOF_FID_TCPTPI,	0,	LSOF_FNM_TCPTPI	},	/* 18 */
	{ LSOF_FID_UID,		0,	LSOF_FNM_UID	},	/* 19 */
	{ LSOF_FID_TERM,	0,	LSOF_FNM_TERM	},	/* 20 */

#if	defined(HASFIELDAP1)
	{ '1',			0,	HASFIELDAP1},		/* TERM + 1 */
#endif	/* defined(HASFIELDAP1) */

#if	defined(HASFIELDAP2)
	{ '2',			0,	HASFIELDAP2},		/* TERM + 2 */
#endif	/* defined(HASFIELDAP2) */

#if	defined(HASFIELDAP3)
	{ '3',			0,	HASFIELDAP3},		/* TERM + 3 */
#endif	/* defined(HASFIELDAP3) */

#if	defined(HASFIELDAP4)
	{ '4',			0,	HASFIELDAP4},		/* TERM + 4 */
#endif	/* defined(HASFIELDAP4) */

#if	defined(HASFIELDAP5)
	{ '5',			0,	HASFIELDAP5},		/* TERM + 5 */
#endif	/* defined(HASFIELDAP5) */

#if	defined(HASFIELDAP6)
	{ '6',			0,	HASFIELDAP6},		/* TERM + 6 */
#endif	/* defined(HASFIELDAP6) */

#if	defined(HASFIELDAP7)
	{ '7',			0,	HASFIELDAP7},		/* TERM + 7 */
#endif	/* defined(HASFIELDAP7) */

#if	defined(HASFIELDAP8)
	{ '8',			0,	HASFIELDAP8},		/* TERM + 8 */
#endif	/* defined(HASFIELDAP8) */

#if	defined(HASFIELDAP9)
	{ '9',			0,	HASFIELDAP9},		/* TERM + 9 */
#endif	/* defined(HASFIELDAP9) */

	{ ' ',			0,	NULL		}
};

int Hdr = 0;			/* header print status */
struct lfile *Lf = (struct lfile *)NULL;
				/* current local file structure */
struct lproc *Lp = (struct lproc *)NULL;
				/* current local process table entry */
struct lproc *Lproc = (struct lproc *)NULL;
				/* local process table */
char *Memory = (char *)NULL;	/* core file path */

#if	defined(HASPROCFS)
struct mounts *Mtprocfs = (struct mounts *)NULL;
				/* /proc mount entry */
#endif	/* defined(HASPROCFS) */

int Mxpgrp = 0;			/* maximum process groip ID table entries */
int Mxpid = 0;			/* maximum PID table entries */
int Mxuid = 0;			/* maximum UID table entries */
gid_t Mygid;			/* real GID of this lsof process */
int Mypid;			/* lsof's process ID */
uid_t Myuid;			/* real UID of this lsof process */
char *Namech = (char *)NULL;	/* name characters for printing */
int Ndev = 0;			/* number of entries in Devtp[] */

#if	defined(HASNLIST)
struct NLIST_TYPE *Nl = (struct NLIST_TYPE *)NULL;
				/* kernel name list */
int Nll = 0;			/* Nl calloc'd length */
#endif	/* defined(HASNLIST) */

int Nlproc = 0;			/* number of entries in Lproc[] */
int NmColW;			/* NAME column width */
char *Nmlst = (char *)NULL;	/* namelist file path */
int NodeColW;			/* NODE column width */
int Npgrp = 0;			/* -g option count */
int Npid = 0;			/* -p option count */
int Npuns;			/* number of unselected PIDs (starts at Npid) */
int Ntype;			/* node type (see N_* symbols) */
int Nuid = 0;			/* -u option count */
int Nuidexcl = 0;		/* -u option count of UIDs excluded */
int Nuidincl = 0;		/* -u option count of UIDs included */
struct nwad *Nwad = (struct nwad *)NULL;
				/* list of network addresses */
int OffDecDig = OFFDECDIG;	/* offset decimal form (0t...) digit limit */
int OffColW;			/* OFFSET column width */
int PgrpColW;			/* PGRP column width */
int PidColW;			/* PID column width */
struct lfile *Plf = (struct lfile *)NULL;
				/* previous local file structure */
char *Pn;			/* program name */
int PpidColW;			/* PPID column width */

#if	defined(HASPROCFS)
int Procfind = 0;		/* 1 when searching for an proc file system
				 * file and one was found */
struct procfsid *Procfsid = (struct procfsid *)NULL;
				/* proc file system PID search table */
int Procsrch = 0;		/* 1 if searching for any proc file system
				 * file */
#endif	/* defined(HASPROCFS) */

int PrPass = 0;			/* print pass: 0 = compute column widths
				 *	       1 = print */
int RptTm = 0;			/* repeat time -- set by -r */
struct l_dev **Sdev = (struct l_dev **)NULL;
				/* pointer to Devtp[] pointers, sorted
				 * by device */
int Selall = 1;			/* all processes are selected (default) */
int Selflags = 0;		/* selection flags -- see SEL* in lsof.h */
int Setgid = 0;			/* setgid state */
int Selinet = 0;		/* select only Internet socket files */
int Setuidroot = 0;		/* setuid-root state */
struct sfile *Sfile = (struct sfile *)NULL;
				/* chain of files to search for */
struct int_lst *Spgrp = (struct int_lst *)NULL;
				/* process group IDs to search for */
struct int_lst *Spid = (struct int_lst *)NULL;
				/* Process IDs to search for */
struct seluid *Suid = (struct seluid *)NULL;
				/* User IDs to include or exclude */
int SzColW;			/* SIZE column width */
int SzOffColW;			/* SIZE/OFF column width */
char *SzOffFmt_0t = (char *)NULL;
				/* SZOFFTYPE 0t%u printf specification */
char *SzOffFmt_d = (char *)NULL;
				/* SZOFFTYPE %d printf  specification */
char *SzOffFmt_dv = (char *)NULL;
				/* SZOFFTYPE %*d printf  specification */
char *SzOffFmt_x = (char *)NULL;
				/* SZOFFTYPE %#x printf  specification */
char Terminator = '\n';		/* output field terminator */
int TmLimit = TMLIMIT;		/* Readlink() and stat() timeout (seconds) */
int TypeColW;			/* TYPE column width */
int UserColW;			/* USER column width */