2 * Copyright (C) 2010 Timo Teras <timo.teras@iki.fi>
4 * This is free software, licensed under the GNU General Public License v2.
7 //config: bool "flock (6.1 kb)"
10 //config: Manage locks from shell scripts
12 //applet:IF_FLOCK(APPLET(flock, BB_DIR_USR_BIN, BB_SUID_DROP))
14 //kbuild:lib-$(CONFIG_FLOCK) += flock.o
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"
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"
28 int flock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
29 int flock_main(int argc UNUSED_PARAM, char **argv)
41 static const char flock_longopts[] ALIGN1 =
42 "shared\0" No_argument "s"
43 "exclusive\0" No_argument "x"
44 "unlock\0" No_argument "u"
45 "nonblock\0" No_argument "n"
49 opt = getopt32long(argv, "^+" "sxnu" "\0" "-1", flock_longopts);
53 fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666);
54 if (fd < 0 && errno == EISDIR)
55 fd = open(argv[0], O_RDONLY|O_NOCTTY);
57 bb_perror_msg_and_die("can't open '%s'", argv[0]);
58 //TODO? close_on_exec_on(fd);
60 fd = xatoi_positive(argv[0]);
64 /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32:
65 * we use "+" in order to support "flock -opt FILE PROG -with-opts",
66 * we need to remove -c by hand.
70 && ( (argv[0][1] == 'c' && !argv[0][2])
71 || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0)
76 bb_error_msg_and_die("-c takes only one argument");
80 if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) {
81 /* With suitably matched constants, mode setting is much simpler */
82 mode = opt & (LOCK_SH + LOCK_EX + LOCK_NB + LOCK_UN);
83 if (!(mode & ~LOCK_NB))
96 if (flock(fd, mode) != 0) {
97 if (errno == EWOULDBLOCK)
99 bb_perror_nomsg_and_die();
105 /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */
107 argv[0] = (char*)get_shell_name();
108 argv[1] = (char*)"-c";
109 /* argv[2] = "PROG ARGS"; */
110 /* argv[3] = NULL; */
112 rc = spawn_and_wait(argv);
114 bb_simple_perror_msg(argv[0]);