hostapd: update to latest git version
[openwrt.git] / package / busybox / patches / 340-lock_util.patch
1 --- a/include/applets.src.h
2 +++ b/include/applets.src.h
3 @@ -218,6 +218,7 @@ IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN,
4 IF_LOAD_POLICY(APPLET(load_policy, BB_DIR_USR_SBIN, BB_SUID_DROP))
5 IF_LOADFONT(APPLET(loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP))
6 IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP))
7 +IF_LOCK(APPLET(lock, BB_DIR_BIN, BB_SUID_DROP))
8 IF_LOGGER(APPLET(logger, BB_DIR_USR_BIN, BB_SUID_DROP))
9 /* Needs to be run by root or be suid root - needs to change uid and gid: */
10 IF_LOGIN(APPLET(login, BB_DIR_BIN, BB_SUID_REQUIRE))
11 --- a/miscutils/Config.src
12 +++ b/miscutils/Config.src
13 @@ -419,6 +419,11 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
14 help
15 Enables the 'hdparm -d' option to get/set using_dma flag.
16
17 +config LOCK
18 + bool "lock"
19 + help
20 + Small utility for using locks in scripts
21 +
22 config MAKEDEVS
23 bool "makedevs"
24 default y
25 --- a/miscutils/Kbuild.src
26 +++ b/miscutils/Kbuild.src
27 @@ -29,6 +29,7 @@ lib-$(CONFIG_INOTIFYD) += inotifyd.o
28 lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
29 lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
30 lib-$(CONFIG_LESS) += less.o
31 +lib-$(CONFIG_LOCK) += lock.o
32 lib-$(CONFIG_MAKEDEVS) += makedevs.o
33 lib-$(CONFIG_MAN) += man.o
34 lib-$(CONFIG_MICROCOM) += microcom.o
35 --- /dev/null
36 +++ b/miscutils/lock.c
37 @@ -0,0 +1,135 @@
38 +/*
39 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
40 + *
41 + * This is free software, licensed under the GNU General Public License v2.
42 + */
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 +//usage:#define lock_trivial_usage NOUSAGE_STR
53 +//usage:#define lock_full_usage ""
54 +
55 +static int unlock = 0;
56 +static int shared = 0;
57 +static int waitonly = 0;
58 +static int fd;
59 +static char *file;
60 +
61 +static void usage(char *name)
62 +{
63 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
64 + " -s Use shared locking\n"
65 + " -u Unlock\n"
66 + " -w Wait for the lock to become free, don't acquire lock\n"
67 + "\n", name);
68 + exit(1);
69 +}
70 +
71 +static void exit_unlock(int sig)
72 +{
73 + flock(fd, LOCK_UN);
74 + exit(0);
75 +}
76 +
77 +static int do_unlock(void)
78 +{
79 + FILE *f;
80 + int i;
81 +
82 + if ((f = fopen(file, "r")) == NULL)
83 + return 0;
84 +
85 + fscanf(f, "%d", &i);
86 + if (i > 0)
87 + kill(i, SIGTERM);
88 +
89 + fclose(f);
90 +
91 + return 0;
92 +}
93 +
94 +static int do_lock(void)
95 +{
96 + int pid;
97 + char pidstr[8];
98 +
99 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
100 + if ((fd = open(file, O_RDWR)) < 0) {
101 + fprintf(stderr, "Can't open %s\n", file);
102 + return 1;
103 + }
104 + }
105 +
106 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
107 + fprintf(stderr, "Can't lock %s\n", file);
108 + return 1;
109 + }
110 +
111 + pid = fork();
112 +
113 + if (pid < 0)
114 + return -1;
115 +
116 + if (pid == 0) {
117 + signal(SIGKILL, exit_unlock);
118 + signal(SIGTERM, exit_unlock);
119 + signal(SIGINT, exit_unlock);
120 + if (waitonly)
121 + exit_unlock(0);
122 + else
123 + while (1)
124 + sleep(1);
125 + } else {
126 + if (!waitonly) {
127 + lseek(fd, 0, SEEK_SET);
128 + ftruncate(fd, 0);
129 + sprintf(pidstr, "%d\n", pid);
130 + write(fd, pidstr, strlen(pidstr));
131 + close(fd);
132 + }
133 +
134 + return 0;
135 + }
136 + return 0;
137 +}
138 +
139 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
140 +int lock_main(int argc, char **argv)
141 +{
142 + char **args = &argv[1];
143 + int c = argc - 1;
144 +
145 + while ((*args != NULL) && (*args)[0] == '-') {
146 + char *ch = *args;
147 + while (*(++ch) > 0) {
148 + switch(*ch) {
149 + case 'w':
150 + waitonly = 1;
151 + break;
152 + case 's':
153 + shared = 1;
154 + break;
155 + case 'u':
156 + unlock = 1;
157 + break;
158 + }
159 + }
160 + c--;
161 + args++;
162 + }
163 +
164 + if (c != 1)
165 + usage(argv[0]);
166 +
167 + file = *args;
168 + if (unlock)
169 + return do_unlock();
170 + else
171 + return do_lock();
172 +}
This page took 0.043963 seconds and 5 git commands to generate.