File: evloop.c

package info (click to toggle)
pommed 1.25~dfsg-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,428 kB
  • ctags: 967
  • sloc: ansic: 8,707; makefile: 224; sh: 118
file content (426 lines) | stat: -rw-r--r-- 7,242 bytes parent folder | download
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * pommed - Apple laptops hotkeys handler daemon
 *
 * $Id: evloop.c 486 2008-06-06 19:54:54Z jblache $
 *
 * Copyright (C) 2006-2008 Julien BLACHE <jb@jblache.org>
 *
 * This program 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 of the License.
 *
 * This program 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 have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>

#include <syslog.h>

#include <errno.h>

#include <sys/epoll.h>

#include "timerfd-syscalls.h"

#include "pommed.h"
#include "evloop.h"


/* epoll fd */
static int epfd;

/* event sources registered on the main loop */
static struct pommed_event *sources;

/* timers */
static struct pommed_timer *timers;
static int timer_job_id;

static int running;


int
evloop_add(int fd, uint32_t events, pommed_event_cb cb)
{
  int ret;

  struct epoll_event epoll_ev;
  struct pommed_event *pommed_ev;

  pommed_ev = (struct pommed_event *)malloc(sizeof(*pommed_ev));

  if (pommed_ev == NULL)
    {
      logmsg(LOG_ERR, "Could not allocate memory for new source");

      return -1;
    }

  pommed_ev->fd = fd;
  pommed_ev->cb = cb;
  pommed_ev->next = sources;

  epoll_ev.events = events;
  epoll_ev.data.ptr = pommed_ev;

  ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epoll_ev);

  if (ret < 0)
    {
      logmsg(LOG_ERR, "Could not add source to epoll: %s", strerror(errno));

      free(pommed_ev);
      return -1;
    }

  sources = pommed_ev;

  return 0;
}

int
evloop_remove(int fd)
{
  int ret;

  struct pommed_event *p;
  struct pommed_event *e;

  ret = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);

  if (ret < 0)
    {
      logmsg(LOG_ERR, "Could not remove source from epoll: %s", strerror(errno));

      return -1;
    }

  for (p = NULL, e = sources; e != NULL; p = e, e = e->next)
    {
      if (e->fd != fd)
	continue;

      if (p != NULL)
	p->next = e->next;
      else
	sources = e->next;

      free(e);

      break;
    }

  return 0;
}


static void
evloop_timer_callback(int fd, uint32_t events)
{
  uint64_t ticks;

  struct pommed_timer *t;
  struct pommed_timer_job *j;

  /* Acknowledge timer */
  read(fd, &ticks, sizeof(ticks));

  j = NULL;
  for (t = timers; t != NULL; t = t->next)
    {
      if (t->fd == fd)
	{
	  j = t->jobs;

	  break;
	}
    }

  while (j != NULL)
    {
      j->cb(j->id, ticks);

      j = j->next;
    }
}

static int
evloop_create_timer(int timeout)
{
  int fd;
  int ret;

  struct itimerspec timing;

  fd = timerfd_create(CLOCK_MONOTONIC, 0);
  if (fd < 0)
    {
      logmsg(LOG_ERR, "Could not create timer: %s", strerror(errno));

      return -1;
    }

  timing.it_interval.tv_sec = (timeout >= 1000) ? timeout / 1000 : 0;
  timing.it_interval.tv_nsec = (timeout - (timing.it_interval.tv_sec * 1000)) * 1000000;

  ret = clock_gettime(CLOCK_MONOTONIC, &timing.it_value);
  if (ret < 0)
    {
      logmsg(LOG_ERR, "Could not get current time: %s", strerror(errno));

      close(fd);
      return -1;
    }

  timing.it_value.tv_sec += timing.it_interval.tv_sec;
  timing.it_value.tv_nsec += timing.it_interval.tv_nsec;
  if (timing.it_value.tv_nsec > 1000000000)
    {
      timing.it_value.tv_sec++;
      timing.it_value.tv_nsec -= 1000000000;
    }

  ret = timerfd_settime(fd, TFD_TIMER_ABSTIME, &timing, NULL);
  if (ret < 0)
    {
      logmsg(LOG_ERR, "Could not setup timer: %s", strerror(errno));

      close(fd);
      return -1;
    }

  ret = evloop_add(fd, EPOLLIN, evloop_timer_callback);
  if (ret < 0)
    {
      close(fd);
      return -1;
    }

  return fd;
}

