From f3b412fbd6dd94d64eb6a63d88baef2816891c29 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 23 Sep 2005 00:38:22 +0000 Subject: r10438: Move portability functions to lib/replace/; replace now simply ensures that a given set of (working) POSIX functions are available (without prefixes to their names, etc). See lib/replace/README for a list. Functions that behave different from their POSIX specification (such as sys_select, sys_read, etc) have kept the sys_ prefix. (This used to be commit 29919a71059b29fa27a49b1f5b84bb8881de65fc) --- source4/lib/com/tables.c | 4 +- source4/lib/replace/README | 54 +++++++++++++++++++++++++++ source4/lib/replace/SConscript | 2 +- source4/lib/replace/config.m4 | 84 +++++++++++++++++++++++++++++++++++++++++- source4/lib/replace/config.mk | 4 +- source4/lib/replace/dlfcn.c | 71 +++++++++++++++++++++++++++++++++++ source4/lib/replace/replace.c | 73 ++++++++++++++++++------------------ source4/lib/replace/replace.h | 22 +++++++++++ source4/lib/signal.c | 2 +- source4/lib/system.c | 71 ----------------------------------- source4/lib/time.c | 34 +++++++++++++++++ source4/lib/util.c | 20 +--------- 12 files changed, 307 insertions(+), 134 deletions(-) create mode 100644 source4/lib/replace/README create mode 100644 source4/lib/replace/dlfcn.c (limited to 'source4/lib') diff --git a/source4/lib/com/tables.c b/source4/lib/com/tables.c index cf63e7e9a2..39bd6a6bd7 100644 --- a/source4/lib/com/tables.c +++ b/source4/lib/com/tables.c @@ -58,13 +58,13 @@ static struct IUnknown *get_com_class_so(TALLOC_CTX *mem_ctx, const struct GUID mod_name = talloc_asprintf(mem_ctx, "%s.so", clsid_str); talloc_free(clsid_str); - mod = sys_dlopen(mod_name, 0); + mod = dlopen(mod_name, 0); if (!mod) { return NULL; } - f = sys_dlsym(mod, "get_class_object"); + f = dlsym(mod, "get_class_object"); if (!f) { return NULL; diff --git a/source4/lib/replace/README b/source4/lib/replace/README new file mode 100644 index 0000000000..74b5397949 --- /dev/null +++ b/source4/lib/replace/README @@ -0,0 +1,54 @@ +This subsystem ensures that we can always use a certain core set of +functions and types, that are either provided by the OS or by replacement +functions / definitions in this subsystem. The aim is to try to stick +to POSIX functions in here as much as possible. Convenience functions +that are available on no platform at all belong in different subsystems +(such as LIBUTIL). + +The following functions are guarenteed: + +ftruncate +strlcpy +strlcat +mktime +rename +innetgr +initgroups +memmove +strdup +inet_ntoa +setlinebuf +vsyslog +timegm +setenv +strtoull +strtoll +strndup +strnlen +waitpid +seteuid +setegid +asprintf +snprintf +vasprintf +vsnprintf +opendir +readdir +telldir +seekdir +closedir +dlopen +dlclose +dlsym +dlerror +chroot +bzero +strerror +errno +mkstemp (a secure one!) + +Prerequisites: +memset (for bzero) +syslog (for vsyslog) +setnetgrent, getnetgrent, endnetgrent (for innetgr) +mktemp (for mkstemp) diff --git a/source4/lib/replace/SConscript b/source4/lib/replace/SConscript index 84088db162..982120e1bf 100644 --- a/source4/lib/replace/SConscript +++ b/source4/lib/replace/SConscript @@ -9,4 +9,4 @@ if hostenv['configure']: conf.Finish() hostenv.StaticLibrary('repdir', ['repdir/repdir.c']) -hostenv.StaticLibrary('replace', ['replace.c', 'snprintf.c']) +hostenv.StaticLibrary('replace', ['replace.c', 'snprintf.c','dlfcn.c']) diff --git a/source4/lib/replace/config.m4 b/source4/lib/replace/config.m4 index 4dfeb5647a..88e3be5e94 100644 --- a/source4/lib/replace/config.m4 +++ b/source4/lib/replace/config.m4 @@ -15,5 +15,87 @@ if test x"$samba_cv_REPLACE_INET_NTOA" = x"yes"; then AC_DEFINE(REPLACE_INET_NTOA,1,[Whether inet_ntoa should be replaced]) fi +dnl Provided by replace.c: +AC_CHECK_HEADERS(sys/syslog.h syslog.h) AC_CHECK_FUNCS(strtoull __strtoull strtouq strtoll __strtoll strtoq) -AC_CHECK_FUNCS(seteuid setresuid setegid setresgid) +AC_CHECK_FUNCS(seteuid setresuid setegid setresgid chroot bzero strerror) +AC_CHECK_FUNCS(timegm setenv vsyslog setlinebuf mktime ftruncate chsize rename) +AC_CHECK_FUNCS(waitpid strnlen strlcpy strlcat innetgr initgroups memmove strdup) +AC_HAVE_DECL(setresuid, [#include ]) +AC_HAVE_DECL(setresgid, [#include ]) +AC_HAVE_DECL(errno, [#include ]) + +AC_CACHE_CHECK([for secure mkstemp],samba_cv_HAVE_SECURE_MKSTEMP,[ +AC_TRY_RUN([#include +#include +#include +#include +main() { + struct stat st; + char tpl[20]="/tmp/test.XXXXXX"; + int fd = mkstemp(tpl); + if (fd == -1) exit(1); + unlink(tpl); + if (fstat(fd, &st) != 0) exit(1); + if ((st.st_mode & 0777) != 0600) exit(1); + exit(0); +}], +samba_cv_HAVE_SECURE_MKSTEMP=yes, +samba_cv_HAVE_SECURE_MKSTEMP=no, +samba_cv_HAVE_SECURE_MKSTEMP=cross)]) +if test x"$samba_cv_HAVE_SECURE_MKSTEMP" = x"yes"; then + AC_DEFINE(HAVE_SECURE_MKSTEMP,1,[Whether mkstemp is secure]) +fi + +dnl Provided by snprintf.c: +AC_HAVE_DECL(asprintf, [#include ]) +AC_HAVE_DECL(vasprintf, [#include ]) +AC_HAVE_DECL(vsnprintf, [#include ]) +AC_HAVE_DECL(snprintf, [#include ]) +AC_CHECK_FUNCS(snprintf vsnprintf asprintf vasprintf) + +AC_CACHE_CHECK([for C99 vsnprintf],samba_cv_HAVE_C99_VSNPRINTF,[ +AC_TRY_RUN([ +#include +#include +void foo(const char *format, ...) { + va_list ap; + int len; + char buf[20]; + long long l = 1234567890; + l *= 100; + + va_start(ap, format); + len = vsnprintf(buf, 0, format, ap); + va_end(ap); + if (len != 5) exit(1); + + va_start(ap, format); + len = vsnprintf(0, 0, format, ap); + va_end(ap); + if (len != 5) exit(1); + + if (snprintf(buf, 3, "hello") != 5 || strcmp(buf, "he") != 0) exit(1); + + if (snprintf(buf, 20, "%lld", l) != 12 || strcmp(buf, "123456789000") != 0) exit(1); + + exit(0); +} +main() { foo("hello"); } +], +samba_cv_HAVE_C99_VSNPRINTF=yes,samba_cv_HAVE_C99_VSNPRINTF=no,samba_cv_HAVE_C99_VSNPRINTF=cross)]) +if test x"$samba_cv_HAVE_C99_VSNPRINTF" = x"yes"; then + AC_DEFINE(HAVE_C99_VSNPRINTF,1,[Whether there is a C99 compliant vsnprintf]) +fi + +dnl Provided by dlfcn.c: +AC_SEARCH_LIBS_EXT(dlopen, [dl], DL_LIBS) +SMB_EXT_LIB(DL,[${DL_LIBS}],[${DL_CFLAGS}],[${DL_CPPFLAGS}],[${DL_LDFLAGS}]) +SAVE_LIBS="$LIBS" +LIBS="$LIBS $DL_LIBS" +AC_CHECK_HEADERS(dlfcn.h) +AC_CHECK_FUNCS(dlopen dlsym dlerror dlclose) +LIBS="$SAVE_LIBS" + +AC_CHECK_FUNCS([syslog memset setnetgrent getnetgrent endnetgrent],, + [AC_MSG_ERROR([Need syslog and memset])]) diff --git a/source4/lib/replace/config.mk b/source4/lib/replace/config.mk index 16952ea32d..2658f0e96a 100644 --- a/source4/lib/replace/config.mk +++ b/source4/lib/replace/config.mk @@ -13,7 +13,9 @@ NOPROTO = YES [SUBSYSTEM::LIBREPLACE] INIT_OBJ_FILES = lib/replace/replace.o ADD_OBJ_FILES = \ - lib/replace/snprintf.o + lib/replace/snprintf.o \ + lib/replace/dlfcn.o +NOPROTO = YES REQUIRED_SUBSYSTEMS = REPLACE_READDIR # End SUBSYSTEM LIBREPLACE ############################## diff --git a/source4/lib/replace/dlfcn.c b/source4/lib/replace/dlfcn.c new file mode 100644 index 0000000000..7a9e7230de --- /dev/null +++ b/source4/lib/replace/dlfcn.c @@ -0,0 +1,71 @@ +/* + Unix SMB/CIFS implementation. + Samba system utilities + Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Jeremy Allison 1998-2002 + + This program 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 2 of the License, or + (at your option) any later version. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* + Unix SMB/CIFS implementation. + Samba system utilities + Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Jeremy Allison 1998-2002 + + This program 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 2 of the License, or + (at your option) any later version. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +#ifndef HAVE_DLOPEN +void *dlopen(const char *name, int flags) +{ + return NULL; +} +#endif + +#ifndef HAVE_DLSYM +void *dlsym(void *handle, const char *symbol) +{ + return NULL; +} +#endif + +#ifndef HAVE_DLERROR +const char *dlerror(void) +{ + return "dynamic loading of objects not supported on this platform"; +} +#endif + +#ifndef HAVE_DLCLOSE +int dlclose(void *handle) +{ + return 0; +} +#endif diff --git a/source4/lib/replace/replace.c b/source4/lib/replace/replace.c index ef8e053257..aa79f23fd0 100644 --- a/source4/lib/replace/replace.c +++ b/source4/lib/replace/replace.c @@ -361,39 +361,6 @@ duplicate a string #endif /* HAVE_SYSLOG */ #endif /* HAVE_VSYSLOG */ -/******************************************************************* -yield the difference between *A and *B, in seconds, ignoring leap seconds -********************************************************************/ -static int tm_diff(struct tm *a, struct tm *b) -{ - int ay = a->tm_year + (1900 - 1); - int by = b->tm_year + (1900 - 1); - int intervening_leap_days = - (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400); - int years = ay - by; - int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday); - int hours = 24*days + (a->tm_hour - b->tm_hour); - int minutes = 60*hours + (a->tm_min - b->tm_min); - int seconds = 60*minutes + (a->tm_sec - b->tm_sec); - - return seconds; -} - -/******************************************************************* - return the UTC offset in seconds west of UTC, or 0 if it cannot be determined - ******************************************************************/ -int get_time_zone(time_t t) -{ - struct tm *tm = gmtime(&t); - struct tm tm_utc; - if (!tm) - return 0; - tm_utc = *tm; - tm = localtime(&t); - if (!tm) - return 0; - return tm_diff(&tm_utc,tm); -} #ifndef HAVE_TIMEGM /* @@ -496,14 +463,12 @@ int get_time_zone(time_t t) } #endif -int sys_waitpid(pid_t pid,int *status,int options) +#ifndef HAVE_WAITPID +int waitpid(pid_t pid,int *status,int options) { -#ifdef HAVE_WAITPID - return waitpid(pid,status,options); -#else /* USE_WAITPID */ return wait4(pid, status, options, NULL); -#endif /* USE_WAITPID */ } +#endif #ifndef HAVE_SETEUID int seteuid(uid_t euid) @@ -526,3 +491,35 @@ int sys_waitpid(pid_t pid,int *status,int options) #endif } #endif + +/******************************************************************* +os/2 also doesn't have chroot +********************************************************************/ +#ifndef HAVE_CHROOT +int chroot(const char *dname) +{ + static int done; + if (!done) { + DEBUG(1,("WARNING: no chroot!\n")); + done=1; + } + errno = ENOSYS; + return -1; +} +#endif + +/***************************************************************** + Possibly replace mkstemp if it is broken. +*****************************************************************/ + +#ifndef HAVE_SECURE_MKSTEMP +int rep_mkstemp(char *template) +{ + /* have a reasonable go at emulating it. Hope that + the system mktemp() isn't completly hopeless */ + char *p = mktemp(template); + if (!p) + return -1; + return open(p, O_CREAT|O_EXCL|O_RDWR, 0600); +} +#endif diff --git a/source4/lib/replace/replace.h b/source4/lib/replace/replace.h index f85c8de634..f906e7a2f5 100644 --- a/source4/lib/replace/replace.h +++ b/source4/lib/replace/replace.h @@ -23,6 +23,10 @@ #ifndef _replace_h #define _replace_h +#if defined(_MSC_VER) || defined(__MINGW32__) +#include "lib/replace/win32/replace.h" +#endif + #ifdef __COMPAR_FN_T #define QSORT_CAST (__compar_fn_t) #endif @@ -31,6 +35,15 @@ #define QSORT_CAST (int (*)(const void *, const void *)) #endif +#ifndef HAVE_STRERROR +extern char *sys_errlist[]; +#define strerror(i) sys_errlist[i] +#endif + +#ifndef HAVE_ERRNO_DECL +extern int errno; +#endif + #ifndef HAVE_STRDUP char *strdup(const char *s); #endif @@ -136,4 +149,13 @@ typedef unsigned char u_int8_t; #define HAVE_SOCKLEN_T 1 #endif +#ifdef HAVE_DLFCN_H +#include +#endif + +#ifndef HAVE_SECURE_MKSTEMP +#define mkstemp(path) rep_mkstemp(path) +int rep_mkstemp(char *temp); +#endif + #endif diff --git a/source4/lib/signal.c b/source4/lib/signal.c index 66d3e1f76e..6c0bb4007a 100644 --- a/source4/lib/signal.c +++ b/source4/lib/signal.c @@ -28,7 +28,7 @@ static void sig_cld(int signum) { - while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0) + while (waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0) ; /* diff --git a/source4/lib/system.c b/source4/lib/system.c index eb3799c7ca..629b2b07ff 100644 --- a/source4/lib/system.c +++ b/source4/lib/system.c @@ -23,7 +23,6 @@ #include "system/network.h" #include "system/wait.h" #include "system/filesys.h" -#include "pstring.h" /* The idea is that this file will eventually have wrappers around all @@ -77,20 +76,6 @@ int sys_usleep(long usecs) } -/******************************************************************* - System wrapper for getwd -********************************************************************/ -char *sys_getwd(char *s) -{ - char *wd; -#ifdef HAVE_GETCWD - wd = (char *)getcwd(s, sizeof (pstring)); -#else - wd = (char *)getwd(s); -#endif - return wd; -} - /******************************************************************* A read wrapper that will deal with EINTR. ********************************************************************/ @@ -121,24 +106,6 @@ ssize_t sys_write(int fd, const void *buf, size_t count) -/******************************************************************* -os/2 also doesn't have chroot -********************************************************************/ -int sys_chroot(const char *dname) -{ -#ifndef HAVE_CHROOT - static int done; - if (!done) { - DEBUG(1,("WARNING: no chroot!\n")); - done=1; - } - errno = ENOSYS; - return -1; -#else - return(chroot(dname)); -#endif -} - /************************************************************************** A wrapper for gethostbyname() that tries avoids looking up hostnames in the root domain, which can cause dial-on-demand links to come up for no @@ -181,39 +148,6 @@ struct hostent *sys_gethostbyname(const char *name) #endif /* REDUCE_ROOT_DNS_LOOKUPS */ } - - -/************************************************************************** - Wrappers for dlopen, dlsym, dlclose. -****************************************************************************/ - -void *sys_dlopen(const char *name, int flags) -{ -#if defined(HAVE_DLOPEN) - return dlopen(name, flags); -#else - return NULL; -#endif -} - -void *sys_dlsym(void *handle, const char *symbol) -{ -#if defined(HAVE_DLSYM) - return dlsym(handle, symbol); -#else - return NULL; -#endif -} - -const char *sys_dlerror(void) -{ -#if defined(HAVE_DLERROR) - return dlerror(); -#else - return NULL; -#endif -} - const char *sys_inet_ntoa(struct ipv4_addr in) { struct in_addr in2; @@ -221,11 +155,6 @@ const char *sys_inet_ntoa(struct ipv4_addr in) return inet_ntoa(in2); } -uint32_t sys_inet_addr(const char *s) -{ - return inet_addr(s); -} - struct ipv4_addr sys_inet_makeaddr(int net, int host) { struct in_addr in; diff --git a/source4/lib/time.c b/source4/lib/time.c index 61e92b8c00..5de9046c8d 100644 --- a/source4/lib/time.c +++ b/source4/lib/time.c @@ -594,3 +594,37 @@ NTTIME timeval_to_nttime(const struct timeval *tv) return 10*(tv->tv_usec + ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000)); } + +/******************************************************************* +yield the difference between *A and *B, in seconds, ignoring leap seconds +********************************************************************/ +static int tm_diff(struct tm *a, struct tm *b) +{ + int ay = a->tm_year + (1900 - 1); + int by = b->tm_year + (1900 - 1); + int intervening_leap_days = + (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400); + int years = ay - by; + int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday); + int hours = 24*days + (a->tm_hour - b->tm_hour); + int minutes = 60*hours + (a->tm_min - b->tm_min); + int seconds = 60*minutes + (a->tm_sec - b->tm_sec); + + return seconds; +} + +/******************************************************************* + return the UTC offset in seconds west of UTC, or 0 if it cannot be determined + ******************************************************************/ +int get_time_zone(time_t t) +{ + struct tm *tm = gmtime(&t); + struct tm tm_utc; + if (!tm) + return 0; + tm_utc = *tm; + tm = localtime(&t); + if (!tm) + return 0; + return tm_diff(&tm_utc,tm); +} diff --git a/source4/lib/util.c b/source4/lib/util.c index a1d6f85eb9..0733103e93 100644 --- a/source4/lib/util.c +++ b/source4/lib/util.c @@ -347,7 +347,7 @@ uint32_t interpret_addr(const char *str) /* if it's in the form of an IP address then get the lib to interpret it */ if (is_ipaddress(str)) { - res = sys_inet_addr(str); + res = inet_addr(str); } else { /* otherwise assume it's a network name of some sort and use sys_gethostbyname */ @@ -518,24 +518,6 @@ void dump_data(int level, const uint8_t *buf,int len) } } -/***************************************************************** - Possibly replace mkstemp if it is broken. -*****************************************************************/ - -int smb_mkstemp(char *template) -{ -#if HAVE_SECURE_MKSTEMP - return mkstemp(template); -#else - /* have a reasonable go at emulating it. Hope that - the system mktemp() isn't completly hopeless */ - char *p = mktemp(template); - if (!p) - return -1; - return open(p, O_CREAT|O_EXCL|O_RDWR, 0600); -#endif -} - /***************************************************************** malloc that aborts with smb_panic on fail or zero size. *****************************************************************/ -- cgit