1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <stdio.h> 30*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 31*7c478bd9Sstevel@tonic-gate #include <unistd.h> 32*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 33*7c478bd9Sstevel@tonic-gate #include <ctype.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <signal.h> 36*7c478bd9Sstevel@tonic-gate #include <errno.h> 37*7c478bd9Sstevel@tonic-gate #include <dirent.h> 38*7c478bd9Sstevel@tonic-gate #include <limits.h> 39*7c478bd9Sstevel@tonic-gate #include <door.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/mman.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/un.h> 45*7c478bd9Sstevel@tonic-gate #include <netdb.h> 46*7c478bd9Sstevel@tonic-gate #include <libproc.h> 47*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 48*7c478bd9Sstevel@tonic-gate #include <netdb.h> 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate static char *command; 51*7c478bd9Sstevel@tonic-gate static volatile int interrupt; 52*7c478bd9Sstevel@tonic-gate static int Fflag; 53*7c478bd9Sstevel@tonic-gate static boolean_t nflag = B_FALSE; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate static void intr(int); 56*7c478bd9Sstevel@tonic-gate static void dofcntl(struct ps_prochandle *, int, int, int); 57*7c478bd9Sstevel@tonic-gate static void dosocket(struct ps_prochandle *, int); 58*7c478bd9Sstevel@tonic-gate static void show_files(struct ps_prochandle *); 59*7c478bd9Sstevel@tonic-gate static void show_fileflags(int); 60*7c478bd9Sstevel@tonic-gate static void show_door(struct ps_prochandle *, int); 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate int 63*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 64*7c478bd9Sstevel@tonic-gate { 65*7c478bd9Sstevel@tonic-gate int retc = 0; 66*7c478bd9Sstevel@tonic-gate int opt; 67*7c478bd9Sstevel@tonic-gate int errflg = 0; 68*7c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL) 71*7c478bd9Sstevel@tonic-gate command++; 72*7c478bd9Sstevel@tonic-gate else 73*7c478bd9Sstevel@tonic-gate command = argv[0]; 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* options */ 76*7c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "Fn")) != EOF) { 77*7c478bd9Sstevel@tonic-gate switch (opt) { 78*7c478bd9Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */ 79*7c478bd9Sstevel@tonic-gate Fflag = PGRAB_FORCE; 80*7c478bd9Sstevel@tonic-gate break; 81*7c478bd9Sstevel@tonic-gate case 'n': 82*7c478bd9Sstevel@tonic-gate nflag = B_TRUE; 83*7c478bd9Sstevel@tonic-gate break; 84*7c478bd9Sstevel@tonic-gate default: 85*7c478bd9Sstevel@tonic-gate errflg = 1; 86*7c478bd9Sstevel@tonic-gate break; 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate argc -= optind; 91*7c478bd9Sstevel@tonic-gate argv += optind; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate if (errflg || argc <= 0) { 94*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "usage:\t%s [-F] pid ...\n", 95*7c478bd9Sstevel@tonic-gate command); 96*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 97*7c478bd9Sstevel@tonic-gate " (report open files of each process)\n"); 98*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 99*7c478bd9Sstevel@tonic-gate " -F: force grabbing of the target process\n"); 100*7c478bd9Sstevel@tonic-gate exit(2); 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate /* catch signals from terminal */ 104*7c478bd9Sstevel@tonic-gate if (sigset(SIGHUP, SIG_IGN) == SIG_DFL) 105*7c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, intr); 106*7c478bd9Sstevel@tonic-gate if (sigset(SIGINT, SIG_IGN) == SIG_DFL) 107*7c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, intr); 108*7c478bd9Sstevel@tonic-gate if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL) 109*7c478bd9Sstevel@tonic-gate (void) sigset(SIGQUIT, intr); 110*7c478bd9Sstevel@tonic-gate (void) sigset(SIGPIPE, intr); 111*7c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, intr); 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate (void) proc_initstdio(); 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate while (--argc >= 0 && !interrupt) { 117*7c478bd9Sstevel@tonic-gate char *arg; 118*7c478bd9Sstevel@tonic-gate psinfo_t psinfo; 119*7c478bd9Sstevel@tonic-gate pid_t pid; 120*7c478bd9Sstevel@tonic-gate int gret; 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate (void) proc_flushstdio(); 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate /* get the specified pid and the psinfo struct */ 125*7c478bd9Sstevel@tonic-gate if ((pid = proc_arg_psinfo(arg = *argv++, PR_ARG_PIDS, 126*7c478bd9Sstevel@tonic-gate &psinfo, &gret)) == -1) { 127*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n", 128*7c478bd9Sstevel@tonic-gate command, arg, Pgrab_error(gret)); 129*7c478bd9Sstevel@tonic-gate retc++; 130*7c478bd9Sstevel@tonic-gate } else if ((Pr = Pgrab(pid, Fflag, &gret)) != NULL) { 131*7c478bd9Sstevel@tonic-gate if (Pcreate_agent(Pr) == 0) { 132*7c478bd9Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 133*7c478bd9Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", 134*7c478bd9Sstevel@tonic-gate (int)pid, psinfo.pr_psargs); 135*7c478bd9Sstevel@tonic-gate show_files(Pr); 136*7c478bd9Sstevel@tonic-gate Pdestroy_agent(Pr); 137*7c478bd9Sstevel@tonic-gate } else { 138*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 139*7c478bd9Sstevel@tonic-gate "%s: cannot control process %d\n", 140*7c478bd9Sstevel@tonic-gate command, (int)pid); 141*7c478bd9Sstevel@tonic-gate retc++; 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate Prelease(Pr, 0); 144*7c478bd9Sstevel@tonic-gate Pr = NULL; 145*7c478bd9Sstevel@tonic-gate } else { 146*7c478bd9Sstevel@tonic-gate switch (gret) { 147*7c478bd9Sstevel@tonic-gate case G_SYS: 148*7c478bd9Sstevel@tonic-gate case G_SELF: 149*7c478bd9Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 150*7c478bd9Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", (int)pid, 151*7c478bd9Sstevel@tonic-gate psinfo.pr_psargs); 152*7c478bd9Sstevel@tonic-gate if (gret == G_SYS) 153*7c478bd9Sstevel@tonic-gate (void) printf(" [system process]\n"); 154*7c478bd9Sstevel@tonic-gate else 155*7c478bd9Sstevel@tonic-gate show_files(NULL); 156*7c478bd9Sstevel@tonic-gate break; 157*7c478bd9Sstevel@tonic-gate default: 158*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %d\n", 159*7c478bd9Sstevel@tonic-gate command, Pgrab_error(gret), (int)pid); 160*7c478bd9Sstevel@tonic-gate retc++; 161*7c478bd9Sstevel@tonic-gate break; 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate (void) proc_finistdio(); 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate if (interrupt && retc == 0) 171*7c478bd9Sstevel@tonic-gate retc++; 172*7c478bd9Sstevel@tonic-gate return (retc); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 176*7c478bd9Sstevel@tonic-gate static void 177*7c478bd9Sstevel@tonic-gate intr(int sig) 178*7c478bd9Sstevel@tonic-gate { 179*7c478bd9Sstevel@tonic-gate interrupt = 1; 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate /* ------ begin specific code ------ */ 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate static void 185*7c478bd9Sstevel@tonic-gate show_files(struct ps_prochandle *Pr) 186*7c478bd9Sstevel@tonic-gate { 187*7c478bd9Sstevel@tonic-gate DIR *dirp; 188*7c478bd9Sstevel@tonic-gate struct dirent *dentp; 189*7c478bd9Sstevel@tonic-gate char pname[100]; 190*7c478bd9Sstevel@tonic-gate char fname[PATH_MAX]; 191*7c478bd9Sstevel@tonic-gate struct stat64 statb; 192*7c478bd9Sstevel@tonic-gate struct rlimit rlim; 193*7c478bd9Sstevel@tonic-gate pid_t pid; 194*7c478bd9Sstevel@tonic-gate int fd; 195*7c478bd9Sstevel@tonic-gate char *s; 196*7c478bd9Sstevel@tonic-gate int ret; 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate if (pr_getrlimit(Pr, RLIMIT_NOFILE, &rlim) == 0) { 199*7c478bd9Sstevel@tonic-gate ulong_t nfd = rlim.rlim_cur; 200*7c478bd9Sstevel@tonic-gate if (nfd == RLIM_INFINITY) 201*7c478bd9Sstevel@tonic-gate (void) printf( 202*7c478bd9Sstevel@tonic-gate " Current rlimit: unlimited file descriptors\n"); 203*7c478bd9Sstevel@tonic-gate else 204*7c478bd9Sstevel@tonic-gate (void) printf( 205*7c478bd9Sstevel@tonic-gate " Current rlimit: %lu file descriptors\n", nfd); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate /* in case we are doing this to ourself */ 209*7c478bd9Sstevel@tonic-gate pid = (Pr == NULL)? getpid() : Pstatus(Pr)->pr_pid; 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate (void) sprintf(pname, "/proc/%d/fd", (int)pid); 212*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(pname)) == NULL) { 213*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open directory %s\n", 214*7c478bd9Sstevel@tonic-gate command, pname); 215*7c478bd9Sstevel@tonic-gate return; 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate /* for each open file --- */ 219*7c478bd9Sstevel@tonic-gate while ((dentp = readdir(dirp)) != NULL && !interrupt) { 220*7c478bd9Sstevel@tonic-gate char unknown[12]; 221*7c478bd9Sstevel@tonic-gate dev_t rdev; 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate /* skip '.' and '..' */ 224*7c478bd9Sstevel@tonic-gate if (!isdigit(dentp->d_name[0])) 225*7c478bd9Sstevel@tonic-gate continue; 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate fd = atoi(dentp->d_name); 228*7c478bd9Sstevel@tonic-gate if (pr_fstat64(Pr, fd, &statb) == -1) { 229*7c478bd9Sstevel@tonic-gate s = unknown; 230*7c478bd9Sstevel@tonic-gate (void) sprintf(s, "%4d", fd); 231*7c478bd9Sstevel@tonic-gate perror(s); 232*7c478bd9Sstevel@tonic-gate continue; 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate rdev = NODEV; 236*7c478bd9Sstevel@tonic-gate switch (statb.st_mode & S_IFMT) { 237*7c478bd9Sstevel@tonic-gate case S_IFCHR: s = "S_IFCHR"; rdev = statb.st_rdev; break; 238*7c478bd9Sstevel@tonic-gate case S_IFBLK: s = "S_IFBLK"; rdev = statb.st_rdev; break; 239*7c478bd9Sstevel@tonic-gate case S_IFIFO: s = "S_IFIFO"; break; 240*7c478bd9Sstevel@tonic-gate case S_IFDIR: s = "S_IFDIR"; break; 241*7c478bd9Sstevel@tonic-gate case S_IFREG: s = "S_IFREG"; break; 242*7c478bd9Sstevel@tonic-gate case S_IFLNK: s = "S_IFLNK"; break; 243*7c478bd9Sstevel@tonic-gate case S_IFSOCK: s = "S_IFSOCK"; break; 244*7c478bd9Sstevel@tonic-gate case S_IFDOOR: s = "S_IFDOOR"; break; 245*7c478bd9Sstevel@tonic-gate case S_IFPORT: s = "S_IFPORT"; break; 246*7c478bd9Sstevel@tonic-gate default: 247*7c478bd9Sstevel@tonic-gate s = unknown; 248*7c478bd9Sstevel@tonic-gate (void) sprintf(s, "0x%.4x ", 249*7c478bd9Sstevel@tonic-gate (int)statb.st_mode & S_IFMT); 250*7c478bd9Sstevel@tonic-gate break; 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate (void) printf("%4d: %s mode:0%.3o", 254*7c478bd9Sstevel@tonic-gate fd, 255*7c478bd9Sstevel@tonic-gate s, 256*7c478bd9Sstevel@tonic-gate (int)statb.st_mode & ~S_IFMT); 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate if (major(statb.st_dev) != (major_t)NODEV && 259*7c478bd9Sstevel@tonic-gate minor(statb.st_dev) != (minor_t)NODEV) 260*7c478bd9Sstevel@tonic-gate (void) printf(" dev:%lu,%lu", 261*7c478bd9Sstevel@tonic-gate (ulong_t)major(statb.st_dev), 262*7c478bd9Sstevel@tonic-gate (ulong_t)minor(statb.st_dev)); 263*7c478bd9Sstevel@tonic-gate else 264*7c478bd9Sstevel@tonic-gate (void) printf(" dev:0x%.8lX", (long)statb.st_dev); 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFPORT) { 267*7c478bd9Sstevel@tonic-gate (void) printf(" uid:%d gid:%d", 268*7c478bd9Sstevel@tonic-gate (int)statb.st_uid, 269*7c478bd9Sstevel@tonic-gate (int)statb.st_gid); 270*7c478bd9Sstevel@tonic-gate (void) printf(" size:%lld\n", 271*7c478bd9Sstevel@tonic-gate (longlong_t)statb.st_size); 272*7c478bd9Sstevel@tonic-gate continue; 273*7c478bd9Sstevel@tonic-gate } 274*7c478bd9Sstevel@tonic-gate 275*7c478bd9Sstevel@tonic-gate (void) printf(" ino:%llu uid:%d gid:%d", 276*7c478bd9Sstevel@tonic-gate (u_longlong_t)statb.st_ino, 277*7c478bd9Sstevel@tonic-gate (int)statb.st_uid, 278*7c478bd9Sstevel@tonic-gate (int)statb.st_gid); 279*7c478bd9Sstevel@tonic-gate 280*7c478bd9Sstevel@tonic-gate if (rdev == NODEV) 281*7c478bd9Sstevel@tonic-gate (void) printf(" size:%lld\n", 282*7c478bd9Sstevel@tonic-gate (longlong_t)statb.st_size); 283*7c478bd9Sstevel@tonic-gate else if (major(rdev) != (major_t)NODEV && 284*7c478bd9Sstevel@tonic-gate minor(rdev) != (minor_t)NODEV) 285*7c478bd9Sstevel@tonic-gate (void) printf(" rdev:%lu,%lu\n", 286*7c478bd9Sstevel@tonic-gate (ulong_t)major(rdev), 287*7c478bd9Sstevel@tonic-gate (ulong_t)minor(rdev)); 288*7c478bd9Sstevel@tonic-gate else 289*7c478bd9Sstevel@tonic-gate (void) printf(" rdev:0x%.8lX\n", (long)rdev); 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate if (!nflag) { 292*7c478bd9Sstevel@tonic-gate dofcntl(Pr, fd, 293*7c478bd9Sstevel@tonic-gate (statb.st_mode & (S_IFMT|S_ENFMT|S_IXGRP)) 294*7c478bd9Sstevel@tonic-gate == (S_IFREG|S_ENFMT), 295*7c478bd9Sstevel@tonic-gate (statb.st_mode & S_IFMT) == S_IFDOOR); 296*7c478bd9Sstevel@tonic-gate 297*7c478bd9Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFSOCK) 298*7c478bd9Sstevel@tonic-gate dosocket(Pr, fd); 299*7c478bd9Sstevel@tonic-gate 300*7c478bd9Sstevel@tonic-gate (void) sprintf(pname, "/proc/%d/path/%d", (int)pid, fd); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate if ((ret = readlink(pname, fname, PATH_MAX - 1)) > 0) { 303*7c478bd9Sstevel@tonic-gate fname[ret] = '\0'; 304*7c478bd9Sstevel@tonic-gate (void) printf(" %s\n", fname); 305*7c478bd9Sstevel@tonic-gate } 306*7c478bd9Sstevel@tonic-gate } 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 310*7c478bd9Sstevel@tonic-gate } 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate /* examine open file with fcntl() */ 313*7c478bd9Sstevel@tonic-gate static void 314*7c478bd9Sstevel@tonic-gate dofcntl(struct ps_prochandle *Pr, int fd, int manditory, int isdoor) 315*7c478bd9Sstevel@tonic-gate { 316*7c478bd9Sstevel@tonic-gate struct flock flock; 317*7c478bd9Sstevel@tonic-gate int fileflags; 318*7c478bd9Sstevel@tonic-gate int fdflags; 319*7c478bd9Sstevel@tonic-gate 320*7c478bd9Sstevel@tonic-gate fileflags = pr_fcntl(Pr, fd, F_GETXFL, 0); 321*7c478bd9Sstevel@tonic-gate fdflags = pr_fcntl(Pr, fd, F_GETFD, 0); 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate if (fileflags != -1 || fdflags != -1) { 324*7c478bd9Sstevel@tonic-gate (void) printf(" "); 325*7c478bd9Sstevel@tonic-gate if (fileflags != -1) 326*7c478bd9Sstevel@tonic-gate show_fileflags(fileflags); 327*7c478bd9Sstevel@tonic-gate if (fdflags != -1 && (fdflags & FD_CLOEXEC)) 328*7c478bd9Sstevel@tonic-gate (void) printf(" FD_CLOEXEC"); 329*7c478bd9Sstevel@tonic-gate if (isdoor) 330*7c478bd9Sstevel@tonic-gate show_door(Pr, fd); 331*7c478bd9Sstevel@tonic-gate (void) fputc('\n', stdout); 332*7c478bd9Sstevel@tonic-gate } else if (isdoor) { 333*7c478bd9Sstevel@tonic-gate (void) printf(" "); 334*7c478bd9Sstevel@tonic-gate show_door(Pr, fd); 335*7c478bd9Sstevel@tonic-gate (void) fputc('\n', stdout); 336*7c478bd9Sstevel@tonic-gate } 337*7c478bd9Sstevel@tonic-gate 338*7c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 339*7c478bd9Sstevel@tonic-gate flock.l_whence = 0; 340*7c478bd9Sstevel@tonic-gate flock.l_start = 0; 341*7c478bd9Sstevel@tonic-gate flock.l_len = 0; 342*7c478bd9Sstevel@tonic-gate flock.l_sysid = 0; 343*7c478bd9Sstevel@tonic-gate flock.l_pid = 0; 344*7c478bd9Sstevel@tonic-gate if (pr_fcntl(Pr, fd, F_GETLK, &flock) != -1) { 345*7c478bd9Sstevel@tonic-gate if (flock.l_type != F_UNLCK && (flock.l_sysid || flock.l_pid)) { 346*7c478bd9Sstevel@tonic-gate unsigned long sysid = flock.l_sysid; 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate (void) printf(" %s %s lock set by", 349*7c478bd9Sstevel@tonic-gate manditory? "manditory" : "advisory", 350*7c478bd9Sstevel@tonic-gate flock.l_type == F_RDLCK? "read" : "write"); 351*7c478bd9Sstevel@tonic-gate if (sysid) 352*7c478bd9Sstevel@tonic-gate (void) printf(" system 0x%lX", sysid); 353*7c478bd9Sstevel@tonic-gate if (flock.l_pid) 354*7c478bd9Sstevel@tonic-gate (void) printf(" process %d", (int)flock.l_pid); 355*7c478bd9Sstevel@tonic-gate (void) fputc('\n', stdout); 356*7c478bd9Sstevel@tonic-gate } 357*7c478bd9Sstevel@tonic-gate } 358*7c478bd9Sstevel@tonic-gate } 359*7c478bd9Sstevel@tonic-gate 360*7c478bd9Sstevel@tonic-gate #ifdef O_PRIV 361*7c478bd9Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 362*7c478bd9Sstevel@tonic-gate O_PRIV | O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 363*7c478bd9Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 364*7c478bd9Sstevel@tonic-gate #else 365*7c478bd9Sstevel@tonic-gate #define ALL_O_FLAGS O_ACCMODE | O_NDELAY | O_NONBLOCK | O_APPEND | \ 366*7c478bd9Sstevel@tonic-gate O_SYNC | O_DSYNC | O_RSYNC | O_XATTR | \ 367*7c478bd9Sstevel@tonic-gate O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_LARGEFILE 368*7c478bd9Sstevel@tonic-gate #endif 369*7c478bd9Sstevel@tonic-gate 370*7c478bd9Sstevel@tonic-gate static void 371*7c478bd9Sstevel@tonic-gate show_fileflags(int flags) 372*7c478bd9Sstevel@tonic-gate { 373*7c478bd9Sstevel@tonic-gate char buffer[136]; 374*7c478bd9Sstevel@tonic-gate char *str = buffer; 375*7c478bd9Sstevel@tonic-gate 376*7c478bd9Sstevel@tonic-gate switch (flags & O_ACCMODE) { 377*7c478bd9Sstevel@tonic-gate case O_RDONLY: 378*7c478bd9Sstevel@tonic-gate (void) strcpy(str, "O_RDONLY"); 379*7c478bd9Sstevel@tonic-gate break; 380*7c478bd9Sstevel@tonic-gate case O_WRONLY: 381*7c478bd9Sstevel@tonic-gate (void) strcpy(str, "O_WRONLY"); 382*7c478bd9Sstevel@tonic-gate break; 383*7c478bd9Sstevel@tonic-gate case O_RDWR: 384*7c478bd9Sstevel@tonic-gate (void) strcpy(str, "O_RDWR"); 385*7c478bd9Sstevel@tonic-gate break; 386*7c478bd9Sstevel@tonic-gate default: 387*7c478bd9Sstevel@tonic-gate (void) sprintf(str, "0x%x", flags & O_ACCMODE); 388*7c478bd9Sstevel@tonic-gate break; 389*7c478bd9Sstevel@tonic-gate } 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate if (flags & O_NDELAY) 392*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_NDELAY"); 393*7c478bd9Sstevel@tonic-gate if (flags & O_NONBLOCK) 394*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_NONBLOCK"); 395*7c478bd9Sstevel@tonic-gate if (flags & O_APPEND) 396*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_APPEND"); 397*7c478bd9Sstevel@tonic-gate #ifdef O_PRIV 398*7c478bd9Sstevel@tonic-gate if (flags & O_PRIV) 399*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_PRIV"); 400*7c478bd9Sstevel@tonic-gate #endif 401*7c478bd9Sstevel@tonic-gate if (flags & O_SYNC) 402*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_SYNC"); 403*7c478bd9Sstevel@tonic-gate if (flags & O_DSYNC) 404*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_DSYNC"); 405*7c478bd9Sstevel@tonic-gate if (flags & O_RSYNC) 406*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_RSYNC"); 407*7c478bd9Sstevel@tonic-gate if (flags & O_CREAT) 408*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_CREAT"); 409*7c478bd9Sstevel@tonic-gate if (flags & O_TRUNC) 410*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_TRUNC"); 411*7c478bd9Sstevel@tonic-gate if (flags & O_EXCL) 412*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_EXCL"); 413*7c478bd9Sstevel@tonic-gate if (flags & O_NOCTTY) 414*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_NOCTTY"); 415*7c478bd9Sstevel@tonic-gate if (flags & O_LARGEFILE) 416*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_LARGEFILE"); 417*7c478bd9Sstevel@tonic-gate if (flags & O_XATTR) 418*7c478bd9Sstevel@tonic-gate (void) strcat(str, "|O_XATTR"); 419*7c478bd9Sstevel@tonic-gate if (flags & ~(ALL_O_FLAGS)) 420*7c478bd9Sstevel@tonic-gate (void) sprintf(str + strlen(str), "|0x%x", 421*7c478bd9Sstevel@tonic-gate flags & ~(ALL_O_FLAGS)); 422*7c478bd9Sstevel@tonic-gate 423*7c478bd9Sstevel@tonic-gate (void) printf("%s", str); 424*7c478bd9Sstevel@tonic-gate } 425*7c478bd9Sstevel@tonic-gate 426*7c478bd9Sstevel@tonic-gate /* show door info */ 427*7c478bd9Sstevel@tonic-gate static void 428*7c478bd9Sstevel@tonic-gate show_door(struct ps_prochandle *Pr, int fd) 429*7c478bd9Sstevel@tonic-gate { 430*7c478bd9Sstevel@tonic-gate door_info_t door_info; 431*7c478bd9Sstevel@tonic-gate psinfo_t psinfo; 432*7c478bd9Sstevel@tonic-gate 433*7c478bd9Sstevel@tonic-gate if (pr_door_info(Pr, fd, &door_info) != 0) 434*7c478bd9Sstevel@tonic-gate return; 435*7c478bd9Sstevel@tonic-gate 436*7c478bd9Sstevel@tonic-gate if (proc_get_psinfo(door_info.di_target, &psinfo) != 0) 437*7c478bd9Sstevel@tonic-gate psinfo.pr_fname[0] = '\0'; 438*7c478bd9Sstevel@tonic-gate 439*7c478bd9Sstevel@tonic-gate (void) printf(" door to "); 440*7c478bd9Sstevel@tonic-gate if (psinfo.pr_fname[0] != '\0') 441*7c478bd9Sstevel@tonic-gate (void) printf("%s[%d]", psinfo.pr_fname, 442*7c478bd9Sstevel@tonic-gate (int)door_info.di_target); 443*7c478bd9Sstevel@tonic-gate else 444*7c478bd9Sstevel@tonic-gate (void) printf("pid %d", (int)door_info.di_target); 445*7c478bd9Sstevel@tonic-gate } 446*7c478bd9Sstevel@tonic-gate 447*7c478bd9Sstevel@tonic-gate static void 448*7c478bd9Sstevel@tonic-gate show_sockaddr(const char *str, struct sockaddr *sa, socklen_t len) 449*7c478bd9Sstevel@tonic-gate { 450*7c478bd9Sstevel@tonic-gate /* LINTED pointer assignment */ 451*7c478bd9Sstevel@tonic-gate struct sockaddr_in *so_in = (struct sockaddr_in *)sa; 452*7c478bd9Sstevel@tonic-gate /* LINTED pointer assignment */ 453*7c478bd9Sstevel@tonic-gate struct sockaddr_in6 *so_in6 = (struct sockaddr_in6 *)sa; 454*7c478bd9Sstevel@tonic-gate struct sockaddr_un *so_un = (struct sockaddr_un *)sa; 455*7c478bd9Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 456*7c478bd9Sstevel@tonic-gate const char *p; 457*7c478bd9Sstevel@tonic-gate 458*7c478bd9Sstevel@tonic-gate switch (sa->sa_family) { 459*7c478bd9Sstevel@tonic-gate default: 460*7c478bd9Sstevel@tonic-gate return; 461*7c478bd9Sstevel@tonic-gate case AF_INET: 462*7c478bd9Sstevel@tonic-gate case AF_NCA: 463*7c478bd9Sstevel@tonic-gate p = (sa->sa_family == AF_INET) ? "AF_INET" : "AF_NCA"; 464*7c478bd9Sstevel@tonic-gate (void) printf("\t%s: %s %s port: %u\n", 465*7c478bd9Sstevel@tonic-gate str, 466*7c478bd9Sstevel@tonic-gate p, 467*7c478bd9Sstevel@tonic-gate inet_ntop(AF_INET, &so_in->sin_addr, abuf, sizeof (abuf)), 468*7c478bd9Sstevel@tonic-gate ntohs(so_in->sin_port)); 469*7c478bd9Sstevel@tonic-gate return; 470*7c478bd9Sstevel@tonic-gate case AF_INET6: 471*7c478bd9Sstevel@tonic-gate (void) printf("\t%s: AF_INET6 %s port: %u\n", 472*7c478bd9Sstevel@tonic-gate str, 473*7c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, &so_in6->sin6_addr, 474*7c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 475*7c478bd9Sstevel@tonic-gate ntohs(so_in->sin_port)); 476*7c478bd9Sstevel@tonic-gate return; 477*7c478bd9Sstevel@tonic-gate case AF_UNIX: 478*7c478bd9Sstevel@tonic-gate if (len >= sizeof (so_un->sun_family)) { 479*7c478bd9Sstevel@tonic-gate /* Null terminate */ 480*7c478bd9Sstevel@tonic-gate len -= sizeof (so_un->sun_family); 481*7c478bd9Sstevel@tonic-gate so_un->sun_path[len] = NULL; 482*7c478bd9Sstevel@tonic-gate (void) printf("\t%s: AF_UNIX %s\n", 483*7c478bd9Sstevel@tonic-gate str, so_un->sun_path); 484*7c478bd9Sstevel@tonic-gate } 485*7c478bd9Sstevel@tonic-gate return; 486*7c478bd9Sstevel@tonic-gate case AF_IMPLINK: p = "AF_IMPLINK"; break; 487*7c478bd9Sstevel@tonic-gate case AF_PUP: p = "AF_PUP"; break; 488*7c478bd9Sstevel@tonic-gate case AF_CHAOS: p = "AF_CHAOS"; break; 489*7c478bd9Sstevel@tonic-gate case AF_NS: p = "AF_NS"; break; 490*7c478bd9Sstevel@tonic-gate case AF_NBS: p = "AF_NBS"; break; 491*7c478bd9Sstevel@tonic-gate case AF_ECMA: p = "AF_ECMA"; break; 492*7c478bd9Sstevel@tonic-gate case AF_DATAKIT: p = "AF_DATAKIT"; break; 493*7c478bd9Sstevel@tonic-gate case AF_CCITT: p = "AF_CCITT"; break; 494*7c478bd9Sstevel@tonic-gate case AF_SNA: p = "AF_SNA"; break; 495*7c478bd9Sstevel@tonic-gate case AF_DECnet: p = "AF_DECnet"; break; 496*7c478bd9Sstevel@tonic-gate case AF_DLI: p = "AF_DLI"; break; 497*7c478bd9Sstevel@tonic-gate case AF_LAT: p = "AF_LAT"; break; 498*7c478bd9Sstevel@tonic-gate case AF_HYLINK: p = "AF_HYLINK"; break; 499*7c478bd9Sstevel@tonic-gate case AF_APPLETALK: p = "AF_APPLETALK"; break; 500*7c478bd9Sstevel@tonic-gate case AF_NIT: p = "AF_NIT"; break; 501*7c478bd9Sstevel@tonic-gate case AF_802: p = "AF_802"; break; 502*7c478bd9Sstevel@tonic-gate case AF_OSI: p = "AF_OSI"; break; 503*7c478bd9Sstevel@tonic-gate case AF_X25: p = "AF_X25"; break; 504*7c478bd9Sstevel@tonic-gate case AF_OSINET: p = "AF_OSINET"; break; 505*7c478bd9Sstevel@tonic-gate case AF_GOSIP: p = "AF_GOSIP"; break; 506*7c478bd9Sstevel@tonic-gate case AF_IPX: p = "AF_IPX"; break; 507*7c478bd9Sstevel@tonic-gate case AF_ROUTE: p = "AF_ROUTE"; break; 508*7c478bd9Sstevel@tonic-gate case AF_LINK: p = "AF_LINK"; break; 509*7c478bd9Sstevel@tonic-gate } 510*7c478bd9Sstevel@tonic-gate 511*7c478bd9Sstevel@tonic-gate (void) printf("\t%s: %s\n", str, p); 512*7c478bd9Sstevel@tonic-gate } 513*7c478bd9Sstevel@tonic-gate 514*7c478bd9Sstevel@tonic-gate static void 515*7c478bd9Sstevel@tonic-gate show_socktype(uint_t type) 516*7c478bd9Sstevel@tonic-gate { 517*7c478bd9Sstevel@tonic-gate static const char *types[] = { 518*7c478bd9Sstevel@tonic-gate NULL, "DGRAM", "STREAM", NULL, "RAW", "RDM", "SEQPACKET" 519*7c478bd9Sstevel@tonic-gate }; 520*7c478bd9Sstevel@tonic-gate 521*7c478bd9Sstevel@tonic-gate if (type < sizeof (types) / sizeof (*types) && types[type] != NULL) 522*7c478bd9Sstevel@tonic-gate (void) printf("\tSOCK_%s\n", types[type]); 523*7c478bd9Sstevel@tonic-gate else 524*7c478bd9Sstevel@tonic-gate (void) printf("\tunknown socket type %u\n", type); 525*7c478bd9Sstevel@tonic-gate } 526*7c478bd9Sstevel@tonic-gate 527*7c478bd9Sstevel@tonic-gate static void 528*7c478bd9Sstevel@tonic-gate show_sockopts(struct ps_prochandle *Pr, int fd) 529*7c478bd9Sstevel@tonic-gate { 530*7c478bd9Sstevel@tonic-gate int val, vlen; 531*7c478bd9Sstevel@tonic-gate char buf[64]; 532*7c478bd9Sstevel@tonic-gate char buf1[32]; 533*7c478bd9Sstevel@tonic-gate int i; 534*7c478bd9Sstevel@tonic-gate struct boolopt { 535*7c478bd9Sstevel@tonic-gate int opt; 536*7c478bd9Sstevel@tonic-gate const char *name; 537*7c478bd9Sstevel@tonic-gate }; 538*7c478bd9Sstevel@tonic-gate static struct boolopt boolopts[] = { 539*7c478bd9Sstevel@tonic-gate { SO_DEBUG, "SO_DEBUG," }, 540*7c478bd9Sstevel@tonic-gate { SO_REUSEADDR, "SO_REUSEADDR," }, 541*7c478bd9Sstevel@tonic-gate { SO_KEEPALIVE, "SO_KEEPALIVE," }, 542*7c478bd9Sstevel@tonic-gate { SO_DONTROUTE, "SO_DONTROUTE," }, 543*7c478bd9Sstevel@tonic-gate { SO_BROADCAST, "SO_BROADCAST," }, 544*7c478bd9Sstevel@tonic-gate { SO_OOBINLINE, "SO_OOBINLINE," }, 545*7c478bd9Sstevel@tonic-gate { SO_DGRAM_ERRIND, "SO_DGRAM_ERRIND,"} 546*7c478bd9Sstevel@tonic-gate }; 547*7c478bd9Sstevel@tonic-gate struct linger l; 548*7c478bd9Sstevel@tonic-gate 549*7c478bd9Sstevel@tonic-gate buf[0] = ','; 550*7c478bd9Sstevel@tonic-gate buf[1] = '\0'; 551*7c478bd9Sstevel@tonic-gate 552*7c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (boolopts) / sizeof (boolopts[0]); i++) { 553*7c478bd9Sstevel@tonic-gate vlen = sizeof (val); 554*7c478bd9Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, boolopts[i].opt, &val, 555*7c478bd9Sstevel@tonic-gate &vlen) == 0 && val != 0) 556*7c478bd9Sstevel@tonic-gate (void) strlcat(buf, boolopts[i].name, sizeof (buf)); 557*7c478bd9Sstevel@tonic-gate } 558*7c478bd9Sstevel@tonic-gate 559*7c478bd9Sstevel@tonic-gate vlen = sizeof (l); 560*7c478bd9Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_LINGER, &l, &vlen) == 0 && 561*7c478bd9Sstevel@tonic-gate l.l_onoff != 0) { 562*7c478bd9Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_LINGER(%d),", 563*7c478bd9Sstevel@tonic-gate l.l_linger); 564*7c478bd9Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 565*7c478bd9Sstevel@tonic-gate } 566*7c478bd9Sstevel@tonic-gate 567*7c478bd9Sstevel@tonic-gate vlen = sizeof (val); 568*7c478bd9Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_SNDBUF, &val, &vlen) == 0) { 569*7c478bd9Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_SNDBUF(%d),", val); 570*7c478bd9Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 571*7c478bd9Sstevel@tonic-gate } 572*7c478bd9Sstevel@tonic-gate vlen = sizeof (val); 573*7c478bd9Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_RCVBUF, &val, &vlen) == 0) { 574*7c478bd9Sstevel@tonic-gate (void) snprintf(buf1, sizeof (buf1), "SO_RCVBUF(%d),", val); 575*7c478bd9Sstevel@tonic-gate (void) strlcat(buf, buf1, sizeof (buf)); 576*7c478bd9Sstevel@tonic-gate } 577*7c478bd9Sstevel@tonic-gate 578*7c478bd9Sstevel@tonic-gate buf[strlen(buf) - 1] = '\0'; 579*7c478bd9Sstevel@tonic-gate if (buf[1] != '\0') 580*7c478bd9Sstevel@tonic-gate (void) printf("\t%s\n", buf+1); 581*7c478bd9Sstevel@tonic-gate } 582*7c478bd9Sstevel@tonic-gate 583*7c478bd9Sstevel@tonic-gate /* the file is a socket */ 584*7c478bd9Sstevel@tonic-gate static void 585*7c478bd9Sstevel@tonic-gate dosocket(struct ps_prochandle *Pr, int fd) 586*7c478bd9Sstevel@tonic-gate { 587*7c478bd9Sstevel@tonic-gate /* A buffer large enough for PATH_MAX size AF_UNIX address */ 588*7c478bd9Sstevel@tonic-gate long buf[(sizeof (short) + PATH_MAX + sizeof (long) - 1) 589*7c478bd9Sstevel@tonic-gate / sizeof (long)]; 590*7c478bd9Sstevel@tonic-gate struct sockaddr *sa = (struct sockaddr *)buf; 591*7c478bd9Sstevel@tonic-gate socklen_t len; 592*7c478bd9Sstevel@tonic-gate int type, tlen; 593*7c478bd9Sstevel@tonic-gate 594*7c478bd9Sstevel@tonic-gate tlen = sizeof (type); 595*7c478bd9Sstevel@tonic-gate if (pr_getsockopt(Pr, fd, SOL_SOCKET, SO_TYPE, &type, &tlen) 596*7c478bd9Sstevel@tonic-gate == 0) 597*7c478bd9Sstevel@tonic-gate show_socktype((uint_t)type); 598*7c478bd9Sstevel@tonic-gate 599*7c478bd9Sstevel@tonic-gate show_sockopts(Pr, fd); 600*7c478bd9Sstevel@tonic-gate 601*7c478bd9Sstevel@tonic-gate len = sizeof (buf); 602*7c478bd9Sstevel@tonic-gate if (pr_getsockname(Pr, fd, sa, &len) == 0) 603*7c478bd9Sstevel@tonic-gate show_sockaddr("sockname", sa, len); 604*7c478bd9Sstevel@tonic-gate 605*7c478bd9Sstevel@tonic-gate len = sizeof (buf); 606*7c478bd9Sstevel@tonic-gate if (pr_getpeername(Pr, fd, sa, &len) == 0) 607*7c478bd9Sstevel@tonic-gate show_sockaddr("peername", sa, len); 608*7c478bd9Sstevel@tonic-gate } 609