flock.c 2.7 KB

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