reduce the possibility of race conditions when using the lock utility
[openwrt/svn-archive/openwrt.git] / openwrt / package / busybox / patches / 340-lock_util.patch
1 diff -urN busybox.old/include/applets.h busybox.dev/include/applets.h
2 --- busybox.old/include/applets.h 2006-03-14 09:46:09.000000000 +0100
3 +++ busybox.dev/include/applets.h 2006-03-14 09:49:17.000000000 +0100
4 @@ -340,6 +340,9 @@
5 #ifdef CONFIG_LOADKMAP
6 APPLET(loadkmap, loadkmap_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
7 #endif
8 +#ifdef CONFIG_LOCK
9 + APPLET_NOUSAGE("lock", lock_main, _BB_DIR_BIN, _BB_SUID_NEVER)
10 +#endif
11 #ifdef CONFIG_LOGGER
12 APPLET(logger, logger_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
13 #endif
14 diff -urN busybox.old/miscutils/Config.in busybox.dev/miscutils/Config.in
15 --- busybox.old/miscutils/Config.in 2004-08-27 01:12:59.000000000 +0200
16 +++ busybox.dev/miscutils/Config.in 2006-03-14 09:52:13.000000000 +0100
17 @@ -148,6 +148,12 @@
18 Enables the 'hdparm -d' option to get/set using_dma flag.
19 This is dangerous stuff, so you should probably say N.
20
21 +config CONFIG_LOCK
22 + bool "lock"
23 + default y
24 + help
25 + Small utility for using locks in scripts
26 +
27 config CONFIG_MAKEDEVS
28 bool "makedevs"
29 default n
30 diff -urN busybox.old/miscutils/Makefile.in busybox.dev/miscutils/Makefile.in
31 --- busybox.old/miscutils/Makefile.in 2004-10-08 09:45:39.000000000 +0200
32 +++ busybox.dev/miscutils/Makefile.in 2006-03-14 09:52:41.000000000 +0100
33 @@ -31,6 +31,7 @@
34 MISCUTILS-$(CONFIG_DEVFSD) += devfsd.o
35 MISCUTILS-$(CONFIG_HDPARM) += hdparm.o
36 MISCUTILS-$(CONFIG_LAST) += last.o
37 +MISCUTILS-$(CONFIG_LOCK) += lock.o
38 MISCUTILS-$(CONFIG_MAKEDEVS) += makedevs.o
39 MISCUTILS-$(CONFIG_MT) += mt.o
40 MISCUTILS-$(CONFIG_RX) += rx.o
41 diff -urN busybox.old/miscutils/lock.c busybox.dev/miscutils/lock.c
42 --- busybox.old/miscutils/lock.c 1970-01-01 01:00:00.000000000 +0100
43 +++ busybox.dev/miscutils/lock.c 2006-03-14 09:50:40.000000000 +0100
44 @@ -0,0 +1,130 @@
45 +#include <sys/types.h>
46 +#include <sys/file.h>
47 +#include <sys/stat.h>
48 +#include <signal.h>
49 +#include <fcntl.h>
50 +#include <unistd.h>
51 +#include <stdio.h>
52 +#include "busybox.h"
53 +
54 +static int unlock = 0;
55 +static int shared = 0;
56 +static int waitonly = 0;
57 +static int fd;
58 +static char *file;
59 +
60 +static void usage(char *name)
61 +{
62 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
63 + " -s Use shared locking\n"
64 + " -u Unlock\n"
65 + " -w Wait for the lock to become free, don't acquire lock\n"
66 + "\n", name);
67 + exit(1);
68 +}
69 +
70 +static void exit_unlock(int sig)
71 +{
72 + flock(fd, LOCK_UN);
73 + unlink(file);
74 + exit(0);
75 +}
76 +
77 +static int do_unlock(void)
78 +{
79 + FILE *f;
80 + int i;
81 +
82 + if ((f = fopen(file, "r")) == NULL)
83 + return 0;
84 +
85 + fscanf(f, "%d", &i);
86 + if (i > 0)
87 + kill(i, SIGTERM);
88 +
89 + fclose(f);
90 +
91 + return 0;
92 +}
93 +
94 +static int do_lock(void)
95 +{
96 + int pid;
97 + char pidstr[8];
98 +
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);
102 + return 1;
103 + }
104 + }
105 +
106 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
107 + fprintf(stderr, "Can't lock %s\n", file);
108 + return 1;
109 + }
110 +
111 + pid = fork();
112 +
113 + if (pid < 0)
114 + return -1;
115 +
116 + if (pid == 0) {
117 + signal(SIGKILL, exit_unlock);
118 + signal(SIGTERM, exit_unlock);
119 + signal(SIGINT, exit_unlock);
120 + if (waitonly)
121 + exit_unlock(0);
122 + else
123 + while (1)
124 + sleep(1);
125 + } else {
126 + if (!waitonly) {
127 + lseek(fd, 0, SEEK_SET);
128 + ftruncate(fd, 0);
129 + sprintf(pidstr, "%d\n", pid);
130 + write(fd, pidstr, strlen(pidstr));
131 + close(fd);
132 + }
133 +
134 + return 0;
135 + }
136 +}
137 +
138 +#ifndef CONFIG_LOCK
139 +int main(int argc, char **argv)
140 +#else
141 +int lock_main(int argc, char **argv)
142 +#endif
143 +{
144 + char **args = &argv[1];
145 + int c = argc - 1;
146 +
147 + while ((*args != NULL) && (*args)[0] == '-') {
148 + char *ch = *args;
149 + while (*(++ch) > 0) {
150 + switch(*ch) {
151 + case 'w':
152 + waitonly = 1;
153 + break;
154 + case 's':
155 + shared = 1;
156 + break;
157 + case 'u':
158 + unlock = 1;
159 + break;
160 + }
161 + }
162 + c--;
163 + args++;
164 + }
165 +
166 + if (c != 1)
167 + usage(argv[0]);
168 +
169 + file = *args;
170 + if (unlock)
171 + return do_unlock();
172 + else
173 + return do_lock();
174 +}