2010-03-08 20:45:52 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2010-03-08 20:45:52 +00:00
|
|
|
* Copyright (c) 2010 Konstantin Belousov
|
2015-09-05 17:02:01 +00:00
|
|
|
* Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
|
2010-03-08 20:45:52 +00:00
|
|
|
* 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/sysctl.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-05-12 10:11:39 +00:00
|
|
|
#include <libprocstat.h>
|
2010-03-08 20:45:52 +00:00
|
|
|
|
|
|
|
#include "procstat.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
procstat_print_signame(int sig)
|
|
|
|
{
|
|
|
|
char name[12];
|
|
|
|
int i;
|
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) {
|
2010-03-08 20:45:52 +00:00
|
|
|
strlcpy(name, sys_signame[sig], sizeof(name));
|
|
|
|
for (i = 0; name[i] != 0; i++)
|
|
|
|
name[i] = toupper(name[i]);
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{d:signal/%-7s/%s} ", name);
|
|
|
|
xo_open_container(name);
|
|
|
|
} else {
|
|
|
|
xo_emit("{d:signal/%-7d/%d} ", sig);
|
|
|
|
snprintf(name, 12, "%d", sig);
|
|
|
|
xo_open_container(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
procstat_close_signame(int sig)
|
|
|
|
{
|
|
|
|
char name[12];
|
|
|
|
int i;
|
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) {
|
2015-09-05 17:02:01 +00:00
|
|
|
strlcpy(name, sys_signame[sig], sizeof(name));
|
|
|
|
for (i = 0; name[i] != 0; i++)
|
|
|
|
name[i] = toupper(name[i]);
|
|
|
|
xo_close_container(name);
|
2010-03-08 20:45:52 +00:00
|
|
|
} else
|
2015-09-05 17:02:01 +00:00
|
|
|
snprintf(name, 12, "%d", sig);
|
|
|
|
xo_close_container(name);
|
2010-03-08 20:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
procstat_print_sig(const sigset_t *set, int sig, char flag)
|
|
|
|
{
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{d:sigmember/%c}", sigismember(set, sig) ? flag : '-');
|
|
|
|
switch (flag) {
|
|
|
|
case 'B':
|
|
|
|
xo_emit("{en:mask/%s}", sigismember(set, sig) ?
|
|
|
|
"true" : "false");
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
xo_emit("{en:catch/%s}", sigismember(set, sig) ?
|
|
|
|
"true" : "false");
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
xo_emit("{en:list/%s}", sigismember(set, sig) ?
|
|
|
|
"true" : "false");
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
xo_emit("{en:ignore/%s}", sigismember(set, sig) ?
|
|
|
|
"true" : "false");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xo_emit("{en:unknown/%s}", sigismember(set, sig) ?
|
|
|
|
"true" : "false");
|
|
|
|
break;
|
|
|
|
}
|
2010-03-08 20:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp)
|
2010-03-08 20:45:52 +00:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
if ((procstat_opts & PS_OPT_NOHEADER) == 0)
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{T:/%5s %-16s %-7s %4s}\n", "PID", "COMM", "SIG",
|
|
|
|
"FLAGS");
|
2010-03-08 20:45:52 +00:00
|
|
|
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
|
|
|
|
xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
|
|
|
|
xo_open_container("signals");
|
2010-03-08 20:45:52 +00:00
|
|
|
for (j = 1; j <= _SIG_MAXSIG; j++) {
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
|
|
|
|
xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
|
2010-03-08 20:45:52 +00:00
|
|
|
procstat_print_signame(j);
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit(" ");
|
2010-03-08 20:45:52 +00:00
|
|
|
procstat_print_sig(&kipp->ki_siglist, j, 'P');
|
|
|
|
procstat_print_sig(&kipp->ki_sigignore, j, 'I');
|
|
|
|
procstat_print_sig(&kipp->ki_sigcatch, j, 'C');
|
2015-09-05 17:02:01 +00:00
|
|
|
procstat_close_signame(j);
|
|
|
|
xo_emit("\n");
|
2010-03-08 20:45:52 +00:00
|
|
|
}
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_close_container("signals");
|
2010-03-08 20:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-20 07:50:59 +00:00
|
|
|
procstat_threads_sigs(struct procstat *procstat, struct kinfo_proc *kipp)
|
2010-03-08 20:45:52 +00:00
|
|
|
{
|
|
|
|
struct kinfo_proc *kip;
|
2013-04-20 07:50:59 +00:00
|
|
|
int j;
|
|
|
|
unsigned int count, i;
|
2015-09-05 17:02:01 +00:00
|
|
|
char *threadid;
|
2010-03-08 20:45:52 +00:00
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
if ((procstat_opts & PS_OPT_NOHEADER) == 0)
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{T:/%5s %6s %-16s %-7s %4s}\n", "PID", "TID", "COMM",
|
2010-03-08 20:45:52 +00:00
|
|
|
"SIG", "FLAGS");
|
|
|
|
|
2013-04-20 07:50:59 +00:00
|
|
|
kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
|
2015-09-05 17:02:01 +00:00
|
|
|
kipp->ki_pid, &count);
|
2010-03-08 20:45:52 +00:00
|
|
|
if (kip == NULL)
|
|
|
|
return;
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
|
|
|
|
xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
|
|
|
|
xo_open_container("threads");
|
2013-04-20 07:50:59 +00:00
|
|
|
kinfo_proc_sort(kip, count);
|
|
|
|
for (i = 0; i < count; i++) {
|
2010-03-08 20:45:52 +00:00
|
|
|
kipp = &kip[i];
|
2015-09-05 17:02:01 +00:00
|
|
|
asprintf(&threadid, "%d", kipp->ki_tid);
|
|
|
|
if (threadid == NULL)
|
|
|
|
xo_errc(1, ENOMEM, "Failed to allocate memory in "
|
|
|
|
"procstat_threads_sigs()");
|
|
|
|
xo_open_container(threadid);
|
|
|
|
xo_emit("{e:thread_id/%6d/%d}", kipp->ki_tid);
|
|
|
|
xo_open_container("signals");
|
2010-03-08 20:45:52 +00:00
|
|
|
for (j = 1; j <= _SIG_MAXSIG; j++) {
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
|
|
|
|
xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid);
|
|
|
|
xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
|
2010-03-08 20:45:52 +00:00
|
|
|
procstat_print_signame(j);
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_emit(" ");
|
2010-03-08 20:45:52 +00:00
|
|
|
procstat_print_sig(&kipp->ki_siglist, j, 'P');
|
|
|
|
procstat_print_sig(&kipp->ki_sigmask, j, 'B');
|
2015-09-05 17:02:01 +00:00
|
|
|
procstat_close_signame(j);
|
|
|
|
xo_emit("\n");
|
2010-03-08 20:45:52 +00:00
|
|
|
}
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_close_container("signals");
|
|
|
|
xo_close_container(threadid);
|
|
|
|
free(threadid);
|
2010-03-08 20:45:52 +00:00
|
|
|
}
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_close_container("threads");
|
2013-04-20 07:50:59 +00:00
|
|
|
procstat_freeprocs(procstat, kip);
|
2010-03-08 20:45:52 +00:00
|
|
|
}
|