2008-08-20 14:00:34 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/miscutils/lock.c
|
2017-01-15 01:21:40 +00:00
|
|
|
@@ -0,0 +1,155 @@
|
2006-06-27 00:51:59 +00:00
|
|
|
+/*
|
2016-06-07 06:58:31 +00:00
|
|
|
+ * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
|
2006-06-27 00:51:59 +00:00
|
|
|
+ *
|
|
|
|
+ * This is free software, licensed under the GNU General Public License v2.
|
|
|
|
+ */
|
2017-01-15 01:21:40 +00:00
|
|
|
+
|
|
|
|
+//config:config LOCK
|
|
|
|
+//config: bool "lock"
|
|
|
|
+//config: default n
|
|
|
|
+//config: help
|
|
|
|
+//config: Small utility for using locks in scripts
|
|
|
|
+
|
|
|
|
+//applet:IF_LOCK(APPLET(lock, BB_DIR_BIN, BB_SUID_DROP))
|
|
|
|
+
|
|
|
|
+//kbuild:lib-$(CONFIG_LOCK) += lock.o
|
|
|
|
+
|
|
|
|
+//usage:#define lock_trivial_usage NOUSAGE_STR
|
|
|
|
+//usage:#define lock_full_usage ""
|
|
|
|
+
|
2006-04-05 02:09:22 +00:00
|
|
|
+#include <sys/types.h>
|
|
|
|
+#include <sys/file.h>
|
|
|
|
+#include <sys/stat.h>
|
|
|
|
+#include <signal.h>
|
|
|
|
+#include <fcntl.h>
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <stdio.h>
|
2007-11-10 16:51:11 +00:00
|
|
|
+#include "busybox.h"
|
2006-04-05 02:09:22 +00:00
|
|
|
+
|
|
|
|
+static int unlock = 0;
|
|
|
|
+static int shared = 0;
|
|
|
|
+static int waitonly = 0;
|
2015-09-09 20:36:10 +00:00
|
|
|
+static int try_lock = 0;
|
2006-04-05 02:09:22 +00:00
|
|
|
+static int fd;
|
|
|
|
+static char *file;
|
|
|
|
+
|
|
|
|
+static void usage(char *name)
|
|
|
|
+{
|
|
|
|
+ fprintf(stderr, "Usage: %s [-suw] <filename>\n"
|
|
|
|
+ " -s Use shared locking\n"
|
|
|
|
+ " -u Unlock\n"
|
|
|
|
+ " -w Wait for the lock to become free, don't acquire lock\n"
|
2015-09-09 20:36:10 +00:00
|
|
|
+ " -n Don't wait for the lock to become free. Fail with exit code\n"
|
2006-04-05 02:09:22 +00:00
|
|
|
+ "\n", name);
|
|
|
|
+ exit(1);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void exit_unlock(int sig)
|
|
|
|
+{
|
|
|
|
+ flock(fd, LOCK_UN);
|
|
|
|
+ exit(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int do_unlock(void)
|
|
|
|
+{
|
|
|
|
+ FILE *f;
|
|
|
|
+ int i;
|
2007-11-10 16:51:11 +00:00
|
|
|
+
|
2006-04-11 13:54:43 +00:00
|
|
|
+ if ((f = fopen(file, "r")) == NULL)
|
|
|
|
+ return 0;
|
2007-11-10 16:51:11 +00:00
|
|
|
+
|
2006-04-05 02:09:22 +00:00
|
|
|
+ fscanf(f, "%d", &i);
|
|
|
|
+ if (i > 0)
|
|
|
|
+ kill(i, SIGTERM);
|
2007-11-10 16:51:11 +00:00
|
|
|
+
|
2006-04-05 02:09:22 +00:00
|
|
|
+ fclose(f);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
2007-11-10 16:51:11 +00:00
|
|
|
+
|
2006-04-05 02:09:22 +00:00
|
|
|
+static int do_lock(void)
|
|
|
|
+{
|
|
|
|
+ int pid;
|
2015-09-09 20:36:10 +00:00
|
|
|
+ int flags;
|
2006-04-05 02:09:22 +00:00
|
|
|
+ char pidstr[8];
|
|
|
|
+
|
2006-10-26 01:32:49 +00:00
|
|
|
+ if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
|
|
|
|
+ if ((fd = open(file, O_RDWR)) < 0) {
|
|
|
|
+ fprintf(stderr, "Can't open %s\n", file);
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
2006-04-05 02:09:22 +00:00
|
|
|
+ }
|
|
|
|
+
|
2015-09-09 20:36:10 +00:00
|
|
|
+ flags = shared ? LOCK_SH : LOCK_EX;
|
|
|
|
+ flags |= try_lock ? LOCK_NB : 0;
|
|
|
|
+
|
|
|
|
+ if (flock(fd, flags) < 0) {
|
2006-04-05 02:09:22 +00:00
|
|
|
+ fprintf(stderr, "Can't lock %s\n", file);
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pid = fork();
|
|
|
|
+
|
|
|
|
+ if (pid < 0)
|
|
|
|
+ return -1;
|
2007-11-10 16:51:11 +00:00
|
|
|
+
|
2006-04-05 02:09:22 +00:00
|
|
|
+ if (pid == 0) {
|
|
|
|
+ signal(SIGKILL, exit_unlock);
|
|
|
|
+ signal(SIGTERM, exit_unlock);
|
|
|
|
+ signal(SIGINT, exit_unlock);
|
|
|
|
+ if (waitonly)
|
|
|
|
+ exit_unlock(0);
|
|
|
|
+ else
|
|
|
|
+ while (1)
|
|
|
|
+ sleep(1);
|
|
|
|
+ } else {
|
|
|
|
+ if (!waitonly) {
|
|
|
|
+ lseek(fd, 0, SEEK_SET);
|
|
|
|
+ ftruncate(fd, 0);
|
|
|
|
+ sprintf(pidstr, "%d\n", pid);
|
|
|
|
+ write(fd, pidstr, strlen(pidstr));
|
|
|
|
+ close(fd);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
2006-12-31 15:40:13 +00:00
|
|
|
+ return 0;
|
2006-04-05 02:09:22 +00:00
|
|
|
+}
|
|
|
|
+
|
2007-11-10 16:51:11 +00:00
|
|
|
+int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2006-04-05 02:09:22 +00:00
|
|
|
+int lock_main(int argc, char **argv)
|
|
|
|
+{
|
|
|
|
+ char **args = &argv[1];
|
|
|
|
+ int c = argc - 1;
|
|
|
|
+
|
|
|
|
+ while ((*args != NULL) && (*args)[0] == '-') {
|
|
|
|
+ char *ch = *args;
|
|
|
|
+ while (*(++ch) > 0) {
|
|
|
|
+ switch(*ch) {
|
|
|
|
+ case 'w':
|
|
|
|
+ waitonly = 1;
|
|
|
|
+ break;
|
|
|
|
+ case 's':
|
|
|
|
+ shared = 1;
|
|
|
|
+ break;
|
|
|
|
+ case 'u':
|
|
|
|
+ unlock = 1;
|
|
|
|
+ break;
|
2015-09-09 20:36:10 +00:00
|
|
|
+ case 'n':
|
|
|
|
+ try_lock = 1;
|
|
|
|
+ break;
|
2006-04-05 02:09:22 +00:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ c--;
|
|
|
|
+ args++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (c != 1)
|
|
|
|
+ usage(argv[0]);
|
|
|
|
+
|
|
|
|
+ file = *args;
|
|
|
|
+ if (unlock)
|
|
|
|
+ return do_unlock();
|
|
|
|
+ else
|
|
|
|
+ return do_lock();
|
|
|
|
+}
|