Contents of /trunk/mkinitrd-magellan/busybox/util-linux/flock.c
Parent Directory | Revision Log
Revision 1123 -
(show annotations)
(download)
Wed Aug 18 21:56:57 2010 UTC (14 years, 1 month ago) by niro
File MIME type: text/plain
File size: 2113 byte(s)
Wed Aug 18 21:56:57 2010 UTC (14 years, 1 month ago) by niro
File MIME type: text/plain
File size: 2113 byte(s)
-updated to busybox-1.17.1
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 | */ |
6 | #include <sys/file.h> |
7 | #include "libbb.h" |
8 | |
9 | int flock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
10 | int flock_main(int argc UNUSED_PARAM, char **argv) |
11 | { |
12 | int mode, opt, fd; |
13 | enum { |
14 | OPT_s = (1 << 0), |
15 | OPT_x = (1 << 1), |
16 | OPT_n = (1 << 2), |
17 | OPT_u = (1 << 3), |
18 | OPT_c = (1 << 4), |
19 | }; |
20 | |
21 | #if ENABLE_LONG_OPTS |
22 | static const char getopt_longopts[] ALIGN1 = |
23 | "shared\0" No_argument "s" |
24 | "exclusive\0" No_argument "x" |
25 | "unlock\0" No_argument "u" |
26 | "nonblock\0" No_argument "n" |
27 | ; |
28 | applet_long_options = getopt_longopts; |
29 | #endif |
30 | opt_complementary = "-1"; |
31 | |
32 | opt = getopt32(argv, "+sxnu"); |
33 | argv += optind; |
34 | |
35 | if (argv[1]) { |
36 | fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666); |
37 | if (fd < 0 && errno == EISDIR) |
38 | fd = open(argv[0], O_RDONLY|O_NOCTTY); |
39 | if (fd < 0) |
40 | bb_perror_msg_and_die("can't open '%s'", argv[0]); |
41 | //TODO? close_on_exec_on(fd); |
42 | } else { |
43 | fd = xatoi_u(argv[0]); |
44 | } |
45 | argv++; |
46 | |
47 | /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32: |
48 | * we use "+" in order to support "flock -opt FILE PROG -with-opts", |
49 | * we need to remove -c by hand. |
50 | * TODO: in upstream, -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" |
51 | */ |
52 | if (argv[0] |
53 | && argv[0][0] == '-' |
54 | && ( (argv[0][1] == 'c' && !argv[0][2]) |
55 | || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0) |
56 | ) |
57 | ) { |
58 | argv++; |
59 | } |
60 | |
61 | if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) { |
62 | /* With suitably matched constants, mode setting is much simpler */ |
63 | mode = opt & (LOCK_SH + LOCK_EX + LOCK_NB + LOCK_UN); |
64 | if (!(mode & ~LOCK_NB)) |
65 | mode |= LOCK_EX; |
66 | } else { |
67 | if (opt & OPT_u) |
68 | mode = LOCK_UN; |
69 | else if (opt & OPT_s) |
70 | mode = LOCK_SH; |
71 | else |
72 | mode = LOCK_EX; |
73 | if (opt & OPT_n) |
74 | mode |= LOCK_NB; |
75 | } |
76 | |
77 | if (flock(fd, mode) != 0) { |
78 | if (errno == EWOULDBLOCK) |
79 | return EXIT_FAILURE; |
80 | bb_perror_nomsg_and_die(); |
81 | } |
82 | |
83 | if (argv[0]) |
84 | return spawn_and_wait(argv); |
85 | |
86 | return EXIT_SUCCESS; |
87 | } |