From 6794ef532372e846714a909238282edd241f81eb Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sat, 19 Sep 2009 17:44:34 +0000 Subject: [PATCH] Move nanosleep() emulation into its own file Check librt.a for nanosleep if we don't find it in libc --- Makefile.in | 16 ++--- configure | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++- configure.in | 6 +- nanosleep.c | 60 ++++++++++++++++++ sudoreplay.c | 29 +-------- 5 files changed, 248 insertions(+), 36 deletions(-) create mode 100644 nanosleep.c diff --git a/Makefile.in b/Makefile.in index 37a69c503..22c350b2a 100644 --- a/Makefile.in +++ b/Makefile.in @@ -108,12 +108,12 @@ SRCS = aix.c alias.c alloc.c audit.c bsm_audit.c check.c closefrom.c \ def_data.c defaults.c env.c error.c fileops.c find_path.c fnmatch.c \ getcwd.c getprogname.c getspwuid.c gettime.c glob.c goodpath.c gram.c \ gram.y interfaces.c isblank.c lbuf.c ldap.c list.c logging.c match.c \ - mkstemp.c memrchr.c parse.c pwutil.c script.c set_perms.c sigaction.c \ - snprintf.c strcasecmp.c strerror.c strlcat.c strlcpy.c sudo.c \ - sudo_noexec.c sudo_edit.c sudo_nss.c term.c testsudoers.c tgetpass.c \ - toke.c toke.l tsgetgrpw.c utimes.c vasgroups.c visudo.c zero_bytes.c \ - redblack.c selinux.c sesh.c sudoreplay.c getdate.c getdate.y getline.c \ - timestr.c $(AUTH_SRCS) + mkstemp.c memrchr.c nanosleep.c parse.c pwutil.c script.c set_perms.c \ + sigaction.c snprintf.c strcasecmp.c strerror.c strlcat.c strlcpy.c \ + sudo.c sudo_noexec.c sudo_edit.c sudo_nss.c term.c testsudoers.c \ + tgetpass.c toke.c toke.l tsgetgrpw.c utimes.c vasgroups.c visudo.c \ + zero_bytes.c redblack.c selinux.c sesh.c sudoreplay.c getdate.c \ + getdate.y getline.c timestr.c $(AUTH_SRCS) AUTH_SRCS = auth/afs.c auth/aix_auth.c auth/bsdauth.c auth/dce.c auth/fwtk.c \ auth/kerb4.c auth/kerb5.c auth/pam.c auth/passwd.c auth/rfc1938.c \ @@ -269,7 +269,7 @@ fnmatch.o: $(srcdir)/fnmatch.c $(srcdir)/emul/fnmatch.h $(srcdir)/compat.h confi $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/fnmatch.c getcwd.o: $(srcdir)/getcwd.c $(srcdir)/compat.h config.h $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/getcwd.c -getdate.o: $(srcdir)/getdate.c config.h +getdate.o: $(srcdir)/getdate.c $(srcdir)/compat.h config.h $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/getdate.c getline.o: $(srcdir)/getline.c config.h $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/getline.c @@ -303,6 +303,8 @@ memrchr.o: $(srcdir)/memrchr.c $(SUDODEP) $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/memrchr.c mkstemp.o: $(srcdir)/mkstemp.c $(SUDODEP) $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/mkstemp.c +nanosleep.o: $(srcdir)/nanosleep.c $(srcdir)/compat.h config.h + $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/nanosleep.c parse.o: $(srcdir)/parse.c $(SUDODEP) $(srcdir)/parse.h $(srcdir)/list.h $(devdir)/gram.h $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/parse.c pwutil.o: $(srcdir)/pwutil.c $(SUDODEP) diff --git a/configure b/configure index 175a6b6aa..39486e3b2 100755 --- a/configure +++ b/configure @@ -15826,12 +15826,11 @@ LIBS=$ac_save_LIBS - for ac_func in dup2 strchr strrchr memchr memcpy memset sysconf tzset \ strftime setrlimit initgroups getgroups fstat gettimeofday \ - regcomp setlocale getaddrinfo setsid setenv vhangup nanosleep + regcomp setlocale getaddrinfo setsid setenv vhangup do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` { echo "$as_me:$LINENO: checking for $ac_func" >&5 @@ -18741,6 +18740,176 @@ done +for ac_func in nanosleep +do +as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` +{ echo "$as_me:$LINENO: checking for $ac_func" >&5 +echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } +if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ +/* Define $ac_func to an innocuous variant, in case declares $ac_func. + For example, HP-UX 11i declares gettimeofday. */ +#define $ac_func innocuous_$ac_func + +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char $ac_func (); below. + Prefer to if __STDC__ is defined, since + exists even on freestanding compilers. */ + +#ifdef __STDC__ +# include +#else +# include +#endif + +#undef $ac_func + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char $ac_func (); +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined __stub_$ac_func || defined __stub___$ac_func +choke me +#endif + +int +main () +{ +return $ac_func (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && + $as_test_x conftest$ac_exeext; then + eval "$as_ac_var=yes" +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + eval "$as_ac_var=no" +fi + +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +fi +ac_res=`eval echo '${'$as_ac_var'}'` + { echo "$as_me:$LINENO: result: $ac_res" >&5 +echo "${ECHO_T}$ac_res" >&6; } +if test `eval echo '${'$as_ac_var'}'` = yes; then + cat >>confdefs.h <<_ACEOF +#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +else + + # On Solaris, nanosleep is in librt + { echo "$as_me:$LINENO: checking for nanosleep in -lrt" >&5 +echo $ECHO_N "checking for nanosleep in -lrt... $ECHO_C" >&6; } +if test "${ac_cv_lib_rt_nanosleep+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + ac_check_lib_save_LIBS=$LIBS +LIBS="-lrt $LIBS" +cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char nanosleep (); +int +main () +{ +return nanosleep (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && + $as_test_x conftest$ac_exeext; then + ac_cv_lib_rt_nanosleep=yes +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + ac_cv_lib_rt_nanosleep=no +fi + +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +LIBS=$ac_check_lib_save_LIBS +fi +{ echo "$as_me:$LINENO: result: $ac_cv_lib_rt_nanosleep" >&5 +echo "${ECHO_T}$ac_cv_lib_rt_nanosleep" >&6; } +if test $ac_cv_lib_rt_nanosleep = yes; then + LIBS="${LIBS} -lrt" +else + case " $LIBOBJS " in + *" nanosleep.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS nanosleep.$ac_objext" + ;; +esac + +fi + + +fi +done + + for ac_func in closefrom do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` diff --git a/configure.in b/configure.in index 486830b1e..f50e9ba91 100644 --- a/configure.in +++ b/configure.in @@ -1836,7 +1836,7 @@ dnl AC_FUNC_GETGROUPS AC_CHECK_FUNCS(dup2 strchr strrchr memchr memcpy memset sysconf tzset \ strftime setrlimit initgroups getgroups fstat gettimeofday \ - regcomp setlocale getaddrinfo setsid setenv vhangup nanosleep) + regcomp setlocale getaddrinfo setsid setenv vhangup) AC_CHECK_FUNCS(getline, [], [ AC_LIBOBJ(getline) AC_CHECK_FUNCS(fgetln) @@ -1883,6 +1883,10 @@ AC_CHECK_FUNCS(utimes, [AC_CHECK_FUNCS(futimes futimesat, [break])], [AC_CHECK_F SUDO_FUNC_FNMATCH([AC_DEFINE(HAVE_FNMATCH)], [AC_LIBOBJ(fnmatch)]) SUDO_FUNC_ISBLANK AC_REPLACE_FUNCS(memrchr strerror strcasecmp sigaction strlcpy strlcat) +AC_CHECK_FUNCS(nanosleep, [], [ + # On Solaris, nanosleep is in librt + AC_CHECK_LIB(rt, nanosleep, [LIBS="${LIBS} -lrt"], [AC_LIBOBJ(nanosleep)]) +]) AC_CHECK_FUNCS(closefrom, [], [AC_LIBOBJ(closefrom) AC_CHECK_DECL(F_CLOSEM, AC_DEFINE(HAVE_FCNTL_CLOSEM), [], [ #include diff --git a/nanosleep.c b/nanosleep.c new file mode 100644 index 000000000..2f34130e1 --- /dev/null +++ b/nanosleep.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2009 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#ifdef HAVE_SYS_SELECT_H +#include +#endif /* HAVE_SYS_SELECT_H */ +#if TIME_WITH_SYS_TIME +# include +#endif +#ifndef HAVE_TIMESPEC +# include +#endif +#include + +#include "compat.h" + +#ifndef lint +__unused static const char rcsid[] = "$Sudo$"; +#endif /* lint */ + +int +nanosleep(ts, rts) + const struct timespec *ts; + struct timespec *rts; +{ + struct timeval timeout, endtime, now; + int rval; + + timeout.tv_sec = ts->tv_sec; + timeout.tv_usec = ts->tv_nsec / 1000; + if (rts != NULL) { + gettimeofday(&endtime, NULL); + timeradd(&endtime, &timeout, &endtime); + } + rval = select(0, NULL, NULL, NULL, &timeout); + if (rts != NULL && rval == -1 && errno == EINTR) { + gettimeofday(&now, NULL); + timersub(&endtime, &now, &timeout); + rts->tv_sec = timeout.tv_sec; + rts->tv_nsec = timeout.tv_usec * 1000; + } + return(rval); +} diff --git a/sudoreplay.c b/sudoreplay.c index 976cd7842..96c4978ab 100644 --- a/sudoreplay.c +++ b/sudoreplay.c @@ -160,6 +160,9 @@ extern size_t strlcpy __P((char *, const char *, size_t)); #ifndef HAVE_SNPRINTF int snprintf __P((char *, size_t, const char *, ...)) __printflike(3, 4); #endif +#ifndef HAVE_NANOSLEEP +int nanosleep __P((const struct timespec *, struct timespec *)); +#endif static int list_sessions __P((int, char **, const char *, const char *, const char *)); static int parse_expr __P((struct search_node **, char **)); @@ -310,32 +313,6 @@ main(argc, argv) exit(0); } -#ifndef HAVE_NANOSLEEP -static int -nanosleep(ts, rts) - const struct timespec *ts; - struct timespec *rts; -{ - struct timeval timeout, endtime, now; - int rval; - - timeout.tv_sec = ts->tv_sec; - timeout.tv_usec = ts->tv_nsec / 1000; - if (rts != NULL) { - gettimeofday(&endtime, NULL); - timeradd(&endtime, &timeout, &endtime); - } - rval = select(0, NULL, NULL, NULL, &timeout); - if (rts != NULL && rval == -1 && errno == EINTR) { - gettimeofday(&now, NULL); - timersub(&endtime, &now, &timeout); - rts->tv_sec = timeout.tv_sec; - rts->tv_nsec = timeout.tv_usec * 1000; - } - return(rval); -} -#endif - static void delay(secs) double secs; -- 2.40.0