Imported Upstream version 4.87
[platform/upstream/lsof.git] / store.c
1 /*
2  * store.c - common global storage for lsof
3  */
4
5
6 /*
7  * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana
8  * 47907.  All rights reserved.
9  *
10  * Written by Victor A. Abell
11  *
12  * This software is not subject to any license of the American Telephone
13  * and Telegraph Company or the Regents of the University of California.
14  *
15  * Permission is granted to anyone to use this software for any purpose on
16  * any computer system, and to alter it and redistribute it freely, subject
17  * to the following restrictions:
18  *
19  * 1. Neither the authors nor Purdue University are responsible for any
20  *    consequences of the use of this software.
21  *
22  * 2. The origin of this software must not be misrepresented, either by
23  *    explicit claim or by omission.  Credit to the authors and Purdue
24  *    University must appear in documentation and sources.
25  *
26  * 3. Altered versions must be plainly marked as such, and must not be
27  *    misrepresented as being the original software.
28  *
29  * 4. This notice may not be removed or altered.
30  */
31
32 #ifndef lint
33 static char copyright[] =
34 "@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n";
35 static char *rcsid = "$Id: store.c,v 1.41 2013/01/02 17:14:59 abe Exp $";
36 #endif
37
38
39 #include "lsof.h"
40
41
42 /*
43  * Global storage definitions
44  */
45
46 #if     defined(HASBLKDEV)
47 struct l_dev *BDevtp = (struct l_dev *)NULL;
48                                 /* block device table pointer */
49 int BNdev = 0;                  /* number of entries in BDevtp[] */
50 struct l_dev **BSdev = (struct l_dev **)NULL;
51                                 /* pointer to BDevtp[] pointers, sorted
52                                  * by device */
53 #endif  /* defined(HASBLKDEV) */
54
55 int CkPasswd = 0;               /* time to check /etc/passwd for change */
56
57 #if     defined(HAS_STD_CLONE)
58 struct clone *Clone = (struct clone *)NULL;
59                                 /* clone device list */
60 #endif  /* defined(HAS_STD_CLONE) */
61
62 int CmdColW;                    /* COMMAND column width */
63 struct str_lst *Cmdl = (struct str_lst *)NULL;
64                                 /* command names selected with -c */
65 int CmdLim = CMDL;              /* COMMAND column width limit */
66 int Cmdni = 0;                  /* command name inclusions selected with -c */
67 int Cmdnx = 0;                  /* command name exclusions selected with -c */
68 lsof_rx_t *CmdRx = (lsof_rx_t *)NULL;
69                                 /* command regular expression table */
70
71 #if     defined(HASSELINUX)
72 cntxlist_t *CntxArg = (cntxlist_t *)NULL;
73                                 /* security context arguments supplied with
74                                  * -Z */
75 int CntxColW;                   /* security context column width */
76 int CntxStatus = 0;             /* security context status: 0 == disabled,
77                                  * 1 == enabled */
78 #endif  /* defined(HASSELINUX) */
79
80 #if     defined(HASDCACHE)
81 unsigned DCcksum;               /* device cache file checksum */
82 int DCfd = -1;                  /* device cache file descriptor */
83 FILE *DCfs = (FILE *)NULL;      /* stream pointer for DCfd */
84 char *DCpathArg = (char *)NULL; /* device cache path from -D[b|r|u]<path> */
85 char *DCpath[] = {              /* device cache paths, indexed by DCpathX
86                                  *when it's >= 0 */
87         (char *)NULL, (char *)NULL, (char *)NULL, (char *)NULL
88 };
89 int DCpathX = -1;               /* device cache path index:
90                                  *      -1 = path not defined
91                                  *       0 = defined via -D
92                                  *       1 = defined via HASENVDC
93                                  *       2 = defined via HASSYSDC
94                                  *       3 = defined via HASPERSDC and
95                                  *           HASPERSDCPATH */
96 int DCrebuilt = 0;              /* an unsafe device cache file has been
97                                  * rebuilt */
98 int DCstate = 3;                /* device cache state:
99                                  *      0 = ignore (-Di)
100                                  *      1 = build (-Db[path])
101                                  *      2 = read; don't rebuild (-Dr[path])
102                                  *      3 = update; read and rebuild if
103                                  *          necessary (-Du[path])
104                                  */
105 int DCunsafe = 0;               /* device cache file is potentially unsafe,
106                                  * (The [cm]time check failed.) */
107 #endif  /* defined(HASDCACHE) */
108
109 int DChelp = 0;                 /* -D? status */
110
111 int DevColW;                    /* DEVICE column width */
112 dev_t DevDev;                   /* device number of /dev or its equivalent */
113 struct l_dev *Devtp = (struct l_dev *)NULL;
114                                 /* device table pointer */
115
116
117 /*
118  * Externals for a stkdir(), dumbed-down for older AIX compilers.
119  */
120
121 char **Dstk = (char **)NULL;    /* the directory stack */
122 int Dstkx = 0;                  /* Dstk[] index */
123 int Dstkn = 0;                  /* Dstk[] entries allocated */
124 efsys_list_t *Efsysl = (efsys_list_t *)NULL;
125                                 /* file systems for which kernel blocks are
126                                  * to be eliminated */
127 int ErrStat = 0;                /* path stat() error count */
128 uid_t Euid;                     /* effective UID of this lsof process */
129 int Fand = 0;                   /* -a option status */
130 int Fblock = 0;                 /* -b option status */
131 int FcColW;                     /* FCT column width */
132 int Fcntx = 0;                  /* -Z option status */
133 int FdColW;                     /* FD column width */
134 int Ffilesys = 0;               /* -f option status:
135                                  *    0 = paths may be file systems
136                                  *    1 = paths are just files
137                                  *    2 = paths must be file systems */
138
139 #if     defined(HASNCACHE)
140 int Fncache = 1;                /* -C option status */
141 int NcacheReload = 1;           /* 1 == call ncache_load() */
142 #endif  /* defined(HASNCACHE) */
143
144 int Ffield = 0;                 /* -f and -F status */
145 int FgColW;                     /* FILE-FLAG column width */
146 int Fhelp = 0;                  /* -h option status */
147 int Fhost = 1;                  /* -H option status */
148 int Fnet = 0;                   /* -i option status: 0==none
149                                  *                   1==find all
150                                  *                   2==some found*/
151 int FnetTy = 0;                 /* Fnet type request: 0==all
152                                  *                    4==IPv4
153                                  *                    6==IPv6 */
154 int Fnfs = 0;                   /* -N option status: 0==none, 1==find all,
155                                  * 2==some found*/
156 int Fnlink = 0;                 /* -L option status */
157 int Foffset = 0;                /* -o option status */
158 int Fovhd = 0;                  /* -O option status */
159 int Fport = 1;                  /* -P option status */
160
161 #if     !defined(HASNORPC_H)
162 # if    defined(HASPMAPENABLED)
163 int FportMap = 1;               /* +|-M option status */
164 # else  /* !defined(HASPMAPENABLED) */
165 int FportMap = 0;               /* +|-M option status */
166 # endif /* defined(HASPMAPENABLED) */
167 #endif  /* !defined(HASNORPC_H) */
168
169 int Fpgid = 0;                  /* -g option status */
170 int Fppid = 0;                  /* -R option status */
171 int Fsize = 0;                  /* -s option status */
172 int FsColW;                     /* FSTR-ADDR column width */
173 int Fsv = FSV_DEFAULT;          /* file struct value selections */
174 int FsvByf = 0;                 /* Fsv was set by +f */
175 int FsvFlagX = 0;               /* hex format status for FSV_FG */
176 int Ftask = 0;                  /* -K option value */
177 int NiColW;                     /* NODE-ID column width */
178 char *NiTtl = NITTL;            /* NODE-ID column title */
179 int Ftcptpi = TCPTPI_STATE;     /* -T option status */
180 int Fterse = 0;                 /* -t option status */
181 int Funix = 0;                  /* -U option status */
182 int Futol = 1;                  /* -l option status */
183 int Fverbose = 0;               /* -V option status */
184
185 #if     defined(WARNINGSTATE)
186 int Fwarn = 1;                  /* +|-w option status */
187 #else   /* !defined(WARNINGSTATE) */
188 int Fwarn = 0;                  /* +|-w option status */
189 #endif  /* defined(WARNINGSTATE) */
190
191 #if     defined(HASXOPT_VALUE)
192 int Fxopt = HASXOPT_VALUE;      /* -X option status */
193 #endif  /* defined(HASXOPT_VALUE) */
194
195 int Fxover = 0;                 /* -x option value */
196 int Fzone = 0;                  /* -z option status */
197
198 struct fd_lst *Fdl = (struct fd_lst *)NULL;
199                                 /* file descriptors selected with -d */
200 int FdlTy = -1;                 /* Fdl[] type: -1 == none
201                                  *              0 == include
202                                  *              1 == exclude */
203
204 struct fieldsel FieldSel[] = {
205     { LSOF_FID_ACCESS, 0,  LSOF_FNM_ACCESS, NULL,     0          }, /*  0 */
206     { LSOF_FID_CMD,    0,  LSOF_FNM_CMD,    NULL,     0          }, /*  1 */
207     { LSOF_FID_CT,     0,  LSOF_FNM_CT,     &Fsv,     FSV_CT     }, /*  2 */
208     { LSOF_FID_DEVCH,  0,  LSOF_FNM_DEVCH,  NULL,     0          }, /*  3 */
209     { LSOF_FID_DEVN,   0,  LSOF_FNM_DEVN,   NULL,     0          }, /*  4 */
210     { LSOF_FID_FD,     0,  LSOF_FNM_FD,     NULL,     0          }, /*  5 */
211     { LSOF_FID_FA,     0,  LSOF_FNM_FA,     &Fsv,     FSV_FA     }, /*  6 */
212     { LSOF_FID_FG,     0,  LSOF_FNM_FG,     &Fsv,     FSV_FG     }, /*  7 */
213     { LSOF_FID_INODE,  0,  LSOF_FNM_INODE,  NULL,     0          }, /*  8 */
214     { LSOF_FID_NLINK,  0,  LSOF_FNM_NLINK,  &Fnlink,  1          }, /*  9 */
215     { LSOF_FID_TID,    0,  LSOF_FNM_TID,    NULL,     0          }, /* 11 */
216     { LSOF_FID_LOCK,   0,  LSOF_FNM_LOCK,   NULL,     0          }, /* 11 */
217     { LSOF_FID_LOGIN,  0,  LSOF_FNM_LOGIN,  NULL,     0          }, /* 12 */
218     { LSOF_FID_MARK,   1,  LSOF_FNM_MARK,   NULL,     0          }, /* 13 */
219     { LSOF_FID_NAME,   0,  LSOF_FNM_NAME,   NULL,     0          }, /* 14 */
220     { LSOF_FID_NI,     0,  LSOF_FNM_NI,     &Fsv,     FSV_NI     }, /* 15 */
221     { LSOF_FID_OFFSET, 0,  LSOF_FNM_OFFSET, NULL,     0          }, /* 16 */
222     { LSOF_FID_PID,    1,  LSOF_FNM_PID,    NULL,     0          }, /* 17 */
223     { LSOF_FID_PGID,   0,  LSOF_FNM_PGID,   &Fpgid,   1          }, /* 18 */
224     { LSOF_FID_PROTO,  0,  LSOF_FNM_PROTO,  NULL,     0          }, /* 19 */
225     { LSOF_FID_RDEV,   0,  LSOF_FNM_RDEV,   NULL,     0          }, /* 20 */
226     { LSOF_FID_PPID,   0,  LSOF_FNM_PPID,   &Fppid,   1          }, /* 21 */
227     { LSOF_FID_SIZE,   0,  LSOF_FNM_SIZE,   NULL,     0          }, /* 22 */
228     { LSOF_FID_STREAM, 0,  LSOF_FNM_STREAM, NULL,     0          }, /* 23 */
229     { LSOF_FID_TYPE,   0,  LSOF_FNM_TYPE,   NULL,     0          }, /* 24 */
230     { LSOF_FID_TCPTPI, 0,  LSOF_FNM_TCPTPI, &Ftcptpi, TCPTPI_ALL }, /* 25 */
231     { LSOF_FID_UID,    0,  LSOF_FNM_UID,    NULL,     0          }, /* 26 */
232     { LSOF_FID_ZONE,   0,  LSOF_FNM_ZONE,   &Fzone,   1          }, /* 27 */
233     { LSOF_FID_CNTX,   0,  LSOF_FNM_CNTX,   &Fcntx,   1          }, /* 28 */
234     { LSOF_FID_TERM,   0,  LSOF_FNM_TERM,   NULL,     0          }, /* 29 */
235     { ' ',             0,  NULL,            NULL,     0          }
236 };
237
238 int Hdr = 0;                    /* header print status */
239 char *InodeFmt_d = (char *) NULL;
240                                 /* INODETYPE decimal printf specification */
241 char *InodeFmt_x = (char *) NULL;
242                                 /* INODETYPE hexadecimal printf specification */
243 int LastPid = -1;               /* last PID listed (for eliminating duplicates
244                                  * in terse output) */
245 struct lfile *Lf = (struct lfile *)NULL;
246                                 /* current local file structure */
247 struct lproc *Lp = (struct lproc *)NULL;
248                                 /* current local process table entry */
249 struct lproc *Lproc = (struct lproc *)NULL;
250                                 /* local process table */
251 char *Memory = (char *)NULL;    /* core file path */
252 int MntSup = 0;                 /* mount supplement state: 0 == none
253                                  *                         1 == create
254                                  *                         2 == read */
255 char *MntSupP = (char *)NULL;   /* mount supplement path -- if MntSup == 2 */
256
257 #if     defined(HASPROCFS)
258 struct mounts *Mtprocfs = (struct mounts *)NULL;
259                                 /* /proc mount entry */
260 #endif  /* defined(HASPROCFS) */
261
262 int Mxpgid = 0;                 /* maximum process group ID table entries */
263 int Mxpid = 0;                  /* maximum PID table entries */
264 int Mxuid = 0;                  /* maximum UID table entries */
265 gid_t Mygid;                    /* real GID of this lsof process */
266 int Mypid;                      /* lsof's process ID */
267 uid_t Myuid;                    /* real UID of this lsof process */
268 char *Namech = (char *)NULL;    /* name characters for printing */
269 size_t Namechl = (size_t)0;     /* sizeof(Namech) */
270 int NCmdRxU = 0;                /* number of CmdRx[] entries */
271 int Ndev = 0;                   /* number of entries in Devtp[] */
272
273 #if     defined(HASNLIST)
274 struct NLIST_TYPE *Nl = (struct NLIST_TYPE *)NULL;
275                                 /* kernel name list */
276 int Nll = 0;                    /* Nl calloc'd length */
277 #endif  /* defined(HASNLIST) */
278
279 long Nlink = 0l;                /* report nlink values below this number
280                                  * (0 = report all nlink values) */
281 int Nlproc = 0;                 /* number of entries in Lproc[] */
282 int NlColW;                     /* NLINK column width */
283 int NmColW;                     /* NAME column width */
284 char *Nmlst = (char *)NULL;     /* namelist file path */
285 int NodeColW;                   /* NODE column width */
286 int Npgid = 0;                  /* -g option count */
287 int Npgidi = 0;                 /* -g option inclusion count */
288 int Npgidx = 0;                 /* -g option exclusion count */
289 int Npid = 0;                   /* -p option count */
290 int Npidi = 0;                  /* -p option inclusion count */
291 int Npidx = 0;                  /* -p option exclusion count */
292 int Npuns;                      /* number of unselected PIDs (starts at Npid) */
293 int Ntype;                      /* node type (see N_* symbols) */
294 int Nuid = 0;                   /* -u option count */
295 int Nuidexcl = 0;               /* -u option count of UIDs excluded */
296 int Nuidincl = 0;               /* -u option count of UIDs included */
297 struct nwad *Nwad = (struct nwad *)NULL;
298                                 /* list of network addresses */
299 int OffDecDig = OFFDECDIG;      /* offset decimal form (0t...) digit limit */
300 int OffColW;                    /* OFFSET column width */
301 int PgidColW;                   /* PGID column width */
302 int PidColW;                    /* PID column width */
303 struct lfile *Plf = (struct lfile *)NULL;
304                                 /* previous local file structure */
305 char *Pn;                       /* program name */
306 int PpidColW;                   /* PPID column width */
307
308 #if     defined(HASPROCFS)
309 int Procfind = 0;               /* 1 when searching for an proc file system
310                                  * file and one was found */
311 struct procfsid *Procfsid = (struct procfsid *)NULL;
312                                 /* proc file system PID search table */
313 int Procsrch = 0;               /* 1 if searching for any proc file system
314                                  * file */
315 #endif  /* defined(HASPROCFS) */
316
317 int PrPass = 0;                 /* print pass: 0 = compute column widths
318                                  *             1 = print */
319 int RptTm = 0;                  /* repeat time -- set by -r */
320 struct l_dev **Sdev = (struct l_dev **)NULL;
321                                 /* pointer to Devtp[] pointers, sorted
322                                  * by device */
323 int Selall = 1;                 /* all processes are selected (default) */
324 int Selflags = 0;               /* selection flags -- see SEL* in lsof.h */
325 int Setgid = 0;                 /* setgid state */
326 int Selinet = 0;                /* select only Internet socket files */
327 int Setuidroot = 0;             /* setuid-root state */
328 struct sfile *Sfile = (struct sfile *)NULL;
329                                 /* chain of files to search for */
330 struct int_lst *Spgid = (struct int_lst *)NULL;
331                                 /* process group IDs to search for */
332 struct int_lst *Spid = (struct int_lst *)NULL;
333                                 /* Process IDs to search for */
334 struct seluid *Suid = (struct seluid *)NULL;
335                                 /* User IDs to include or exclude */
336 int SzColW;                     /* SIZE column width */
337 int SzOffColW;                  /* SIZE/OFF column width */
338 char *SzOffFmt_0t = (char *)NULL;
339                                 /* SZOFFTYPE 0t%u printf specification */
340 char *SzOffFmt_d = (char *)NULL;
341                                 /* SZOFFTYPE %d printf  specification */
342 char *SzOffFmt_dv = (char *)NULL;
343                                 /* SZOFFTYPE %*d printf  specification */
344 char *SzOffFmt_x = (char *)NULL;
345                                 /* SZOFFTYPE %#x printf  specification */
346 int TaskPrtFl = 0;              /* task print flag */
347 int TcpStAlloc = 0;             /* allocated (possibly unused) entries in TCP 
348                                  * state tables */
349 unsigned char *TcpStI = (unsigned char *)NULL;
350                                 /* included TCP states */
351 int TcpStIn = 0;                /* number of entries in TcpStI[] */
352 int TcpStOff = 0;               /* offset for TCP state number to adjust
353                                  * negative numbers to an index into TcpSt[],
354                                  * TcpStI[] and TcpStX[] */
355 unsigned char *TcpStX = (unsigned char *)NULL;
356                                 /* excluded TCP states */
357 int TcpStXn = 0;                /* number of entries in TcpStX[] */
358 int TcpNstates = 0;             /* number of TCP states -- either in
359                                  * tcpstates[] or TcpSt[] */
360 char **TcpSt = (char **)NULL;   /* local TCP state names, indexed by system
361                                  * state value */
362 char Terminator = '\n';         /* output field terminator */
363 int TidColW = 0;                /* TID column width */
364 int TmLimit = TMLIMIT;          /* Readlink() and stat() timeout (seconds) */
365 int TypeColW;                   /* TYPE column width */
366 int UdpStAlloc = 0;             /* allocated (possibly unused) entries in UDP 
367                                  * state tables */
368 unsigned char *UdpStI = (unsigned char *)NULL;
369                                 /* included UDP states */
370 int UdpStIn = 0;                /* number of entries in UdpStI[] */
371 int UdpStOff = 0;               /* offset for UDP state number to adjust
372                                  * negative numbers to an index into UdpSt[],
373                                  * UdpStI[] and UdpStX[] */
374 unsigned char *UdpStX = (unsigned char *)NULL;
375                                 /* excluded UDP states */
376 int UdpStXn = 0;                /* number of entries in UdpStX[] */
377 int UdpNstates = 0;             /* number of UDP states  in UdpSt[] */
378 char **UdpSt = (char **)NULL;   /* local UDP state names, indexed by system
379                                  * state number */
380 int UserColW;                   /* USER column width */
381
382 #if     defined(HASZONES)
383 znhash_t **ZoneArg = (znhash_t **)NULL;
384                                 /* zone arguments supplied with -z */
385 #endif  /* defined(HASZONES) */
386
387 int ZoneColW;                   /* ZONE column width */