forked from lsof-org/lsof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
1697 lines (1516 loc) · 35.5 KB
/
misc.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
/*
* misc.c - common miscellaneous 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";
#endif
#include "lsof.h"
#if defined(HASWIDECHAR)
# if defined(WIDECHARINCL)
#include WIDECHARINCL
# endif /* defined(WIDECHARINCL) */
# if defined(HASWCTYPE_H)
#include <wctype.h>
# endif /* defined(HASWCTYPE_H) */
#endif /* defined(HASWIDECHAR) */
/*
* Local definitions
*/
#if !defined(MAXSYMLINKS)
#define MAXSYMLINKS 32
#endif /* !defined(MAXSYMLINKS) */
/*
* Local function prototypes
*/
_PROTOTYPE(static void closePipes,(void));
_PROTOTYPE(static int dolstat,(char *path, char *buf, int len));
_PROTOTYPE(static int dostat,(char *path, char *buf, int len));
_PROTOTYPE(static int doreadlink,(char *path, char *buf, int len));
_PROTOTYPE(static int doinchild,(int (*fn)(), char *fp, char *rbuf, int rbln));
#if defined(HASINTSIGNAL)
_PROTOTYPE(static int handleint,(int sig));
#else /* !defined(HASINTSIGNAL) */
_PROTOTYPE(static void handleint,(int sig));
#endif /* defined(HASINTSIGNAL) */
/*
* Local variables
*/
static pid_t Cpid = 0; /* child PID */
static jmp_buf Jmp_buf; /* jump buffer */
static int Pipes[] = /* pipes for child process */
{ -1, -1, -1, -1 };
static int CtSigs[] = { 0, SIGINT, SIGKILL };
/* child termination signals (in order
* of application) -- the first is a
* dummy to allow pipe closure to
* cause the child to exit */
#define NCTSIGS (sizeof(CtSigs) / sizeof(int))
#if defined(HASNLIST)
/*
* build-Nl() - build kernel name list table
*/
static struct drive_Nl *Build_Nl = (struct drive_Nl *)NULL;
/* the default Drive_Nl address */
void
build_Nl(d)
struct drive_Nl *d; /* data to drive the construction */
{
struct drive_Nl *dp;
int i, n;
for (dp = d, n = 0; dp->nn; dp++, n++)
;
if (n < 1) {
(void) fprintf(stderr,
"%s: can't calculate kernel name list length\n", Pn);
Exit(1);
}
if (!(Nl = (struct NLIST_TYPE *)calloc((n + 1),
sizeof(struct NLIST_TYPE))))
{
(void) fprintf(stderr,
"%s: can't allocate %d bytes to kernel name list structure\n",
Pn, (int)((n + 1) * sizeof(struct NLIST_TYPE)));
Exit(1);
}
for (dp = d, i = 0; i < n; dp++, i++) {
Nl[i].NL_NAME = dp->knm;
}
Nll = (int)((n + 1) * sizeof(struct NLIST_TYPE));
Build_Nl = d;
}
#endif /* defined(HASNLIST) */
/*
* childx() - make child process exit (if possible)
*/
void
childx()
{
static int at, sx;
pid_t wpid;
if (Cpid > 1) {
/*
* First close the pipes to and from the child. That should cause the
* child to exit. Compute alarm time shares.
*/
(void) closePipes();
if ((at = TmLimit / NCTSIGS) < TMLIMMIN)
at = TMLIMMIN;
/*
* Loop, waiting for the child to exit. After the first pass, help
* the child exit by sending it signals.
*/
for (sx = 0; sx < NCTSIGS; sx++) {
if (setjmp(Jmp_buf)) {
/*
* An alarm has rung. Disable further alarms.
*
* If there are more signals to send, continue the signal loop.
*
* If the last signal has been sent, issue a warning (unless
* warninge have been suppressed) and exit the signal loop.
*/
(void) alarm(0);
(void) signal(SIGALRM, SIG_DFL);
if (sx < (NCTSIGS - 1))
continue;
if (!Fwarn)
(void) fprintf(stderr,
"%s: WARNING -- child process %d may be hung.\n",
Pn, (int)Cpid);
break;
}
/*
* Send the next signal to the child process, after the first pass
* through the loop.
*
* Wrap the wait() with an alarm.
*/
if (sx)
(void) kill(Cpid, CtSigs[sx]);
(void) signal(SIGALRM, handleint);
(void) alarm(at);
wpid = (pid_t) wait(NULL);
(void) alarm(0);
(void) signal(SIGALRM, SIG_DFL);
if (wpid == Cpid)
break;
}
Cpid = 0;
}
}
/*
* closePipes() - close open pipe file descriptors
*/
static void
closePipes()
{
int i;
for (i = 0; i < 4; i++) {
if (Pipes[i] >= 0) {
(void) close(Pipes[i]);
Pipes[i] = -1;
}
}
}
/*
* compdev() - compare Devtp[] entries
*/
int
compdev(a1, a2)
COMP_P *a1, *a2;
{
struct l_dev **p1 = (struct l_dev **)a1;
struct l_dev **p2 = (struct l_dev **)a2;
if ((dev_t)((*p1)->rdev) < (dev_t)((*p2)->rdev))
return(-1);
if ((dev_t)((*p1)->rdev) > (dev_t)((*p2)->rdev))
return(1);
if ((INODETYPE)((*p1)->inode) < (INODETYPE)((*p2)->inode))
return(-1);
if ((INODETYPE)((*p1)->inode) > (INODETYPE)((*p2)->inode))
return(1);
return(strcmp((*p1)->name, (*p2)->name));
}
/*
* doinchild() -- do a function in a child process
*/
static int
doinchild(fn, fp, rbuf, rbln)
int (*fn)(); /* function to perform */
char *fp; /* function parameter */
char *rbuf; /* response buffer */
int rbln; /* response buffer length */
{
int en, rv;
/*
* Check reply buffer size.
*/
if (!Fovhd && rbln > MAXPATHLEN) {
(void) fprintf(stderr,
"%s: doinchild error; response buffer too large: %d\n",
Pn, rbln);
Exit(1);
}
/*
* Set up to handle an alarm signal; handle an alarm signal; build
* pipes for exchanging information with a child process; start the
* child process; and perform functions in the child process.
*/
if (!Fovhd) {
if (setjmp(Jmp_buf)) {
/*
* Process an alarm that has rung.
*/
(void) alarm(0);
(void) signal(SIGALRM, SIG_DFL);
(void) childx();
errno = ETIMEDOUT;
return(1);
} else if (!Cpid) {
/*
* Create pipes to exchange function information with a child
* process.
*/
if (pipe(Pipes) < 0 || pipe(&Pipes[2]) < 0) {
(void) fprintf(stderr, "%s: can't open pipes: %s\n",
Pn, strerror(errno));
Exit(1);
}
/*
* Fork a child to execute functions.
*/
if ((Cpid = fork()) == 0) {
/*
* Begin the child process.
*/
int r_al, r_rbln;
char r_arg[MAXPATHLEN+1];
union {
char r_rbuf[MAXPATHLEN+1];
/*
* This field is only for adjusting the alignment of r_rbuf that
* can be used as an argument for stat().
*/
struct stat _;
} r;
int (*r_fn)();
/*
* Close sufficient open file descriptors except Pipes[0] and
* Pipes[3].
*/
#if defined(HAS_DUP2) && defined(HAS_CLOSEFROM)
int rc;
rc = dup2(Pipes[0], 0);
if (rc < 0) {
(void) fprintf(stderr,
"%s: can't dup Pipes[0] to fd 0: %s\n",
Pn, strerror(errno));
Exit(1);
}
Pipes[0] = 0;
rc = dup2(Pipes[3], 1);
if (rc < 0) {
(void) fprintf(stderr,
"%s: can't dup Pipes.[3] to fd 1: %s\n",
Pn, strerror(errno));
Exit(1);
}
Pipes[3] = 1;
(void) closefrom(2);
Pipes[1] = -1;
Pipes[2] = -1;
#else /* !defined(HAS_DUP2) && !defined(HAS_CLOSEFROM) */
int fd;
for (fd = 0; fd < MaxFd; fd++) {
if (fd == Pipes[0] || fd == Pipes[3])
continue;
(void) close(fd);
if (fd == Pipes[1])
Pipes[1] = -1;
else if (fd == Pipes[2])
Pipes[2] = -1;
}
if (Pipes[1] >= 0) {
(void) close(Pipes[1]);
Pipes[1] = -1;
}
if (Pipes[2] >= 0) {
(void) close(Pipes[2]);
Pipes[2] = -1;
}
#endif /* defined(HAS_DUP2) && defined(HAS_CLOSEFROM) */
/*
* Read function requests, process them, and return replies.
*/
for (;;) {
if (read(Pipes[0], (char *)&r_fn, sizeof(r_fn))
!= (int)sizeof(r_fn)
|| read(Pipes[0], (char *)&r_al, sizeof(int))
!= (int)sizeof(int)
|| r_al < 1
|| r_al > (int)sizeof(r_arg)
|| read(Pipes[0], r_arg, r_al) != r_al
|| read(Pipes[0], (char *)&r_rbln, sizeof(r_rbln))
!= (int)sizeof(r_rbln)
|| r_rbln < 1 || r_rbln > (int)sizeof(r.r_rbuf))
break;
zeromem (r.r_rbuf, r_rbln);
rv = r_fn(r_arg, r.r_rbuf, r_rbln);
en = errno;
if (write(Pipes[3], (char *)&rv, sizeof(rv))
!= sizeof(rv)
|| write(Pipes[3], (char *)&en, sizeof(en))
!= sizeof(en)
|| write(Pipes[3], r.r_rbuf, r_rbln) != r_rbln)
break;
}
(void) _exit(0);
}
/*
* Continue in the parent process to finish the setup.
*/
if (Cpid < 0) {
(void) fprintf(stderr, "%s: can't fork: %s\n",
Pn, strerror(errno));
Exit(1);
}
(void) close(Pipes[0]);
(void) close(Pipes[3]);
Pipes[0] = Pipes[3] = -1;
}
}
if (!Fovhd) {
int len;
/*
* Send a function to the child and wait for the response.
*/
len = strlen(fp) + 1;
(void) signal(SIGALRM, handleint);
(void) alarm(TmLimit);
if (write(Pipes[1], (char *)&fn, sizeof(fn)) != sizeof(fn)
|| write(Pipes[1], (char *)&len, sizeof(len)) != sizeof(len)
|| write(Pipes[1], fp, len) != len
|| write(Pipes[1], (char *)&rbln, sizeof(rbln)) != sizeof(rbln)
|| read(Pipes[2], (char *)&rv, sizeof(rv)) != sizeof(rv)
|| read(Pipes[2], (char *)&en, sizeof(en)) != sizeof(en)
|| read(Pipes[2], rbuf, rbln) != rbln) {
(void) alarm(0);
(void) signal(SIGALRM, SIG_DFL);
(void) childx();
errno = ECHILD;
return(-1);
}
} else {
/*
* Do the operation directly -- not in a child.
*/
(void) signal(SIGALRM, handleint);
(void) alarm(TmLimit);
rv = fn(fp, rbuf, rbln);
en = errno;
}
/*
* Function completed, response collected -- complete the operation.
*/
(void) alarm(0);
(void) signal(SIGALRM, SIG_DFL);
errno = en;
return(rv);
}
/*
* dolstat() - do an lstat() function
*/
static int
dolstat(path, rbuf, rbln)
char *path; /* path */
char *rbuf; /* response buffer */
int rbln; /* response buffer length */
/* ARGSUSED */
{
return(lstat(path, (struct stat *)rbuf));
}
/*
* doreadlink() -- do a readlink() function
*/
static int
doreadlink(path, rbuf, rbln)
char *path; /* path */
char *rbuf; /* response buffer */
int rbln; /* response buffer length */
{
return(readlink(path, rbuf, rbln));
}
/*
* dostat() - do a stat() function
*/
static int
dostat(path, rbuf, rbln)
char *path; /* path */
char *rbuf; /* response buffer */
int rbln; /* response buffer length */
/* ARGSUSED */
{
return(stat(path, (struct stat *)rbuf));
}
#if defined(WILLDROPGID)
/*
* dropgid() - drop setgid permission
*/
void
dropgid()
{
if (!Setuidroot && Setgid) {
if (setgid(Mygid) < 0) {
(void) fprintf(stderr, "%s: can't setgid(%d): %s\n",
Pn, (int)Mygid, strerror(errno));
Exit(1);
}
Setgid = 0;
}
}
#endif /* defined(WILLDROPGID) */
/*
* enter_dev_ch() - enter device characters in file structure
*/
void
enter_dev_ch(m)
char *m;
{
char *mp;
if (!m || *m == '\0')
return;
if (!(mp = mkstrcpy(m, (MALLOC_S *)NULL))) {
(void) fprintf(stderr, "%s: no more dev_ch space at PID %d: \n",
Pn, Lp->pid);
safestrprt(m, stderr, 1);
Exit(1);
}
if (Lf->dev_ch)
(void) free((FREE_P *)Lf->dev_ch);
Lf->dev_ch = mp;
}
/*
* enter_IPstate() -- enter a TCP or UDP state
*/
void
enter_IPstate(ty, nm, nr)
char *ty; /* type -- TCP or UDP */
char *nm; /* state name (may be NULL) */
int nr; /* state number */
{
#if defined(USE_LIB_PRINT_TCPTPI)
TcpNstates = nr;
#else /* !defined(USE_LIB_PRINT_TCPTPI) */
int al, i, j, oc, nn, ns, off, tx;
char *cp;
MALLOC_S len;
/*
* Check the type name and set the type index.
*/
if (!ty) {
(void) fprintf(stderr,
"%s: no type specified to enter_IPstate()\n", Pn);
Exit(1);
}
if (!strcmp(ty, "TCP"))
tx = 0;
else if (!strcmp(ty, "UDP"))
tx = 1;
else {
(void) fprintf(stderr, "%s: unknown type for enter_IPstate: %s\n",
Pn, ty);
Exit(1);
}
/*
* If the name argument is NULL, reduce the allocated table to its minimum
* size.
*/
if (!nm) {
if (tx) {
if (UdpSt) {
if (!UdpNstates) {
(void) free((MALLOC_P *)UdpSt);
UdpSt = (char **)NULL;
}
if (UdpNstates < UdpStAlloc) {
len = (MALLOC_S)(UdpNstates * sizeof(char *));
if (!(UdpSt = (char **)realloc((MALLOC_P *)UdpSt, len)))
{
(void) fprintf(stderr,
"%s: can't reduce UdpSt[]\n", Pn);
Exit(1);
}
}
UdpStAlloc = UdpNstates;
}
} else {
if (TcpSt) {
if (!TcpNstates) {
(void) free((MALLOC_P *)TcpSt);
TcpSt = (char **)NULL;
}
if (TcpNstates < TcpStAlloc) {
len = (MALLOC_S)(TcpNstates * sizeof(char *));
if (!(TcpSt = (char **)realloc((MALLOC_P *)TcpSt, len)))
{
(void) fprintf(stderr,
"%s: can't reduce TcpSt[]\n", Pn);
Exit(1);
}
}
TcpStAlloc = TcpNstates;
}
}
return;
}
/*
* Check the name and number.
*/
if ((len = (size_t)strlen(nm)) < 1) {
(void) fprintf(stderr,
"%s: bad %s name (\"%s\"), number=%d\n", Pn, ty, nm, nr);
Exit(1);
}
/*
* Make a copy of the name.
*/
if (!(cp = mkstrcpy(nm, (MALLOC_S *)NULL))) {
(void) fprintf(stderr,
"%s: enter_IPstate(): no %s space for %s\n",
Pn, ty, nm);
Exit(1);
}
/*
* Set the necessary offset for using nr as an index. If it is
* a new offset, adjust previous entries.
*/
if ((nr < 0) && ((off = -nr) > (tx ? UdpStOff : TcpStOff))) {
if (tx ? UdpSt : TcpSt) {
/*
* A new, larger offset (smaller negative state number) could mean
* a previously allocated state table must be enlarged and its
* previous entries moved.
*/
oc = off - (tx ? UdpStOff : TcpStOff);
al = tx ? UdpStAlloc : TcpStAlloc;
ns = tx ? UdpNstates : TcpNstates;
if ((nn = ns + oc) >= al) {
while ((nn + 5) > al) {
al += TCPUDPALLOC;
}
len = (MALLOC_S)(al * sizeof(char *));
if (tx) {
if (!(UdpSt = (char **)realloc((MALLOC_P *)UdpSt, len)))
goto no_IP_space;
UdpStAlloc = al;
} else {
if (!(TcpSt = (char **)realloc((MALLOC_P *)TcpSt, len)))
goto no_IP_space;
TcpStAlloc = al;
}
for (i = 0, j = oc; i < oc; i++, j++) {
if (tx) {
if (i < UdpNstates)
UdpSt[j] = UdpSt[i];
UdpSt[i] = (char *)NULL;
} else {
if (i < TcpNstates)
TcpSt[j] = TcpSt[i];
TcpSt[i] = (char *)NULL;
}
}
if (tx)
UdpNstates += oc;
else
TcpNstates += oc;
}
}
if (tx)
UdpStOff = off;
else
TcpStOff = off;
}
/*
* Enter name as {Tc|Ud}pSt[nr + {Tc|Ud}pStOff].
*
* Allocate space, as required.
*/
al = tx ? UdpStAlloc : TcpStAlloc;
off = tx ? UdpStOff : TcpStOff;
nn = nr + off + 1;
if (nn > al) {
i = tx ? UdpNstates : TcpNstates;
while ((nn + 5) > al) {
al += TCPUDPALLOC;
}
len = (MALLOC_S)(al * sizeof(char *));
if (tx) {
if (UdpSt)
UdpSt = (char **)realloc((MALLOC_P *)UdpSt, len);
else
UdpSt = (char **)malloc(len);
if (!UdpSt) {
no_IP_space:
(void) fprintf(stderr, "%s: no %s state space\n", Pn, ty);
Exit(1);
}
UdpNstates = nn;
UdpStAlloc = al;
} else {
if (TcpSt)
TcpSt = (char **)realloc((MALLOC_P *)TcpSt, len);
else
TcpSt = (char **)malloc(len);
if (!TcpSt)
goto no_IP_space;
TcpNstates = nn;
TcpStAlloc = al;
}
while (i < al) {
if (tx)
UdpSt[i] = (char *)NULL;
else
TcpSt[i] = (char *)NULL;
i++;
}
} else {
if (tx) {
if (nn > UdpNstates)
UdpNstates = nn;
} else {
if (nn > TcpNstates)
TcpNstates = nn;
}
}
if (tx) {
if (UdpSt[nr + UdpStOff]) {
dup_IP_state:
(void) fprintf(stderr,
"%s: duplicate %s state %d (already %s): %s\n",
Pn, ty, nr,
tx ? UdpSt[nr + UdpStOff] : TcpSt[nr + TcpStOff],
nm);
Exit(1);
}
UdpSt[nr + UdpStOff] = cp;
} else {
if (TcpSt[nr + TcpStOff])
goto dup_IP_state;
TcpSt[nr + TcpStOff] = cp;
}
#endif /* defined(USE_LIB_PRINT_TCPTPI) */
}
/*
* enter_nm() - enter name in local file structure
*/
void
enter_nm(m)
char *m;
{
char *mp;
if (!m || *m == '\0')
return;
if (!(mp = mkstrcpy(m, (MALLOC_S *)NULL))) {
(void) fprintf(stderr, "%s: no more nm space at PID %d for: ",
Pn, Lp->pid);
safestrprt(m, stderr, 1);
Exit(1);
}
if (Lf->nm)
(void) free((FREE_P *)Lf->nm);
Lf->nm = mp;
}
/*
* Exit() - do a clean exit()
*/
void
Exit(xv)
int xv; /* exit() value */
{
(void) childx();
#if defined(HASDCACHE)
if (DCrebuilt && !Fwarn)
(void) fprintf(stderr, "%s: WARNING: %s was updated.\n",
Pn, DCpath[DCpathX]);
#endif /* defined(HASDCACHE) */
exit(xv);
}
#if defined(HASNLIST)
/*
* get_Nl_value() - get Nl value for nickname
*/
int
get_Nl_value(nn, d, v)
char *nn; /* nickname of requested entry */
struct drive_Nl *d; /* drive_Nl table that built Nl
* (if NULL, use Build_Nl) */
KA_T *v; /* returned value (if NULL,
* return nothing) */
{
int i;
if (!Nl || !Nll)
return(-1);
if (!d)
d = Build_Nl;
for (i = 0; d->nn; d++, i++) {
if (strcmp(d->nn, nn) == 0) {
if (v)
*v = (KA_T)Nl[i].n_value;
return(i);
}
}
return(-1);
}
#endif /* defined(HASNLIST) */
/*
* handleint() - handle an interrupt
*/
#if defined(HASINTSIGNAL)
static int
#else
static void
#endif
/* ARGSUSED */
handleint(sig)
int sig;
{
longjmp(Jmp_buf, 1);
}
/*
* hashbyname() - hash by name
*/
int
hashbyname(nm, mod)
char *nm; /* pointer to NUL-terminated name */
int mod; /* hash modulus */
{
int i, j;
for (i = j = 0; *nm; nm++) {
i ^= (int)*nm << j;
if (++j > 7)
j = 0;
}
return(((int)(i * 31415)) & (mod - 1));
}
/*
* is_nw_addr() - is this network address selected?
*/
int
is_nw_addr(ia, p, af)
unsigned char *ia; /* Internet address */
int p; /* port */
int af; /* address family -- e.g., AF_INET,
* AF_INET6 */
{
struct nwad *n;
if (!(n = Nwad))
return(0);
for (; n; n = n->next) {
if (n->proto) {
if (strcasecmp(n->proto, Lf->iproto) != 0)
continue;
}
if (af && n->af && af != n->af)
continue;
#if defined(HASIPv6)
if (af == AF_INET6) {
if (n->a[15] || n->a[14] || n->a[13] || n->a[12]
|| n->a[11] || n->a[10] || n->a[9] || n->a[8]
|| n->a[7] || n->a[6] || n->a[5] || n->a[4]
|| n->a[3] || n->a[2] || n->a[1] || n->a[0]) {
if (ia[15] != n->a[15] || ia[14] != n->a[14]
|| ia[13] != n->a[13] || ia[12] != n->a[12]
|| ia[11] != n->a[11] || ia[10] != n->a[10]
|| ia[9] != n->a[9] || ia[8] != n->a[8]
|| ia[7] != n->a[7] || ia[6] != n->a[6]
|| ia[5] != n->a[5] || ia[4] != n->a[4]
|| ia[3] != n->a[3] || ia[2] != n->a[2]
|| ia[1] != n->a[1] || ia[0] != n->a[0])
continue;
}
} else if (af == AF_INET)
#endif /* defined(HASIPv6) */
{
if (n->a[3] || n->a[2] || n->a[1] || n->a[0]) {
if (ia[3] != n->a[3] || ia[2] != n->a[2]
|| ia[1] != n->a[1] || ia[0] != n->a[0])
continue;
}
}
#if defined(HASIPv6)
else
continue;
#endif /* defined(HASIPv6) */
if (n->sport == -1 || (p >= n->sport && p <= n->eport)) {
n->f = 1;
return(1);
}
}
return(0);
}
/*
* mkstrcpy() - make a string copy in malloc()'d space
*
* return: copy pointer
* copy length (optional)
*/
char *
mkstrcpy(src, rlp)
char *src; /* source */
MALLOC_S *rlp; /* returned length pointer (optional)
* The returned length is an strlen()
* equivalent */
{
MALLOC_S len;
char *ns;
len = (MALLOC_S)(src ? strlen(src) : 0);
ns = (char *)malloc(len + 1);
if (ns) {
if (src)
(void) snpf(ns, len + 1, "%s", src);
else
*ns = '\0';
}
if (rlp)
*rlp = len;
return(ns);
}
/*
* mkstrcat() - make a catenated copy of up to three strings under optional
* string-by-string count control
*
* return: copy pointer
* copy string length (optional)
*/
char *
mkstrcat(s1, l1, s2, l2, s3, l3, clp)
char *s1; /* source string 1 */
int l1; /* length of string 1 (-1 if none) */
char *s2; /* source string 2 */
int l2; /* length of string 2 (-1 if none) */
char *s3; /* source string 3 (optional) */
int l3 ; /* length of string 3 (-1 if none) */
MALLOC_S *clp; /* pointer to return of copy length
* (optional) */
{
MALLOC_S cl, len1, len2, len3;
char *cp;
if (s1)
len1 = (MALLOC_S)((l1 >= 0) ? l1 : strlen(s1));
else
len1 = (MALLOC_S)0;
if (s2)
len2 = (MALLOC_S)((l2 >= 0) ? l2 : strlen(s2));
else
len2 = (MALLOC_S)0;
if (s3)
len3 = (MALLOC_S)((l3 >= 0) ? l3 : strlen(s3));
else
len3 = (MALLOC_S)0;
cl = len1 + len2 + len3;
if ((cp = (char *)malloc(cl + 1))) {
char *tp = cp;
if (s1 && len1) {
(void) strncpy(tp, s1, len1);