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/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 <stdbool.h>
39 #include <stdint.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
procstat_print_signame(int sig)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
procstat_close_signame(int sig)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
procstat_print_sig(const sigset_t * set,int sig,char flag)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
procstat_sigs(struct procstat * prstat __unused,struct kinfo_proc * kipp)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
procstat_threads_sigs(struct procstat * procstat,struct kinfo_proc * kipp)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
167 for (j = 1; j <= _SIG_MAXSIG; j++) {
168 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
169 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid);
170 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
171 procstat_print_signame(j);
172 xo_emit(" ");
173 procstat_print_sig(&kipp->ki_siglist, j, 'P');
174 procstat_print_sig(&kipp->ki_sigmask, j, 'B');
175 procstat_close_signame(j);
176 xo_emit("\n");
177 }
178 xo_close_container("signals");
179 xo_close_container(threadid);
180 free(threadid);
181 }
182 xo_close_container("threads");
183 procstat_freeprocs(procstat, kip);
184 }
185
186 void
procstat_sigfastblock(struct procstat * procstat,struct kinfo_proc * kipp)187 procstat_sigfastblock(struct procstat *procstat, struct kinfo_proc *kipp)
188 {
189 struct kinfo_proc *kip;
190 char *threadid;
191 uintptr_t sigfastblk_addr;
192 int error, name[4];
193 unsigned int count, i;
194 size_t len;
195 bool has_sigfastblk_addr;
196
197 if ((procstat_opts & PS_OPT_NOHEADER) == 0)
198 xo_emit("{T:/%5s %6s %-16s %-16s}\n", "PID", "TID",
199 "COMM", "SIGFBLK");
200
201 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
202 kipp->ki_pid, &count);
203 if (kip == NULL)
204 return;
205 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
206 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
207 xo_open_container("threads");
208 kinfo_proc_sort(kip, count);
209 for (i = 0; i < count; i++) {
210 kipp = &kip[i];
211 len = sizeof(sigfastblk_addr);
212 name[0] = CTL_KERN;
213 name[1] = KERN_PROC;
214 name[2] = KERN_PROC_SIGFASTBLK;
215 name[3] = kipp->ki_tid;
216 error = sysctl(name, 4, &sigfastblk_addr, &len, NULL, 0);
217 if (error < 0) {
218 if (errno != ESRCH && errno != ENOTTY) {
219 warn("sysctl: kern.proc.fastsigblk: %d",
220 kipp->ki_tid);
221 }
222 has_sigfastblk_addr = false;
223 } else
224 has_sigfastblk_addr = true;
225
226 asprintf(&threadid, "%d", kipp->ki_tid);
227 if (threadid == NULL)
228 xo_errc(1, ENOMEM, "Failed to allocate memory in "
229 "procstat_sigfastblock()");
230 xo_open_container(threadid);
231 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
232 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid);
233 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
234 xo_emit("{e:sigfastblock/%#-16jx/%#jx}", has_sigfastblk_addr ?
235 (uintmax_t)sigfastblk_addr : (uintmax_t)-1);
236 xo_emit("{d:sigfastblock/%#-16jx/%#jx}", has_sigfastblk_addr ?
237 (uintmax_t)sigfastblk_addr : (uintmax_t)-1);
238 xo_emit("\n");
239 xo_close_container(threadid);
240 free(threadid);
241 }
242 xo_close_container("threads");
243 procstat_freeprocs(procstat, kip);
244 }
245