| 1 |
|
| 2 |
submitted as http://bugs.busybox.net/view.php?id=1433 |
| 3 |
|
| 4 |
diff -Nur busybox-1.4.1.orig/include/applets.h busybox-1.4.1/include/applets.h |
| 5 |
--- busybox-1.4.1.orig/include/applets.h 2007-02-05 11:01:25.000000000 +0100 |
| 6 |
+++ busybox-1.4.1/include/applets.h 2007-02-05 13:04:14.000000000 +0100 |
| 7 |
@@ -178,6 +178,7 @@ |
| 8 |
USE_LN(APPLET(ln, _BB_DIR_BIN, _BB_SUID_NEVER)) |
| 9 |
USE_LOADFONT(APPLET(loadfont, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
| 10 |
USE_LOADKMAP(APPLET(loadkmap, _BB_DIR_SBIN, _BB_SUID_NEVER)) |
| 11 |
+USE_LOCK(APPLET_NOUSAGE(lock, lock, _BB_DIR_BIN, _BB_SUID_NEVER)) |
| 12 |
USE_LOGGER(APPLET(logger, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
| 13 |
USE_LOGIN(APPLET(login, _BB_DIR_BIN, _BB_SUID_ALWAYS)) |
| 14 |
USE_LOGNAME(APPLET(logname, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) |
| 15 |
diff -Nur busybox-1.4.1.orig/miscutils/Config.in busybox-1.4.1/miscutils/Config.in |
| 16 |
--- busybox-1.4.1.orig/miscutils/Config.in 2007-01-24 22:34:48.000000000 +0100 |
| 17 |
+++ busybox-1.4.1/miscutils/Config.in 2007-02-05 13:04:14.000000000 +0100 |
| 18 |
@@ -237,6 +237,12 @@ |
| 19 |
Enables the 'hdparm -d' option to get/set using_dma flag. |
| 20 |
This is dangerous stuff, so you should probably say N. |
| 21 |
|
| 22 |
+config LOCK |
| 23 |
+ bool "lock" |
| 24 |
+ default y |
| 25 |
+ help |
| 26 |
+ Small utility for using locks in scripts |
| 27 |
+ |
| 28 |
config MAKEDEVS |
| 29 |
bool "makedevs" |
| 30 |
default n |
| 31 |
diff -Nur busybox-1.4.1.orig/miscutils/Kbuild busybox-1.4.1/miscutils/Kbuild |
| 32 |
--- busybox-1.4.1.orig/miscutils/Kbuild 2007-01-24 22:34:48.000000000 +0100 |
| 33 |
+++ busybox-1.4.1/miscutils/Kbuild 2007-02-05 13:04:14.000000000 +0100 |
| 34 |
@@ -15,6 +15,7 @@ |
| 35 |
lib-$(CONFIG_HDPARM) += hdparm.o |
| 36 |
lib-$(CONFIG_LAST) += last.o |
| 37 |
lib-$(CONFIG_LESS) += less.o |
| 38 |
+lib-$(CONFIG_LOCK) += lock.o |
| 39 |
lib-$(CONFIG_MAKEDEVS) += makedevs.o |
| 40 |
lib-$(CONFIG_MOUNTPOINT) += mountpoint.o |
| 41 |
lib-$(CONFIG_MT) += mt.o |
| 42 |
diff -Nur busybox-1.4.1.orig/miscutils/lock.c busybox-1.4.1/miscutils/lock.c |
| 43 |
--- busybox-1.4.1.orig/miscutils/lock.c 1970-01-01 01:00:00.000000000 +0100 |
| 44 |
+++ busybox-1.4.1/miscutils/lock.c 2007-02-05 13:04:14.000000000 +0100 |
| 45 |
@@ -0,0 +1,136 @@ |
| 46 |
+/* |
| 47 |
+ * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org> |
| 48 |
+ * |
| 49 |
+ * This is free software, licensed under the GNU General Public License v2. |
| 50 |
+ */ |
| 51 |
+#include <sys/types.h> |
| 52 |
+#include <sys/file.h> |
| 53 |
+#include <sys/stat.h> |
| 54 |
+#include <signal.h> |
| 55 |
+#include <fcntl.h> |
| 56 |
+#include <unistd.h> |
| 57 |
+#include <stdio.h> |
| 58 |
+#include "busybox.h" |
| 59 |
+ |
| 60 |
+static int unlock = 0; |
| 61 |
+static int shared = 0; |
| 62 |
+static int waitonly = 0; |
| 63 |
+static int fd; |
| 64 |
+static char *file; |
| 65 |
+ |
| 66 |
+static void usage(char *name) |
| 67 |
+{ |
| 68 |
+ fprintf(stderr, "Usage: %s [-suw] <filename>\n" |
| 69 |
+ " -s Use shared locking\n" |
| 70 |
+ " -u Unlock\n" |
| 71 |
+ " -w Wait for the lock to become free, don't acquire lock\n" |
| 72 |
+ "\n", name); |
| 73 |
+ exit(1); |
| 74 |
+} |
| 75 |
+ |
| 76 |
+static void exit_unlock(int sig) |
| 77 |
+{ |
| 78 |
+ flock(fd, LOCK_UN); |
| 79 |
+ unlink(file); |
| 80 |
+ exit(0); |
| 81 |
+} |
| 82 |
+ |
| 83 |
+static int do_unlock(void) |
| 84 |
+{ |
| 85 |
+ FILE *f; |
| 86 |
+ int i; |
| 87 |
+ |
| 88 |
+ if ((f = fopen(file, "r")) == NULL) |
| 89 |
+ return 0; |
| 90 |
+ |
| 91 |
+ fscanf(f, "%d", &i); |
| 92 |
+ if (i > 0) |
| 93 |
+ kill(i, SIGTERM); |
| 94 |
+ |
| 95 |
+ fclose(f); |
| 96 |
+ |
| 97 |
+ return 0; |
| 98 |
+} |
| 99 |
+ |
| 100 |
+static int do_lock(void) |
| 101 |
+{ |
| 102 |
+ int pid; |
| 103 |
+ char pidstr[8]; |
| 104 |
+ |
| 105 |
+ if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) { |
| 106 |
+ if ((fd = open(file, O_RDWR)) < 0) { |
| 107 |
+ fprintf(stderr, "Can't open %s\n", file); |
| 108 |
+ return 1; |
| 109 |
+ } |
| 110 |
+ } |
| 111 |
+ |
| 112 |
+ if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 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 |
+#ifndef CONFIG_LOCK |
| 146 |
+int main(int argc, char **argv) |
| 147 |
+#else |
| 148 |
+int lock_main(int argc, char **argv) |
| 149 |
+#endif |
| 150 |
+{ |
| 151 |
+ char **args = &argv[1]; |
| 152 |
+ int c = argc - 1; |
| 153 |
+ |
| 154 |
+ while ((*args != NULL) && (*args)[0] == '-') { |
| 155 |
+ char *ch = *args; |
| 156 |
+ while (*(++ch) > 0) { |
| 157 |
+ switch(*ch) { |
| 158 |
+ case 'w': |
| 159 |
+ waitonly = 1; |
| 160 |
+ break; |
| 161 |
+ case 's': |
| 162 |
+ shared = 1; |
| 163 |
+ break; |
| 164 |
+ case 'u': |
| 165 |
+ unlock = 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 |
+} |