refresh busybox patches
[openwrt/svn-archive/archive.git] / package / busybox / patches / 340-lock_util.patch
1 Index: busybox-1.7.2/include/applets.h
2 ===================================================================
3 --- busybox-1.7.2.orig/include/applets.h 2007-10-30 15:35:03.000000000 -0500
4 +++ busybox-1.7.2/include/applets.h 2007-10-30 15:35:03.000000000 -0500
5 @@ -209,6 +209,7 @@
6 USE_LOAD_POLICY(APPLET(load_policy, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
7 USE_LOADFONT(APPLET(loadfont, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
8 USE_LOADKMAP(APPLET(loadkmap, _BB_DIR_SBIN, _BB_SUID_NEVER))
9 +USE_LOCK(APPLET_NOUSAGE(lock, lock, _BB_DIR_BIN, _BB_SUID_NEVER))
10 USE_LOGGER(APPLET(logger, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
11 USE_LOGIN(APPLET(login, _BB_DIR_BIN, _BB_SUID_ALWAYS))
12 USE_LOGNAME(APPLET_NOFORK(logname, logname, _BB_DIR_USR_BIN, _BB_SUID_NEVER, logname))
13 Index: busybox-1.7.2/miscutils/Config.in
14 ===================================================================
15 --- busybox-1.7.2.orig/miscutils/Config.in 2007-10-30 15:34:59.000000000 -0500
16 +++ busybox-1.7.2/miscutils/Config.in 2007-10-30 15:35:03.000000000 -0500
17 @@ -244,6 +244,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 LOCK
22 + bool "lock"
23 + default y
24 + help
25 + Small utility for using locks in scripts
26 +
27 config MAKEDEVS
28 bool "makedevs"
29 default n
30 Index: busybox-1.7.2/miscutils/Kbuild
31 ===================================================================
32 --- busybox-1.7.2.orig/miscutils/Kbuild 2007-10-30 15:34:59.000000000 -0500
33 +++ busybox-1.7.2/miscutils/Kbuild 2007-10-30 15:35:03.000000000 -0500
34 @@ -16,6 +16,7 @@
35 lib-$(CONFIG_HDPARM) += hdparm.o
36 lib-$(CONFIG_LAST) += last.o
37 lib-$(CONFIG_LESS) += less.o
38 +lib-$(CONFIG_LOCK) += lock.o
39 lib-$(CONFIG_MAKEDEVS) += makedevs.o
40 lib-$(CONFIG_MOUNTPOINT) += mountpoint.o
41 lib-$(CONFIG_MT) += mt.o
42 Index: busybox-1.7.2/miscutils/lock.c
43 ===================================================================
44 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
45 +++ busybox-1.7.2/miscutils/lock.c 2007-10-30 15:35:03.000000000 -0500
46 @@ -0,0 +1,135 @@
47 +/*
48 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
49 + *
50 + * This is free software, licensed under the GNU General Public License v2.
51 + */
52 +#include <sys/types.h>
53 +#include <sys/file.h>
54 +#include <sys/stat.h>
55 +#include <signal.h>
56 +#include <fcntl.h>
57 +#include <unistd.h>
58 +#include <stdio.h>
59 +#include "busybox.h"
60 +
61 +static int unlock = 0;
62 +static int shared = 0;
63 +static int waitonly = 0;
64 +static int fd;
65 +static char *file;
66 +
67 +static void usage(char *name)
68 +{
69 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
70 + " -s Use shared locking\n"
71 + " -u Unlock\n"
72 + " -w Wait for the lock to become free, don't acquire lock\n"
73 + "\n", name);
74 + exit(1);
75 +}
76 +
77 +static void exit_unlock(int sig)
78 +{
79 + flock(fd, LOCK_UN);
80 + exit(0);
81 +}
82 +
83 +static int do_unlock(void)
84 +{
85 + FILE *f;
86 + int i;
87 +
88 + if ((f = fopen(file, "r")) == NULL)
89 + return 0;
90 +
91 + fscanf(f, "%d", &i);
92 + if (i > 0)
93 + kill(i, SIGTERM);
94 +
95 + fclose(f);
96 +
97 + return 0;
98 +}
99 +
100 +static int do_lock(void)
101 +{
102 + int pid;
103 + char pidstr[8];
104 +
105 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
106 + if ((fd = open(file, O_RDWR)) < 0) {
107 + fprintf(stderr, "Can't open %s\n", file);
108 + return 1;
109 + }
110 + }
111 +
112 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
113 + fprintf(stderr, "Can't lock %s\n", file);
114 + return 1;
115 + }
116 +
117 + pid = fork();
118 +
119 + if (pid < 0)
120 + return -1;
121 +
122 + if (pid == 0) {
123 + signal(SIGKILL, exit_unlock);
124 + signal(SIGTERM, exit_unlock);
125 + signal(SIGINT, exit_unlock);
126 + if (waitonly)
127 + exit_unlock(0);
128 + else
129 + while (1)
130 + sleep(1);
131 + } else {
132 + if (!waitonly) {
133 + lseek(fd, 0, SEEK_SET);
134 + ftruncate(fd, 0);
135 + sprintf(pidstr, "%d\n", pid);
136 + write(fd, pidstr, strlen(pidstr));
137 + close(fd);
138 + }
139 +
140 + return 0;
141 + }
142 + return 0;
143 +}
144 +
145 +#ifndef CONFIG_LOCK
146 +int main(int argc, char **argv)
147 +#else
148 +int lock_main(int argc, char **argv)
149 +#endif
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 +}