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 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 6 | |
| 7 | //usage:#define flock_trivial_usage |
| 8 | //usage: "[-sxun] FD|{FILE [-c] PROG ARGS}" |
| 9 | //usage:#define flock_full_usage "\n\n" |
| 10 | //usage: "[Un]lock file descriptor, or lock FILE, run PROG\n" |
| 11 | //usage: "\nOptions:" |
| 12 | //usage: "\n -s Shared lock" |
| 13 | //usage: "\n -x Exclusive lock (default)" |
| 14 | //usage: "\n -u Unlock FD" |
| 15 | //usage: "\n -n Fail rather than wait" |
| 16 | |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 17 | #include <sys/file.h> |
| 18 | #include "libbb.h" |
| 19 | |
| 20 | int flock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 21 | int flock_main(int argc UNUSED_PARAM, char **argv) |
| 22 | { |
| 23 | int mode, opt, fd; |
| 24 | enum { |
| 25 | OPT_s = (1 << 0), |
| 26 | OPT_x = (1 << 1), |
| 27 | OPT_n = (1 << 2), |
| 28 | OPT_u = (1 << 3), |
| 29 | OPT_c = (1 << 4), |
| 30 | }; |
| 31 | |
| 32 | #if ENABLE_LONG_OPTS |
Denys Vlasenko | b7c9fb2 | 2011-02-03 00:05:48 +0100 | [diff] [blame] | 33 | static const char getopt_longopts[] ALIGN1 = |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 34 | "shared\0" No_argument "s" |
| 35 | "exclusive\0" No_argument "x" |
| 36 | "unlock\0" No_argument "u" |
| 37 | "nonblock\0" No_argument "n" |
| 38 | ; |
| 39 | applet_long_options = getopt_longopts; |
| 40 | #endif |
| 41 | opt_complementary = "-1"; |
| 42 | |
| 43 | opt = getopt32(argv, "+sxnu"); |
| 44 | argv += optind; |
| 45 | |
| 46 | if (argv[1]) { |
| 47 | fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666); |
| 48 | if (fd < 0 && errno == EISDIR) |
| 49 | fd = open(argv[0], O_RDONLY|O_NOCTTY); |
| 50 | if (fd < 0) |
| 51 | bb_perror_msg_and_die("can't open '%s'", argv[0]); |
| 52 | //TODO? close_on_exec_on(fd); |
| 53 | } else { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 54 | fd = xatoi_positive(argv[0]); |
Timo Teras | 892d4b6 | 2010-03-18 22:45:35 +0100 | [diff] [blame] | 55 | } |
| 56 | argv++; |
| 57 | |
| 58 | /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32: |
| 59 | * we use "+" in order to support "flock -opt FILE PROG -with-opts", |
| 60 | * we need to remove -c by hand. |
| 61 | * TODO: in upstream, -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" |
| 62 | */ |
| 63 | if (argv[0] |
| 64 | && argv[0][0] == '-' |
| 65 | && ( (argv[0][1] == 'c' && !argv[0][2]) |
| 66 | || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0) |
| 67 | ) |
| 68 | ) { |
| 69 | argv++; |
| 70 | } |
| 71 | |
| 72 | if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) { |
| 73 | /* With suitably matched constants, mode setting is much simpler */ |
| 74 | mode = opt & (LOCK_SH + LOCK_EX + LOCK_NB + LOCK_UN); |
| 75 | if (!(mode & ~LOCK_NB)) |
| 76 | mode |= LOCK_EX; |
| 77 | } else { |
| 78 | if (opt & OPT_u) |
| 79 | mode = LOCK_UN; |
| 80 | else if (opt & OPT_s) |
| 81 | mode = LOCK_SH; |
| 82 | else |
| 83 | mode = LOCK_EX; |
| 84 | if (opt & OPT_n) |
| 85 | mode |= LOCK_NB; |
| 86 | } |
| 87 | |
| 88 | if (flock(fd, mode) != 0) { |
| 89 | if (errno == EWOULDBLOCK) |
| 90 | return EXIT_FAILURE; |
| 91 | bb_perror_nomsg_and_die(); |
| 92 | } |
| 93 | |
| 94 | if (argv[0]) |
| 95 | return spawn_and_wait(argv); |
| 96 | |
| 97 | return EXIT_SUCCESS; |
| 98 | } |