commit bash-20210209 snapshot

This commit is contained in:
Chet Ramey
2021-02-11 17:19:31 -05:00
parent 220a95cdfb
commit 26db5626df
9 changed files with 368 additions and 6 deletions
+12 -2
View File
@@ -93,7 +93,7 @@ CSOURCES = clktck.c clock.c getcwd.c getenv.c oslib.c setlinebuf.c \
wcsdup.c fpurge.c zgetline.c mbscmp.c uconvert.c ufuncs.c \
casemod.c dprintf.c input_avail.c mbscasecmp.c fnxform.c \
strchrnul.c unicode.c wcswidth.c wcsnwidth.c shmbchar.c strdup.c \
utf8.c random.c gettimeofday.c
utf8.c random.c gettimeofday.c timers.c
# The header files for this library.
HSOURCES =
@@ -108,7 +108,7 @@ OBJECTS = clktck.o clock.o getenv.o oslib.o setlinebuf.o strnlen.o \
fmtullong.o fmtumax.o zcatfd.o zmapfd.o winsize.o wcsdup.o \
fpurge.o zgetline.o mbscmp.o uconvert.o ufuncs.o casemod.o \
input_avail.o mbscasecmp.o fnxform.o unicode.o shmbchar.o \
utf8.o random.o gettimeofday.o wcsnwidth.o ${LIBOBJS}
utf8.o random.o gettimeofday.o timers.o wcsnwidth.o ${LIBOBJS}
SUPPORT = Makefile
@@ -197,6 +197,7 @@ strtoul.o: strtoul.c
strtoull.o: strtoull.c
strtoumax.o: strtoumax.c
strtrans.o: strtrans.c
timers.o: timers.c
times.o: times.c
timeval.o: timeval.c
tmpfile.o: tmpfile.c
@@ -277,6 +278,7 @@ strtoul.o: ${BUILD_DIR}/config.h
strtoull.o: ${BUILD_DIR}/config.h
strtoumax.o: ${BUILD_DIR}/config.h
strtrans.o: ${BUILD_DIR}/config.h
timers.o: ${BUILD_DIR}/config.h
times.o: ${BUILD_DIR}/config.h
timeval.o: ${BUILD_DIR}/config.h
tmpfile.o: ${BUILD_DIR}/config.h ${topdir}/config-top.h
@@ -620,6 +622,14 @@ fnxform.o: ${topdir}/bashintl.h ${LIBINTL_H} ${BASHINCDIR}/gettext.h
shmbchar.o: ${BASHINCDIR}/shmbchar.h
shmbchar.o: ${BASHINCDIR}/shmbutil.h
timers.o: ${topdir}/bashansi.h ${BASHINCDIR}/ansi_stdlib.h
timers.o: ${BASHINCDIR}/stdc.h
timers.o: ${topdir}/xmalloc.h ${topdir}/sig.h
timers.o: ${BASHINCDIR}/posixtime.h ${BASHINCDIR}/stat-time.h
timers.o: ${BASHINCDIR}/posixselect.h
timers.o: ${topdir}/bashjmp.h ${BASHINCDIR}/posixjmp.h
timers.o: ${BASHINCDIR}/timer.h
unicode.o: ${topdir}/bashansi.h ${BASHINCDIR}/ansi_stdlib.h
unicode.o: ${BASHINCDIR}/stdc.h
unicode.o: ${topdir}/xmalloc.h
+238
View File
@@ -0,0 +1,238 @@
/* timers - functions to manage shell timers */
/* Copyright (C) 2021 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "bashtypes.h"
#include "posixtime.h"
#if defined (HAVE_UNISTD_H)
#include <unistd.h>
#endif
#if defined (HAVE_SELECT)
# include "posixselect.h"
# include "stat-time.h"
#endif
#include "sig.h"
#include "bashjmp.h"
#include "xmalloc.h"
#include "timer.h"
#include <errno.h>
#if !defined (errno)
extern int errno;
#endif /* !errno */
extern unsigned int falarm (unsigned int, unsigned int);
sh_timer *
shtimer_alloc (void)
{
sh_timer *r;
r = (sh_timer *)xmalloc (sizeof (sh_timer));
shtimer_flush (r);
return r;
}
void
shtimer_flush (sh_timer *t)
{
t->tmout.tv_sec = 0;
t->tmout.tv_usec = 0;
t->fd = t->flags = t->alrmflag = 0;
t->alrm_handler = t->old_handler = 0;
memset (t->jmpenv, '\0', sizeof (t->jmpenv));
t->handler = 0;
t->data = 0;
}
void
shtimer_dispose (sh_timer *t)
{
free (t);
}
/* We keep the timer as an offset into the future from the time it's set. */
void
shtimer_set (sh_timer *t, time_t sec, long usec)
{
struct timeval now;
if (t->flags & SHTIMER_ALARM)
{
t->alrmflag = 0; /* just paranoia */
t->old_handler = set_signal_handler (SIGALRM, t->alrm_handler);
t->flags |= SHTIMER_SIGSET;
falarm (t->tmout.tv_sec = sec, t->tmout.tv_usec = usec);
return;
}
if (gettimeofday (&now, 0) < 0)
{
now.tv_sec = 0;
now.tv_usec = 0;
}
t->tmout.tv_sec = now.tv_sec + sec;
t->tmout.tv_usec = now.tv_usec + usec;
if (t->tmout.tv_usec > USEC_PER_SEC)
{
t->tmout.tv_sec++;
t->tmout.tv_usec -= USEC_PER_SEC;
}
}
void
shtimer_unset (sh_timer *t)
{
t->tmout.tv_sec = 0;
t->tmout.tv_usec = 0;
if (t->flags & SHTIMER_ALARM)
{
t->alrmflag = 0;
falarm (0, 0);
if (t->old_handler && (t->flags & SHTIMER_SIGSET))
{
set_signal_handler (SIGALRM, t->old_handler);
t->flags &= ~SHTIMER_SIGSET;
}
}
}
void
shtimer_cleanup (sh_timer *t)
{
shtimer_unset (t);
}
void
shtimer_clear (sh_timer *t)
{
shtimer_unset (t);
shtimer_dispose (t);
}
int
shtimer_chktimeout (sh_timer *t)
{
struct timeval now;
int r;
/* Use the flag to avoid returning sigalrm_seen here */
if (t->flags & SHTIMER_ALARM)
return t->alrmflag;
/* Could check a flag for this */
if (t->tmout.tv_sec == 0 && t->tmout.tv_usec == 0)
return 0;
if (gettimeofday (&now, 0) < 0)
return 0;
r = ((now.tv_sec > t->tmout.tv_sec) ||
(now.tv_sec == t->tmout.tv_sec && now.tv_usec >= t->tmout.tv_usec));
return r;
}
#if defined (HAVE_SELECT) || defined (HAVE_PSELECT)
int
shtimer_select (sh_timer *t)
{
int r;
sigset_t blocked_sigs, prevmask;
struct timeval now, tv;
fd_set readfds;
#if defined (HAVE_PSELECT)
struct timespec ts;
#endif
/* We don't want a SIGCHLD to interrupt this */
sigemptyset (&blocked_sigs);
# if defined (SIGCHLD)
sigaddset (&blocked_sigs, SIGCHLD);
# endif
if (gettimeofday (&now, 0) < 0)
{
if (t->flags & SHTIMER_LONGJMP)
sh_longjmp (t->jmpenv, 1);
else
return -1;
}
/* If the timer has already expired, return immediately */
if ((now.tv_sec > t->tmout.tv_sec) ||
(now.tv_sec == t->tmout.tv_sec && now.tv_usec >= t->tmout.tv_usec))
{
if (t->flags & SHTIMER_LONGJMP)
sh_longjmp (t->jmpenv, 1);
else
return 0;
}
/* compute timeout */
tv.tv_sec = t->tmout.tv_sec - now.tv_sec;
tv.tv_usec = t->tmout.tv_usec - now.tv_usec;
if (tv.tv_usec < 0)
{
tv.tv_sec--;
tv.tv_usec += USEC_PER_SEC;
}
#if defined (HAVE_PSELECT)
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
#else
sigemptyset (&prevmask);
#endif /* !HAVE_PSELECT */
FD_ZERO (&readfds);
FD_SET (t->fd, &readfds);
#if defined (HAVE_PSELECT)
r = pselect(t->fd + 1, &readfds, (fd_set *)0, (fd_set *)0, &ts, &blocked_sigs);
#else
sigprocmask (SIG_SETMASK, &blocked_sigs, &prevmask);
r = select(t->fd + 1, &readfds, (fd_set *)0, (fd_set *)0, &tv);
sigprocmask (SIG_SETMASK, &prevmask, NULL);
#endif
if (r < 0)
return r; /* caller will handle */
else if (r == 0 && (t->flags & SHTIMER_LONGJMP))
sh_longjmp (t->jmpenv, 1);
else
return r;
}
#endif /* !HAVE_TIMEVAL || !HAVE_SELECT */
int
shtimer_alrm (sh_timer *t)
{
return 0;
}