diff -N -r --unified=5 usblock.old/lockd/lockd.c usblock/lockd/lockd.c --- usblock.old/lockd/lockd.c 2006-09-07 20:17:04.000000000 +0200 +++ usblock/lockd/lockd.c 2007-02-28 02:37:38.000000000 +0100 @@ -16,11 +16,10 @@ #include "lockd.h" extern char *optarg; extern int optind,opterr,optopt; - /* arguments for lockd -i set transmitter getting in range script -o set transmitter going out of bounds script -t set sleep time between polls (in seconds) @@ -29,12 +28,10 @@ -d daemonize //maybe add different keys for different scripts later */ - - char *key=NULL; char *script_in=NULL; char *script_out=NULL; int sleep_time=8; int daemonize=0; @@ -46,200 +43,193 @@ FILE *f=NULL; //our file handle to /dev/lock0 int lock_in_range=0; //is transmitter in range status void FreeStuff() { -closelog(); //close syslog handle -if(f!=NULL)fclose(f); -unlink(pid_filename); //deleting pid file + closelog(); //close syslog handle + if(f!=NULL)fclose(f); + unlink(pid_filename); //deleting pid file } void SignalHandler(int signal) { -syslog(LOG_NOTICE,"caught signal:%d,exiting now.",signal); -FreeStuff(); -exit(signal);//exit daemon + syslog(LOG_NOTICE,"caught signal:%d,exiting now.",signal); + FreeStuff(); + exit(signal);//exit daemon } void RegisterSignalHandler(void) { -signal(SIGHUP, SignalHandler); -signal(SIGINT, SignalHandler); -signal(SIGQUIT, SignalHandler); -signal(SIGILL, SignalHandler); -signal(SIGTRAP, SignalHandler); -signal(SIGABRT, SignalHandler); -signal(SIGIOT, SignalHandler); -signal(SIGFPE, SignalHandler); -signal(SIGKILL, SignalHandler); -signal(SIGSEGV, SignalHandler); -signal(SIGPIPE, SignalHandler); -signal(SIGTERM, SignalHandler); -signal(SIGSTOP, SignalHandler); -signal(SIGUSR1, SIG_IGN); + signal(SIGHUP, SignalHandler); + signal(SIGINT, SignalHandler); + signal(SIGQUIT, SignalHandler); + signal(SIGILL, SignalHandler); + signal(SIGTRAP, SignalHandler); + signal(SIGABRT, SignalHandler); + signal(SIGIOT, SignalHandler); + signal(SIGFPE, SignalHandler); + signal(SIGKILL, SignalHandler); + signal(SIGSEGV, SignalHandler); + signal(SIGPIPE, SignalHandler); + signal(SIGTERM, SignalHandler); + signal(SIGSTOP, SignalHandler); + signal(SIGUSR1, SIG_IGN); } int GetOptions(int argc,const char* argv[]) { -int opt; -while((opt = getopt(argc,(char* const *)argv,"di:o:k:t:p:")) != -1) { -switch(opt) { -case 'd': daemonize=1; - break; -case 'i': - script_in=strdup(optarg); -// printf("Setting In-Range executable to:%s\n",script_in); - break; -case 'o': - script_out=strdup(optarg); -// printf("Setting Out-Of-Bounds executable to:%s\n",script_out); - break; -case 'k': - key=strdup(optarg); -// printf("Setting Transmitter-Key to:%s\n",key); - break; -case 'p': - pid_filename=strdup(optarg); -// printf("Setting PID-file to:%s\n",pid_filename); - break; -case 't': - sleep_time=atoi(optarg); -// printf("Setting Sleep-Time to:%d\n",sleep_time); - break; -case ':': - printf("option needs a value\n"); - exit(0); - break; - -case '?': - printf("unknown option:%c!\n",optopt); - exit(0); - break; -} + int opt; + while((opt = getopt(argc,(char* const *)argv,"di:o:k:t:p:")) != -1) { + switch(opt) { + case 'd': daemonize=1; + break; + case 'i': + script_in=strdup(optarg); +// printf("Setting In-Range executable to:%s\n",script_in); + break; + case 'o': + script_out=strdup(optarg); +// printf("Setting Out-Of-Bounds executable to:%s\n",script_out); + break; + case 'k': + key=strdup(optarg); +// printf("Setting Transmitter-Key to:%s\n",key); + break; + case 'p': + pid_filename=strdup(optarg); +// printf("Setting PID-file to:%s\n",pid_filename); + break; + case 't': + sleep_time=atoi(optarg); +// printf("Setting Sleep-Time to:%d\n",sleep_time); + break; + case ':': + printf("option needs a value\n"); + exit(0); + break; + case '?': + printf("unknown option:%c!\n",optopt); + exit(0); + break; + } + } } -} - - - int main(int argc,const char *argv[]) { -GetOptions(argc,argv); - - -if(daemonize) { -openlog ("lockd", LOG_PID, LOG_DAEMON); -if(pid_filename==NULL)pid_filename=strdup(PID_FILE);//if no pid-filename given use default -if(!access(pid_filename,R_OK)) { //if pid file found kill old lockd process -if ((pid_file=fopen(pid_filename,"rt"))) { -fscanf(pid_file,"%d",&pid); -fclose(pid_file); -if (kill(pid,SIGUSR1)==0) { -//syslog(LOG_NOTICE, "lockd is already running."); -fprintf(stderr,"lockd is already running(pid:%d).\n",pid); -exit(-1); -} -unlink(pid_filename); -} -} - -RegisterSignalHandler(); //Get Signal Handlers ready -switch (pid=fork()) { //fork a daemon process -case -1: - syslog(LOG_NOTICE, "error can't fork."); - exit(-3); -case 0: - break; -default: - if((pid_file=fopen(pid_filename, "wt"))) { - fprintf(pid_file,"%d\n",pid); - fclose(pid_file); - } else syslog(LOG_NOTICE,"failed to create pid-file:%s", pid_filename); - exit (0); -} - -syslog (LOG_NOTICE, "lock daemon started."); + GetOptions(argc,argv); -}else printf("Starting usblock-daemon Copyright (c) 2006 Tim 'pez2001' Theede.\n"); - - -while(1) { //start of daemon loop -f=fopen("/dev/lock0","r"); -if(f==NULL) { -if(daemonize)syslog(LOG_NOTICE,"error can't open file: '/dev/lock0'"); -else printf("error can't open file: '/dev/lock0'\n"); -return(0); -} -lock_file=(char*)malloc(256); -memset(lock_file,0,256); -//fseek(f,0,SEEK_SET); -lock_file_len=fread(lock_file,1,255,f); -if(lock_file_len==0){ -if(daemonize)syslog(LOG_NOTICE,"error can't read file: '/dev/lock0'"); -else printf("error can't read file: '/dev/lock0'\n"); -return(0); -} -lock_file_len-=1; -lock_file[lock_file_len]=0; //clear the newline char at EOL :-) -//printf("Read this from lock device:%s(len:%d)\n",lock_file,lock_file_len); - -if(key!=NULL) { -if(!strcmp(key,lock_file)) { -if(!lock_in_range) { -if(daemonize) syslog(LOG_NOTICE,"Transmitter with given Key in Range."); -else printf("Transmitter with given Key In-Range.\n"); -lock_in_range=1; -if(script_in!=NULL)system(script_in); -} -}else { -if(lock_in_range) { -if(daemonize) syslog(LOG_NOTICE,"Transmitter Out of Bounds."); -else printf("Transmitter Out of Bounds.\n"); -if(script_out!=NULL)system(script_out); -lock_in_range=0; -} -} -}else { // no key given just give the found key as parameter for the scripts -if(strcmp("0,0,0,0",lock_file)) { -if(!lock_in_range) { -if(daemonize) syslog(LOG_NOTICE,"Transmitter in Range."); -else printf("Transmitter in Range.\n"); -lock_in_range=1; -if(script_in!=NULL) { -char *script_key=NULL; -script_key=(char*)malloc(strlen(script_in)+strlen(" -key ")+strlen(lock_file)+1); -script_key=strcpy(script_key,script_in); -script_key=strcat(script_key," -key "); -script_key=strcat(script_key,lock_file); -//printf("Script Command :%s\n",script_key); -system(script_key); -free(script_key); -} + if(daemonize) { + openlog ("lockd", LOG_PID, LOG_DAEMON); + if(pid_filename==NULL)pid_filename=strdup(PID_FILE);//if no pid-filename given use default + if(!access(pid_filename,R_OK)) { //if pid file found kill old lockd process + if ((pid_file=fopen(pid_filename,"rt"))) { + fscanf(pid_file,"%d",&pid); + fclose(pid_file); + if (kill(pid,SIGUSR1)==0) { + //syslog(LOG_NOTICE, "lockd is already running."); + fprintf(stderr,"lockd is already running(pid:%d).\n",pid); + exit(-1); + } + unlink(pid_filename); + } + } + + RegisterSignalHandler(); //Get Signal Handlers ready + switch (pid=fork()) { //fork a daemon process + case -1: + syslog(LOG_NOTICE, "error can't fork."); + exit(-3); + case 0: + break; + default: + if((pid_file=fopen(pid_filename, "wt"))) { + fprintf(pid_file,"%d\n",pid); + fclose(pid_file); + } else syslog(LOG_NOTICE,"failed to create pid-file:%s", pid_filename); + exit (0); + } + + syslog (LOG_NOTICE, "lock daemon started."); + + }else printf("Starting usblock-daemon Copyright (c) 2006 Tim 'pez2001' Theede.\n"); + + while(1) { //start of daemon loop + f=fopen("/dev/lock0","r"); + if(f==NULL) { + if(daemonize)syslog(LOG_NOTICE,"error can't open file: '/dev/lock0'"); + else printf("error can't open file: '/dev/lock0'\n"); + return(0); + } + lock_file=(char*)malloc(256); + memset(lock_file,0,256); + //fseek(f,0,SEEK_SET); + lock_file_len=fread(lock_file,1,255,f); + if(lock_file_len==0){ + if(daemonize)syslog(LOG_NOTICE,"error can't read file: '/dev/lock0'"); + else printf("error can't read file: '/dev/lock0'\n"); + return(0); + } + lock_file_len-=1; + lock_file[lock_file_len]=0; //clear the newline char at EOL :-) + //printf("Read this from lock device:%s(len:%d)\n",lock_file,lock_file_len); + fclose(f); + f=NULL; + + if(key!=NULL) { + if(!strcmp(key,lock_file)) { + if(!lock_in_range) { + if(daemonize) syslog(LOG_NOTICE,"Transmitter with given Key in Range."); + else printf("Transmitter with given Key In-Range.\n"); + lock_in_range=1; + if(script_in!=NULL)system(script_in); + } + }else { + if(lock_in_range) { + if(daemonize) syslog(LOG_NOTICE,"Transmitter Out of Bounds."); + else printf("Transmitter Out of Bounds.\n"); + if(script_out!=NULL)system(script_out); + lock_in_range=0; + } + } + }else { // no key given just give the found key as parameter for the scripts + if(strcmp("0,0,0,0",lock_file)) { + if(!lock_in_range) { + if(daemonize) syslog(LOG_NOTICE,"Transmitter in Range."); + else printf("Transmitter in Range.\n"); + lock_in_range=1; + if(script_in!=NULL) { + char *script_key=NULL; + script_key=(char*)malloc(strlen(script_in)+strlen(" -key ")+strlen(lock_file)+1); + script_key=strcpy(script_key,script_in); + script_key=strcat(script_key," -key "); + script_key=strcat(script_key,lock_file); + //printf("Script Command :%s\n",script_key); + system(script_key); + free(script_key); + } + } + }else { + if(lock_in_range) { + + if(daemonize) syslog(LOG_NOTICE,"Transmitter Out of Bounds."); + else printf("Transmitter Out of Bounds.\n"); + if(script_out!=NULL) { + char *script_key=NULL; + //script_key=(char*)malloc(strlen(script_out)+strlen(" -key ")+strlen(lock_file)+1); + //script_key=strcpy(script_key,script_out); + //script_key=strcat(script_key," -key "); + //script_key=strcat(script_key,lock_file); + //printf("Script Command :%s\n",script_key); + //system(script_key); + system(script_out); + free(script_key); + } + lock_in_range=0; + } + } + + } + free(lock_file); + sleep(sleep_time); + }//end of daemon loop + + //cleaning up + FreeStuff(); + return(0); } -}else { -if(lock_in_range) { - -if(daemonize) syslog(LOG_NOTICE,"Transmitter Out of Bounds."); -else printf("Transmitter Out of Bounds.\n"); -if(script_out!=NULL) { -char *script_key=NULL; -//script_key=(char*)malloc(strlen(script_out)+strlen(" -key ")+strlen(lock_file)+1); -//script_key=strcpy(script_key,script_out); -//script_key=strcat(script_key," -key "); -//script_key=strcat(script_key,lock_file); -//printf("Script Command :%s\n",script_key); -//system(script_key); -system(script_out); -free(script_key); -} -lock_in_range=0; -} -} - -} -free(lock_file); -fclose(f); -f=NULL; -sleep(sleep_time); -}//end of daemon loop - -//cleaning up -FreeStuff(); -return(0); -} -