| 1 | $FreeWRT$
|
|---|
| 2 | --- busybox-1.1.3.orig/miscutils/lock.c Thu Jan 1 00:00:00 1970
|
|---|
| 3 | +++ busybox-1.1.3/miscutils/lock.c Sat Nov 25 19:54:19 2006
|
|---|
| 4 | @@ -0,0 +1,128 @@
|
|---|
| 5 | +#include <sys/types.h>
|
|---|
| 6 | +#include <sys/file.h>
|
|---|
| 7 | +#include <sys/stat.h>
|
|---|
| 8 | +#include <signal.h>
|
|---|
| 9 | +#include <fcntl.h>
|
|---|
| 10 | +#include <unistd.h>
|
|---|
| 11 | +#include <stdio.h>
|
|---|
| 12 | +#include "busybox.h"
|
|---|
| 13 | +
|
|---|
| 14 | +static int unlock = 0;
|
|---|
| 15 | +static int shared = 0;
|
|---|
| 16 | +static int waitonly = 0;
|
|---|
| 17 | +static int fd;
|
|---|
| 18 | +static char *file;
|
|---|
| 19 | +
|
|---|
| 20 | +static void usage(char *name)
|
|---|
| 21 | +{
|
|---|
| 22 | + fprintf(stderr, "Usage: %s [-suw] <filename>\n"
|
|---|
| 23 | + " -s Use shared locking\n"
|
|---|
| 24 | + " -u Unlock\n"
|
|---|
| 25 | + " -w Wait for the lock to become free, don't acquire lock\n"
|
|---|
| 26 | + "\n", name);
|
|---|
| 27 | + exit(1);
|
|---|
| 28 | +}
|
|---|
| 29 | +
|
|---|
| 30 | +static void exit_unlock(int sig)
|
|---|
| 31 | +{
|
|---|
| 32 | + flock(fd, LOCK_UN);
|
|---|
| 33 | + unlink(file);
|
|---|
| 34 | + exit(0);
|
|---|
| 35 | +}
|
|---|
| 36 | +
|
|---|
| 37 | +static int do_unlock(void)
|
|---|
| 38 | +{
|
|---|
| 39 | + FILE *f;
|
|---|
| 40 | + int i;
|
|---|
| 41 | +
|
|---|
| 42 | + if ((f = fopen(file, "r")) == NULL)
|
|---|
| 43 | + return 0;
|
|---|
| 44 | +
|
|---|
| 45 | + fscanf(f, "%d", &i);
|
|---|
| 46 | + if (i > 0)
|
|---|
| 47 | + kill(i, SIGTERM);
|
|---|
| 48 | +
|
|---|
| 49 | + fclose(f);
|
|---|
| 50 | +
|
|---|
| 51 | + return 0;
|
|---|
| 52 | +}
|
|---|
| 53 | +
|
|---|
| 54 | +static int do_lock(void)
|
|---|
| 55 | +{
|
|---|
| 56 | + int pid;
|
|---|
| 57 | + char pidstr[8];
|
|---|
| 58 | +
|
|---|
| 59 | + if ((fd = open(file, O_RDWR | O_CREAT, 0700)) < 0) {
|
|---|
| 60 | + fprintf(stderr, "Can't open %s\n", file);
|
|---|
| 61 | + return 1;
|
|---|
| 62 | + }
|
|---|
| 63 | +
|
|---|
| 64 | + if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
|
|---|
| 65 | + fprintf(stderr, "Can't lock %s\n", file);
|
|---|
| 66 | + return 1;
|
|---|
| 67 | + }
|
|---|
| 68 | +
|
|---|
| 69 | + pid = fork();
|
|---|
| 70 | +
|
|---|
| 71 | + if (pid < 0)
|
|---|
| 72 | + return -1;
|
|---|
| 73 | +
|
|---|
| 74 | + if (pid == 0) {
|
|---|
| 75 | + signal(SIGKILL, exit_unlock);
|
|---|
| 76 | + signal(SIGTERM, exit_unlock);
|
|---|
| 77 | + signal(SIGINT, exit_unlock);
|
|---|
| 78 | + if (waitonly)
|
|---|
| 79 | + exit_unlock(0);
|
|---|
| 80 | + else
|
|---|
| 81 | + while (1)
|
|---|
| 82 | + sleep(1);
|
|---|
| 83 | + } else {
|
|---|
| 84 | + if (!waitonly) {
|
|---|
| 85 | + lseek(fd, 0, SEEK_SET);
|
|---|
| 86 | + ftruncate(fd, 0);
|
|---|
| 87 | + sprintf(pidstr, "%d\n", pid);
|
|---|
| 88 | + write(fd, pidstr, strlen(pidstr));
|
|---|
| 89 | + close(fd);
|
|---|
| 90 | + }
|
|---|
| 91 | +
|
|---|
| 92 | + return 0;
|
|---|
| 93 | + }
|
|---|
| 94 | +}
|
|---|
| 95 | +
|
|---|
| 96 | +#ifndef CONFIG_LOCK
|
|---|
| 97 | +int main(int argc, char **argv)
|
|---|
| 98 | +#else
|
|---|
| 99 | +int lock_main(int argc, char **argv)
|
|---|
| 100 | +#endif
|
|---|
| 101 | +{
|
|---|
| 102 | + char **args = &argv[1];
|
|---|
| 103 | + int c = argc - 1;
|
|---|
| 104 | +
|
|---|
| 105 | + while ((*args != NULL) && (*args)[0] == '-') {
|
|---|
| 106 | + char *ch = *args;
|
|---|
| 107 | + while (*(++ch) > 0) {
|
|---|
| 108 | + switch(*ch) {
|
|---|
| 109 | + case 'w':
|
|---|
| 110 | + waitonly = 1;
|
|---|
| 111 | + break;
|
|---|
| 112 | + case 's':
|
|---|
| 113 | + shared = 1;
|
|---|
| 114 | + break;
|
|---|
| 115 | + case 'u':
|
|---|
| 116 | + unlock = 1;
|
|---|
| 117 | + break;
|
|---|
| 118 | + }
|
|---|
| 119 | + }
|
|---|
| 120 | + c--;
|
|---|
| 121 | + args++;
|
|---|
| 122 | + }
|
|---|
| 123 | +
|
|---|
| 124 | + if (c != 1)
|
|---|
| 125 | + usage(argv[0]);
|
|---|
| 126 | +
|
|---|
| 127 | + file = *args;
|
|---|
| 128 | + if (unlock)
|
|---|
| 129 | + return do_unlock();
|
|---|
| 130 | + else
|
|---|
| 131 | + return do_lock();
|
|---|
| 132 | +}
|
|---|