From 2572dd0657d80c408f1031ddff62fe7665cd6504 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Wed, 3 Oct 2012 01:17:21 +0200 Subject: [PATCH] Kill a bit of cruft, explain the reasonning behind the kill/traps, and fix a potential buffering issue eating the first slider event --- input.c | 29 +++++++++++++---------------- kpdfview.c | 9 --------- slider_watcher.c | 13 +++---------- 3 files changed, 16 insertions(+), 35 deletions(-) diff --git a/input.c b/input.c index 126f94a9c..7f79d1986 100644 --- a/input.c +++ b/input.c @@ -50,7 +50,8 @@ void slider_handler(int sig) { /* Kill lipc-wait-event properly on exit */ if(pclose_arg.pid != 0) { - kill(pclose_arg.pid, SIGTERM); + // Be a little more gracious, lipc seems to handle SIGINT properly + kill(pclose_arg.pid, SIGINT); } } @@ -84,7 +85,7 @@ static int openInputDevice(lua_State *L) { return luaL_error(L, "cannot fork() slider event listener"); } if(childpid == 0) { - // Setup signal handler, to cleanup on exit + // We send a SIGTERM to this child on exit, trap it to kill lipc properly. signal(SIGTERM, slider_handler); FILE *fp; @@ -99,25 +100,21 @@ static int openInputDevice(lua_State *L) { ev.code = key_code; ev.value = 1; - /* listen power slider events */ - char *exec_file = "lipc-wait-event"; - char *arg1 = "-m"; // Hang for ever, don't exit on the first one: we're in a dedicated child, and we'll be killed/closed on exit, so we don't care - char *arg2 = "-s"; - char *arg3 = "0"; - char *arg4 = "com.lab126.powerd"; - char *arg5 = "goingToScreenSaver,outOfScreenSaver,charging,notCharging"; - char *arg6 = (char *) NULL; - char *argv[] = {exec_file, arg1, arg2, arg3, arg4, arg5, arg6}; + /* listen power slider events (listen for ever for multiple events) */ + char *argv[] = {"lipc-wait-event", "-m", "-s", "0", "com.lab126.powerd", "goingToScreenSaver,outOfScreenSaver,charging,notCharging", (char *) NULL}; /* @TODO 07.06 2012 (houqp) * plugin and out event can only be watched by: lipc-wait-event com.lab126.hal usbPlugOut,usbPlugIn */ - fp = popen_noshell(exec_file, (const char * const *)argv, "r", &pclose_arg, 0); + fp = popen_noshell("lipc-wait-event", (const char * const *)argv, "r", &pclose_arg, 0); if (!fp) { err(EXIT_FAILURE, "popen_noshell()"); } + /* Flush to get rid of buffering issues? */ + fflush(fp); + while(fgets(std_out, sizeof(std_out)-1, fp)) { if(std_out[0] == 'g') { ev.code = CODE_IN_SAVER; @@ -147,13 +144,13 @@ static int openInputDevice(lua_State *L) { if (status == -1) { err(EXIT_FAILURE, "pclose_noshell()"); } else { - printf("Power slider event listener child exited with status %d.\n", status); + printf("lipc-wait-event exited with status %d.\n", status); if WIFEXITED(status) { - printf("Child exited normally with status: %d.\n", WEXITSTATUS(status)); + printf("lipc-wait-event exited normally with status: %d.\n", WEXITSTATUS(status)); } if WIFSIGNALED(status) { - printf("Child terminated by signal: %d.\n", WTERMSIG(status)); + printf("lipc-wait-event terminated by signal: %d.\n", WTERMSIG(status)); } } @@ -193,7 +190,7 @@ static int closeInputDevices(lua_State *L) { } if(slider_pid != -1) { /* kill and wait for child process */ - kill(slider_pid, SIGTERM); + kill(slider_pid, SIGTERM); // We could kill -slider_pid (note the minus) to kill the whole process group, but we trap SIGTERM to handle things nicely waitpid(-1, NULL, 0); } return 0; diff --git a/kpdfview.c b/kpdfview.c index 9c7915d46..af6465e2e 100644 --- a/kpdfview.c +++ b/kpdfview.c @@ -123,15 +123,6 @@ int main(int argc, char **argv) { } } - /* Make popen_noshell & valgrind happy */ - if (fflush(stdout) != 0) - err(EXIT_FAILURE, "fflush(stdout)"); - if (fflush(stderr) != 0) - err(EXIT_FAILURE, "fflush(stderr)"); - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - return 0; } diff --git a/slider_watcher.c b/slider_watcher.c index cca6827a5..3d16c06ad 100644 --- a/slider_watcher.c +++ b/slider_watcher.c @@ -65,16 +65,9 @@ main ( int argc, char *argv[] ) ev.value = 1; /* listen power slider events */ - char *exec_file = "lipc-wait-event"; - char *arg1 = "-m"; - char *arg2 = "-s"; - char *arg3 = "0"; - char *arg4 = "com.lab126.powerd"; - char *arg5 = "goingToScreenSaver,outOfScreenSaver"; - char *arg6 = (char *) NULL; - char *chargv[] = {exec_file, arg1, arg2, arg3, arg4, arg5, arg6}; - - fp = popen_noshell(exec_file, (const char * const *)chargv, "r", &pclose_arg, 0); + char *argv[] = {"lipc-wait-event", "-m", "-s", "0", "com.lab126.powerd", "goingToScreenSaver,outOfScreenSaver", (char *) NULL}; + + fp = popen_noshell("lipc-wait-event", (const char * const *)chargv, "r", &pclose_arg, 0); if (!fp) { err(EXIT_FAILURE, "popen_noshell()"); }