2006-11-16 07:57:24 +05:30
|
|
|
/*
|
|
|
|
Copyright (c) 2001-2006, Gerrit Pape
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. The name of the author may not be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2008-03-02 18:23:15 +05:30
|
|
|
/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
|
2006-11-16 07:57:24 +05:30
|
|
|
/* Collected into one file from runit's many tiny files */
|
|
|
|
/* TODO: review, eliminate unneeded stuff, move good stuff to libbb */
|
|
|
|
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "runit_lib.h"
|
|
|
|
|
2009-08-02 04:25:49 +05:30
|
|
|
#ifdef UNUSED
|
2006-11-16 07:57:24 +05:30
|
|
|
unsigned byte_chr(char *s,unsigned n,int c)
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
ch = c;
|
|
|
|
t = s;
|
|
|
|
for (;;) {
|
2007-01-28 03:51:52 +05:30
|
|
|
if (!n) break;
|
|
|
|
if (*t == ch) break;
|
|
|
|
++t;
|
|
|
|
--n;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
return t - s;
|
|
|
|
}
|
|
|
|
|
2007-01-28 03:51:52 +05:30
|
|
|
static /* as it isn't used anywhere else */
|
2007-08-20 22:57:40 +05:30
|
|
|
void tai_pack(char *s, const struct tai *t)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
uint64_t x;
|
|
|
|
|
|
|
|
x = t->x;
|
|
|
|
s[7] = x & 255; x >>= 8;
|
|
|
|
s[6] = x & 255; x >>= 8;
|
|
|
|
s[5] = x & 255; x >>= 8;
|
|
|
|
s[4] = x & 255; x >>= 8;
|
|
|
|
s[3] = x & 255; x >>= 8;
|
|
|
|
s[2] = x & 255; x >>= 8;
|
|
|
|
s[1] = x & 255; x >>= 8;
|
|
|
|
s[0] = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tai_unpack(const char *s,struct tai *t)
|
|
|
|
{
|
|
|
|
uint64_t x;
|
|
|
|
|
|
|
|
x = (unsigned char) s[0];
|
|
|
|
x <<= 8; x += (unsigned char) s[1];
|
|
|
|
x <<= 8; x += (unsigned char) s[2];
|
|
|
|
x <<= 8; x += (unsigned char) s[3];
|
|
|
|
x <<= 8; x += (unsigned char) s[4];
|
|
|
|
x <<= 8; x += (unsigned char) s[5];
|
|
|
|
x <<= 8; x += (unsigned char) s[6];
|
|
|
|
x <<= 8; x += (unsigned char) s[7];
|
|
|
|
t->x = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
|
|
|
|
{
|
|
|
|
t->sec.x = u->sec.x + v->sec.x;
|
|
|
|
t->nano = u->nano + v->nano;
|
|
|
|
t->atto = u->atto + v->atto;
|
|
|
|
if (t->atto > 999999999UL) {
|
|
|
|
t->atto -= 1000000000UL;
|
|
|
|
++t->nano;
|
|
|
|
}
|
|
|
|
if (t->nano > 999999999UL) {
|
|
|
|
t->nano -= 1000000000UL;
|
|
|
|
++t->sec.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-28 03:52:17 +05:30
|
|
|
int taia_less(const struct taia *t, const struct taia *u)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
if (t->sec.x < u->sec.x) return 1;
|
|
|
|
if (t->sec.x > u->sec.x) return 0;
|
|
|
|
if (t->nano < u->nano) return 1;
|
|
|
|
if (t->nano > u->nano) return 0;
|
|
|
|
return t->atto < u->atto;
|
|
|
|
}
|
|
|
|
|
|
|
|
void taia_now(struct taia *t)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
2007-01-28 03:51:52 +05:30
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
tai_unix(&t->sec, now.tv_sec);
|
2006-11-16 07:57:24 +05:30
|
|
|
t->nano = 1000 * now.tv_usec + 500;
|
|
|
|
t->atto = 0;
|
|
|
|
}
|
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
/* UNUSED
|
2007-01-28 03:51:52 +05:30
|
|
|
void taia_pack(char *s, const struct taia *t)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
unsigned long x;
|
|
|
|
|
2007-01-28 03:51:52 +05:30
|
|
|
tai_pack(s, &t->sec);
|
2006-11-16 07:57:24 +05:30
|
|
|
s += 8;
|
|
|
|
|
|
|
|
x = t->atto;
|
|
|
|
s[7] = x & 255; x >>= 8;
|
|
|
|
s[6] = x & 255; x >>= 8;
|
|
|
|
s[5] = x & 255; x >>= 8;
|
|
|
|
s[4] = x;
|
|
|
|
x = t->nano;
|
|
|
|
s[3] = x & 255; x >>= 8;
|
|
|
|
s[2] = x & 255; x >>= 8;
|
|
|
|
s[1] = x & 255; x >>= 8;
|
|
|
|
s[0] = x;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
*/
|
2006-11-16 07:57:24 +05:30
|
|
|
|
2007-01-28 03:52:17 +05:30
|
|
|
void taia_sub(struct taia *t, const struct taia *u, const struct taia *v)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
unsigned long unano = u->nano;
|
|
|
|
unsigned long uatto = u->atto;
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
t->sec.x = u->sec.x - v->sec.x;
|
|
|
|
t->nano = unano - v->nano;
|
|
|
|
t->atto = uatto - v->atto;
|
|
|
|
if (t->atto > uatto) {
|
|
|
|
t->atto += 1000000000UL;
|
|
|
|
--t->nano;
|
|
|
|
}
|
|
|
|
if (t->nano > unano) {
|
|
|
|
t->nano += 1000000000UL;
|
|
|
|
--t->sec.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: breaks tai encapsulation */
|
2007-01-28 03:52:17 +05:30
|
|
|
void taia_uint(struct taia *t, unsigned s)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
t->sec.x = s;
|
|
|
|
t->nano = 0;
|
|
|
|
t->atto = 0;
|
|
|
|
}
|
|
|
|
|
2007-01-28 03:52:53 +05:30
|
|
|
static
|
|
|
|
uint64_t taia2millisec(const struct taia *t)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2007-01-28 03:52:53 +05:30
|
|
|
return (t->sec.x * 1000) + (t->nano / 1000000);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
2007-01-28 03:54:59 +05:30
|
|
|
void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
int millisecs;
|
|
|
|
int i;
|
|
|
|
|
2007-01-28 03:54:59 +05:30
|
|
|
if (taia_less(deadline, stamp))
|
2006-11-16 07:57:24 +05:30
|
|
|
millisecs = 0;
|
|
|
|
else {
|
2007-01-28 03:51:52 +05:30
|
|
|
uint64_t m;
|
|
|
|
struct taia t;
|
2006-11-16 07:57:24 +05:30
|
|
|
t = *stamp;
|
2007-01-28 03:51:52 +05:30
|
|
|
taia_sub(&t, deadline, &t);
|
|
|
|
millisecs = m = taia2millisec(&t);
|
|
|
|
if (m > 1000) millisecs = 1000;
|
|
|
|
millisecs += 20;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
2007-01-28 03:51:52 +05:30
|
|
|
for (i = 0; i < len; ++i)
|
2006-11-16 07:57:24 +05:30
|
|
|
x[i].revents = 0;
|
|
|
|
|
2007-01-28 03:51:52 +05:30
|
|
|
poll(x, len, millisecs);
|
2006-11-16 07:57:24 +05:30
|
|
|
/* XXX: some kernels apparently need x[0] even if len is 0 */
|
|
|
|
/* XXX: how to handle EAGAIN? are kernels really this dumb? */
|
|
|
|
/* XXX: how to handle EINVAL? when exactly can this happen? */
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
#endif
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
int lock_ex(int fd)
|
|
|
|
{
|
2010-09-05 22:58:29 +05:30
|
|
|
return flock(fd, LOCK_EX);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int lock_exnb(int fd)
|
|
|
|
{
|
2010-09-05 22:58:29 +05:30
|
|
|
return flock(fd, LOCK_EX | LOCK_NB);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
2010-09-05 22:58:29 +05:30
|
|
|
#ifdef UNUSED
|
2006-11-16 07:57:24 +05:30
|
|
|
int open_append(const char *fn)
|
|
|
|
{
|
2010-09-05 22:58:29 +05:30
|
|
|
return open(fn, O_WRONLY | O_NDELAY | O_APPEND | O_CREAT, 0600);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
2010-09-05 22:58:29 +05:30
|
|
|
int open_trunc(const char *fn)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2010-09-05 22:58:29 +05:30
|
|
|
return open(fn, O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT, 0644);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
2010-09-05 22:58:29 +05:30
|
|
|
#endif
|
2006-11-16 07:57:24 +05:30
|
|
|
|
2010-09-05 22:58:29 +05:30
|
|
|
int open_read(const char *fn)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2010-09-05 22:58:29 +05:30
|
|
|
return open(fn, O_RDONLY|O_NDELAY);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int open_write(const char *fn)
|
|
|
|
{
|
2006-11-18 00:28:16 +05:30
|
|
|
return open(fn, O_WRONLY|O_NDELAY);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
2010-09-05 22:58:29 +05:30
|
|
|
unsigned FAST_FUNC pmatch(const char *p, const char *s, unsigned len)
|
2007-08-20 22:57:40 +05:30
|
|
|
{
|
2006-11-16 07:57:24 +05:30
|
|
|
for (;;) {
|
|
|
|
char c = *p++;
|
|
|
|
if (!c) return !len;
|
|
|
|
switch (c) {
|
|
|
|
case '*':
|
2007-11-06 08:35:54 +05:30
|
|
|
c = *p;
|
|
|
|
if (!c) return 1;
|
2006-11-16 07:57:24 +05:30
|
|
|
for (;;) {
|
|
|
|
if (!len) return 0;
|
|
|
|
if (*s == c) break;
|
2007-11-06 08:35:54 +05:30
|
|
|
++s;
|
|
|
|
--len;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case '+':
|
2007-11-06 08:35:54 +05:30
|
|
|
c = *p++;
|
|
|
|
if (c != *s) return 0;
|
2006-11-16 07:57:24 +05:30
|
|
|
for (;;) {
|
|
|
|
if (!len) return 1;
|
|
|
|
if (*s != c) break;
|
2007-11-06 08:35:54 +05:30
|
|
|
++s;
|
|
|
|
--len;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
case '?':
|
|
|
|
if (*p == '?') {
|
|
|
|
if (*s != '?') return 0;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
++s; --len;
|
|
|
|
continue;
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
if (!len) return 0;
|
|
|
|
if (*s != c) return 0;
|
2007-11-06 08:35:54 +05:30
|
|
|
++s;
|
|
|
|
--len;
|
2006-11-16 07:57:24 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|