update busybox to 1.8.2
[openwrt.git] / package / busybox / patches / 340-lock_util.patch
1 Index: busybox-1.8.1/include/applets.h
2 ===================================================================
3 --- busybox-1.8.1.orig/include/applets.h 2007-11-10 16:54:28.318054115 +0100
4 +++ busybox-1.8.1/include/applets.h 2007-11-10 17:39:21.487529096 +0100
5 @@ -218,6 +218,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.8.1/miscutils/Config.in
14 ===================================================================
15 --- busybox-1.8.1.orig/miscutils/Config.in 2007-11-10 16:54:16.477379354 +0100
16 +++ busybox-1.8.1/miscutils/Config.in 2007-11-10 16:54:28.366056851 +0100
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.8.1/miscutils/Kbuild
31 ===================================================================
32 --- busybox-1.8.1.orig/miscutils/Kbuild 2007-11-10 16:54:16.481379580 +0100
33 +++ busybox-1.8.1/miscutils/Kbuild 2007-11-10 16:54:28.370057076 +0100
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_MICROCOM) += microcom.o
41 lib-$(CONFIG_MOUNTPOINT) += mountpoint.o
42 Index: busybox-1.8.1/miscutils/lock.c
43 ===================================================================
44 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
45 +++ busybox-1.8.1/miscutils/lock.c 2007-11-10 17:40:37.203843924 +0100
46 @@ -0,0 +1,132 @@
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 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
146 +int lock_main(int argc, char **argv)
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 +}
This page took 0.076663 seconds and 5 git commands to generate.