blob: 5b18026a54d872c1e0dc75dc691893c9be4e1b03 [file] [log] [blame]
preludedrewb89fbe42010-12-30 20:19:26 -07001/*
2 * print.c - common print support functions 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
33static char copyright[] =
34"@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n";
35static char *rcsid = "$Id: print.c,v 1.51 2010/07/29 15:59:28 abe Exp $";
36#endif
37
38
39#include "lsof.h"
40
41
42/*
43 * Local definitions, structures and function prototypes
44 */
45
46#define HCINC 64 /* host cache size increase chunk */
47#define PORTHASHBUCKETS 128 /* port hash bucket count
48 * !!MUST BE A POWER OF 2!! */
49#define PORTTABTHRESH 10 /* threshold at which we will switch
50 * from using getservbyport() to
51 * getservent() -- see lkup_port()
52 * and fill_porttab() */
53
54struct hostcache {
55 unsigned char a[MAX_AF_ADDR]; /* numeric address */
56 int af; /* address family -- e.g., AF_INET
57 * or AF_INET6 */
58 char *name; /* name */
59};
60
61struct porttab {
62 int port;
63 MALLOC_S nl; /* name length (excluding '\0') */
64 int ss; /* service name status, 0 = lookup not
65 * yet performed */
66 char *name;
67 struct porttab *next;
68};
69
70
71static struct porttab **Pth[4] = { NULL, NULL, NULL, NULL };
72 /* port hash buckets:
73 * Pth[0] for TCP service names
74 * Pth[1] for UDP service names
75 * Pth[2] for TCP portmap info
76 * Pth[3] for UDP portmap info
77 */
78#define HASHPORT(p) (((((int)(p)) * 31415) >> 3) & (PORTHASHBUCKETS - 1))
79
80# if !defined(__BIONIC__) // bionic doesn't have RPC support
81_PROTOTYPE(static void fill_portmap,(void));
82# endif /* !defined(__BIONIC__) */
83_PROTOTYPE(static void fill_porttab,(void));
84_PROTOTYPE(static char *lkup_port,(int p, int pr, int src));
85_PROTOTYPE(static char *lkup_svcnam,(int h, int p, int pr, int ss));
86_PROTOTYPE(static int printinaddr,(void));
87_PROTOTYPE(static void update_portmap,(struct porttab *pt, char *pn));
88
89
90/*
91 * endnm() - locate end of Namech
92 */
93
94char *
95endnm(sz)
96 size_t *sz; /* returned remaining size */
97{
98 register char *s;
99 register size_t tsz;
100
101 for (s = Namech, tsz = Namechl; *s; s++, tsz--)
102 ;
103 *sz = tsz;
104 return(s);
105}
106
107# if !defined(__BIONIC__) // bionic doesn't have RPC support
108
109/*
110 * fill_portmap() -- fill the RPC portmap program name table via a conversation
111 * with the portmapper
112 *
113 * The following copyright notice acknowledges that this function was adapted
114 * from getrpcportnam() of the source code of the OpenBSD netstat program.
115 */
116
117/*
118* Copyright (c) 1983, 1988, 1993
119* The Regents of the University of California. All rights reserved.
120*
121* Redistribution and use in source and binary forms, with or without
122* modification, are permitted provided that the following conditions
123* are met:
124* 1. Redistributions of source code must retain the above copyright
125* notice, this list of conditions and the following disclaimer.
126* 2. Redistributions in binary form must reproduce the above copyright
127* notice, this list of conditions and the following disclaimer in the
128* documentation and/or other materials provided with the distribution.
129* 3. All advertising materials mentioning features or use of this software
130* must display the following acknowledgement:
131* This product includes software developed by the University of
132* California, Berkeley and its contributors.
133* 4. Neither the name of the University nor the names of its contributors
134* may be used to endorse or promote products derived from this software
135* without specific prior written permission.
136*
137* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
138* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
139* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
140* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
141* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
142* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
143* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
144* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
145* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
146* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
147* SUCH DAMAGE.
148*/
149
150static void
151fill_portmap()
152{
153 char buf[128], *cp, *nm;
154 CLIENT *c;
155 int h, port, pr;
156 MALLOC_S nl;
157 struct pmaplist *p = (struct pmaplist *)NULL;
158 struct porttab *pt;
159 struct rpcent *r;
160 struct TIMEVAL_LSOF tm;
161
162#if !defined(CAN_USE_CLNT_CREATE)
163 struct hostent *he;
164 struct sockaddr_in ia;
165 int s = RPC_ANYSOCK;
166#endif /* !defined(CAN_USE_CLNT_CREATE) */
167
168/*
169 * Construct structures for communicating with the portmapper.
170 */
171
172#if !defined(CAN_USE_CLNT_CREATE)
173 zeromem(&ia, sizeof(ia));
174 ia.sin_family = AF_INET;
175 if ((he = gethostbyname("localhost")))
176 MEMMOVE((caddr_t)&ia.sin_addr, he->h_addr, he->h_length);
177 ia.sin_port = htons(PMAPPORT);
178#endif /* !defined(CAN_USE_CLNT_CREATE) */
179
180 tm.tv_sec = 60;
181 tm.tv_usec = 0;
182/*
183 * Get an RPC client handle. Then ask for a dump of the port map.
184 */
185
186#if defined(CAN_USE_CLNT_CREATE)
187 if (!(c = clnt_create("localhost", PMAPPROG, PMAPVERS, "tcp")))
188#else /* !defined(CAN_USE_CLNT_CREATE) */
189 if (!(c = clnttcp_create(&ia, PMAPPROG, PMAPVERS, &s, 0, 0)))
190#endif /* defined(CAN_USE_CLNT_CREATE) */
191
192 return;
193 if (clnt_call(c, PMAPPROC_DUMP, XDR_VOID, NULL, XDR_PMAPLIST,
194 (caddr_t)&p, tm)
195 != RPC_SUCCESS) {
196 clnt_destroy(c);
197 return;
198 }
199/*
200 * Loop through the port map dump, creating portmap table entries from TCP
201 * and UDP members.
202 */
203 for (; p; p = p->pml_next) {
204
205 /*
206 * Determine the port map entry's protocol; ignore all but TCP and UDP.
207 */
208 if (p->pml_map.pm_prot == IPPROTO_TCP)
209 pr = 2;
210 else if (p->pml_map.pm_prot == IPPROTO_UDP)
211 pr = 3;
212 else
213 continue;
214 /*
215 * See if there's already a portmap entry for this port. If there is,
216 * ignore this entry.
217 */
218 h = HASHPORT((port = (int)p->pml_map.pm_port));
219 for (pt = Pth[pr][h]; pt; pt = pt->next) {
220 if (pt->port == port)
221 break;
222 }
223 if (pt)
224 continue;
225 /*
226 * Save the registration name or number.
227 */
228 cp = (char *)NULL;
229 if ((r = (struct rpcent *)getrpcbynumber(p->pml_map.pm_prog))) {
230 if (r->r_name && strlen(r->r_name))
231 cp = r->r_name;
232 }
233 if (!cp) {
234 (void) snpf(buf, sizeof(buf), "%lu",
235 (unsigned long)p->pml_map.pm_prog);
236 cp = buf;
237 }
238 if (!strlen(cp))
239 continue;
240 /*
241 * Allocate space for the portmap name entry and copy it there.
242 */
243 if (!(nm = mkstrcpy(cp, &nl))) {
244 (void) fprintf(stderr,
245 "%s: can't allocate space for portmap entry: ", Pn);
246 safestrprt(cp, stderr, 1);
247 Exit(1);
248 }
249 if (!nl) {
250 (void) free((FREE_P *)nm);
251 continue;
252 }
253 /*
254 * Allocate and fill a porttab struct entry for the portmap table.
255 * Link it to the head of its hash bucket, and make it the new head.
256 */
257 if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
258 (void) fprintf(stderr,
259 "%s: can't allocate porttab entry for portmap: ", Pn);
260 safestrprt(nm, stderr, 1);
261 Exit(1);
262 }
263 pt->name = nm;
264 pt->nl = nl;
265 pt->port = port;
266 pt->next = Pth[pr][h];
267 pt->ss = 0;
268 Pth[pr][h] = pt;
269 }
270 clnt_destroy(c);
271}
272
273# endif /* !defined(__BIONIC__) */
274
275
276/*
277 * fill_porttab() -- fill the TCP and UDP service name port table with a
278 * getservent() scan
279 */
280
281static void
282fill_porttab()
283{
284 int h, p, pr;
285 MALLOC_S nl;
286 char *nm;
287 struct porttab *pt;
288 struct servent *se;
289
290 (void) endservent();
291/*
292 * Scan the services data base for TCP and UDP entries that have a non-null
293 * name associated with them.
294 */
295 (void) setservent(1);
296 while ((se = getservent())) {
297 if (!se->s_name || !se->s_proto)
298 continue;
299 if (strcasecmp(se->s_proto, "TCP") == 0)
300 pr = 0;
301 else if (strcasecmp(se->s_proto, "UDP") == 0)
302 pr = 1;
303 else
304 continue;
305 if (!se->s_name || !strlen(se->s_name))
306 continue;
307 p = ntohs(se->s_port);
308 /*
309 * See if a port->service entry is already cached for this port and
310 * prototcol. If it is, leave it alone.
311 */
312 h = HASHPORT(p);
313 for (pt = Pth[pr][h]; pt; pt = pt->next) {
314 if (pt->port == p)
315 break;
316 }
317 if (pt)
318 continue;
319 /*
320 * Add a new entry to the cache for this port and protocol.
321 */
322 if (!(nm = mkstrcpy(se->s_name, &nl))) {
323 (void) fprintf(stderr,
324 "%s: can't allocate %d bytes for port %d name: %s\n",
325 Pn, (int)(nl + 1), p, se->s_name);
326 Exit(1);
327 }
328 if (!nl) {
329 (void) free((FREE_P *)nm);
330 continue;
331 }
332 if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
333 (void) fprintf(stderr,
334 "%s: can't allocate porttab entry for port %d: %s\n",
335 Pn, p, se->s_name);
336 Exit(1);
337 }
338 pt->name = nm;
339 pt->nl = nl - 1;
340 pt->port = p;
341 pt->next = Pth[pr][h];
342 pt->ss = 0;
343 Pth[pr][h] = pt;
344 }
345 (void) endservent();
346}
347
348
349/*
350 * gethostnm() - get host name
351 */
352
353char *
354gethostnm(ia, af)
355 unsigned char *ia; /* Internet address */
356 int af; /* address family -- e.g., AF_INET
357 * or AF_INET6 */
358{
359 int al = MIN_AF_ADDR;
360 char hbuf[256];
361 static struct hostcache *hc = (struct hostcache *)NULL;
362 static int hcx = 0;
363 char *hn, *np;
364 struct hostent *he = (struct hostent *)NULL;
365 int i, j;
366 MALLOC_S len;
367 static int nhc = 0;
368/*
369 * Search cache.
370 */
371
372#if defined(HASIPv6)
373 if (af == AF_INET6)
374 al = MAX_AF_ADDR;
375#endif /* defined(HASIPv6) */
376
377 for (i = 0; i < hcx; i++) {
378 if (af != hc[i].af)
379 continue;
380 for (j = 0; j < al; j++) {
381 if (ia[j] != hc[i].a[j])
382 break;
383 }
384 if (j >= al)
385 return(hc[i].name);
386 }
387/*
388 * If -n has been specified, construct a numeric address. Otherwise, look up
389 * host name by address. If that fails, or if there is no name in the returned
390 * hostent structure, construct a numeric version of the address.
391 */
392 if (Fhost)
393 he = gethostbyaddr((char *)ia, al, af);
394 if (!he || !he->h_name) {
395
396#if defined(HASIPv6)
397 if (af == AF_INET6) {
398
399 /*
400 * Since IPv6 numeric addresses use `:' as a separator, enclose
401 * them in brackets.
402 */
403 hbuf[0] = '[';
404 if (!inet_ntop(af, ia, hbuf + 1, sizeof(hbuf) - 3)) {
405 (void) snpf(&hbuf[1], (sizeof(hbuf) - 1),
406 "can't format IPv6 address]");
407 } else {
408 len = strlen(hbuf);
409 (void) snpf(&hbuf[len], sizeof(hbuf) - len, "]");
410 }
411 } else
412#endif /* defined(HASIPv6) */
413
414 if (af == AF_INET)
415 (void) snpf(hbuf, sizeof(hbuf), "%u.%u.%u.%u", ia[0], ia[1],
416 ia[2], ia[3]);
417 else
418 (void) snpf(hbuf, sizeof(hbuf), "(unknown AF value: %d)", af);
419 hn = hbuf;
420 } else
421 hn = (char *)he->h_name;
422/*
423 * Allocate space for name and copy name to it.
424 */
425 if (!(np = mkstrcpy(hn, (MALLOC_S *)NULL))) {
426 (void) fprintf(stderr, "%s: no space for host name: ", Pn);
427 safestrprt(hn, stderr, 1);
428 Exit(1);
429 }
430/*
431 * Add address/name entry to cache. Allocate cache space in HCINC chunks.
432 */
433 if (hcx >= nhc) {
434 nhc += HCINC;
435 len = (MALLOC_S)(nhc * sizeof(struct hostcache));
436 if (!hc)
437 hc = (struct hostcache *)malloc(len);
438 else
439 hc = (struct hostcache *)realloc((MALLOC_P *)hc, len);
440 if (!hc) {
441 (void) fprintf(stderr, "%s: no space for host cache\n", Pn);
442 Exit(1);
443 }
444 }
445 hc[hcx].af = af;
446 for (i = 0; i < al; i++) {
447 hc[hcx].a[i] = ia[i];
448 }
449 hc[hcx++].name = np;
450 return(np);
451}
452
453
454/*
455 * lkup_port() - look up port for protocol
456 */
457
458static char *
459lkup_port(p, pr, src)
460 int p; /* port number */
461 int pr; /* protocol index: 0 = tcp, 1 = udp */
462 int src; /* port source: 0 = local
463 * 1 = foreign */
464{
465 int h, nh;
466 MALLOC_S nl;
467 char *nm, *pn;
468 static char pb[128];
469 static int pm = 0;
470 struct porttab *pt;
471/*
472 * If the hash buckets haven't been allocated, do so.
473 */
474 if (!Pth[0]) {
475 nh = FportMap ? 4 : 2;
476 for (h = 0; h < nh; h++) {
477 if (!(Pth[h] = (struct porttab **)calloc(PORTHASHBUCKETS,
478 sizeof(struct porttab *))))
479 {
480 (void) fprintf(stderr,
481 "%s: can't allocate %d bytes for %s %s hash buckets\n",
482 Pn,
483 (int)(2 * (PORTHASHBUCKETS * sizeof(struct porttab *))),
484 (h & 1) ? "UDP" : "TCP",
485 (h > 1) ? "portmap" : "port");
486 Exit(1);
487 }
488 }
489 }
490/*
491 * If we're looking up program names for portmapped ports, make sure the
492 * portmap table has been loaded.
493 */
494
495# if !defined(__BIONIC__) // bionic doesn't have RPC support
496 if (FportMap && !pm) {
497 (void) fill_portmap();
498 pm++;
499 }
500# endif /* !defined(__BIONIC__)
501
502/*
503 * Hash the port and see if its name has been cached. Look for a local
504 * port first in the portmap, if portmap searching is enabled.
505 */
506 h = HASHPORT(p);
507 if (!src && FportMap) {
508 for (pt = Pth[pr+2][h]; pt; pt = pt->next) {
509 if (pt->port != p)
510 continue;
511 if (!pt->ss) {
512 pn = Fport ? lkup_svcnam(h, p, pr, 0) : (char *)NULL;
513 if (!pn) {
514 (void) snpf(pb, sizeof(pb), "%d", p);
515 pn = pb;
516 }
517 (void) update_portmap(pt, pn);
518 }
519 return(pt->name);
520 }
521 }
522 for (pt = Pth[pr][h]; pt; pt = pt->next) {
523 if (pt->port == p)
524 return(pt->name);
525 }
526/*
527 * Search for a possible service name, unless the -P option has been specified.
528 *
529 * If there is no service name, return a %d conversion.
530 *
531 * Don't cache %d conversions; a zero port number is a %d conversion that
532 * is represented by "*".
533 */
534 pn = Fport ? lkup_svcnam(h, p, pr, 1) : (char *)NULL;
535 if (!pn || !strlen(pn)) {
536 if (p) {
537 (void) snpf(pb, sizeof(pb), "%d", p);
538 return(pb);
539 } else
540 return("*");
541 }
542/*
543 * Allocate a new porttab entry for the TCP or UDP service name.
544 */
545 if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
546 (void) fprintf(stderr,
547 "%s: can't allocate porttab entry for port %d\n", Pn, p);
548 Exit(1);
549 }
550/*
551 * Allocate space for the name; copy it to the porttab entry; and link the
552 * porttab entry to its hash bucket.
553 *
554 * Return a pointer to the name.
555 */
556 if (!(nm = mkstrcpy(pn, &nl))) {
557 (void) fprintf(stderr,
558 "%s: can't allocate space for port name: ", Pn);
559 safestrprt(pn, stderr, 1);
560 Exit(1);
561 }
562 pt->name = nm;
563 pt->nl = nl;
564 pt->port = p;
565 pt->next = Pth[pr][h];
566 pt->ss = 0;
567 Pth[pr][h] = pt;
568 return(nm);
569}
570
571
572/*
573 * lkup_svcnam() - look up service name for port
574 */
575
576static char *
577lkup_svcnam(h, p, pr, ss)
578 int h; /* porttab hash index */
579 int p; /* port number */
580 int pr; /* protocol: 0 = TCP, 1 = UDP */
581 int ss; /* search status: 1 = Pth[pr][h]
582 * already searched */
583{
584 static int fl[PORTTABTHRESH];
585 static int fln = 0;
586 static int gsbp = 0;
587 int i;
588 struct porttab *pt;
589 static int ptf = 0;
590 struct servent *se;
591/*
592 * Do nothing if -P has been specified.
593 */
594 if (!Fport)
595 return((char *)NULL);
596
597 for (;;) {
598
599 /*
600 * Search service name cache, if it hasn't already been done.
601 * Return the name of a match.
602 */
603 if (!ss) {
604 for (pt = Pth[pr][h]; pt; pt = pt->next) {
605 if (pt->port == p)
606 return(pt->name);
607 }
608 }
609/*
610 * If fill_porttab() has been called, there is no service name.
611 *
612 * Do PORTTABTHRES getservbport() calls, remembering the failures, so they
613 * won't be repeated.
614 *
615 * After PORTABTHRESH getservbyport() calls, call fill_porttab() once,
616 */
617 if (ptf)
618 break;
619 if (gsbp < PORTTABTHRESH) {
620 for (i = 0; i < fln; i++) {
621 if (fl[i] == p)
622 return((char *)NULL);
623 }
624 gsbp++;
625 if ((se = getservbyport(htons(p), pr ? "udp" : "tcp")))
626 return(se->s_name);
627 if (fln < PORTTABTHRESH)
628 fl[fln++] = p;
629 return((char *)NULL);
630 }
631 (void) fill_porttab();
632 ptf++;
633 ss = 0;
634 }
635 return((char *)NULL);
636}
637
638
639/*
640 * print_file() - print file
641 */
642
643void
644print_file()
645{
646 char buf[128];
647 char *cp = (char *)NULL;
648 dev_t dev;
649 int devs, len;
650
651 if (PrPass && !Hdr) {
652
653 /*
654 * Print the header line if this is the second pass and the
655 * header hasn't already been printed.
656 */
657 (void) printf("%-*.*s %*s", CmdColW, CmdColW, CMDTTL, PidColW,
658 PIDTTL);
659
660#if defined(HASTASKS)
661 if (TaskPrtFl)
662 (void) printf(" %*s", TidColW, TIDTTL);
663#endif /* defined(HASTASKS)
664
665#if defined(HASZONES)
666 if (Fzone)
667 (void) printf(" %-*s", ZoneColW, ZONETTL);
668#endif /* defined(HASZONES) */
669
670#if defined(HASSELINUX)
671 if (Fcntx)
672 (void) printf(" %-*s", CntxColW, CNTXTTL);
673#endif /* defined(HASSELINUX) */
674
675#if defined(HASPPID)
676 if (Fppid)
677 (void) printf(" %*s", PpidColW, PPIDTTL);
678#endif /* defined(HASPPID) */
679
680 if (Fpgid)
681 (void) printf(" %*s", PgidColW, PGIDTTL);
682 (void) printf(" %*s %*s %*s",
683 UserColW, USERTTL,
684 FdColW - 2, FDTTL,
685 TypeColW, TYPETTL);
686
687#if defined(HASFSTRUCT)
688 if (Fsv) {
689
690# if !defined(HASNOFSADDR)
691 if (Fsv & FSV_FA)
692 (void) printf(" %*s", FsColW, FSTTL);
693# endif /* !defined(HASNOFSADDR) */
694
695# if !defined(HASNOFSCOUNT)
696 if (Fsv & FSV_CT)
697 (void) printf(" %*s", FcColW, FCTTL);
698# endif /* !defined(HASNOFSCOUNT) */
699
700# if !defined(HASNOFSFLAGS)
701 if (Fsv & FSV_FG)
702 (void) printf(" %*s", FgColW, FGTTL);
703# endif /* !defined(HASNOFSFLAGS) */
704
705# if !defined(HASNOFSNADDR)
706 if (Fsv & FSV_NI)
707 (void) printf(" %*s", NiColW, NiTtl);
708# endif /* !defined(HASNOFSNADDR) */
709
710 }
711#endif /* defined(HASFSTRUCT) */
712
713 (void) printf(" %*s", DevColW, DEVTTL);
714 if (Foffset)
715 (void) printf(" %*s", SzOffColW, OFFTTL);
716 else if (Fsize)
717 (void) printf(" %*s", SzOffColW, SZTTL);
718 else
719 (void) printf(" %*s", SzOffColW, SZOFFTTL);
720 if (Fnlink)
721 (void) printf(" %*s", NlColW, NLTTL);
722 (void) printf(" %*s %s\n", NodeColW, NODETTL, NMTTL);
723 Hdr++;
724 }
725/*
726 * Size or print the command.
727 */
728 cp = (Lp->cmd && *Lp->cmd != '\0') ? Lp->cmd : "(unknown)";
729 if (!PrPass) {
730 len = safestrlen(cp, 2);
731 if (CmdLim && (len > CmdLim))
732 len = CmdLim;
733 if (len > CmdColW)
734 CmdColW = len;
735 } else
736 safestrprtn(cp, CmdColW, stdout, 2);
737/*
738 * Size or print the process ID.
739 */
740 if (!PrPass) {
741 (void) snpf(buf, sizeof(buf), "%d", Lp->pid);
742 if ((len = strlen(buf)) > PidColW)
743 PidColW = len;
744 } else
745 (void) printf(" %*d", PidColW, Lp->pid);
746
747#if defined(HASTASKS)
748/*
749 * Size or print task ID.
750 */
751 if (!PrPass) {
752 if (Lp->tid) {
753 (void) snpf(buf, sizeof(buf), "%d", Lp->tid);
754 if ((len = strlen(buf)) > TidColW)
755 TidColW = len;
756 TaskPrtFl = 1;
757 }
758 } else if (TaskPrtFl) {
759 if (Lp->tid)
760 (void) printf(" %*d", TidColW, Lp->tid);
761 else
762 (void) printf(" %*s", TidColW, "");
763 }
764#endif /* defined(HASTASKS) */
765
766#if defined(HASZONES)
767/*
768 * Size or print the zone.
769 */
770 if (Fzone) {
771 if (!PrPass) {
772 if (Lp->zn) {
773 if ((len = strlen(Lp->zn)) > ZoneColW)
774 ZoneColW = len;
775 }
776 } else
777 (void) printf(" %-*s", ZoneColW, Lp->zn ? Lp->zn : "");
778 }
779#endif /* defined(HASZONES) */
780
781#if defined(HASSELINUX)
782/*
783 * Size or print the context.
784 */
785 if (Fcntx) {
786 if (!PrPass) {
787 if (Lp->cntx) {
788 if ((len = strlen(Lp->cntx)) > CntxColW)
789 CntxColW = len;
790 }
791 } else
792 (void) printf(" %-*s", CntxColW, Lp->cntx ? Lp->cntx : "");
793 }
794#endif /* defined(HASSELINUX) */
795
796#if defined(HASPPID)
797 if (Fppid) {
798
799 /*
800 * Size or print the parent process ID.
801 */
802 if (!PrPass) {
803 (void) snpf(buf, sizeof(buf), "%d", Lp->ppid);
804 if ((len = strlen(buf)) > PpidColW)
805 PpidColW = len;
806 } else
807 (void) printf(" %*d", PpidColW, Lp->ppid);
808 }
809#endif /* defined(HASPPID) */
810
811 if (Fpgid) {
812
813 /*
814 * Size or print the process group ID.
815 */
816 if (!PrPass) {
817 (void) snpf(buf, sizeof(buf), "%d", Lp->pgid);
818 if ((len = strlen(buf)) > PgidColW)
819 PgidColW = len;
820 } else
821 (void) printf(" %*d", PgidColW, Lp->pgid);
822 }
823/*
824 * Size or print the user ID or login name.
825 */
826 if (!PrPass) {
827 if ((len = strlen(printuid((UID_ARG)Lp->uid, NULL))) > UserColW)
828 UserColW = len;
829 } else
830 (void) printf(" %*.*s", UserColW, UserColW,
831 printuid((UID_ARG)Lp->uid, NULL));
832/*
833 * Size or print the file descriptor, access mode and lock status.
834 */
835 if (!PrPass) {
836 (void) snpf(buf, sizeof(buf), "%s%c%c",
837 Lf->fd,
838 (Lf->lock == ' ') ? Lf->access
839 : (Lf->access == ' ') ? '-'
840 : Lf->access,
841 Lf->lock);
842 if ((len = strlen(buf)) > FdColW)
843 FdColW = len;
844 } else
845 (void) printf(" %*.*s%c%c", FdColW - 2, FdColW - 2, Lf->fd,
846 (Lf->lock == ' ') ? Lf->access
847 : (Lf->access == ' ') ? '-'
848 : Lf->access,
849 Lf->lock);
850/*
851 * Size or print the type.
852 */
853 if (!PrPass) {
854 if ((len = strlen(Lf->type)) > TypeColW)
855 TypeColW = len;
856 } else
857 (void) printf(" %*.*s", TypeColW, TypeColW, Lf->type);
858
859#if defined(HASFSTRUCT)
860/*
861 * Size or print the file structure address, file usage count, and node
862 * ID (address).
863 */
864
865 if (Fsv) {
866
867# if !defined(HASNOFSADDR)
868 if (Fsv & FSV_FA) {
869 cp = (Lf->fsv & FSV_FA) ? print_kptr(Lf->fsa, buf, sizeof(buf))
870 : "";
871 if (!PrPass) {
872 if ((len = strlen(cp)) > FsColW)
873 FsColW = len;
874 } else
875 (void) printf(" %*.*s", FsColW, FsColW, cp);
876
877 }
878# endif /* !defined(HASNOFSADDR) */
879
880# if !defined(HASNOFSCOUNT)
881 if (Fsv & FSV_CT) {
882 if (Lf->fsv & FSV_CT) {
883 (void) snpf(buf, sizeof(buf), "%ld", Lf->fct);
884 cp = buf;
885 } else
886 cp = "";
887 if (!PrPass) {
888 if ((len = strlen(cp)) > FcColW)
889 FcColW = len;
890 } else
891 (void) printf(" %*.*s", FcColW, FcColW, cp);
892 }
893# endif /* !defined(HASNOFSCOUNT) */
894
895# if !defined(HASNOFSFLAGS)
896 if (Fsv & FSV_FG) {
897 if ((Lf->fsv & FSV_FG) && (FsvFlagX || Lf->ffg || Lf->pof))
898 cp = print_fflags(Lf->ffg, Lf->pof);
899 else
900 cp = "";
901 if (!PrPass) {
902 if ((len = strlen(cp)) > FgColW)
903 FgColW = len;
904 } else
905 (void) printf(" %*.*s", FgColW, FgColW, cp);
906 }
907# endif /* !defined(HASNOFSFLAGS) */
908
909# if !defined(HASNOFSNADDR)
910 if (Fsv & FSV_NI) {
911 cp = (Lf->fsv & FSV_NI) ? print_kptr(Lf->fna, buf, sizeof(buf))
912 : "";
913 if (!PrPass) {
914 if ((len = strlen(cp)) > NiColW)
915 NiColW = len;
916 } else
917 (void) printf(" %*.*s", NiColW, NiColW, cp);
918 }
919# endif /* !defined(HASNOFSNADDR) */
920
921 }
922#endif /* defined(HASFSTRUCT) */
923
924/*
925 * Size or print the device information.
926 */
927
928 if (Lf->rdev_def) {
929 dev = Lf->rdev;
930 devs = 1;
931 } else if (Lf->dev_def) {
932 dev = Lf->dev;
933 devs = 1;
934 } else
935 devs = 0;
936 if (devs) {
937
938#if defined(HASPRINTDEV)
939 cp = HASPRINTDEV(Lf, &dev);
940#else /* !defined(HASPRINTDEV) */
941 (void) snpf(buf, sizeof(buf), "%u,%u", GET_MAJ_DEV(dev),
942 GET_MIN_DEV(dev));
943 cp = buf;
944#endif /* defined(HASPRINTDEV) */
945
946 }
947
948 if (!PrPass) {
949 if (devs)
950 len = strlen(cp);
951 else if (Lf->dev_ch)
952 len = strlen(Lf->dev_ch);
953 else
954 len = 0;
955 if (len > DevColW)
956 DevColW = len;
957 } else {
958 if (devs)
959 (void) printf(" %*.*s", DevColW, DevColW, cp);
960 else {
961 if (Lf->dev_ch)
962 (void) printf(" %*.*s", DevColW, DevColW, Lf->dev_ch);
963 else
964 (void) printf(" %*.*s", DevColW, DevColW, "");
965 }
966 }
967/*
968 * Size or print the size or offset.
969 */
970 if (!PrPass) {
971 if (Lf->sz_def) {
972
973#if defined(HASPRINTSZ)
974 cp = HASPRINTSZ(Lf);
975#else /* !defined(HASPRINTSZ) */
976 (void) snpf(buf, sizeof(buf), SzOffFmt_d, Lf->sz);
977 cp = buf;
978#endif /* defined(HASPRINTSZ) */
979
980 len = strlen(cp);
981 } else if (Lf->off_def) {
982
983#if defined(HASPRINTOFF)
984 cp = HASPRINTOFF(Lf, 0);
985#else /* !defined(HASPRINTOFF) */
986 (void) snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
987 cp = buf;
988#endif /* defined(HASPRINTOFF) */
989
990 len = strlen(cp);
991 if (OffDecDig && len > (OffDecDig + 2)) {
992
993#if defined(HASPRINTOFF)
994 cp = HASPRINTOFF(Lf, 1);
995#else /* !defined(HASPRINTOFF) */
996 (void) snpf(buf, sizeof(buf), SzOffFmt_x, Lf->off);
997 cp = buf;
998#endif /* defined(HASPRINTOFF) */
999
1000 len = strlen(cp);
1001 }
1002 } else
1003 len = 0;
1004 if (len > SzOffColW)
1005 SzOffColW = len;
1006 } else {
1007 putchar(' ');
1008 if (Lf->sz_def)
1009
1010#if defined(HASPRINTSZ)
1011 (void) printf("%*.*s", SzOffColW, SzOffColW, HASPRINTSZ(Lf));
1012#else /* !defined(HASPRINTSZ) */
1013 (void) printf(SzOffFmt_dv, SzOffColW, Lf->sz);
1014#endif /* defined(HASPRINTSZ) */
1015
1016 else if (Lf->off_def) {
1017
1018#if defined(HASPRINTOFF)
1019 cp = HASPRINTOFF(Lf, 0);
1020#else /* !defined(HASPRINTOFF) */
1021 (void) snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
1022 cp = buf;
1023#endif /* defined(HASPRINTOFF) */
1024
1025 if (OffDecDig && (int)strlen(cp) > (OffDecDig + 2)) {
1026
1027#if defined(HASPRINTOFF)
1028 cp = HASPRINTOFF(Lf, 1);
1029#else /* !defined(HASPRINTOFF) */
1030 (void) snpf(buf, sizeof(buf), SzOffFmt_x, Lf->off);
1031 cp = buf;
1032#endif /* defined(HASPRINTOFF) */
1033
1034 }
1035 (void) printf("%*.*s", SzOffColW, SzOffColW, cp);
1036 } else
1037 (void) printf("%*.*s", SzOffColW, SzOffColW, "");
1038 }
1039/*
1040 * Size or print the link count.
1041 */
1042 if (Fnlink) {
1043 if (Lf->nlink_def) {
1044 (void) snpf(buf, sizeof(buf), " %ld", Lf->nlink);
1045 cp = buf;
1046 } else
1047 cp = "";
1048 if (!PrPass) {
1049 if ((len = strlen(cp)) > NlColW)
1050 NlColW = len;
1051 } else
1052 (void) printf(" %*s", NlColW, cp);
1053 }
1054/*
1055 * Size or print the inode information.
1056 */
1057 switch (Lf->inp_ty) {
1058 case 1:
1059
1060#if defined(HASPRINTINO)
1061 cp = HASPRINTINO(Lf);
1062#else /* !defined(HASPRINTINO) */
1063 (void) snpf(buf, sizeof(buf), InodeFmt_d, Lf->inode);
1064 cp = buf;
1065#endif /* defined(HASPRINTINO) */
1066
1067 break;
1068 case 2:
1069 if (Lf->iproto[0])
1070 cp = Lf->iproto;
1071 else
1072 cp = "";
1073 break;
1074 case 3:
1075 (void) snpf(buf, sizeof(buf), InodeFmt_x, Lf->inode);
1076 cp = buf;
1077 break;
1078 default:
1079 cp = "";
1080 }
1081 if (!PrPass) {
1082 if ((len = strlen(cp)) > NodeColW)
1083 NodeColW = len;
1084 } else {
1085 (void) printf(" %*.*s", NodeColW, NodeColW, cp);
1086 }
1087/*
1088 * If this is the second pass, print the name column. (It doesn't need
1089 * to be sized.)
1090 */
1091 if (PrPass) {
1092 putchar(' ');
1093
1094#if defined(HASPRINTNM)
1095 HASPRINTNM(Lf);
1096#else /* !defined(HASPRINTNM) */
1097 printname(1);
1098#endif /* defined(HASPRINTNM) */
1099
1100 }
1101}
1102
1103
1104/*
1105 * printinaddr() - print Internet addresses
1106 */
1107
1108static int
1109printinaddr()
1110{
1111 int i, len, src;
1112 char *host, *port;
1113 int nl = Namechl - 1;
1114 char *np = Namech;
1115 char pbuf[32];
1116/*
1117 * Process local network address first. If there's a foreign address,
1118 * separate it from the local address with "->".
1119 */
1120 for (i = 0, *np = '\0'; i < 2; i++) {
1121 if (!Lf->li[i].af)
1122 continue;
1123 host = port = (char *)NULL;
1124 if (i) {
1125
1126 /*
1127 * If this is the foreign address, insert the separator.
1128 */
1129 if (nl < 2)
1130
1131addr_too_long:
1132
1133 {
1134 (void) snpf(Namech, Namechl,
1135 "network addresses too long");
1136 return(1);
1137 }
1138 (void) snpf(np, nl, "->");
1139 np += 2;
1140 nl -= 2;
1141 }
1142 /*
1143 * Convert the address to a host name.
1144 */
1145
1146#if defined(HASIPv6)
1147 if ((Lf->li[i].af == AF_INET6
1148 && IN6_IS_ADDR_UNSPECIFIED(&Lf->li[i].ia.a6))
1149 || (Lf->li[i].af == AF_INET
1150 && Lf->li[i].ia.a4.s_addr == INADDR_ANY))
1151 host ="*";
1152 else
1153 host = gethostnm((unsigned char *)&Lf->li[i].ia, Lf->li[i].af);
1154#else /* !defined(HASIPv6) */
1155 if (Lf->li[i].ia.a4.s_addr == INADDR_ANY)
1156 host ="*";
1157 else
1158 host = gethostnm((unsigned char *)&Lf->li[i].ia, Lf->li[i].af);
1159#endif /* defined(HASIPv6) */
1160
1161 /*
1162 * Process the port number.
1163 */
1164 if (Lf->li[i].p > 0) {
1165 if (Fport || FportMap) {
1166
1167 /*
1168 * If converting port numbers to service names, or looking
1169 * up portmap program names and numbers, do so by protocol.
1170 *
1171 * Identify the port source as local if: 1) it comes from the
1172 * local entry (0) of the file's Internet address array; or
1173 * 2) it comes from the foreign entry (1), and the foreign
1174 * Internet address matches the local one; or 3) it is the
1175 * loopback address 127.0.0.1. (Test 2 may not always work
1176 * -- e.g., on hosts with multiple interfaces.)
1177 */
1178 if ((src = i) && FportMap) {
1179
1180#if defined(HASIPv6)
1181 if (Lf->li[0].af == AF_INET6) {
1182 if (IN6_IS_ADDR_LOOPBACK(&Lf->li[i].ia.a6)
1183 || IN6_ARE_ADDR_EQUAL(&Lf->li[0].ia.a6,
1184 &Lf->li[1].ia.a6)
1185 )
1186 src = 0;
1187 } else
1188#endif /* defined(HASIPv6) */
1189
1190 if (Lf->li[0].af == AF_INET) {
1191 if (Lf->li[i].ia.a4.s_addr == htonl(INADDR_LOOPBACK)
1192 || Lf->li[0].ia.a4.s_addr == Lf->li[1].ia.a4.s_addr
1193 )
1194 src = 0;
1195 }
1196 }
1197 if (strcasecmp(Lf->iproto, "TCP") == 0)
1198 port = lkup_port(Lf->li[i].p, 0, src);
1199 else if (strcasecmp(Lf->iproto, "UDP") == 0)
1200 port = lkup_port(Lf->li[i].p, 1, src);
1201 }
1202 if (!port) {
1203 (void) snpf(pbuf, sizeof(pbuf), "%d", Lf->li[i].p);
1204 port = pbuf;
1205 }
1206 } else if (Lf->li[i].p == 0)
1207 port = "*";
1208 /*
1209 * Enter the host name.
1210 */
1211 if (host) {
1212 if ((len = strlen(host)) > nl)
1213 goto addr_too_long;
1214 if (len) {
1215 (void) snpf(np, nl, "%s", host);
1216 np += len;
1217 nl -= len;
1218 }
1219 }
1220 /*
1221 * Enter the port number, preceded by a colon.
1222 */
1223 if (port) {
1224 if (((len = strlen(port)) + 1) >= nl)
1225 goto addr_too_long;
1226 (void) snpf(np, nl, ":%s", port);
1227 np += len + 1;
1228 nl -= len - 1;
1229 }
1230 }
1231 if (Namech[0]) {
1232 safestrprt(Namech, stdout, 0);
1233 return(1);
1234 }
1235 return(0);
1236}
1237
1238
1239/*
1240 * print_init() - initialize for printing
1241 */
1242
1243void
1244print_init()
1245{
1246 PrPass = (Ffield || Fterse) ? 1 : 0;
1247 CmdColW = strlen(CMDTTL);
1248 DevColW = strlen(DEVTTL);
1249 FdColW = strlen(FDTTL);
1250 if (Fnlink)
1251 NlColW = strlen(NLTTL);
1252 NmColW = strlen(NMTTL);
1253 NodeColW = strlen(NODETTL);
1254 PgidColW = strlen(PGIDTTL);
1255 PidColW = strlen(PIDTTL);
1256 PpidColW = strlen(PPIDTTL);
1257 if (Fsize)
1258 SzOffColW = strlen(SZTTL);
1259 else if (Foffset)
1260 SzOffColW = strlen(OFFTTL);
1261 else
1262 SzOffColW = strlen(SZOFFTTL);
1263 TaskPrtFl = 0;
1264
1265#if defined(HASTASKS)
1266 TidColW = strlen(TIDTTL);
1267#endif /* defined(HASTASKS) */
1268
1269 TypeColW = strlen(TYPETTL);
1270 UserColW = strlen(USERTTL);
1271
1272#if defined(HASFSTRUCT)
1273
1274# if !defined(HASNOFSADDR)
1275 FsColW = strlen(FSTTL);
1276# endif /* !defined(HASNOFSADDR) */
1277
1278# if !defined(HASNOFSCOUNT)
1279 FcColW = strlen(FCTTL);
1280# endif /* !defined(HASNOFSCOUNT) */
1281
1282# if !defined(HASNOFSFLAGS)
1283 FgColW = strlen(FGTTL);
1284# endif /* !defined(HASNOFSFLAGS) */
1285
1286# if !defined(HASNOFSNADDR)
1287 NiColW = strlen(NiTtl);
1288# endif /* !defined(HASNOFSNADDR) */
1289#endif /* defined(HASFSTRUCT) */
1290
1291#if defined(HASSELINUX)
1292 if (Fcntx)
1293 CntxColW = strlen(CNTXTTL);
1294#endif /* defined(HASSELINUX) */
1295
1296#if defined(HASZONES)
1297 if (Fzone)
1298 ZoneColW = strlen(ZONETTL);
1299#endif /* defined(HASZONES) */
1300
1301}
1302
1303
1304#if !defined(HASPRIVPRIPP)
1305/*
1306 * printiproto() - print Internet protocol name
1307 */
1308
1309void
1310printiproto(p)
1311 int p; /* protocol number */
1312{
1313 int i;
1314 static int m = -1;
1315 char *s;
1316
1317 switch (p) {
1318
1319#if defined(IPPROTO_TCP)
1320 case IPPROTO_TCP:
1321 s = "TCP";
1322 break;
1323#endif /* defined(IPPROTO_TCP) */
1324
1325#if defined(IPPROTO_UDP)
1326 case IPPROTO_UDP:
1327 s = "UDP";
1328 break;
1329#endif /* defined(IPPROTO_UDP) */
1330
1331#if defined(IPPROTO_IP)
1332# if !defined(IPPROTO_HOPOPTS) || IPPROTO_IP!=IPPROTO_HOPOPTS
1333 case IPPROTO_IP:
1334 s = "IP";
1335 break;
1336# endif /* !defined(IPPROTO_HOPOPTS) || IPPROTO_IP!=IPPROTO_HOPOPTS */
1337#endif /* defined(IPPROTO_IP) */
1338
1339#if defined(IPPROTO_ICMP)
1340 case IPPROTO_ICMP:
1341 s = "ICMP";
1342 break;
1343#endif /* defined(IPPROTO_ICMP) */
1344
1345#if defined(IPPROTO_ICMPV6)
1346 case IPPROTO_ICMPV6:
1347 s = "ICMPV6";
1348 break;
1349#endif /* defined(IPPROTO_ICMPV6) */
1350
1351#if defined(IPPROTO_IGMP)
1352 case IPPROTO_IGMP:
1353 s = "IGMP";
1354 break;
1355#endif /* defined(IPPROTO_IGMP) */
1356
1357#if defined(IPPROTO_GGP)
1358 case IPPROTO_GGP:
1359 s = "GGP";
1360 break;
1361#endif /* defined(IPPROTO_GGP) */
1362
1363#if defined(IPPROTO_EGP)
1364 case IPPROTO_EGP:
1365 s = "EGP";
1366 break;
1367#endif /* defined(IPPROTO_EGP) */
1368
1369#if defined(IPPROTO_PUP)
1370 case IPPROTO_PUP:
1371 s = "PUP";
1372 break;
1373#endif /* defined(IPPROTO_PUP) */
1374
1375#if defined(IPPROTO_IDP)
1376 case IPPROTO_IDP:
1377 s = "IDP";
1378 break;
1379#endif /* defined(IPPROTO_IDP) */
1380
1381#if defined(IPPROTO_ND)
1382 case IPPROTO_ND:
1383 s = "ND";
1384 break;
1385#endif /* defined(IPPROTO_ND) */
1386
1387#if defined(IPPROTO_RAW)
1388 case IPPROTO_RAW:
1389 s = "RAW";
1390 break;
1391#endif /* defined(IPPROTO_RAW) */
1392
1393#if defined(IPPROTO_HELLO)
1394 case IPPROTO_HELLO:
1395 s = "HELLO";
1396 break;
1397#endif /* defined(IPPROTO_HELLO) */
1398
1399#if defined(IPPROTO_PXP)
1400 case IPPROTO_PXP:
1401 s = "PXP";
1402 break;
1403#endif /* defined(IPPROTO_PXP) */
1404
1405#if defined(IPPROTO_RAWIP)
1406 case IPPROTO_RAWIP:
1407 s = "RAWIP";
1408 break;
1409#endif /* defined(IPPROTO_RAWIP) */
1410
1411#if defined(IPPROTO_RAWIF)
1412 case IPPROTO_RAWIF:
1413 s = "RAWIF";
1414 break;
1415#endif /* defined(IPPROTO_RAWIF) */
1416
1417#if defined(IPPROTO_HOPOPTS)
1418 case IPPROTO_HOPOPTS:
1419 s = "HOPOPTS";
1420 break;
1421#endif /* defined(IPPROTO_HOPOPTS) */
1422
1423#if defined(IPPROTO_IPIP)
1424 case IPPROTO_IPIP:
1425 s = "IPIP";
1426 break;
1427#endif /* defined(IPPROTO_IPIP) */
1428
1429#if defined(IPPROTO_ST)
1430 case IPPROTO_ST:
1431 s = "ST";
1432 break;
1433#endif /* defined(IPPROTO_ST) */
1434
1435#if defined(IPPROTO_PIGP)
1436 case IPPROTO_PIGP:
1437 s = "PIGP";
1438 break;
1439#endif /* defined(IPPROTO_PIGP) */
1440
1441#if defined(IPPROTO_RCCMON)
1442 case IPPROTO_RCCMON:
1443 s = "RCCMON";
1444 break;
1445#endif /* defined(IPPROTO_RCCMON) */
1446
1447#if defined(IPPROTO_NVPII)
1448 case IPPROTO_NVPII:
1449 s = "NVPII";
1450 break;
1451#endif /* defined(IPPROTO_NVPII) */
1452
1453#if defined(IPPROTO_ARGUS)
1454 case IPPROTO_ARGUS:
1455 s = "ARGUS";
1456 break;
1457#endif /* defined(IPPROTO_ARGUS) */
1458
1459#if defined(IPPROTO_EMCON)
1460 case IPPROTO_EMCON:
1461 s = "EMCON";
1462 break;
1463#endif /* defined(IPPROTO_EMCON) */
1464
1465#if defined(IPPROTO_XNET)
1466 case IPPROTO_XNET:
1467 s = "XNET";
1468 break;
1469#endif /* defined(IPPROTO_XNET) */
1470
1471#if defined(IPPROTO_CHAOS)
1472 case IPPROTO_CHAOS:
1473 s = "CHAOS";
1474 break;
1475#endif /* defined(IPPROTO_CHAOS) */
1476
1477#if defined(IPPROTO_MUX)
1478 case IPPROTO_MUX:
1479 s = "MUX";
1480 break;
1481#endif /* defined(IPPROTO_MUX) */
1482
1483#if defined(IPPROTO_MEAS)
1484 case IPPROTO_MEAS:
1485 s = "MEAS";
1486 break;
1487#endif /* defined(IPPROTO_MEAS) */
1488
1489#if defined(IPPROTO_HMP)
1490 case IPPROTO_HMP:
1491 s = "HMP";
1492 break;
1493#endif /* defined(IPPROTO_HMP) */
1494
1495#if defined(IPPROTO_PRM)
1496 case IPPROTO_PRM:
1497 s = "PRM";
1498 break;
1499#endif /* defined(IPPROTO_PRM) */
1500
1501#if defined(IPPROTO_TRUNK1)
1502 case IPPROTO_TRUNK1:
1503 s = "TRUNK1";
1504 break;
1505#endif /* defined(IPPROTO_TRUNK1) */
1506
1507#if defined(IPPROTO_TRUNK2)
1508 case IPPROTO_TRUNK2:
1509 s = "TRUNK2";
1510 break;
1511#endif /* defined(IPPROTO_TRUNK2) */
1512
1513#if defined(IPPROTO_LEAF1)
1514 case IPPROTO_LEAF1:
1515 s = "LEAF1";
1516 break;
1517#endif /* defined(IPPROTO_LEAF1) */
1518
1519#if defined(IPPROTO_LEAF2)
1520 case IPPROTO_LEAF2:
1521 s = "LEAF2";
1522 break;
1523#endif /* defined(IPPROTO_LEAF2) */
1524
1525#if defined(IPPROTO_RDP)
1526 case IPPROTO_RDP:
1527 s = "RDP";
1528 break;
1529#endif /* defined(IPPROTO_RDP) */
1530
1531#if defined(IPPROTO_IRTP)
1532 case IPPROTO_IRTP:
1533 s = "IRTP";
1534 break;
1535#endif /* defined(IPPROTO_IRTP) */
1536
1537#if defined(IPPROTO_TP)
1538 case IPPROTO_TP:
1539 s = "TP";
1540 break;
1541#endif /* defined(IPPROTO_TP) */
1542
1543#if defined(IPPROTO_BLT)
1544 case IPPROTO_BLT:
1545 s = "BLT";
1546 break;
1547#endif /* defined(IPPROTO_BLT) */
1548
1549#if defined(IPPROTO_NSP)
1550 case IPPROTO_NSP:
1551 s = "NSP";
1552 break;
1553#endif /* defined(IPPROTO_NSP) */
1554
1555#if defined(IPPROTO_INP)
1556 case IPPROTO_INP:
1557 s = "INP";
1558 break;
1559#endif /* defined(IPPROTO_INP) */
1560
1561#if defined(IPPROTO_SEP)
1562 case IPPROTO_SEP:
1563 s = "SEP";
1564 break;
1565#endif /* defined(IPPROTO_SEP) */
1566
1567#if defined(IPPROTO_3PC)
1568 case IPPROTO_3PC:
1569 s = "3PC";
1570 break;
1571#endif /* defined(IPPROTO_3PC) */
1572
1573#if defined(IPPROTO_IDPR)
1574 case IPPROTO_IDPR:
1575 s = "IDPR";
1576 break;
1577#endif /* defined(IPPROTO_IDPR) */
1578
1579#if defined(IPPROTO_XTP)
1580 case IPPROTO_XTP:
1581 s = "XTP";
1582 break;
1583#endif /* defined(IPPROTO_XTP) */
1584
1585#if defined(IPPROTO_DDP)
1586 case IPPROTO_DDP:
1587 s = "DDP";
1588 break;
1589#endif /* defined(IPPROTO_DDP) */
1590
1591#if defined(IPPROTO_CMTP)
1592 case IPPROTO_CMTP:
1593 s = "CMTP";
1594 break;
1595#endif /* defined(IPPROTO_CMTP) */
1596
1597#if defined(IPPROTO_TPXX)
1598 case IPPROTO_TPXX:
1599 s = "TPXX";
1600 break;
1601#endif /* defined(IPPROTO_TPXX) */
1602
1603#if defined(IPPROTO_IL)
1604 case IPPROTO_IL:
1605 s = "IL";
1606 break;
1607#endif /* defined(IPPROTO_IL) */
1608
1609#if defined(IPPROTO_IPV6)
1610 case IPPROTO_IPV6:
1611 s = "IPV6";
1612 break;
1613#endif /* defined(IPPROTO_IPV6) */
1614
1615#if defined(IPPROTO_SDRP)
1616 case IPPROTO_SDRP:
1617 s = "SDRP";
1618 break;
1619#endif /* defined(IPPROTO_SDRP) */
1620
1621#if defined(IPPROTO_ROUTING)
1622 case IPPROTO_ROUTING:
1623 s = "ROUTING";
1624 break;
1625#endif /* defined(IPPROTO_ROUTING) */
1626
1627#if defined(IPPROTO_FRAGMENT)
1628 case IPPROTO_FRAGMENT:
1629 s = "FRAGMNT";
1630 break;
1631#endif /* defined(IPPROTO_FRAGMENT) */
1632
1633#if defined(IPPROTO_IDRP)
1634 case IPPROTO_IDRP:
1635 s = "IDRP";
1636 break;
1637#endif /* defined(IPPROTO_IDRP) */
1638
1639#if defined(IPPROTO_RSVP)
1640 case IPPROTO_RSVP:
1641 s = "RSVP";
1642 break;
1643#endif /* defined(IPPROTO_RSVP) */
1644
1645#if defined(IPPROTO_GRE)
1646 case IPPROTO_GRE:
1647 s = "GRE";
1648 break;
1649#endif /* defined(IPPROTO_GRE) */
1650
1651#if defined(IPPROTO_MHRP)
1652 case IPPROTO_MHRP:
1653 s = "MHRP";
1654 break;
1655#endif /* defined(IPPROTO_MHRP) */
1656
1657#if defined(IPPROTO_BHA)
1658 case IPPROTO_BHA:
1659 s = "BHA";
1660 break;
1661#endif /* defined(IPPROTO_BHA) */
1662
1663#if defined(IPPROTO_ESP)
1664 case IPPROTO_ESP:
1665 s = "ESP";
1666 break;
1667#endif /* defined(IPPROTO_ESP) */
1668
1669#if defined(IPPROTO_AH)
1670 case IPPROTO_AH:
1671 s = "AH";
1672 break;
1673#endif /* defined(IPPROTO_AH) */
1674
1675#if defined(IPPROTO_INLSP)
1676 case IPPROTO_INLSP:
1677 s = "INLSP";
1678 break;
1679#endif /* defined(IPPROTO_INLSP) */
1680
1681#if defined(IPPROTO_SWIPE)
1682 case IPPROTO_SWIPE:
1683 s = "SWIPE";
1684 break;
1685#endif /* defined(IPPROTO_SWIPE) */
1686
1687#if defined(IPPROTO_NHRP)
1688 case IPPROTO_NHRP:
1689 s = "NHRP";
1690 break;
1691#endif /* defined(IPPROTO_NHRP) */
1692
1693#if defined(IPPROTO_NONE)
1694 case IPPROTO_NONE:
1695 s = "NONE";
1696 break;
1697#endif /* defined(IPPROTO_NONE) */
1698
1699#if defined(IPPROTO_DSTOPTS)
1700 case IPPROTO_DSTOPTS:
1701 s = "DSTOPTS";
1702 break;
1703#endif /* defined(IPPROTO_DSTOPTS) */
1704
1705#if defined(IPPROTO_AHIP)
1706 case IPPROTO_AHIP:
1707 s = "AHIP";
1708 break;
1709#endif /* defined(IPPROTO_AHIP) */
1710
1711#if defined(IPPROTO_CFTP)
1712 case IPPROTO_CFTP:
1713 s = "CFTP";
1714 break;
1715#endif /* defined(IPPROTO_CFTP) */
1716
1717#if defined(IPPROTO_SATEXPAK)
1718 case IPPROTO_SATEXPAK:
1719 s = "SATEXPK";
1720 break;
1721#endif /* defined(IPPROTO_SATEXPAK) */
1722
1723#if defined(IPPROTO_KRYPTOLAN)
1724 case IPPROTO_KRYPTOLAN:
1725 s = "KRYPTOL";
1726 break;
1727#endif /* defined(IPPROTO_KRYPTOLAN) */
1728
1729#if defined(IPPROTO_RVD)
1730 case IPPROTO_RVD:
1731 s = "RVD";
1732 break;
1733#endif /* defined(IPPROTO_RVD) */
1734
1735#if defined(IPPROTO_IPPC)
1736 case IPPROTO_IPPC:
1737 s = "IPPC";
1738 break;
1739#endif /* defined(IPPROTO_IPPC) */
1740
1741#if defined(IPPROTO_ADFS)
1742 case IPPROTO_ADFS:
1743 s = "ADFS";
1744 break;
1745#endif /* defined(IPPROTO_ADFS) */
1746
1747#if defined(IPPROTO_SATMON)
1748 case IPPROTO_SATMON:
1749 s = "SATMON";
1750 break;
1751#endif /* defined(IPPROTO_SATMON) */
1752
1753#if defined(IPPROTO_VISA)
1754 case IPPROTO_VISA:
1755 s = "VISA";
1756 break;
1757#endif /* defined(IPPROTO_VISA) */
1758
1759#if defined(IPPROTO_IPCV)
1760 case IPPROTO_IPCV:
1761 s = "IPCV";
1762 break;
1763#endif /* defined(IPPROTO_IPCV) */
1764
1765#if defined(IPPROTO_CPNX)
1766 case IPPROTO_CPNX:
1767 s = "CPNX";
1768 break;
1769#endif /* defined(IPPROTO_CPNX) */
1770
1771#if defined(IPPROTO_CPHB)
1772 case IPPROTO_CPHB:
1773 s = "CPHB";
1774 break;
1775#endif /* defined(IPPROTO_CPHB) */
1776
1777#if defined(IPPROTO_WSN)
1778 case IPPROTO_WSN:
1779 s = "WSN";
1780 break;
1781#endif /* defined(IPPROTO_WSN) */
1782
1783#if defined(IPPROTO_PVP)
1784 case IPPROTO_PVP:
1785 s = "PVP";
1786 break;
1787#endif /* defined(IPPROTO_PVP) */
1788
1789#if defined(IPPROTO_BRSATMON)
1790 case IPPROTO_BRSATMON:
1791 s = "BRSATMN";
1792 break;
1793#endif /* defined(IPPROTO_BRSATMON) */
1794
1795#if defined(IPPROTO_WBMON)
1796 case IPPROTO_WBMON:
1797 s = "WBMON";
1798 break;
1799#endif /* defined(IPPROTO_WBMON) */
1800
1801#if defined(IPPROTO_WBEXPAK)
1802 case IPPROTO_WBEXPAK:
1803 s = "WBEXPAK";
1804 break;
1805#endif /* defined(IPPROTO_WBEXPAK) */
1806
1807#if defined(IPPROTO_EON)
1808 case IPPROTO_EON:
1809 s = "EON";
1810 break;
1811#endif /* defined(IPPROTO_EON) */
1812
1813#if defined(IPPROTO_VMTP)
1814 case IPPROTO_VMTP:
1815 s = "VMTP";
1816 break;
1817#endif /* defined(IPPROTO_VMTP) */
1818
1819#if defined(IPPROTO_SVMTP)
1820 case IPPROTO_SVMTP:
1821 s = "SVMTP";
1822 break;
1823#endif /* defined(IPPROTO_SVMTP) */
1824
1825#if defined(IPPROTO_VINES)
1826 case IPPROTO_VINES:
1827 s = "VINES";
1828 break;
1829#endif /* defined(IPPROTO_VINES) */
1830
1831#if defined(IPPROTO_TTP)
1832 case IPPROTO_TTP:
1833 s = "TTP";
1834 break;
1835#endif /* defined(IPPROTO_TTP) */
1836
1837#if defined(IPPROTO_IGP)
1838 case IPPROTO_IGP:
1839 s = "IGP";
1840 break;
1841#endif /* defined(IPPROTO_IGP) */
1842
1843#if defined(IPPROTO_DGP)
1844 case IPPROTO_DGP:
1845 s = "DGP";
1846 break;
1847#endif /* defined(IPPROTO_DGP) */
1848
1849#if defined(IPPROTO_TCF)
1850 case IPPROTO_TCF:
1851 s = "TCF";
1852 break;
1853#endif /* defined(IPPROTO_TCF) */
1854
1855#if defined(IPPROTO_IGRP)
1856 case IPPROTO_IGRP:
1857 s = "IGRP";
1858 break;
1859#endif /* defined(IPPROTO_IGRP) */
1860
1861#if defined(IPPROTO_OSPFIGP)
1862 case IPPROTO_OSPFIGP:
1863 s = "OSPFIGP";
1864 break;
1865#endif /* defined(IPPROTO_OSPFIGP) */
1866
1867#if defined(IPPROTO_SRPC)
1868 case IPPROTO_SRPC:
1869 s = "SRPC";
1870 break;
1871#endif /* defined(IPPROTO_SRPC) */
1872
1873#if defined(IPPROTO_LARP)
1874 case IPPROTO_LARP:
1875 s = "LARP";
1876 break;
1877#endif /* defined(IPPROTO_LARP) */
1878
1879#if defined(IPPROTO_MTP)
1880 case IPPROTO_MTP:
1881 s = "MTP";
1882 break;
1883#endif /* defined(IPPROTO_MTP) */
1884
1885#if defined(IPPROTO_AX25)
1886 case IPPROTO_AX25:
1887 s = "AX25";
1888 break;
1889#endif /* defined(IPPROTO_AX25) */
1890
1891#if defined(IPPROTO_IPEIP)
1892 case IPPROTO_IPEIP:
1893 s = "IPEIP";
1894 break;
1895#endif /* defined(IPPROTO_IPEIP) */
1896
1897#if defined(IPPROTO_MICP)
1898 case IPPROTO_MICP:
1899 s = "MICP";
1900 break;
1901#endif /* defined(IPPROTO_MICP) */
1902
1903#if defined(IPPROTO_SCCSP)
1904 case IPPROTO_SCCSP:
1905 s = "SCCSP";
1906 break;
1907#endif /* defined(IPPROTO_SCCSP) */
1908
1909#if defined(IPPROTO_ETHERIP)
1910 case IPPROTO_ETHERIP:
1911 s = "ETHERIP";
1912 break;
1913#endif /* defined(IPPROTO_ETHERIP) */
1914
1915#if defined(IPPROTO_ENCAP)
1916# if !defined(IPPROTO_IPIP) || IPPROTO_IPIP!=IPPROTO_ENCAP
1917 case IPPROTO_ENCAP:
1918 s = "ENCAP";
1919 break;
1920# endif /* !defined(IPPROTO_IPIP) || IPPROTO_IPIP!=IPPROTO_ENCAP */
1921#endif /* defined(IPPROTO_ENCAP) */
1922
1923#if defined(IPPROTO_APES)
1924 case IPPROTO_APES:
1925 s = "APES";
1926 break;
1927#endif /* defined(IPPROTO_APES) */
1928
1929#if defined(IPPROTO_GMTP)
1930 case IPPROTO_GMTP:
1931 s = "GMTP";
1932 break;
1933#endif /* defined(IPPROTO_GMTP) */
1934
1935#if defined(IPPROTO_DIVERT)
1936 case IPPROTO_DIVERT:
1937 s = "DIVERT";
1938 break;
1939#endif /* defined(IPPROTO_DIVERT) */
1940
1941 default:
1942 s = (char *)NULL;
1943 }
1944 if (s)
1945 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "%.*s", IPROTOL-1, s);
1946 else {
1947 if (m < 0) {
1948 for (i = 0, m = 1; i < IPROTOL-2; i++)
1949 m *= 10;
1950 }
1951 if (m > p)
1952 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "%d?", p);
1953 else
1954 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "*%d?", p % (m/10));
1955 }
1956}
1957#endif /* !defined(HASPRIVPRIPP) */
1958
1959
1960/*
1961 * printname() - print output name field
1962 */
1963
1964void
1965printname(nl)
1966 int nl; /* NL status */
1967{
1968
1969#if defined(HASNCACHE)
1970 char buf[MAXPATHLEN];
1971 char *cp;
1972 int fp;
1973#endif /* defined(HASNCACHE) */
1974
1975 int ps = 0;
1976
1977 if (Lf->nm && Lf->nm[0]) {
1978
1979 /*
1980 * Print the name characters, if there are some.
1981 */
1982 safestrprt(Lf->nm, stdout, 0);
1983 ps++;
1984 if (!Lf->li[0].af && !Lf->li[1].af)
1985 goto print_nma;
1986 }
1987 if (Lf->li[0].af || Lf->li[1].af) {
1988 if (ps)
1989 putchar(' ');
1990 /*
1991 * If the file has Internet addresses, print them.
1992 */
1993 if (printinaddr())
1994 ps++;
1995 goto print_nma;
1996 }
1997 if (((Lf->ntype == N_BLK) || (Lf->ntype == N_CHR))
1998 && Lf->dev_def && Lf->rdev_def
1999 && printdevname(&Lf->dev, &Lf->rdev, 0, Lf->ntype))
2000 {
2001
2002 /*
2003 * If this is a block or character device and it has a name, print it.
2004 */
2005 ps++;
2006 goto print_nma;
2007 }
2008 if (Lf->is_com) {
2009
2010 /*
2011 * If this is a common node, print that fact.
2012 */
2013 (void) fputs("COMMON: ", stdout);
2014 ps++;
2015 goto print_nma;
2016 }
2017
2018#if defined(HASPRIVNMCACHE)
2019 if (HASPRIVNMCACHE(Lf)) {
2020 ps++;
2021 goto print_nma;
2022 }
2023#endif /* defined(HASPRIVNMCACHE) */
2024
2025 if (Lf->lmi_srch) {
2026 struct mounts *mp;
2027 /*
2028 * Do a deferred local mount info table search for the file system
2029 * (mounted) directory name and inode number, and mounted device name.
2030 */
2031 for (mp = readmnt(); mp; mp = mp->next) {
2032 if (Lf->dev == mp->dev) {
2033 Lf->fsdir = mp->dir;
2034 Lf->fsdev = mp->fsname;
2035
2036#if defined(HASFSINO)
2037 Lf->fs_ino = mp->inode;
2038#endif /* defined(HASFSINO) */
2039
2040 break;
2041 }
2042 }
2043 Lf->lmi_srch = 0;
2044 }
2045 if (Lf->fsdir || Lf->fsdev) {
2046
2047 /*
2048 * Print the file system directory name, device name, and
2049 * possible path name components.
2050 */
2051
2052#if !defined(HASNCACHE) || HASNCACHE<2
2053 if (Lf->fsdir) {
2054 safestrprt(Lf->fsdir, stdout, 0);
2055 ps++;
2056 }
2057#endif /* !defined(HASNCACHE) || HASNCACHE<2 */
2058
2059#if defined(HASNCACHE)
2060
2061# if HASNCACHE<2
2062 if (Lf->na) {
2063 if (NcacheReload) {
2064
2065# if defined(NCACHELDPFX)
2066 NCACHELDPFX
2067# endif /* defined(NCACHELDPFX) */
2068
2069 (void) ncache_load();
2070
2071# if defined(NCACHELDSFX)
2072 NCACHELDSFX
2073# endif /* defined(NCACHELDSFX) */
2074
2075 NcacheReload = 0;
2076 }
2077 if ((cp = ncache_lookup(buf, sizeof(buf), &fp))) {
2078 char *cp1;
2079
2080 if (*cp == '\0')
2081 goto print_nma;
2082 if (fp && Lf->fsdir) {
2083 if (*cp != '/') {
2084 cp1 = strrchr(Lf->fsdir, '/');
2085 if (cp1 == (char *)NULL || *(cp1 + 1) != '\0')
2086 putchar('/');
2087 }
2088 } else
2089 (void) fputs(" -- ", stdout);
2090 safestrprt(cp, stdout, 0);
2091 ps++;
2092 goto print_nma;
2093 }
2094 }
2095# else /* HASNCACHE>1 */
2096 if (NcacheReload) {
2097
2098# if defined(NCACHELDPFX)
2099 NCACHELDPFX
2100# endif /* defined(NCACHELDPFX) */
2101
2102 (void) ncache_load();
2103
2104# if defined(NCACHELDSFX)
2105 NCACHELDSFX
2106# endif /* defined(NCACHELDSFX) */
2107
2108 NcacheReload = 0;
2109 }
2110 if ((cp = ncache_lookup(buf, sizeof(buf), &fp))) {
2111 if (fp) {
2112 safestrprt(cp, stdout, 0);
2113 ps++;
2114 } else {
2115 if (Lf->fsdir) {
2116 safestrprt(Lf->fsdir, stdout, 0);
2117 ps++;
2118 }
2119 if (*cp) {
2120 (void) fputs(" -- ", stdout);
2121 safestrprt(cp, stdout, 0);
2122 ps++;
2123 }
2124 }
2125 goto print_nma;
2126 }
2127 if (Lf->fsdir) {
2128 safestrprt(Lf->fsdir, stdout, 0);
2129 ps++;
2130 }
2131# endif /* HASNCACHE<2 */
2132#endif /* defined(HASNCACHE) */
2133
2134 if (Lf->fsdev) {
2135 if (Lf->fsdir)
2136 (void) fputs(" (", stdout);
2137 else
2138 (void) putchar('(');
2139 safestrprt(Lf->fsdev, stdout, 0);
2140 (void) putchar(')');
2141 ps++;
2142 }
2143 }
2144/*
2145 * Print the NAME column addition, if there is one. If there isn't
2146 * make sure a NL is printed, as requested.
2147 */
2148
2149print_nma:
2150
2151 if (Lf->nma) {
2152 if (ps)
2153 putchar(' ');
2154 safestrprt(Lf->nma, stdout, 0);
2155 ps++;
2156 }
2157/*
2158 * If this file has TCP/IP state information, print it.
2159 */
2160 if (!Ffield && Ftcptpi
2161 && (Lf->lts.type >= 0
2162
2163#if defined(HASTCPTPIQ)
2164 || ((Ftcptpi & TCPTPI_QUEUES) && (Lf->lts.rqs || Lf->lts.sqs))
2165#endif /* defined(HASTCPTPIQ) */
2166
2167#if defined(HASTCPTPIW)
2168 || ((Ftcptpi & TCPTPI_WINDOWS) && (Lf->lts.rws || Lf->lts.wws))
2169#endif /* defined(HASTCPTPIW) */
2170
2171 )) {
2172 if (ps)
2173 putchar(' ');
2174 (void) print_tcptpi(1);
2175 return;
2176 }
2177 if (nl)
2178 putchar('\n');
2179}
2180
2181
2182/*
2183 * printrawaddr() - print raw socket address
2184 */
2185
2186void
2187printrawaddr(sa)
2188 struct sockaddr *sa; /* socket address */
2189{
2190 char *ep;
2191 size_t sz;
2192
2193 ep = endnm(&sz);
2194 (void) snpf(ep, sz, "%u/%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
2195 sa->sa_family,
2196 (unsigned char)sa->sa_data[0],
2197 (unsigned char)sa->sa_data[1],
2198 (unsigned char)sa->sa_data[2],
2199 (unsigned char)sa->sa_data[3],
2200 (unsigned char)sa->sa_data[4],
2201 (unsigned char)sa->sa_data[5],
2202 (unsigned char)sa->sa_data[6],
2203 (unsigned char)sa->sa_data[7],
2204 (unsigned char)sa->sa_data[8],
2205 (unsigned char)sa->sa_data[9],
2206 (unsigned char)sa->sa_data[10],
2207 (unsigned char)sa->sa_data[11],
2208 (unsigned char)sa->sa_data[12],
2209 (unsigned char)sa->sa_data[13]);
2210}
2211
2212
2213/*
2214 * printsockty() - print socket type
2215 */
2216
2217char *
2218printsockty(ty)
2219 int ty; /* socket type -- e.g., from so_type */
2220{
2221 static char buf[64];
2222 char *cp;
2223
2224 switch (ty) {
2225
2226#if defined(SOCK_STREAM)
2227 case SOCK_STREAM:
2228 cp = "STREAM";
2229 break;
2230#endif /* defined(SOCK_STREAM) */
2231
2232#if defined(SOCK_STREAM)
2233 case SOCK_DGRAM:
2234 cp = "DGRAM";
2235 break;
2236#endif /* defined(SOCK_DGRAM) */
2237
2238#if defined(SOCK_RAW)
2239 case SOCK_RAW:
2240 cp = "RAW";
2241 break;
2242#endif /* defined(SOCK_RAW) */
2243
2244#if defined(SOCK_RDM)
2245 case SOCK_RDM:
2246 cp = "RDM";
2247 break;
2248#endif /* defined(SOCK_RDM) */
2249
2250#if defined(SOCK_SEQPACKET)
2251 case SOCK_SEQPACKET:
2252 cp = "SEQPACKET";
2253 break;
2254#endif /* defined(SOCK_SEQPACKET) */
2255
2256 default:
2257 (void) snpf(buf, sizeof(buf), "SOCK_%#x", ty);
2258 return(buf);
2259 }
2260 (void) snpf(buf, sizeof(buf), "SOCK_%s", cp);
2261 return(buf);
2262}
2263
2264
2265/*
2266 * printuid() - print User ID or login name
2267 */
2268
2269char *
2270printuid(uid, ty)
2271 UID_ARG uid; /* User IDentification number */
2272 int *ty; /* returned UID type pointer (NULL
2273 * (if none wanted). If non-NULL
2274 * then: *ty = 0 = login name
2275 * = 1 = UID number */
2276{
2277 int i;
2278 struct passwd *pw;
2279 struct stat sb;
2280 static struct stat sbs;
2281 static struct uidcache {
2282 uid_t uid;
2283 char nm[LOGINML+1];
2284 struct uidcache *next;
2285 } **uc = (struct uidcache **)NULL;
2286 struct uidcache *up, *upn;
2287 static char user[USERPRTL+1];
2288
2289 if (Futol) {
2290 if (CkPasswd) {
2291
2292 /*
2293 * Get the mtime and ctime of /etc/passwd, as required.
2294 */
2295 if (stat("/etc/passwd", &sb) != 0) {
2296 (void) fprintf(stderr, "%s: can't stat(/etc/passwd): %s\n",
2297 Pn, strerror(errno));
2298 Exit(1);
2299 }
2300 }
2301 /*
2302 * Define the UID cache, if necessary.
2303 */
2304 if (!uc) {
2305 if (!(uc = (struct uidcache **)calloc(UIDCACHEL,
2306 sizeof(struct uidcache *))))
2307 {
2308 (void) fprintf(stderr,
2309 "%s: no space for %d byte UID cache hash buckets\n",
2310 Pn, (int)(UIDCACHEL * (sizeof(struct uidcache *))));
2311 Exit(1);
2312 }
2313 if (CkPasswd) {
2314 sbs = sb;
2315 CkPasswd = 0;
2316 }
2317 }
2318 /*
2319 * If it's time to check /etc/passwd and if its the mtime/ctime has
2320 * changed, destroy the existing UID cache.
2321 */
2322 if (CkPasswd) {
2323 if (sbs.st_mtime != sb.st_mtime || sbs.st_ctime != sb.st_ctime)
2324 {
2325 for (i = 0; i < UIDCACHEL; i++) {
2326 if ((up = uc[i])) {
2327 do {
2328 upn = up->next;
2329 (void) free((FREE_P *)up);
2330 } while ((up = upn) != (struct uidcache *)NULL);
2331 uc[i] = (struct uidcache *)NULL;
2332 }
2333 }
2334 sbs = sb;
2335 }
2336 CkPasswd = 0;
2337 }
2338 /*
2339 * Search the UID cache.
2340 */
2341 i = (int)((((unsigned long)uid * 31415L) >> 7) & (UIDCACHEL - 1));
2342 for (up = uc[i]; up; up = up->next) {
2343 if (up->uid == (uid_t)uid) {
2344 if (ty)
2345 *ty = 0;
2346 return(up->nm);
2347 }
2348 }
2349 /*
2350 * The UID is not in the cache.
2351 *
2352 * Look up the login name from the UID for a new cache entry.
2353 */
2354 if (!(pw = getpwuid((uid_t)uid))) {
2355 if (!Fwarn) {
2356 (void) fprintf(stderr, "%s: no pwd entry for UID %lu\n",
2357 Pn, (unsigned long)uid);
2358 }
2359 } else {
2360
2361 /*
2362 * Allocate and fill a new cache entry. Link it to its hash bucket.
2363 */
2364 if (!(upn = (struct uidcache *)malloc(sizeof(struct uidcache))))
2365 {
2366 (void) fprintf(stderr,
2367 "%s: no space for UID cache entry for: %lu, %s)\n",
2368 Pn, (unsigned long)uid, pw->pw_name);
2369 Exit(1);
2370 }
2371 (void) strncpy(upn->nm, pw->pw_name, LOGINML);
2372 upn->nm[LOGINML] = '\0';
2373 upn->uid = (uid_t)uid;
2374 upn->next = uc[i];
2375 uc[i] = upn;
2376 if (ty)
2377 *ty = 0;
2378 return(upn->nm);
2379 }
2380 }
2381/*
2382 * Produce a numeric conversion of the UID.
2383 */
2384 (void) snpf(user, sizeof(user), "%*lu", USERPRTL, (unsigned long)uid);
2385 if (ty)
2386 *ty = 1;
2387 return(user);
2388}
2389
2390
2391/*
2392 * printunkaf() - print unknown address family
2393 */
2394
2395void
2396printunkaf(fam, ty)
2397 int fam; /* unknown address family */
2398 int ty; /* output type: 0 = terse; 1 = full */
2399{
2400 char *p, *s;
2401
2402 p = "";
2403 switch (fam) {
2404
2405#if defined(AF_UNSPEC)
2406 case AF_UNSPEC:
2407 s = "UNSPEC";
2408 break;
2409#endif /* defined(AF_UNSPEC) */
2410
2411#if defined(AF_UNIX)
2412 case AF_UNIX:
2413 s = "UNIX";
2414 break;
2415#endif /* defined(AF_UNIX) */
2416
2417#if defined(AF_INET)
2418 case AF_INET:
2419 s = "INET";
2420 break;
2421#endif /* defined(AF_INET) */
2422
2423#if defined(AF_INET6)
2424 case AF_INET6:
2425 s = "INET6";
2426 break;
2427#endif /* defined(AF_INET6) */
2428
2429#if defined(AF_IMPLINK)
2430 case AF_IMPLINK:
2431 s = "IMPLINK";
2432 break;
2433#endif /* defined(AF_IMPLINK) */
2434
2435#if defined(AF_PUP)
2436 case AF_PUP:
2437 s = "PUP";
2438 break;
2439#endif /* defined(AF_PUP) */
2440
2441#if defined(AF_CHAOS)
2442 case AF_CHAOS:
2443 s = "CHAOS";
2444 break;
2445#endif /* defined(AF_CHAOS) */
2446
2447#if defined(AF_NS)
2448 case AF_NS:
2449 s = "NS";
2450 break;
2451#endif /* defined(AF_NS) */
2452
2453#if defined(AF_ISO)
2454 case AF_ISO:
2455 s = "ISO";
2456 break;
2457#endif /* defined(AF_ISO) */
2458
2459#if defined(AF_NBS)
2460# if !defined(AF_ISO) || AF_NBS!=AF_ISO
2461 case AF_NBS:
2462 s = "NBS";
2463 break;
2464# endif /* !defined(AF_ISO) || AF_NBS!=AF_ISO */
2465#endif /* defined(AF_NBS) */
2466
2467#if defined(AF_ECMA)
2468 case AF_ECMA:
2469 s = "ECMA";
2470 break;
2471#endif /* defined(AF_ECMA) */
2472
2473#if defined(AF_DATAKIT)
2474 case AF_DATAKIT:
2475 s = "DATAKIT";
2476 break;
2477#endif /* defined(AF_DATAKIT) */
2478
2479#if defined(AF_CCITT)
2480 case AF_CCITT:
2481 s = "CCITT";
2482 break;
2483#endif /* defined(AF_CCITT) */
2484
2485#if defined(AF_SNA)
2486 case AF_SNA:
2487 s = "SNA";
2488 break;
2489#endif /* defined(AF_SNA) */
2490
2491#if defined(AF_DECnet)
2492 case AF_DECnet:
2493 s = "DECnet";
2494 break;
2495#endif /* defined(AF_DECnet) */
2496
2497#if defined(AF_DLI)
2498 case AF_DLI:
2499 s = "DLI";
2500 break;
2501#endif /* defined(AF_DLI) */
2502
2503#if defined(AF_LAT)
2504 case AF_LAT:
2505 s = "LAT";
2506 break;
2507#endif /* defined(AF_LAT) */
2508
2509#if defined(AF_HYLINK)
2510 case AF_HYLINK:
2511 s = "HYLINK";
2512 break;
2513#endif /* defined(AF_HYLINK) */
2514
2515#if defined(AF_APPLETALK)
2516 case AF_APPLETALK:
2517 s = "APPLETALK";
2518 break;
2519#endif /* defined(AF_APPLETALK) */
2520
2521#if defined(AF_BSC)
2522 case AF_BSC:
2523 s = "BSC";
2524 break;
2525#endif /* defined(AF_BSC) */
2526
2527#if defined(AF_DSS)
2528 case AF_DSS:
2529 s = "DSS";
2530 break;
2531#endif /* defined(AF_DSS) */
2532
2533#if defined(AF_ROUTE)
2534 case AF_ROUTE:
2535 s = "ROUTE";
2536 break;
2537#endif /* defined(AF_ROUTE) */
2538
2539#if defined(AF_RAW)
2540 case AF_RAW:
2541 s = "RAW";
2542 break;
2543#endif /* defined(AF_RAW) */
2544
2545#if defined(AF_LINK)
2546 case AF_LINK:
2547 s = "LINK";
2548 break;
2549#endif /* defined(AF_LINK) */
2550
2551#if defined(pseudo_AF_XTP)
2552 case pseudo_AF_XTP:
2553 p = "pseudo_";
2554 s = "XTP";
2555 break;
2556#endif /* defined(pseudo_AF_XTP) */
2557
2558#if defined(AF_RMP)
2559 case AF_RMP:
2560 s = "RMP";
2561 break;
2562#endif /* defined(AF_RMP) */
2563
2564#if defined(AF_COIP)
2565 case AF_COIP:
2566 s = "COIP";
2567 break;
2568#endif /* defined(AF_COIP) */
2569
2570#if defined(AF_CNT)
2571 case AF_CNT:
2572 s = "CNT";
2573 break;
2574#endif /* defined(AF_CNT) */
2575
2576#if defined(pseudo_AF_RTIP)
2577 case pseudo_AF_RTIP:
2578 p = "pseudo_";
2579 s = "RTIP";
2580 break;
2581#endif /* defined(pseudo_AF_RTIP) */
2582
2583#if defined(AF_NETMAN)
2584 case AF_NETMAN:
2585 s = "NETMAN";
2586 break;
2587#endif /* defined(AF_NETMAN) */
2588
2589#if defined(AF_INTF)
2590 case AF_INTF:
2591 s = "INTF";
2592 break;
2593#endif /* defined(AF_INTF) */
2594
2595#if defined(AF_NETWARE)
2596 case AF_NETWARE:
2597 s = "NETWARE";
2598 break;
2599#endif /* defined(AF_NETWARE) */
2600
2601#if defined(AF_NDD)
2602 case AF_NDD:
2603 s = "NDD";
2604 break;
2605#endif /* defined(AF_NDD) */
2606
2607#if defined(AF_NIT)
2608# if !defined(AF_ROUTE) || AF_ROUTE!=AF_NIT
2609 case AF_NIT:
2610 s = "NIT";
2611 break;
2612# endif /* !defined(AF_ROUTE) || AF_ROUTE!=AF_NIT */
2613#endif /* defined(AF_NIT) */
2614
2615#if defined(AF_802)
2616# if !defined(AF_RAW) || AF_RAW!=AF_802
2617 case AF_802:
2618 s = "802";
2619 break;
2620# endif /* !defined(AF_RAW) || AF_RAW!=AF_802 */
2621#endif /* defined(AF_802) */
2622
2623#if defined(AF_X25)
2624 case AF_X25:
2625 s = "X25";
2626 break;
2627#endif /* defined(AF_X25) */
2628
2629#if defined(AF_CTF)
2630 case AF_CTF:
2631 s = "CTF";
2632 break;
2633#endif /* defined(AF_CTF) */
2634
2635#if defined(AF_WAN)
2636 case AF_WAN:
2637 s = "WAN";
2638 break;
2639#endif /* defined(AF_WAN) */
2640
2641#if defined(AF_OSINET)
2642# if defined(AF_INET) && AF_INET!=AF_OSINET
2643 case AF_OSINET:
2644 s = "OSINET";
2645 break;
2646# endif /* defined(AF_INET) && AF_INET!=AF_OSINET */
2647#endif /* defined(AF_OSINET) */
2648
2649#if defined(AF_GOSIP)
2650 case AF_GOSIP:
2651 s = "GOSIP";
2652 break;
2653#endif /* defined(AF_GOSIP) */
2654
2655#if defined(AF_SDL)
2656 case AF_SDL:
2657 s = "SDL";
2658 break;
2659#endif /* defined(AF_SDL) */
2660
2661#if defined(AF_IPX)
2662 case AF_IPX:
2663 s = "IPX";
2664 break;
2665#endif /* defined(AF_IPX) */
2666
2667#if defined(AF_SIP)
2668 case AF_SIP:
2669 s = "SIP";
2670 break;
2671#endif /* defined(AF_SIP) */
2672
2673#if defined(psuedo_AF_PIP)
2674 case psuedo_AF_PIP:
2675 p = "pseudo_";
2676 s = "PIP";
2677 break;
2678#endif /* defined(psuedo_AF_PIP) */
2679
2680#if defined(AF_OTS)
2681 case AF_OTS:
2682 s = "OTS";
2683 break;
2684#endif /* defined(AF_OTS) */
2685
2686#if defined(pseudo_AF_BLUE)
2687 case pseudo_AF_BLUE: /* packets for Blue box */
2688 p = "pseudo_";
2689 s = "BLUE";
2690 break;
2691#endif /* defined(pseudo_AF_BLUE) */
2692
2693#if defined(AF_NDRV) /* network driver raw access */
2694 case AF_NDRV:
2695 s = "NDRV";
2696 break;
2697#endif /* defined(AF_NDRV) */
2698
2699#if defined(AF_SYSTEM) /* kernel event messages */
2700 case AF_SYSTEM:
2701 s = "SYSTEM";
2702 break;
2703#endif /* defined(AF_SYSTEM) */
2704
2705#if defined(AF_USER)
2706 case AF_USER:
2707 s = "USER";
2708 break;
2709#endif /* defined(AF_USER) */
2710
2711#if defined(pseudo_AF_KEY)
2712 case pseudo_AF_KEY:
2713 p = "pseudo_";
2714 s = "KEY";
2715 break;
2716#endif /* defined(pseudo_AF_KEY) */
2717
2718#if defined(AF_KEY) /* Security Association DB socket */
2719 case AF_KEY:
2720 s = "KEY";
2721 break;
2722#endif /* defined(AF_KEY) */
2723
2724#if defined(AF_NCA) /* NCA socket */
2725 case AF_NCA:
2726 s = "NCA";
2727 break;
2728#endif /* defined(AF_NCA) */
2729
2730#if defined(AF_POLICY) /* Security Policy DB socket */
2731 case AF_POLICY:
2732 s = "POLICY";
2733 break;
2734#endif /* defined(AF_POLICY) */
2735
2736#if defined(AF_PPP) /* PPP socket */
2737 case AF_PPP:
2738 s = "PPP";
2739 break;
2740#endif /* defined(AF_PPP) */
2741
2742 default:
2743 if (!ty)
2744 (void) snpf(Namech, Namechl, "%#x", fam);
2745 else
2746 (void) snpf(Namech, Namechl,
2747 "no further information on family %#x", fam);
2748 return;
2749 }
2750 if (!ty)
2751 (void) snpf(Namech, Namechl, "%sAF_%s", p, s);
2752 else
2753 (void) snpf(Namech, Namechl, "no further information on %sAF_%s",
2754 p, s);
2755 return;
2756}
2757
2758
2759/*
2760 * update_portmap() - update a portmap entry with its port number or service
2761 * name
2762 */
2763
2764static void
2765update_portmap(pt, pn)
2766 struct porttab *pt; /* porttab entry */
2767 char *pn; /* port name */
2768{
2769 MALLOC_S al, nl;
2770 char *cp;
2771
2772 if (pt->ss)
2773 return;
2774 if (!(al = strlen(pn))) {
2775 pt->ss = 1;
2776 return;
2777 }
2778 nl = al + pt->nl + 2;
2779 if (!(cp = (char *)malloc(nl + 1))) {
2780 (void) fprintf(stderr,
2781 "%s: can't allocate %d bytes for portmap name: %s[%s]\n",
2782 Pn, (int)(nl + 1), pn, pt->name);
2783 Exit(1);
2784 }
2785 (void) snpf(cp, nl + 1, "%s[%s]", pn, pt->name);
2786 (void) free((FREE_P *)pt->name);
2787 pt->name = cp;
2788 pt->nl = nl;
2789 pt->ss = 1;
2790}