|
|
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: char *maillock = ".lock"; /* Lock suffix for mailname */
13: char *lockname = "/usr/spool/mail/tmXXXXXX";
14: char locktmp[30]; /* Usable lock temporary */
15: static char curlock[50]; /* Last used name of lock */
16: static int locked; /* To note that we locked it */
17:
18: /*
19: * Lock the specified mail file by setting the file mailfile.lock.
20: * We must, of course, be careful to remove the lock file by a call
21: * to unlock before we stop. The algorithm used here is to see if
22: * the lock exists, and if it does, to check its modify time. If it
23: * is older than 30 seconds, we assume error and set our own file.
24: * Otherwise, we wait for 5 seconds and try again.
25: */
26:
27: lock(file)
28: char *file;
29: {
30: register int f;
31: struct stat sbuf;
32: long curtime;
33:
34: if (file == NOSTR) {
35: printf("Locked = %d\n", locked);
36: return(0);
37: }
38: if (locked)
39: return(0);
40: strcpy(curlock, file);
41: strcat(curlock, maillock);
42: strcpy(locktmp, lockname);
43: mktemp(locktmp);
44: remove(locktmp);
45: for (;;) {
46: f = lock1(locktmp, curlock);
47: if (f == 0) {
48: locked = 1;
49: return(0);
50: }
51: if (stat(curlock, &sbuf) < 0)
52: return(0);
53: time(&curtime);
54: if (curtime < sbuf.st_ctime + 30) {
55: sleep(5);
56: continue;
57: }
58: remove(curlock);
59: }
60: }
61:
62: /*
63: * Remove the mail lock, and note that we no longer
64: * have it locked.
65: */
66:
67: unlock()
68: {
69:
70: remove(curlock);
71: locked = 0;
72: }
73:
74: /*
75: * Attempt to set the lock by creating the temporary file,
76: * then doing a link/unlink. If it fails, return -1 else 0
77: */
78:
79: lock1(tempfile, name)
80: char tempfile[], name[];
81: {
82: register int fd;
83:
84: fd = creat(tempfile, 0);
85: if (fd < 0)
86: return(-1);
87: close(fd);
88: if (link(tempfile, name) < 0) {
89: remove(tempfile);
90: return(-1);
91: }
92: remove(tempfile);
93: return(0);
94: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.