05cf7e09a36f93d9bb4533bb5e170ba521ac799c
[openwrt/openwrt.git] / package / busybox / patches / 340-lock_util.patch
1 --- a/include/applets.h
2 +++ b/include/applets.h
3 @@ -234,6 +234,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))
11 --- a/include/usage.h
12 +++ b/include/usage.h
13 @@ -2357,6 +2357,9 @@
14 #define loadkmap_example_usage \
15 "$ loadkmap < /etc/i18n/lang-keymap\n"
16
17 +#define lock_trivial_usage NOUSAGE_STR
18 +#define lock_full_usage ""
19 +
20 #define logger_trivial_usage \
21 "[OPTIONS] [MESSAGE]"
22 #define logger_full_usage "\n\n" \
23 --- a/miscutils/Config.in
24 +++ b/miscutils/Config.in
25 @@ -463,6 +463,11 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
26 help
27 Enables the 'hdparm -d' option to get/set using_dma flag.
28
29 +config LOCK
30 + bool "lock"
31 + help
32 + Small utility for using locks in scripts
33 +
34 config MAKEDEVS
35 bool "makedevs"
36 default n
37 --- a/miscutils/Kbuild
38 +++ b/miscutils/Kbuild
39 @@ -27,6 +27,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
47 --- /dev/null
48 +++ b/miscutils/lock.c
49 @@ -0,0 +1,132 @@
50 +/*
51 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
52 + *
53 + * This is free software, licensed under the GNU General Public License v2.
54 + */
55 +#include <sys/types.h>
56 +#include <sys/file.h>
57 +#include <sys/stat.h>
58 +#include <signal.h>
59 +#include <fcntl.h>
60 +#include <unistd.h>
61 +#include <stdio.h>
62 +#include "busybox.h"
63 +
64 +static int unlock = 0;
65 +static int shared = 0;
66 +static int waitonly = 0;
67 +static int fd;
68 +static char *file;
69 +
70 +static void usage(char *name)
71 +{
72 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
73 + " -s Use shared locking\n"
74 + " -u Unlock\n"
75 + " -w Wait for the lock to become free, don't acquire lock\n"
76 + "\n", name);
77 + exit(1);
78 +}
79 +
80 +static void exit_unlock(int sig)
81 +{
82 + flock(fd, LOCK_UN);
83 + exit(0);
84 +}
85 +
86 +static int do_unlock(void)
87 +{
88 + FILE *f;
89 + int i;
90 +
91 + if ((f = fopen(file, "r")) == NULL)
92 + return 0;
93 +
94 + fscanf(f, "%d", &i);
95 + if (i > 0)
96 + kill(i, SIGTERM);
97 +
98 + fclose(f);
99 +
100 + return 0;
101 +}
102 +
103 +static int do_lock(void)
104 +{
105 + int pid;
106 + char pidstr[8];
107 +
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);
111 + return 1;
112 + }
113 + }
114 +
115 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
116 + fprintf(stderr, "Can't lock %s\n", file);
117 + return 1;
118 + }
119 +
120 + pid = fork();
121 +
122 + if (pid < 0)
123 + return -1;
124 +
125 + if (pid == 0) {
126 + signal(SIGKILL, exit_unlock);
127 + signal(SIGTERM, exit_unlock);
128 + signal(SIGINT, exit_unlock);
129 + if (waitonly)
130 + exit_unlock(0);
131 + else
132 + while (1)
133 + sleep(1);
134 + } else {
135 + if (!waitonly) {
136 + lseek(fd, 0, SEEK_SET);
137 + ftruncate(fd, 0);
138 + sprintf(pidstr, "%d\n", pid);
139 + write(fd, pidstr, strlen(pidstr));
140 + close(fd);
141 + }
142 +
143 + return 0;
144 + }
145 + return 0;
146 +}
147 +
148 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
149 +int lock_main(int argc, char **argv)
150 +{
151 + char **args = &argv[1];
152 + int c = argc - 1;
153 +
154 + while ((*args != NULL) && (*args)[0] == '-') {
155 + char *ch = *args;
156 + while (*(++ch) > 0) {
157 + switch(*ch) {
158 + case 'w':
159 + waitonly = 1;
160 + break;
161 + case 's':
162 + shared = 1;
163 + break;
164 + case 'u':
165 + unlock = 1;
166 + break;
167 + }
168 + }
169 + c--;
170 + args++;
171 + }
172 +
173 + if (c != 1)
174 + usage(argv[0]);
175 +
176 + file = *args;
177 + if (unlock)
178 + return do_unlock();
179 + else
180 + return do_lock();
181 +}