finally move buildroot-ng to trunk
[openwrt/svn-archive/archive.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,133 @@
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, 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 +}
170 diff -ruN busybox-1.2.0-old/miscutils/Makefile.in busybox-1.2.0-new/miscutils/Makefile.in
171 --- busybox-1.2.0-old/miscutils/Makefile.in 2006-07-01 00:42:09.000000000 +0200
172 +++ busybox-1.2.0-new/miscutils/Makefile.in 2006-08-01 10:21:15.000000000 +0200
173 @@ -20,6 +20,7 @@
174 MISCUTILS-$(CONFIG_EJECT) += eject.o
175 MISCUTILS-$(CONFIG_HDPARM) += hdparm.o
176 MISCUTILS-$(CONFIG_LAST) += last.o
177 +MISCUTILS-$(CONFIG_LOCK) += lock.o
178 MISCUTILS-${CONFIG_LESS} += less.o
179 MISCUTILS-$(CONFIG_MAKEDEVS) += makedevs.o
180 MISCUTILS-$(CONFIG_MOUNTPOINT) += mountpoint.o