1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2010 Konstantin Belousov 5 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/sysctl.h> 34 #include <sys/user.h> 35 36 #include <ctype.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <signal.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <libprocstat.h> 44 45 #include "procstat.h" 46 47 static void 48 procstat_print_signame(int sig) 49 { 50 char name[12]; 51 int i; 52 53 if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) { 54 strlcpy(name, sys_signame[sig], sizeof(name)); 55 for (i = 0; name[i] != 0; i++) 56 name[i] = toupper(name[i]); 57 xo_emit("{d:signal/%-7s/%s} ", name); 58 xo_open_container(name); 59 } else { 60 xo_emit("{d:signal/%-7d/%d} ", sig); 61 snprintf(name, 12, "%d", sig); 62 xo_open_container(name); 63 } 64 } 65 66 static void 67 procstat_close_signame(int sig) 68 { 69 char name[12]; 70 int i; 71 72 if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) { 73 strlcpy(name, sys_signame[sig], sizeof(name)); 74 for (i = 0; name[i] != 0; i++) 75 name[i] = toupper(name[i]); 76 xo_close_container(name); 77 } else 78 snprintf(name, 12, "%d", sig); 79 xo_close_container(name); 80 } 81 82 static void 83 procstat_print_sig(const sigset_t *set, int sig, char flag) 84 { 85 xo_emit("{d:sigmember/%c}", sigismember(set, sig) ? flag : '-'); 86 switch (flag) { 87 case 'B': 88 xo_emit("{en:mask/%s}", sigismember(set, sig) ? 89 "true" : "false"); 90 break; 91 case 'C': 92 xo_emit("{en:catch/%s}", sigismember(set, sig) ? 93 "true" : "false"); 94 break; 95 case 'P': 96 xo_emit("{en:list/%s}", sigismember(set, sig) ? 97 "true" : "false"); 98 break; 99 case 'I': 100 xo_emit("{en:ignore/%s}", sigismember(set, sig) ? 101 "true" : "false"); 102 break; 103 default: 104 xo_emit("{en:unknown/%s}", sigismember(set, sig) ? 105 "true" : "false"); 106 break; 107 } 108 } 109 110 void 111 procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) 112 { 113 int j; 114 115 if ((procstat_opts & PS_OPT_NOHEADER) == 0) 116 xo_emit("{T:/%5s %-16s %-7s %4s}\n", "PID", "COMM", "SIG", 117 "FLAGS"); 118 119 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 120 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 121 xo_open_container("signals"); 122 for (j = 1; j <= _SIG_MAXSIG; j++) { 123 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 124 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 125 procstat_print_signame(j); 126 xo_emit(" "); 127 procstat_print_sig(&kipp->ki_siglist, j, 'P'); 128 procstat_print_sig(&kipp->ki_sigignore, j, 'I'); 129 procstat_print_sig(&kipp->ki_sigcatch, j, 'C'); 130 procstat_close_signame(j); 131 xo_emit("\n"); 132 } 133 xo_close_container("signals"); 134 } 135 136 void 137 procstat_threads_sigs(struct procstat *procstat, struct kinfo_proc *kipp) 138 { 139 struct kinfo_proc *kip; 140 int j; 141 unsigned int count, i; 142 char *threadid; 143 144 if ((procstat_opts & PS_OPT_NOHEADER) == 0) 145 xo_emit("{T:/%5s %6s %-16s %-7s %4s}\n", "PID", "TID", "COMM", 146 "SIG", "FLAGS"); 147 148 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, 149 kipp->ki_pid, &count); 150 if (kip == NULL) 151 return; 152 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 153 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 154 xo_open_container("threads"); 155 kinfo_proc_sort(kip, count); 156 for (i = 0; i < count; i++) { 157 kipp = &kip[i]; 158 asprintf(&threadid, "%d", kipp->ki_tid); 159 if (threadid == NULL) 160 xo_errc(1, ENOMEM, "Failed to allocate memory in " 161 "procstat_threads_sigs()"); 162 xo_open_container(threadid); 163 xo_emit("{e:thread_id/%6d/%d}", kipp->ki_tid); 164 xo_open_container("signals"); 165 for (j = 1; j <= _SIG_MAXSIG; j++) { 166 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 167 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid); 168 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 169 procstat_print_signame(j); 170 xo_emit(" "); 171 procstat_print_sig(&kipp->ki_siglist, j, 'P'); 172 procstat_print_sig(&kipp->ki_sigmask, j, 'B'); 173 procstat_close_signame(j); 174 xo_emit("\n"); 175 } 176 xo_close_container("signals"); 177 xo_close_container(threadid); 178 free(threadid); 179 } 180 xo_close_container("threads"); 181 procstat_freeprocs(procstat, kip); 182 } 183