add lock utility to busybox
[openwrt/svn-archive/archive.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,125 @@
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 + f = fopen(file, "r");
83 + fscanf(f, "%d", &i);
84 + if (i > 0)
85 + kill(i, SIGTERM);
86 + fclose(f);
87 +
88 + return 0;
89 +}
90 +
91 +static int do_lock(void)
92 +{
93 + int pid;
94 + char pidstr[8];
95 +
96 + if ((fd = open(file, O_RDWR | O_CREAT, 0700)) < 0) {
97 + fprintf(stderr, "Can't open %s\n", file);
98 + return 1;
99 + }
100 +
101 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
102 + fprintf(stderr, "Can't lock %s\n", file);
103 + return 1;
104 + }
105 +
106 + pid = fork();
107 +
108 + if (pid < 0)
109 + return -1;
110 +
111 + if (pid == 0) {
112 + signal(SIGKILL, exit_unlock);
113 + signal(SIGTERM, exit_unlock);
114 + signal(SIGINT, exit_unlock);
115 + if (waitonly)
116 + exit_unlock(0);
117 + else
118 + while (1)
119 + sleep(1);
120 + } else {
121 + if (!waitonly) {
122 + lseek(fd, 0, SEEK_SET);
123 + ftruncate(fd, 0);
124 + sprintf(pidstr, "%d\n", pid);
125 + write(fd, pidstr, strlen(pidstr));
126 + close(fd);
127 + }
128 +
129 + return 0;
130 + }
131 +}
132 +
133 +#ifndef CONFIG_LOCK
134 +int main(int argc, char **argv)
135 +#else
136 +int lock_main(int argc, char **argv)
137 +#endif
138 +{
139 + char **args = &argv[1];
140 + int c = argc - 1;
141 +
142 + while ((*args != NULL) && (*args)[0] == '-') {
143 + char *ch = *args;
144 + while (*(++ch) > 0) {
145 + switch(*ch) {
146 + case 'w':
147 + waitonly = 1;
148 + break;
149 + case 's':
150 + shared = 1;
151 + break;
152 + case 'u':
153 + unlock = 1;
154 + break;
155 + }
156 + }
157 + c--;
158 + args++;
159 + }
160 +
161 + if (c != 1)
162 + usage(argv[0]);
163 +
164 + file = *args;
165 + if (unlock)
166 + return do_unlock();
167 + else
168 + return do_lock();
169 +}