freebsd-skq/usr.bin/procstat/procstat_files.c

480 lines
11 KiB
C
Raw Normal View History

/*-
* Copyright (c) 2007-2011 Robert N. M. Watson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/capability.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/un.h>
#include <sys/user.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <libprocstat.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "procstat.h"
static const char *
protocol_to_string(int domain, int type, int protocol)
{
switch (domain) {
case AF_INET:
case AF_INET6:
switch (protocol) {
case IPPROTO_TCP:
return ("TCP");
case IPPROTO_UDP:
return ("UDP");
case IPPROTO_ICMP:
return ("ICM");
case IPPROTO_RAW:
return ("RAW");
case IPPROTO_SCTP:
return ("SCT");
case IPPROTO_DIVERT:
return ("IPD");
default:
return ("IP?");
}
case AF_LOCAL:
switch (type) {
case SOCK_STREAM:
return ("UDS");
case SOCK_DGRAM:
return ("UDD");
default:
return ("UD?");
}
default:
return ("?");
}
}
static void
addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
{
char buffer2[INET6_ADDRSTRLEN];
struct sockaddr_in6 *sin6;
struct sockaddr_in *sin;
struct sockaddr_un *sun;
switch (ss->ss_family) {
case AF_LOCAL:
sun = (struct sockaddr_un *)ss;
if (strlen(sun->sun_path) == 0)
strlcpy(buffer, "-", buflen);
else
strlcpy(buffer, sun->sun_path, buflen);
break;
case AF_INET:
sin = (struct sockaddr_in *)ss;
snprintf(buffer, buflen, "%s:%d", inet_ntoa(sin->sin_addr),
ntohs(sin->sin_port));
break;
case AF_INET6:
sin6 = (struct sockaddr_in6 *)ss;
if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
sizeof(buffer2)) != NULL)
snprintf(buffer, buflen, "%s.%d", buffer2,
ntohs(sin6->sin6_port));
else
strlcpy(buffer, "-", sizeof(buffer));
break;
default:
strlcpy(buffer, "", buflen);
break;
}
}
static void
print_address(struct sockaddr_storage *ss)
{
char addr[PATH_MAX];
addr_to_string(ss, addr, sizeof(addr));
printf("%s", addr);
}
static struct cap_desc {
cap_rights_t cd_right;
const char *cd_desc;
} cap_desc[] = {
/* General file I/O. */
{ CAP_READ, "rd" },
{ CAP_WRITE, "wr" },
{ CAP_MMAP, "mm" },
{ CAP_MAPEXEC, "me" },
{ CAP_FEXECVE, "fe" },
{ CAP_FSYNC, "fy" },
{ CAP_FTRUNCATE, "ft" },
{ CAP_SEEK, "se" },
/* VFS methods. */
{ CAP_FCHFLAGS, "cf" },
{ CAP_FCHDIR, "cd" },
{ CAP_FCHMOD, "cm" },
{ CAP_FCHOWN, "cn" },
{ CAP_FCNTL, "fc" },
{ CAP_FPATHCONF, "fp" },
{ CAP_FLOCK, "fl" },
{ CAP_FSCK, "fk" },
{ CAP_FSTAT, "fs" },
{ CAP_FSTATFS, "sf" },
{ CAP_FUTIMES, "fu" },
{ CAP_CREATE, "cr" },
{ CAP_DELETE, "de" },
{ CAP_MKDIR, "md" },
{ CAP_RMDIR, "rm" },
{ CAP_MKFIFO, "mf" },
/* Lookups - used to constraint *at() calls. */
{ CAP_LOOKUP, "lo" },
/* Extended attributes. */
{ CAP_EXTATTR_GET, "eg" },
{ CAP_EXTATTR_SET, "es" },
{ CAP_EXTATTR_DELETE, "ed" },
{ CAP_EXTATTR_LIST, "el" },
/* Access Control Lists. */
{ CAP_ACL_GET, "ag" },
{ CAP_ACL_SET, "as" },
{ CAP_ACL_DELETE, "ad" },
{ CAP_ACL_CHECK, "ac" },
/* Socket operations. */
{ CAP_ACCEPT, "at" },
{ CAP_BIND, "bd" },
{ CAP_CONNECT, "co" },
{ CAP_GETPEERNAME, "pn" },
{ CAP_GETSOCKNAME, "sn" },
{ CAP_GETSOCKOPT, "gs" },
{ CAP_LISTEN, "ln" },
{ CAP_PEELOFF, "pf" },
{ CAP_SETSOCKOPT, "ss" },
{ CAP_SHUTDOWN, "sh" },
/* Mandatory Access Control. */
{ CAP_MAC_GET, "mg" },
{ CAP_MAC_SET, "ms" },
/* Methods on semaphores. */
{ CAP_SEM_GETVALUE, "sg" },
{ CAP_SEM_POST, "sp" },
{ CAP_SEM_WAIT, "sw" },
/* Event monitoring and posting. */
{ CAP_POLL_EVENT, "po" },
{ CAP_POST_EVENT, "ev" },
/* Strange and powerful rights that should not be given lightly. */
{ CAP_IOCTL, "io" },
{ CAP_TTYHOOK, "ty" },
#ifdef NOTYET
{ CAP_PDGETPID, "pg" },
{ CAP_PDWAIT4, "pw" },
{ CAP_PDKILL, "pk" },
#endif
};
static const u_int cap_desc_count = sizeof(cap_desc) /
sizeof(cap_desc[0]);
static u_int
width_capability(cap_rights_t rights)
{
u_int count, i, width;
count = 0;
width = 0;
for (i = 0; i < cap_desc_count; i++) {
if (rights & cap_desc[i].cd_right) {
width += strlen(cap_desc[i].cd_desc);
if (count)
width++;
count++;
}
}
return (width);
}
static void
print_capability(cap_rights_t rights, u_int capwidth)
{
u_int count, i, width;
count = 0;
width = 0;
for (i = width_capability(rights); i < capwidth; i++) {
if (rights || i != 0)
printf(" ");
else
printf("-");
}
for (i = 0; i < cap_desc_count; i++) {
if (rights & cap_desc[i].cd_right) {
printf("%s%s", count ? "," : "", cap_desc[i].cd_desc);
width += strlen(cap_desc[i].cd_desc);
if (count)
width++;
count++;
}
}
}
void
procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
{
struct sockstat sock;
struct filestat_list *head;
struct filestat *fst;
const char *str;
struct vnstat vn;
u_int capwidth, width;
int error;
/*
* To print the header in capability mode, we need to know the width
* of the widest capability string. Even if we get no processes
* back, we will print the header, so we defer aborting due to a lack
* of processes until after the header logic.
*/
capwidth = 0;
head = procstat_getfiles(procstat, kipp, 0);
if (head != NULL && Cflag) {
STAILQ_FOREACH(fst, head, next) {
width = width_capability(fst->fs_cap_rights);
if (width > capwidth)
capwidth = width;
}
if (capwidth < strlen("CAPABILITIES"))
capwidth = strlen("CAPABILITIES");
}
if (!hflag) {
if (Cflag)
printf("%5s %-16s %4s %1s %-9s %-*s "
"%-3s %-12s\n", "PID", "COMM", "FD", "T",
"FLAGS", capwidth, "CAPABILITIES", "PRO",
"NAME");
else
printf("%5s %-16s %4s %1s %1s %-9s "
"%3s %7s %-3s %-12s\n", "PID", "COMM", "FD", "T",
"V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
}
if (head == NULL)
return;
STAILQ_FOREACH(fst, head, next) {
printf("%5d ", kipp->ki_pid);
printf("%-16s ", kipp->ki_comm);
if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
printf("ctty ");
else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
printf(" cwd ");
else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
printf("jail ");
else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
printf("root ");
else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
printf("text ");
else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
printf("trace ");
else
printf("%4d ", fst->fs_fd);
switch (fst->fs_type) {
case PS_FST_TYPE_VNODE:
str = "v";
break;
case PS_FST_TYPE_SOCKET:
str = "s";
break;
case PS_FST_TYPE_PIPE:
str = "p";
break;
case PS_FST_TYPE_FIFO:
str = "f";
break;
case PS_FST_TYPE_KQUEUE:
str = "k";
break;
case PS_FST_TYPE_CRYPTO:
str = "c";
break;
case PS_FST_TYPE_MQUEUE:
str = "m";
break;
case PS_FST_TYPE_SHM:
str = "h";
break;
case PS_FST_TYPE_PTS:
Integrate the new MPSAFE TTY layer to the FreeBSD operating system. The last half year I've been working on a replacement TTY layer for the FreeBSD kernel. The new TTY layer was designed to improve the following: - Improved driver model: The old TTY layer has a driver model that is not abstract enough to make it friendly to use. A good example is the output path, where the device drivers directly access the output buffers. This means that an in-kernel PPP implementation must always convert network buffers into TTY buffers. If a PPP implementation would be built on top of the new TTY layer (still needs a hooks layer, though), it would allow the PPP implementation to directly hand the data to the TTY driver. - Improved hotplugging: With the old TTY layer, it isn't entirely safe to destroy TTY's from the system. This implementation has a two-step destructing design, where the driver first abandons the TTY. After all threads have left the TTY, the TTY layer calls a routine in the driver, which can be used to free resources (unit numbers, etc). The pts(4) driver also implements this feature, which means posix_openpt() will now return PTY's that are created on the fly. - Improved performance: One of the major improvements is the per-TTY mutex, which is expected to improve scalability when compared to the old Giant locking. Another change is the unbuffered copying to userspace, which is both used on TTY device nodes and PTY masters. Upgrading should be quite straightforward. Unlike previous versions, existing kernel configuration files do not need to be changed, except when they reference device drivers that are listed in UPDATING. Obtained from: //depot/projects/mpsafetty/... Approved by: philip (ex-mentor) Discussed: on the lists, at BSDCan, at the DevSummit Sponsored by: Snow B.V., the Netherlands dcons(4) fixed by: kan
2008-08-20 08:31:58 +00:00
str = "t";
break;
case PS_FST_TYPE_SEM:
Rework the lifetime management of the kernel implementation of POSIX semaphores. Specifically, semaphores are now represented as new file descriptor type that is set to close on exec. This removes the need for all of the manual process reference counting (and fork, exec, and exit event handlers) as the normal file descriptor operations handle all of that for us nicely. It is also suggested as one possible implementation in the spec and at least one other OS (OS X) uses this approach. Some bugs that were fixed as a result include: - References to a named semaphore whose name is removed still work after the sem_unlink() operation. Prior to this patch, if a semaphore's name was removed, valid handles from sem_open() would get EINVAL errors from sem_getvalue(), sem_post(), etc. This fixes that. - Unnamed semaphores created with sem_init() were not cleaned up when a process exited or exec'd. They were only cleaned up if the process did an explicit sem_destroy(). This could result in a leak of semaphore objects that could never be cleaned up. - On the other hand, if another process guessed the id (kernel pointer to 'struct ksem' of an unnamed semaphore (created via sem_init)) and had write access to the semaphore based on UID/GID checks, then that other process could manipulate the semaphore via sem_destroy(), sem_post(), sem_wait(), etc. - As part of the permission check (UID/GID), the umask of the proces creating the semaphore was not honored. Thus if your umask denied group read/write access but the explicit mode in the sem_init() call allowed it, the semaphore would be readable/writable by other users in the same group, for example. This includes access via the previous bug. - If the module refused to unload because there were active semaphores, then it might have deregistered one or more of the semaphore system calls before it noticed that there was a problem. I'm not sure if this actually happened as the order that modules are discovered by the kernel linker depends on how the actual .ko file is linked. One can make the order deterministic by using a single module with a mod_event handler that explicitly registers syscalls (and deregisters during unload after any checks). This also fixes a race where even if the sem_module unloaded first it would have destroyed locks that the syscalls might be trying to access if they are still executing when they are unloaded. XXX: By the way, deregistering system calls doesn't do any blocking to drain any threads from the calls. - Some minor fixes to errno values on error. For example, sem_init() isn't documented to return ENFILE or EMFILE if we run out of semaphores the way that sem_open() can. Instead, it should return ENOSPC in that case. Other changes: - Kernel semaphores now use a hash table to manage the namespace of named semaphores nearly in a similar fashion to the POSIX shared memory object file descriptors. Kernel semaphores can now also have names longer than 14 chars (up to MAXPATHLEN) and can include subdirectories in their pathname. - The UID/GID permission checks for access to a named semaphore are now done via vaccess() rather than a home-rolled set of checks. - Now that kernel semaphores have an associated file object, the various MAC checks for POSIX semaphores accept both a file credential and an active credential. There is also a new posixsem_check_stat() since it is possible to fstat() a semaphore file descriptor. - A small set of regression tests (using the ksem API directly) is present in src/tools/regression/posixsem. Reported by: kris (1) Tested by: kris Reviewed by: rwatson (lightly) MFC after: 1 month
2008-06-27 05:39:04 +00:00
str = "e";
break;
case PS_FST_TYPE_NONE:
case PS_FST_TYPE_UNKNOWN:
default:
str = "?";
break;
}
printf("%1s ", str);
if (!Cflag) {
str = "-";
if (fst->fs_type == PS_FST_TYPE_VNODE) {
error = procstat_get_vnode_info(procstat, fst,
&vn, NULL);
switch (vn.vn_type) {
case PS_FST_VTYPE_VREG:
str = "r";
break;
case PS_FST_VTYPE_VDIR:
str = "d";
break;
case PS_FST_VTYPE_VBLK:
str = "b";
break;
case PS_FST_VTYPE_VCHR:
str = "c";
break;
case PS_FST_VTYPE_VLNK:
str = "l";
break;
case PS_FST_VTYPE_VSOCK:
str = "s";
break;
case PS_FST_VTYPE_VFIFO:
str = "f";
break;
case PS_FST_VTYPE_VBAD:
str = "x";
break;
case PS_FST_VTYPE_VNON:
case PS_FST_VTYPE_UNKNOWN:
default:
str = "?";
break;
}
}
printf("%1s ", str);
}
printf("%s", fst->fs_fflags & PS_FST_FFLAG_READ ? "r" : "-");
printf("%s", fst->fs_fflags & PS_FST_FFLAG_WRITE ? "w" : "-");
printf("%s", fst->fs_fflags & PS_FST_FFLAG_APPEND ? "a" : "-");
printf("%s", fst->fs_fflags & PS_FST_FFLAG_ASYNC ? "s" : "-");
printf("%s", fst->fs_fflags & PS_FST_FFLAG_SYNC ? "f" : "-");
printf("%s", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ? "n" : "-");
printf("%s", fst->fs_fflags & PS_FST_FFLAG_DIRECT ? "d" : "-");
printf("%s", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ? "l" : "-");
printf("%s ", fst->fs_fflags & PS_FST_FFLAG_CAPABILITY ?
"c" : "-");
if (!Cflag) {
if (fst->fs_ref_count > -1)
printf("%3d ", fst->fs_ref_count);
else
printf("%3c ", '-');
if (fst->fs_offset > -1)
printf("%7jd ", (intmax_t)fst->fs_offset);
else
printf("%7c ", '-');
}
if (Cflag) {
print_capability(fst->fs_cap_rights, capwidth);
printf(" ");
}
switch (fst->fs_type) {
case PS_FST_TYPE_SOCKET:
error = procstat_get_socket_info(procstat, fst, &sock, NULL);
if (error != 0)
break;
printf("%-3s ",
protocol_to_string(sock.dom_family,
sock.type, sock.proto));
/*
* While generally we like to print two addresses,
* local and peer, for sockets, it turns out to be
* more useful to print the first non-nul address for
* local sockets, as typically they aren't bound and
* connected, and the path strings can get long.
*/
if (sock.dom_family == AF_LOCAL) {
struct sockaddr_un *sun =
(struct sockaddr_un *)&sock.sa_local;
if (sun->sun_path[0] != 0)
print_address(&sock.sa_local);
else
print_address(&sock.sa_peer);
} else {
print_address(&sock.sa_local);
printf(" ");
print_address(&sock.sa_peer);
}
break;
default:
printf("%-3s ", "-");
printf("%-18s", fst->fs_path != NULL ? fst->fs_path : "-");
}
printf("\n");
}
}