10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * CDDL HEADER START
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The contents of this file are subject to the terms of the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Common Development and Distribution License (the "License").
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * You may not use this file except in compliance with the License.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * See the License for the specific language governing permissions
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * and limitations under the License.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * If applicable, add the following below this CDDL HEADER, with the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * CDDL HEADER END
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Use is subject to license terms.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The symbols _sys_errlist and _sys_nerr are not visible in the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * LP64 libc. Use strerror(3C) instead.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#else /* #_LP64 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweextern char * sys_errlist[];
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif /* #_LP64 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * This code stolen from the NSE library and changed to not depend
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * upon any NSE routines or header files.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Simple file locking.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Create a symlink to a file. The "test and set" will be
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * atomic as creating the symlink provides both functions.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The timeout value specifies how long to wait for stale locks
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * to disappear. If the lock is more than 'timeout' seconds old
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * then it is ok to blow it away. This part has a small window
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * of vunerability as the operations of testing the time,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * removing the lock and creating a new one are not atomic.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * It would be possible for two processes to both decide to blow
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * away the lock and then have process A remove the lock and establish
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * its own, and then then have process B remove the lock which accidentily
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * removes A's lock rather than the stale one.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * A further complication is with the NFS. If the file in question is
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * being served by an NFS server, then its time is set by that server.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * We can not use the time on the client machine to check for a stale
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * lock. Therefore, a temp file on the server is created to get
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * the servers current time.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Returns an error message. NULL return means the lock was obtained.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock(char * name, char * lockname, int timeout)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (r == 0) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (const char *)"symlink(%s, %s)", name, lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (r == -1) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The lock must have just gone away - try
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * With the NFS the time given a file is the time on
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * the file server. This time may vary from the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * client's time. Therefore, we create a tmpfile in
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * the same directory to establish the time on the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * server and use this time to see if the lock has
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) sprintf(tmpname, "%s.XXXXXX", lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (statb.st_mtime + timeout < fs_statb.st_mtime) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The lock has expired - blow it away.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* NOTREACHED */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Format a message telling why the lock could not be created.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/* VARARGS4 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock_error(char * msg, char * file, const char * str, char * arg1,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) sprintf(msg, "Could not lock file `%s'; ", file);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* Needs to be changed to use strerror(3C) instead. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#else /* #_LP64 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif /* #_LP64 */