1 --- a/include/applets.h
2 +++ b/include/applets.h
3 @@ -223,6 +223,7 @@ USE_LN(APPLET_NOEXEC(ln, ln, _BB_DIR_BIN
4 USE_LOAD_POLICY(APPLET(load_policy, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
5 USE_LOADFONT(APPLET(loadfont, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
6 USE_LOADKMAP(APPLET(loadkmap, _BB_DIR_SBIN, _BB_SUID_NEVER))
7 +USE_LOCK(APPLET(lock, _BB_DIR_BIN, _BB_SUID_NEVER))
8 USE_LOGGER(APPLET(logger, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
9 USE_LOGIN(APPLET(login, _BB_DIR_BIN, _BB_SUID_ALWAYS))
10 USE_LOGNAME(APPLET_NOFORK(logname, logname, _BB_DIR_USR_BIN, _BB_SUID_NEVER, 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 \
21 "[OPTION]... [MESSAGE]"
22 #define logger_full_usage "\n\n" \
23 --- a/miscutils/Config.in
24 +++ b/miscutils/Config.in
25 @@ -395,6 +395,12 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
26 Enables the 'hdparm -d' option to get/set using_dma flag.
27 This is dangerous stuff, so you should probably say N.
33 + Small utility for using locks in scripts
38 --- a/miscutils/Kbuild
39 +++ b/miscutils/Kbuild
40 @@ -21,6 +21,7 @@ lib-$(CONFIG_INOTIFYD) += inotifyd.o
41 lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
42 lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
43 lib-$(CONFIG_LESS) += less.o
44 +lib-$(CONFIG_LOCK) += lock.o
45 lib-$(CONFIG_MAKEDEVS) += makedevs.o
46 lib-$(CONFIG_MAN) += man.o
47 lib-$(CONFIG_MICROCOM) += microcom.o
49 +++ b/miscutils/lock.c
52 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
54 + * This is free software, licensed under the GNU General Public License v2.
56 +#include <sys/types.h>
57 +#include <sys/file.h>
58 +#include <sys/stat.h>
65 +static int unlock = 0;
66 +static int shared = 0;
67 +static int waitonly = 0;
71 +static void usage(char *name)
73 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
74 + " -s Use shared locking\n"
76 + " -w Wait for the lock to become free, don't acquire lock\n"
81 +static void exit_unlock(int sig)
87 +static int do_unlock(void)
92 + if ((f = fopen(file, "r")) == NULL)
95 + fscanf(f, "%d", &i);
104 +static int do_lock(void)
109 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
110 + if ((fd = open(file, O_RDWR)) < 0) {
111 + fprintf(stderr, "Can't open %s\n", file);
116 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
117 + fprintf(stderr, "Can't lock %s\n", file);
127 + signal(SIGKILL, exit_unlock);
128 + signal(SIGTERM, exit_unlock);
129 + signal(SIGINT, exit_unlock);
137 + lseek(fd, 0, SEEK_SET);
139 + sprintf(pidstr, "%d\n", pid);
140 + write(fd, pidstr, strlen(pidstr));
149 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
150 +int lock_main(int argc, char **argv)
152 + char **args = &argv[1];
155 + while ((*args != NULL) && (*args)[0] == '-') {
157 + while (*(++ch) > 0) {
179 + return do_unlock();