diff --git a/usr.bin/procstat/Makefile b/usr.bin/procstat/Makefile --- a/usr.bin/procstat/Makefile +++ b/usr.bin/procstat/Makefile @@ -5,6 +5,7 @@ PROG= procstat MAN= procstat.1 SRCS= procstat.c \ + procstat_advlock.c \ procstat_args.c \ procstat_auxv.c \ procstat_basic.c \ diff --git a/usr.bin/procstat/procstat.h b/usr.bin/procstat/procstat.h --- a/usr.bin/procstat/procstat.h +++ b/usr.bin/procstat/procstat.h @@ -44,6 +44,7 @@ PS_OPT_SIGNUM = 0x08, PS_OPT_VERBOSE = 0x10, PS_MODE_COMPAT = 0x20, + PS_MODE_NO_KINFO_PROC = 0x40, }; #define PS_SUBCOMMAND_OPTS \ @@ -56,6 +57,7 @@ void kinfo_proc_sort(struct kinfo_proc *kipp, int count); const char * kinfo_proc_thread_name(const struct kinfo_proc *kipp); +void procstat_advlocks(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_args(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_auxv(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_basic(struct procstat *prstat, struct kinfo_proc *kipp); diff --git a/usr.bin/procstat/procstat.1 b/usr.bin/procstat/procstat.1 --- a/usr.bin/procstat/procstat.1 +++ b/usr.bin/procstat/procstat.1 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd February 13, 2021 +.Dd April 7, 2022 .Dt PROCSTAT 1 .Os .Sh NAME @@ -144,6 +144,22 @@ The following commands are available for .Nm : .Bl -tag -width indent +.It Ar advlock +Print information about advisory locks on files. +All three types of locks are listed, BSD-style +.Xr lockf 2 , +POSIX-style +.Xr fcntl 2 +.Va F_SETLK , +and remote +.Xr lockd 8 +locks used by NFSv3. +Note that neither the +.Fl a +option nor +.Va pid +list can be used to limit the display of the locks, mostly because +some types of locks do not have local (or any) owning processes. .It Ar basic Print basic process statistics (this is the default). .It Ar binary | Fl b diff --git a/usr.bin/procstat/procstat.c b/usr.bin/procstat/procstat.c --- a/usr.bin/procstat/procstat.c +++ b/usr.bin/procstat/procstat.c @@ -87,6 +87,8 @@ /* procstat parameters and arguments */ static const struct procstat_cmd cmd_table[] = { + { "advlock", "advisory_locks", NULL, &procstat_advlocks, &cmdopt_none, + PS_CMP_PLURAL | PS_CMP_SUBSTR | PS_MODE_NO_KINFO_PROC }, { "argument", "arguments", NULL, &procstat_args, &cmdopt_none, PS_CMP_PLURAL | PS_CMP_SUBSTR }, { "auxv", "auxv", NULL, &procstat_auxv, &cmdopt_none, PS_CMP_NORMAL }, @@ -449,7 +451,8 @@ } /* Must specify either the -a flag or a list of pids. */ - if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0)) + if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0) && + (cmd->cmp & PS_MODE_NO_KINFO_PROC) == 0) usage(cmd); if (memf != NULL) @@ -465,6 +468,11 @@ xo_open_container(progname); xo_open_container(xocontainer); + if ((cmd->cmp & PS_MODE_NO_KINFO_PROC) != 0) { + cmd->cmd(prstat, NULL); + goto iter; + } + if (aflag) { p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt); if (p == NULL) @@ -520,6 +528,7 @@ } } +iter: xo_close_container(xocontainer); xo_close_container(progname); xo_finish(); diff --git a/usr.bin/procstat/procstat_advlock.c b/usr.bin/procstat/procstat_advlock.c new file mode 100644 --- /dev/null +++ b/usr.bin/procstat/procstat_advlock.c @@ -0,0 +1,102 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2022 The FreeBSD Foundation + * + * Portions of this software were developed by Konstantin Belousov + * under sponsorship from the FreeBSD Foundation. + * + * 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. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "procstat.h" + +void +procstat_advlocks(struct procstat *prstat, struct kinfo_proc *kipp __unused) +{ + struct advlock_list *advl; + struct advlock *a; + static const char advisory_lock_item[] = "advisory_lock"; + + if ((procstat_opts & PS_OPT_NOHEADER) == 0) + xo_emit("{T:/%-3s %-5s %-5s %-6s %-18s %-18s " + "%-8s %-9s %-9s %-12s}\n", "RW", "TYPE", "PID", "SYSID", + "FSID", "RDEV", "INO", "START", "LEN", "PATH"); + + xo_open_list(advisory_lock_item); + advl = procstat_getadvlock(prstat); + if (advl == NULL) { + xo_close_list(advisory_lock_item); + return; + } + + STAILQ_FOREACH(a, advl, next) { + xo_open_instance(advisory_lock_item); + switch (a->rw) { + case PS_ADVLOCK_RO: + xo_emit("{:rw/%s} ", "RO"); + break; + case PS_ADVLOCK_RW: + xo_emit("{:rw/%s} ", "RW"); + break; + default: + xo_emit("{:rw/%s} ", "??"); + break; + } + switch (a->type) { + case PS_ADVLOCK_TYPE_FLOCK: + xo_emit("{:type/%s} ", "FLOCK"); + break; + case PS_ADVLOCK_TYPE_PID: + xo_emit("{:type/%s} ", "FCNTL"); + break; + case PS_ADVLOCK_TYPE_REMOTE: + xo_emit("{:type/%s} ", "LOCKD"); + break; + default: + xo_emit("{:type/%s} ", "?????"); + break; + } + xo_emit("{:pid/%5d} ", a->pid); + xo_emit("{:sysid/%5d} ", a->sysid); + xo_emit("{:fsid/%18#jx} ", (uintmax_t)a->file_fsid); + xo_emit("{:rdev/%#18jx} ", (uintmax_t)a->file_rdev); + xo_emit("{:ino/%8ju} ", (uintmax_t)a->file_fileid); + xo_emit("{:start/%9ju} ", (uintmax_t)a->start); + xo_emit("{:len/%9ju} ", (uintmax_t)a->len); + xo_emit("{:path/%s}", a->path == NULL ? "" : a->path); + xo_emit("\n"); + xo_close_instance(advisory_lock_item); + } + xo_close_list(advisory_lock_item); + procstat_freeadvlock(prstat, advl); +}