3
0

flock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //config:config FLOCK
  7. //config: bool "flock (6.3 kb)"
  8. //config: default y
  9. //config: help
  10. //config: Manage locks from shell scripts
  11. //applet:IF_FLOCK(APPLET(flock, BB_DIR_USR_BIN, BB_SUID_DROP))
  12. //kbuild:lib-$(CONFIG_FLOCK) += flock.o
  13. //usage:#define flock_trivial_usage
  14. //usage: "[-sxun] FD|{FILE [-c] PROG ARGS}"
  15. //usage:#define flock_full_usage "\n\n"
  16. //usage: "[Un]lock file descriptor, or lock FILE, run PROG\n"
  17. //usage: "\n -s Shared lock"
  18. //usage: "\n -x Exclusive lock (default)"
  19. //usage: "\n -u Unlock FD"
  20. //usage: "\n -n Fail rather than wait"
  21. #include <sys/file.h>
  22. #include "libbb.h"
  23. int flock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  24. int flock_main(int argc UNUSED_PARAM, char **argv)
  25. {
  26. int mode, opt, fd;
  27. enum {
  28. OPT_s = (1 << 0),
  29. OPT_x = (1 << 1),
  30. OPT_n = (1 << 2),
  31. OPT_u = (1 << 3),
  32. OPT_c = (1 << 4),
  33. };
  34. #if ENABLE_LONG_OPTS
  35. static const char flock_longopts[] ALIGN1 =
  36. "shared\0" No_argument "s"
  37. "exclusive\0" No_argument "x"
  38. "unlock\0" No_argument "u"
  39. "nonblock\0" No_argument "n"
  40. ;
  41. #endif
  42. opt = getopt32long(argv, "^+" "sxnu" "\0" "-1", flock_longopts);
  43. argv += optind;
  44. if (argv[1]) {
  45. fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666);
  46. if (fd < 0 && errno == EISDIR)
  47. fd = open(argv[0], O_RDONLY|O_NOCTTY);
  48. if (fd < 0)
  49. bb_perror_msg_and_die("can't open '%s'", argv[0]);
  50. //TODO? close_on_exec_on(fd);
  51. } else {
  52. fd = xatoi_positive(argv[0]);
  53. }
  54. argv++;
  55. /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32:
  56. * we use "+" in order to support "flock -opt FILE PROG -with-opts",
  57. * we need to remove -c by hand.
  58. */
  59. if (argv[0]
  60. && argv[0][0] == '-'
  61. && ( (argv[0][1] == 'c' && !argv[0][2])
  62. || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0)
  63. )
  64. ) {
  65. argv++;
  66. if (argv[1])
  67. bb_simple_error_msg_and_die("-c takes only one argument");
  68. opt |= OPT_c;
  69. }
  70. if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) {
  71. /* With suitably matched constants, mode setting is much simpler */
  72. mode = opt & (LOCK_SH + LOCK_EX + LOCK_NB + LOCK_UN);
  73. if (!(mode & ~LOCK_NB))
  74. mode |= LOCK_EX;
  75. } else {
  76. if (opt & OPT_u)
  77. mode = LOCK_UN;
  78. else if (opt & OPT_s)
  79. mode = LOCK_SH;
  80. else
  81. mode = LOCK_EX;
  82. if (opt & OPT_n)
  83. mode |= LOCK_NB;
  84. }
  85. if (flock(fd, mode) != 0) {
  86. if (errno == EWOULDBLOCK)
  87. return EXIT_FAILURE;
  88. bb_perror_nomsg_and_die();
  89. }
  90. if (argv[0]) {
  91. int rc;
  92. if (opt & OPT_c) {
  93. /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */
  94. argv -= 2;
  95. argv[0] = (char*)get_shell_name();
  96. argv[1] = (char*)"-c";
  97. /* argv[2] = "PROG ARGS"; */
  98. /* argv[3] = NULL; */
  99. }
  100. rc = spawn_and_wait(argv);
  101. if (rc < 0)
  102. bb_simple_perror_msg(argv[0]);
  103. return rc;
  104. }
  105. return EXIT_SUCCESS;
  106. }