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