3 * Library code to allow libevent app to register for a wake alarm
4 * and register with wakealarmd to keep suspend at bay for the time.
6 * Copyright (C) 2011 Neil Brown <neilb@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/socket.h>
36 void (*fn)(int,short,void*);
40 static void alarm_clock(int fd, short ev, void *data)
46 n = read(fd, buf, sizeof(buf)-1);
47 if (n < 0 && errno == EAGAIN)
50 strncmp(buf, "Now", 3) == 0) {
51 h->fn(-1, ev, h->data);
52 wakealarm_destroy(&h->ev);
54 /* Some other message, keep waiting */
57 struct event *wakealarm_set(time_t when, void(*fn)(int, short, void*),
60 struct sockaddr_un addr;
61 struct han *h = malloc(sizeof(*h));
69 h->disable = suspend_open();
70 h->sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
71 if (h->sock < 0 || h->disable < 0)
74 addr.sun_family = AF_UNIX;
75 strcpy(addr.sun_path, "/run/suspend/wakealarm");
76 if (connect(h->sock, (struct sockaddr*)&addr, sizeof(addr)) != 0)
79 fcntl(h->sock, F_SETFL, fcntl(h->sock, F_GETFL, 0) | O_NONBLOCK);
80 sprintf(buf, "%lld\n", (long long)when);
81 write(h->sock, buf, strlen(buf));
83 event_set(&h->ev, h->sock, EV_READ|EV_PERSIST, alarm_clock, h);
84 event_add(&h->ev, NULL);
89 suspend_close(h->disable);
96 void wakealarm_destroy(struct event *ev)
98 struct han *h = (struct han *)ev;
101 suspend_close(h->disable);