1 /*- 2 * Copyright (c) 2007 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <sys/sysctl.h> 32 #include <sys/un.h> 33 #include <sys/user.h> 34 35 #include <netinet/in.h> 36 37 #include <arpa/inet.h> 38 39 #include <err.h> 40 #include <inttypes.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "procstat.h" 46 47 static const char * 48 protocol_to_string(int domain, int type, int protocol) 49 { 50 51 switch (domain) { 52 case AF_INET: 53 case AF_INET6: 54 switch (protocol) { 55 case IPPROTO_TCP: 56 return ("TCP"); 57 case IPPROTO_UDP: 58 return ("UDP"); 59 case IPPROTO_ICMP: 60 return ("ICM"); 61 case IPPROTO_RAW: 62 return ("RAW"); 63 case IPPROTO_SCTP: 64 return ("SCT"); 65 case IPPROTO_DIVERT: 66 return ("IPD"); 67 default: 68 return ("IP?"); 69 } 70 71 case AF_LOCAL: 72 switch (type) { 73 case SOCK_STREAM: 74 return ("UDS"); 75 case SOCK_DGRAM: 76 return ("UDD"); 77 default: 78 return ("UD?"); 79 } 80 default: 81 return ("?"); 82 } 83 } 84 85 static void 86 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen) 87 { 88 char buffer2[INET6_ADDRSTRLEN]; 89 struct sockaddr_in6 *sin6; 90 struct sockaddr_in *sin; 91 struct sockaddr_un *sun; 92 93 switch (ss->ss_family) { 94 case AF_LOCAL: 95 sun = (struct sockaddr_un *)ss; 96 if (strlen(sun->sun_path) == 0) 97 strlcpy(buffer, "-", buflen); 98 else 99 strlcpy(buffer, sun->sun_path, buflen); 100 break; 101 102 case AF_INET: 103 sin = (struct sockaddr_in *)ss; 104 snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr), 105 ntohs(sin->sin_port)); 106 break; 107 108 case AF_INET6: 109 sin6 = (struct sockaddr_in6 *)ss; 110 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2, 111 sizeof(buffer2)) != NULL) 112 snprintf(buffer, buflen, "%s.%d", buffer2, 113 ntohs(sin6->sin6_port)); 114 else 115 strlcpy(buffer, "-", sizeof(buffer)); 116 break; 117 118 default: 119 strlcpy(buffer, "", buflen); 120 break; 121 } 122 } 123 124 static void 125 print_address(struct sockaddr_storage *ss) 126 { 127 char addr[PATH_MAX]; 128 129 addr_to_string(ss, addr, sizeof(addr)); 130 printf("%s", addr); 131 } 132 133 void 134 procstat_files(pid_t pid, struct kinfo_proc *kipp) 135 { 136 struct kinfo_file *freep, *kif; 137 int error, i, name[4]; 138 const char *str; 139 size_t len; 140 141 if (!hflag) 142 printf("%5s %-16s %3s %1s %1s %-8s %3s %7s %-3s %-12s\n", 143 "PID", "COMM", "FD", "T", "V", "FLAGS", "REF", "OFFSET", 144 "PRO", "NAME"); 145 146 name[0] = CTL_KERN; 147 name[1] = KERN_PROC; 148 name[2] = KERN_PROC_FILEDESC; 149 name[3] = pid; 150 151 error = sysctl(name, 4, NULL, &len, NULL, 0); 152 if (error < 0 && errno != ESRCH && errno != EPERM) { 153 warn("sysctl: kern.proc.filedesc: %d", pid); 154 return; 155 } 156 if (error < 0) 157 return; 158 159 freep = kif = malloc(len); 160 if (kif == NULL) 161 err(-1, "malloc"); 162 163 if (sysctl(name, 4, kif, &len, NULL, 0) < 0) { 164 warn("sysctl: kern.proc.filedesc %d", pid); 165 free(freep); 166 return; 167 } 168 169 for (i = 0; i < len / sizeof(*kif); i++, kif++) { 170 if (kif->kf_structsize != sizeof(*kif)) 171 errx(-1, "kinfo_file mismatch"); 172 printf("%5d ", pid); 173 printf("%-16s ", kipp->ki_comm); 174 printf("%3d ", kif->kf_fd); 175 switch (kif->kf_type) { 176 case KF_TYPE_VNODE: 177 str = "v"; 178 break; 179 180 case KF_TYPE_SOCKET: 181 str = "s"; 182 break; 183 184 case KF_TYPE_PIPE: 185 str = "p"; 186 break; 187 188 case KF_TYPE_FIFO: 189 str = "f"; 190 break; 191 192 case KF_TYPE_KQUEUE: 193 str = "k"; 194 break; 195 196 case KF_TYPE_CRYPTO: 197 str = "c"; 198 break; 199 200 case KF_TYPE_MQUEUE: 201 str = "m"; 202 break; 203 204 case KF_TYPE_NONE: 205 case KF_TYPE_UNKNOWN: 206 default: 207 str = "?"; 208 break; 209 } 210 printf("%1s ", str); 211 str = "-"; 212 if (kif->kf_type == KF_TYPE_VNODE) { 213 switch (kif->kf_vnode_type) { 214 case KF_VTYPE_VREG: 215 str = "r"; 216 break; 217 218 case KF_VTYPE_VDIR: 219 str = "d"; 220 break; 221 222 case KF_VTYPE_VBLK: 223 str = "b"; 224 break; 225 226 case KF_VTYPE_VCHR: 227 str = "c"; 228 break; 229 230 case KF_VTYPE_VLNK: 231 str = "l"; 232 break; 233 234 case KF_VTYPE_VSOCK: 235 str = "s"; 236 break; 237 238 case KF_VTYPE_VFIFO: 239 str = "f"; 240 break; 241 242 case KF_VTYPE_VBAD: 243 str = "x"; 244 break; 245 246 case KF_VTYPE_VNON: 247 case KF_VTYPE_UNKNOWN: 248 default: 249 str = "?"; 250 break; 251 } 252 } 253 printf("%1s ", str); 254 printf("%s", kif->kf_flags & KF_FLAG_READ ? "r" : "-"); 255 printf("%s", kif->kf_flags & KF_FLAG_WRITE ? "w" : "-"); 256 printf("%s", kif->kf_flags & KF_FLAG_APPEND ? "a" : "-"); 257 printf("%s", kif->kf_flags & KF_FLAG_ASYNC ? "s" : "-"); 258 printf("%s", kif->kf_flags & KF_FLAG_FSYNC ? "f" : "-"); 259 printf("%s", kif->kf_flags & KF_FLAG_NONBLOCK ? "n" : "-"); 260 printf("%s", kif->kf_flags & KF_FLAG_DIRECT ? "d" : "-"); 261 printf("%s ", kif->kf_flags & KF_FLAG_HASLOCK ? "l" : "-"); 262 printf("%3d ", kif->kf_ref_count); 263 printf("%7jd ", (intmax_t)kif->kf_offset); 264 265 switch (kif->kf_type) { 266 case KF_TYPE_VNODE: 267 case KF_TYPE_FIFO: 268 printf("%-3s ", "-"); 269 printf("%-18s", kif->kf_path); 270 break; 271 272 case KF_TYPE_SOCKET: 273 printf("%-3s ", 274 protocol_to_string(kif->kf_sock_domain, 275 kif->kf_sock_type, kif->kf_sock_protocol)); 276 /* 277 * While generally we like to print two addresses, 278 * local and peer, for sockets, it turns out to be 279 * more useful to print the first non-nul address for 280 * local sockets, as typically they aren't bound and 281 * connected, and the path strings can get long. 282 */ 283 if (kif->kf_sock_domain == AF_LOCAL) { 284 struct sockaddr_un *sun = 285 (struct sockaddr_un *)&kif->kf_sa_local; 286 287 if (sun->sun_path[0] != 0) 288 print_address(&kif->kf_sa_local); 289 else 290 print_address(&kif->kf_sa_peer); 291 } else { 292 print_address(&kif->kf_sa_local); 293 printf(" "); 294 print_address(&kif->kf_sa_peer); 295 } 296 break; 297 298 default: 299 printf("%-3s ", "-"); 300 printf("%-18s", "-"); 301 } 302 303 printf("\n"); 304 } 305 free(freep); 306 } 307