1 --- a/include/applets.h
2 +++ b/include/applets.h
3 @@ -233,6 +233,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 IF_LOGIN(APPLET(login, _BB_DIR_BIN, _BB_SUID_REQUIRE))
10 IF_LOGNAME(APPLET_NOFORK(logname, logname, _BB_DIR_USR_BIN, _BB_SUID_DROP, logname))
14 #define loadkmap_example_usage \
15 "$ loadkmap < /etc/i18n/lang-keymap\n"
17 +#define lock_trivial_usage NOUSAGE_STR
18 +#define lock_full_usage ""
20 #define logger_trivial_usage \
22 #define logger_full_usage "\n\n" \
23 --- a/miscutils/Config.in
24 +++ b/miscutils/Config.in
25 @@ -456,6 +456,11 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
27 Enables the 'hdparm -d' option to get/set using_dma flag.
32 + Small utility for using locks in scripts
37 --- a/miscutils/Kbuild
38 +++ b/miscutils/Kbuild
39 @@ -26,6 +26,7 @@ lib-$(CONFIG_INOTIFYD) += inotifyd.o
40 lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
41 lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
42 lib-$(CONFIG_LESS) += less.o
43 +lib-$(CONFIG_LOCK) += lock.o
44 lib-$(CONFIG_MAKEDEVS) += makedevs.o
45 lib-$(CONFIG_MAN) += man.o
46 lib-$(CONFIG_MICROCOM) += microcom.o
48 +++ b/miscutils/lock.c
51 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
53 + * This is free software, licensed under the GNU General Public License v2.
55 +#include <sys/types.h>
56 +#include <sys/file.h>
57 +#include <sys/stat.h>
64 +static int unlock = 0;
65 +static int shared = 0;
66 +static int waitonly = 0;
70 +static void usage(char *name)
72 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
73 + " -s Use shared locking\n"
75 + " -w Wait for the lock to become free, don't acquire lock\n"
80 +static void exit_unlock(int sig)
86 +static int do_unlock(void)
91 + if ((f = fopen(file, "r")) == NULL)
94 + fscanf(f, "%d", &i);
103 +static int do_lock(void)
108 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
109 + if ((fd = open(file, O_RDWR)) < 0) {
110 + fprintf(stderr, "Can't open %s\n", file);
115 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
116 + fprintf(stderr, "Can't lock %s\n", file);
126 + signal(SIGKILL, exit_unlock);
127 + signal(SIGTERM, exit_unlock);
128 + signal(SIGINT, exit_unlock);
136 + lseek(fd, 0, SEEK_SET);
138 + sprintf(pidstr, "%d\n", pid);
139 + write(fd, pidstr, strlen(pidstr));
148 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
149 +int lock_main(int argc, char **argv)
151 + char **args = &argv[1];
154 + while ((*args != NULL) && (*args)[0] == '-') {
156 + while (*(++ch) > 0) {
178 + return do_unlock();