1 --- a/include/applets.src.h
2 +++ b/include/applets.src.h
3 @@ -211,6 +211,7 @@ IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN,
4 IF_LOAD_POLICY(APPLET(load_policy, BB_DIR_USR_SBIN, BB_SUID_DROP))
5 IF_LOADFONT(APPLET(loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP))
6 IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP))
7 +IF_LOCK(APPLET(lock, BB_DIR_BIN, BB_SUID_DROP))
8 IF_LOGGER(APPLET(logger, BB_DIR_USR_BIN, BB_SUID_DROP))
9 /* Needs to be run by root or be suid root - needs to change uid and gid: */
10 IF_LOGIN(APPLET(login, BB_DIR_BIN, BB_SUID_REQUIRE))
11 --- a/miscutils/Config.src
12 +++ b/miscutils/Config.src
13 @@ -385,6 +385,12 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
15 Enables the 'hdparm -d' option to get/set using_dma flag.
21 + Small utility for using locks in scripts
26 --- a/miscutils/Kbuild.src
27 +++ b/miscutils/Kbuild.src
28 @@ -28,6 +28,7 @@ lib-$(CONFIG_INOTIFYD) += inotifyd.o
29 lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
30 lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
31 lib-$(CONFIG_LESS) += less.o
32 +lib-$(CONFIG_LOCK) += lock.o
33 lib-$(CONFIG_MAKEDEVS) += makedevs.o
34 lib-$(CONFIG_MAN) += man.o
35 lib-$(CONFIG_MICROCOM) += microcom.o
37 +++ b/miscutils/lock.c
40 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
42 + * This is free software, licensed under the GNU General Public License v2.
44 +#include <sys/types.h>
45 +#include <sys/file.h>
46 +#include <sys/stat.h>
53 +//usage:#define lock_trivial_usage NOUSAGE_STR
54 +//usage:#define lock_full_usage ""
56 +static int unlock = 0;
57 +static int shared = 0;
58 +static int waitonly = 0;
62 +static void usage(char *name)
64 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
65 + " -s Use shared locking\n"
67 + " -w Wait for the lock to become free, don't acquire lock\n"
72 +static void exit_unlock(int sig)
78 +static int do_unlock(void)
83 + if ((f = fopen(file, "r")) == NULL)
86 + fscanf(f, "%d", &i);
95 +static int do_lock(void)
100 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
101 + if ((fd = open(file, O_RDWR)) < 0) {
102 + fprintf(stderr, "Can't open %s\n", file);
107 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
108 + fprintf(stderr, "Can't lock %s\n", file);
118 + signal(SIGKILL, exit_unlock);
119 + signal(SIGTERM, exit_unlock);
120 + signal(SIGINT, exit_unlock);
128 + lseek(fd, 0, SEEK_SET);
130 + sprintf(pidstr, "%d\n", pid);
131 + write(fd, pidstr, strlen(pidstr));
140 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
141 +int lock_main(int argc, char **argv)
143 + char **args = &argv[1];
146 + while ((*args != NULL) && (*args)[0] == '-') {
148 + while (*(++ch) > 0) {
170 + return do_unlock();