busybox: update to version 1.25.0
[openwrt/staging/chunkeey.git] / package / utils / busybox / patches / 220-add_lock_util.patch
1 --- a/include/applets.src.h
2 +++ b/include/applets.src.h
3 @@ -196,6 +196,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_LOGNAME(APPLET_NOFORK(logname, logname, BB_DIR_USR_BIN, BB_SUID_DROP, logname))
9 IF_LOSETUP(APPLET(losetup, BB_DIR_SBIN, BB_SUID_DROP))
10 IF_LS(APPLET_NOEXEC(ls, ls, BB_DIR_BIN, BB_SUID_DROP, ls))
11 --- a/miscutils/Config.src
12 +++ b/miscutils/Config.src
13 @@ -375,6 +375,12 @@ 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 + default n
20 + help
21 + Small utility for using locks in scripts
22 +
23 config MAKEDEVS
24 bool "makedevs"
25 default y
26 --- a/miscutils/Kbuild.src
27 +++ b/miscutils/Kbuild.src
28 @@ -33,6 +33,7 @@ lib-$(CONFIG_LAST) += last.o
29 endif
30
31 lib-$(CONFIG_LESS) += less.o
32 +lib-$(CONFIG_LOCK) += lock.o
33 lib-$(CONFIG_MAKEDEVS) += makedevs.o
34 lib-$(CONFIG_MAN) += man.o
35 lib-$(CONFIG_MICROCOM) += microcom.o
36 --- /dev/null
37 +++ b/miscutils/lock.c
38 @@ -0,0 +1,144 @@
39 +/*
40 + * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
41 + *
42 + * This is free software, licensed under the GNU General Public License v2.
43 + */
44 +#include <sys/types.h>
45 +#include <sys/file.h>
46 +#include <sys/stat.h>
47 +#include <signal.h>
48 +#include <fcntl.h>
49 +#include <unistd.h>
50 +#include <stdio.h>
51 +#include "busybox.h"
52 +
53 +//usage:#define lock_trivial_usage NOUSAGE_STR
54 +//usage:#define lock_full_usage ""
55 +
56 +static int unlock = 0;
57 +static int shared = 0;
58 +static int waitonly = 0;
59 +static int try_lock = 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 Don't wait for the lock to become free. Fail with exit code\n"
70 + "\n", name);
71 + exit(1);
72 +}
73 +
74 +static void exit_unlock(int sig)
75 +{
76 + flock(fd, LOCK_UN);
77 + exit(0);
78 +}
79 +
80 +static int do_unlock(void)
81 +{
82 + FILE *f;
83 + int i;
84 +
85 + if ((f = fopen(file, "r")) == NULL)
86 + return 0;
87 +
88 + fscanf(f, "%d", &i);
89 + if (i > 0)
90 + kill(i, SIGTERM);
91 +
92 + fclose(f);
93 +
94 + return 0;
95 +}
96 +
97 +static int do_lock(void)
98 +{
99 + int pid;
100 + int flags;
101 + char pidstr[8];
102 +
103 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
104 + if ((fd = open(file, O_RDWR)) < 0) {
105 + fprintf(stderr, "Can't open %s\n", file);
106 + return 1;
107 + }
108 + }
109 +
110 + flags = shared ? LOCK_SH : LOCK_EX;
111 + flags |= try_lock ? LOCK_NB : 0;
112 +
113 + if (flock(fd, flags) < 0) {
114 + fprintf(stderr, "Can't lock %s\n", file);
115 + return 1;
116 + }
117 +
118 + pid = fork();
119 +
120 + if (pid < 0)
121 + return -1;
122 +
123 + if (pid == 0) {
124 + signal(SIGKILL, exit_unlock);
125 + signal(SIGTERM, exit_unlock);
126 + signal(SIGINT, exit_unlock);
127 + if (waitonly)
128 + exit_unlock(0);
129 + else
130 + while (1)
131 + sleep(1);
132 + } else {
133 + if (!waitonly) {
134 + lseek(fd, 0, SEEK_SET);
135 + ftruncate(fd, 0);
136 + sprintf(pidstr, "%d\n", pid);
137 + write(fd, pidstr, strlen(pidstr));
138 + close(fd);
139 + }
140 +
141 + return 0;
142 + }
143 + return 0;
144 +}
145 +
146 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
147 +int lock_main(int argc, char **argv)
148 +{
149 + char **args = &argv[1];
150 + int c = argc - 1;
151 +
152 + while ((*args != NULL) && (*args)[0] == '-') {
153 + char *ch = *args;
154 + while (*(++ch) > 0) {
155 + switch(*ch) {
156 + case 'w':
157 + waitonly = 1;
158 + break;
159 + case 's':
160 + shared = 1;
161 + break;
162 + case 'u':
163 + unlock = 1;
164 + break;
165 + case 'n':
166 + try_lock = 1;
167 + break;
168 + }
169 + }
170 + c--;
171 + args++;
172 + }
173 +
174 + if (c != 1)
175 + usage(argv[0]);
176 +
177 + file = *args;
178 + if (unlock)
179 + return do_unlock();
180 + else
181 + return do_lock();
182 +}