Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 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 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 6 | //config:config FLOCK |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 7 | //config: bool "flock (6.1 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 8 | //config: default y |
| 9 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 10 | //config: Manage locks from shell scripts |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | |
| 12 | //applet:IF_FLOCK(APPLET(flock, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 13 | |
| 14 | //kbuild:lib-$(CONFIG_FLOCK) += flock.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 15 | |
| 16 | //usage:#define flock_trivial_usage |
| 17 | //usage: "[-sxun] FD|{FILE [-c] PROG ARGS}" |
| 18 | //usage:#define flock_full_usage "\n\n" |
| 19 | //usage: "[Un]lock file descriptor, or lock FILE, run PROG\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 20 | //usage: "\n -s Shared lock" |
| 21 | //usage: "\n -x Exclusive lock (default)" |
| 22 | //usage: "\n -u Unlock FD" |
| 23 | //usage: "\n -n Fail rather than wait" |
| 24 | |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 25 | #include <sys/file.h> |
| 26 | #include "libbb.h" |
| 27 | |
| 28 | int flock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 29 | int flock_main(int argc UNUSED_PARAM, char **argv) |
| 30 | { |
| 31 | int mode, opt, fd; |
| 32 | enum { |
| 33 | OPT_s = (1 << 0), |
| 34 | OPT_x = (1 << 1), |
| 35 | OPT_n = (1 << 2), |
| 36 | OPT_u = (1 << 3), |
| 37 | OPT_c = (1 << 4), |
| 38 | }; |
| 39 | |
| 40 | #if ENABLE_LONG_OPTS |
Denys Vlasenko | 00677b5 | 2017-08-08 14:59:35 +0200 | [diff] [blame] | 41 | static const char flock_longopts[] ALIGN1 = |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 42 | "shared\0" No_argument "s" |
| 43 | "exclusive\0" No_argument "x" |
| 44 | "unlock\0" No_argument "u" |
| 45 | "nonblock\0" No_argument "n" |
| 46 | ; |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 47 | #endif |
| 48 | opt_complementary = "-1"; |
| 49 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame^] | 50 | opt = getopt32long(argv, "+sxnu", flock_longopts); |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 51 | argv += optind; |
| 52 | |
| 53 | if (argv[1]) { |
| 54 | fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666); |
| 55 | if (fd < 0 && errno == EISDIR) |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 56 | fd = open(argv[0], O_RDONLY|O_NOCTTY); |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 57 | if (fd < 0) |
| 58 | bb_perror_msg_and_die("can't open '%s'", argv[0]); |
| 59 | //TODO? close_on_exec_on(fd); |
| 60 | } else { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 61 | fd = xatoi_positive(argv[0]); |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 62 | } |
| 63 | argv++; |
| 64 | |
| 65 | /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32: |
| 66 | * we use "+" in order to support "flock -opt FILE PROG -with-opts", |
| 67 | * we need to remove -c by hand. |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 68 | */ |
| 69 | if (argv[0] |
| 70 | && argv[0][0] == '-' |
| 71 | && ( (argv[0][1] == 'c' && !argv[0][2]) |
| 72 | || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0) |
| 73 | ) |
| 74 | ) { |
| 75 | argv++; |
Denys Vlasenko | e1d426f | 2016-04-24 18:19:49 +0200 | [diff] [blame] | 76 | if (argv[1]) |
| 77 | bb_error_msg_and_die("-c takes only one argument"); |
| 78 | opt |= OPT_c; |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) { |
| 82 | /* With suitably matched constants, mode setting is much simpler */ |
| 83 | mode = opt & (LOCK_SH + LOCK_EX + LOCK_NB + LOCK_UN); |
| 84 | if (!(mode & ~LOCK_NB)) |
| 85 | mode |= LOCK_EX; |
| 86 | } else { |
| 87 | if (opt & OPT_u) |
| 88 | mode = LOCK_UN; |
| 89 | else if (opt & OPT_s) |
| 90 | mode = LOCK_SH; |
| 91 | else |
| 92 | mode = LOCK_EX; |
| 93 | if (opt & OPT_n) |
| 94 | mode |= LOCK_NB; |
| 95 | } |
| 96 | |
| 97 | if (flock(fd, mode) != 0) { |
| 98 | if (errno == EWOULDBLOCK) |
| 99 | return EXIT_FAILURE; |
| 100 | bb_perror_nomsg_and_die(); |
| 101 | } |
| 102 | |
Denys Vlasenko | e1d426f | 2016-04-24 18:19:49 +0200 | [diff] [blame] | 103 | if (argv[0]) { |
Denys Vlasenko | 2fbc312 | 2016-04-24 18:21:32 +0200 | [diff] [blame] | 104 | int rc; |
| 105 | if (opt & OPT_c) { |
| 106 | /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */ |
| 107 | argv -= 2; |
| 108 | argv[0] = (char*)get_shell_name(); |
| 109 | argv[1] = (char*)"-c"; |
| 110 | /* argv[2] = "PROG ARGS"; */ |
| 111 | /* argv[3] = NULL; */ |
Denys Vlasenko | e1d426f | 2016-04-24 18:19:49 +0200 | [diff] [blame] | 112 | } |
Denys Vlasenko | 2fbc312 | 2016-04-24 18:21:32 +0200 | [diff] [blame] | 113 | rc = spawn_and_wait(argv); |
| 114 | if (rc < 0) |
| 115 | bb_simple_perror_msg(argv[0]); |
| 116 | return rc; |
Denys Vlasenko | e1d426f | 2016-04-24 18:19:49 +0200 | [diff] [blame] | 117 | } |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 118 | |
| 119 | return EXIT_SUCCESS; |
| 120 | } |