aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimo Teras <timo.teras@iki.fi>2010-03-18 22:45:35 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-03-18 22:45:35 +0100
commit892d4b6b3d7e9950ff3708ca957e1e0590dd5a2d (patch)
tree7b3064dca217fdec81a15e150e7beb0cdc84b9dd
parent8531d76a15890c2c535908ce888b2e2aed35b172 (diff)
downloadbusybox-w32-892d4b6b3d7e9950ff3708ca957e1e0590dd5a2d.tar.gz
busybox-w32-892d4b6b3d7e9950ff3708ca957e1e0590dd5a2d.tar.bz2
busybox-w32-892d4b6b3d7e9950ff3708ca957e1e0590dd5a2d.zip
flock: new applet
function old new delta flock_main - 253 +253 packed_usage 26466 26498 +32 applet_names 2170 2176 +6 applet_main 1280 1284 +4 applet_nameofs 640 642 +2 applet_install_loc 160 161 +1 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 5/0 up/down: 298/0) Total: 298 bytes Signed-off-by: Timo Teras <timo.teras@iki.fi> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/applets.h1
-rw-r--r--include/usage.h10
-rw-r--r--util-linux/Config.in6
-rw-r--r--util-linux/Kbuild1
-rw-r--r--util-linux/flock.c87
5 files changed, 105 insertions, 0 deletions
diff --git a/include/applets.h b/include/applets.h
index 2d784bd84..83c1792ac 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -161,6 +161,7 @@ IF_FLASH_ERASEALL(APPLET(flash_eraseall, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
161IF_FLASH_LOCK(APPLET_ODDNAME(flash_lock, flash_lock_unlock, _BB_DIR_USR_SBIN, _BB_SUID_DROP, flash_lock)) 161IF_FLASH_LOCK(APPLET_ODDNAME(flash_lock, flash_lock_unlock, _BB_DIR_USR_SBIN, _BB_SUID_DROP, flash_lock))
162IF_FLASH_UNLOCK(APPLET_ODDNAME(flash_unlock, flash_lock_unlock, _BB_DIR_USR_SBIN, _BB_SUID_DROP, flash_unlock)) 162IF_FLASH_UNLOCK(APPLET_ODDNAME(flash_unlock, flash_lock_unlock, _BB_DIR_USR_SBIN, _BB_SUID_DROP, flash_unlock))
163IF_FLASHCP(APPLET(flashcp, _BB_DIR_USR_SBIN, _BB_SUID_DROP)) 163IF_FLASHCP(APPLET(flashcp, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
164IF_FLOCK(APPLET(flock, _BB_DIR_USR_BIN, _BB_SUID_DROP))
164IF_FOLD(APPLET(fold, _BB_DIR_USR_BIN, _BB_SUID_DROP)) 165IF_FOLD(APPLET(fold, _BB_DIR_USR_BIN, _BB_SUID_DROP))
165IF_FREE(APPLET(free, _BB_DIR_USR_BIN, _BB_SUID_DROP)) 166IF_FREE(APPLET(free, _BB_DIR_USR_BIN, _BB_SUID_DROP))
166IF_FREERAMDISK(APPLET(freeramdisk, _BB_DIR_SBIN, _BB_SUID_DROP)) 167IF_FREERAMDISK(APPLET(freeramdisk, _BB_DIR_SBIN, _BB_SUID_DROP))
diff --git a/include/usage.h b/include/usage.h
index f4259a11d..0aea010c7 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -1312,6 +1312,16 @@
1312 "\nOptions:" \ 1312 "\nOptions:" \
1313 "\n -v Verbose" \ 1313 "\n -v Verbose" \
1314 1314
1315#define flock_trivial_usage \
1316 "[-sxun] FD|FILE [[-c] PROG ARGS]"
1317#define flock_full_usage "\n\n" \
1318 "[Un]lock file descriptor or file, then run PROG\n" \
1319 "\nOptions:" \
1320 "\n -s Shared lock" \
1321 "\n -x Exclusive lock (default)" \
1322 "\n -u Remove a lock from FD" \
1323 "\n -n Fail rather than wait" \
1324
1315#define fold_trivial_usage \ 1325#define fold_trivial_usage \
1316 "[-bs] [-w WIDTH] [FILE]..." 1326 "[-bs] [-w WIDTH] [FILE]..."
1317#define fold_full_usage "\n\n" \ 1327#define fold_full_usage "\n\n" \
diff --git a/util-linux/Config.in b/util-linux/Config.in
index f04511bb1..d0d8df339 100644
--- a/util-linux/Config.in
+++ b/util-linux/Config.in
@@ -191,6 +191,12 @@ config FINDFS
191 WARNING: 191 WARNING:
192 With all submodules selected, it will add ~8k to busybox. 192 With all submodules selected, it will add ~8k to busybox.
193 193
194config FLOCK
195 bool "flock"
196 default y
197 help
198 Manage locks from shell scripts
199
194config FREERAMDISK 200config FREERAMDISK
195 bool "freeramdisk" 201 bool "freeramdisk"
196 default n 202 default n
diff --git a/util-linux/Kbuild b/util-linux/Kbuild
index 72a2ef1cd..99e3efea3 100644
--- a/util-linux/Kbuild
+++ b/util-linux/Kbuild
@@ -13,6 +13,7 @@ lib-$(CONFIG_FDFLUSH) += freeramdisk.o
13lib-$(CONFIG_FDFORMAT) += fdformat.o 13lib-$(CONFIG_FDFORMAT) += fdformat.o
14lib-$(CONFIG_FDISK) += fdisk.o 14lib-$(CONFIG_FDISK) += fdisk.o
15lib-$(CONFIG_FINDFS) += findfs.o 15lib-$(CONFIG_FINDFS) += findfs.o
16lib-$(CONFIG_FLOCK) += flock.o
16lib-$(CONFIG_FREERAMDISK) += freeramdisk.o 17lib-$(CONFIG_FREERAMDISK) += freeramdisk.o
17lib-$(CONFIG_FSCK_MINIX) += fsck_minix.o 18lib-$(CONFIG_FSCK_MINIX) += fsck_minix.o
18lib-$(CONFIG_GETOPT) += getopt.o 19lib-$(CONFIG_GETOPT) += getopt.o
diff --git a/util-linux/flock.c b/util-linux/flock.c
new file mode 100644
index 000000000..78b1e4ba3
--- /dev/null
+++ b/util-linux/flock.c
@@ -0,0 +1,87 @@
1/*
2 * Copyright (C) 2010 Timo Teras <timo.teras@iki.fi>
3 *
4 * This is free software, licensed under the GNU General Public License v2.
5 */
6#include <sys/file.h>
7#include "libbb.h"
8
9int flock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
10int flock_main(int argc UNUSED_PARAM, char **argv)
11{
12 int mode, opt, fd;
13 enum {
14 OPT_s = (1 << 0),
15 OPT_x = (1 << 1),
16 OPT_n = (1 << 2),
17 OPT_u = (1 << 3),
18 OPT_c = (1 << 4),
19 };
20
21#if ENABLE_LONG_OPTS
22 static const char getopt_longopts[] ALIGN1 =
23 "shared\0" No_argument "s"
24 "exclusive\0" No_argument "x"
25 "unlock\0" No_argument "u"
26 "nonblock\0" No_argument "n"
27 ;
28 applet_long_options = getopt_longopts;
29#endif
30 opt_complementary = "-1";
31
32 opt = getopt32(argv, "+sxnu");
33 argv += optind;
34
35 if (argv[1]) {
36 fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666);
37 if (fd < 0 && errno == EISDIR)
38 fd = open(argv[0], O_RDONLY|O_NOCTTY);
39 if (fd < 0)
40 bb_perror_msg_and_die("can't open '%s'", argv[0]);
41 //TODO? close_on_exec_on(fd);
42 } else {
43 fd = xatoi_u(argv[0]);
44 }
45 argv++;
46
47 /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32:
48 * we use "+" in order to support "flock -opt FILE PROG -with-opts",
49 * we need to remove -c by hand.
50 * TODO: in upstream, -c 'PROG ARGS' means "run sh -c 'PROG ARGS'"
51 */
52 if (argv[0]
53 && argv[0][0] == '-'
54 && ( (argv[0][1] == 'c' && !argv[0][2])
55 || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0)
56 )
57 ) {
58 argv++;
59 }
60
61 if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) {
62 /* With suitably matched constants, mode setting is much simpler */
63 mode = opt & (LOCK_SH + LOCK_EX + LOCK_NB + LOCK_UN);
64 if (!(mode & ~LOCK_NB))
65 mode |= LOCK_EX;
66 } else {
67 if (opt & OPT_u)
68 mode = LOCK_UN;
69 else if (opt & OPT_s)
70 mode = LOCK_SH;
71 else
72 mode = LOCK_EX;
73 if (opt & OPT_n)
74 mode |= LOCK_NB;
75 }
76
77 if (flock(fd, mode) != 0) {
78 if (errno == EWOULDBLOCK)
79 return EXIT_FAILURE;
80 bb_perror_nomsg_and_die();
81 }
82
83 if (argv[0])
84 return spawn_and_wait(argv);
85
86 return EXIT_SUCCESS;
87}