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