220-add_lock_util.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. --- a/include/applets.src.h
  2. +++ b/include/applets.src.h
  3. @@ -211,6 +211,7 @@ IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN,
  4. IF_LOAD_POLICY(APPLET(load_policy, BB_DIR_USR_SBIN, BB_SUID_DROP))
  5. IF_LOADFONT(APPLET(loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP))
  6. IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP))
  7. +IF_LOCK(APPLET(lock, BB_DIR_BIN, BB_SUID_DROP))
  8. IF_LOGGER(APPLET(logger, BB_DIR_USR_BIN, BB_SUID_DROP))
  9. /* Needs to be run by root or be suid root - needs to change uid and gid: */
  10. IF_LOGIN(APPLET(login, BB_DIR_BIN, BB_SUID_REQUIRE))
  11. --- a/miscutils/Config.src
  12. +++ b/miscutils/Config.src
  13. @@ -385,6 +385,12 @@ config FEATURE_HDPARM_HDIO_GETSET_DMA
  14. help
  15. Enables the 'hdparm -d' option to get/set using_dma flag.
  16. +config LOCK
  17. + bool "lock"
  18. + default n
  19. + help
  20. + Small utility for using locks in scripts
  21. +
  22. config MAKEDEVS
  23. bool "makedevs"
  24. default y
  25. --- a/miscutils/Kbuild.src
  26. +++ b/miscutils/Kbuild.src
  27. @@ -28,6 +28,7 @@ lib-$(CONFIG_INOTIFYD) += inotifyd.o
  28. lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
  29. lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
  30. lib-$(CONFIG_LESS) += less.o
  31. +lib-$(CONFIG_LOCK) += lock.o
  32. lib-$(CONFIG_MAKEDEVS) += makedevs.o
  33. lib-$(CONFIG_MAN) += man.o
  34. lib-$(CONFIG_MICROCOM) += microcom.o
  35. --- /dev/null
  36. +++ b/miscutils/lock.c
  37. @@ -0,0 +1,144 @@
  38. +/*
  39. + * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
  40. + *
  41. + * This is free software, licensed under the GNU General Public License v2.
  42. + */
  43. +#include <sys/types.h>
  44. +#include <sys/file.h>
  45. +#include <sys/stat.h>
  46. +#include <signal.h>
  47. +#include <fcntl.h>
  48. +#include <unistd.h>
  49. +#include <stdio.h>
  50. +#include "busybox.h"
  51. +
  52. +//usage:#define lock_trivial_usage NOUSAGE_STR
  53. +//usage:#define lock_full_usage ""
  54. +
  55. +static int unlock = 0;
  56. +static int shared = 0;
  57. +static int waitonly = 0;
  58. +static int try_lock = 0;
  59. +static int fd;
  60. +static char *file;
  61. +
  62. +static void usage(char *name)
  63. +{
  64. + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
  65. + " -s Use shared locking\n"
  66. + " -u Unlock\n"
  67. + " -w Wait for the lock to become free, don't acquire lock\n"
  68. + " -n Don't wait for the lock to become free. Fail with exit code\n"
  69. + "\n", name);
  70. + exit(1);
  71. +}
  72. +
  73. +static void exit_unlock(int sig)
  74. +{
  75. + flock(fd, LOCK_UN);
  76. + exit(0);
  77. +}
  78. +
  79. +static int do_unlock(void)
  80. +{
  81. + FILE *f;
  82. + int i;
  83. +
  84. + if ((f = fopen(file, "r")) == NULL)
  85. + return 0;
  86. +
  87. + fscanf(f, "%d", &i);
  88. + if (i > 0)
  89. + kill(i, SIGTERM);
  90. +
  91. + fclose(f);
  92. +
  93. + return 0;
  94. +}
  95. +
  96. +static int do_lock(void)
  97. +{
  98. + int pid;
  99. + int flags;
  100. + char pidstr[8];
  101. +
  102. + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
  103. + if ((fd = open(file, O_RDWR)) < 0) {
  104. + fprintf(stderr, "Can't open %s\n", file);
  105. + return 1;
  106. + }
  107. + }
  108. +
  109. + flags = shared ? LOCK_SH : LOCK_EX;
  110. + flags |= try_lock ? LOCK_NB : 0;
  111. +
  112. + if (flock(fd, flags) < 0) {
  113. + fprintf(stderr, "Can't lock %s\n", file);
  114. + return 1;
  115. + }
  116. +
  117. + pid = fork();
  118. +
  119. + if (pid < 0)
  120. + return -1;
  121. +
  122. + if (pid == 0) {
  123. + signal(SIGKILL, exit_unlock);
  124. + signal(SIGTERM, exit_unlock);
  125. + signal(SIGINT, exit_unlock);
  126. + if (waitonly)
  127. + exit_unlock(0);
  128. + else
  129. + while (1)
  130. + sleep(1);
  131. + } else {
  132. + if (!waitonly) {
  133. + lseek(fd, 0, SEEK_SET);
  134. + ftruncate(fd, 0);
  135. + sprintf(pidstr, "%d\n", pid);
  136. + write(fd, pidstr, strlen(pidstr));
  137. + close(fd);
  138. + }
  139. +
  140. + return 0;
  141. + }
  142. + return 0;
  143. +}
  144. +
  145. +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  146. +int lock_main(int argc, char **argv)
  147. +{
  148. + char **args = &argv[1];
  149. + int c = argc - 1;
  150. +
  151. + while ((*args != NULL) && (*args)[0] == '-') {
  152. + char *ch = *args;
  153. + while (*(++ch) > 0) {
  154. + switch(*ch) {
  155. + case 'w':
  156. + waitonly = 1;
  157. + break;
  158. + case 's':
  159. + shared = 1;
  160. + break;
  161. + case 'u':
  162. + unlock = 1;
  163. + break;
  164. + case 'n':
  165. + try_lock = 1;
  166. + break;
  167. + }
  168. + }
  169. + c--;
  170. + args++;
  171. + }
  172. +
  173. + if (c != 1)
  174. + usage(argv[0]);
  175. +
  176. + file = *args;
  177. + if (unlock)
  178. + return do_unlock();
  179. + else
  180. + return do_lock();
  181. +}