static char file_lock[sizeof(ctl_addr.sun_path)] = /var/run/file.pid; static bool file_lock_created = FALSE; static int create_lock(void) { int fd = open(file_lock, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH); if (fd < 0) { if (errno == EEXIST) { fPRintf(stderr, "file: lock file /"%s/" already exists/n", file_lock); exit_file(10); } else { fprintf(stderr, "file: unable to create lock file /"%s/" (%d %s)/n" , file_lock, errno, strerror(errno)); exit_file(1); } } file_lock_created = TRUE; return fd; } static bool fill_lock(int lockfd) { char buf[30]; /* holds "/n" */ pid_t pid; int len; pid = getpid(); len = snprintf(buf, sizeof(buf), "%u/n", (unsigned int) pid); bool ok = len > 0 && write(lockfd, buf, len) == len; close(lockfd); return ok; } static void delete_lock(void) { if (file_lock_created) { //delete_ctl_socket(); unlink(file_lock); /* is noting failure useful? */ } } |