Deleted Added
full compact
procstat_files.c (287209) procstat_files.c (287486)
1/*-
2 * Copyright (c) 2007-2011 Robert N. M. Watson
1/*-
2 * Copyright (c) 2007-2011 Robert N. M. Watson
3 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
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

--- 7 unchanged lines hidden (view full) ---

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 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright

--- 7 unchanged lines hidden (view full) ---

19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
26 * $FreeBSD: head/usr.bin/procstat/procstat_files.c 287209 2015-08-27 15:16:41Z ed $
27 * $FreeBSD: head/usr.bin/procstat/procstat_files.c 287486 2015-09-05 17:02:01Z allanjude $
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>

--- 83 unchanged lines hidden (view full) ---

118 break;
119
120 default:
121 strlcpy(buffer, "", buflen);
122 break;
123 }
124}
125
28 */
29
30#include <sys/param.h>
31#include <sys/capsicum.h>
32#include <sys/socket.h>
33#include <sys/sysctl.h>
34#include <sys/un.h>
35#include <sys/user.h>

--- 83 unchanged lines hidden (view full) ---

119 break;
120
121 default:
122 strlcpy(buffer, "", buflen);
123 break;
124 }
125}
126
126static void
127print_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
135static 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" },

--- 125 unchanged lines hidden (view full) ---

