1
0

220-add_lock_util.patch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. --- a/include/applets.src.h
  2. +++ b/include/applets.src.h
  3. @@ -196,6 +196,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_LOGNAME(APPLET_NOFORK(logname, logname, BB_DIR_USR_BIN, BB_SUID_DROP, logname))
  9. IF_LOSETUP(APPLET(losetup, BB_DIR_SBIN, BB_SUID_DROP))
  10. IF_LS(APPLET_NOEXEC(ls, ls, BB_DIR_BIN, BB_SUID_DROP, ls))
  11. --- a/miscutils/Config.src
  12. +++ b/miscutils/Config.src
  13. @@ -375,6 +375,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. @@ -33,6 +33,7 @@ lib-$(CONFIG_LAST) += last.o
  28. endif
  29. lib-$(CONFIG_LESS) += less.o
  30. +lib-$(CONFIG_LOCK) += lock.o
  31. lib-$(CONFIG_MAKEDEVS) += makedevs.o
  32. lib-$(CONFIG_MAN) += man.o
  33. lib-$(CONFIG_MICROCOM) += microcom.o
  34. --- /dev/null
  35. +++ b/miscutils/lock.c
  36. @@ -0,0 +1,144 @@
  37. +/*
  38. + * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
  39. + *
  40. + * This is free software, licensed under the GNU General Public License v2.
  41. + */
  42. +#include <sys/types.h>
  43. +#include <sys/file.h>
  44. +#include <sys/stat.h>
  45. +#include <signal.h>
  46. +#include <fcntl.h>
  47. +#include <unistd.h>
  48. +#include <stdio.h>
  49. +#include "busybox.h"
  50. +
  51. +//usage:#define lock_trivial_usage NOUSAGE_STR
  52. +//usage:#define lock_full_usage ""
  53. +
  54. +static int unlock = 0;
  55. +static int shared = 0;
  56. +static int waitonly = 0;
  57. +static int try_lock = 0;
  58. +static int fd;
  59. +static char *file;
  60. +
  61. +static void usage(char *name)
  62. +{
  63. + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
  64. + " -s Use shared locking\n"
  65. + " -u Unlock\n"
  66. + " -w Wait for the lock to become free, don't acquire lock\n"
  67. + " -n Don't wait for the lock to become free. Fail with exit code\n"
  68. + "\n", name);
  69. + exit(1);
  70. +}
  71. +
  72. +static void exit_unlock(int sig)
  73. +{
  74. + flock(fd, LOCK_UN);
  75. + exit(0);
  76. +}
  77. +
  78. +static int do_unlock(void)
  79. +{
  80. + FILE *f;
  81. + int i;
  82. +
  83. + if ((f = fopen(file, "r")) == NULL)
  84. + return 0;
  85. +
  86. + fscanf(f, "%d", &i);
  87. + if (i > 0)
  88. + kill(i, SIGTERM);
  89. +
  90. + fclose(f);
  91. +
  92. + return 0;
  93. +}
  94. +
  95. +static int do_lock(void)
  96. +{
  97. + int pid;
  98. + int flags;
  99. + char pidstr[8];
  100. +
  101. + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
  102. + if ((fd = open(file, O_RDWR)) < 0) {
  103. + fprintf(stderr, "Can't open %s\n", file);
  104. + return 1;
  105. + }
  106. + }
  107. +
  108. + flags = shared ? LOCK_SH : LOCK_EX;
  109. + flags |= try_lock ? LOCK_NB : 0;
  110. +
  111. + if (flock(fd, flags) < 0) {
  112. + fprintf(stderr, "Can't lock %s\n", file);
  113. + return 1;
  114. + }
  115. +
  116. + pid = fork();
  117. +
  118. + if (pid < 0)
  119. + return -1;
  120. +
  121. + if (pid == 0) {
  122. + signal(SIGKILL, exit_unlock);
  123. + signal(SIGTERM, exit_unlock);
  124. + signal(SIGINT, exit_unlock);
  125. + if (waitonly)
  126. + exit_unlock(0);
  127. + else
  128. + while (1)
  129. + sleep(1);
  130. + } else {
  131. + if (!waitonly) {
  132. + lseek(fd, 0, SEEK_SET);
  133. + ftruncate(fd, 0);
  134. + sprintf(pidstr, "%d\n", pid);
  135. + write(fd, pidstr, strlen(pidstr));
  136. + close(fd);
  137. + }
  138. +
  139. + return 0;
  140. + }
  141. + return 0;
  142. +}
  143. +
  144. +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  145. +int lock_main(int argc, char **argv)
  146. +{
  147. + char **args = &argv[1];
  148. + int c = argc - 1;
  149. +
  150. + while ((*args != NULL) && (*args)[0] == '-') {
  151. + char *ch = *args;
  152. + while (*(++ch) > 0) {
  153. + switch(*ch) {
  154. + case 'w':
  155. + waitonly = 1;
  156. + break;
  157. + case 's':
  158. + shared = 1;
  159. + break;
  160. + case 'u':
  161. + unlock = 1;
  162. + break;
  163. + case 'n':
  164. + try_lock = 1;
  165. + break;
  166. + }
  167. + }
  168. + c--;
  169. + args++;
  170. + }
  171. +
  172. + if (c != 1)
  173. + usage(argv[0]);
  174. +
  175. + file = *args;
  176. + if (unlock)
  177. + return do_unlock();
  178. + else
  179. + return do_lock();
  180. +}