1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 30 #include <sys/cdefs.h> 31 #include <sys/param.h> 32 #include <sys/sysctl.h> 33 #include <sys/user.h> 34 35 #include <ctype.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <signal.h> 39 #include <stdbool.h> 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <libprocstat.h> 45 46 #include "procstat.h" 47 48 static void 49 procstat_print_signame(int sig) 50 { 51 char name[12]; 52 int i; 53 54 if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) { 55 strlcpy(name, sys_signame[sig], sizeof(name)); 56 for (i = 0; name[i] != 0; i++) 57 name[i] = toupper(name[i]); 58 xo_emit("{d:signal/%-7s/%s} ", name); 59 xo_open_container(name); 60 } else { 61 xo_emit("{d:signal/%-7d/%d} ", sig); 62 snprintf(name, 12, "%d", sig); 63 xo_open_container(name); 64 } 65 } 66 67 static void 68 procstat_close_signame(int sig) 69 { 70 char name[12]; 71 int i; 72 73 if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) { 74 strlcpy(name, sys_signame[sig], sizeof(name)); 75 for (i = 0; name[i] != 0; i++) 76 name[i] = toupper(name[i]); 77 xo_close_container(name); 78 } else { 79 snprintf(name, 12, "%d", sig); 80 xo_close_container(name); 81 } 82 } 83 84 static void 85 procstat_print_sig(const sigset_t *set, int sig, char flag) 86 { 87 xo_emit("{d:sigmember/%c}", sigismember(set, sig) ? flag : '-'); 88 switch (flag) { 89 case 'B': 90 xo_emit("{en:mask/%s}", sigismember(set, sig) ? 91 "true" : "false"); 92 break; 93 case 'C': 94 xo_emit("{en:catch/%s}", sigismember(set, sig) ? 95 "true" : "false"); 96 break; 97 case 'P': 98 xo_emit("{en:list/%s}", sigismember(set, sig) ? 99 "true" : "false"); 100 break; 101 case 'I': 102 xo_emit("{en:ignore/%s}", sigismember(set, sig) ? 103 "true" : "false"); 104 break; 105 default: 106 xo_emit("{en:unknown/%s}", sigismember(set, sig) ? 107 "true" : "false"); 108 break; 109 } 110 } 111 112 void 113 procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) 114 { 115 int j; 116 117 if ((procstat_opts & PS_OPT_NOHEADER) == 0) 118 xo_emit("{T:/%5s %-16s %-7s %4s}\n", "PID", "COMM", "SIG", 119 "FLAGS"); 120 121 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 122 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 123 xo_open_container("signals"); 124 for (j = 1; j <= _SIG_MAXSIG; j++) { 125 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 126 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 127 procstat_print_signame(j); 128 xo_emit(" "); 129 procstat_print_sig(&kipp->ki_siglist, j, 'P'); 130 procstat_print_sig(&kipp->ki_sigignore, j, 'I'); 131 procstat_print_sig(&kipp->ki_sigcatch, j, 'C'); 132 procstat_close_signame(j); 133 xo_emit("\n"); 134 } 135 xo_close_container("signals"); 136 } 137 138 void 139 procstat_threads_sigs(struct procstat *procstat, struct kinfo_proc *kipp) 140 { 141 struct kinfo_proc *kip; 142 int j; 143 unsigned int count, i; 144 char *threadid; 145 146 if ((procstat_opts & PS_OPT_NOHEADER) == 0) 147 xo_emit("{T:/%5s %6s %-16s %-7s %4s}\n", "PID", "TID", "COMM", 148 "SIG", "FLAGS"); 149 150 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, 151 kipp->ki_pid, &count); 152 if (kip == NULL) 153 return; 154 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 155 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 156 xo_open_container("threads"); 157 kinfo_proc_sort(kip, count); 158 for (i = 0; i < count; i++) { 159 kipp = &kip[i]; 160 asprintf(&threadid, "%d", kipp->ki_tid); 161 if (threadid == NULL) 162 xo_errc(1, ENOMEM, "Failed to allocate memory in " 163 "procstat_threads_sigs()"); 164 xo_open_container(threadid); 165 xo_emit("{e:thread_id/%6d/%d}", kipp->ki_tid); 166 xo_open_container("signals"); 167 168 for (j = 1; j <= _SIG_MAXSIG; j++) { 169 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 170 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid); 171 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 172 procstat_print_signame(j); 173 xo_emit(" "); 174 procstat_print_sig(&kipp->ki_siglist, j, 'P'); 175 procstat_print_sig(&kipp->ki_sigmask, j, 'B'); 176 procstat_close_signame(j); 177 xo_emit("\n"); 178 } 179 xo_close_container("signals"); 180 xo_close_container(threadid); 181 free(threadid); 182 } 183 xo_close_container("threads"); 184 procstat_freeprocs(procstat, kip); 185 } 186 187 void 188 procstat_sigfastblock(struct procstat *procstat, struct kinfo_proc *kipp) 189 { 190 struct kinfo_proc *kip; 191 char *threadid; 192 uintptr_t sigfastblk_addr; 193 int error, name[4]; 194 unsigned int count, i; 195 size_t len; 196 bool has_sigfastblk_addr; 197 198 if ((procstat_opts & PS_OPT_NOHEADER) == 0) 199 xo_emit("{T:/%5s %6s %-16s %-16s}\n", "PID", "TID", 200 "COMM", "SIGFBLK"); 201 202 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, 203 kipp->ki_pid, &count); 204 if (kip == NULL) 205 return; 206 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 207 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 208 xo_open_container("threads"); 209 kinfo_proc_sort(kip, count); 210 for (i = 0; i < count; i++) { 211 kipp = &kip[i]; 212 len = sizeof(sigfastblk_addr); 213 name[0] = CTL_KERN; 214 name[1] = KERN_PROC; 215 name[2] = KERN_PROC_SIGFASTBLK; 216 name[3] = kipp->ki_tid; 217 error = sysctl(name, 4, &sigfastblk_addr, &len, NULL, 0); 218 if (error < 0) { 219 if (errno != ESRCH && errno != ENOTTY) { 220 warn("sysctl: kern.proc.fastsigblk: %d", 221 kipp->ki_tid); 222 } 223 has_sigfastblk_addr = false; 224 } else 225 has_sigfastblk_addr = true; 226 227 asprintf(&threadid, "%d", kipp->ki_tid); 228 if (threadid == NULL) 229 xo_errc(1, ENOMEM, "Failed to allocate memory in " 230 "procstat_sigfastblock()"); 231 xo_open_container(threadid); 232 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 233 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid); 234 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 235 xo_emit("{e:sigfastblock/%#-16jx/%#jx}", has_sigfastblk_addr ? 236 (uintmax_t)sigfastblk_addr : (uintmax_t)-1); 237 xo_emit("{d:sigfastblock/%#-16jx/%#jx}", has_sigfastblk_addr ? 238 (uintmax_t)sigfastblk_addr : (uintmax_t)-1); 239 xo_emit("\n"); 240 xo_close_container(threadid); 241 free(threadid); 242 } 243 xo_close_container("threads"); 244 procstat_freeprocs(procstat, kip); 245 } 246