File: eventloop.c

package info (click to toggle)
mcelog 104-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 996 kB
  • ctags: 1,508
  • sloc: ansic: 7,739; sh: 481; makefile: 87
file content (147 lines) | stat: -rw-r--r-- 3,420 bytes parent folder | download | duplicates (4)
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
/* Copyright (C) 2009 Intel Corporation 
   Author: Andi Kleen
   Event loop for mcelog daemon mode.

   mcelog is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; version
   2.

   mcelog 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
   General Public License for more details.

   You should find a copy of v2 of the GNU General Public License somewhere
   on your Linux system; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#define _GNU_SOURCE 1
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/fcntl.h>
#include <signal.h>
#include "mcelog.h"
#include "eventloop.h"

#define MAX_POLLFD 10

static int max_pollfd;

struct pollcb { 
	poll_cb_t cb;
	int fd;
	void *data;
};

static struct pollfd pollfds[MAX_POLLFD];
static struct pollcb pollcbs[MAX_POLLFD];	

static sigset_t event_sigs;

static int closeonexec(int fd)
{
	int flags = fcntl(fd, F_GETFD);
	if (flags < 0 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) { 
		SYSERRprintf("Cannot set FD_CLOEXEC flag on fd");
		return -1;
	}
	return 0;
}

int register_pollcb(int fd, int events, poll_cb_t cb, void *data)
{
	int i = max_pollfd;

	if (closeonexec(fd) < 0)
		return -1;

	if (i >= MAX_POLLFD) {
		Eprintf("poll table overflow");
		return -1;
	}
	max_pollfd++;

	pollfds[i].fd = fd;
	pollfds[i].events = events;
	pollcbs[i].cb = cb;
	pollcbs[i].data = data;
	return 0;
}

/* Could mark free and put into a free list */
void unregister_pollcb(struct pollfd *pfd)
{
	int i = pfd - pollfds;
	assert(i >= 0 && i < max_pollfd);
	memmove(pollfds + i, pollfds + i + 1, 
		(max_pollfd - i - 1) * sizeof(struct pollfd));
	memmove(pollcbs + i, pollcbs + i + 1, 
		(max_pollfd - i - 1) * sizeof(struct pollcb));
	max_pollfd--;
}

static void poll_callbacks(int n)
{
	int k;

	for (k = 0; k < max_pollfd && n > 0; k++) {
		struct pollfd *f = pollfds + k;
		if (f->revents) { 
			struct pollcb *c = pollcbs + k;
			c->cb(f, c->data);
			n--;
		}
	}
}

/* Run signal handler only directly after event loop */
int event_signal(int sig)
{
	static int first = 1;
	sigset_t mask;
	if (first && sigprocmask(SIG_BLOCK, NULL, &event_sigs) < 0) 
		return -1;
	first = 0;
	if (sigprocmask(SIG_BLOCK, NULL, &mask) < 0)
		return -1;
	sigaddset(&mask, sig);
	if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
		return -1;
	return 0;
}

/* Handle old glibc without ppoll. */
static int ppoll_fallback(struct pollfd *pfd, nfds_t nfds, 
			  const struct timespec *ts, const sigset_t *ss)
{
	sigset_t origmask;
	int ready;
	sigprocmask(SIG_SETMASK, ss, &origmask);
	ready = poll(pfd, nfds, ts ? ts->tv_sec : -1);
	sigprocmask(SIG_SETMASK, &origmask, NULL);
	return ready;
}

static int (*ppoll_vec)(struct pollfd *, nfds_t, const struct timespec
			*, const sigset_t *);

void eventloop(void)
{
#if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 5 || __GLIBC__ > 2
	ppoll_vec = ppoll;
#endif
	if (!ppoll_vec) 
		ppoll_vec = ppoll_fallback;

	for (;;) { 
		int n = ppoll_vec(pollfds, max_pollfd, NULL, &event_sigs);
		if (n <= 0) {
			if (n < 0 && errno != EINTR)
				SYSERRprintf("poll error");
			continue;
		}
		poll_callbacks(n); 
	}			
}