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]
22 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
31 #include <sys/types.h>
32 #include <sys/param.h>
34 #include <sys/errno.h>
35 #include <errno.h> /* errno */
39 * The symbols _sys_errlist and _sys_nerr are not visible in the
40 * LP64 libc. Use strerror(3C) instead.
43 extern char * sys_errlist
[];
47 static void file_lock_error();
50 * This code stolen from the NSE library and changed to not depend
51 * upon any NSE routines or header files.
53 * Simple file locking.
54 * Create a symlink to a file. The "test and set" will be
55 * atomic as creating the symlink provides both functions.
57 * The timeout value specifies how long to wait for stale locks
58 * to disappear. If the lock is more than 'timeout' seconds old
59 * then it is ok to blow it away. This part has a small window
60 * of vunerability as the operations of testing the time,
61 * removing the lock and creating a new one are not atomic.
62 * It would be possible for two processes to both decide to blow
63 * away the lock and then have process A remove the lock and establish
64 * its own, and then then have process B remove the lock which accidentily
65 * removes A's lock rather than the stale one.
67 * A further complication is with the NFS. If the file in question is
68 * being served by an NFS server, then its time is set by that server.
69 * We can not use the time on the client machine to check for a stale
70 * lock. Therefore, a temp file on the server is created to get
71 * the servers current time.
73 * Returns an error message. NULL return means the lock was obtained.
77 file_lock(char * name
, char * lockname
, int timeout
)
83 char tmpname
[MAXPATHLEN
];
84 static char msg
[MAXPATHLEN
];
90 r
= symlink(name
, lockname
);
94 if (errno
!= EEXIST
) {
95 file_lock_error(msg
, name
,
96 (const char *)"symlink(%s, %s)", name
, lockname
);
101 r
= lstat(lockname
, &statb
);
104 * The lock must have just gone away - try
111 * With the NFS the time given a file is the time on
112 * the file server. This time may vary from the
113 * client's time. Therefore, we create a tmpfile in
114 * the same directory to establish the time on the
115 * server and use this time to see if the lock has
118 (void) sprintf(tmpname
, "%s.XXXXXX", lockname
);
119 (void) mktemp(tmpname
);
120 fd
= creat(tmpname
, 0666);
124 file_lock_error(msg
, name
,
125 (const char *)"creat(%s)", tmpname
);
128 if (stat(tmpname
, &fs_statb
) == -1) {
129 file_lock_error(msg
, name
,
130 (const char *)"stat(%s)", tmpname
);
133 (void) unlink(tmpname
);
134 if (statb
.st_mtime
+ timeout
< fs_statb
.st_mtime
) {
136 * The lock has expired - blow it away.
138 (void) unlink(lockname
);
147 * Format a message telling why the lock could not be created.
151 file_lock_error(char * msg
, char * file
, const char * str
, char * arg1
,
156 (void) sprintf(msg
, "Could not lock file `%s'; ", file
);
158 (void) sprintf(&msg
[len
], str
, arg1
, arg2
);
159 (void) strcat(msg
, " failed - ");
161 /* Needs to be changed to use strerror(3C) instead. */
163 (void) sprintf(&msg
[len
], "errno %d", errno
);
165 if (errno
< sys_nerr
) {
166 (void) strcat(msg
, sys_errlist
[errno
]);
169 (void) sprintf(&msg
[len
], "errno %d", errno
);