summaryrefslogtreecommitdiffstats
path: root/src/ps/signames.c
blob: 129a4c25997eb5a58530e634f7f836bc201eb405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * signames.c - ps signal names
 *
 * Copyright © 2023      Craig Small <csmall@dropbear.xyz>
 * Copyright © 2020      Luis Chamberlain <mcgrof@kernel.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <ctype.h>
#include <pwd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>

#include "common.h"
#include "signals.h"

/* For libraries like musl */
#ifndef __SIGRTMIN
#define __SIGRTMIN SIGRTMIN
#endif
#ifndef __SIGRTMAX
#define __SIGRTMAX SIGRTMAX
#endif

/*
 * The actual list of unsupported signals varies by operating system. This
 * program is Linux specific as it processes /proc/ for signal information and
 * there is no generic way to extract each process signal information for each
 * OS. This program also relies on Linux glibc defines to figure out which
 * signals are reserved for use by libc and then which ones are real time
 * specific.
 */

/*
 * As per glibc:
 *
 * A system that defines real time signals overrides __SIGRTMAX to be something
 * other than __SIGRTMIN. This also means we can count on __SIGRTMIN being the
 * first real time signal, meaning what Linux programs it for your architecture
 * in the kernel. SIGRTMIN then will be the application specific first real
 * time signal, that is, on top of libc. The values in between
 *
 * 	__SIGRTMIN .. SIGRTMIN
 *
 * are used by * libc, typically for helping threading implementation.
 */
static const char *sigstat_strsignal_abbrev(int sig, char *abbrev, size_t len)
{
	memset(abbrev, '\0', len);

	if (sig == 0 || sig >= NSIG) {
		snprintf(abbrev, len, "BOGUS_%02d", sig - _NSIG);
		return abbrev;
	}

	/*
	 * The standard lower signals we can count on this being the kernel
	 * specific SIGRTMIN.
	 */
	if (sig < __SIGRTMIN) {
                const char *signame = NULL;
#ifdef HAVE_SIGABBREV_NP
                signame = sigabbrev_np(sig);
#else
                signame = signal_number_to_name(sig);
#endif /* HAVE_SIGABBREV_NP */
                if (signame != NULL && signame[0] != '\0') {
                    strncpy(abbrev, signame, len);
                    return abbrev;
                }
	}

	/* This means your system should *not* have realtime signals */
	if (__SIGRTMAX == __SIGRTMIN) {
		snprintf(abbrev, len, "INVALID_%02d", sig);
		return abbrev;
	}

	/*
	 * If we're dealing with a libc real time signal start counting
	 * after libc's version of SIGRTMIN
	 */
	if (sig >= SIGRTMIN) {
		if (sig == SIGRTMIN)
			snprintf(abbrev, len, "RTMIN");
		else if (sig == SIGRTMAX)
			snprintf(abbrev, len, "RTMAX");
		else
			snprintf(abbrev, len, "RTMIN+%02d", sig - SIGRTMIN);
	} else
		snprintf(abbrev, len, "LIBC+%02d", sig - __SIGRTMIN);

	return abbrev;
}

/*
 * For instance SIGTERM is 15, but its actual mask value is
 * 1 << (15-1) = 0x4000
 */
static uint64_t mask_sig_val_num(int signum)
{
	return ((uint64_t) 1 << (signum -1));
}

int print_signame(char *restrict const outbuf, const char *restrict const sig, const size_t len_in)
{
	unsigned int i;
	char abbrev[PATH_MAX];
	unsigned int n = 0;
	char *c = outbuf;
        size_t len = len_in;
	uint64_t mask, mask_in;
	uint64_t test_val = 0;

        if (1 != sscanf(sig, "%" PRIu64, &mask_in))
            return 0;
        mask = mask_in;

	for (i=1; i < NSIG; i++) {
		test_val = mask_sig_val_num(i);
		if (test_val & mask) {
                        n = strlen(sigstat_strsignal_abbrev(i, abbrev, PATH_MAX));
                        if (n+1 >= len) { // +1 for the '+'
                            strcpy(c, "+");
                            len -= 1;
                            c += 1;
                            break;
                        } else {
			    n = snprintf(c, len, (c==outbuf)?"%s":",%s",
				     sigstat_strsignal_abbrev(i, abbrev,
					   		      PATH_MAX));
			    len -= n;
			    c+=n;
                        }
		}
	}
        if (c == outbuf) {
            n = snprintf(c, len, "%c", '-');
            len -= n;
            c += n;
        }
	return (int) (c-outbuf);
}