/* A utility to launch a program and terminate that program upon receiving an "Exit" code from LIRC. Copyright (c) 2006 Jason Kraftcheck Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include /* Program name referenced in lirc config */ char* app_name = "gamelaunch"; /* PID of child process (game) */ pid_t child = 0; /* Callback function for parsing lirc config. The only option this app knows about is "Exit", so indicate an error if anything else is specified. */ int check_lirc_config( char* str ) { return strcmp( str, "Exit" ) ? -1 : 0; } /* SIGCHILD handler. Will be called when child process (game) exits. Clean up zombie child process and terminate this process. */ void handle_sigchld( int signum ) { int status; if (waitpid( child, &status, WNOHANG ) != child) { signal( SIGCHLD, SIG_IGN ); fprintf( stderr, "%s: Stray SIGCHLD\n", app_name ); if (kill( child, SIGKILL ) == 0) waitpid( child, &status, 0 ); } lirc_deinit(); exit( status ); } /* Handle signals indicating that this process should exit. Kill the child process. */ void handle_sigquit( int signum ) { /* Normally nothing in this function beyond the first line should be executed. Killing the child will result in a call to handle_sigchild, which will terminate this process. */ if (0 == kill( child, SIGQUIT )) { sleep( 2 ); kill( child, SIGKILL ); } lirc_deinit(); exit( 4 ); } /* Print brief help message and exit */ void usage( char* name, int exitval ) { printf("Usage: %s [-config ] [options...]\n", name ); printf("LIRC app name: %s\n", app_name ); exit( exitval ); } int main( int argc, char* argv[] ) { void (*s)(int); /* Return value from signal() */ int fd = -1; /* File descriptor of lirc socket */ int i = 1; /* Index into argv */ char *config_file = NULL; /* Config file name */ char *lirc_code = NULL; /* Result of lirc_nextcode */ char *lirc_opt = NULL; /* Result of lirc_code2char */ struct lirc_config *lirc_conf = NULL; int done; if (i < argc && (!strcmp( argv[i], "-help" ) || !strcmp( argv[i], "-h" ))) usage( argv[0], 0 ); /* If user specified a config file, set config_file to that string, otherwise leave config_file as NULL so lirc uses the default */ if (i < argc && !strcmp( argv[i], "-config" )) { if (++i >= argc) usage( argv[0], 1 ); config_file = argv[i++]; } /* Must have at least the name of the program to run */ if (i >= argc) { usage( argv[0], 1 ); } /* Initialize LIRC. Save socket file descriptor so it can be closed before exec'ing the child process */ fd = lirc_init( app_name, 1 ); if (fd == -1) { fprintf( stderr, "%s: Failed to init lirc client library.\n", app_name ); exit( 2 ); } if (lirc_readconfig( config_file, &lirc_conf, &check_lirc_config ) != 0) { lirc_deinit(); fprintf( stderr, "%s: lirc_readconfig failed.\n", app_name ); exit( 2 ); } /* Set signal handler to be called when child process exits */ s = signal( SIGCHLD, &handle_sigchld ); if (s == SIG_ERR) { fprintf( stderr, "%s: Error registering handler for SIGCHLD\n", app_name ); exit( 1 ); } /* Set signal handlers for cleanup when this process is exiting */ s = signal( SIGQUIT, &handle_sigquit ); if (s == SIG_ERR) { fprintf( stderr, "%s: Error registering handler for SIGQUIT\n", app_name ); exit( 1 ); } s = signal( SIGINT, &handle_sigquit ); if (s == SIG_ERR) { fprintf( stderr, "%s: Error registering handler for SIGINT\n", app_name ); exit( 1 ); } else if (s == SIG_IGN) { signal( SIGINT, SIG_IGN ); } /* Start child process (game) */ child = fork(); if (child == -1) { perror( "fork" ); exit( 2 ); } if (child == 0) { close( fd ); execvp( argv[i], argv+i ); perror( argv[i] ); exit( 3 ); } /* Loop until we get an "Exit" code from LIRC */ done = 0; while (!done && lirc_nextcode( &lirc_code ) == 0) { if (!lirc_code) continue; while (lirc_code2char( lirc_conf, lirc_code, &lirc_opt ) == 0 && lirc_opt != NULL) { if (!strcmp( lirc_opt, "Exit" )) { done = 1; break; } } free( lirc_code ); } lirc_freeconfig( lirc_conf ); /* Send SIGQUIT to this process. The quit handler should kill the child process, which will in turn invoke the SIGCHLD handler to clean things up and exit. */ raise( SIGQUIT ); return 4; }