int
evloop_add_timer(int timeout, pommed_timer_cb cb)
{
  int fd;

  struct pommed_timer *t;
  struct pommed_timer_job *j;

  j = (struct pommed_timer_job *)malloc(sizeof(struct pommed_timer_job));
  if (j == NULL)
    {
      logmsg(LOG_ERR, "Could not allocate memory for timer job");
      return -1;
    }

  j->cb = cb;
  j->id = timer_job_id;
  timer_job_id++;

  for (t = timers; t != NULL; t = t->next)
    {
      if (t->timeout == timeout)
	break;
    }

  if (t == NULL)
    {
      t = (struct pommed_timer *)malloc(sizeof(struct pommed_timer));
      if (t == NULL)
	{
	  logmsg(LOG_ERR, "Could not allocate memory for timer");
	  return -1;
	}

      fd = evloop_create_timer(timeout);
      if (fd < 0)
	{
	  free(t);
	  return -1;
	}

      t->fd = fd;
      t->timeout = timeout;
      t->jobs = NULL;
      t->next = timers;
      timers = t;
    }

  j->next = t->jobs;
  t->jobs = j;

  return 0;
}

int
evloop_remove_timer(int id)
{
  int found;
  int ret;

  struct pommed_timer *t;
  struct pommed_timer *pt;
  struct pommed_timer_job *j;
  struct pommed_timer_job *pj;

  found = 0;
  for (pt = NULL, t = timers; t != NULL; pt = t, t = t->next)
    {
      for (pj = NULL, j = t->jobs; j != NULL; pj = j, j = j->next)
	{
	  if (j->id == id)
	    {
	      if (pj != NULL)
		pj->next = j->next;
	      else
		t->jobs = j->next;

	      free(j);

	      found = 1;

	      break;
	    }
	}

      if (found)
	break;
    }

  if (t == NULL)
    return 0;

  if (t->jobs == NULL)
    {
      ret = evloop_remove(t->fd);
      if (ret < 0)
	return ret;

      close(t->fd);

      if (pt != NULL)
	pt->next = t->next;
      else
	timers = t->next;

      free(t);
    }

  return 0;
}


int
evloop_iteration(void)
{
  int i;
  int nfds;

  struct epoll_event epoll_ev[MAX_EPOLL_EVENTS];
  struct pommed_event *pommed_ev;

  if (!running)
    return -1;

  nfds = epoll_wait(epfd, epoll_ev, MAX_EPOLL_EVENTS, -1);

  if (nfds < 0)
    {
      if (errno == EINTR)
	return 0; /* pommed.c will continue */
      else
	{
	  logmsg(LOG_ERR, "epoll_wait() error: %s", strerror(errno));

	  return -1; /* pommed.c will exit */
	}
    }

  for (i = 0; i < nfds; i++)
    {
      pommed_ev = epoll_ev[i].data.ptr;
      pommed_ev->cb(pommed_ev->fd, epoll_ev[i].events);
    }

  return nfds;
}

void
evloop_stop(void)
{
  running = 0;
}


int
evloop_init(void)
{
  sources = NULL;

  timers = NULL;
  timer_job_id = 0;

  running = 1;

  epfd = epoll_create(MAX_EPOLL_EVENTS);
  if (epfd < 0)
    {
      logmsg(LOG_ERR, "Could not create epoll fd: %s", strerror(errno));

      return -1;
    }

  return 0;
}

void
evloop_cleanup(void)
{
  struct pommed_event *p;
  struct pommed_timer *t;
  struct pommed_timer_job *j;
  struct pommed_timer_job *jobs;

  close(epfd);

  while (sources != NULL)
    {
      p = sources;
      sources = sources->next;

      close(p->fd);

      free(p);
    }

  while (timers != NULL)
    {
      t = timers;
      timers = timers->next;

      jobs = t->jobs;
      while (jobs != NULL)
	{
	  j = jobs;
	  jobs = jobs->next;

	  free(j);
	}

      free(t);
    }
}