install static libraries from uclibc to the rootfs staging dir
[openwrt.git] / package / busybox / patches / 340-lock_util.patch
1 --- a/include/applets.h
2 +++ b/include/applets.h
3 @@ -223,6 +223,7 @@ USE_LN(APPLET_NOEXEC(ln, ln, _BB_DIR_BIN
4 USE_LOAD_POLICY(APPLET(load_policy, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
5 USE_LOADFONT(APPLET(loadfont, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
6 USE_LOADKMAP(APPLET(loadkmap, _BB_DIR_SBIN, _BB_SUID_NEVER))
7 +USE_LOCK(APPLET(lock, _BB_DIR_BIN, _BB_SUID_NEVER))
8 USE_LOGGER(APPLET(logger, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
9 USE_LOGIN(APPLET(login, _BB_DIR_BIN, _BB_SUID_ALWAYS))
10 USE_LOGNAME(APPLET_NOFORK(logname, logname, _BB_DIR_USR_BIN, _BB_SUID_NEVER, logname))
11 --- a/include/usage.h
12 +++ b/include/usage.h
13 @@ -2150,6 +2150,9 @@
14 #define loadkmap_example_usage \
15 "$ loadkmap < /etc/i18n/lang-keymap\n"
16
17 +#define lock_trivial_usage NOUSAGE_STR
18 +#define lock_full_usage ""
19 +
20 #define logger_trivial_usage \
21 "[OPTION]... [MESSAGE]"
22 #define logger_full_usage "\n\n" \
23 --- a/miscutils/Config.in
24 +++ b/miscutils/Config.in
25 @@ -395,6 +395,12 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
26 Enables the 'hdparm -d' option to get/set using_dma flag.
27 This is dangerous stuff, so you should probably say N.
28
29 +config LOCK
30 + bool "lock"
31 + default y
32 + help
33 + Small utility for using locks in scripts
34 +
35 config MAKEDEVS
36 bool "makedevs"
37 default n
38 --- a/miscutils/Kbuild
39 +++ b/miscutils/Kbuild
40 @@ -21,6 +21,7 @@ lib-$(CONFIG_INOTIFYD) += inotifyd.o
41 lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
42 lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
43 lib-$(CONFIG_LESS) += less.o
44 +lib-$(CONFIG_LOCK) += lock.o
45 lib-$(CONFIG_MAKEDEVS) += makedevs.o
46 lib-$(CONFIG_MAN) += man.o
47 lib-$(CONFIG_MICROCOM) += microcom.o
48 --- /dev/null
49 +++ b/miscutils/lock.c
50 @@ -0,0 +1,132 @@
51 +/*
52 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
53 + *
54 + * This is free software, licensed under the GNU General Public License v2.
55 + */
56 +#include <sys/types.h>
57 +#include <sys/file.h>
58 +#include <sys/stat.h>
59 +#include <signal.h>
60 +#include <fcntl.h>
61 +#include <unistd.h>
62 +#include <stdio.h>
63 +#include "busybox.h"
64 +
65 +static int unlock = 0;
66 +static int shared = 0;
67 +static int waitonly = 0;
68 +static int fd;
69 +static char *file;
70 +
71 +static void usage(char *name)
72 +{
73 + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
74 + " -s Use shared locking\n"
75 + " -u Unlock\n"
76 + " -w Wait for the lock to become free, don't acquire lock\n"
77 + "\n", name);
78 + exit(1);
79 +}
80 +
81 +static void exit_unlock(int sig)
82 +{
83 + flock(fd, LOCK_UN);
84 + exit(0);
85 +}
86 +
87 +static int do_unlock(void)
88 +{
89 + FILE *f;
90 + int i;
91 +
92 + if ((f = fopen(file, "r")) == NULL)
93 + return 0;
94 +
95 + fscanf(f, "%d", &i);
96 + if (i > 0)
97 + kill(i, SIGTERM);
98 +
99 + fclose(f);
100 +
101 + return 0;
102 +}
103 +
104 +static int do_lock(void)
105 +{
106 + int pid;
107 + char pidstr[8];
108 +
109 + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
110 + if ((fd = open(file, O_RDWR)) < 0) {
111 + fprintf(stderr, "Can't open %s\n", file);
112 + return 1;
113 + }
114 + }
115 +
116 + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
117 + fprintf(stderr, "Can't lock %s\n", file);
118 + return 1;
119 + }
120 +
121 + pid = fork();
122 +
123 + if (pid < 0)
124 + return -1;
125 +
126 + if (pid == 0) {
127 + signal(SIGKILL, exit_unlock);
128 + signal(SIGTERM, exit_unlock);
129 + signal(SIGINT, exit_unlock);
130 + if (waitonly)
131 + exit_unlock(0);
132 + else
133 + while (1)
134 + sleep(1);
135 + } else {
136 + if (!waitonly) {
137 + lseek(fd, 0, SEEK_SET);
138 + ftruncate(fd, 0);
139 + sprintf(pidstr, "%d\n", pid);
140 + write(fd, pidstr, strlen(pidstr));
141 + close(fd);
142 + }
143 +
144 + return 0;
145 + }
146 + return 0;
147 +}
148 +
149 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
150 +int lock_main(int argc, char **argv)
151 +{
152 + char **args = &argv[1];
153 + int c = argc - 1;
154 +
155 + while ((*args != NULL) && (*args)[0] == '-') {
156 + char *ch = *args;
157 + while (*(++ch) > 0) {
158 + switch(*ch) {
159 + case 'w':
160 + waitonly = 1;
161 + break;
162 + case 's':
163 + shared = 1;
164 + break;
165 + case 'u':
166 + unlock = 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 +}
This page took 0.05332 seconds and 5 git commands to generate.