1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include "jitterbug.h"
#define TIMEOUT 20
#define LOCKFILE "/.lock"
/* lock and unlock the bug tracking system */
static int locked;
extern int guest;
void lock_system(void)
{
int i;
int fd;
int pid = getpid();
char fname[1000];
if (guest) return;
if (locked) {
locked++;
return;
}
check_overflow(strlen(root_directory()) + strlen(LOCKFILE) + 10, sizeof(fname));
sprintf(fname,"%s/%s", root_directory(), LOCKFILE);
for (i=0;i<TIMEOUT;i++) {
fd = open(fname, O_EXCL | O_CREAT | O_WRONLY, 0644);
if (fd != -1) {
write(fd, &pid, sizeof(pid));
close(fd);
locked = 1;
return;
}
sleep(1);
}
if (unlink(fname)) fatal("unable to create/remove lockfile %s : %s\n",
fname, strerror(errno));
/* damn, something probably died while it was locked. We'll
claim the lock for ourselves so that we can recover */
locked = 1;
}
void unlock_system(void)
{
char fname[1000];
if (guest) return;
if (locked > 1) {
locked--;
return;
}
if (!locked) return;
check_overflow(strlen(root_directory()) + strlen(LOCKFILE) + 10, sizeof(fname));
sprintf(fname,"%s/%s", root_directory(), LOCKFILE);
unlink(fname);
locked = 0;
}
|