4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/types.h>
43 static char *lockext
= ".lock"; /* Lock suffix for mailname */
44 static char curlock
[PATHSIZE
]; /* Last used name of lock */
45 static int locked
; /* To note that we locked it */
46 static time_t locktime
; /* time lock file was touched */
47 static time_t lock1(char *, char *);
50 * Lock the specified mail file by setting the file mailfile.lock.
51 * We must, of course, be careful to remove the lock file by a call
52 * to unlock before we stop. The algorithm used here is to see if
53 * the lock exists, and if it does, to check its modify time. If it
54 * is older than 5 minutes, we assume error and set our own file.
55 * Otherwise, we wait for 5 seconds and try again.
60 maillock(char *user
, int retrycnt
)
65 char locktmp
[PATHSIZE
]; /* Usable lock temporary */
70 (void) strcpy(file
, MAILDIR
);
71 (void) strcat(file
, user
);
72 (void) strcpy(curlock
, file
);
73 (void) strcat(curlock
, lockext
);
74 (void) strcpy(locktmp
, file
);
75 (void) strcat(locktmp
, "XXXXXX");
76 (void) mktemp(locktmp
);
77 (void) remove(locktmp
);
80 t
= lock1(locktmp
, curlock
);
86 if (stat(curlock
, &sbuf
) < 0) {
95 * Compare the time of the temp file with the time
96 * of the lock file, rather than with the current
97 * time of day, since the files may reside on
98 * another machine whose time of day differs from
99 * ours. If the lock file is less than 5 minutes
102 if (t
< sbuf
.st_ctime
+ 300) {
106 (void) remove(curlock
);
111 * Remove the mail lock, and note that we no longer
117 (void) remove(curlock
);
122 * Attempt to set the lock by creating the temporary file,
123 * then doing a link/unlink. If it succeeds, return 0,
124 * else return a guess of the current time on the machine
128 lock1(char tempfile
[], char name
[])
133 fd
= open(tempfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
136 (void) fstat(fd
, &sbuf
);
138 * Write the string "0" into the lock file to give us some
139 * interoperability with SVR4 mailers. SVR4 mailers expect
140 * a process ID to be written into the lock file and then
141 * use kill() to see if the process is alive or not. We write
142 * 0 into it so that SVR4 mailers will always think our lock file
145 (void) write(fd
, "0", 2);
147 if (link(tempfile
, name
) < 0) {
148 (void) remove(tempfile
);
149 return (sbuf
.st_ctime
);
151 (void) remove(tempfile
);
156 * Update the change time on the lock file so
157 * others will know we're still using it.
169 /* if it hasn't been at least 3 minutes, don't bother */
170 if (time(&t
) < locktime
+ 180)
174 if (stat(curlock
, &sbuf
) < 0)
177 * Don't actually change the times, we just want the
178 * side effect that utime causes st_ctime to be set
179 * to the current time.
181 tp
.actime
= sbuf
.st_atime
;
182 tp
.modtime
= sbuf
.st_mtime
;
183 (void) utime(curlock
, &tp
);