f42edcb0dee6222cae8a811cb05c5fb5a45ea178
[openwrt.git] / package / utils / busybox / patches / 220-add_lock_util.patch
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  
17 +config LOCK
18 +       bool "lock"
19 +       default n
20 +       help
21 +         Small utility for using locks in scripts
22 +
23  config MAKEDEVS
24         bool "makedevs"
25         default y
26 --- a/miscutils/Kbuild.src
27 +++ b/miscutils/Kbuild.src
28 @@ -28,6 +28,7 @@ lib-$(CONFIG_INOTIFYD)    += inotifyd.o
29  lib-$(CONFIG_FEATURE_LAST_SMALL)+= last.o
30  lib-$(CONFIG_FEATURE_LAST_FANCY)+= last_fancy.o
31  lib-$(CONFIG_LESS)        += less.o
32 +lib-$(CONFIG_LOCK)        += lock.o
33  lib-$(CONFIG_MAKEDEVS)    += makedevs.o
34  lib-$(CONFIG_MAN)         += man.o
35  lib-$(CONFIG_MICROCOM)    += microcom.o
36 --- /dev/null
37 +++ b/miscutils/lock.c
38 @@ -0,0 +1,135 @@
39 +/*
40 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
41 + *
42 + * This is free software, licensed under the GNU General Public License v2.
43 + */
44 +#include <sys/types.h>
45 +#include <sys/file.h>
46 +#include <sys/stat.h>
47 +#include <signal.h>
48 +#include <fcntl.h>
49 +#include <unistd.h>
50 +#include <stdio.h>
51 +#include "busybox.h"
52 +
53 +//usage:#define lock_trivial_usage NOUSAGE_STR
54 +//usage:#define lock_full_usage ""
55 +
56 +static int unlock = 0;
57 +static int shared = 0;
58 +static int waitonly = 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", 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 +       char pidstr[8];
99 +
100 +       if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
101 +               if ((fd = open(file, O_RDWR)) < 0) {
102 +                       fprintf(stderr, "Can't open %s\n", file);
103 +                       return 1;
104 +               }
105 +       }
106 +
107 +       if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
108 +               fprintf(stderr, "Can't lock %s\n", file);
109 +               return 1;
110 +       }
111 +
112 +       pid = fork();
113 +
114 +       if (pid < 0)
115 +               return -1;
116 +
117 +       if (pid == 0) {
118 +               signal(SIGKILL, exit_unlock);
119 +               signal(SIGTERM, exit_unlock);
120 +               signal(SIGINT, exit_unlock);
121 +               if (waitonly)
122 +                       exit_unlock(0);
123 +               else
124 +                       while (1)
125 +                               sleep(1);
126 +       } else {
127 +               if (!waitonly) {
128 +                       lseek(fd, 0, SEEK_SET);
129 +                       ftruncate(fd, 0);
130 +                       sprintf(pidstr, "%d\n", pid);
131 +                       write(fd, pidstr, strlen(pidstr));
132 +                       close(fd);
133 +               }
134 +
135 +               return 0;
136 +       }
137 +       return 0;
138 +}
139 +
140 +int lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
141 +int lock_main(int argc, char **argv)
142 +{
143 +       char **args = &argv[1];
144 +       int c = argc - 1;
145 +
146 +       while ((*args != NULL) && (*args)[0] == '-') {
147 +               char *ch = *args;
148 +               while (*(++ch) > 0) {
149 +                       switch(*ch) {
150 +                               case 'w':
151 +                                       waitonly = 1;
152 +                                       break;
153 +                               case 's':
154 +                                       shared = 1;
155 +                                       break;
156 +                               case 'u':
157 +                                       unlock = 1;
158 +                                       break;
159 +                       }
160 +               }
161 +               c--;
162 +               args++;
163 +       }
164 +
165 +       if (c != 1)
166 +               usage(argv[0]);
167 +
168 +       file = *args;
169 +       if (unlock)
170 +               return do_unlock();
171 +       else
172 +               return do_lock();
173 +}