blob: d1b61d474e0a0a535423f74b097b851bf0b05677 [file] [log] [blame]
Jeff Dike63ae2a92006-03-27 01:14:30 -08001/*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <signal.h>
10#include <string.h>
11#include <sys/poll.h>
12#include <sys/types.h>
13#include <sys/time.h>
14#include "user_util.h"
15#include "kern_util.h"
16#include "user.h"
17#include "process.h"
18#include "sigio.h"
19#include "irq_user.h"
20#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070021#include "um_malloc.h"
Jeff Dike63ae2a92006-03-27 01:14:30 -080022
Jeff Dikef2e62992007-02-10 01:44:23 -080023/*
24 * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
25 * and os_free_irq_by_cb, which are called under irq_lock.
26 */
Jeff Dike63ae2a92006-03-27 01:14:30 -080027static struct pollfd *pollfds = NULL;
28static int pollfds_num = 0;
29static int pollfds_size = 0;
30
31int os_waiting_for_events(struct irq_fd *active_fds)
32{
33 struct irq_fd *irq_fd;
34 int i, n, err;
35
36 n = poll(pollfds, pollfds_num, 0);
Jesper Juhl191ef962006-05-01 12:15:57 -070037 if (n < 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080038 err = -errno;
Jesper Juhl191ef962006-05-01 12:15:57 -070039 if (errno != EINTR)
Jeff Dike63ae2a92006-03-27 01:14:30 -080040 printk("sigio_handler: os_waiting_for_events:"
41 " poll returned %d, errno = %d\n", n, errno);
42 return err;
43 }
44
Jesper Juhl191ef962006-05-01 12:15:57 -070045 if (n == 0)
Jeff Dike63ae2a92006-03-27 01:14:30 -080046 return 0;
47
48 irq_fd = active_fds;
49
Jesper Juhl191ef962006-05-01 12:15:57 -070050 for (i = 0; i < pollfds_num; i++) {
51 if (pollfds[i].revents != 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080052 irq_fd->current_events = pollfds[i].revents;
53 pollfds[i].fd = -1;
54 }
55 irq_fd = irq_fd->next;
56 }
57 return n;
58}
59
Jeff Dike63ae2a92006-03-27 01:14:30 -080060int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
61{
62 if (pollfds_num == pollfds_size) {
63 if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
64 /* return min size needed for new pollfds area */
Jeff Dikef2e62992007-02-10 01:44:23 -080065 return (pollfds_size + 1) * sizeof(pollfds[0]);
Jeff Dike63ae2a92006-03-27 01:14:30 -080066 }
67
Jesper Juhl191ef962006-05-01 12:15:57 -070068 if (pollfds != NULL) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080069 memcpy(tmp_pfd, pollfds,
70 sizeof(pollfds[0]) * pollfds_size);
71 /* remove old pollfds */
72 kfree(pollfds);
73 }
74 pollfds = tmp_pfd;
75 pollfds_size++;
Jesper Juhl191ef962006-05-01 12:15:57 -070076 } else
77 kfree(tmp_pfd); /* remove not used tmp_pfd */
Jeff Dike63ae2a92006-03-27 01:14:30 -080078
Jesper Juhl191ef962006-05-01 12:15:57 -070079 pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
80 .events = events,
81 .revents = 0 });
Jeff Dike63ae2a92006-03-27 01:14:30 -080082 pollfds_num++;
83
Jesper Juhl191ef962006-05-01 12:15:57 -070084 return 0;
Jeff Dike63ae2a92006-03-27 01:14:30 -080085}
86
87void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
88 struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
89{
90 struct irq_fd **prev;
91 int i = 0;
92
93 prev = &active_fds;
Jesper Juhl191ef962006-05-01 12:15:57 -070094 while (*prev != NULL) {
95 if ((*test)(*prev, arg)) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080096 struct irq_fd *old_fd = *prev;
Jesper Juhl191ef962006-05-01 12:15:57 -070097 if ((pollfds[i].fd != -1) &&
98 (pollfds[i].fd != (*prev)->fd)) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080099 printk("os_free_irq_by_cb - mismatch between "
100 "active_fds and pollfds, fd %d vs %d\n",
101 (*prev)->fd, pollfds[i].fd);
102 goto out;
103 }
104
105 pollfds_num--;
106
107 /* This moves the *whole* array after pollfds[i]
108 * (though it doesn't spot as such)!
109 */
Jeff Dike63ae2a92006-03-27 01:14:30 -0800110 memmove(&pollfds[i], &pollfds[i + 1],
111 (pollfds_num - i) * sizeof(pollfds[0]));
112 if(*last_irq_ptr2 == &old_fd->next)
113 *last_irq_ptr2 = prev;
114
115 *prev = (*prev)->next;
116 if(old_fd->type == IRQ_WRITE)
117 ignore_sigio_fd(old_fd->fd);
118 kfree(old_fd);
119 continue;
120 }
121 prev = &(*prev)->next;
122 i++;
123 }
124 out:
125 return;
126}
127
Jeff Dike63ae2a92006-03-27 01:14:30 -0800128int os_get_pollfd(int i)
129{
Jesper Juhl191ef962006-05-01 12:15:57 -0700130 return pollfds[i].fd;
Jeff Dike63ae2a92006-03-27 01:14:30 -0800131}
132
133void os_set_pollfd(int i, int fd)
134{
135 pollfds[i].fd = fd;
136}
137
138void os_set_ioignore(void)
139{
Jeff Dike4b84c692006-09-25 23:33:04 -0700140 signal(SIGIO, SIG_IGN);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800141}
142
143void init_irq_signals(int on_sigstack)
144{
Jeff Dike63ae2a92006-03-27 01:14:30 -0800145 int flags;
146
147 flags = on_sigstack ? SA_ONSTACK : 0;
Jeff Dike63ae2a92006-03-27 01:14:30 -0800148
Jeff Dikeaceb3432006-07-10 04:45:05 -0700149 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
150 flags | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
151 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
152 flags | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800153 set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
154 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
155 signal(SIGWINCH, SIG_IGN);
156}