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 <unistd.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <string.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <fcntl.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/types.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/param.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/stat.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <sys/errno.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <errno.h> /* errno */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#if defined(_LP64)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The symbols _sys_errlist and _sys_nerr are not visible in the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * LP64 libc. Use strerror(3C) instead.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#else /* #_LP64 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweextern char * sys_errlist[];
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweextern int sys_nerr;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif /* #_LP64 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowestatic void file_lock_error();
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 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowechar *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock(char * name, char * lockname, int timeout)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int r;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int fd;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe struct stat statb;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe struct stat fs_statb;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe char tmpname[MAXPATHLEN];
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe static char msg[MAXPATHLEN];
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (timeout <= 0) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe timeout = 15;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe for (;;) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe r = symlink(name, lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (r == 0) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return (NULL);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (errno != EEXIST) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe file_lock_error(msg, name,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (const char *)"symlink(%s, %s)", name, lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return (msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe for (;;) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) 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 /*
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 * expired.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) sprintf(tmpname, "%s.XXXXXX", lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) mktemp(tmpname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe fd = creat(tmpname, 0666);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (fd != -1) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) close(fd);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe } else {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe file_lock_error(msg, name,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (const char *)"creat(%s)", tmpname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return (msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (stat(tmpname, &fs_statb) == -1) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe file_lock_error(msg, name,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (const char *)"stat(%s)", tmpname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return (msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) unlink(tmpname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (statb.st_mtime + timeout < fs_statb.st_mtime) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The lock has expired - blow it away.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) unlink(lockname);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe break;
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 Lowe/* VARARGS4 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowestatic void
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowefile_lock_error(char * msg, char * file, const char * str, char * arg1,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe char * arg2)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int len;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) sprintf(msg, "Could not lock file `%s'; ", file);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe len = strlen(msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) sprintf(&msg[len], str, arg1, arg2);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) strcat(msg, " failed - ");
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#if defined(_LP64)
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /* Needs to be changed to use strerror(3C) instead. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe len = strlen(msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) sprintf(&msg[len], "errno %d", errno);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#else /* #_LP64 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe if (errno < sys_nerr) {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) strcat(msg, sys_errlist[errno]);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe } else {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe len = strlen(msg);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe (void) sprintf(&msg[len], "errno %d", errno);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif /* #_LP64 */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe}