Author: tjansen Date: Thu Nov 26 16:21:48 2009 New Revision: 1819 Log: Rewrote pisa_create_lock_file(). This gets rid of PISA_IFEL in util.c. Also, the killold option has been removed. Modified: trunk/libpisa/util.c trunk/libpisa/util.h trunk/pisacd/cdmain.c trunk/pisasd/sdmain.c Modified: trunk/libpisa/util.c ============================================================================== --- trunk/libpisa/util.c Thu Nov 26 16:14:45 2009 (r1818) +++ trunk/libpisa/util.c Thu Nov 26 16:21:48 2009 (r1819) @@ -246,93 +246,48 @@ return err; } +/** + * Remove the lock file. + * + * @param filename filename for the lock file + * @return 0 on success, -1 otherwise + */ int pisa_remove_lock_file(const char *filename) { return unlink(filename); } -int pisa_create_lock_file(const char *filename, int killold) +/** + * Create and lock a lock file to prevent multiple daemons from running. + * If we're a new process trying to get hold of an already existing lock we + * exit. If we're the only process, we print our process id to the lock file. + * + * Note that we don't close fd to keep the lock intact. Since we use lockf and + * not file existence for the decision, lingering files do not prevent us + * from running if their old process lost their lock (i.e. died). + * + * @param filename filename for the lock file + */ +void pisa_create_lock_file(const char *filename) { - int err = 0, fd = 0, old_pid = 0, new_pid_str_len = 0; - char old_pid_str[64], new_pid_str[64]; + char pid_str[8]; + int fd, pid; - memset(old_pid_str, 0, sizeof(old_pid_str)); - memset(new_pid_str, 0, sizeof(new_pid_str)); - - /* New pid */ - snprintf(new_pid_str, sizeof(new_pid_str)-1, "%d\n", getpid()); - new_pid_str_len = strlen(new_pid_str); - PISA_IFEL((new_pid_str_len <= 0), -1, "pID length error.\n"); - - /* Read old pid */ - fd = open(filename, O_RDWR | O_CREAT, 0644); - PISA_IFEL((fd <= 0), -1, "opening lock file failed\n"); - - if (read(fd, old_pid_str, sizeof(old_pid_str) - 1) < 0) { - PISA_ERROR("Cannot read data from fd[%d] (%s)\n", - fd, strerror(errno)); - err = errno; - goto out_err; - } - old_pid = atoi(old_pid_str); - - // TODO we should exit in this case, if the kill switch is not set - - if (lockf(fd, F_TLOCK, 0) < 0) - { - PISA_IFEL(!killold, -12, - "PISA daemon already running with pid %d\n", old_pid); - - PISA_INFO("\nPISA daemon already running with pid %d\n" - "-k option given, terminating old one...\n", old_pid); - /* Erase the old lock file to avoid having multiple pids - in the file */ - if (lockf(fd, F_ULOCK, 0) < 0) { - PISA_ERROR("Cannot acquire a lock on fd[%d] (%s)\n", - fd, strerror(errno)); - err = errno; - goto out_err; - } - close(fd); - PISA_IFEL(pisa_remove_lock_file(filename), -1, - "Removing lock file failed.\n"); - - /* fd = open(filename, O_RDWR | O_CREAT, 0644); */ - fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644); - - /* Don't close file descriptor because new started process is - running. */ - PISA_IFEL((fd <= 0), -1, "Opening lock file failed.\n"); - PISA_IFEL(lockf(fd, F_TLOCK, 0), -1, "Lock attempt failed.\n"); - - err = kill(old_pid, SIGKILL); - if (err != 0) { - PISA_ERROR("\nError when trying to send signal SIGKILL "\ - "process identified by process identifier "\ - "%d.\n", old_pid); - PISA_PERROR("errno after kill() is: "); - } - } - /* else if (killold) - { - lseek(fd,0,SEEK_SET); - write(fd, new_pid_str, new_pid_str_len); - system("NEW_PID=$(sudo awk NR==1 /var/lock/hipd.lock)"); - system("OLD_PID=$(/bin/pidof -o $NEW_PID hipd)"); - system("kill -9 $OLD_PID"); - } */ - - lseek(fd, 0, SEEK_SET); - - PISA_IFEL((write(fd, new_pid_str, new_pid_str_len) != new_pid_str_len), - -1, "Writing new process identifier failed.\n"); + if (!(fd = open(filename, O_RDWR | O_CREAT, 0644))) { + PISA_ERROR("Can't open lock file."); + exit(1); + } - out_err: - if (err == -12) { - exit(0); + if (lockf(fd, F_TLOCK, 0) < 0) { + memset(pid_str, 0, sizeof(pid_str)); + read(fd, pid_str, sizeof(pid_str) - 1); + pid = atoi(pid_str); + PISA_ERROR("Another process is already running: PID %i.\n", pid); + exit(2); } - return err; + snprintf(pid_str, sizeof(pid_str), "%d\n", getpid()); + write(fd, pid_str, strlen(pid_str)); } /** Modified: trunk/libpisa/util.h ============================================================================== --- trunk/libpisa/util.h Thu Nov 26 16:14:45 2009 (r1818) +++ trunk/libpisa/util.h Thu Nov 26 16:21:48 2009 (r1819) @@ -55,7 +55,7 @@ bool pisa_check_if_hipd_runs(void); -int pisa_create_lock_file(const char *filename, const int killold); +void pisa_create_lock_file(const char *filename); int pisa_remove_lock_file(const char *filename); int pisa_read_nameserver(struct in_addr *dnsaddr, const char *resolvconf); Modified: trunk/pisacd/cdmain.c ============================================================================== --- trunk/pisacd/cdmain.c Thu Nov 26 16:14:45 2009 (r1818) +++ trunk/pisacd/cdmain.c Thu Nov 26 16:21:48 2009 (r1819) @@ -302,7 +302,7 @@ cd_get_cmdargs(argc, argv); /* Check and create lockfile */ - pisa_create_lock_file(PISACD_LOCK_FILE, cd_ctx.flag_kill_running); + pisa_create_lock_file(PISACD_LOCK_FILE); /* Check, whether hipd is running. This is a requirement for * using pisacd. The output will be written to stderr before Modified: trunk/pisasd/sdmain.c ============================================================================== --- trunk/pisasd/sdmain.c Thu Nov 26 16:14:45 2009 (r1818) +++ trunk/pisasd/sdmain.c Thu Nov 26 16:21:48 2009 (r1819) @@ -330,7 +330,7 @@ sd_get_cmdargs(argc, argv); /* Check and create lockfile */ - pisa_create_lock_file(PISASD_LOCK_FILE, sd_ctx.flag_kill_running); + pisa_create_lock_file(PISASD_LOCK_FILE); /* Check, whether hipd is running. This is a requirement for * using pisasd. The output will be written to stderr before