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