Commit | Line | Data |
---|---|---|
0f5e4452 KS |
1 | /* Copyright (c) 1979 Regents of the University of California */ |
2 | char maillock[] = ".lock"; /* Lock suffix for mailname */ | |
3 | char locktmp[] = "/usr/spool/mail/tmXXXXXX"; | |
4 | char curlock[50]; /* Last used name of lock */ | |
5 | static int locked; /* To note that we locked it */ | |
6 | ||
7 | /* | |
8 | * Lock the specified mail file by setting the file mailfile.lock. | |
9 | * We must, of course, be careful to remove the lock file by a call | |
10 | * to unlock before we stop. The algorithm used here is to see if | |
11 | * the lock exists, and if it does, to check its modify time. If it | |
12 | * is older than 200 seconds, we assume error and set our own file. | |
13 | * Otherwise, we wait for 5 seconds and try again. | |
14 | */ | |
15 | ||
16 | lock(file) | |
17 | char *file; | |
18 | { | |
19 | register int f; | |
20 | struct stat sbuf; | |
21 | long curtime; | |
22 | ||
23 | if (locked) | |
24 | return(0); | |
25 | strcpy(curlock, mailname); | |
26 | strcat(curlock, maillock); | |
27 | mktemp(locktmp); | |
28 | unlink(locktmp); | |
29 | for (;;) { | |
30 | f = lock1(locktmp, curlock); | |
31 | if (f == 0) { | |
32 | locked = 1; | |
33 | return(0); | |
34 | } | |
35 | stat(curlock, &sbuf); | |
36 | time(&curtime); | |
37 | if (curtime < sbuf.st_ctime + 200) { | |
38 | sleep(5); | |
39 | continue; | |
40 | } | |
41 | unlink(curlock); | |
42 | cnt++; | |
43 | } | |
44 | return(-1); | |
45 | } | |
46 | ||
47 | /* | |
48 | * Remove the mail lock, and note that we no longer | |
49 | * have it locked. | |
50 | */ | |
51 | ||
52 | unlock() | |
53 | { | |
54 | ||
55 | unlink(curlock); | |
56 | locked = 0; | |
57 | } | |
58 | ||
59 | /* | |
60 | * Attempt to set the lock by creating the temporary file, | |
61 | * then doing a link/unlink. If it fails, return -1 else 0 | |
62 | */ | |
63 | ||
64 | lock1(tempfile, name) | |
65 | char tempfile[], name[]; | |
66 | { | |
67 | register int fd; | |
68 | ||
69 | fd = creat(tempfile, 0); | |
70 | if (fd < 0) | |
71 | return(-1); | |
72 | if (link(tempfile, name) < 0) { | |
73 | unlink(tempfile); | |
74 | return(-1); | |
75 | } | |
76 | unlink(tempfile); | |
77 | close(fd); | |
78 | return(0); | |
79 | } |