add copyright headers to busybox patches
[10.03/openwrt.git] / package / busybox / patches / 340-lock_util.patch
1
2         Copyright (C) 2006 OpenWrt.org
3
4 diff -urN busybox.old/include/applets.h busybox.dev/include/applets.h
5 --- busybox.old/include/applets.h       2006-04-05 01:06:29.000000000 +0200
6 +++ busybox.dev/include/applets.h       2006-04-05 01:19:09.000000000 +0200
7 @@ -167,6 +167,7 @@
8  USE_LN(APPLET(ln, ln_main, _BB_DIR_BIN, _BB_SUID_NEVER))
9  USE_LOADFONT(APPLET(loadfont, loadfont_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
10  USE_LOADKMAP(APPLET(loadkmap, loadkmap_main, _BB_DIR_SBIN, _BB_SUID_NEVER))
11 +USE_LOCK(APPLET_NOUSAGE(lock, lock_main, _BB_DIR_BIN, _BB_SUID_NEVER))
12  USE_LOGGER(APPLET(logger, logger_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
13  USE_LOGIN(APPLET(login, login_main, _BB_DIR_BIN, _BB_SUID_ALWAYS))
14  USE_LOGNAME(APPLET(logname, logname_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
15 diff -urN busybox.old/miscutils/Config.in busybox.dev/miscutils/Config.in
16 --- busybox.old/miscutils/Config.in     2006-03-22 22:16:24.000000000 +0100
17 +++ busybox.dev/miscutils/Config.in     2006-04-05 01:07:12.000000000 +0200
18 @@ -209,6 +209,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 CONFIG_LOCK
23 +       bool "lock"
24 +       default y
25 +       help
26 +         Small utility for using locks in scripts
27 +
28  config CONFIG_MAKEDEVS
29         bool "makedevs"
30         default n
31 diff -urN busybox.old/miscutils/Makefile.in busybox.dev/miscutils/Makefile.in
32 --- busybox.old/miscutils/Makefile.in   2006-03-22 22:16:24.000000000 +0100
33 +++ busybox.dev/miscutils/Makefile.in   2006-04-05 01:10:50.000000000 +0200
34 @@ -20,6 +20,7 @@
35  MISCUTILS-$(CONFIG_EJECT)       += eject.o
36  MISCUTILS-$(CONFIG_HDPARM)      += hdparm.o
37  MISCUTILS-$(CONFIG_LAST)        += last.o
38 +MISCUTILS-$(CONFIG_LOCK)        += lock.o
39  MISCUTILS-${CONFIG_LESS}        += less.o
40  MISCUTILS-$(CONFIG_MAKEDEVS)    += makedevs.o
41  MISCUTILS-$(CONFIG_MOUNTPOINT)  += mountpoint.o
42 diff -urN busybox.old/miscutils/lock.c busybox.dev/miscutils/lock.c
43 --- busybox.old/miscutils/lock.c        1970-01-01 01:00:00.000000000 +0100
44 +++ busybox.dev/miscutils/lock.c        2006-04-05 01:07:12.000000000 +0200
45 @@ -0,0 +1,133 @@
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, 0700)) < 0) {
106 +               fprintf(stderr, "Can't open %s\n", file);
107 +               return 1;
108 +       }
109 +
110 +       if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
111 +               fprintf(stderr, "Can't lock %s\n", file);
112 +               return 1;
113 +       }
114 +
115 +       pid = fork();
116 +
117 +       if (pid < 0)
118 +               return -1;
119 +       
120 +       if (pid == 0) {
121 +               signal(SIGKILL, exit_unlock);
122 +               signal(SIGTERM, exit_unlock);
123 +               signal(SIGINT, exit_unlock);
124 +               if (waitonly)
125 +                       exit_unlock(0);
126 +               else
127 +                       while (1)
128 +                               sleep(1);
129 +       } else {
130 +               if (!waitonly) {
131 +                       lseek(fd, 0, SEEK_SET);
132 +                       ftruncate(fd, 0);
133 +                       sprintf(pidstr, "%d\n", pid);
134 +                       write(fd, pidstr, strlen(pidstr));
135 +                       close(fd);
136 +               }
137 +
138 +               return 0;
139 +       }
140 +}
141 +
142 +#ifndef CONFIG_LOCK
143 +int main(int argc, char **argv)
144 +#else
145 +int lock_main(int argc, char **argv)
146 +#endif
147 +{
148 +       char **args = &argv[1];
149 +       int c = argc - 1;
150 +
151 +       while ((*args != NULL) && (*args)[0] == '-') {
152 +               char *ch = *args;
153 +               while (*(++ch) > 0) {
154 +                       switch(*ch) {
155 +                               case 'w':
156 +                                       waitonly = 1;
157 +                                       break;
158 +                               case 's':
159 +                                       shared = 1;
160 +                                       break;
161 +                               case 'u':
162 +                                       unlock = 1;
163 +                                       break;
164 +                       }
165 +               }
166 +               c--;
167 +               args++;
168 +       }
169 +
170 +       if (c != 1)
171 +               usage(argv[0]);
172 +
173 +       file = *args;
174 +       if (unlock)
175 +               return do_unlock();
176 +       else
177 +               return do_lock();
178 +}