fix nozomi unpack command
[openwrt.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,135 @@
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 + exit(0);
77 +}
78 +
79 +static int do_unlock(void)
80 +{
81 + FILE *f;
82 + int i;
83 +
84 + if ((f = fopen(file, "r")) == NULL)
85 + return 0;
86 +
87 + fscanf(f, "%d", &i);
88 + if (i > 0)
89 + kill(i, SIGTERM);
90 +
91 + fclose(f);
92 +
93 + return 0;
94 +}
95 +
96 +static int do_lock(void)
97 +{
98 + int pid;
99 + char pidstr[8];
100 +
101 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
102 + if ((fd = open(file, O_RDWR)) < 0) {
103 + fprintf(stderr, "Can't open %s\n", file);
104 + return 1;
105 + }
106 + }
107 +
108 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
109 + fprintf(stderr, "Can't lock %s\n", file);
110 + return 1;
111 + }
112 +
113 + pid = fork();
114 +
115 + if (pid < 0)
116 + return -1;
117 +
118 + if (pid == 0) {
119 + signal(SIGKILL, exit_unlock);
120 + signal(SIGTERM, exit_unlock);
121 + signal(SIGINT, exit_unlock);
122 + if (waitonly)
123 + exit_unlock(0);
124 + else
125 + while (1)
126 + sleep(1);
127 + } else {
128 + if (!waitonly) {
129 + lseek(fd, 0, SEEK_SET);
130 + ftruncate(fd, 0);
131 + sprintf(pidstr, "%d\n", pid);
132 + write(fd, pidstr, strlen(pidstr));
133 + close(fd);
134 + }
135 +
136 + return 0;
137 + }
138 + return 0;
139 +}
140 +
141 +#ifndef CONFIG_LOCK
142 +int main(int argc, char **argv)
143 +#else
144 +int lock_main(int argc, char **argv)
145 +#endif
146 +{
147 + char **args = &argv[1];
148 + int c = argc - 1;
149 +
150 + while ((*args != NULL) && (*args)[0] == '-') {
151 + char *ch = *args;
152 + while (*(++ch) > 0) {
153 + switch(*ch) {
154 + case 'w':
155 + waitonly = 1;
156 + break;
157 + case 's':
158 + shared = 1;
159 + break;
160 + case 'u':
161 + unlock = 1;
162 + break;
163 + }
164 + }
165 + c--;
166 + args++;
167 + }
168 +
169 + if (c != 1)
170 + usage(argv[0]);
171 +
172 + file = *args;
173 + if (unlock)
174 + return do_unlock();
175 + else
176 + return do_lock();
177 +}
This page took 0.04814 seconds and 5 git commands to generate.