2 Copyright (c) 2001-2006, Gerrit Pape
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
29 /* Collected into one file from runit's many tiny files */
30 /* TODO: review, eliminate unneeded stuff, move good stuff to libbb */
35 #include "runit_lib.h"
39 unsigned byte_chr(char *s,unsigned n,int c)
60 return fcntl(fd,F_SETFD,FD_CLOEXEC);
66 void fmt_ptime30nul(char *s, struct taia *ta) {
70 if (ta->sec.x < 4611686018427387914ULL)
71 return; /* impossible? */
72 u = ta->sec.x -4611686018427387914ULL;
73 t = gmtime((time_t*)&u);
76 //fmt_ulong(s, 1900 + t->tm_year);
77 //s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2);
78 //s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2);
79 //s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2);
80 //s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2);
81 //s[16] = ':'; fmt_uint0(&s[17], t->tm_sec, 2);
82 //s[19] = '.'; fmt_uint0(&s[20], ta->nano, 9);
83 sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%09u",
84 (unsigned)(1900 + t->tm_year),
85 (unsigned)(t->tm_mon+1),
86 (unsigned)(t->tm_mday),
87 (unsigned)(t->tm_hour),
88 (unsigned)(t->tm_min),
89 (unsigned)(t->tm_sec),
92 /* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */
93 /* 5 + 3 + 3 + 3 + 3 + 3 + 9 = */
94 /* 20 (up to '.' inclusive) + 9 (not including '\0') */
97 unsigned fmt_taia25(char *s, struct taia *t) {
98 static char pack[TAIA_PACK];
102 bin2hex(s, pack, 12);
109 static /* as it isn't used anywhere else */
110 void tai_pack(char *s,const struct tai *t)
115 s[7] = x & 255; x >>= 8;
116 s[6] = x & 255; x >>= 8;
117 s[5] = x & 255; x >>= 8;
118 s[4] = x & 255; x >>= 8;
119 s[3] = x & 255; x >>= 8;
120 s[2] = x & 255; x >>= 8;
121 s[1] = x & 255; x >>= 8;
129 void tai_sub(struct tai *t, const struct tai *u, const struct tai *v)
136 /*** tai_unpack.c ***/
138 void tai_unpack(const char *s,struct tai *t)
142 x = (unsigned char) s[0];
143 x <<= 8; x += (unsigned char) s[1];
144 x <<= 8; x += (unsigned char) s[2];
145 x <<= 8; x += (unsigned char) s[3];
146 x <<= 8; x += (unsigned char) s[4];
147 x <<= 8; x += (unsigned char) s[5];
148 x <<= 8; x += (unsigned char) s[6];
149 x <<= 8; x += (unsigned char) s[7];
156 void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
158 t->sec.x = u->sec.x + v->sec.x;
159 t->nano = u->nano + v->nano;
160 t->atto = u->atto + v->atto;
161 if (t->atto > 999999999UL) {
162 t->atto -= 1000000000UL;
165 if (t->nano > 999999999UL) {
166 t->nano -= 1000000000UL;
172 /*** taia_less.c ***/
174 int taia_less(const struct taia *t, const struct taia *u)
176 if (t->sec.x < u->sec.x) return 1;
177 if (t->sec.x > u->sec.x) return 0;
178 if (t->nano < u->nano) return 1;
179 if (t->nano > u->nano) return 0;
180 return t->atto < u->atto;
186 void taia_now(struct taia *t)
189 gettimeofday(&now, NULL);
190 tai_unix(&t->sec, now.tv_sec);
191 t->nano = 1000 * now.tv_usec + 500;
196 /*** taia_pack.c ***/
198 void taia_pack(char *s, const struct taia *t)
202 tai_pack(s, &t->sec);
206 s[7] = x & 255; x >>= 8;
207 s[6] = x & 255; x >>= 8;
208 s[5] = x & 255; x >>= 8;
211 s[3] = x & 255; x >>= 8;
212 s[2] = x & 255; x >>= 8;
213 s[1] = x & 255; x >>= 8;
220 void taia_sub(struct taia *t, const struct taia *u, const struct taia *v)
222 unsigned long unano = u->nano;
223 unsigned long uatto = u->atto;
225 t->sec.x = u->sec.x - v->sec.x;
226 t->nano = unano - v->nano;
227 t->atto = uatto - v->atto;
228 if (t->atto > uatto) {
229 t->atto += 1000000000UL;
232 if (t->nano > unano) {
233 t->nano += 1000000000UL;
239 /*** taia_uint.c ***/
241 /* XXX: breaks tai encapsulation */
243 void taia_uint(struct taia *t, unsigned s)
254 uint64_t taia2millisec(const struct taia *t)
256 return (t->sec.x * 1000) + (t->nano / 1000000);
260 void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp)
265 if (taia_less(deadline, stamp))
271 taia_sub(&t, deadline, &t);
272 millisecs = m = taia2millisec(&t);
273 if (m > 1000) millisecs = 1000;
277 for (i = 0; i < len; ++i)
280 poll(x, len, millisecs);
281 /* XXX: some kernels apparently need x[0] even if len is 0 */
282 /* XXX: how to handle EAGAIN? are kernels really this dumb? */
283 /* XXX: how to handle EINVAL? when exactly can this happen? */
291 return flock(fd,LOCK_EX);
295 /*** lock_exnb.c ***/
297 int lock_exnb(int fd)
299 return flock(fd,LOCK_EX | LOCK_NB);
303 /*** open_append.c ***/
305 int open_append(const char *fn)
307 return open(fn, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
311 /*** open_read.c ***/
313 int open_read(const char *fn)
315 return open(fn, O_RDONLY|O_NDELAY);
319 /*** open_trunc.c ***/
321 int open_trunc(const char *fn)
323 return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644);
327 /*** open_write.c ***/
329 int open_write(const char *fn)
331 return open(fn, O_WRONLY|O_NDELAY);
337 unsigned pmatch(const char *p, const char *s, unsigned len) {
343 if (!(c = *p)) return 1;
351 if ((c = *p++) != *s) return 0;
361 if (*s != '?') return 0;
369 if (*s != c) return 0;
381 int seek_set(int fd,seek_pos pos)
383 if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0;
388 /*** sig_block.c ***/
390 void sig_block(int sig)
395 sigprocmask(SIG_BLOCK, &ss, NULL);
398 void sig_unblock(int sig)
403 sigprocmask(SIG_UNBLOCK, &ss, NULL);
406 void sig_blocknone(void)
410 sigprocmask(SIG_SETMASK, &ss, NULL);
414 /*** sig_catch.c ***/
416 void sig_catch(int sig,void (*f)(int))
421 sigemptyset(&sa.sa_mask);
422 sigaction(sig,&sa, NULL);
426 /*** sig_pause.c ***/
438 unsigned str_chr(const char *s,int c)
454 /*** wait_nohang.c ***/
456 int wait_nohang(int *wstat)
458 return waitpid(-1, wstat, WNOHANG);
464 int wait_pid(int *wstat, int pid)
469 r = waitpid(pid, wstat, 0);
470 while ((r == -1) && (errno == EINTR));