Use sudo_timeval macros and remove compat macros from missing.h

This commit is contained in:
Todd C. Miller
2014-01-30 15:51:59 -07:00
parent 659b1f0e34
commit b813c4de48
7 changed files with 17 additions and 56 deletions

View File

@@ -48,6 +48,7 @@
#include "fatal.h" #include "fatal.h"
#include "sudo_debug.h" #include "sudo_debug.h"
#include "sudo_event.h" #include "sudo_event.h"
#include "sudo_util.h"
/* XXX - use non-exiting allocators? */ /* XXX - use non-exiting allocators? */
@@ -167,7 +168,7 @@ sudo_ev_add(struct sudo_event_base *base, struct sudo_event *ev,
ev->timeout.tv_sec += timo->tv_sec; ev->timeout.tv_sec += timo->tv_sec;
ev->timeout.tv_usec += timo->tv_usec; ev->timeout.tv_usec += timo->tv_usec;
TAILQ_FOREACH(evtmp, &base->timeouts, timeouts_entries) { TAILQ_FOREACH(evtmp, &base->timeouts, timeouts_entries) {
if (timevalcmp(timo, &evtmp->timeout, <)) if (sudo_timevalcmp(timo, &evtmp->timeout, <))
break; break;
} }
if (evtmp != NULL) { if (evtmp != NULL) {
@@ -275,7 +276,7 @@ rescan:
/* Timed out, activate timeout events. */ /* Timed out, activate timeout events. */
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
while ((ev = TAILQ_FIRST(&base->timeouts)) != NULL) { while ((ev = TAILQ_FIRST(&base->timeouts)) != NULL) {
if (timevalcmp(&ev->timeout, &now, >)) if (sudo_timevalcmp(&ev->timeout, &now, >))
break; break;
/* Remove from timeouts list. */ /* Remove from timeouts list. */
CLR(ev->flags, SUDO_EVQ_TIMEOUTS); CLR(ev->flags, SUDO_EVQ_TIMEOUTS);
@@ -385,14 +386,13 @@ sudo_ev_get_timeleft(struct sudo_event *ev, struct timeval *tv)
debug_decl(sudo_ev_get_timeleft, SUDO_DEBUG_EVENT) debug_decl(sudo_ev_get_timeleft, SUDO_DEBUG_EVENT)
if (!ISSET(ev->flags, SUDO_EVQ_TIMEOUTS)) { if (!ISSET(ev->flags, SUDO_EVQ_TIMEOUTS)) {
timevalclear(tv); sudo_timevalclear(tv);
debug_return_int(-1); debug_return_int(-1);
} }
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
*tv = ev->timeout; sudo_timevalsub(&ev->timeout, &now, tv);
timevalsub(tv, &now);
if (tv->tv_sec < 0 || (tv->tv_sec == 0 && tv->tv_usec < 0)) if (tv->tv_sec < 0 || (tv->tv_sec == 0 && tv->tv_usec < 0))
timevalclear(tv); sudo_timevalclear(tv);
debug_return_int(0); debug_return_int(0);
} }

View File

@@ -54,6 +54,7 @@
#include "fatal.h" #include "fatal.h"
#include "sudo_debug.h" #include "sudo_debug.h"
#include "sudo_event.h" #include "sudo_event.h"
#include "sudo_util.h"
/* XXX - use non-exiting allocators? */ /* XXX - use non-exiting allocators? */
@@ -155,14 +156,13 @@ sudo_ev_scan_impl(struct sudo_event_base *base, int flags)
if ((ev = TAILQ_FIRST(&base->timeouts)) != NULL) { if ((ev = TAILQ_FIRST(&base->timeouts)) != NULL) {
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
tv = ev->timeout; sudo_timevalsub(&ev->timeout, &now, &tv);
timevalsub(&tv, &now);
if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0)) if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
timevalclear(&tv); sudo_timevalclear(&tv);
timeout = &tv; timeout = &tv;
} else { } else {
if (ISSET(flags, SUDO_EVLOOP_NONBLOCK)) { if (ISSET(flags, SUDO_EVLOOP_NONBLOCK)) {
timevalclear(&tv); sudo_timevalclear(&tv);
timeout = &tv; timeout = &tv;
} else { } else {
timeout = NULL; timeout = NULL;

View File

@@ -312,41 +312,6 @@ const char *getprogname(void);
extern int errno; extern int errno;
#endif /* !HAVE_DECL_ERRNO */ #endif /* !HAVE_DECL_ERRNO */
#ifndef timevalclear
# define timevalclear(tv) ((tv)->tv_sec = (tv)->tv_usec = 0)
#endif
#ifndef timevalisset
# define timevalisset(tv) ((tv)->tv_sec || (tv)->tv_usec)
#endif
#ifndef timevalcmp
# define timevalcmp(tv1, tv2, op) \
(((tv1)->tv_sec == (tv2)->tv_sec) ? \
((tv1)->tv_usec op (tv2)->tv_usec) : \
((tv1)->tv_sec op (tv2)->tv_sec))
#endif
#ifndef timevaladd
# define timevaladd(tv1, tv2) \
do { \
(tv1)->tv_sec += (tv2)->tv_sec; \
(tv1)->tv_usec += (tv2)->tv_usec; \
if ((tv1)->tv_usec >= 1000000) { \
(tv1)->tv_sec++; \
(tv1)->tv_usec -= 1000000; \
} \
} while (0)
#endif
#ifndef timevalsub
# define timevalsub(tv1, tv2) \
do { \
(tv1)->tv_sec -= (tv2)->tv_sec; \
(tv1)->tv_usec -= (tv2)->tv_usec; \
if ((tv1)->tv_usec < 0) { \
(tv1)->tv_sec--; \
(tv1)->tv_usec += 1000000; \
} \
} while (0)
#endif
/* Not all systems define NSIG in signal.h */ /* Not all systems define NSIG in signal.h */
#if !defined(NSIG) #if !defined(NSIG)
# if defined(_NSIG) # if defined(_NSIG)

View File

@@ -700,9 +700,7 @@ sudoers_io_log(const char *buf, unsigned int len, int idx)
else else
#endif #endif
ignore_result(fwrite(buf, 1, len, io_log_files[idx].fd.f)); ignore_result(fwrite(buf, 1, len, io_log_files[idx].fd.f));
delay.tv_sec = now.tv_sec; sudo_timevalsub(&now, &last_time, &delay);
delay.tv_usec = now.tv_usec;
timevalsub(&delay, &last_time);
#ifdef HAVE_ZLIB_H #ifdef HAVE_ZLIB_H
if (iolog_compress) if (iolog_compress)
gzprintf(io_log_files[IOFD_TIMING].fd.g, "%d %f %u\n", idx, gzprintf(io_log_files[IOFD_TIMING].fd.g, "%d %f %u\n", idx,

View File

@@ -1145,7 +1145,7 @@ check_input(int fd, int what, void *v)
if (!paused) { if (!paused) {
/* Determine remaining timeout, if any. */ /* Determine remaining timeout, if any. */
sudo_ev_get_timeleft(ev, &tv); sudo_ev_get_timeleft(ev, &tv);
if (!timevalisset(&tv)) { if (!sudo_timevalisset(&tv)) {
/* No time left, event is done. */ /* No time left, event is done. */
debug_return; debug_return;
} }

View File

@@ -442,16 +442,15 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno)
goto done; goto done;
} }
/* Set modified bit if use changed the file. */ /* Set modified bit if the user changed the file. */
modified = true; modified = true;
mtim_get(&sb, &tv); mtim_get(&sb, &tv);
if (orig_size == sb.st_size && timevalcmp(&orig_mtim, &tv, ==)) { if (orig_size == sb.st_size && sudo_timevalcmp(&orig_mtim, &tv, ==)) {
/* /*
* If mtime and size match but the user spent no measurable * If mtime and size match but the user spent no measurable
* time in the editor we can't tell if the file was changed. * time in the editor we can't tell if the file was changed.
*/ */
timevalsub(&tv1, &tv2); if (sudo_timevalcmp(&tv1, &tv2, !=))
if (timevalisset(&tv2))
modified = false; modified = false;
} }

View File

@@ -274,13 +274,12 @@ sudo_edit(struct command_details *command_details)
continue; continue;
} }
mtim_get(&sb, &tv); mtim_get(&sb, &tv);
if (tf[i].osize == sb.st_size && timevalcmp(&tf[i].omtim, &tv, ==)) { if (tf[i].osize == sb.st_size && sudo_timevalcmp(&tf[i].omtim, &tv, ==)) {
/* /*
* If mtime and size match but the user spent no measurable * If mtime and size match but the user spent no measurable
* time in the editor we can't tell if the file was changed. * time in the editor we can't tell if the file was changed.
*/ */
timevalsub(&tv1, &tv2); if (sudo_timevalcmp(&tv1, &tv2, !=)) {
if (timevalisset(&tv2)) {
warningx(U_("%s unchanged"), tf[i].ofile); warningx(U_("%s unchanged"), tf[i].ofile);
unlink(tf[i].tfile); unlink(tf[i].tfile);
close(tfd); close(tfd);