File: check-sigs.c

package info (click to toggle)
pdksh 5.2.13-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 1,776 kB
  • ctags: 2,226
  • sloc: ansic: 23,133; perl: 770; makefile: 585; sh: 279; sed: 40
file content (255 lines) | stat: -rw-r--r-- 4,909 bytes parent folder | download | duplicates (7)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* A simple program to print information about signal handlers */

#include "config.h"
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#ifdef HAVE_STRING_H
# include <string.h>
#else
# include <strings.h>
# define strchr index
# define strrchr rindex
#endif /* HAVE_STRING_H */
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */

#ifndef SIG_ERR
# define SIG_ERR	((RETSIGTYPE (*)()) -1)
#endif /* SIG_ERR */

/* must be similar to struct Trap in sh.h */
struct signal_info {
	int	signal;
	char	*name;
	char	*mess;
};
struct signal_info siginfo[] = {
	{ 0, "0", "Signal 0" },
#include "siglist.out"
	{ 0, (char *) 0, (char *)0  },
};

int		usage();
#if 0
RETSIGTYPE	sig_catcher();
#endif /* 0 */
char		*signal_name();
#ifndef HAVE_STRERROR
char		*strerror(int);
#endif /* !HAVE_STRERROR */

char	*progname =	"check-sigs";
int	caught_sigs;

int
main(argc, argv)
	int argc;
	char **argv;
{
	extern int	optind;
	extern char	*optarg;
	extern int	errno;

	int		opt;
	int		i;
	int		eno;
	int		report_all = 0;
	RETSIGTYPE	(*f)();
	char		*ofile = (char *) 0;
	char		*s;
	int		wait_forever = 0;

	if (argc > 0 && argv[0] && *argv[0])
		progname = argv[0];

	while ((opt = getopt(argc, argv, "ao:w")) != EOF) {
		switch (opt) {
		case 'a':
			report_all = 1;
			break;

		case 'o':
			ofile = optarg;
			break;

		case 'w':
			wait_forever = 1;
			break;

		default:
			usage(1);
		}
	}

	if (argc != optind)
		usage(0);

	if (ofile && freopen(ofile, "w", stdout) == (FILE *) 0) {
		fprintf(stderr, "%s: Couldn't open output file `%s' - %s\n",
			progname, ofile, strerror(errno));
		exit(1);
	}

	if (!wait_forever) {
		char *blocked = "";
#ifdef POSIX_SIGNALS
		sigset_t mask;

		sigprocmask(SIG_BLOCK, (sigset_t *) 0, &mask);
#endif /* POSIX_SIGNALS */
#ifdef BSD42_SIGNALS
		int mask;

		mask = sigblock(0);
#endif /* BSD42_SIGNALS */
		for (i = 1; i < NSIG; i++) {
			f = signal(i, SIG_DFL);
			eno = errno;
#ifdef BSD42_SIGNALS
			blocked = (mask & sigmask(i)) ? "blocked" : "";
#endif /* BSD42_SIGNALS */
#ifdef POSIX_SIGNALS
			blocked = sigismember(&mask, i) ? "blocked" : "";
#endif /* POSIX_SIGNALS */
			if (f == SIG_DFL && !report_all && !*blocked)
				continue;
			printf("%3d: %7s %30s: ",
				i, blocked, (s = signal_name(i)) ? s : "");
			if (f == SIG_IGN)
				printf("ignored\n");
			else if (f == SIG_ERR)
				printf("error - %s\n", strerror(eno));
			else if (f != SIG_DFL)
				printf("caught - function address %lx\n",
					(long) f);
			else if (report_all)
				printf("default\n");
		}
	}

#if 0	/* code assumes BSD signals - not worth posixizing */
	if (wait_forever) {
		printf("pid is %d\n", getpid());
		sigsetmask(-1L);
		for (i = 0; i < NSIG; i++)
			(void) signal(i, sig_catcher);
		while (1) {
			sigpause(0L);
			for (i = 1; i < NSIG; i++)
				if (caught_sigs & sigmask(i))
					printf("received signal %d - %s\n",
						i,
						(s = signal_name(i)) ? s
								     : "");
			caught_sigs = 0L;
		}
	}
#endif

	return 0;
}

int
usage(verbose)
	int verbose;
{
	fprintf(stderr, "Usage: %s [-?aw] [-o file]\n", progname);
	if (verbose)
		fprintf(stderr, "\
	-a	report on all signals - instead of just non-default signals\n\
	-o f	redirect standard output to file f\n\
	-w	wait forever, reporting all signals sent\n\
");

	exit(1);
	return 0;
}

#if 0
RETSIGTYPE
sig_catcher(sig)
	int sig;
{
	caught_sigs |= sigmask(sig);
	return RETSIGVAL;
}
#endif /* 0 */



char *
signal_name(sig)
	int sig;
{
	static char buf[1024];

#ifdef HAVE_SYS_SIGLIST
# ifndef SYS_SIGLIST_DECLARED
	extern char	*sys_siglist[];
# endif
	/* Use system description, if available... */
	if (sys_siglist[sig] && sys_siglist[sig][0])
		return sys_siglist[sig];
#endif	/* HAVE_SYS_SIGLIST */


	if (sig > 0 && sig < sizeof(siginfo) / sizeof(siginfo[0]) - 1) {
		sprintf(buf, "SIG%s (%s)",
			siginfo[sig].name, siginfo[sig].mess);
		return buf;
	}

	return (char *) 0;
}

#ifndef HAVE_STRERROR
char *
strerror(err)
	int err;
{
	static char	buf[64];
# ifdef HAVE_SYS_ERRLIST
#  ifndef SYS_ERRLIST_DECLARED
	extern int	sys_nerr;
	extern char	*sys_errlist[];
#  endif
	char		*p;

	if (err < 0 || err >= sys_nerr)
		sprintf(p = buf, "Unknown system error %d", err);
	else
		p = sys_errlist[err];
	return p;
# else /* HAVE_SYS_ERRLIST */
	switch (err) {
	  case EINVAL:
		return "Invalid argument";
	  case EACCES:
		return "Permission denied";
	  case ESRCH:
		return "No such process";
	  case EPERM:
		return "Not owner";
	  case ENOENT:
		return "No such file or directory";
	  case ENOTDIR:
		return "Not a directory";
	  case ENOEXEC:
		return "Exec format error";
	  case ENOMEM:
		return "Not enough memory";
	  case E2BIG:
		return "Argument list too long";
	  default:
		sprintf(buf, "Unknown system error %d", err);
		return buf;
	}
# endif /* HAVE_SYS_ERRLIST */
}
#endif /* !HAVE_STRERROR */