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 83 static void 84 procstat_print_sig(const sigset_t *set, int sig, char flag) 85 { 86 xo_emit("{d:sigmember/%c}", sigismember(set, sig) ? flag : '-'); 87 switch (flag) { 88 case 'B': 89 xo_emit("{en:mask/%s}", sigismember(set, sig) ? 90 "true" : "false"); 91 break; 92 case 'C': 93 xo_emit("{en:catch/%s}", sigismember(set, sig) ? 94 "true" : "false"); 95 break; 96 case 'P': 97 xo_emit("{en:list/%s}", sigismember(set, sig) ? 98 "true" : "false"); 99 break; 100 case 'I': 101 xo_emit("{en:ignore/%s}", sigismember(set, sig) ? 102 "true" : "false"); 103 break; 104 default: 105 xo_emit("{en:unknown/%s}", sigismember(set, sig) ? 106 "true" : "false"); 107 break; 108 } 109 } 110 111 void 112 procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp) 113 { 114 int j; 115 116 if ((procstat_opts & PS_OPT_NOHEADER) == 0) 117 xo_emit("{T:/%5s %-16s %-7s %4s}\n", "PID", "COMM", "SIG", 118 "FLAGS"); 119 120 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 121 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 122 xo_open_container("signals"); 123 for (j = 1; j <= _SIG_MAXSIG; j++) { 124 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 125 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 126 procstat_print_signame(j); 127 xo_emit(" "); 128 procstat_print_sig(&kipp->ki_siglist, j, 'P'); 129 procstat_print_sig(&kipp->ki_sigignore, j, 'I'); 130 procstat_print_sig(&kipp->ki_sigcatch, j, 'C'); 131 procstat_close_signame(j); 132 xo_emit("\n"); 133 } 134 xo_close_container("signals"); 135 } 136 137 void 138 procstat_threads_sigs(struct procstat *procstat, struct kinfo_proc *kipp) 139 { 140 struct kinfo_proc *kip; 141 int j; 142 unsigned int count, i; 143 char *threadid; 144 145 if ((procstat_opts & PS_OPT_NOHEADER) == 0) 146 xo_emit("{T:/%5s %6s %-16s %-7s %4s}\n", "PID", "TID", "COMM", 147 "SIG", "FLAGS"); 148 149 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, 150 kipp->ki_pid, &count); 151 if (kip == NULL) 152 return; 153 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid); 154 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm); 155 xo_open_container("threads"); 156 kinfo_proc_sort(kip, count); 157 for (i = 0; i < count; i++) { 158 kipp = &kip[i]; 159 asprintf(&threadid, "%d", kipp->ki_tid); 160 if (threadid == NULL) 161 xo_errc(1, ENOMEM, "Failed to allocate memory in " 162 "procstat_threads_sigs()"); 163 xo_open_container(threadid); 164 xo_emit("{e:thread_id/%6d/%d}", kipp->ki_tid); 165 xo_open_container("signals"); 166 for (j = 1; j <= _SIG_MAXSIG; j++) { 167 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid); 168 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid); 169 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm); 170 procstat_print_signame(j); 171 xo_emit(" "); 172 procstat_print_sig(&kipp->ki_siglist, j, 'P'); 173 procstat_print_sig(&kipp->ki_sigmask, j, 'B'); 174 procstat_close_signame(j); 175 xo_emit("\n"); 176 } 177 xo_close_container("signals"); 178 xo_close_container(threadid); 179 free(threadid); 180 } 181 xo_close_container("threads"); 182 procstat_freeprocs(procstat, kip); 183 } 184