xref: /freebsd/usr.bin/procstat/procstat_files.c (revision bd81e07d2761cf1c13063eb49a5c0cb4a6951318)
1 /*-
2  * Copyright (c) 2007-2011 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/param.h>
30 #include <sys/capsicum.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
33 #include <sys/un.h>
34 #include <sys/user.h>
35 
36 #include <netinet/in.h>
37 
38 #include <arpa/inet.h>
39 
40 #include <err.h>
41 #include <libprocstat.h>
42 #include <inttypes.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "procstat.h"
48 
49 static const char *
50 protocol_to_string(int domain, int type, int protocol)
51 {
52 
53 	switch (domain) {
54 	case AF_INET:
55 	case AF_INET6:
56 		switch (protocol) {
57 		case IPPROTO_TCP:
58 			return ("TCP");
59 		case IPPROTO_UDP:
60 			return ("UDP");
61 		case IPPROTO_ICMP:
62 			return ("ICM");
63 		case IPPROTO_RAW:
64 			return ("RAW");
65 		case IPPROTO_SCTP:
66 			return ("SCT");
67 		case IPPROTO_DIVERT:
68 			return ("IPD");
69 		default:
70 			return ("IP?");
71 		}
72 
73 	case AF_LOCAL:
74 		switch (type) {
75 		case SOCK_STREAM:
76 			return ("UDS");
77 		case SOCK_DGRAM:
78 			return ("UDD");
79 		default:
80 			return ("UD?");
81 		}
82 	default:
83 		return ("?");
84 	}
85 }
86 
87 static void
88 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
89 {
90 	char buffer2[INET6_ADDRSTRLEN];
91 	struct sockaddr_in6 *sin6;
92 	struct sockaddr_in *sin;
93 	struct sockaddr_un *sun;
94 
95 	switch (ss->ss_family) {
96 	case AF_LOCAL:
97 		sun = (struct sockaddr_un *)ss;
98 		if (strlen(sun->sun_path) == 0)
99 			strlcpy(buffer, "-", buflen);
100 		else
101 			strlcpy(buffer, sun->sun_path, buflen);
102 		break;
103 
104 	case AF_INET:
105 		sin = (struct sockaddr_in *)ss;
106 		snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr),
107 		    ntohs(sin->sin_port));
108 		break;
109 
110 	case AF_INET6:
111 		sin6 = (struct sockaddr_in6 *)ss;
112 		if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
113 		    sizeof(buffer2)) != NULL)
114 			snprintf(buffer, buflen, "%s.%d", buffer2,
115 			    ntohs(sin6->sin6_port));
116 		else
117 			strlcpy(buffer, "-", buflen);
118 		break;
119 
120 	default:
121 		strlcpy(buffer, "", buflen);
122 		break;
123 	}
124 }
125 
126 static void
127 print_address(struct sockaddr_storage *ss)
128 {
129 	char addr[PATH_MAX];
130 
131 	addr_to_string(ss, addr, sizeof(addr));
132 	printf("%s", addr);
133 }
134 
135 static struct cap_desc {
136 	uint64_t	 cd_right;
137 	const char	*cd_desc;
138 } cap_desc[] = {
139 	/* General file I/O. */
140 	{ CAP_READ,		"rd" },
141 	{ CAP_WRITE,		"wr" },
142 	{ CAP_SEEK,		"se" },
143 	{ CAP_MMAP,		"mm" },
144 	{ CAP_CREATE,		"cr" },
145 	{ CAP_FEXECVE,		"fe" },
146 	{ CAP_FSYNC,		"fy" },
147 	{ CAP_FTRUNCATE,	"ft" },
148 
149 	/* VFS methods. */
150 	{ CAP_FCHDIR,		"cd" },
151 	{ CAP_FCHFLAGS,		"cf" },
152 	{ CAP_FCHMOD,		"cm" },
153 	{ CAP_FCHOWN,		"cn" },
154 	{ CAP_FCNTL,		"fc" },
155 	{ CAP_FLOCK,		"fl" },
156 	{ CAP_FPATHCONF,	"fp" },
157 	{ CAP_FSCK,		"fk" },
158 	{ CAP_FSTAT,		"fs" },
159 	{ CAP_FSTATFS,		"sf" },
160 	{ CAP_FUTIMES,		"fu" },
161 	{ CAP_LINKAT_SOURCE,	"ls" },
162 	{ CAP_LINKAT_TARGET,	"lt" },
163 	{ CAP_MKDIRAT,		"md" },
164 	{ CAP_MKFIFOAT,		"mf" },
165 	{ CAP_MKNODAT,		"mn" },
166 	{ CAP_RENAMEAT_SOURCE,	"rs" },
167 	{ CAP_RENAMEAT_TARGET,	"rt" },
168 	{ CAP_SYMLINKAT,	"sl" },
169 	{ CAP_UNLINKAT,		"un" },
170 
171 	/* Lookups - used to constrain *at() calls. */
172 	{ CAP_LOOKUP,		"lo" },
173 
174 	/* Extended attributes. */
175 	{ CAP_EXTATTR_GET,	"eg" },
176 	{ CAP_EXTATTR_SET,	"es" },
177 	{ CAP_EXTATTR_DELETE,	"ed" },
178 	{ CAP_EXTATTR_LIST,	"el" },
179 
180 	/* Access Control Lists. */
181 	{ CAP_ACL_GET,		"ag" },
182 	{ CAP_ACL_SET,		"as" },
183 	{ CAP_ACL_DELETE,	"ad" },
184 	{ CAP_ACL_CHECK,	"ac" },
185 
186 	/* Socket operations. */
187 	{ CAP_ACCEPT,		"at" },
188 	{ CAP_BIND,		"bd" },
189 	{ CAP_CONNECT,		"co" },
190 	{ CAP_GETPEERNAME,	"pn" },
191 	{ CAP_GETSOCKNAME,	"sn" },
192 	{ CAP_GETSOCKOPT,	"gs" },
193 	{ CAP_LISTEN,		"ln" },
194 	{ CAP_PEELOFF,		"pf" },
195 	{ CAP_SETSOCKOPT,	"ss" },
196 	{ CAP_SHUTDOWN,		"sh" },
197 
198 	/* Mandatory Access Control. */
199 	{ CAP_MAC_GET,		"mg" },
200 	{ CAP_MAC_SET,		"ms" },
201 
202 	/* Methods on semaphores. */
203 	{ CAP_SEM_GETVALUE,	"sg" },
204 	{ CAP_SEM_POST,		"sp" },
205 	{ CAP_SEM_WAIT,		"sw" },
206 
207 	/* Event monitoring and posting. */
208 	{ CAP_EVENT,		"ev" },
209 	{ CAP_KQUEUE_EVENT,	"ke" },
210 	{ CAP_KQUEUE_CHANGE,	"kc" },
211 
212 	/* Strange and powerful rights that should not be given lightly. */
213 	{ CAP_IOCTL,		"io" },
214 	{ CAP_TTYHOOK,		"ty" },
215 
216 	/* Process management via process descriptors. */
217 	{ CAP_PDGETPID,		"pg" },
218 	{ CAP_PDWAIT,		"pw" },
219 	{ CAP_PDKILL,		"pk" },
220 
221 	/*
222 	 * Rights that allow to use bindat(2) and connectat(2) syscalls on a
223 	 * directory descriptor.
224 	 */
225 	{ CAP_BINDAT,		"ba" },
226 	{ CAP_CONNECTAT,	"ca" },
227 
228 	/* Aliases and defines that combine multiple rights. */
229 	{ CAP_PREAD,		"prd" },
230 	{ CAP_PWRITE,		"pwr" },
231 
232 	{ CAP_MMAP_R,		"mmr" },
233 	{ CAP_MMAP_W,		"mmw" },
234 	{ CAP_MMAP_X,		"mmx" },
235 	{ CAP_MMAP_RW,		"mrw" },
236 	{ CAP_MMAP_RX,		"mrx" },
237 	{ CAP_MMAP_WX,		"mwx" },
238 	{ CAP_MMAP_RWX,		"mma" },
239 
240 	{ CAP_RECV,		"re" },
241 	{ CAP_SEND,		"sd" },
242 
243 	{ CAP_SOCK_CLIENT,	"scl" },
244 	{ CAP_SOCK_SERVER,	"ssr" },
245 };
246 static const u_int	cap_desc_count = sizeof(cap_desc) /
247 			    sizeof(cap_desc[0]);
248 
249 static u_int
250 width_capability(cap_rights_t *rightsp)
251 {
252 	u_int count, i, width;
253 
254 	count = 0;
255 	width = 0;
256 	for (i = 0; i < cap_desc_count; i++) {
257 		if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
258 			width += strlen(cap_desc[i].cd_desc);
259 			if (count)
260 				width++;
261 			count++;
262 		}
263 	}
264 	return (width);
265 }
266 
267 static void
268 print_capability(cap_rights_t *rightsp, u_int capwidth)
269 {
270 	u_int count, i, width;
271 
272 	count = 0;
273 	width = 0;
274 	for (i = width_capability(rightsp); i < capwidth; i++) {
275 		if (i != 0)
276 			printf(" ");
277 		else
278 			printf("-");
279 	}
280 	for (i = 0; i < cap_desc_count; i++) {
281 		if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
282 			printf("%s%s", count ? "," : "", cap_desc[i].cd_desc);
283 			width += strlen(cap_desc[i].cd_desc);
284 			if (count)
285 				width++;
286 			count++;
287 		}
288 	}
289 }
290 
291 void
292 procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
293 {
294 	struct sockstat sock;
295 	struct filestat_list *head;
296 	struct filestat *fst;
297 	const char *str;
298 	struct vnstat vn;
299 	u_int capwidth, width;
300 	int error;
301 
302 	/*
303 	 * To print the header in capability mode, we need to know the width
304 	 * of the widest capability string.  Even if we get no processes
305 	 * back, we will print the header, so we defer aborting due to a lack
306 	 * of processes until after the header logic.
307 	 */
308 	capwidth = 0;
309 	head = procstat_getfiles(procstat, kipp, 0);
310 	if (head != NULL && Cflag) {
311 		STAILQ_FOREACH(fst, head, next) {
312 			width = width_capability(&fst->fs_cap_rights);
313 			if (width > capwidth)
314 				capwidth = width;
315 		}
316 		if (capwidth < strlen("CAPABILITIES"))
317 			capwidth = strlen("CAPABILITIES");
318 	}
319 
320 	if (!hflag) {
321 		if (Cflag)
322 			printf("%5s %-16s %5s %1s %-8s %-*s "
323 			    "%-3s %-12s\n", "PID", "COMM", "FD", "T",
324 			    "FLAGS", capwidth, "CAPABILITIES", "PRO",
325 			    "NAME");
326 		else
327 			printf("%5s %-16s %5s %1s %1s %-8s "
328 			    "%3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T",
329 			    "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
330 	}
331 
332 	if (head == NULL)
333 		return;
334 	STAILQ_FOREACH(fst, head, next) {
335 		printf("%5d ", kipp->ki_pid);
336 		printf("%-16s ", kipp->ki_comm);
337 		if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
338 			printf(" ctty ");
339 		else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
340 			printf("  cwd ");
341 		else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
342 			printf(" jail ");
343 		else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
344 			printf(" root ");
345 		else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
346 			printf(" text ");
347 		else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
348 			printf("trace ");
349 		else
350 			printf("%5d ", fst->fs_fd);
351 
352 		switch (fst->fs_type) {
353 		case PS_FST_TYPE_VNODE:
354 			str = "v";
355 			break;
356 
357 		case PS_FST_TYPE_SOCKET:
358 			str = "s";
359 			break;
360 
361 		case PS_FST_TYPE_PIPE:
362 			str = "p";
363 			break;
364 
365 		case PS_FST_TYPE_FIFO:
366 			str = "f";
367 			break;
368 
369 		case PS_FST_TYPE_KQUEUE:
370 			str = "k";
371 			break;
372 
373 		case PS_FST_TYPE_CRYPTO:
374 			str = "c";
375 			break;
376 
377 		case PS_FST_TYPE_MQUEUE:
378 			str = "m";
379 			break;
380 
381 		case PS_FST_TYPE_SHM:
382 			str = "h";
383 			break;
384 
385 		case PS_FST_TYPE_PTS:
386 			str = "t";
387 			break;
388 
389 		case PS_FST_TYPE_SEM:
390 			str = "e";
391 			break;
392 
393 		case PS_FST_TYPE_NONE:
394 		case PS_FST_TYPE_UNKNOWN:
395 		default:
396 			str = "?";
397 			break;
398 		}
399 		printf("%1s ", str);
400 		if (!Cflag) {
401 			str = "-";
402 			if (fst->fs_type == PS_FST_TYPE_VNODE) {
403 				error = procstat_get_vnode_info(procstat, fst,
404 				    &vn, NULL);
405 				switch (vn.vn_type) {
406 				case PS_FST_VTYPE_VREG:
407 					str = "r";
408 					break;
409 
410 				case PS_FST_VTYPE_VDIR:
411 					str = "d";
412 					break;
413 
414 				case PS_FST_VTYPE_VBLK:
415 					str = "b";
416 					break;
417 
418 				case PS_FST_VTYPE_VCHR:
419 					str = "c";
420 					break;
421 
422 				case PS_FST_VTYPE_VLNK:
423 					str = "l";
424 					break;
425 
426 				case PS_FST_VTYPE_VSOCK:
427 					str = "s";
428 					break;
429 
430 				case PS_FST_VTYPE_VFIFO:
431 					str = "f";
432 					break;
433 
434 				case PS_FST_VTYPE_VBAD:
435 					str = "x";
436 					break;
437 
438 				case PS_FST_VTYPE_VNON:
439 				case PS_FST_VTYPE_UNKNOWN:
440 				default:
441 					str = "?";
442 					break;
443 				}
444 			}
445 			printf("%1s ", str);
446 		}
447 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-");
448 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-");
449 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-");
450 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-");
451 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-");
452 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-");
453 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-");
454 		printf("%s", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-");
455 		printf(" ");
456 		if (!Cflag) {
457 			if (fst->fs_ref_count > -1)
458 				printf("%3d ", fst->fs_ref_count);
459 			else
460 				printf("%3c ", '-');
461 			if (fst->fs_offset > -1)
462 				printf("%7jd ", (intmax_t)fst->fs_offset);
463 			else
464 				printf("%7c ", '-');
465 		}
466 		if (Cflag) {
467 			print_capability(&fst->fs_cap_rights, capwidth);
468 			printf(" ");
469 		}
470 		switch (fst->fs_type) {
471 		case PS_FST_TYPE_SOCKET:
472 			error = procstat_get_socket_info(procstat, fst, &sock, NULL);
473 			if (error != 0)
474 				break;
475 			printf("%-3s ",
476 			    protocol_to_string(sock.dom_family,
477 			    sock.type, sock.proto));
478 			/*
479 			 * While generally we like to print two addresses,
480 			 * local and peer, for sockets, it turns out to be
481 			 * more useful to print the first non-nul address for
482 			 * local sockets, as typically they aren't bound and
483 			 *  connected, and the path strings can get long.
484 			 */
485 			if (sock.dom_family == AF_LOCAL) {
486 				struct sockaddr_un *sun =
487 				    (struct sockaddr_un *)&sock.sa_local;
488 
489 				if (sun->sun_path[0] != 0)
490 					print_address(&sock.sa_local);
491 				else
492 					print_address(&sock.sa_peer);
493 			} else {
494 				print_address(&sock.sa_local);
495 				printf(" ");
496 				print_address(&sock.sa_peer);
497 			}
498 			break;
499 
500 		default:
501 			printf("%-3s ", "-");
502 			printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-");
503 		}
504 
505 		printf("\n");
506 	}
507 	procstat_freefiles(procstat, head);
508 }
509