2 /* Copyright Gerhard Rieger 2001-2008 */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* the subroutine filan makes a "FILe descriptor ANalysis". It checks the
6 type of file descriptor and tries to retrieve as much info about it as
7 possible without modifying its state.
8 NOTE: it works on UNIX (kernel) file descriptors, not on libc files! */
11 #include "xioconfig.h" /* what features are enabled */
13 #include "sysincludes.h"
29 static int filan_streams_analyze(int fd
, FILE *outfile
);
31 /* dirty workaround so we dont get an error on AIX when being linked with
33 int allow_severity
, deny_severity
;
35 /* global variables for configuring filan */
36 bool filan_followsymlinks
;
40 int sockoptan(int fd
, const struct sockopt
*optname
, int socklay
, FILE *outfile
);
41 int tcpan(int fd
, FILE *outfile
);
42 const char *getfiletypestring(int st_mode
);
44 static int printtime(FILE *outfile
, time_t time
);
46 static int headprinted
;
48 /* analyse a file system entry, referred by file name */
49 int filan_file(const char *filename
, FILE *outfile
) {
53 struct stat64 buf
= {0};
55 struct stat buf
= {0};
56 #endif /* !HAVE_STAT64 */
58 if (filan_followsymlinks
) {
60 result
= Stat64(filename
, &buf
);
62 result
= Stat(filename
, &buf
);
63 #endif /* !HAVE_STAT64 */
65 Warn3("stat(\"%s\", %p): %s", filename
, &buf
, strerror(errno
));
69 result
= Lstat64(filename
, &buf
);
71 result
= Lstat(filename
, &buf
);
72 #endif /* !HAVE_STAT64 */
74 Warn3("lstat(\"%s\", %p): %s", filename
, &buf
, strerror(errno
));
77 switch (buf
.st_mode
&S_IFMT
) {
79 case S_IFSOCK
: /* probably, it's useless to make a socket and describe it */
84 Open(filename
, O_RDONLY
|O_NOCTTY
|O_NONBLOCK
90 Warn2("open(\"%s\", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_LARGEFILE, 0700): %s",
91 filename
, strerror(errno
));
95 result
= filan_stat(&buf
, fd
, -1, outfile
);
100 /* analyze a file descriptor */
101 int filan_fd(int fd
, FILE *outfile
) {
103 struct stat64 buf
= {0};
105 struct stat buf
= {0};
106 #endif /* !HAVE_STAT64 */
109 Debug1("checking file descriptor %u", fd
);
111 result
= Fstat64(fd
, &buf
);
113 result
= Fstat(fd
, &buf
);
114 #endif /* !HAVE_STAT64 */
116 if (errno
== EBADF
) {
117 Debug2("fstat(%d): %s", fd
, strerror(errno
));
119 Warn2("fstat(%d): %s", fd
, strerror(errno
));
123 Debug2("fd %d is a %s", fd
, getfiletypestring(buf
.st_mode
));
125 result
= filan_stat(&buf
, fd
, fd
, outfile
);
128 /* even more dynamic info */
129 { /* see if data is available */
132 ufds
.events
= POLLIN
|POLLPRI
|POLLOUT
147 if (Poll(&ufds
, 1, 0) < 0) {
148 Warn4("poll({%d, %hd, %hd}, 1, 0): %s",
149 ufds
.fd
, ufds
.events
, ufds
.revents
, strerror(errno
));
151 fputs("poll: ", outfile
);
152 if (ufds
.revents
& POLLIN
) fputs("IN,", outfile
);
153 if (ufds
.revents
& POLLPRI
) fputs("PRI,", outfile
);
154 if (ufds
.revents
& POLLOUT
) fputs("OUT,", outfile
);
155 if (ufds
.revents
& POLLERR
) fputs("ERR,", outfile
);
156 if (ufds
.revents
& POLLNVAL
) fputs("NVAL,", outfile
);
158 if (ufds
.revents
& POLLIN
) {
160 if ((result
= Ioctl(fd
, FIONREAD
, &sizet
) >= 0)) {
161 fprintf (outfile
, "; FIONREAD="F_Zu
, sizet
);
164 #endif /* defined(FIONREAD) */
165 #if _WITH_SOCKET && defined(MSG_DONTWAIT)
166 if ((ufds
.revents
& POLLIN
) && isasocket(fd
)) {
167 char _peername
[SOCKADDR_MAX
];
168 struct sockaddr
*pa
= (struct sockaddr
*)_peername
;
169 struct msghdr msgh
= {0};
170 char peekbuff
[1]; /* [0] fails with some compilers */
171 #if HAVE_STRUCT_IOVEC
177 fputs("; ", outfile
);
179 msgh
.msg_namelen
= sizeof(*pa
);
180 #if HAVE_STRUCT_IOVEC
181 iovec
.iov_base
= peekbuff
;
182 iovec
.iov_len
= sizeof(peekbuff
);
183 msgh
.msg_iov
= &iovec
;
186 #if HAVE_STRUCT_MSGHDR_MSGCONTROL
187 msgh
.msg_control
= ctrlbuff
;
189 #if HAVE_STRUCT_MSGHDR_MSGCONTROLLEN
190 msgh
.msg_controllen
= sizeof(ctrlbuff
);
192 #if HAVE_STRUCT_MSGHDR_MSGFLAGS
195 if ((bytes
= Recvmsg(fd
, &msgh
, MSG_PEEK
|MSG_DONTWAIT
)) < 0) {
196 Warn1("recvmsg(): %s", strerror(errno
));
198 fprintf(outfile
, "recvmsg="F_Zd
", ", bytes
);
201 #endif /* _WITH_SOCKET && defined(MSG_DONTWAIT) */
205 fputc('\n', outfile
);
215 #endif /* !HAVE_STAT64 */
216 , int statfd
, int dynfd
, FILE *outfile
) {
222 if (filan_rawoutput
) {
223 fputs(" FD type\tdevice\tinode\tmode\tlinks\tuid\tgid"
234 "\tatime\t\tmtime\t\tctime\t\tcloexec\tflags"
235 #if defined(F_GETOWN)
239 } else /* !rawoutput */ {
240 fputs(" FD type\tdevice\tinode\tmode\tlinks\tuid\tgid"
251 "\tatime\t\t\t\tmtime\t\t\t\tctime\t\t\t\tcloexec\tflags"
252 #if defined(F_GETOWN)
257 } /* endif !rawoutput */
259 #if defined(F_GETSIG)
260 fputs("\tsigio", outfile
);
261 #endif /* defined(F_GETSIG) */
262 fputc('\n', outfile
);
265 if (filan_rawoutput
) {
266 snprintf(stdevstr
, 8, F_st_dev
, buf
->st_dev
);
268 snprintf(stdevstr
, 8, "%hu,%hu", (unsigned short)buf
->st_dev
>>8, (unsigned short)buf
->st_dev
&0xff);
270 fprintf(outfile
, "%4d: %s\t%s\t"
275 #endif /* HAVE_STAT64 */
276 "\t"F_mode
"\t"F_st_nlink
"\t"F_uid
"\t"F_gid
285 #endif /* HAVE_STAT64 */
294 #endif /* HAVE_STAT64 */
297 (dynfd
>=0?dynfd
:statfd
), getfiletypestring(buf
->st_mode
),
300 buf
->st_mode
, buf
->st_nlink
, buf
->st_uid
,
303 (unsigned short)buf
->st_rdev
>>8, (unsigned short)buf
->st_rdev
&0xff,
310 , buf
->st_blocks
/* on Linux, this applies to stat and stat64 */
314 printtime(outfile
, buf
->st_atime
);
315 printtime(outfile
, buf
->st_mtime
);
316 printtime(outfile
, buf
->st_ctime
);
320 fputc('\t', outfile
);
321 time
= asctime(localtime(&buf
->st_mtime
));
322 if (strchr(time
, '\n')) *strchr(time
, '\n') = '\0';
323 fputs(time
, outfile
);
325 fputc('\t', outfile
);
326 time
= asctime(localtime(&buf
->st_ctime
));
327 if (strchr(time
, '\n')) *strchr(time
, '\n') = '\0';
328 fputs(time
, outfile
);
332 /* here comes dynamic info - it is only meaningful with preexisting FDs */
333 if (dynfd
>= 0) { /*!indent */
335 #if defined(F_GETOWN)
338 #if defined(F_GETSIG)
340 #endif /* defined(F_GETSIG) */
342 cloexec
= Fcntl(dynfd
, F_GETFD
);
343 flags
= Fcntl(dynfd
, F_GETFL
);
344 #if defined(F_GETOWN)
345 sigown
= Fcntl(dynfd
, F_GETOWN
);
347 #if defined(F_GETSIG)
348 sigio
= Fcntl(dynfd
, F_GETSIG
);
349 #endif /* defined(F_GETSIG) */
350 fprintf(outfile
, "\t%d\tx%06x", cloexec
, flags
);
351 #if defined(F_GETOWN)
352 fprintf(outfile
, "\t%d", sigown
);
354 #if defined(F_GETSIG)
355 fprintf(outfile
, "\t%d", sigio
);
356 #endif /* defined(F_GETSIG) */
359 #if defined(F_GETOWN)
362 #if defined(F_GETSIG)
364 #endif /* defined(F_GETSIG) */
368 /* ever heard of POSIX streams? here we handle these */
369 filan_streams_analyze(statfd
, outfile
);
371 /* now see for type specific infos */
372 if (statfd
>= 0) { /*!indent */
373 switch (buf
->st_mode
&S_IFMT
) {
374 case (S_IFIFO
): /* 1, FIFO */
376 case (S_IFCHR
): /* 2, character device */
377 result
= cdevan(statfd
, outfile
);
379 case (S_IFDIR
): /* 4, directory */
381 case (S_IFBLK
): /* 6, block device */
383 case (S_IFREG
): /* 8, regular file */
385 case (S_IFLNK
): /* 10, symbolic link */
388 case (S_IFSOCK
): /* 12, socket */
390 result
= sockan(statfd
, outfile
);
392 Warn("SOCKET support not compiled in");
394 #endif /* !_WITH_SOCKET */
396 #endif /* S_IFSOCK */
408 result
= Fcntl(fd
, F_GETFD
);
409 fcntl(fd
, F_GETFL
, );
410 fcntl(fd
, F_GETLK
, );
412 fcntl(fd
, F_GETOWN
, );
415 fcntl(fd
, F_GETSIG
, );
420 int devinfo(int fd
) {
426 /* returns 0 on success (not a stream descriptor, or no module)
427 returns <0 on failure */
428 static int filan_streams_analyze(int fd
, FILE *outfile
) {
430 # define SL_NMODS 8 /* max number of module names we can store */
431 struct str_list modnames
;
434 if (!isastream(fd
)) {
435 fprintf(outfile
, "\t(no STREAMS modules)");
438 #if 0 /* uncomment for debugging */
439 fprintf(outfile
, "\tfind=%d", ioctl(fd
, I_FIND
, "ldterm"));
441 modnames
.sl_nmods
= ioctl(fd
, I_LIST
, 0);
442 if (modnames
.sl_nmods
< 0) {
443 fprintf(stderr
, "ioctl(%d, I_LIST, 0): %s\n", fd
, strerror(errno
));
446 modnames
.sl_modlist
= Malloc(modnames
.sl_nmods
*(sizeof(struct str_mlist
)));
447 if (modnames
.sl_modlist
== NULL
) {
448 fprintf(stderr
, "out of memory\n");
451 if (ioctl(fd
, I_LIST
, &modnames
) < 0) {
452 fprintf(stderr
, "ioctl(%d, I_LIST, %p): %s\n",
453 fd
, &modnames
, strerror(errno
));
454 free(modnames
.sl_modlist
);
457 fprintf(outfile
, "\tSTREAMS: ");
458 for (i
= 0; i
< modnames
.sl_nmods
; ++i
) {
459 fprintf(outfile
, "\"%s\"", modnames
.sl_modlist
[i
].l_name
);
460 if (i
+1 < modnames
.sl_nmods
) fputc(',', outfile
);
462 free(modnames
.sl_modlist
);
463 #endif /* defined(I_LIST) */
468 /* character device analysis */
469 int cdevan(int fd
, FILE *outfile
) {
473 if ((ret
= Isatty(fd
)) < 0) {
474 Warn2("isatty(%d): %s", fd
, strerror(errno
));
478 struct termios termarg
;
482 if ((name
= Ttyname(fd
)) == NULL
) {
483 /*Warn2("ttyname(%d): %s", fd, strerror(errno));*/
484 fputs("\tNULL", outfile
);
486 fprintf(outfile
, "\t%s", name
);
488 if (Tcgetattr(fd
, &termarg
) < 0) {
489 Warn3("tcgetattr(%d, %p): %s", fd
, &termarg
, strerror(errno
));
492 fprintf(outfile
, " \tIFLAGS=%08x OFLAGS=%08x CFLAGS=%08x LFLAGS=%08x",
493 termarg
.c_iflag
, termarg
.c_oflag
, termarg
.c_cflag
, termarg
.c_lflag
);
495 /* and the control characters */
496 if (filan_rawoutput
) {
497 for (i
=0; i
<NCCS
; ++i
) {
498 fprintf(outfile
, " cc[%d]=%d", i
, termarg
.c_cc
[i
]);
501 for (i
=0; i
<NCCS
; ++i
) {
504 ch
= termarg
.c_cc
[i
];
506 s
[0] = ch
; s
[1]= '\0';
507 } else if (ch
< ' ') {
508 s
[0] = '^'; s
[1] = ch
+'@'; s
[2] = '\0';
511 s
[1] = (ch
>>4)>=10?(ch
>>4)-10+'A':(ch
>>4)+'0';
512 s
[2] = (ch
&0x0f)>=10?(ch
&0x0f)-10+'A':(ch
&0x0f)+'0';
515 fprintf(outfile
, " cc[%d]=%s", i
, s
);
519 #endif /* _WITH_TERMIOS */
525 int sockan(int fd
, FILE *outfile
) {
526 #define FILAN_OPTLEN 256
527 #define FILAN_NAMELEN 256
530 static const char *socktypes
[] = {
531 "undef", "STREAM", "DGRAM", "RAW", "RDM",
532 "SEQPACKET", "undef", "undef", "undef", "undef",
533 "PACKET", "undef" } ;
534 char nambuff
[FILAN_NAMELEN
];
535 /* in Linux these optcodes are 'enum', but on AIX they are bits! */
536 static const struct sockopt sockopts
[] = {
538 {SO_REUSEADDR
, "REUSEADDR"},
542 {SO_PROTOTYPE
, "PROTOTYPE"},
544 {SO_DONTROUTE
, "DONTROUTE"},
545 {SO_BROADCAST
, "BROADCAST"},
546 {SO_SNDBUF
, "SNDBUF"},
547 {SO_RCVBUF
, "RCVBUF"},
548 {SO_KEEPALIVE
, "KEEPALIVE"},
549 {SO_OOBINLINE
, "OOBINLINE"},
551 {SO_NO_CHECK
, "NO_CHECK"},
554 {SO_PRIORITY
, "PRIORITY"},
556 {SO_LINGER
, "LINGER"},
558 {SO_BSDCOMPAT
, "BSDCOMPAT"},
561 {SO_REUSEPORT
, "REUSEPORT"},
562 #endif /* defined(SO_REUSEPORT) */
564 {SO_PASSCRED
, "PASSCRED"},
567 {SO_PEERCRED
, "PEERCRED"},
570 {SO_RCVLOWAT
, "RCVLOWAT"},
573 {SO_SNDLOWAT
, "SNDLOWAT"},
576 {SO_RCVTIMEO
, "RCVTIMEO"},
579 {SO_SNDTIMEO
, "SNDTIMEO"},
581 #ifdef SO_SECURITY_AUTHENTICATION
582 {SO_SECURITY_AUTHENTICATION
, "SECURITY_AUTHENTICATION"},
584 #ifdef SO_SECURITY_ENCRYPTION_TRANSPORT
585 {SO_SECURITY_ENCRYPTION_TRANSPORT
, "SECURITY_ENCRYPTION_TRANSPORT"},
587 #ifdef SO_SECURITY_ENCRYPTION_NETWORK
588 {SO_SECURITY_ENCRYPTION_NETWORK
, "SECURITY_ENCRYPTION_NETWORK"},
590 #ifdef SO_BINDTODEVICE
591 {SO_BINDTODEVICE
, "BINDTODEVICE"},
593 #ifdef SO_ATTACH_FILTER
594 {SO_ATTACH_FILTER
, "ATTACH_FILTER"},
596 #ifdef SO_DETACH_FILTER
597 {SO_DETACH_FILTER
, "DETACH_FILTER"},
600 char optval
[FILAN_OPTLEN
];
601 const struct sockopt
*optname
;
602 union sockaddr_union sockname
, peername
; /* the longest I know of */
604 #if 0 && defined(SIOCGIFNAME)
605 /*Linux struct ifreq ifc = {{{ 0 }}};*/
606 struct ifreq ifc
= {{ 0 }};
609 optlen
= FILAN_OPTLEN
;
610 result
= Getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, optval
, &optlen
);
612 Debug4("getsockopt(%d, SOL_SOCKET, SO_TYPE, %p, {"F_socklen
"}): %s",
613 fd
, optval
, optlen
, strerror(errno
));
615 Debug3("fd %d: socket of type %d (\"%s\")", fd
, *(int *)optval
,
616 socktypes
[*(int *)optval
]);
619 optname
= sockopts
; while (optname
->so
) {
620 optlen
= FILAN_OPTLEN
;
622 Getsockopt(fd
, SOL_SOCKET
, optname
->so
, (void *)optval
, &optlen
);
624 Debug5("getsockopt(%d, SOL_SOCKET, %d, %p, {"F_socklen
"}): %s",
625 fd
, optname
->so
, optval
, optlen
, strerror(errno
));
626 fputc('\t', outfile
);
627 } else if (optlen
== sizeof(int)) {
628 Debug2("getsockopt(,,, {%d}, %d)",
629 *(int *)optval
, optlen
);
630 /*Info2("%s: %d", optname->name, *(int *)optval);*/
631 fprintf(outfile
, "%s=%d\t", optname
->name
, *(int *)optval
);
633 Debug3("getsockopt(,,, {%d,%d}, %d)",
634 ((int *)optval
)[0], ((int *)optval
)[1], optlen
);
635 fprintf(outfile
, "%s={%d,%d}\t", optname
->name
,
636 ((int *)optval
)[0], ((int *)optval
)[1]);
641 namelen
= sizeof(sockname
);
642 result
= Getsockname(fd
, (struct sockaddr
*)&sockname
, &namelen
);
645 Warn2("getsockname(%d): %s", fd
, strerror(errno
));
648 fputc('\t', outfile
);
649 fputs(sockaddr_info((struct sockaddr
*)&sockname
, namelen
, nambuff
, sizeof(nambuff
)),
652 namelen
= sizeof(peername
);
653 result
= Getpeername(fd
, (struct sockaddr
*)&peername
, &namelen
);
656 Warn2("getpeername(%d): %s", fd
, strerror(errno
));
658 /* only valid if getpeername() succeeded */
659 fputs(" <-> ", outfile
);
660 fprintf(outfile
, "%s\t",
661 sockaddr_info((struct sockaddr
*)&peername
, namelen
,
662 nambuff
, sizeof(nambuff
)));
665 #if 0 && defined(SIOCGIFNAME)
666 if ((result
= Ioctl(fd
, SIOCGIFNAME
, &ifc
)) < 0) {
667 Warn3("ioctl(%d, SIOCGIFNAME, %p): %s", fd
, &ifc
, strerror(errno
));
669 fprintf(outfile
, "IFNAME=\"%s\"\t", ifc
.ifr_name
);
671 #endif /* SIOCGIFNAME */
673 switch (((struct sockaddr
*)&sockname
)->sa_family
) {
676 /* no options for unix domain sockets known yet -> no unixan() */
682 result
= ipan(fd
, outfile
);
687 result
= ipan(fd
, outfile
);
688 result
|= ip6an(fd
, outfile
);
692 fputs("**** NO FURTHER ANALYSIS FOR THIS SOCKET TYPE IMPLEMENTED", outfile
);
699 #endif /* _WITH_SOCKET */
702 #if WITH_IP4 || WITH_IP6
703 /* prints the option values for the IP protocol and the IP based protocols */
704 /* no distinction between IP4 and IP6 yet */
705 int ipan(int fd
, FILE *outfile
) {
706 /* in Linux these optcodes are 'enum', but on AIX they are bits! */
707 static const struct sockopt ipopts
[] = {
711 {IP_HDRINCL
, "IP_HDRINCL"},
714 {IP_OPTIONS
, "IP_OPTIONS"},
716 #ifdef IP_ROUTER_ALERT
717 {IP_ROUTER_ALERT
, "IP_ROUTER_ALERT"},
720 {IP_RECVOPTS
, "IP_RECVOPTS"},
723 {IP_RETOPTS
, "IP_RETOPTS"},
726 {IP_PKTINFO
, "IP_PKTINFO"},
729 {IP_PKTOPTIONS
, "IP_PKTOPTIONS"},
731 #ifdef IP_MTU_DISCOVER
732 {IP_MTU_DISCOVER
, "IP_MTU_DISCOVER"},
735 {IP_RECVERR
, "IP_RECVERR"},
738 {IP_RECVTTL
, "IP_RECVTTL"},
741 {IP_RECVTOS
, "IP_RECVTOS"},
747 {IP_FREEBIND
, "IP_FREEBIND"},
749 #ifdef IP_MULTICAST_TTL
750 {IP_MULTICAST_TTL
, "IP_MULTICAST_TTL"},
752 #ifdef IP_MULTICAST_LOOP
753 {IP_MULTICAST_LOOP
, "IP_MULTICAST_LOOP"},
756 const struct sockopt
*optname
;
758 socklen_t optlen
= sizeof(opttype
);
760 optname
= ipopts
; while (optname
->so
) {
761 sockoptan(fd
, optname
, SOL_IP
, outfile
);
764 /* want to pass the fd to the next layer protocol. dont know how to get the
765 protocol number from the fd? use TYPE to identify TCP. */
766 if (Getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &opttype
, &optlen
) >= 0) {
769 case SOCK_STREAM
: tcpan(fd
, outfile
); break;
779 /* prints the option values for the IPv6 protocol */
780 int ip6an(int fd
, FILE *outfile
) {
781 static const struct sockopt ip6opts
[] = {
783 {IPV6_V6ONLY
, "IPV6_V6ONLY"},
786 const struct sockopt
*optname
;
788 optname
= ip6opts
; while (optname
->so
) {
789 sockoptan(fd
, optname
, SOL_IPV6
, outfile
);
794 #endif /* WITH_IP6 */
798 int tcpan(int fd
, FILE *outfile
) {
799 static const struct sockopt tcpopts
[] = {
801 { TCP_NODELAY
, "TCP_NODELAY" },
804 { TCP_MAXSEG
, "TCP_MAXSEG" },
807 { TCP_STDURG
, "TCP_STDURG" },
810 { TCP_RFC1323
, "TCP_RFC1323" },
813 { TCP_CORK
, "TCP_CORK" },
816 { TCP_KEEPIDLE
, "TCP_KEEPIDLE" },
819 { TCP_KEEPINTVL
, "TCP_KEEPINTVL" },
822 { TCP_KEEPCNT
, "TCP_KEEPCNT" },
825 { TCP_SYNCNT
, "TCP_SYNCNT" },
828 { TCP_LINGER2
, "TCP_LINGER2" },
830 #ifdef TCP_DEFER_ACCEPT
831 { TCP_DEFER_ACCEPT
, "TCP_ACCEPT" },
833 #ifdef TCP_WINDOW_CLAMP
834 { TCP_WINDOW_CLAMP
, "TCP_WINDOW_CLAMP" },
837 { TCP_INFO
, "TCP_INFO" },
840 { TCP_QUICKACK
, "TCP_QUICKACK" },
843 { TCP_MD5SIG
, "TCP_MD5SIG" },
846 { TCP_NOOPT
, "TCP_NOOPT" },
849 { TCP_NOPUSH
, "TCP_NOPUSH" },
851 #ifdef TCP_SACK_DISABLE
852 { TCP_SACK_DISABLE
, "TCP_SACK_DISABLE" },
854 #ifdef TCP_SIGNATURE_ENABLE
855 { TCP_SIGNATURE_ENABLE
, "TCP_SIGNATURE_ENABLE" },
857 #ifdef TCP_ABORT_THRESHOLD
858 { TCP_ABORT_THRESHOLD
, "TCP_ABORT_THRESHOLD" },
860 #ifdef TCP_CONN_ABORT_THRESHOLD
861 { TCP_CONN_ABORT_THRESHOLD
, "TCP_CONN_ABORT_THRESHOLD" },
864 { TCP_KEEPINIT
, "TCP_KEEPINIT" },
867 { TCP_PAWS
, "TCP_PAWS" },
870 { TCP_SACKENA
, "TCP_SACKENA" },
873 { TCP_TSOPTENA
, "TCP_TSOPTENA" },
877 const struct sockopt
*optname
;
879 optname
= tcpopts
; while (optname
->so
) {
880 sockoptan(fd
, optname
, SOL_TCP
, outfile
);
885 #endif /* WITH_TCP */
889 int sockoptan(int fd
, const struct sockopt
*optname
, int socklay
, FILE *outfile
) {
890 #define FILAN_OPTLEN 256
891 char optval
[FILAN_OPTLEN
];
895 optlen
= FILAN_OPTLEN
;
897 Getsockopt(fd
, socklay
, optname
->so
, (void *)optval
, &optlen
);
899 Debug6("getsockopt(%d, %d, %d, %p, {"F_socklen
"}): %s",
900 fd
, socklay
, optname
->so
, optval
, optlen
, strerror(errno
));
901 fputc('\t', outfile
);
903 } else if (optlen
== 0) {
904 Debug1("getsockopt(,,, {}, %d)", optlen
);
905 fprintf(outfile
, "%s=\"\"\t", optname
->name
);
906 } else if (optlen
== sizeof(int)) {
907 Debug2("getsockopt(,,, {%d}, %d)",
908 *(int *)optval
, optlen
);
909 fprintf(outfile
, "%s=%d\t", optname
->name
, *(int *)optval
);
911 char outbuf
[FILAN_OPTLEN
*9+128], *cp
= outbuf
;
913 for (i
= 0; i
< optlen
/sizeof(unsigned int); ++i
) {
914 cp
+= sprintf(cp
, "%08x ", ((unsigned int *)optval
)[i
]);
916 *--cp
= '\0'; /* delete trailing space */
917 Debug2("getsockopt(,,, {%s}, %d)", outbuf
, optlen
);
919 fprintf(outfile
, "%s={%s}\t", optname
->name
, outbuf
);
924 #endif /* _WITH_SOCKET */
928 int isasocket(int fd
) {
934 #endif /* HAVE_STAT64 */
942 Info3("fstat(%d, %p): %s", fd
, &props
, strerror(errno
));
945 /* note: when S_ISSOCK was undefined, it always gives 0 */
946 return S_ISSOCK(props
.st_mode
);
948 #endif /* _WITH_SOCKET */
951 const char *getfiletypestring(int st_mode
) {
954 switch (st_mode
&S_IFMT
) {
955 case S_IFIFO
: s
= "pipe"; break;
956 case S_IFCHR
: s
= "chrdev"; break;
957 case S_IFDIR
: s
= "dir"; break;
958 case S_IFBLK
: s
= "blkdev"; break;
959 case S_IFREG
: s
= "file"; break;
960 case S_IFLNK
: s
= "symlink"; break;
961 case S_IFSOCK
: s
= "socket"; break;
963 default: s
= "undef"; break;
968 static int printtime(FILE *outfile
, time_t time
) {
971 if (filan_rawoutput
) {
972 fprintf(outfile
, "\t"F_time
, time
);
974 fputc('\t', outfile
);
975 s
= asctime(localtime(&time
));
976 if (strchr(s
, '\n')) *strchr(s
, '\n') = '\0';