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