From: NeilBrown Date: Fri, 16 Mar 2012 00:36:32 +0000 (+1100) Subject: Add "susman" daemon X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=efe5fdc9893d05315264c9a4e1fbe52cf16ad941;p=susman.git Add "susman" daemon susman runs all three daemons lsusd lsused wakealarmd We wait for each to get started before starting the next. Signed-off-by: NeilBrown --- diff --git a/Makefile b/Makefile index 6371a81..10af626 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -PROGS = lsusd lsused request_suspend wakealarmd +PROGS = lsusd lsused request_suspend wakealarmd susman TESTS = block_test watch_test event_test alarm_test LIBS = suspend_block.o watcher.o wakeevent.o wakealarm.o @@ -28,6 +28,12 @@ lsused: lsused.o libsus.a wakealarmd: wakealarmd.o libsus.a $(CC) -o wakealarmd wakealarmd.o libsus.a -levent +%-m.o: %.c + $(CC) -o $@ -c $(CFLAGS) -Dmain=$* $< + +susman: susman.o lsusd-m.o lsused-m.o wakealarmd-m.o libsus.a + $(CC) -o susman susman.o lsusd-m.o lsused-m.o wakealarmd-m.o libsus.a -levent + request_suspend: request_suspend.o block_test: block_test.o libsus.a diff --git a/lsusd.c b/lsusd.c index 1886816..5e5dbd9 100644 --- a/lsusd.c +++ b/lsusd.c @@ -168,6 +168,8 @@ main(int argc, char *argv) if (dir < 0 || disable < 0) exit(1); + close(0); + while (1) { int count; diff --git a/lsused.c b/lsused.c index e4d0f98..8924cd7 100644 --- a/lsused.c +++ b/lsused.c @@ -253,6 +253,8 @@ main(int argc, char *argv[]) if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) exit(1); listen(s, 20); + /* Incase someone is waiting for us... */ + close(0); event_init(); diff --git a/susman.c b/susman.c new file mode 100644 index 0000000..a33ee39 --- /dev/null +++ b/susman.c @@ -0,0 +1,66 @@ +/* + * susman - manage suspend + * This daemon forks and runs three processes + * - one which manages suspend based on files in /var/run/suspend + * - one which listens on a socket and handles suspend requests that way, + * - one which provides a wakeup service using the RTC alarm. + * + * Copyright (C) 2011 Neil Brown + * + * 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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. + */ +#define _GNU_SOURCE +#include +#include + +int lsusd(int argc, char *argv[]); +int lsused(int argc, char *argv[]); +int wakealarmd(int argc, char *argv[]); + +void runone(int (*fun)(int argc, char *argv[])) +{ + int pfd[2]; + char c; + + if (pipe(pfd) < 0) + exit(2); + switch (fork()) { + case -1: + exit (2); + default: + break; + case 0: + close(pfd[0]); + dup2(pfd[1], 0); + close(pfd[1]); + (*fun)(0, NULL); + exit(1); + } + close(pfd[1]); + /* Block for lsused to start up */ + read(pfd[0], &c, 1); + close(pfd[0]); +} + +int main(int argc, char *argv[]) +{ + int pfd[2]; + char c; + + runone(lsusd); + runone(lsused); + wakealarmd(0, NULL); + exit(0); +}