Imported Upstream version 4.88
[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.42 2014/10/13 22:36:20 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 FpipeE = 0;                 /* -E option status: 0==none, 1==info,
160                                  * 2==info+files */
161 int Fport = 1;                  /* -P option status */
162
163 #if     !defined(HASNORPC_H)
164 # if    defined(HASPMAPENABLED)
165 int FportMap = 1;               /* +|-M option status */
166 # else  /* !defined(HASPMAPENABLED) */
167 int FportMap = 0;               /* +|-M option status */
168 # endif /* defined(HASPMAPENABLED) */
169 #endif  /* !defined(HASNORPC_H) */
170
171 int Fpgid = 0;                  /* -g option status */
172 int Fppid = 0;                  /* -R option status */
173 int Fsize = 0;                  /* -s option status */
174 int FsColW;                     /* FSTR-ADDR column width */
175 int Fsv = FSV_DEFAULT;          /* file struct value selections */
176 int FsvByf = 0;                 /* Fsv was set by +f */
177 int FsvFlagX = 0;               /* hex format status for FSV_FG */
178 int Ftask = 0;                  /* -K option value */
179 int NiColW;                     /* NODE-ID column width */
180 char *NiTtl = NITTL;            /* NODE-ID column title */
181 int Ftcptpi = TCPTPI_STATE;     /* -T option status */
182 int Fterse = 0;                 /* -t option status */
183 int Funix = 0;                  /* -U option status */
184 int Futol = 1;                  /* -l option status */
185 int Fverbose = 0;               /* -V option status */
186
187 #if     defined(WARNINGSTATE)
188 int Fwarn = 1;                  /* +|-w option status */
189 #else   /* !defined(WARNINGSTATE) */
190 int Fwarn = 0;                  /* +|-w option status */
191 #endif  /* defined(WARNINGSTATE) */
192
193 #if     defined(HASXOPT_VALUE)
194 int Fxopt = HASXOPT_VALUE;      /* -X option status */
195 #endif  /* defined(HASXOPT_VALUE) */
196
197 int Fxover = 0;                 /* -x option value */
198 int Fzone = 0;                  /* -z option status */
199
200 struct fd_lst *Fdl = (struct fd_lst *)NULL;
201                                 /* file descriptors selected with -d */
202 int FdlTy = -1;                 /* Fdl[] type: -1 == none
203                                  *              0 == include
204                                  *              1 == exclude */
205
206 struct fieldsel FieldSel[] = {
207     { LSOF_FID_ACCESS, 0,  LSOF_FNM_ACCESS, NULL,     0          }, /*  0 */
208     { LSOF_FID_CMD,    0,  LSOF_FNM_CMD,    NULL,     0          }, /*  1 */
209     { LSOF_FID_CT,     0,  LSOF_FNM_CT,     &Fsv,     FSV_CT     }, /*  2 */
210     { LSOF_FID_DEVCH,  0,  LSOF_FNM_DEVCH,  NULL,     0          }, /*  3 */
211     { LSOF_FID_DEVN,   0,  LSOF_FNM_DEVN,   NULL,     0          }, /*  4 */
212     { LSOF_FID_FD,     1,  LSOF_FNM_FD,     NULL,     0          }, /*  5 */
213     { LSOF_FID_FA,     0,  LSOF_FNM_FA,     &Fsv,     FSV_FA     }, /*  6 */
214     { LSOF_FID_FG,     0,  LSOF_FNM_FG,     &Fsv,     FSV_FG     }, /*  7 */
215     { LSOF_FID_INODE,  0,  LSOF_FNM_INODE,  NULL,     0          }, /*  8 */
216     { LSOF_FID_NLINK,  0,  LSOF_FNM_NLINK,  &Fnlink,  1          }, /*  9 */
217     { LSOF_FID_TID,    0,  LSOF_FNM_TID,    NULL,     0          }, /* 11 */
218     { LSOF_FID_LOCK,   0,  LSOF_FNM_LOCK,   NULL,     0          }, /* 11 */
219     { LSOF_FID_LOGIN,  0,  LSOF_FNM_LOGIN,  NULL,     0          }, /* 12 */
220     { LSOF_FID_MARK,   1,  LSOF_FNM_MARK,   NULL,     0          }, /* 13 */
221     { LSOF_FID_NAME,   0,  LSOF_FNM_NAME,   NULL,     0          }, /* 14 */
222     { LSOF_FID_NI,     0,  LSOF_FNM_NI,     &Fsv,     FSV_NI     }, /* 15 */
223     { LSOF_FID_OFFSET, 0,  LSOF_FNM_OFFSET, NULL,     0          }, /* 16 */
224     { LSOF_FID_PID,    1,  LSOF_FNM_PID,    NULL,     0          }, /* 17 */
225     { LSOF_FID_PGID,   0,  LSOF_FNM_PGID,   &Fpgid,   1          }, /* 18 */
226     { LSOF_FID_PROTO,  0,  LSOF_FNM_PROTO,  NULL,     0          }, /* 19 */
227     { LSOF_FID_RDEV,   0,  LSOF_FNM_RDEV,   NULL,     0          }, /* 20 */
228     { LSOF_FID_PPID,   0,  LSOF_FNM_PPID,   &Fppid,   1          }, /* 21 */
229     { LSOF_FID_SIZE,   0,  LSOF_FNM_SIZE,   NULL,     0          }, /* 22 */
230     { LSOF_FID_STREAM, 0,  LSOF_FNM_STREAM, NULL,     0          }, /* 23 */
231     { LSOF_FID_TYPE,   0,  LSOF_FNM_TYPE,   NULL,     0          }, /* 24 */
232     { LSOF_FID_TCPTPI, 0,  LSOF_FNM_TCPTPI, &Ftcptpi, TCPTPI_ALL }, /* 25 */
233     { LSOF_FID_UID,    0,  LSOF_FNM_UID,    NULL,     0          }, /* 26 */
234     { LSOF_FID_ZONE,   0,  LSOF_FNM_ZONE,   &Fzone,   1          }, /* 27 */
235     { LSOF_FID_CNTX,   0,  LSOF_FNM_CNTX,   &Fcntx,   1          }, /* 28 */
236     { LSOF_FID_TERM,   0,  LSOF_FNM_TERM,   NULL,     0          }, /* 29 */
237     { ' ',             0,  NULL,            NULL,     0          }
238 };
239
240 int Hdr = 0;                    /* header print status */
241 char *InodeFmt_d = (char *) NULL;
242                                 /* INODETYPE decimal printf specification */
243 char *InodeFmt_x = (char *) NULL;
244                                 /* INODETYPE hexadecimal printf specification */
245 int LastPid = -1;               /* last PID listed (for eliminating duplicates
246                                  * in terse output) */
247 struct lfile *Lf = (struct lfile *)NULL;
248                                 /* current local file structure */
249 struct lproc *Lp = (struct lproc *)NULL;
250                                 /* current local process table entry */
251 struct lproc *Lproc = (struct lproc *)NULL;
252                                 /* local process table */
253 int MaxFd;                      /* maximum file descriptors to close */
254 char *Memory = (char *)NULL;    /* core file path */
255 int MntSup = 0;                 /* mount supplement state: 0 == none
256                                  *                         1 == create
257                                  *                         2 == read */
258 char *MntSupP = (char *)NULL;   /* mount supplement path -- if MntSup == 2 */
259
260 #if     defined(HASPROCFS)
261 struct mounts *Mtprocfs = (struct mounts *)NULL;
262                                 /* /proc mount entry */
263 #endif  /* defined(HASPROCFS) */
264
265 int Mxpgid = 0;                 /* maximum process group ID table entries */
266 int Mxpid = 0;                  /* maximum PID table entries */
267 int Mxuid = 0;                  /* maximum UID table entries */
268 gid_t Mygid;                    /* real GID of this lsof process */
269 int Mypid;                      /* lsof's process ID */
270 uid_t Myuid;                    /* real UID of this lsof process */
271 char *Namech = (char *)NULL;    /* name characters for printing */
272 size_t Namechl = (size_t)0;     /* sizeof(Namech) */
273 int NCmdRxU = 0;                /* number of CmdRx[] entries */
274 int Ndev = 0;                   /* number of entries in Devtp[] */
275
276 #if     defined(HASNLIST)
277 struct NLIST_TYPE *Nl = (struct NLIST_TYPE *)NULL;
278                                 /* kernel name list */
279 int Nll = 0;                    /* Nl calloc'd length */
280 #endif  /* defined(HASNLIST) */
281
282 long Nlink = 0l;                /* report nlink values below this number
283                                  * (0 = report all nlink values) */
284 int Nlproc = 0;                 /* number of entries in Lproc[] */
285 int NlColW;                     /* NLINK column width */
286 int NmColW;                     /* NAME column width */
287 char *Nmlst = (char *)NULL;     /* namelist file path */
288 int NodeColW;                   /* NODE column width */
289 int Npgid = 0;                  /* -g option count */
290 int Npgidi = 0;                 /* -g option inclusion count */
291 int Npgidx = 0;                 /* -g option exclusion count */
292 int Npid = 0;                   /* -p option count */
293 int Npidi = 0;                  /* -p option inclusion count */
294 int Npidx = 0;                  /* -p option exclusion count */
295 int Npuns;                      /* number of unselected PIDs (starts at Npid) */
296 int Ntype;                      /* node type (see N_* symbols) */
297 int Nuid = 0;                   /* -u option count */
298 int Nuidexcl = 0;               /* -u option count of UIDs excluded */
299 int Nuidincl = 0;               /* -u option count of UIDs included */
300 struct nwad *Nwad = (struct nwad *)NULL;
301                                 /* list of network addresses */
302 int OffDecDig = OFFDECDIG;      /* offset decimal form (0t...) digit limit */
303 int OffColW;                    /* OFFSET column width */
304 int PgidColW;                   /* PGID column width */
305 int PidColW;                    /* PID column width */
306 struct lfile *Plf = (struct lfile *)NULL;
307                                 /* previous local file structure */
308 char *Pn;                       /* program name */
309 int PpidColW;                   /* PPID column width */
310
311 #if     defined(HASPROCFS)
312 int Procfind = 0;               /* 1 when searching for an proc file system
313                                  * file and one was found */
314 struct procfsid *Procfsid = (struct procfsid *)NULL;
315                                 /* proc file system PID search table */
316 int Procsrch = 0;               /* 1 if searching for any proc file system
317                                  * file */
318 #endif  /* defined(HASPROCFS) */
319
320 int PrPass = 0;                 /* print pass: 0 = compute column widths
321                                  *             1 = print */
322 int RptTm = 0;                  /* repeat time -- set by -r */
323 struct l_dev **Sdev = (struct l_dev **)NULL;
324                                 /* pointer to Devtp[] pointers, sorted
325                                  * by device */
326 int Selall = 1;                 /* all processes are selected (default) */
327 int Selflags = 0;               /* selection flags -- see SEL* in lsof.h */
328 int Setgid = 0;                 /* setgid state */
329 int Selinet = 0;                /* select only Internet socket files */
330 int Setuidroot = 0;             /* setuid-root state */
331 struct sfile *Sfile = (struct sfile *)NULL;
332                                 /* chain of files to search for */
333 struct int_lst *Spgid = (struct int_lst *)NULL;
334                                 /* process group IDs to search for */
335 struct int_lst *Spid = (struct int_lst *)NULL;
336                                 /* Process IDs to search for */
337 struct seluid *Suid = (struct seluid *)NULL;
338                                 /* User IDs to include or exclude */
339 int SzColW;                     /* SIZE column width */
340 int SzOffColW;                  /* SIZE/OFF column width */
341 char *SzOffFmt_0t = (char *)NULL;
342                                 /* SZOFFTYPE 0t%u printf specification */
343 char *SzOffFmt_d = (char *)NULL;
344                                 /* SZOFFTYPE %d printf  specification */
345 char *SzOffFmt_dv = (char *)NULL;
346                                 /* SZOFFTYPE %*d printf  specification */
347 char *SzOffFmt_x = (char *)NULL;
348                                 /* SZOFFTYPE %#x printf  specification */
349 int TaskPrtFl = 0;              /* task print flag */
350 int TcpStAlloc = 0;             /* allocated (possibly unused) entries in TCP 
351                                  * state tables */
352 unsigned char *TcpStI = (unsigned char *)NULL;
353                                 /* included TCP states */
354 int TcpStIn = 0;                /* number of entries in TcpStI[] */
355 int TcpStOff = 0;               /* offset for TCP state number to adjust
356                                  * negative numbers to an index into TcpSt[],
357                                  * TcpStI[] and TcpStX[] */
358 unsigned char *TcpStX = (unsigned char *)NULL;
359                                 /* excluded TCP states */
360 int TcpStXn = 0;                /* number of entries in TcpStX[] */
361 int TcpNstates = 0;             /* number of TCP states -- either in
362                                  * tcpstates[] or TcpSt[] */
363 char **TcpSt = (char **)NULL;   /* local TCP state names, indexed by system
364                                  * state value */
365 char Terminator = '\n';         /* output field terminator */
366 int TidColW = 0;                /* TID column width */
367 int TmLimit = TMLIMIT;          /* Readlink() and stat() timeout (seconds) */
368 int TypeColW;                   /* TYPE column width */
369 int UdpStAlloc = 0;             /* allocated (possibly unused) entries in UDP 
370                                  * state tables */
371 unsigned char *UdpStI = (unsigned char *)NULL;
372                                 /* included UDP states */
373 int UdpStIn = 0;                /* number of entries in UdpStI[] */
374 int UdpStOff = 0;               /* offset for UDP state number to adjust
375                                  * negative numbers to an index into UdpSt[],
376                                  * UdpStI[] and UdpStX[] */
377 unsigned char *UdpStX = (unsigned char *)NULL;
378                                 /* excluded UDP states */
379 int UdpStXn = 0;                /* number of entries in UdpStX[] */
380 int UdpNstates = 0;             /* number of UDP states  in UdpSt[] */
381 char **UdpSt = (char **)NULL;   /* local UDP state names, indexed by system
382                                  * state number */
383 int UserColW;                   /* USER column width */
384
385 #if     defined(HASZONES)
386 znhash_t **ZoneArg = (znhash_t **)NULL;
387                                 /* zone arguments supplied with -z */
388 #endif  /* defined(HASZONES) */
389
390 int ZoneColW;                   /* ZONE column width */