flock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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"
  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 getopt_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. applet_long_options = getopt_longopts;
  42. #endif
  43. opt_complementary = "-1";
  44. opt = getopt32(argv, "+sxnu");
  45. argv += optind;
  46. if (argv[1]) {
  47. fd = open(argv[0], O_RDONLY|O_NOCTTY|O_CREAT, 0666);
  48. if (fd < 0 && errno == EISDIR)
  49. fd = open(argv[0], O_RDONLY|O_NOCTTY);
  50. if (fd < 0)
  51. bb_perror_msg_and_die("can't open '%s'", argv[0]);
  52. //TODO? close_on_exec_on(fd);
  53. } else {
  54. fd = xatoi_positive(argv[0]);
  55. }
  56. argv++;
  57. /* If it is "flock FILE -c PROG", then -c isn't caught by getopt32:
  58. * we use "+" in order to support "flock -opt FILE PROG -with-opts",
  59. * we need to remove -c by hand.
  60. */
  61. if (argv[0]
  62. && argv[0][0] == '-'
  63. && ( (argv[0][1] == 'c' && !argv[0][2])
  64. || (ENABLE_LONG_OPTS && strcmp(argv[0] + 1, "-command") == 0)
  65. )
  66. ) {
  67. argv++;
  68. if (argv[1])
  69. bb_error_msg_and_die("-c takes only one argument");
  70. opt |= OPT_c;
  71. }
  72. if (OPT_s == LOCK_SH && OPT_x == LOCK_EX && OPT_n == LOCK_NB && OPT_u == LOCK_UN) {
  73. /* With suitably matched constants, mode setting is much simpler */
  74. mode = opt & (LOCK_SH + LOCK_EX + LOCK_NB + LOCK_UN);
  75. if (!(mode & ~LOCK_NB))
  76. mode |= LOCK_EX;
  77. } else {
  78. if (opt & OPT_u)
  79. mode = LOCK_UN;
  80. else if (opt & OPT_s)
  81. mode = LOCK_SH;
  82. else
  83. mode = LOCK_EX;
  84. if (opt & OPT_n)
  85. mode |= LOCK_NB;
  86. }
  87. if (flock(fd, mode) != 0) {
  88. if (errno == EWOULDBLOCK)
  89. return EXIT_FAILURE;
  90. bb_perror_nomsg_and_die();
  91. }
  92. if (argv[0]) {
  93. int rc;
  94. if (opt & OPT_c) {
  95. /* -c 'PROG ARGS' means "run sh -c 'PROG ARGS'" */
  96. argv -= 2;
  97. argv[0] = (char*)get_shell_name();
  98. argv[1] = (char*)"-c";
  99. /* argv[2] = "PROG ARGS"; */
  100. /* argv[3] = NULL; */
  101. }
  102. rc = spawn_and_wait(argv);
  103. if (rc < 0)
  104. bb_simple_perror_msg(argv[0]);
  105. return rc;
  106. }
  107. return EXIT_SUCCESS;
  108. }