|
|
1.1 ! root 1: # ! 2: ! 3: /* ! 4: * A mailing program. ! 5: * ! 6: * Stuff to do version 7 style locking. ! 7: */ ! 8: ! 9: #include "rcv.h" ! 10: #include <sys/stat.h> ! 11: ! 12: static char *SccsId = "@(#)lock.c 2.3 12/17/82"; ! 13: ! 14: static int locked; /* To note that we locked it */ ! 15: static char lockfile[64]; /* Last locked file */ ! 16: ! 17: /* ! 18: * Lock the specified mail file by setting the execute bit on mailfile. ! 19: * We must, of course, be careful to remove the lock by a call ! 20: * to unlock before we stop. The algorithm used here is to see if ! 21: * the lock exists, and if it does, to check its modify time. If it ! 22: * is older than 5 minutes, we assume error and set our own lock. ! 23: * Otherwise, we wait for 5 seconds and try again. ! 24: */ ! 25: ! 26: lock(file) ! 27: char *file; ! 28: { ! 29: register int f; ! 30: struct stat sbuf; ! 31: long curtime; ! 32: ! 33: if (file == NOSTR) { ! 34: printf("Locked = %d\n", locked); ! 35: return(0); ! 36: } ! 37: if (locked) ! 38: return(0); ! 39: strcpy(lockfile, file); ! 40: for (;;) { ! 41: f = lock1(file); ! 42: if (f == 0) { ! 43: locked = 1; ! 44: return(0); ! 45: } ! 46: if (stat(file, &sbuf) < 0) ! 47: return(0); ! 48: time(&curtime); ! 49: if (curtime < sbuf.st_ctime + 300) { ! 50: sleep(5); ! 51: continue; ! 52: } ! 53: unlock(); ! 54: } ! 55: } ! 56: ! 57: /* ! 58: * Remove the mail lock, and note that we no longer ! 59: * have it locked. ! 60: */ ! 61: ! 62: unlock() ! 63: { ! 64: struct stat sbuf; ! 65: ! 66: if (stat(lockfile, &sbuf) < 0) ! 67: return; ! 68: if (chmod(lockfile, sbuf.st_mode & ~01) < 0) ! 69: return; ! 70: locked = 0; ! 71: } ! 72: ! 73: /* ! 74: * Attempt to set the lock by setting the execute bit. ! 75: * If it fails, return -1 else 0 ! 76: */ ! 77: ! 78: lock1(file) ! 79: char *file; ! 80: { ! 81: struct stat sbuf; ! 82: ! 83: if (stat(file, &sbuf) < 0) ! 84: return(-1); ! 85: if (sbuf.st_mode & 01) ! 86: return(-1); ! 87: if (chmod(file, sbuf.st_mode | 01) < 0) ! 88: return(-1); ! 89: return(0); ! 90: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.