File: rtlinux_signal.c

package info (click to toggle)
rtlinux 3.1pre3-2
  • links: PTS
  • area: non-free
  • in suites: sarge, woody
  • size: 4,892 kB
  • ctags: 4,228
  • sloc: ansic: 26,204; sh: 2,069; makefile: 1,414; perl: 855; tcl: 489; asm: 380; cpp: 42
file content (282 lines) | stat: -rw-r--r-- 6,837 bytes parent folder | download | duplicates (2)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * (C) Finite State Machine Labs Inc. 2000 business@fsmlabs.com
 *
 * Released under the terms of GPL 2.
 * Open RTLinux makes use of a patented process described in
 * US Patent 5,995,745. Use of this process is governed
 * by the Open RTLinux Patent License which can be obtained from
 * www.fsmlabs.com/PATENT or by sending email to
 * licensequestions@fsmlabs.com
 */

/*
 * This is a hack to get NR_IRQS on the PowerPC since it depends on
 * the configured machine and processor type as well as having __KERNEL__
 * defined.  Other architectures (alpha, mips) may need this one day as well.
 * -- Cort
 */
#define __KERNEL__
#include <linux/autoconf.h>
#include <asm/irq.h>
#undef __KERNEL__

#include <fcntl.h>
#include <linux/unistd.h>
#include <asm/errno.h>
#include <rtlinux_signal.h>
/*#include <sys/mman.h>*//* sys/mman.h has old and wrong values -- Cort */
#include <asm/mman.h>
#include <stdio.h>
#include <pthread.h>

int gethrtime_init = 0;

#define	SETUP_CALL(ptr,file)				\
	if (!ptr)					\
	{						\
		char s[256];				\
		int fd;					\
							\
		memset(s, 0, 256);			\
		if ((fd = open(file, O_RDONLY)) < 0)	\
			return (-1);			\
		if ((read(fd, &s, 256)) < 0)		\
			return (-1);			\
		ptr = (typeof(ptr)) strtoul(s, NULL, 10);	\
		close(fd);				\
	}

/* gethrtime function pointer */
hrtime_t(*gethrtime_p) (void);
int (*rtf_put_p) (unsigned int fifo, void *buf, int count);
int (*rtf_get_p) (unsigned int fifo, void *buf, int count);
unsigned long __NR_rtf_create_p = 0;
unsigned long __NR_rtf_destroy_p = 0;

volatile unsigned long create_fifo_retval, create_fifo_num,
    create_fifo_size, create_fifo_done;
volatile unsigned long destroy_fifo_retval, destroy_fifo_num,
    destroy_fifo_done;

hrtime_t gethrtime(void)
{
	return gethrtime_p();
}

int rtf_put(unsigned int fifo, void *buf, int count)
{
	return rtf_put_p(fifo, buf, count);
}

int rtf_get(unsigned int fifo, void *buf, int count)
{
	return rtf_get_p(fifo, buf, count);
}

/* system calls for FIFO manipulation -Nathan */
_syscall1(int, rtf_destroy_p, unsigned int, fifo);
_syscall2(int, rtf_create_p, unsigned int, fifo, int, size);

int rtf_destroy(unsigned int fifo)
{
	if (__NR_rtf_destroy_p == 0)
		SETUP_CALL(__NR_rtf_destroy_p,
			   "/proc/rtlinux/rtf_destroy");
	return (rtf_destroy_p(fifo));
}

int rtf_create(unsigned int fifo, int size)
{
	if (__NR_rtf_create_p == 0)
		SETUP_CALL(__NR_rtf_create_p, "/proc/rtlinux/rtf_create");
	return (rtf_create_p(fifo, size));
}

/* global signal set of per-process blocked signals -Nathan */
rtlinux_sigset_t rtlinux_blocked = { 0, };

int rtlinux_sigaction(int sig, struct rtlinux_sigaction *act,
		      struct rtlinux_sigaction *oldact)
{
	char s[256];
	int fd;
	unsigned long err;

	SETUP_CALL(gethrtime_p, "/proc/rtlinux/gethrtime");
	SETUP_CALL(rtf_put_p, "/proc/rtlinux/rtf_put");
	SETUP_CALL(rtf_get_p, "/proc/rtlinux/rtf_get");

	/* Lock all our pages so we don't have to worry about faults
	 * when the code is running in kernel mode.
	 */
	if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
		perror("mlockall");
		return -1;
	}

	sprintf(s, "/proc/rtlinux/sigaction");
	if ((fd = open(s, O_WRONLY)) <= -1)
		return -1;

	act->sa_signal = sig;
	if ((err = write(fd, act, sizeof(*act))) != sizeof(*act))
		return -1;
	else
		return 0;
}

