blob: dd0bfd430d042dd95e224fd51a44858e39ad043e [file] [log] [blame]
Timo Teras892d4b62010-03-18 22:45:35 +01001/*
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 Vlasenkodd898c92016-11-23 11:46:32 +01006//config:config FLOCK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02007//config: bool "flock (6.1 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +01008//config: default y
9//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020010//config: Manage locks from shell scripts
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011
12//applet:IF_FLOCK(APPLET(flock, BB_DIR_USR_BIN, BB_SUID_DROP))
13
14//kbuild:lib-$(CONFIG_FLOCK) += flock.o
Pere Orga5bc8c002011-04-11 03:29:49 +020015
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 Orga5bc8c002011-04-11 03:29:49 +020020//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 Teras892d4b62010-03-18 22:45:35 +010025#include <sys/file.h>
26#include "libbb.h"
27
28int flock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
29int 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 Vlasenko00677b52017-08-08 14:59:35 +020041 static const char flock_longopts[] ALIGN1 =
Timo Teras892d4b62010-03-18 22:45:35 +010042 "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 Teras892d4b62010-03-18 22:45:35 +010047#endif
Timo Teras892d4b62010-03-18 22:45:35 +010048
Denys Vlasenko22542ec2017-08-08 21:55:02 +020049 opt = getopt32long(argv, "^+" "sxnu" "\0" "-1", flock_longopts);
Timo Teras892d4b62010-03-18 22:45:35 +010050 argv += optind;
51
52 if (argv[1]) {
53 fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666);
54 if (fd < 0 && errno == EISDIR)
Denys Vlasenko69675782013-01-14 01:34:48 +010055 fd = open(argv[0], O_RDONLY|O_NOCTTY);
Timo Teras892d4b62010-03-18 22:45:35 +010056 if (fd < 0)
57 bb_perror_msg_and_die("can't open '%s'", argv[0]);
58 //TODO? close_on_exec_on(fd);
59 } else {
Denys Vlasenko77832482010-08-12 14:14:45 +020060 fd = xatoi_positive(argv[0]);
Timo Teras892d4b62010-03-18 22:45:35 +010061 }
62 argv++;
63
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.
Timo Teras892d4b62010-03-18 22:45:35 +010067 */
68 if (argv[0]
69 && argv[0][0] == '-'
70 && ( (argv[0][1] == 'c' && !argv[0][2])
71 || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0)
72 )
73 ) {
74 argv++;
Denys Vlasenkoe1d426f2016-04-24 18:19:49 +020075 if (argv[1])
76 bb_error_msg_and_die("-c takes only one argument");
77 opt |= OPT_c;
Timo Teras892d4b62010-03-18 22:45:35 +010078 }
79
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))
84 mode |= LOCK_EX;
85 } else {
86 if (opt & OPT_u)
87 mode = LOCK_UN;
88 else if (opt & OPT_s)
89 mode = LOCK_SH;
90 else
91 mode = LOCK_EX;
92 if (opt & OPT_n)
93 mode |= LOCK_NB;
94 }
95
96 if (flock(fd, mode) != 0) {
97 if (errno == EWOULDBLOCK)
98 return EXIT_FAILURE;
99 bb_perror_nomsg_and_die();
100 }
101
Denys Vlasenkoe1d426f2016-04-24 18:19:49 +0200102 if (argv[0]) {
Denys Vlasenko2fbc3122016-04-24 18:21:32 +0200103 int rc;
104 if (opt & OPT_c) {
105 /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */
106 argv -= 2;
107 argv[0] = (char*)get_shell_name();
108 argv[1] = (char*)"-c";
109 /* argv[2] = "PROG ARGS"; */
110 /* argv[3] = NULL; */
Denys Vlasenkoe1d426f2016-04-24 18:19:49 +0200111 }
Denys Vlasenko2fbc3122016-04-24 18:21:32 +0200112 rc = spawn_and_wait(argv);
113 if (rc < 0)
114 bb_simple_perror_msg(argv[0]);
115 return rc;
Denys Vlasenkoe1d426f2016-04-24 18:19:49 +0200116 }
Timo Teras892d4b62010-03-18 22:45:35 +0100117
118 return EXIT_SUCCESS;
119}