large init script cleanup and merge of whiterussian changes, new dnsmasq config handling
[openwrt.git] / openwrt / package / busybox / patches / 340-lock_util.patch
1 diff -urN busybox.old/include/applets.h busybox.dev/include/applets.h
2 --- busybox.old/include/applets.h       2006-04-05 01:06:29.000000000 +0200
3 +++ busybox.dev/include/applets.h       2006-04-05 01:19:09.000000000 +0200
4 @@ -167,6 +167,7 @@
5  USE_LN(APPLET(ln, ln_main, _BB_DIR_BIN, _BB_SUID_NEVER))
6  USE_LOADFONT(APPLET(loadfont, loadfont_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
7  USE_LOADKMAP(APPLET(loadkmap, loadkmap_main, _BB_DIR_SBIN, _BB_SUID_NEVER))
8 +USE_LOCK(APPLET_NOUSAGE(lock, lock_main, _BB_DIR_BIN, _BB_SUID_NEVER))
9  USE_LOGGER(APPLET(logger, logger_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
10  USE_LOGIN(APPLET(login, login_main, _BB_DIR_BIN, _BB_SUID_ALWAYS))
11  USE_LOGNAME(APPLET(logname, logname_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
12 diff -urN busybox.old/miscutils/Config.in busybox.dev/miscutils/Config.in
13 --- busybox.old/miscutils/Config.in     2006-03-22 22:16:24.000000000 +0100
14 +++ busybox.dev/miscutils/Config.in     2006-04-05 01:07:12.000000000 +0200
15 @@ -209,6 +209,12 @@
16           Enables the 'hdparm -d' option to get/set using_dma flag.
17           This is dangerous stuff, so you should probably say N.
18  
19 +config CONFIG_LOCK
20 +       bool "lock"
21 +       default y
22 +       help
23 +         Small utility for using locks in scripts
24 +
25  config CONFIG_MAKEDEVS
26         bool "makedevs"
27         default n
28 diff -urN busybox.old/miscutils/Makefile.in busybox.dev/miscutils/Makefile.in
29 --- busybox.old/miscutils/Makefile.in   2006-03-22 22:16:24.000000000 +0100
30 +++ busybox.dev/miscutils/Makefile.in   2006-04-05 01:10:50.000000000 +0200
31 @@ -20,6 +20,7 @@
32  MISCUTILS-$(CONFIG_EJECT)       += eject.o
33  MISCUTILS-$(CONFIG_HDPARM)      += hdparm.o
34  MISCUTILS-$(CONFIG_LAST)        += last.o
35 +MISCUTILS-$(CONFIG_LOCK)        += lock.o
36  MISCUTILS-${CONFIG_LESS}        += less.o
37  MISCUTILS-$(CONFIG_MAKEDEVS)    += makedevs.o
38  MISCUTILS-$(CONFIG_MOUNTPOINT)  += mountpoint.o
39 diff -urN busybox.old/miscutils/lock.c busybox.dev/miscutils/lock.c
40 --- busybox.old/miscutils/lock.c        1970-01-01 01:00:00.000000000 +0100
41 +++ busybox.dev/miscutils/lock.c        2006-04-05 01:07:12.000000000 +0200
42 @@ -0,0 +1,125 @@
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 +static int unlock = 0;
53 +static int shared = 0;
54 +static int waitonly = 0;
55 +static int fd;
56 +static char *file;
57 +
58 +static void usage(char *name)
59 +{
60 +       fprintf(stderr, "Usage: %s [-suw] <filename>\n"
61 +                       "       -s      Use shared locking\n"
62 +                       "       -u      Unlock\n"
63 +                       "       -w      Wait for the lock to become free, don't acquire lock\n"
64 +                                       "\n", name);
65 +       exit(1);
66 +}
67 +
68 +static void exit_unlock(int sig)
69 +{
70 +       flock(fd, LOCK_UN);
71 +       unlink(file);
72 +       exit(0);
73 +}
74 +
75 +static int do_unlock(void)
76 +{
77 +       FILE *f;
78 +       int i;
79 +       
80 +       f = fopen(file, "r");
81 +       fscanf(f, "%d", &i);
82 +       if (i > 0)
83 +               kill(i, SIGTERM);
84 +       fclose(f);
85 +
86 +       return 0;
87 +}
88 +               
89 +static int do_lock(void)
90 +{
91 +       int pid;
92 +       char pidstr[8];
93 +
94 +       if ((fd = open(file, O_RDWR | O_CREAT, 0700)) < 0) {
95 +               fprintf(stderr, "Can't open %s\n", file);
96 +               return 1;
97 +       }
98 +
99 +       if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
100 +               fprintf(stderr, "Can't lock %s\n", file);
101 +               return 1;
102 +       }
103 +
104 +       pid = fork();
105 +
106 +       if (pid < 0)
107 +               return -1;
108 +       
109 +       if (pid == 0) {
110 +               signal(SIGKILL, exit_unlock);
111 +               signal(SIGTERM, exit_unlock);
112 +               signal(SIGINT, exit_unlock);
113 +               if (waitonly)
114 +                       exit_unlock(0);
115 +               else
116 +                       while (1)
117 +                               sleep(1);
118 +       } else {
119 +               if (!waitonly) {
120 +                       lseek(fd, 0, SEEK_SET);
121 +                       ftruncate(fd, 0);
122 +                       sprintf(pidstr, "%d\n", pid);
123 +                       write(fd, pidstr, strlen(pidstr));
124 +                       close(fd);
125 +               }
126 +
127 +               return 0;
128 +       }
129 +}
130 +
131 +#ifndef CONFIG_LOCK
132 +int main(int argc, char **argv)
133 +#else
134 +int lock_main(int argc, char **argv)
135 +#endif
136 +{
137 +       char **args = &argv[1];
138 +       int c = argc - 1;
139 +
140 +       while ((*args != NULL) && (*args)[0] == '-') {
141 +               char *ch = *args;
142 +               while (*(++ch) > 0) {
143 +                       switch(*ch) {
144 +                               case 'w':
145 +                                       waitonly = 1;
146 +                                       break;
147 +                               case 's':
148 +                                       shared = 1;
149 +                                       break;
150 +                               case 'u':
151 +                                       unlock = 1;
152 +                                       break;
153 +                       }
154 +               }
155 +               c--;
156 +               args++;
157 +       }
158 +
159 +       if (c != 1)
160 +               usage(argv[0]);
161 +
162 +       file = *args;
163 +       if (unlock)
164 +               return do_unlock();
165 +       else
166 +               return do_lock();
167 +}