/* XXX: This function (and functions it depends upon) will allow you to set
 * non-existant signals as blocked or unblocked.  Mainly, this is for forward
 * compatibility when we add more signals; please don't abuse it.  -Nathan */
int rtlinux_sigprocmask(int how, rtlinux_sigset_t * set,
			rtlinux_sigset_t * oldset)
{
	int retval, i;
	int fd;

	/* first save the old set */
	if (oldset != NULL) {
		for (i = 0; i < RTLINUX_SIGMAX; i++) {
			retval = rtlinux_sigismember(&rtlinux_blocked, i);
			if (retval == -1)
				return (-1);
			else if (retval == 1)
				if ((rtlinux_sigaddset(oldset, i)) == -1)
					return (-1);
		}
	}

	if (how == RTLINUX_SIG_BLOCK) {
		for (i = 0; i < RTLINUX_SIGMAX; i++) {
			retval = rtlinux_sigismember(set, i);
			if (retval == -1)
				return (-1);
			else if (retval == 1)
				if (
				    (rtlinux_sigaddset
				     (&rtlinux_blocked, i)) == -1)
					return (-1);
		}
	} else if (how == RTLINUX_SIG_UNBLOCK) {
		for (i = 0; i < RTLINUX_SIGMAX; i++) {
			retval = rtlinux_sigismember(set, i);
			if (retval == -1)
				return (-1);
			else if (retval == 1)
				if (
				    (rtlinux_sigdelset
				     (&rtlinux_blocked, i)) == -1)
					return (-1);
		}
	} else if (how == RTLINUX_SIG_SETMASK) {
		rtlinux_sigemptyset(&rtlinux_blocked);
		for (i = 0; i < RTLINUX_SIGMAX; i++) {
			retval = rtlinux_sigismember(set, i);
			if (retval == -1)
				return (-1);
			else if (retval == 1)
				if (
				    (rtlinux_sigaddset
				     (&rtlinux_blocked, i)) == -1)
					return (-1);
		}
	} else {
		return (-1);
	}

	if ((fd = open("/proc/rtlinux/sigprocmask", O_WRONLY)) < 0)
		return (-1);
	if (
	    (retval =
	     write(fd, &rtlinux_blocked,
		   sizeof(rtlinux_sigset_t))) != sizeof(rtlinux_sigset_t)) {
		close(fd);
		fprintf(stderr, "wrote %d of %d bytes\n", retval,
			sizeof(rtlinux_sigset_t));
		return (-1);
	}

	if ((close(fd)) != 0)
		return (-1);

	/* if all else suceeds */
	return (0);
}

/* The size of rtlinux_sigset_t is currently 34 bytes -- 272 bits, so we have
 * to do some malicious bit twiddling of our own here. The first two functions
 * aren't so bad, just beware the rest. -Nathan */
int rtlinux_sigemptyset(rtlinux_sigset_t * set)
{
	if (set == NULL)
		return (-1);
	memset(set, 0x00, sizeof(rtlinux_sigset_t));
	return (0);
}

int rtlinux_sigfillset(rtlinux_sigset_t * set)
{
	if (set == NULL)
		return (-1);

	memset(set, 0xff, sizeof(rtlinux_sigset_t));
	return (0);
}

int rtlinux_sigaddset(rtlinux_sigset_t * set, int sig)
{
	int offset;
	unsigned long int value;

	if (set == NULL)
		return (-1);
	if (sig > RTLINUX_SIGMAX) {
		errno = EINVAL;
		return (-1);
	}

	offset = (int) ((sig / (sizeof(unsigned long int) * 8)));
	value = 1 << (sig % (sizeof(unsigned long int) * 8));

	set->__val[offset] |= value;
	return (0);
}

int rtlinux_sigdelset(rtlinux_sigset_t * set, int sig)
{
	int offset;
	unsigned long int value;

	if (set == NULL)
		return (-1);
	if (sig > RTLINUX_SIGMAX) {
		errno = EINVAL;
		return (-1);
	}

	offset = (int) ((sig / (sizeof(unsigned long int) * 8)));
	value = 1 << (sig % (sizeof(unsigned long int) * 8));

	set->__val[offset] &= ~(value);
	return (0);
}

int rtlinux_sigismember(const rtlinux_sigset_t * set, int sig)
{
	int offset;
	unsigned long int value;

	if (set == NULL)
		return (-1);
	if (sig > RTLINUX_SIGMAX) {
		errno = EINVAL;
		return (-1);
	}

	offset = (int) ((sig / (sizeof(unsigned long int) * 8)));
	value = 1 << (sig % (sizeof(unsigned long int) * 8));

	if (((unsigned long int) (set->__val[offset] & value)) > 0)
		return (1);
	return (0);
}