10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * CDDL HEADER START
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
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 *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * or http://www.opensolaris.org/os/licensing.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * See the License for the specific language governing permissions
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * and limitations under the License.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
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 *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * CDDL HEADER END
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Use is subject to license terms.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <stdio.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <stdlib.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <string.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/errno.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/param.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/stat.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/types.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <unistd.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <vroot/vroot.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <signal.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <errno.h> /* errno */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <libintl.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweextern char *sys_errlist[];
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweextern int sys_nerr;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowestatic void file_lock_error(char *msg, char *file, char *str, int arg1, int arg2);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#define BLOCK_INTERUPTS sigfillset(&newset) ; \
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sigprocmask(SIG_SETMASK, &newset, &oldset)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#define UNBLOCK_INTERUPTS \
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sigprocmask(SIG_SETMASK, &oldset, &newset)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
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 *
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 *
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 *
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 *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Returns an error message. NULL return means the lock was obtained.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
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 Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweint
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock(char *name, char *lockname, int *file_locked, int timeout)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int counter = 0;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe static char msg[MAXPATHLEN+1];
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int printed_warning = 0;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int r;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe struct stat statb;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sigset_t newset;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sigset_t oldset;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *file_locked = 0;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (timeout <= 0) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe timeout = 120;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe for (;;) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe BLOCK_INTERUPTS;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe r = symlink(name, lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (r == 0) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *file_locked = 1;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe UNBLOCK_INTERUPTS;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return 0; /* success */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe UNBLOCK_INTERUPTS;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (errno != EEXIST) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe file_lock_error(msg, name, (char *)"symlink(%s, %s)",
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (int) name, (int) lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe fprintf(stderr, "%s", msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return errno;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe counter = 0;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe for (;;) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sleep(1);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe r = lstat(lockname, &statb);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (r == -1) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The lock must have just gone away - try
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * again.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe break;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if ((counter > 5) && (!printed_warning)) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* Print waiting message after 5 secs */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) getcwd(msg, MAXPATHLEN);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe fprintf(stderr,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe gettext("file_lock: file %s is already locked.\n"),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe name);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe fprintf(stderr,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe gettext("file_lock: will periodically check the lockfile %s for two minutes.\n"),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe fprintf(stderr,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe gettext("Current working directory %s\n"),
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe printed_warning = 1;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (++counter > timeout ) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Waited enough - return an error..
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return EEXIST;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* NOTREACHED */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe}
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Format a message telling why the lock could not be created.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowestatic void
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock_error(char *msg, char *file, char *str, int arg1, int arg2)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int len;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sprintf(msg, gettext("Could not lock file `%s'; "), file);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe len = strlen(msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sprintf(&msg[len], str, arg1, arg2);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe strcat(msg, gettext(" failed - "));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (errno < sys_nerr) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe strcat(msg, strerror(errno));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe } else {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe len = strlen(msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe sprintf(&msg[len], "errno %d", errno);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe}
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe