1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#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
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"
46 ;
47#endif
48
49 opt = getopt32long(argv, "^+" "sxnu" "\0" "-1", flock_longopts);
50 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)
55 fd = open(argv[0], O_RDONLY|O_NOCTTY);
56 if (fd < 0)
57 bb_perror_msg_and_die("can't open '%s'", argv[0]);
58
59 } else {
60 fd = xatoi_positive(argv[0]);
61 }
62 argv++;
63
64
65
66
67
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++;
75 if (argv[1])
76 bb_simple_error_msg_and_die("-c takes only one argument");
77 opt |= OPT_c;
78 }
79
80 if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) {
81
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
102 if (argv[0]) {
103 int rc;
104 if (opt & OPT_c) {
105
106 argv -= 2;
107 argv[0] = (char*)get_shell_name();
108 argv[1] = (char*)"-c";
109
110
111 }
112 rc = spawn_and_wait(argv);
113 if (rc < 0)
114 bb_simple_perror_msg(argv[0]);
115 return rc;
116 }
117
118 return EXIT_SUCCESS;
119}
120