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