27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate#include <stdio.h>
297c478bdstevel@tonic-gate#include <stdlib.h>
307c478bdstevel@tonic-gate#include <unistd.h>
317c478bdstevel@tonic-gate#include <string.h>
327c478bdstevel@tonic-gate#include <fcntl.h>
337c478bdstevel@tonic-gate#include <sys/types.h>
347c478bdstevel@tonic-gate#include <sys/param.h>
357c478bdstevel@tonic-gate#include <sys/stat.h>
367c478bdstevel@tonic-gate#include <errno.h>
407c478bdstevel@tonic-gate * Format a message telling why the lock could not be created.
417c478bdstevel@tonic-gate */
427c478bdstevel@tonic-gate/* VARARGS5 */
437c478bdstevel@tonic-gatestatic void
447c478bdstevel@tonic-gatefile_lock_error(char *msg, char *file, int err, const char *str,
457c478bdstevel@tonic-gate    char *arg1, char *arg2, size_t mlen)
477c478bdstevel@tonic-gate	size_t	len;
487c478bdstevel@tonic-gate	char	*errstr;
507c478bdstevel@tonic-gate	(void) snprintf(msg, mlen, "Could not lock file `%s'; ", file);
517c478bdstevel@tonic-gate	len = strlen(msg);
527c478bdstevel@tonic-gate	(void) snprintf(&msg[len], (mlen - len), str, arg1, arg2);
537c478bdstevel@tonic-gate	(void) strcat(msg, " failed - ");
547c478bdstevel@tonic-gate	if ((errstr = strerror(err)) != NULL) {
557c478bdstevel@tonic-gate		(void) strlcat(msg, errstr, mlen);
567c478bdstevel@tonic-gate	} else {
577c478bdstevel@tonic-gate		len = strlen(msg);
587c478bdstevel@tonic-gate		(void) sprintf(&msg[len], "errno %d", err);
597c478bdstevel@tonic-gate	}
637c478bdstevel@tonic-gate * This code stolen from the NSE library and changed to not depend
647c478bdstevel@tonic-gate * upon any NSE routines or header files.
657c478bdstevel@tonic-gate *
667c478bdstevel@tonic-gate * Simple file locking.
677c478bdstevel@tonic-gate * Create a symlink to a file.  The "test and set" will be
687c478bdstevel@tonic-gate * atomic as creating the symlink provides both functions.
697c478bdstevel@tonic-gate *
707c478bdstevel@tonic-gate * The timeout value specifies how long to wait for stale locks
717c478bdstevel@tonic-gate * to disappear.  If the lock is more than 'timeout' seconds old
727c478bdstevel@tonic-gate * then it is ok to blow it away.  This part has a small window
737c478bdstevel@tonic-gate * of vunerability as the operations of testing the time,
747c478bdstevel@tonic-gate * removing the lock and creating a new one are not atomic.
757c478bdstevel@tonic-gate * It would be possible for two processes to both decide to blow
767c478bdstevel@tonic-gate * away the lock and then have process A remove the lock and establish
777c478bdstevel@tonic-gate * its own, and then then have process B remove the lock which accidentily
787c478bdstevel@tonic-gate * removes A's lock rather than the stale one.
797c478bdstevel@tonic-gate *
807c478bdstevel@tonic-gate * A further complication is with the NFS.  If the file in question is
817c478bdstevel@tonic-gate * being served by an NFS server, then its time is set by that server.
827c478bdstevel@tonic-gate * We can not use the time on the client machine to check for a stale
837c478bdstevel@tonic-gate * lock.  Therefore, a temp file on the server is created to get
847c478bdstevel@tonic-gate * the servers current time.
857c478bdstevel@tonic-gate *
867c478bdstevel@tonic-gate * Returns an error message.  NULL return means the lock was obtained.
877c478bdstevel@tonic-gate *
887c478bdstevel@tonic-gate */
897c478bdstevel@tonic-gatechar *
907c478bdstevel@tonic-gatefile_lock(char *name, char *lockname, int timeout)
927c478bdstevel@tonic-gate	int		r;
937c478bdstevel@tonic-gate	int		fd;
947c478bdstevel@tonic-gate	struct	stat	statb;
957c478bdstevel@tonic-gate	struct	stat	fs_statb;
967c478bdstevel@tonic-gate	char		tmpname[MAXPATHLEN];
977c478bdstevel@tonic-gate	static	char	msg[MAXPATHLEN];
997c478bdstevel@tonic-gate	if (timeout <= 0) {
1007c478bdstevel@tonic-gate		timeout = 15;
1017c478bdstevel@tonic-gate	}
1027c478bdstevel@tonic-gate	for (;;) {
1037c478bdstevel@tonic-gate		r = symlink(name, lockname);
1047c478bdstevel@tonic-gate		if (r == 0) {
1057c478bdstevel@tonic-gate			return (NULL);
1067c478bdstevel@tonic-gate		}
1077c478bdstevel@tonic-gate		if (errno != EEXIST) {
1087c478bdstevel@tonic-gate			file_lock_error(msg, name, errno,
1097c478bdstevel@tonic-gate			    (const char *)"symlink(%s, %s)", name, lockname,
1107c478bdstevel@tonic-gate			    sizeof (msg));
1117c478bdstevel@tonic-gate			return (msg);
1127c478bdstevel@tonic-gate		}
1137c478bdstevel@tonic-gate		for (;;) {
1147c478bdstevel@tonic-gate			(void) sleep(1);
1157c478bdstevel@tonic-gate			r = lstat(lockname, &statb);
1167c478bdstevel@tonic-gate			if (r == -1) {
1177c478bdstevel@tonic-gate				/*
1187c478bdstevel@tonic-gate				 * The lock must have just gone away - try
1197c478bdstevel@tonic-gate				 * again.
1207c478bdstevel@tonic-gate				 */
1217c478bdstevel@tonic-gate				break;
1227c478bdstevel@tonic-gate			}
1247c478bdstevel@tonic-gate			/*
1257c478bdstevel@tonic-gate			 * With the NFS the time given a file is the time on
1267c478bdstevel@tonic-gate			 * the file server.  This time may vary from the
1277c478bdstevel@tonic-gate			 * client's time.  Therefore, we create a tmpfile in
1287c478bdstevel@tonic-gate			 * the same directory to establish the time on the
1297c478bdstevel@tonic-gate			 * server and use this time to see if the lock has
1307c478bdstevel@tonic-gate			 * expired.
1317c478bdstevel@tonic-gate			 */
1327c478bdstevel@tonic-gate			(void) snprintf(tmpname, MAXPATHLEN, "%s.XXXXXX",
1337c478bdstevel@tonic-gate			    lockname);
1347c478bdstevel@tonic-gate			(void) mktemp(tmpname);
1357c478bdstevel@tonic-gate			fd = creat(tmpname, 0666);
1367c478bdstevel@tonic-gate			if (fd != -1) {
1377c478bdstevel@tonic-gate				(void) close(fd);
1387c478bdstevel@tonic-gate			} else {
1397c478bdstevel@tonic-gate				file_lock_error(msg, name, errno,
1407c478bdstevel@tonic-gate				    (const char *)"creat(%s)", tmpname, 0,
1417c478bdstevel@tonic-gate				    sizeof (msg));
1427c478bdstevel@tonic-gate				return (msg);
1437c478bdstevel@tonic-gate			}
1447c478bdstevel@tonic-gate			if (stat(tmpname, &fs_statb) == -1) {
1457c478bdstevel@tonic-gate				file_lock_error(msg, name, errno,
1467c478bdstevel@tonic-gate				    (const char *)"stat(%s)", tmpname, 0,
1477c478bdstevel@tonic-gate				    sizeof (msg));
1487c478bdstevel@tonic-gate				return (msg);
1497c478bdstevel@tonic-gate			}
1507c478bdstevel@tonic-gate			(void) unlink(tmpname);
1517c478bdstevel@tonic-gate			if (statb.st_mtime + timeout < fs_statb.st_mtime) {
1527c478bdstevel@tonic-gate				/*
1537c478bdstevel@tonic-gate				 * The lock has expired - blow it away.
1547c478bdstevel@tonic-gate				 */
1557c478bdstevel@tonic-gate				(void) unlink(lockname);
1567c478bdstevel@tonic-gate				break;
1577c478bdstevel@tonic-gate			}
1587c478bdstevel@tonic-gate		}
1597c478bdstevel@tonic-gate	}
1607c478bdstevel@tonic-gate	/* NOTREACHED */