1 --- a/include/applets.src.h
2 +++ b/include/applets.src.h
3 @@ -218,6 +218,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 @@ -419,6 +419,11 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
15 Enables the 'hdparm -d' option to get/set using_dma flag.
20 + Small utility for using locks in scripts
25 --- a/miscutils/Kbuild.src
26 +++ b/miscutils/Kbuild.src
27 @@ -29,6 +29,7 @@ lib-$(CONFIG_INOTIFYD) += inotifyd.o
28 lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
29 lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
30 lib-$(CONFIG_LESS) += less.o
31 +lib-$(CONFIG_LOCK) += lock.o
32 lib-$(CONFIG_MAKEDEVS) += makedevs.o
33 lib-$(CONFIG_MAN) += man.o
34 lib-$(CONFIG_MICROCOM) += microcom.o
36 +++ b/miscutils/lock.c
39 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
41 + * This is free software, licensed under the GNU General Public License v2.
43 +#include <sys/types.h>
44 +#include <sys/file.h>
45 +#include <sys/stat.h>
52 +//usage:#define lock_trivial_usage NOUSAGE_STR
53 +//usage:#define lock_full_usage ""
55 +static int unlock = 0;
56 +static int shared = 0;
57 +static int waitonly = 0;
61 +static void usage(char *name)
63 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
64 + " -s Use shared locking\n"
66 + " -w Wait for the lock to become free, don't acquire lock\n"
71 +static void exit_unlock(int sig)
77 +static int do_unlock(void)
82 + if ((f = fopen(file, "r")) == NULL)
85 + fscanf(f, "%d", &i);
94 +static int do_lock(void)
99 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
100 + if ((fd = open(file, O_RDWR)) < 0) {
101 + fprintf(stderr, "Can't open %s\n", file);
106 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
107 + fprintf(stderr, "Can't lock %s\n", file);
117 + signal(SIGKILL, exit_unlock);
118 + signal(SIGTERM, exit_unlock);
119 + signal(SIGINT, exit_unlock);
127 + lseek(fd, 0, SEEK_SET);
129 + sprintf(pidstr, "%d\n", pid);
130 + write(fd, pidstr, strlen(pidstr));
139 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
140 +int lock_main(int argc, char **argv)
142 + char **args = &argv[1];
145 + while ((*args != NULL) && (*args)[0] == '-') {
147 + while (*(++ch) > 0) {
169 + return do_unlock();