--- /dev/null	2013-09-14 18:48:47.000000000 +0100
+++ new/usr/src/cmd/lockf/lockf.c	2013-09-14 18:48:47.531780362 +0100
@@ -0,0 +1,272 @@
+/*
+ * Copyright (C) 2008 Paul Armstrong. All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ *  SUCH DAMAGE.
+ */
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <limits.h>
+#include <locale.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <wait.h>
+
+#define	SLEEP_TIME 1 /* Sleep time between parent checks */
+
+#if !defined(TEXT_DOMAIN)
+#define	TEXT_DOMAIN "SYS_TEST"
+#endif
+
+/* globals for use in signal handlers */
+int pid = 0;
+boolean_t lock_existed = B_FALSE;
+char *file_name = NULL;
+
+static void
+usage(void)
+{
+	(void) fprintf(stderr,
+	    gettext("Usage: lockf -l|-t|-w -f /path/to/lock | -u pid\n"));
+	exit(EXIT_FAILURE);
+}
+
+/*ARGSUSED*/
+static void
+print_pid(int signal_number)
+{
+	(void) printf("%d\n", pid);
+	exit(EXIT_SUCCESS);
+}
+
+/*ARGSUSED*/
+static void
+cleanup(int signal_handler)
+{
+	if (!lock_existed) {
+		if (-1 == unlink(file_name))
+			perror(file_name);
+	}
+	exit(EXIT_SUCCESS);
+}
+
+int
+main(int argc, char **argv)
+{
+	boolean_t arg_err = B_FALSE;
+	boolean_t lock = B_FALSE, test = B_FALSE;
+	boolean_t unlock = B_FALSE, wait_lock = B_FALSE;
+	extern char *optarg;
+	extern int optind;
+	int calling_pid;
+	int child_status;
+	int filedes;
+	int flag;
+	int return_code;
+	mode_t mode = S_IRUSR | S_IWUSR;
+	struct stat *statbuf;
+
+	(void) setlocale(LC_ALL, "");
+	(void) textdomain(TEXT_DOMAIN);
+
+	while ((flag = getopt(argc, argv, "f:ltu:w")) != -1) {
+		switch (flag) {
+		case 'f':
+			file_name = optarg;
+			break;
+		case 'l':
+			if (unlock || wait_lock || test)
+				arg_err = B_TRUE;
+			lock = B_TRUE;
+			break;
+		case 't':
+			if (unlock || lock)
+				arg_err = B_TRUE;
+			test = B_TRUE;
+			break;
+		case 'u':
+			if (lock || test)
+				arg_err = B_TRUE;
+			unlock = B_TRUE;
+			pid = atoi(optarg);
+			break;
+		case 'w':
+			if (unlock || lock || test)
+				arg_err = B_TRUE;
+			wait_lock = B_TRUE;
+			lock = B_TRUE;
+			break;
+		default:
+			usage();
+		}
+	}
+
+	if (1 == argc)
+		usage();
+
+	argc -= optind;
+	argv -= optind;
+
+	if (1 <= argc)
+		usage();
+
+	if (arg_err) {
+		(void) fprintf(stderr,
+		    gettext("Use only one of -l, -t, -u or -w\n"));
+		usage();
+	}
+
+	if ((lock || test) && (NULL == file_name)) {
+		(void) fprintf(stderr,
+		    gettext("-l, -t and -w require -f\n"));
+		usage();
+	}
+	if (unlock && pid <= 0) {
+		(void) fprintf(stderr,
+		    gettext("-u requires a valid PID\n"));
+		usage();
+	}
+
+	if (unlock) {
+		if (-1 == kill(pid, SIGINT)) {
+			perror("kill");
+			return (EXIT_FAILURE);
+		} else {
+			return (EXIT_SUCCESS);
+		}
+	}
+
+	/* Make test cheap, do it before the fork and return immediately */
+	if (test) {
+		statbuf = malloc(sizeof (struct stat));
+		if (NULL == statbuf) {
+			perror("malloc");
+			exit(EXIT_FAILURE);
+		}
+		if (stat(file_name, statbuf) == -1) {
+			switch (errno) {
+			case ENOENT:
+				exit(EXIT_SUCCESS);
+				/*NOTREACHED*/
+			case EACCES:
+				err(EXIT_FAILURE,
+				    gettext("Bad permissions on lockfile"));
+				/*NOTREACHED*/
+			case ENAMETOOLONG:
+				err(EXIT_FAILURE, "lockfile name too long");
+			}
+		}
+		if ((filedes = open(file_name, O_WRONLY, mode)) == -1) {
+			perror(file_name);
+			return (EXIT_FAILURE);
+		}
+		return_code = lockf(filedes, F_TEST, 0);
+		if (return_code < 0)
+			return (EXIT_FAILURE);
+		return (EXIT_SUCCESS);
+	}
+
+	calling_pid = getppid();
+	if (1 == calling_pid)
+		err(EXIT_FAILURE, "Calling process seems to have died early");
+
+	if ((pid = fork()) < 0) {
+		perror("fork");
+		return (EXIT_FAILURE);
+	}
+
+	if (pid != 0) {
+		(void) signal(SIGUSR1, print_pid);
+
+		if (waitpid(pid, &child_status, 0) != pid) {
+			perror("waitpid");
+			exit(EXIT_FAILURE);
+		}
+
+		return (EXIT_FAILURE);
+	} else {
+
+		(void) close(STDIN_FILENO);
+		(void) close(STDOUT_FILENO);
+
+		return_code = chdir("/");
+		if (return_code < 0) {
+			perror("chdir");
+			return (EXIT_FAILURE);
+		}
+
+		/* Other users shouldn't be trying to write to our lock file */
+		(void) umask(077);
+
+		/*
+		 * Note if the file existed beforehand so we can avoid deleting
+		 * it when we clean up
+		 */
+		if ((filedes =
+		    open(file_name, O_WRONLY | O_CREAT | O_EXCL, mode)) == -1) {
+
+			lock_existed = B_TRUE;
+
+			if ((filedes =
+			    open(file_name, O_WRONLY | O_CREAT, mode)) == -1) {
+				perror(file_name);
+				exit(EXIT_FAILURE);
+			}
+		}
+
+		(void) signal(SIGINT, cleanup);
+		(void) signal(SIGQUIT, cleanup);
+		(void) signal(SIGABRT, cleanup);
+		(void) signal(SIGTERM, cleanup);
+
+		if (wait_lock) {
+			return_code = lockf(filedes, F_LOCK, 0);
+		} else {
+			return_code = lockf(filedes, F_TLOCK, 0);
+		}
+
+		if (return_code < 0)
+			return (EXIT_FAILURE);
+
+		/* Once we have a lock (or not), then kill the parent */
+		if (-1 == kill(getppid(), SIGUSR1)) {
+			perror("kill");
+			return (EXIT_FAILURE);
+		}
+
+		while (calling_pid) {
+			(void) sleep(SLEEP_TIME);
+			if (-1 == kill(calling_pid, 0))
+				calling_pid = 0;
+		}
+
+		cleanup(NULL);
+
+		return (EXIT_SUCCESS);
+	}
+}
