10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51548Srshoaib * Common Development and Distribution License (the "License"). 61548Srshoaib * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211548Srshoaib 220Sstevel@tonic-gate /* 231548Srshoaib * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <unistd.h> 320Sstevel@tonic-gate #include <fcntl.h> 330Sstevel@tonic-gate #include <ctype.h> 340Sstevel@tonic-gate #include <string.h> 350Sstevel@tonic-gate #include <signal.h> 360Sstevel@tonic-gate #include <errno.h> 370Sstevel@tonic-gate #include <dirent.h> 380Sstevel@tonic-gate #include <limits.h> 390Sstevel@tonic-gate #include <door.h> 400Sstevel@tonic-gate #include <sys/types.h> 411095Spriyanka #include <sys/socket.h> 420Sstevel@tonic-gate #include <sys/stat.h> 430Sstevel@tonic-gate #include <sys/mman.h> 440Sstevel@tonic-gate #include <sys/mkdev.h> 450Sstevel@tonic-gate #include <sys/un.h> 460Sstevel@tonic-gate #include <netdb.h> 470Sstevel@tonic-gate #include <libproc.h> 481095Spriyanka #include <netinet/in.h> 490Sstevel@tonic-gate #include <arpa/inet.h> 500Sstevel@tonic-gate #include <netdb.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate static char *command; 530Sstevel@tonic-gate static volatile int interrupt; 540Sstevel@tonic-gate static int Fflag; 550Sstevel@tonic-gate static boolean_t nflag = B_FALSE; 560Sstevel@tonic-gate 570Sstevel@tonic-gate static void intr(int); 580Sstevel@tonic-gate static void dofcntl(struct ps_prochandle *, int, int, int); 590Sstevel@tonic-gate static void dosocket(struct ps_prochandle *, int); 600Sstevel@tonic-gate static void show_files(struct ps_prochandle *); 610Sstevel@tonic-gate static void show_fileflags(int); 620Sstevel@tonic-gate static void show_door(struct ps_prochandle *, int); 630Sstevel@tonic-gate 640Sstevel@tonic-gate int 650Sstevel@tonic-gate main(int argc, char **argv) 660Sstevel@tonic-gate { 670Sstevel@tonic-gate int retc = 0; 680Sstevel@tonic-gate int opt; 690Sstevel@tonic-gate int errflg = 0; 700Sstevel@tonic-gate struct ps_prochandle *Pr; 710Sstevel@tonic-gate 720Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL) 730Sstevel@tonic-gate command++; 740Sstevel@tonic-gate else 750Sstevel@tonic-gate command = argv[0]; 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* options */ 780Sstevel@tonic-gate while ((opt = getopt(argc, argv, "Fn")) != EOF) { 790Sstevel@tonic-gate switch (opt) { 800Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */ 810Sstevel@tonic-gate Fflag = PGRAB_FORCE; 820Sstevel@tonic-gate break; 830Sstevel@tonic-gate case 'n': 840Sstevel@tonic-gate nflag = B_TRUE; 850Sstevel@tonic-gate break; 860Sstevel@tonic-gate default: 870Sstevel@tonic-gate errflg = 1; 880Sstevel@tonic-gate break; 890Sstevel@tonic-gate } 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate argc -= optind; 930Sstevel@tonic-gate argv += optind; 940Sstevel@tonic-gate 950Sstevel@tonic-gate if (errflg || argc <= 0) { 960Sstevel@tonic-gate (void) fprintf(stderr, "usage:\t%s [-F] pid ...\n", 970Sstevel@tonic-gate command); 980Sstevel@tonic-gate (void) fprintf(stderr, 990Sstevel@tonic-gate " (report open files of each process)\n"); 1000Sstevel@tonic-gate (void) fprintf(stderr, 1010Sstevel@tonic-gate " -F: force grabbing of the target process\n"); 1020Sstevel@tonic-gate exit(2); 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* catch signals from terminal */ 1060Sstevel@tonic-gate if (sigset(SIGHUP, SIG_IGN) == SIG_DFL) 1070Sstevel@tonic-gate (void) sigset(SIGHUP, intr); 1080Sstevel@tonic-gate if (sigset(SIGINT, SIG_IGN) == SIG_DFL) 1090Sstevel@tonic-gate (void) sigset(SIGINT, intr); 1100Sstevel@tonic-gate if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL) 1110Sstevel@tonic-gate (void) sigset(SIGQUIT, intr); 1120Sstevel@tonic-gate (void) sigset(SIGPIPE, intr); 1130Sstevel@tonic-gate (void) sigset(SIGTERM, intr); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate (void) proc_initstdio(); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate while (--argc >= 0 && !interrupt) { 1190Sstevel@tonic-gate char *arg; 1200Sstevel@tonic-gate psinfo_t psinfo; 1210Sstevel@tonic-gate pid_t pid; 1220Sstevel@tonic-gate int gret; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate (void) proc_flushstdio(); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* get the specified pid and the psinfo struct */ 1270Sstevel@tonic-gate if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS, 1280Sstevel@tonic-gate &psinfo, &gret)) == -1) { 1290Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n", 1300Sstevel@tonic-gate command, arg, Pgrab_error(gret)); 1310Sstevel@tonic-gate retc++; 1320Sstevel@tonic-gate } else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) { 1330Sstevel@tonic-gate if (Pcreate_agent(Pr) == 0) { 1340Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 1350Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", 1360Sstevel@tonic-gate (int)pid, psinfo.pr_psargs); 1370Sstevel@tonic-gate show_files(Pr); 1380Sstevel@tonic-gate Pdestroy_agent(Pr); 1390Sstevel@tonic-gate } else { 1400Sstevel@tonic-gate (void) fprintf(stderr, 1410Sstevel@tonic-gate "%s: cannot control process %d\n", 1420Sstevel@tonic-gate command, (int)pid); 1430Sstevel@tonic-gate retc++; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate Prelease(Pr, 0); 1460Sstevel@tonic-gate Pr = NULL; 1470Sstevel@tonic-gate } else { 1480Sstevel@tonic-gate switch (gret) { 1490Sstevel@tonic-gate case G_SYS: 1500Sstevel@tonic-gate case G_SELF: 1510Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 1520Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", (int)pid, 1530Sstevel@tonic-gate psinfo.pr_psargs); 1540Sstevel@tonic-gate if (gret == G_SYS) 1550Sstevel@tonic-gate (void) printf(" [system process]\n"); 1560Sstevel@tonic-gate else 1570Sstevel@tonic-gate show_files(NULL); 1580Sstevel@tonic-gate break; 1590Sstevel@tonic-gate default: 1600Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %d\n", 1610Sstevel@tonic-gate command, Pgrab_error(gret), (int)pid); 1620Sstevel@tonic-gate retc++; 1630Sstevel@tonic-gate break; 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate (void) proc_finistdio(); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if (interrupt && retc == 0) 1730Sstevel@tonic-gate retc++; 1740Sstevel@tonic-gate return (retc); 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* ARGSUSED */ 1780Sstevel@tonic-gate static void 1790Sstevel@tonic-gate intr(int sig) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate interrupt = 1; 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* ------ begin specific code ------ */ 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate static void 1870Sstevel@tonic-gate show_files(struct ps_prochandle *Pr) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate DIR *dirp; 1900Sstevel@tonic-gate struct dirent *dentp; 1910Sstevel@tonic-gate char pname[100]; 1920Sstevel@tonic-gate char fname[PATH_MAX]; 1930Sstevel@tonic-gate struct stat64 statb; 1940Sstevel@tonic-gate struct rlimit rlim; 1950Sstevel@tonic-gate pid_t pid; 1960Sstevel@tonic-gate int fd; 1970Sstevel@tonic-gate char *s; 1980Sstevel@tonic-gate int ret; 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) { 2010Sstevel@tonic-gate ulong_t nfd = rlim.rlim_cur; 2020Sstevel@tonic-gate if (nfd == RLIM_INFINITY) 2030Sstevel@tonic-gate (void) printf( 2040Sstevel@tonic-gate " Current rlimit: unlimited file descriptors\n"); 2050Sstevel@tonic-gate else 2060Sstevel@tonic-gate (void) printf( 2070Sstevel@tonic-gate " Current rlimit: %lu file descriptors\n", nfd); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* in case we are doing this to ourself */ 2110Sstevel@tonic-gate pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate (void) sprintf(pname, "/proc/%d/fd", (int)pid); 2140Sstevel@tonic-gate if ((dirp = opendir(pname)) == NULL) { 2150Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open directory %s\n", 2160Sstevel@tonic-gate command, pname); 2170Sstevel@tonic-gate return; 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* for each open file --- */ 2210Sstevel@tonic-gate while ((dentp = readdir(dirp)) != NULL && !interrupt) { 2220Sstevel@tonic-gate char unknown[12]; 2230Sstevel@tonic-gate dev_t rdev; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* skip '.' and '..' */ 2260Sstevel@tonic-gate if (!isdigit(dentp->d_name[0])) 2270Sstevel@tonic-gate continue; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate fd = atoi(dentp->d_name); 2300Sstevel@tonic-gate if (pr_fstat64(Pr, fd, &statb) == -1) { 2310Sstevel@tonic-gate s = unknown; 2320Sstevel@tonic-gate (void) sprintf(s, "%4d", fd); 2330Sstevel@tonic-gate perror(s); 2340Sstevel@tonic-gate continue; 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate rdev = NODEV; 2380Sstevel@tonic-gate switch (statb.st_mode & S_IFMT) { 2390Sstevel@tonic-gate case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break; 2400Sstevel@tonic-gate case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break; 2410Sstevel@tonic-gate case S_IFIFO: s = "S_IFIFO"; break; 2420Sstevel@tonic-gate case S_IFDIR: s = "S_IFDIR"; break; 2430Sstevel@tonic-gate case S_IFREG: s = "S_IFREG"; break; 2440Sstevel@tonic-gate case S_IFLNK: s = "S_IFLNK"; break; 2450Sstevel@tonic-gate case S_IFSOCK: s = "S_IFSOCK"; break; 2460Sstevel@tonic-gate case S_IFDOOR: s = "S_IFDOOR"; break; 2470Sstevel@tonic-gate case S_IFPORT: s = "S_IFPORT"; break; 2480Sstevel@tonic-gate default: 2490Sstevel@tonic-gate s = unknown; 2500Sstevel@tonic-gate (void) sprintf(s, "0x%.4x ", 2510Sstevel@tonic-gate (int)statb.st_mode & S_IFMT); 2520Sstevel@tonic-gate break; 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate (void) printf("%4d: %s mode:0%.3o", 2560Sstevel@tonic-gate fd, 2570Sstevel@tonic-gate s, 2580Sstevel@tonic-gate (int)statb.st_mode & ~S_IFMT); 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate if (major(statb.st_dev) != (major_t)NODEV && 2610Sstevel@tonic-gate minor(statb.st_dev) != (minor_t)NODEV) 2620Sstevel@tonic-gate (void) printf(" dev:%lu,%lu", 2630Sstevel@tonic-gate (ulong_t)major(statb.st_dev), 2640Sstevel@tonic-gate (ulong_t)minor(statb.st_dev)); 2650Sstevel@tonic-gate else 2660Sstevel@tonic-gate (void) printf(" dev:0x%.8lX", (long)statb.st_dev); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFPORT) { 2690Sstevel@tonic-gate (void) printf(" uid:%d gid:%d", 2700Sstevel@tonic-gate (int)statb.st_uid, 2710Sstevel@tonic-gate (int)statb.st_gid); 2720Sstevel@tonic-gate (void) printf(" size:%lld\n", 2730Sstevel@tonic-gate (longlong_t)statb.st_size); 2740Sstevel@tonic-gate continue; 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate (void) printf(" ino:%llu uid:%d gid:%d", 2780Sstevel@tonic-gate (u_longlong_t)statb.st_ino, 2790Sstevel@tonic-gate (int)statb.st_uid, 2800Sstevel@tonic-gate (int)statb.st_gid); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate if (rdev == NODEV) 2830Sstevel@tonic-gate (void) printf(" size:%lld\n", 2840Sstevel@tonic-gate (longlong_t)statb.st_size); 2850Sstevel@tonic-gate else if (major(rdev) != (major_t)NODEV && 2860Sstevel@tonic-gate minor(rdev) != (minor_t)NODEV) 2870Sstevel@tonic-gate (void) printf(" rdev:%lu,%lu\n", 2880Sstevel@tonic-gate (ulong_t)major(rdev), 2890Sstevel@tonic-gate (ulong_t)minor(rdev)); 2900Sstevel@tonic-gate else 2910Sstevel@tonic-gate (void) printf(" rdev:0x%.8lX\n", (long)rdev); 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate if (!nflag) { 2940Sstevel@tonic-gate dofcntl(Pr, fd, 2950Sstevel@tonic-gate (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP)) 2960Sstevel@tonic-gate == (S_IFREG|S_ENFMT), 2970Sstevel@tonic-gate (statb.st_mode & S_IFMT) == S_IFDOOR); 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFSOCK) 3000Sstevel@tonic-gate dosocket(Pr, fd); 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate (void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate if ((ret = readlink(pname, fname, PATH_MAX - 1)) > 0) { 3050Sstevel@tonic-gate fname[ret] = '\0'; 3060Sstevel@tonic-gate (void) printf(" %s\n", fname); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate (void) closedir(dirp); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* examine open file with fcntl() */ 3150Sstevel@tonic-gate static void 3160Sstevel@tonic-gate dofcntl(struct ps_prochandle *Pr, int fd, int manditory, int isdoor) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate struct flock flock; 3190Sstevel@tonic-gate int fileflags; 3200Sstevel@tonic-gate int fdflags; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0); 3230Sstevel@tonic-gate fdflags = pr_fcntl(Pr, fd, F_GETFD, 0); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if (fileflags != -1 || fdflags != -1) { 3260Sstevel@tonic-gate (void) printf(" "); 3270Sstevel@tonic-gate if (fileflags != -1) 3280Sstevel@tonic-gate show_fileflags(fileflags); 3290Sstevel@tonic-gate if (fdflags != -1 && (fdflags & FD_CLOEXEC)) 3300Sstevel@tonic-gate (void) printf(" FD_CLOEXEC"); 3310Sstevel@tonic-gate if (isdoor) 3320Sstevel@tonic-gate show_door(Pr, fd); 3330Sstevel@tonic-gate (void) fputc('\n', stdout); 3340Sstevel@tonic-gate } else if (isdoor) { 3350Sstevel@tonic-gate (void) printf(" "); 3360Sstevel@tonic-gate show_door(Pr, fd); 3370Sstevel@tonic-gate (void) fputc('\n', stdout); 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate flock.l_type = F_WRLCK; 3410Sstevel@tonic-gate flock.l_whence = 0; 3420Sstevel@tonic-gate flock.l_start = 0; 3430Sstevel@tonic-gate flock.l_len = 0; 3440Sstevel@tonic-gate flock.l_sysid = 0; 3450Sstevel@tonic-gate flock.l_pid = 0; 3460Sstevel@tonic-gate if (pr_fcntl(Pr, fd, F_GETLK, &flock) != -1) { 3470Sstevel@tonic-gate if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) { 3480Sstevel@tonic-gate unsigned long sysid = flock.l_sysid; 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate (void) printf(" %s %s lock set by", 3510Sstevel@tonic-gate manditory? "manditory" : "advisory", 3520Sstevel@tonic-gate flock.l_type == F_RDLCK? "read" : "write"); 3530Sstevel@tonic-gate if (sysid) 3540Sstevel@tonic-gate (void) printf(" system 0x%lX", sysid); 3550Sstevel@tonic-gate if (flock.l_pid) 3560Sstevel@tonic-gate (void) printf(" process %d", (int)flock.l_pid); 3570Sstevel@tonic-gate (void) fputc('\n', stdout); 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate #ifdef O_PRIV 3630Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 3640Sstevel@tonic-gate O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 3650Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 3660Sstevel@tonic-gate #else 3670Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 3680Sstevel@tonic-gate O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 3690Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 3700Sstevel@tonic-gate #endif 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate static void 3730Sstevel@tonic-gate show_fileflags(int flags) 3740Sstevel@tonic-gate { 3750Sstevel@tonic-gate char buffer[136]; 3760Sstevel@tonic-gate char *str = buffer; 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate switch (flags & O_ACCMODE) { 3790Sstevel@tonic-gate case O_RDONLY: 3800Sstevel@tonic-gate (void) strcpy(str, "O_RDONLY"); 3810Sstevel@tonic-gate break; 3820Sstevel@tonic-gate case O_WRONLY: 3830Sstevel@tonic-gate (void) strcpy(str, "O_WRONLY"); 3840Sstevel@tonic-gate break; 3850Sstevel@tonic-gate case O_RDWR: 3860Sstevel@tonic-gate (void) strcpy(str, "O_RDWR"); 3870Sstevel@tonic-gate break; 3880Sstevel@tonic-gate default: 3890Sstevel@tonic-gate (void) sprintf(str, "0x%x", flags & O_ACCMODE); 3900Sstevel@tonic-gate break; 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate if (flags & O_NDELAY) 3940Sstevel@tonic-gate (void) strcat(str, "|O_NDELAY"); 3950Sstevel@tonic-gate if (flags & O_NONBLOCK) 3960Sstevel@tonic-gate (void) strcat(str, "|O_NONBLOCK"); 3970Sstevel@tonic-gate if (flags & O_APPEND) 3980Sstevel@tonic-gate (void) strcat(str, "|O_APPEND"); 3990Sstevel@tonic-gate #ifdef O_PRIV 4000Sstevel@tonic-gate if (flags & O_PRIV) 4010Sstevel@tonic-gate (void) strcat(str, "|O_PRIV"); 4020Sstevel@tonic-gate #endif 4030Sstevel@tonic-gate if (flags & O_SYNC) 4040Sstevel@tonic-gate (void) strcat(str, "|O_SYNC"); 4050Sstevel@tonic-gate if (flags & O_DSYNC) 4060Sstevel@tonic-gate (void) strcat(str, "|O_DSYNC"); 4070Sstevel@tonic-gate if (flags & O_RSYNC) 4080Sstevel@tonic-gate (void) strcat(str, "|O_RSYNC"); 4090Sstevel@tonic-gate if (flags & O_CREAT) 4100Sstevel@tonic-gate (void) strcat(str, "|O_CREAT"); 4110Sstevel@tonic-gate if (flags & O_TRUNC) 4120Sstevel@tonic-gate (void) strcat(str, "|O_TRUNC"); 4130Sstevel@tonic-gate if (flags & O_EXCL) 4140Sstevel@tonic-gate (void) strcat(str, "|O_EXCL"); 4150Sstevel@tonic-gate if (flags & O_NOCTTY) 4160Sstevel@tonic-gate (void) strcat(str, "|O_NOCTTY"); 4170Sstevel@tonic-gate if (flags & O_LARGEFILE) 4180Sstevel@tonic-gate (void) strcat(str, "|O_LARGEFILE"); 4190Sstevel@tonic-gate if (flags & O_XATTR) 4200Sstevel@tonic-gate (void) strcat(str, "|O_XATTR"); 4210Sstevel@tonic-gate if (flags & ~(ALL_O_FLAGS)) 4220Sstevel@tonic-gate (void) sprintf(str + strlen(str), "|0x%x", 4230Sstevel@tonic-gate flags & ~(ALL_O_FLAGS)); 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate (void) printf("%s", str); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate /* show door info */ 4290Sstevel@tonic-gate static void 4300Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd) 4310Sstevel@tonic-gate { 4320Sstevel@tonic-gate door_info_t door_info; 4330Sstevel@tonic-gate psinfo_t psinfo; 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate if (pr_door_info(Pr, fd, &door_info) != 0) 4360Sstevel@tonic-gate return; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate if (proc_get_psinfo(door_info.di_target, &psinfo) != 0) 4390Sstevel@tonic-gate psinfo.pr_fname[0] = '\0'; 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate (void) printf(" door to "); 4420Sstevel@tonic-gate if (psinfo.pr_fname[0] != '\0') 4430Sstevel@tonic-gate (void) printf("%s[%d]", psinfo.pr_fname, 4440Sstevel@tonic-gate (int)door_info.di_target); 4450Sstevel@tonic-gate else 4460Sstevel@tonic-gate (void) printf("pid %d", (int)door_info.di_target); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate static void 4500Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len) 4510Sstevel@tonic-gate { 4520Sstevel@tonic-gate /* LINTED pointer assignment */ 4530Sstevel@tonic-gate struct sockaddr_in *so_in = (struct sockaddr_in *)sa; 4540Sstevel@tonic-gate /* LINTED pointer assignment */ 4550Sstevel@tonic-gate struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)sa; 4560Sstevel@tonic-gate struct sockaddr_un *so_un = (struct sockaddr_un *)sa; 4570Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 4580Sstevel@tonic-gate const char *p; 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate switch (sa->sa_family) { 4610Sstevel@tonic-gate default: 4620Sstevel@tonic-gate return; 4630Sstevel@tonic-gate case AF_INET: 4641548Srshoaib (void) printf("\t%s: AF_INET %s port: %u\n", 4650Sstevel@tonic-gate str, 4660Sstevel@tonic-gate inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)), 4670Sstevel@tonic-gate ntohs(so_in->sin_port)); 4680Sstevel@tonic-gate return; 4690Sstevel@tonic-gate case AF_INET6: 4700Sstevel@tonic-gate (void) printf("\t%s: AF_INET6 %s port: %u\n", 4710Sstevel@tonic-gate str, 4720Sstevel@tonic-gate inet_ntop(AF_INET6, &so_in6->sin6_addr, 4730Sstevel@tonic-gate abuf, sizeof (abuf)), 4740Sstevel@tonic-gate ntohs(so_in->sin_port)); 4750Sstevel@tonic-gate return; 4760Sstevel@tonic-gate case AF_UNIX: 4770Sstevel@tonic-gate if (len >= sizeof (so_un->sun_family)) { 4780Sstevel@tonic-gate /* Null terminate */ 4790Sstevel@tonic-gate len -= sizeof (so_un->sun_family); 4800Sstevel@tonic-gate so_un->sun_path[len] = NULL; 4810Sstevel@tonic-gate (void) printf("\t%s: AF_UNIX %s\n", 4820Sstevel@tonic-gate str, so_un->sun_path); 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate return; 4850Sstevel@tonic-gate case AF_IMPLINK: p = "AF_IMPLINK"; break; 4860Sstevel@tonic-gate case AF_PUP: p = "AF_PUP"; break; 4870Sstevel@tonic-gate case AF_CHAOS: p = "AF_CHAOS"; break; 4880Sstevel@tonic-gate case AF_NS: p = "AF_NS"; break; 4890Sstevel@tonic-gate case AF_NBS: p = "AF_NBS"; break; 4900Sstevel@tonic-gate case AF_ECMA: p = "AF_ECMA"; break; 4910Sstevel@tonic-gate case AF_DATAKIT: p = "AF_DATAKIT"; break; 4920Sstevel@tonic-gate case AF_CCITT: p = "AF_CCITT"; break; 4930Sstevel@tonic-gate case AF_SNA: p = "AF_SNA"; break; 4940Sstevel@tonic-gate case AF_DECnet: p = "AF_DECnet"; break; 4950Sstevel@tonic-gate case AF_DLI: p = "AF_DLI"; break; 4960Sstevel@tonic-gate case AF_LAT: p = "AF_LAT"; break; 4970Sstevel@tonic-gate case AF_HYLINK: p = "AF_HYLINK"; break; 4980Sstevel@tonic-gate case AF_APPLETALK: p = "AF_APPLETALK"; break; 4990Sstevel@tonic-gate case AF_NIT: p = "AF_NIT"; break; 5000Sstevel@tonic-gate case AF_802: p = "AF_802"; break; 5010Sstevel@tonic-gate case AF_OSI: p = "AF_OSI"; break; 5020Sstevel@tonic-gate case AF_X25: p = "AF_X25"; break; 5030Sstevel@tonic-gate case AF_OSINET: p = "AF_OSINET"; break; 5040Sstevel@tonic-gate case AF_GOSIP: p = "AF_GOSIP"; break; 5050Sstevel@tonic-gate case AF_IPX: p = "AF_IPX"; break; 5060Sstevel@tonic-gate case AF_ROUTE: p = "AF_ROUTE"; break; 5070Sstevel@tonic-gate case AF_LINK: p = "AF_LINK"; break; 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate (void) printf("\t%s: %s\n", str, p); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate static void 5140Sstevel@tonic-gate show_socktype(uint_t type) 5150Sstevel@tonic-gate { 5160Sstevel@tonic-gate static const char *types[] = { 5170Sstevel@tonic-gate NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET" 5180Sstevel@tonic-gate }; 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate if (type < sizeof (types) / sizeof (*types) && types[type] != NULL) 5210Sstevel@tonic-gate (void) printf("\tSOCK_%s\n", types[type]); 5220Sstevel@tonic-gate else 5230Sstevel@tonic-gate (void) printf("\tunknown socket type %u\n", type); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5261095Spriyanka #define BUFSIZE 200 5270Sstevel@tonic-gate static void 5280Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd) 5290Sstevel@tonic-gate { 5300Sstevel@tonic-gate int val, vlen; 5311095Spriyanka char buf[BUFSIZE]; 5320Sstevel@tonic-gate char buf1[32]; 5331095Spriyanka char ipaddr[INET_ADDRSTRLEN]; 5340Sstevel@tonic-gate int i; 5351095Spriyanka in_addr_t nexthop_val; 5360Sstevel@tonic-gate struct boolopt { 5370Sstevel@tonic-gate int opt; 5380Sstevel@tonic-gate const char *name; 5390Sstevel@tonic-gate }; 5400Sstevel@tonic-gate static struct boolopt boolopts[] = { 5410Sstevel@tonic-gate { SO_DEBUG, "SO_DEBUG," }, 5420Sstevel@tonic-gate { SO_REUSEADDR, "SO_REUSEADDR," }, 5430Sstevel@tonic-gate { SO_KEEPALIVE, "SO_KEEPALIVE," }, 5440Sstevel@tonic-gate { SO_DONTROUTE, "SO_DONTROUTE," }, 5450Sstevel@tonic-gate { SO_BROADCAST, "SO_BROADCAST," }, 5460Sstevel@tonic-gate { SO_OOBINLINE, "SO_OOBINLINE," }, 5470Sstevel@tonic-gate { SO_DGRAM_ERRIND, "SO_DGRAM_ERRIND,"} 5480Sstevel@tonic-gate }; 5490Sstevel@tonic-gate struct linger l; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate buf[0] = ','; 5520Sstevel@tonic-gate buf[1] = '\0'; 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) { 5550Sstevel@tonic-gate vlen = sizeof (val); 5560Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, boolopts[i].opt, &val, 5570Sstevel@tonic-gate &vlen) == 0 && val != 0) 5580Sstevel@tonic-gate (void) strlcat(buf, boolopts[i].name, sizeof (buf)); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate vlen = sizeof (l); 5620Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 && 5630Sstevel@tonic-gate l.l_onoff != 0) { 5640Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),", 5650Sstevel@tonic-gate l.l_linger); 5660Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate vlen = sizeof (val); 5700Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) { 5710Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val); 5720Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate vlen = sizeof (val); 5750Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) { 5760Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val); 5770Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 5780Sstevel@tonic-gate } 5791095Spriyanka vlen = sizeof (nexthop_val); 5801095Spriyanka if (pr_getsockopt(Pr, fd, IPPROTO_IP, IP_NEXTHOP, &nexthop_val, 5811095Spriyanka &vlen) == 0) { 582*1628Spriyanka if (vlen > 0) { 583*1628Spriyanka (void) inet_ntop(AF_INET, (void *) &nexthop_val, 584*1628Spriyanka ipaddr, sizeof (ipaddr)); 585*1628Spriyanka (void) snprintf(buf1, sizeof (buf1), "IP_NEXTHOP(%s),", 586*1628Spriyanka ipaddr); 587*1628Spriyanka (void) strlcat(buf, buf1, sizeof (buf)); 588*1628Spriyanka } 5891095Spriyanka } 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate buf[strlen(buf) - 1] = '\0'; 5920Sstevel@tonic-gate if (buf[1] != '\0') 5930Sstevel@tonic-gate (void) printf("\t%s\n", buf+1); 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate /* the file is a socket */ 5970Sstevel@tonic-gate static void 5980Sstevel@tonic-gate dosocket(struct ps_prochandle *Pr, int fd) 5990Sstevel@tonic-gate { 6000Sstevel@tonic-gate /* A buffer large enough for PATH_MAX size AF_UNIX address */ 6010Sstevel@tonic-gate long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1) 6020Sstevel@tonic-gate / sizeof (long)]; 6030Sstevel@tonic-gate struct sockaddr *sa = (struct sockaddr *)buf; 6040Sstevel@tonic-gate socklen_t len; 6050Sstevel@tonic-gate int type, tlen; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate tlen = sizeof (type); 6080Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) 6090Sstevel@tonic-gate == 0) 6100Sstevel@tonic-gate show_socktype((uint_t)type); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate show_sockopts(Pr, fd); 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate len = sizeof (buf); 6150Sstevel@tonic-gate if (pr_getsockname(Pr, fd, sa, &len) == 0) 6160Sstevel@tonic-gate show_sockaddr("sockname", sa, len); 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate len = sizeof (buf); 6190Sstevel@tonic-gate if (pr_getpeername(Pr, fd, sa, &len) == 0) 6200Sstevel@tonic-gate show_sockaddr("peername", sa, len); 6210Sstevel@tonic-gate } 622