forked from cvpcs/android_external_lsof
-
Notifications
You must be signed in to change notification settings - Fork 35
/
dproc.c
1409 lines (1321 loc) · 34 KB
/
dproc.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dproc.c - Linux process access functions for /proc-based lsof
*/
/*
* 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.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright 1997 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: dproc.c,v 1.23 2010/07/29 16:04:52 abe Exp $";
#endif
#include "lsof.h"
/*
* Local definitions
*/
#define FDINFO_FLAGS 1 /* fdinfo flags available */
#define FDINFO_POS 2 /* fdinfo position available */
#define FDINFO_ALL (FDINFO_FLAGS | FDINFO_POS)
#define LSTAT_TEST_FILE "/"
#define LSTAT_TEST_SEEK 1
#if !defined(ULLONG_MAX)
#define ULLONG_MAX 18446744073709551615ULL
#endif /* !defined(ULLONG_MAX) */
/*
* Local structures
*/
struct l_fdinfo {
int flags; /* flags: line value */
off_t pos; /* pos: line value */
};
/*
* Local variables
*/
static short Cckreg; /* conditional status of regular file
* checking:
* 0 = unconditionally check
* 1 = conditionally check */
static short Ckscko; /* socket file only checking status:
* 0 = none
* 1 = check only socket files */
/*
* Local function prototypes
*/
_PROTOTYPE(static int get_fdinfo,(char *p, struct l_fdinfo *fi));
_PROTOTYPE(static int getlinksrc,(char *ln, char *src, int srcl));
_PROTOTYPE(static int nm2id,(char *nm, int *id, int *idl));
_PROTOTYPE(static int read_id_stat,(int ty, char *p, int id, char **cmd,
int *ppid, int *pgid));
_PROTOTYPE(static void process_proc_map,(char *p, struct stat *s, int ss));
_PROTOTYPE(static int process_id,(char *idp, int idpl, char *cmd, UID_ARG uid,
int pid, int ppid, int pgid, int tid));
_PROTOTYPE(static int statEx,(char *p, struct stat *s, int *ss));
#if defined(HASSELINUX)
_PROTOTYPE(static int cmp_cntx_eq,(char *pcntx, char *ucntx));
#include <fnmatch.h>
/*
* cmp_cntx_eq -- compare program and user security contexts
*/
static int
cmp_cntx_eq(pcntx, ucntx)
char *pcntx; /* program context */
char *ucntx; /* user supplied context */
{
return !fnmatch(ucntx, pcntx, 0);
}
/*
* enter_cntx_arg() - enter name ecurity context argument
*/
int
enter_cntx_arg(cntx)
char *cntx; /* context */
{
cntxlist_t *cntxp;
/*
* Search the argument list for a duplicate.
*/
for (cntxp = CntxArg; cntxp; cntxp = cntxp->next) {
if (!strcmp(cntxp->cntx, cntx)) {
if (!Fwarn) {
(void) fprintf(stderr, "%s: duplicate context: %s\n",
Pn, cntx);
}
return(1);
}
}
/*
* Create and link a new context argument list entry.
*/
if (!(cntxp = (cntxlist_t *)malloc((MALLOC_S)sizeof(cntxlist_t)))) {
(void) fprintf(stderr, "%s: no space for context: %s\n", Pn, cntx);
Exit(1);
}
cntxp->f = 0;
cntxp->cntx = cntx;
cntxp->next = CntxArg;
CntxArg = cntxp;
return(0);
}
#endif /* defined(HASSELINUX) */
/*
* gather_proc_info() -- gather process information
*/
void
gather_proc_info()
{
char *cmd, *tcmd;
struct dirent *dp;
unsigned char ht, pidts;
int n, nl, pgid, pid, ppid, rv, tid, tpgid, tppid, tx;
static char *path = (char *)NULL;
static int pathl = 0;
static char *pidpath = (char *)NULL;
static MALLOC_S pidpathl = 0;
static MALLOC_S pidx = 0;
static DIR *ps = (DIR *)NULL;
struct stat sb;
static char *taskpath = (char *)NULL;
static int taskpathl = 0;
static char *tidpath = (char *)NULL;
static int tidpathl = 0;
DIR *ts;
UID_ARG uid;
/*
* Do one-time setup.
*/
if (!pidpath) {
pidx = strlen(PROCFS) + 1;
pidpathl = pidx + 64 + 1; /* 64 is growth room */
if (!(pidpath = (char *)malloc(pidpathl))) {
(void) fprintf(stderr,
"%s: can't allocate %d bytes for \"%s/\"<pid>\n",
Pn, pidpathl, PROCFS);
Exit(1);
}
(void) snpf(pidpath, pidpathl, "%s/", PROCFS);
}
/*
* Get lock and net information.
*/
(void) make_proc_path(pidpath, pidx, &path, &pathl, "locks");
(void) get_locks(path);
(void) make_proc_path(pidpath, pidx, &path, &pathl, "net/");
(void) set_net_paths(path, strlen(path));
/*
* If only socket files have been selected, or socket files have been selected
* ANDed with other selection options, enable the skipping of regular files.
*
* If socket files and some process options have been selected, enable
* conditional skipping of regular file; i.e., regular files will be skipped
* unless they belong to a process selected by one of the specified options.
*/
if (Selflags & SELNW) {
/*
* Some network files selection options have been specified.
*/
if (Fand || !(Selflags & ~SELNW)) {
/*
* Selection ANDing or only network file options have been
* specified, so set unconditional skipping of regular files
* and socket file only checking.
*/
Cckreg = 0;
Ckscko = 1;
} else {
/*
* If ORed file selection options have been specified, or no ORed
* process selection options have been specified, enable
* unconditional file checking and clear socket file only checking.
*
* If only ORed process selection options have been specified,
* enable conditional file skipping and socket file only checking.
*/
if ((Selflags & SELFILE) || !(Selflags & SELPROC))
Cckreg = Ckscko = 0;
else
Cckreg = Ckscko = 1;
}
} else {
/*
* No network file selection options were specified. Enable
* unconditional file checking and clear socket file only checking.
*/
Cckreg = Ckscko = 0;
}
/*
* Read /proc, looking for PID directories. Open each one and
* gather its process and file information.
*/
if (!ps) {
if (!(ps = opendir(PROCFS))) {
(void) fprintf(stderr, "%s: can't open %s\n", Pn, PROCFS);
Exit(1);
}
} else
(void) rewinddir(ps);
while ((dp = readdir(ps))) {
if (nm2id(dp->d_name, &pid, &n))
continue;
/*
* Build path to PID's directory.
*/
if ((pidx + n + 1 + 1) > pidpathl) {
pidpathl = pidx + n + 1 + 1 + 64;
if (!(pidpath = (char *)realloc((MALLOC_P *)pidpath, pidpathl)))
{
(void) fprintf(stderr,
"%s: can't allocate %d bytes for \"%s/%s/\"\n",
Pn, pidpathl, PROCFS, dp->d_name);
Exit(1);
}
}
(void) snpf(pidpath + pidx, pidpathl - pidx, "%s/", dp->d_name);
n += (pidx + 1);
/*
* Process the PID's stat info.
*/
if (stat(pidpath, &sb))
continue;
uid = (UID_ARG)sb.st_uid;
ht = pidts = 0;
#if defined(HASTASKS)
/*
* If task reporting is selected, check the tasks of the process first,
* so that the "-p<PID> -aK" options work properly.
*/
if ((Selflags & SELTASK)) {
(void) make_proc_path(pidpath, n, &taskpath, &taskpathl,
"task");
tx = n + 4;
if ((ts = opendir(taskpath))) {
/*
* Process the PID's tasks. Record the open files of those
* whose TIDs do not match the PID and which are themselves
* not zombies.
*/
while ((dp = readdir(ts))) {
/*
* Get the task ID. Skip the task if its ID matches the
* process PID.
*/
if (nm2id(dp->d_name, &tid, &nl))
continue;
if (tid == pid) {
pidts = 1;
continue;
}
/*
* Form the path for the TID.
*/
if ((tx + 1 + nl + 1 + 4) > tidpathl) {
tidpathl = tx + 1 + n + 1 + 4 + 64;
if (tidpath)
tidpath = (char *)realloc((MALLOC_P *)tidpath,
tidpathl);
else
tidpath = (char *)malloc((MALLOC_S)tidpathl);
if (!tidpath) {
(void) fprintf(stderr,
"%s: can't allocate %d task bytes", Pn,
tidpathl);
(void) fprintf(stderr, " for \"%s/%s/stat\"\n",
taskpath, dp->d_name);
Exit(1);
}
}
(void) snpf(tidpath, tidpathl, "%s/%s/stat", taskpath,
dp->d_name);
/*
* Check the task state.
*/
rv = read_id_stat(1, tidpath, tid, &tcmd, &tppid,
&tpgid);
if ((rv < 0) || (rv == 1))
continue;
/*
* Attempt to record the task.
*/
if (!process_id(tidpath, (tx + 1 + nl+ 1), tcmd, uid,
pid, tppid, tpgid, tid))
{
ht = 1;
}
}
(void) closedir(ts);
}
}
#endif /* defined(HASTASKS) */
/*
* If the main process is a task and task selection has been specified
* along with option ANDing, enter the main process temporarily as a
* task, so that the "-aK" option set lists the main process along
* with its tasks.
*/
(void) make_proc_path(pidpath, n, &path, &pathl, "stat");
if (((rv = read_id_stat(0, path, pid, &cmd, &ppid, &pgid)) >= 0)
&& (rv != 1))
{
tid = (Fand && ht && pidts && (Selflags & SELTASK)) ? pid : 0;
if ((!process_id(pidpath, n, cmd, uid, pid, ppid, pgid, tid))
&& tid)
{
Lp->tid = 0;
}
}
}
}
/*
* get_fdinfo() - get values from /proc/<PID>fdinfo/FD
*/
static int
get_fdinfo(p, fi)
char *p; /* path to fdinfo file */
struct l_fdinfo *fi; /* pointer to local fdinfo values
* return structure */
{
char buf[MAXPATHLEN + 1], *ep, **fp;
FILE *fs;
int rv = 0;
unsigned long ul;
unsigned long long ull;
/*
* Signal no values returned (0) if no fdinfo pointer was provided or if the
* fdinfo path can't be opened.
*/
if (!fi)
return(0);
if (!p || !*p || !(fs = fopen(p, "r")))
return(0);
/*
* Read the fdinfo file.
*/
while (fgets(buf, sizeof(buf), fs)) {
if (get_fields(buf, (char *)NULL, &fp, (int *)NULL, 0) < 2)
continue;
if (!fp[0] || !*fp[0] || !fp[1] || !*fp[1])
continue;
if (!strcmp(fp[0], "flags:")) {
/*
* Process a "flags:" line.
*/
ep = (char *)NULL;
if ((ul = strtoul(fp[1], &ep, 0)) == ULONG_MAX
|| !ep || *ep)
continue;
fi->flags = (unsigned int)ul;
if ((rv |= FDINFO_FLAGS) == FDINFO_ALL)
break;
} else if (!strcmp(fp[0], "pos:")) {
/*
* Process a "pos:" line.
*/
ep = (char *)NULL;
if ((ull = strtoull(fp[1], &ep, 0)) == ULLONG_MAX
|| !ep || *ep)
continue;
fi->pos = (off_t)ull;
if ((rv |= FDINFO_POS) == FDINFO_ALL)
break;
}
}
fclose(fs);
/*
* Signal via the return value what information was obtained. (0 == none)
*/
return(rv);
}
/*
* getlinksrc() - get the source path name for the /proc/<PID>/fd/<FD> link
*/
static int
getlinksrc(ln, src, srcl)
char *ln; /* link path */
char *src; /* link source path return address */
int srcl; /* length of src[] */
{
char *cp;
int ll;
if ((ll = readlink(ln, src, srcl - 1)) < 1
|| ll >= srcl)
return(-1);
src[ll] = '\0';
if (*src == '/')
return(ll);
if ((cp = strchr(src, ':'))) {
*cp = '\0';
ll = strlen(src);
}
return(ll);
}
/*
* initialize() - perform all initialization
*/
void
initialize()
{
int fd;
struct l_fdinfo fi;
char path[MAXPATHLEN];
struct stat sb;
/*
* Test for -i and -X option conflict.
*/
if (Fxopt && (Fnet || Nwad)) {
(void) fprintf(stderr, "%s: -i is useless when -X is specified.\n",
Pn);
usage(1, 0, 0);
}
/*
* Open LSTAT_TEST_FILE and seek to byte LSTAT_TEST_SEEK, then lstat the
* /proc/<PID>/fd/<FD> for LSTAT_TEST_FILE to see what position is reported.
* If the result is LSTAT_TEST_SEEK, enable offset reporting.
*
* If the result isn't LSTAT_TEST_SEEK, next check the fdinfo file for the
* open LSTAT_TEST_FILE file descriptor. If it exists and contains a "pos:"
* value, and if the value is LSTAT_TEST_SEEK, enable offset reporting.
*/
if ((fd = open(LSTAT_TEST_FILE, O_RDONLY)) >= 0) {
if (lseek(fd, (off_t)LSTAT_TEST_SEEK, SEEK_SET)
== (off_t)LSTAT_TEST_SEEK) {
(void) snpf(path, sizeof(path), "%s/%d/fd/%d", PROCFS, Mypid,
fd);
if (!lstat(path, &sb)) {
if (sb.st_size == (off_t)LSTAT_TEST_SEEK)
OffType = 1;
}
}
if (!OffType) {
(void) snpf(path, sizeof(path), "%s/%d/fdinfo/%d", PROCFS,
Mypid, fd);
if (get_fdinfo(path, &fi) & FDINFO_POS) {
if (fi.pos == (off_t)LSTAT_TEST_SEEK)
OffType = 2;
}
}
(void) close(fd);
}
if (!OffType) {
if (Foffset && !Fwarn)
(void) fprintf(stderr,
"%s: WARNING: can't report offset; disregarding -o.\n",
Pn);
Foffset = 0;
Fsize = 1;
}
if (Fsv && (OffType != 2)) {
if (!Fwarn && FsvByf)
(void) fprintf(stderr,
"%s: WARNING: can't report file flags; disregarding +f.\n",
Pn);
Fsv = 0;
}
/*
* Make sure the local mount info table is loaded if doing anything other
* than just Internet lookups. (HasNFS is defined during the loading of the
* local mount table.)
*/
if (Selinet == 0)
(void) readmnt();
}
/*
* make_proc_path() - make a path in a /proc directory
*
* entry:
* pp = pointer to /proc prefix
* lp = length of prefix
* np = pointer to malloc'd buffer to receive new file's path
* nl = length of new file path buffer
* sf = new path's suffix
*
* return: length of new path
* np = updated with new path
* nl = updated with new path length
*/
int
make_proc_path(pp, pl, np, nl, sf)
char *pp; /* path prefix -- e.g., /proc/<pid>/ */
int pl; /* strlen(pp) */
char **np; /* malloc'd receiving buffer */
int *nl; /* strlen(*np) */
char *sf; /* suffix of new path */
{
char *cp;
MALLOC_S rl, sl;
sl = strlen(sf);
if ((rl = pl + sl + 1) > *nl) {
if ((cp = *np))
cp = (char *)realloc((MALLOC_P *)cp, rl);
else
cp = (char *)malloc(rl);
if (!cp) {
(void) fprintf(stderr,
"%s: can't allocate %d bytes for %s%s\n",
Pn, rl, pp, sf);
Exit(1);
}
*nl = rl;
*np = cp;
}
(void) snpf(*np, *nl, "%s", pp);
(void) snpf(*np + pl, *nl - pl, "%s", sf);
return(rl - 1);
}
/*
* nm2id() - convert a name to an integer ID
*/
static int
nm2id(nm, id, idl)
char *nm; /* pointer to name */
int *id; /* pointer to ID receiver */
int *idl; /* pointer to ID length receiver */
{
register int tid, tidl;
for (*id = *idl = tid = tidl = 0; *nm; nm++) {
#if defined(__STDC__) /* { */
if (!isdigit((unsigned char)*nm))
#else /* !defined(__STDC__) } { */
if (!isascii(*nm) || !isdigit((unsigned char)*cp))
#endif /* defined(__STDC__) } */
{
return(1);
}
tid = tid * 10 + (int)(*nm - '0');
tidl++;
}
*id = tid;
*idl = tidl;
return(0);
}
/*
* open_proc_stream() -- open a /proc stream
*/
FILE *
open_proc_stream(p, m, buf, sz, act)
char *p; /* pointer to path to open */
char *m; /* pointer to mode -- e.g., "r" */
char **buf; /* pointer tp setvbuf() address
* (NULL if none) */
size_t *sz; /* setvbuf() size (0 if none or if
* getpagesize() desired */
int act; /* fopen() failure action:
* 0 : return (FILE *)NULL
* <>0 : fprintf() an error message
* and Exit(1)
*/
{
FILE *fs; /* opened stream */
static size_t psz = (size_t)0; /* page size */
size_t tsz; /* temporary size */
/*
* Open the stream.
*/
if (!(fs = fopen(p, m))) {
if (!act)
return((FILE *)NULL);
(void) fprintf(stderr, "%s: can't fopen(%s, \"%s\"): %s\n",
Pn, p, m, strerror(errno));
Exit(1);
}
/*
* Return the stream if no buffer change is required.
*/
if (!buf)
return(fs);
/*
* Determine the buffer size required.
*/
if (!(tsz = *sz)) {
if (!psz)
psz = getpagesize();
tsz = psz;
}
/*
* Allocate a buffer for the stream, as required.
*/
if (!*buf) {
if (!(*buf = (char *)malloc((MALLOC_S)tsz))) {
(void) fprintf(stderr,
"%s: can't allocate %d bytes for %s stream buffer\n",
Pn, (int)tsz, p);
Exit(1);
}
*sz = tsz;
}
/*
* Assign the buffer to the stream.
*/
if (setvbuf(fs, *buf, _IOFBF, tsz)) {
(void) fprintf(stderr, "%s: setvbuf(%s)=%d failure: %s\n",
Pn, p, (int)tsz, strerror(errno));
Exit(1);
}
return(fs);
}
/*
* process_id - process ID: PID or LWP
*
* return: 0 == ID processed
* 1 == ID not processed
*/
static int
process_id(idp, idpl, cmd, uid, pid, ppid, pgid, tid)
char *idp; /* pointer to ID's path */
int idpl; /* pointer to ID's path length */
char *cmd; /* pointer to ID's command */
UID_ARG uid; /* ID's UID */
int pid; /* ID's PID */
int ppid; /* parent PID */
int pgid; /* parent GID */
int tid; /* task ID, if non-zero */
{
int av;
static char *dpath = (char *)NULL;
static int dpathl = 0;
short enls, enss, lnk, oty, pn, pss, sf, tsf;
int fd, i, ls, n, ss, sv;
struct l_fdinfo fi;
DIR *fdp;
struct dirent *fp;
static char *ipath = (char *)NULL;
static int ipathl = 0;
int j = 0;
struct stat lsb, sb;
char nmabuf[MAXPATHLEN + 1], pbuf[MAXPATHLEN + 1];
static char *path = (char *)NULL;
static int pathl = 0;
static char *pathi = (char *)NULL;
static int pathil = 0;
int txts = 0;
#if defined(HASSELINUX)
cntxlist_t *cntxp;
#endif /* defined(HASSELINUX) */
/*
* See if process is excluded.
*/
if (is_proc_excl(pid, pgid, uid, &pss, &sf, tid)
|| is_cmd_excl(cmd, &pss, &sf))
return(1);
if (Cckreg) {
/*
* If conditional checking of regular files is enabled, enable
* socket file only checking, based on the process' selection
* status.
*/
Ckscko = (sf & SELPROC) ? 0 : 1;
}
alloc_lproc(pid, pgid, ppid, uid, cmd, (int)pss, (int)sf);
Lp->tid = tid;
Plf = (struct lfile *)NULL;
/*
* Process the ID's current working directory info.
*/
if (!Ckscko) {
(void) make_proc_path(idp, idpl, &path, &pathl, "cwd");
alloc_lfile(CWD, -1);
if (getlinksrc(path, pbuf, sizeof(pbuf)) < 1) {
if (!Fwarn) {
(void) memset((void *)&sb, 0, sizeof(sb));
lnk = ss = 0;
(void) snpf(nmabuf, sizeof(nmabuf), "(readlink: %s)",
strerror(errno));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
pn = 1;
} else
pn = 0;
} else {
lnk = pn = 1;
ss = SB_ALL;
if (HasNFS) {
if ((sv = statsafely(path, &sb)))
sv = statEx(pbuf, &sb, &ss);
} else
sv = stat(path, &sb);
if (sv) {
ss = 0;
if (!Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf), "(stat: %s)",
strerror(errno));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
}
}
}
if (pn) {
(void) process_proc_node(lnk ? pbuf : path,
&sb, ss,
(struct stat *)NULL, 0);
if (Lf->sf)
link_lfile();
}
}
/*
* Process the ID's root directory info.
*/
if (!Ckscko) {
(void) make_proc_path(idp, idpl, &path, &pathl, "root");
alloc_lfile(RTD, -1);
if (getlinksrc(path, pbuf, sizeof(pbuf)) < 1) {
if (!Fwarn) {
(void) memset((void *)&sb, 0, sizeof(sb));
lnk = ss = 0;
(void) snpf(nmabuf, sizeof(nmabuf), "(readlink: %s)",
strerror(errno));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
pn = 1;
} else
pn = 0;
} else {
lnk = pn = 1;
ss = SB_ALL;
if (HasNFS) {
if ((sv = statsafely(path, &sb)))
sv = statEx(pbuf, &sb, &ss);
} else
sv = stat(path, &sb);
if (sv) {
ss = 0;
if (!Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf), "(stat: %s)",
strerror(errno));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
}
}
}
if (pn) {
(void) process_proc_node(lnk ? pbuf : path,
&sb, ss,
(struct stat *)NULL, 0);
if (Lf->sf)
link_lfile();
}
}
/*
* Process the ID's execution info.
*/
if (!Ckscko) {
(void) make_proc_path(idp, idpl, &path, &pathl, "exe");
alloc_lfile("txt", -1);
if (getlinksrc(path, pbuf, sizeof(pbuf)) < 1) {
(void) memset((void *)&sb, 0, sizeof(sb));
lnk = ss = 0;
if (!Fwarn) {
if ((errno != ENOENT) || uid) {
(void) snpf(nmabuf, sizeof(nmabuf), "(readlink: %s)",
strerror(errno));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
}
pn = 1;
} else
pn = 0;
} else {
lnk = pn = 1;
ss = SB_ALL;
if (HasNFS) {
if ((sv = statsafely(path, &sb))) {
sv = statEx(pbuf, &sb, &ss);
if (!sv && (ss & SB_DEV) && (ss & SB_INO))
txts = 1;
}
} else
sv = stat(path, &sb);
if (sv) {
ss = 0;
if (!Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf), "(stat: %s)",
strerror(errno));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
}
} else
txts = 1;
}
if (pn) {
(void) process_proc_node(lnk ? pbuf : path,
&sb, ss,
(struct stat *)NULL, 0);
if (Lf->sf)
link_lfile();
}
}
/*
* Process the ID's memory map info.
*/
if (!Ckscko) {
(void) make_proc_path(idp, idpl, &path, &pathl, "maps");
(void) process_proc_map(path, txts ? &sb : (struct stat *)NULL,
txts ? ss : 0);
}
#if defined(HASSELINUX)
/*
* Process the PID's SELinux context.
*/
if (Fcntx) {
/*
* If the -Z (cntx) option was specified, match the valid contexts.
*/
errno = 0;
if (getpidcon(pid, &Lp->cntx) == -1) {
Lp->cntx = (char *)NULL;
if (!Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf),
"(getpidcon: %s)", strerror(errno));
if (!(Lp->cntx = strdup(nmabuf))) {
(void) fprintf(stderr,
"%s: no context error space: PID %ld",
Pn, (long)Lp->pid);
Exit(1);
}
}
} else if (CntxArg) {
/*
* See if context includes the process.
*/
for (cntxp = CntxArg; cntxp; cntxp = cntxp->next) {
if (cmp_cntx_eq(Lp->cntx, cntxp->cntx)) {
cntxp->f = 1;
Lp->pss |= PS_PRI;
Lp->sf |= SELCNTX;
break;
}
}
}
}
#endif /* defined(HASSELINUX) */
/*
* Process the ID's file descriptor directory.
*/
if ((i = make_proc_path(idp, idpl, &dpath, &dpathl, "fd/")) < 3)
return(0);
dpath[i - 1] = '\0';
if ((OffType == 2)
&& ((j = make_proc_path(idp, idpl, &ipath, &ipathl, "fdinfo/")) >= 7))
oty = 1;
else
oty = 0;
if (!(fdp = opendir(dpath))) {
if (!Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf), "%s (opendir: %s)",
dpath, strerror(errno));
alloc_lfile("NOFD", -1);
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
link_lfile();
}
return(0);
}
dpath[i - 1] = '/';
while ((fp = readdir(fdp))) {
if (nm2id(fp->d_name, &fd, &n))
continue;
(void) make_proc_path(dpath, i, &path, &pathl, fp->d_name);
(void) alloc_lfile((char *)NULL, fd);
if (getlinksrc(path, pbuf, sizeof(pbuf)) < 1) {
(void) memset((void *)&sb, 0, sizeof(sb));
lnk = ss = 0;
if (!Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf), "(readlink: %s)",
strerror(errno));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
pn = 1;
} else
pn = 0;
} else {
lnk = 1;
if (HasNFS) {
if (lstatsafely(path, &lsb)) {
(void) statEx(pbuf, &lsb, &ls);
enls = errno;
} else {
enls = 0;
ls = SB_ALL;
}
if (statsafely(path, &sb)) {
(void) statEx(pbuf, &sb, &ss);
enss = errno;
} else {
enss = 0;
ss = SB_ALL;
}
} else {
ls = lstat(path, &lsb) ? 0 : SB_ALL;
enls = errno;
ss = stat(path, &sb) ? 0 : SB_ALL;
enss = errno;
}
if (!ls && !Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf), "lstat: %s)",
strerror(enls));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
}
if (!ss && !Fwarn) {
(void) snpf(nmabuf, sizeof(nmabuf), "(stat: %s)",
strerror(enss));
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void) add_nma(nmabuf, strlen(nmabuf));
}
if (Ckscko) {
if ((ss & SB_MODE) && ((sb.st_mode & S_IFMT) == S_IFSOCK))