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 Loweextern char *sys_errlist[];
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowestatic void file_lock_error(char *msg, char *file, char *str, int arg1, int arg2);
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 Lowe * 12/6/91 Added the parameter "file_locked". Before this parameter
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * was added, the calling procedure would have to wait for file_lock()
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * to return before it sets the flag. If the user interrupted "make"
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * between the time the lock was acquired and the time file_lock()
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * returns, make wouldn't know that the file has been locked, and therefore
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * it wouldn' remove the lock. Setting the flag right after locking the file
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * makes this window much smaller.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock(char *name, char *lockname, int *file_locked, int timeout)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (r == 0) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return 0; /* success */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe file_lock_error(msg, name, (char *)"symlink(%s, %s)",
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (r == -1) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The lock must have just gone away - try
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* Print waiting message after 5 secs */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe gettext("file_lock: file %s is already locked.\n"),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe gettext("file_lock: will periodically check the lockfile %s for two minutes.\n"),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Waited enough - return an error..
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* NOTREACHED */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Format a message telling why the lock could not be created.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock_error(char *msg, char *file, char *str, int arg1, int arg2)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sprintf(msg, gettext("Could not lock file `%s'; "), file);