Author: marten Date: Wed Oct 14 16:38:43 2009 New Revision: 1147 Log: Added Lock-file creation to the management tool Modified: trunk/pairing/management.c Modified: trunk/pairing/management.c ============================================================================== --- trunk/pairing/management.c Wed Oct 14 16:02:06 2009 (r1146) +++ trunk/pairing/management.c Wed Oct 14 16:38:43 2009 (r1147) @@ -342,6 +342,35 @@ return ul; } +/* Tries to create a lock file to let only one instance of the program run + * @return 0 -> An instance is running + * @return 1 -> Lock file was created successfully + */ +static int get_lock(void) +{ + int fdlock; + struct flock fl; + + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 1; + + if((fdlock = open("pisamgmt.lock", O_WRONLY|O_CREAT, 666)) == -1) + { + return 0; + } + + if(fcntl(fdlock, F_SETLK, &fl) == -1) + { + return 0; + } + + return 1; + + +} + /** Main program that will be run on the user's computer. */ int main(int argc, char *argv[]) @@ -357,6 +386,12 @@ pisa_userhitlist_entry *uhlentry = NULL; pisa_userhitlist_entry new_entry = {}; + if(!get_lock()) + { + PISA_ERROR("A management instance is already running!"); + return ERROR_ALREADY_RUNNING; + } + pisa_mgmt_conf_init(&managementconf); new_entry.active = NO_CHANGE;