268print_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)
127static struct cap_desc {
128 uint64_t cd_right;
129 const char *cd_desc;
130} cap_desc[] = {
131 /* General file I/O. */
132 { CAP_READ, "rd" },
133 { CAP_WRITE, "wr" },
134 { CAP_SEEK, "se" },

--- 125 unchanged lines hidden (view full) ---

260print_capability(cap_rights_t *rightsp, u_int capwidth)
261{
262 u_int count, i, width;
263
264 count = 0;
265 width = 0;
266 for (i = width_capability(rightsp); i < capwidth; i++) {
267 if (i != 0)
276 printf(" ");
268 xo_emit(" ");
277 else
269 else
278 printf("-");
270 xo_emit("-");
279 }
271 }
272 xo_open_list("capabilities");
280 for (i = 0; i < cap_desc_count; i++) {
281 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
273 for (i = 0; i < cap_desc_count; i++) {
274 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
282 printf("%s%s", count ? "," : "", cap_desc[i].cd_desc);
275 xo_emit("{D:/%s}{l:capabilities/%s}", count ? "," : "",
276 cap_desc[i].cd_desc);
283 width += strlen(cap_desc[i].cd_desc);
284 if (count)
285 width++;
286 count++;
287 }
288 }
277 width += strlen(cap_desc[i].cd_desc);
278 if (count)
279 width++;
280 count++;
281 }
282 }
283 xo_close_list("capabilities");
289}
290
291void
292procstat_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;
284}
285
286void
287procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
288{
289 struct sockstat sock;
290 struct filestat_list *head;
291 struct filestat *fst;
292 const char *str;
293 struct vnstat vn;
294 u_int capwidth, width;
295 int error;
296 char src_addr[PATH_MAX];
297 char dst_addr[PATH_MAX];
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;

--- 5 unchanged lines hidden (view full) ---

314 capwidth = width;
315 }
316 if (capwidth < strlen("CAPABILITIES"))
317 capwidth = strlen("CAPABILITIES");
318 }
319
320 if (!hflag) {
321 if (Cflag)
298
299 /*
300 * To print the header in capability mode, we need to know the width
301 * of the widest capability string. Even if we get no processes
302 * back, we will print the header, so we defer aborting due to a lack
303 * of processes until after the header logic.
304 */
305 capwidth = 0;

--- 5 unchanged lines hidden (view full) ---

311 capwidth = width;
312 }
313 if (capwidth < strlen("CAPABILITIES"))
314 capwidth = strlen("CAPABILITIES");
315 }
316
317 if (!hflag) {
318 if (Cflag)
322 printf("%5s %-16s %5s %1s %-8s %-*s "
323 "%-3s %-12s\n", "PID", "COMM", "FD", "T",
319 xo_emit("{T:/%5s %-16s %5s %1s %-8s %-*s "
320 "%-3s %-12s}\n", "PID", "COMM", "FD", "T",
324 "FLAGS", capwidth, "CAPABILITIES", "PRO",
325 "NAME");
326 else
321 "FLAGS", capwidth, "CAPABILITIES", "PRO",
322 "NAME");
323 else
327 printf("%5s %-16s %5s %1s %1s %-8s "
328 "%3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T",
324 xo_emit("{T:/%5s %-16s %5s %1s %1s %-8s "
325 "%3s %7s %-3s %-12s}\n", "PID", "COMM", "FD", "T",
329 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
330 }
331
332 if (head == NULL)
333 return;
326 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
327 }
328
329 if (head == NULL)
330 return;
331 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
332 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
333 xo_open_list("files");
334 STAILQ_FOREACH(fst, head, next) {
334 STAILQ_FOREACH(fst, head, next) {
335 printf("%5d ", kipp->ki_pid);
336 printf("%-16s ", kipp->ki_comm);
335 xo_open_instance("files");
336 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
337 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
337 if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
338 if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
338 printf(" ctty ");
339 xo_emit("{P: }{:fd/%s} ", "ctty");
339 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
340 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
340 printf(" cwd ");
341 xo_emit("{P: }{:fd/%s} ", "cwd");
341 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
342 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
342 printf(" jail ");
343 xo_emit("{P: }{:fd/%s} ", "jail");
343 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
344 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
344 printf(" root ");
345 xo_emit("{P: }{:fd/%s} ", "root");
345 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
346 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
346 printf(" text ");
347 xo_emit("{P: }{:fd/%s} ", "text");
347 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
348 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
348 printf("trace ");
349 xo_emit("{:fd/%s} ", "trace");
349 else
350 else
350 printf("%5d ", fst->fs_fd);
351 xo_emit("{:fd/%5d} ", fst->fs_fd);
351
352 switch (fst->fs_type) {
353 case PS_FST_TYPE_VNODE:
354 str = "v";
352
353 switch (fst->fs_type) {
354 case PS_FST_TYPE_VNODE:
355 str = "v";
356 xo_emit("{eq:fd_type/vnode}");
355 break;
356
357 case PS_FST_TYPE_SOCKET:
358 str = "s";
357 break;
358
359 case PS_FST_TYPE_SOCKET:
360 str = "s";
361 xo_emit("{eq:fd_type/socket}");
359 break;
360
361 case PS_FST_TYPE_PIPE:
362 str = "p";
362 break;
363
364 case PS_FST_TYPE_PIPE:
365 str = "p";
366 xo_emit("{eq:fd_type/pipe}");
363 break;
364
365 case PS_FST_TYPE_FIFO:
366 str = "f";
367 break;
368
369 case PS_FST_TYPE_FIFO:
370 str = "f";
371 xo_emit("{eq:fd_type/fifo}");
367 break;
368
369 case PS_FST_TYPE_KQUEUE:
370 str = "k";
372 break;
373
374 case PS_FST_TYPE_KQUEUE:
375 str = "k";
376 xo_emit("{eq:fd_type/kqueue}");
371 break;
372
373 case PS_FST_TYPE_CRYPTO:
374 str = "c";
377 break;
378
379 case PS_FST_TYPE_CRYPTO:
380 str = "c";
381 xo_emit("{eq:fd_type/crypto}");
375 break;
376
377 case PS_FST_TYPE_MQUEUE:
378 str = "m";
382 break;
383
384 case PS_FST_TYPE_MQUEUE:
385 str = "m";
386 xo_emit("{eq:fd_type/mqueue}");
379 break;
380
381 case PS_FST_TYPE_SHM:
382 str = "h";
387 break;
388
389 case PS_FST_TYPE_SHM:
390 str = "h";
391 xo_emit("{eq:fd_type/shm}");
383 break;
384
385 case PS_FST_TYPE_PTS:
386 str = "t";
392 break;
393
394 case PS_FST_TYPE_PTS:
395 str = "t";
396 xo_emit("{eq:fd_type/pts}");
387 break;
388
389 case PS_FST_TYPE_SEM:
390 str = "e";
397 break;
398
399 case PS_FST_TYPE_SEM:
400 str = "e";
401 xo_emit("{eq:fd_type/sem}");
391 break;
392
393 case PS_FST_TYPE_NONE:
402 break;
403
404 case PS_FST_TYPE_NONE:
405 str = "?";
406 xo_emit("{eq:fd_type/none}");
407 break;
408
394 case PS_FST_TYPE_UNKNOWN:
395 default:
396 str = "?";
409 case PS_FST_TYPE_UNKNOWN:
410 default:
411 str = "?";
412 xo_emit("{eq:fd_type/unknown}");
397 break;
398 }
413 break;
414 }
399 printf("%1s ", str);
415 xo_emit("{d:fd_type/%1s/%s} ", 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";
416 if (!Cflag) {
417 str = "-";
418 if (fst->fs_type == PS_FST_TYPE_VNODE) {
419 error = procstat_get_vnode_info(procstat, fst,
420 &vn, NULL);
421 switch (vn.vn_type) {
422 case PS_FST_VTYPE_VREG:
423 str = "r";
424 xo_emit("{eq:vode_type/regular}");
408 break;
409
410 case PS_FST_VTYPE_VDIR:
411 str = "d";
425 break;
426
427 case PS_FST_VTYPE_VDIR:
428 str = "d";
429 xo_emit("{eq:vode_type/directory}");
412 break;
413
414 case PS_FST_VTYPE_VBLK:
415 str = "b";
430 break;
431
432 case PS_FST_VTYPE_VBLK:
433 str = "b";
434 xo_emit("{eq:vode_type/block}");
416 break;
417
418 case PS_FST_VTYPE_VCHR:
419 str = "c";
435 break;
436
437 case PS_FST_VTYPE_VCHR:
438 str = "c";
439 xo_emit("{eq:vode_type/character}");
420 break;
421
422 case PS_FST_VTYPE_VLNK:
423 str = "l";
440 break;
441
442 case PS_FST_VTYPE_VLNK:
443 str = "l";
444 xo_emit("{eq:vode_type/link}");
424 break;
425
426 case PS_FST_VTYPE_VSOCK:
427 str = "s";
445 break;
446
447 case PS_FST_VTYPE_VSOCK:
448 str = "s";
449 xo_emit("{eq:vode_type/socket}");
428 break;
429
430 case PS_FST_VTYPE_VFIFO:
431 str = "f";
450 break;
451
452 case PS_FST_VTYPE_VFIFO:
453 str = "f";
454 xo_emit("{eq:vode_type/fifo}");
432 break;
433
434 case PS_FST_VTYPE_VBAD:
435 str = "x";
455 break;
456
457 case PS_FST_VTYPE_VBAD:
458 str = "x";
459 xo_emit("{eq:vode_type/revoked_device}");
436 break;
437
438 case PS_FST_VTYPE_VNON:
460 break;
461
462 case PS_FST_VTYPE_VNON:
463 str = "?";
464 xo_emit("{eq:vode_type/non}");
465 break;
466
439 case PS_FST_VTYPE_UNKNOWN:
440 default:
441 str = "?";
467 case PS_FST_VTYPE_UNKNOWN:
468 default:
469 str = "?";
470 xo_emit("{eq:vode_type/unknown}");
442 break;
443 }
444 }
471 break;
472 }
473 }
445 printf("%1s ", str);
474 xo_emit("{d:vnode_type/%1s/%s} ", str);
446 }
475 }
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(" ");
476
477 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_READ ?
478 "r" : "-");
479 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_WRITE ?
480 "w" : "-");
481 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_APPEND ?
482 "a" : "-");
483 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_ASYNC ?
484 "s" : "-");
485 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_SYNC ?
486 "f" : "-");
487 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ?
488 "n" : "-");
489 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_DIRECT ?
490 "d" : "-");
491 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ?
492 "l" : "-");
493 xo_emit(" ");
494 xo_open_list("fd_flags");
495 if (fst->fs_fflags & PS_FST_FFLAG_READ)
496 xo_emit("{elq:fd_flags/read}");
497 if (fst->fs_fflags & PS_FST_FFLAG_WRITE)
498 xo_emit("{elq:fd_flags/write}");
499 if (fst->fs_fflags & PS_FST_FFLAG_APPEND)
500 xo_emit("{elq:fd_flags/append}");
501 if (fst->fs_fflags & PS_FST_FFLAG_ASYNC)
502 xo_emit("{elq:fd_flags/async}");
503 if (fst->fs_fflags & PS_FST_FFLAG_SYNC)
504 xo_emit("{elq:fd_flags/fsync}");
505 if (fst->fs_fflags & PS_FST_FFLAG_NONBLOCK)
506 xo_emit("{elq:fd_flags/nonblocking}");
507 if (fst->fs_fflags & PS_FST_FFLAG_DIRECT)
508 xo_emit("{elq:fd_flags/direct_io}");
509 if (fst->fs_fflags & PS_FST_FFLAG_HASLOCK)
510 xo_emit("{elq:fd_flags/lock_held}");
511 xo_close_list("fd_flags");
512
456 if (!Cflag) {
457 if (fst->fs_ref_count > -1)
513 if (!Cflag) {
514 if (fst->fs_ref_count > -1)
458 printf("%3d ", fst->fs_ref_count);
515 xo_emit("{:ref_count/%3d/%d} ",
516 fst->fs_ref_count);
459 else
517 else
460 printf("%3c ", '-');
518 xo_emit("{q:ref_count/%3c/%c} ", '-');
461 if (fst->fs_offset > -1)
519 if (fst->fs_offset > -1)
462 printf("%7jd ", (intmax_t)fst->fs_offset);
520 xo_emit("{:offset/%7jd/%jd} ",
521 (intmax_t)fst->fs_offset);
463 else
522 else
464 printf("%7c ", '-');
523 xo_emit("{q:offset/%7c/%c} ", '-');
465 }
466 if (Cflag) {
467 print_capability(&fst->fs_cap_rights, capwidth);
524 }
525 if (Cflag) {
526 print_capability(&fst->fs_cap_rights, capwidth);
468 printf(" ");
527 xo_emit(" ");
469 }
470 switch (fst->fs_type) {
471 case PS_FST_TYPE_SOCKET:
528 }
529 switch (fst->fs_type) {
530 case PS_FST_TYPE_SOCKET:
472 error = procstat_get_socket_info(procstat, fst, &sock, NULL);
531 error = procstat_get_socket_info(procstat, fst, &sock,
532 NULL);
473 if (error != 0)
474 break;
533 if (error != 0)
534 break;
475 printf("%-3s ",
535 xo_emit("{:protocol/%-3s/%s} ",
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)
536 protocol_to_string(sock.dom_family,
537 sock.type, sock.proto));
538 /*
539 * While generally we like to print two addresses,
540 * local and peer, for sockets, it turns out to be
541 * more useful to print the first non-nul address for
542 * local sockets, as typically they aren't bound and
543 * connected, and the path strings can get long.
544 */
545 if (sock.dom_family == AF_LOCAL) {
546 struct sockaddr_un *sun =
547 (struct sockaddr_un *)&sock.sa_local;
548
549 if (sun->sun_path[0] != 0)
490 print_address(&sock.sa_local);
550 addr_to_string(&sock.sa_local,
551 src_addr, sizeof(src_addr));
491 else
552 else
492 print_address(&sock.sa_peer);
553 addr_to_string(&sock.sa_peer,
554 src_addr, sizeof(src_addr));
555 xo_emit("{:path/%s}", src_addr);
493 } else {
556 } else {
494 print_address(&sock.sa_local);
495 printf(" ");
496 print_address(&sock.sa_peer);
557 addr_to_string(&sock.sa_local, src_addr,
558 sizeof(src_addr));
559 addr_to_string(&sock.sa_peer, dst_addr,
560 sizeof(dst_addr));
561 xo_emit("{:path/%s %s}", src_addr, dst_addr);
497 }
498 break;
499
500 default:
562 }
563 break;
564
565 default:
501 printf("%-3s ", "-");
502 printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-");
566 xo_emit("{:protocol/%-3s/%s} ", "-");
567 xo_emit("{:path/%-18s/%s}", fst->fs_path != NULL ?
568 fst->fs_path : "-");
503 }
504
569 }
570
505 printf("\n");
571 xo_emit("\n");
572 xo_close_instance("files");
506 }
573 }
574 xo_close_list("files");
507 procstat_freefiles(procstat, head);
508}
575 procstat_freefiles(procstat, head);
576}