diff --git a/configure.ac b/configure.ac index 53472b3..9f3ea82 100644 --- a/configure.ac +++ b/configure.ac @@ -1,626 +1,626 @@ # configure.ac for libgpg-error # Copyright (C) 2003, 2004, 2006, 2010, 2013-2017 g10 Code GmbH # # This file is part of libgpg-error. # # libgpg-error is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # libgpg-error 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 Lesser General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . # SPDX-License-Identifier: LGPL-2.1+ # (Process this file with autoconf to produce a configure script.) # The following lines are used by ./autogen.sh. AC_PREREQ(2.59) min_automake_version="1.14" # To build a release you need to create a tag with the version number # (git tag -s libgpg-error-n.m) and run "./autogen.sh --force". # Please bump the version number immediately after the release, do # another commit, and a push so that the git magic is able to work. # See below for the LT versions. m4_define([mym4_package],[libgpg-error]) m4_define([mym4_major], [1]) m4_define([mym4_minor], [28]) # Below is m4 magic to extract and compute the revision number, the # decimalized short revision number, a beta version string, and a flag # indicating a development version (mym4_isgit). Note that the m4 # processing is done by autoconf and not during the configure run. m4_define([mym4_verslist], m4_split(m4_esyscmd([./autogen.sh --find-version] \ mym4_package mym4_major mym4_minor),[:])) m4_define([mym4_isbeta], m4_argn(2, mym4_verslist)) m4_define([mym4_version], m4_argn(4, mym4_verslist)) m4_define([mym4_revision], m4_argn(7, mym4_verslist)) m4_define([mym4_revision_dec], m4_argn(8, mym4_verslist)) m4_esyscmd([echo ]mym4_version[>VERSION]) AC_INIT([mym4_package],[mym4_version], [https://bugs.gnupg.org]) # LT Version numbers, remember to change them just *before* a release. # (Code changed: REVISION++) # (Interfaces added/removed/changed: CURRENT++, REVISION=0) # (Interfaces added: AGE++) # (Interfaces removed: AGE=0) # Note that added error codes don't constitute an interface change. LIBGPG_ERROR_LT_CURRENT=22 LIBGPG_ERROR_LT_AGE=22 LIBGPG_ERROR_LT_REVISION=0 ################################################ AC_SUBST(LIBGPG_ERROR_LT_CURRENT) AC_SUBST(LIBGPG_ERROR_LT_AGE) AC_SUBST(LIBGPG_ERROR_LT_REVISION) VERSION_NUMBER=m4_esyscmd(printf "0x%02x%02x00" mym4_major mym4_minor) AC_SUBST(VERSION_NUMBER) AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE([serial-tests dist-bzip2]) AM_MAINTAINER_MODE AC_CONFIG_SRCDIR([src/err-sources.h.in]) AC_CONFIG_HEADER([config.h]) AC_CONFIG_MACRO_DIR([m4]) AM_SILENT_RULES # We need to know about the host architecture to avoid spurious # warnings. AC_CANONICAL_HOST AB_INIT # Checks for programs. AC_PROG_CC AM_PROG_CC_C_O AC_PROG_CPP AC_PROG_AWK AC_CHECK_TOOL(AR, ar, :) AC_GNU_SOURCE # Set some variables depending on the platform for later use. have_w32_system=no have_w64_system=no have_w32ce_system=no case "${host}" in x86_64-*mingw32*) have_w32_system=yes have_w64_system=yes ;; *-mingw32ce*) have_w32_system=yes have_w32ce_system=yes ;; *-mingw32*) have_w32_system=yes ;; *-apple-darwin*) # This is the equivalent of the _GNU_SOURCE feature-test-macro # on GNU libc systems. AC_DEFINE(_DARWIN_C_SOURCE, 900000L, Expose all libc features (__DARWIN_C_FULL).) ;; *) ;; esac if test "$have_w32_system" != yes; then gl_THREADLIB_EARLY fi # We build libgpg-error with large file support so that we have a 64 # bit off_t. Our external interface uses the gpgrt_off_t which is # anyway specified as 64 bit. Thus the same libgpg-error can be used # by software which is not build with large file support. AC_SYS_LARGEFILE LT_PREREQ([2.2.6]) LT_INIT([win32-dll disable-static]) LT_LANG([Windows Resource]) # We need to compile and run a program on the build machine. dnl The AC_PROG_CC_FOR_BUILD macro in the AC archive is broken for dnl autoconf 2.57. dnl AC_PROG_CC_FOR_BUILD AC_MSG_CHECKING(for cc for build) if test "$cross_compiling" = "yes"; then CC_FOR_BUILD="${CC_FOR_BUILD-cc}" else CC_FOR_BUILD="${CC_FOR_BUILD-$CC}" fi AC_MSG_RESULT($CC_FOR_BUILD) AC_ARG_VAR(CC_FOR_BUILD,[build system C compiler]) AH_BOTTOM([ /* Force using of NLS for W32 even if no libintl has been found. This is okay because we have our own gettext implementation for W32. */ #if defined(HAVE_W32_SYSTEM) && !defined(ENABLE_NLS) #define ENABLE_NLS 1 #endif /* Connect the generic estream-printf.c to our framework. */ #define _ESTREAM_PRINTF_REALLOC _gpgrt_realloc #define _ESTREAM_PRINTF_EXTRA_INCLUDE "gpgrt-int.h" /* For building we need to define these macro. */ #define GPG_ERR_ENABLE_GETTEXT_MACROS 1 #define GPG_ERR_ENABLE_ERRNO_MACROS 1 #define GPGRT_ENABLE_ES_MACROS 1 ]) # Note, that autogen.sh greps for the next line. AM_GNU_GETTEXT_VERSION([0.19.3]) AM_GNU_GETTEXT([external]) # Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS([stdlib.h locale.h stdint.h sys/select.h sys/time.h]) AC_FUNC_STRERROR_R case "${host_os}" in solaris*) # All versions of Solaris from 2.4 have a thread-safe strerror(). # Since Solaris 10, in addition strerror_r() exists. ;; *) AC_CHECK_FUNC([strerror_r], [], AC_MSG_WARN([[Without strerror_r, gpg_strerror_r might not be thread-safe]])) ;; esac -AC_CHECK_FUNCS([flockfile vasprintf mmap rand strlwr]) +AC_CHECK_FUNCS([flockfile vasprintf mmap rand strlwr stpcpy setenv]) # # Checks for typedefs, structures, and compiler characteristics. # AC_C_CONST AC_CHECK_SIZEOF(int) AC_CHECK_SIZEOF(long) AC_CHECK_SIZEOF(long long) AC_HEADER_TIME AC_CHECK_SIZEOF(time_t,,[[ #include #if TIME_WITH_SYS_TIME # include # include #else # if HAVE_SYS_TIME_H # include # else # include # endif #endif ]]) # Find a 64 bit integer type to be used instead of off_t. We prefer # the standard integer types over int64_t and finally try long long. if test "$ac_cv_sizeof_int" = "8"; then replacement_for_off_t="int" elif test "$ac_cv_sizeof_long" = "8"; then replacement_for_off_t="long" elif test "$ac_cv_header_stdint_h" = yes; then replacement_for_off_t="int64_t" elif test "$ac_cv_sizeof_long_long" = "8"; then replacement_for_off_t="long long" else AC_MSG_ERROR([[ *** *** No 64 bit signed integer type found. Can't build this library. ***]]) fi AC_DEFINE_UNQUOTED(REPLACEMENT_FOR_OFF_T, "$replacement_for_off_t", [Used by mkheader to insert the replacement type.]) # # Setup gcc specific options # AC_MSG_NOTICE([checking for cc features]) if test "$GCC" = yes; then # Check whether gcc does not emit a diagnostic for unknown -Wno-* # options. This is the case for gcc >= 4.6 AC_MSG_CHECKING([if gcc ignores unknown -Wno-* options]) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6 ) #kickerror #endif]],[])],[_gcc_silent_wno=yes],[_gcc_silent_wno=no]) AC_MSG_RESULT($_gcc_silent_wno) # Note that it is okay to use CFLAGS here because these are just # warning options and the user should have a chance of overriding # them. if test "$USE_MAINTAINER_MODE" = "yes"; then M_CFLAGS="-O3 -Wall -Wcast-align -Wshadow -Wstrict-prototypes" M_CFLAGS="$M_CFLAGS -Wformat -Wno-format-y2k -Wformat-security" if test x"$_gcc_silent_wno" = xyes ; then _gcc_wopt=yes else AC_MSG_CHECKING([if gcc supports -Wno-missing-field-initializers]) _gcc_cflags_save=$CFLAGS CFLAGS="-Wno-missing-field-initializers" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])], [_gcc_wopt=yes],[_gcc_wopt=no]) AC_MSG_RESULT($_gcc_wopt) CFLAGS=$_gcc_cflags_save; fi if test x"$_gcc_wopt" = xyes ; then M_CFLAGS="$M_CFLAGS -W -Wno-sign-compare -Wno-missing-field-initializers" fi AC_MSG_CHECKING([if gcc supports -Wdeclaration-after-statement]) _gcc_cflags_save=$CFLAGS CFLAGS="-Wdeclaration-after-statement" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])],_gcc_wopt=yes,_gcc_wopt=no) AC_MSG_RESULT($_gcc_wopt) CFLAGS=$_gcc_cflags_save; if test x"$_gcc_wopt" = xyes ; then M_CFLAGS="$M_CFLAGS -Wdeclaration-after-statement" fi # Prepend the maintainer-cflags so that the user can override # them, e.g. to override the optimization flags for debugging. CFLAGS="$M_CFLAGS $CFLAGS" else CFLAGS="$CFLAGS -Wall" fi AC_MSG_CHECKING([if gcc supports -Wpointer-arith]) _gcc_cflags_save=$CFLAGS CFLAGS="-Wpointer-arith" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])],_gcc_psign=yes,_gcc_psign=no) AC_MSG_RESULT($_gcc_psign) CFLAGS=$_gcc_cflags_save; if test x"$_gcc_psign" = xyes ; then CFLAGS="$CFLAGS -Wpointer-arith" fi # The undocumented option -Wno-psabi suppresses the annoying # "the ABI of passing union with long double has changed in GCC 4.4" # which is emitted in estream-printf.c but entirely irrelvant # because that union is local to the file. if test x"$_gcc_silent_wno" = xyes ; then CFLAGS="$CFLAGS -Wno-psabi" fi fi # # Check whether the compiler supports the GCC style aligned attribute # AC_CACHE_CHECK([whether the GCC style aligned attribute is supported], [gcry_cv_gcc_attribute_aligned], [gcry_cv_gcc_attribute_aligned=no AC_COMPILE_IFELSE([AC_LANG_SOURCE( [[struct { int a; } foo __attribute__ ((aligned (16)));]])], [gcry_cv_gcc_attribute_aligned=yes])]) if test "$gcry_cv_gcc_attribute_aligned" = "yes" ; then AC_DEFINE(HAVE_GCC_ATTRIBUTE_ALIGNED,1, [Defined if a GCC style "__attribute__ ((aligned (n))" is supported]) fi # # Check for ELF visibility support. # AC_CACHE_CHECK(whether the visibility attribute is supported, gcry_cv_visibility_attribute, [gcry_cv_visibility_attribute=no AC_LANG_CONFTEST([AC_LANG_SOURCE( [[int foo __attribute__ ((visibility ("hidden"))) = 1; int bar __attribute__ ((visibility ("protected"))) = 1; ]])]) if ${CC-cc} -Werror -S conftest.c -o conftest.s \ 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD ; then if grep '\.hidden.*foo' conftest.s >/dev/null 2>&1 ; then if grep '\.protected.*bar' conftest.s >/dev/null 2>&1; then gcry_cv_visibility_attribute=yes fi fi fi ]) if test "$gcry_cv_visibility_attribute" = "yes"; then AC_CACHE_CHECK(for broken visibility attribute, gcry_cv_broken_visibility_attribute, [gcry_cv_broken_visibility_attribute=yes AC_LANG_CONFTEST([AC_LANG_SOURCE( [[int foo (int x); int bar (int x) __asm__ ("foo") __attribute__ ((visibility ("hidden"))); int bar (int x) { return x; } ]])]) if ${CC-cc} -Werror -S conftest.c -o conftest.s \ 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD ; then if grep '\.hidden@<:@ _@:>@foo' conftest.s >/dev/null 2>&1; then gcry_cv_broken_visibility_attribute=no fi fi ]) fi if test "$gcry_cv_visibility_attribute" = "yes"; then AC_CACHE_CHECK(for broken alias attribute, gcry_cv_broken_alias_attribute, [gcry_cv_broken_alias_attribute=yes AC_LANG_CONFTEST([AC_LANG_SOURCE( [[extern int foo (int x) __asm ("xyzzy"); int bar (int x) { return x; } extern __typeof (bar) foo __attribute ((weak, alias ("bar"))); extern int dfoo; extern __typeof (dfoo) dfoo __asm ("abccb"); int dfoo = 1; ]])]) if ${CC-cc} -Werror -S conftest.c -o conftest.s \ 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD ; then if grep 'xyzzy' conftest.s >/dev/null 2>&1 && \ grep 'abccb' conftest.s >/dev/null 2>&1; then gcry_cv_broken_alias_attribute=no fi fi ]) fi if test "$gcry_cv_visibility_attribute" = "yes"; then AC_CACHE_CHECK(if gcc supports -fvisibility=hidden, gcry_cv_gcc_has_f_visibility, [gcry_cv_gcc_has_f_visibility=no _gcc_cflags_save=$CFLAGS CFLAGS="-fvisibility=hidden" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])], gcry_cv_gcc_has_f_visibility=yes) CFLAGS=$_gcc_cflags_save; ]) fi if test "$gcry_cv_visibility_attribute" = "yes" \ && test "$gcry_cv_broken_visibility_attribute" != "yes" \ && test "$gcry_cv_broken_alias_attribute" != "yes" \ && test "$gcry_cv_gcc_has_f_visibility" = "yes" then AC_DEFINE(GPGRT_USE_VISIBILITY, 1, [Define to use the GNU C visibility attribute.]) CFLAGS="$CFLAGS -fvisibility=hidden" fi # # Check whether ld supports a version script. # (Actually not a check but a list of systems which are known to support it.) # have_ld_version_script=no case "${host}" in *-*-linux*) have_ld_version_script=yes ;; *-*-gnu*) have_ld_version_script=yes ;; esac AC_ARG_ENABLE([ld-version-script], AC_HELP_STRING([--enable-ld-version-script], [enable/disable use of linker version script. (default is system dependent)]), [have_ld_version_script=$enableval], [ : ] ) AM_CONDITIONAL(HAVE_LD_VERSION_SCRIPT, test "$have_ld_version_script" = "yes") # # Check for thread library. # # Windows has always thread support; thus we don't bother to test for # it as it may lead to false results when cross building. if test "$have_w32_system" = yes; then AC_DEFINE([USE_WINDOWS_THREADS], [1]) LIBTHREAD= LTLIBTHREAD= LIBMULTITHREAD= LTLIBMULTITHREAD= THREADLIB_CPPFLAGS="" AC_SUBST([LIBTHREAD]) AC_SUBST([LTLIBTHREAD]) AC_SUBST([LIBMULTITHREAD]) AC_SUBST([LTLIBMULTITHREAD]) else gl_LOCK if test "$gl_threads_api" = posix; then AC_CHECK_SIZEOF(pthread_mutex_t,,[AC_INCLUDES_DEFAULT #include ]) fi fi # Default value for GPG_ERROR_CONFIG_LIBS config_libs="-lgpg-error" # # Check for other libraries # LIB_SCHED_YIELD= AC_SUBST([LIB_SCHED_YIELD]) AC_SEARCH_LIBS([sched_yield], [rt posix4], [if test "$ac_cv_search_sched_yield" != "none required"; then LIB_SCHED_YIELD=$ac_cv_search_sched_yield config_libs="$config_libs $LIB_SCHED_YIELD" fi]) # Check for optional readline support GNUPG_CHECK_READLINE # # Prepare building of estream # estream_INIT # # Substitution used for gpg-error-config # GPG_ERROR_CONFIG_LIBS="$config_libs" if test "x$LIBTHREAD" != x; then GPG_ERROR_CONFIG_LIBS="${GPG_ERROR_CONFIG_LIBS} ${LIBTHREAD}" fi if test "x$LIBMULTITHREAD" != x; then GPG_ERROR_CONFIG_MT_LIBS="${LIBMULTITHREAD}" else GPG_ERROR_CONFIG_MT_LIBS="" fi GPG_ERROR_CONFIG_CFLAGS="" if test "x$THREADLIB_CPPFLAGS" != x; then GPG_ERROR_CONFIG_MT_CFLAGS="${THREADLIB_CPPFLAGS}" else GPG_ERROR_CONFIG_MT_CFLAGS="" fi GPG_ERROR_CONFIG_ISUBDIRAFTER="" GPG_ERROR_CONFIG_HOST="$host" AC_SUBST(GPG_ERROR_CONFIG_LIBS) AC_SUBST(GPG_ERROR_CONFIG_CFLAGS) AC_SUBST(GPG_ERROR_CONFIG_MT_LIBS) AC_SUBST(GPG_ERROR_CONFIG_MT_CFLAGS) AC_SUBST(GPG_ERROR_CONFIG_ISUBDIRAFTER) AC_SUBST(GPG_ERROR_CONFIG_HOST) # # Special defines for certain platforms # force_use_syscfg=no if test "$have_w32_system" = yes; then AC_DEFINE(HAVE_W32_SYSTEM,1,[Defined if we run on a W32 API based system]) if test "$have_w64_system" = yes; then AC_DEFINE(HAVE_W64_SYSTEM,1,[Defined if we run on 64 bit W32 API system]) fi if test "$have_w32ce_system" = yes; then AC_DEFINE(HAVE_W32CE_SYSTEM,1,[Defined if we run on WindowsCE]) GPG_ERROR_CONFIG_ISUBDIRAFTER="gpg-extra" fi force_use_syscfg=yes fi if test x$cross_compiling = xyes; then force_use_syscfg=yes fi AM_CONDITIONAL(HAVE_W32_SYSTEM, test "$have_w32_system" = yes) AM_CONDITIONAL(HAVE_W64_SYSTEM, test "$have_w64_system" = yes) AM_CONDITIONAL(HAVE_W32CE_SYSTEM, test "$have_w32ce_system" = yes) AM_CONDITIONAL(CROSS_COMPILING, test x$cross_compiling = xyes) AM_CONDITIONAL(FORCE_USE_SYSCFG, test x$force_use_syscfg = xyes) AC_DEFINE_UNQUOTED(HOST_TRIPLET_STRING, "$host", [The host triplet]) # # gpgrt_log_clock may require linking with extra libaries. As long as # we don't have a good test for this we require the use of this # configure option to enabling printing of a timestamp. # AC_MSG_CHECKING([whether to enable log_clock]) AC_ARG_ENABLE(log_clock, AC_HELP_STRING([--enable-log-clock], [enable log_clock timestamps]), enable_log_clock=$enableval, enable_log_clock=no) AC_MSG_RESULT($enable_log_clock) if test "$enable_log_clock" = yes ; then AC_DEFINE(ENABLE_LOG_CLOCK,1,[Defined to use log_clock timestamps]) fi # # Add -Werror to CFLAGS. This hack can be used to avoid problems with # misbehaving autoconf tests in case the user supplied -Werror. # AC_ARG_ENABLE(werror, AC_HELP_STRING([--enable-werror], [append -Werror to CFLAGS]), [if test $enableval = yes ; then CFLAGS="$CFLAGS -Werror" fi]) # # Provide information about the build. # BUILD_REVISION="mym4_revision" AC_SUBST(BUILD_REVISION) AC_DEFINE_UNQUOTED(BUILD_REVISION, "$BUILD_REVISION", [GIT commit id revision used to build this package]) changequote(,)dnl BUILD_VERSION=`echo "$PACKAGE_VERSION"|sed 's/\([0-9.]*\).*/\1./'` changequote([,])dnl BUILD_VERSION="${BUILD_VERSION}0.mym4_revision_dec" BUILD_FILEVERSION=`echo "${BUILD_VERSION}" | tr . ,` AC_SUBST(BUILD_VERSION) AC_SUBST(BUILD_FILEVERSION) AC_ARG_ENABLE([build-timestamp], AC_HELP_STRING([--enable-build-timestamp], [set an explicit build timestamp for reproducibility. (default is the current time in ISO-8601 format)]), [if test "$enableval" = "yes"; then BUILD_TIMESTAMP=`date -u +%Y-%m-%dT%H:%M+0000 2>/dev/null || date` else BUILD_TIMESTAMP="$enableval" fi], [BUILD_TIMESTAMP=""]) AC_SUBST(BUILD_TIMESTAMP) AC_DEFINE_UNQUOTED(BUILD_TIMESTAMP, "$BUILD_TIMESTAMP", [The time this package was configured for a build]) AC_ARG_ENABLE(languages, AC_HELP_STRING([--disable-languages], [do not build support for other languages than C])) AM_CONDITIONAL([LANGUAGES_SOME], [test "x$enable_languages" != xno]) build_doc=yes AC_ARG_ENABLE([doc], AC_HELP_STRING([--disable-doc], [do not build the documentation]), build_doc=$enableval, build_doc=yes) AM_CONDITIONAL([BUILD_DOC], [test "x$build_doc" != xno]) build_tests=yes AC_ARG_ENABLE([tests], AC_HELP_STRING([--disable-tests], [do not build the tests]), build_tests=$enableval, build_tests=yes) AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != xno]) # # Substitution # AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([doc/Makefile po/Makefile.in m4/Makefile]) AC_CONFIG_FILES([src/Makefile tests/Makefile]) AC_CONFIG_FILES([lang/Makefile lang/cl/Makefile lang/cl/gpg-error.asd]) AC_CONFIG_FILES([src/versioninfo.rc src/gpg-error.w32-manifest]) AC_CONFIG_FILES([src/gpg-error-config], [chmod +x src/gpg-error-config]) AC_OUTPUT tmp= if test "$have_w32_system" != yes; then if test x"$gl_use_threads" = xno; then tmp=" NO-THREADS" fi fi echo " $PACKAGE_NAME v$PACKAGE_VERSION has been configured as follows: Revision: mym4_revision (mym4_revision_dec) Platform: $host$tmp " if test "$gcry_cv_gcc_attribute_aligned" != "yes" ; then cat <. # SPDX-License-Identifier: LGPL-2.1+ # # We distribute the generated sources err-sources.h and err-codes.h, # because they are needed to build the po directory, and they don't # depend on the configuration anyway. # if HAVE_W32CE_SYSTEM gpg_extra_headers = gpg-extra/errno.h extra_cppflags = -idirafter gpg-extra else gpg_extra_headers = extra_cppflags = endif localedir = $(datadir)/locale bin_PROGRAMS = gpg-error if HAVE_W32_SYSTEM noinst_PROGRAMS = gen-w32-lock-obj else noinst_PROGRAMS = gen-posix-lock-obj endif # Distributed lock object definitions for cross compilation. lock_obj_pub = \ syscfg/lock-obj-pub.aarch64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.aarch64-apple-darwin.h \ syscfg/lock-obj-pub.alpha-unknown-linux-gnu.h \ syscfg/lock-obj-pub.arm-unknown-linux-androideabi.h \ syscfg/lock-obj-pub.arm-unknown-linux-gnueabi.h \ syscfg/lock-obj-pub.arm-apple-darwin.h \ syscfg/lock-obj-pub.hppa-unknown-linux-gnu.h \ syscfg/lock-obj-pub.i386-apple-darwin.h \ syscfg/lock-obj-pub.i686-pc-gnu.h \ syscfg/lock-obj-pub.i686-pc-kfreebsd-gnu.h \ syscfg/lock-obj-pub.i686-pc-linux-gnu.h \ syscfg/lock-obj-pub.m68k-unknown-linux-gnu.h \ syscfg/lock-obj-pub.mips-unknown-linux-gnu.h \ syscfg/lock-obj-pub.mips64el-unknown-linux-gnuabi64.h \ syscfg/lock-obj-pub.mipsel-unknown-linux-gnu.h \ syscfg/lock-obj-pub.nios2-unknown-linux-gnu.h \ syscfg/lock-obj-pub.or1k-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc64le-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc-unknown-linux-gnuspe.h \ syscfg/lock-obj-pub.s390x-ibm-linux-gnu.h \ syscfg/lock-obj-pub.sh3-unknown-linux-gnu.h \ syscfg/lock-obj-pub.sh4-unknown-linux-gnu.h \ syscfg/lock-obj-pub.sparc-unknown-linux-gnu.h \ syscfg/lock-obj-pub.sparc64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.x86_64-apple-darwin.h \ syscfg/lock-obj-pub.x86_64-pc-kfreebsd-gnu.h \ syscfg/lock-obj-pub.x86_64-pc-linux-gnu.h \ syscfg/lock-obj-pub.x86_64-pc-linux-gnux32.h \ syscfg/lock-obj-pub.x86_64-pc-linux-musl.h \ syscfg/lock-obj-pub.tilegx-unknown-linux-gnu.h \ syscfg/lock-obj-pub.ia64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.mingw32.h lib_LTLIBRARIES = libgpg-error.la nodist_include_HEADERS = gpg-error.h gpgrt.h bin_SCRIPTS = gpg-error-config m4datadir = $(datadir)/aclocal m4data_DATA = gpg-error.m4 EXTRA_DIST = mkstrtable.awk err-sources.h.in err-codes.h.in \ mkerrnos.awk errnos.in README \ mkerrcodes.awk mkerrcodes1.awk mkerrcodes2.awk mkerrcodes.c \ mkheader.c gpg-error.h.in mkw32errmap.c w32-add.h w32ce-add.h \ err-sources.h err-codes.h gpg-error-config.in gpg-error.m4 \ gpg-error.vers gpg-error.def.in \ versioninfo.rc.in gpg-error.w32-manifest.in \ $(lock_obj_pub) BUILT_SOURCES = err-sources.h err-codes.h code-to-errno.h code-from-errno.h \ err-sources-sym.h err-codes-sym.h errnos-sym.h gpg-error.h gpgrt.h \ gpg-error.def mkw32errmap.map.c tmp_files = _mkerrcodes.h _gpg-error.def.h mkw32errmap.tab.h mkw32errmap.map.c CLEANFILES = err-sources.h err-codes.h code-to-errno.h code-from-errno.h \ gpg-error.h gpgrt.h \ mkerrcodes mkerrcodes.h gpg-error.def mkw32errmap.tab.h \ mkw32errmap.map.c err-sources-sym.h err-codes-sym.h errnos-sym.h \ gpg-extra/errno.h mkheader $(tmp_files) lock-obj-pub.native.h # # {{{ Begin Windows part # if HAVE_W32_SYSTEM arch_sources = w32-gettext.c w32-lock.c w32-lock-obj.h w32-thread.c \ - w32-iconv.c w32-estream.c + w32-iconv.c w32-estream.c w32-reg.c RCCOMPILE = $(RC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ -DLOCALEDIR=\"$(localedir)\" $(AM_CPPFLAGS) $(CPPFLAGS) LTRCCOMPILE = $(LIBTOOL) --mode=compile --tag=RC $(RCCOMPILE) SUFFIXES = .rc .lo .rc.lo: $(LTRCCOMPILE) -i "$<" -o "$@" gpg_error_res = versioninfo.lo no_undefined = -no-undefined export_symbols = -export-symbols gpg-error.def # i686-w64-mingw32.gcc version 4.9.1 takes the long long helper # functions from libgcc_s_sjlj-1.dll and not from a static libgcc. As # a plain C program we do not use exception handler and thus there is # no need to use this DLL. Thus we force gcc to link that statically. extra_ltoptions = -XCClinker -static-libgcc versioninfo.lo : gpg-error.w32-manifest install-def-file: gpg-error.def -$(INSTALL) -d $(DESTDIR)$(libdir) $(INSTALL) gpg-error.def $(DESTDIR)$(libdir)/gpg-error.def uninstall-def-file: -rm $(DESTDIR)$(libdir)/gpg-error.def libgpg_error_la_DEPENDENCIES = $(gpg_error_res) gpg-error.def intllibs = +socklibs = -lws2_32 # # }}} End Windows part # else # # {{{ Begin Unix part # arch_sources = posix-lock.c posix-lock-obj.h posix-thread.c gpg_error_res = no_undefined = export_symbols = extra_ltoptions = install-def-file: uninstall-def-file: intllibs = @LTLIBINTL@ +socklibs = endif # # }}} End Unix part # if HAVE_LD_VERSION_SCRIPT libgpg_error_vers_opt = -Wl,--version-script=$(srcdir)/gpg-error.vers else libgpg_error_vers_opt = endif libgpg_error_la_LDFLAGS = \ $(no_undefined) $(export_symbols) $(libgpg_error_vers_opt) \ $(extra_ltoptions) -version-info \ @LIBGPG_ERROR_LT_CURRENT@:@LIBGPG_ERROR_LT_REVISION@:@LIBGPG_ERROR_LT_AGE@ libgpg_error_la_SOURCES = gettext.h $(arch_sources) \ gpgrt-int.h init.c init.h version.c lock.h thread.h \ estream.c estream-printf.c estream-printf.h \ strsource.c strerror.c code-to-errno.c code-from-errno.c \ visibility.c visibility.h \ sysutils.c \ logging.c \ b64dec.c nodist_libgpg_error_la_SOURCES = gpg-error.h # libgpg_error_la_DEPENDENCIES = \ # $(srcdir)/gpg-error.vers # Note that RCCOMPILE needs the same defines as ..._la_CPPFLAGS but # without the extra_cppflags because they may include am -idirafter # which is not supported by the RC compiler. libgpg_error_la_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" $(extra_cppflags) -libgpg_error_la_LIBADD = $(gpg_error_res) $(intllibs) $(LIBTHREAD) +libgpg_error_la_LIBADD = $(gpg_error_res) $(intllibs) $(socklibs) $(LIBTHREAD) gpg_error_SOURCES = strsource-sym.c strerror-sym.c gpg-error.c gpg_error_CPPFLAGS = -DPKGDATADIR=\"$(pkgdatadir)\" \ -DLOCALEDIR=\"$(localedir)\" $(extra_cppflags) gpg_error_LDADD = ./libgpg-error.la $(LTLIBINTL) # We build err-sources.h and err-codes.h in the source directory. # This is needed because gettext does only look into the source # directory to find the files listed in po/POTFILE.in. To make these # rules work we also need to depend on Makefile.am and not on the # generated files Makefile.in or Makefile. $(srcdir)/err-sources.h: Makefile.am mkstrtable.awk err-sources.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=3 \ $(srcdir)/err-sources.h.in >$@ err-sources-sym.h: Makefile mkstrtable.awk err-sources.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=2 -v nogettext=1 \ $(srcdir)/err-sources.h.in >$@ $(srcdir)/err-codes.h: Makefile.am mkstrtable.awk err-codes.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=3 \ $(srcdir)/err-codes.h.in >$@ err-codes-sym.h: Makefile mkstrtable.awk err-codes.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=2 -v nogettext=1 \ $(srcdir)/err-codes.h.in >$@ code-to-errno.h: Makefile mkerrnos.awk errnos.in $(AWK) -f $(srcdir)/mkerrnos.awk $(srcdir)/errnos.in >$@ # It is correct to use $(CPP). We want the host's idea of the error codes. mkerrcodes.h: Makefile mkerrcodes.awk $(gpg_extra_headers) $(AWK) -f $(srcdir)/mkerrcodes1.awk $(srcdir)/errnos.in >_$@ $(CPP) $(CPPFLAGS) $(extra_cppflags) -P _$@ | grep GPG_ERR_ | \ $(AWK) -f $(srcdir)/mkerrcodes.awk >$@ -rm _$@ if HAVE_W32CE_SYSTEM # It is correct to use $(CPP). We want the host's idea of the error codes. mkw32errmap.tab.h: Makefile mkw32errmap.c $(CPP) -DRESOLVE_MACROS $(srcdir)/mkw32errmap.c | \ grep '{&mkw32errmap_marker' >$@ mkw32errmap.map.c: mkw32errmap ./mkw32errmap --map > $@ gpg-extra/errno.h: mkw32errmap -$(MKDIR_P) gpg-extra ./mkw32errmap > $@ else mkw32errmap.map.c: echo "/*dummy*/" > $@ endif # We use CC proper for preprocessing thus we have to convince it that # the data is really to be preprocessed. gpg-error.def: Makefile gpg-error.def.in cat $(srcdir)/gpg-error.def.in >_$@.h $(CPP) $(DEFAULT_INCLUDES) $(INCLUDES) $(extra_cppflags) _$@.h | \ grep -v '^#' >$@ -rm _$@.h # It is correct to use $(CC_FOR_BUILD) here. We want to run the # program at build time. mkerrcodes: mkerrcodes.c mkerrcodes.h Makefile $(CC_FOR_BUILD) -I. -I$(srcdir) -o $@ $(srcdir)/mkerrcodes.c if HAVE_W32CE_SYSTEM # It is correct to use $(CC_FOR_BUILD) here. We want to run the # program at build time. mkw32errmap: mkw32errmap.c mkw32errmap.tab.h Makefile $(CC_FOR_BUILD) -I. -I$(srcdir) -o $@ $(srcdir)/mkw32errmap.c endif code-from-errno.h: mkerrcodes Makefile ./mkerrcodes | $(AWK) -f $(srcdir)/mkerrcodes2.awk >$@ errnos-sym.h: Makefile mkstrtable.awk errnos.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=2 -v nogettext=1 \ -v prefix=GPG_ERR_ -v namespace=errnos_ \ $(srcdir)/errnos.in >$@ mkheader: mkheader.c Makefile $(CC_FOR_BUILD) -g -O0 -I. -I$(srcdir) -o $@ $(srcdir)/mkheader.c parts_of_gpg_error_h = \ gpg-error.h.in \ err-sources.h.in \ err-codes.h.in \ errnos.in \ w32-add.h \ w32ce-add.h \ $(lock_obj_pub) # If we are cross-compiling or building on Windows we better make sure # that no stale native lock include file will be found by mkheader. if FORCE_USE_SYSCFG pre_mkheader_cmds = if test -f lock-obj-pub.native.h; \ then rm lock-obj-pub.native.h; fi else pre_mkheader_cmds = : parts_of_gpg_error_h += ./lock-obj-pub.native.h ./lock-obj-pub.native.h: Makefile gen-posix-lock-obj$(EXEEXT) posix-lock-obj.h ./gen-posix-lock-obj >$@ endif # We also depend on versioninfo.rc because that is build by # config.status and thus has up-to-date version numbers. gpg-error.h: Makefile mkheader $(parts_of_gpg_error_h) \ versioninfo.rc ../config.h $(pre_mkheader_cmds) ./mkheader $(host_os) $(host_triplet) $(srcdir)/gpg-error.h.in \ ../config.h $(PACKAGE_VERSION) $(VERSION_NUMBER) >$@ gpgrt.h: gpg-error.h cp gpg-error.h gpgrt.h install-data-local: if HAVE_W32CE_SYSTEM -$(MKDIR_P) "$(DESTDIR)$(includedir)/gpg-extra" $(INSTALL_DATA) gpg-extra/errno.h \ "$(DESTDIR)$(includedir)/gpg-extra/errno.h" else : endif diff --git a/src/gpg-error.def.in b/src/gpg-error.def.in index 7c39502..5c7835f 100644 --- a/src/gpg-error.def.in +++ b/src/gpg-error.def.in @@ -1,192 +1,196 @@ /* libgpg-error.def - Exported symbols for W32 * Copyright (C) 2014 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ * * Note: This file should be updated manually and the ordinals shall * never be changed. Also check gpg-error.vers and visibility.h. * * This file needs to be pre-processed. */ #include EXPORTS gpg_strerror @1 gpg_strerror_r @2 gpg_strsource @3 gpg_err_code_from_errno @4 gpg_err_code_to_errno @5 /* @6 - Not anymore used. */ gpg_err_code_from_syserror @7 gpg_err_set_errno @8 #ifdef HAVE_W32CE_SYSTEM _gpg_w32ce_get_errno @9 _gpg_w32ce_strerror @10 #endif #ifdef HAVE_W32_SYSTEM _gpg_w32_bindtextdomain @11 _gpg_w32_textdomain @12 _gpg_w32_gettext @13 _gpg_w32_dgettext @14 _gpg_w32_dngettext @15 _gpg_w32_gettext_localename @16 _gpg_w32_gettext_use_utf8 @17 #endif /* @18 - Not anymore used. */ gpg_error_check_version @19 gpgrt_lock_init @20 gpgrt_lock_lock @21 gpgrt_lock_unlock @22 gpgrt_lock_destroy @23 gpgrt_yield @24 gpgrt_lock_trylock @25 gpgrt_set_syscall_clamp @26 gpgrt_fopen @27 gpgrt_mopen @28 gpgrt_fopenmem @29 gpgrt_fopenmem_init @30 gpgrt_fdopen @31 gpgrt_fdopen_nc @32 gpgrt_sysopen @33 gpgrt_sysopen_nc @34 gpgrt_fpopen @35 gpgrt_fpopen_nc @36 gpgrt_freopen @37 gpgrt_fopencookie @38 gpgrt_fclose @39 gpgrt_fclose_snatch @40 gpgrt_onclose @41 gpgrt_fileno @42 gpgrt_fileno_unlocked @43 gpgrt_syshd @44 gpgrt_syshd_unlocked @45 _gpgrt_set_std_fd @46 _gpgrt_get_std_stream @47 gpgrt_flockfile @48 gpgrt_ftrylockfile @49 gpgrt_funlockfile @50 gpgrt_feof @51 gpgrt_feof_unlocked @52 gpgrt_ferror @53 gpgrt_ferror_unlocked @54 gpgrt_clearerr @55 gpgrt_clearerr_unlocked @56 gpgrt_fflush @57 gpgrt_fseek @58 gpgrt_fseeko @59 gpgrt_ftell @60 gpgrt_ftello @61 gpgrt_rewind @62 gpgrt_fgetc @63 _gpgrt_getc_underflow @64 gpgrt_fputc @65 _gpgrt_putc_overflow @66 gpgrt_ungetc @67 gpgrt_read @68 gpgrt_write @69 gpgrt_write_sanitized @70 gpgrt_write_hexstring @71 gpgrt_fread @72 gpgrt_fwrite @73 gpgrt_fgets @74 gpgrt_fputs @75 gpgrt_fputs_unlocked @76 gpgrt_getline @77 gpgrt_read_line @78 gpgrt_free @79 gpgrt_fprintf @80 gpgrt_fprintf_unlocked @81 gpgrt_printf @82 gpgrt_printf_unlocked @83 gpgrt_vfprintf @84 gpgrt_vfprintf_unlocked @85 gpgrt_setvbuf @86 gpgrt_setbuf @87 gpgrt_set_binary @88 gpgrt_tmpfile @89 gpgrt_opaque_set @90 gpgrt_opaque_get @91 gpgrt_fname_set @92 gpgrt_fname_get @93 gpgrt_asprintf @94 gpgrt_vasprintf @95 gpgrt_bsprintf @96 gpgrt_vbsprintf @97 gpgrt_snprintf @98 gpgrt_vsnprintf @99 gpgrt_check_version @100 gpg_err_init @101 gpg_err_deinit @102 gpgrt_set_alloc_func @103 _gpgrt_pending @104 _gpgrt_pending_unlocked @105 gpgrt_set_nonblock @106 gpgrt_get_nonblock @107 gpgrt_poll @108 #ifdef HAVE_W32_SYSTEM gpgrt_w32_iconv_open @109 gpgrt_w32_iconv_close @110 gpgrt_w32_iconv @111 #endif gpgrt_get_syscall_clamp @112 gpgrt_b64dec_start @113 gpgrt_b64dec_proc @114 gpgrt_b64dec_finish @115 gpgrt_get_errorcount @116 gpgrt_inc_errorcount @117 gpgrt_log_set_sink @118 gpgrt_log_set_socket_dir_cb @119 gpgrt_log_set_pid_suffix_cb @120 gpgrt_log_set_prefix @121 gpgrt_log_get_prefix @122 gpgrt_log_test_fd @123 gpgrt_log_get_fd @124 gpgrt_log_get_stream @125 gpgrt_log @126 gpgrt_logv @127 gpgrt_logv_prefix @128 gpgrt_log_string @129 gpgrt_log_bug @130 gpgrt_log_fatal @131 gpgrt_log_error @132 gpgrt_log_info @133 gpgrt_log_debug @134 gpgrt_log_debug_string @135 gpgrt_log_printf @136 gpgrt_log_printhex @137 gpgrt_log_clock @138 gpgrt_log_flush @139 _gpgrt_log_assert @140 gpgrt_realloc @141 gpgrt_malloc @142 gpgrt_calloc @143 gpgrt_strdup @144 gpgrt_strconcat @145 +#ifdef HAVE_W32_SYSTEM + gpgrt_w32_reg_query_string @148 +#endif + ;; end of file with public symbols for Windows. diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index c226e2b..c0e83b1 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -1,556 +1,593 @@ /* gpgrt-int.h - Internal definitions * Copyright (C) 2014, 2017 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #ifndef _GPGRT_GPGRT_INT_H #define _GPGRT_GPGRT_INT_H #include "gpg-error.h" #include "visibility.h" /* * Internal i18n macros. */ #ifdef ENABLE_NLS # ifdef HAVE_W32_SYSTEM # include "gettext.h" # else # include # endif # define _(a) gettext (a) # ifdef gettext_noop # define N_(a) gettext_noop (a) # else # define N_(a) (a) # endif #else /*!ENABLE_NLS*/ # define _(a) (a) # define N_(a) (a) #endif /*!ENABLE_NLS */ /* * Hacks mainly required for Slowaris. */ #ifdef _GPGRT_NEED_AFLOCAL # ifndef HAVE_W32_SYSTEM # include # include # else # ifdef HAVE_WINSOCK2_H # include # endif # include # endif # ifndef PF_LOCAL # ifdef PF_UNIX # define PF_LOCAL PF_UNIX # else # define PF_LOCAL AF_UNIX # endif # endif /*PF_LOCAL*/ # ifndef AF_LOCAL # define AF_LOCAL AF_UNIX # endif /*AF_UNIX*/ /* We used to avoid this macro in GnuPG and inlined the AF_LOCAL name * length computation directly with the little twist of adding 1 extra * byte. It seems that this was needed once on an old HP/UX box and * there are also rumours that 4.3 Reno and DEC systems need it. This * one-off buglet did not harm any current system until it came to Mac * OS X where the kernel (as of May 2009) exhibited a strange bug: The * systems basically froze in the connect call if the passed name * contained an invalid directory part. Ignore the old Unices. */ # ifndef SUN_LEN # define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \ + strlen ((ptr)->sun_path)) # endif /*SUN_LEN*/ #endif /*_GPGRT_NEED_AFLOCAL*/ /* * Common helper macros. */ #ifndef DIM # define DIM(array) (sizeof (array) / sizeof (*array)) #endif /* * Local error function prototypes. */ const char *_gpg_strerror (gpg_error_t err); int _gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen); const char *_gpg_strsource (gpg_error_t err); gpg_err_code_t _gpg_err_code_from_errno (int err); int _gpg_err_code_to_errno (gpg_err_code_t code); gpg_err_code_t _gpg_err_code_from_syserror (void); void _gpg_err_set_errno (int err); gpg_error_t _gpg_err_init (void); void _gpg_err_deinit (int mode); void _gpgrt_set_alloc_func (void *(*f)(void *a, size_t n)); void *_gpgrt_realloc (void *a, size_t n); void *_gpgrt_malloc (size_t n); void *_gpgrt_calloc (size_t n, size_t m); char *_gpgrt_strdup (const char *string); char *_gpgrt_strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0); void _gpgrt_free (void *a); /* The next is only to be used by visibility.c. */ char *_gpgrt_strconcat_core (const char *s1, va_list arg_ptr); +#define xfree(a) _gpgrt_free ((a)) +#define xtrymalloc(a) _gpgrt_malloc ((a)) +#define xtrycalloc(a,b) _gpgrt_calloc ((a),(b)) +#define xtryrealloc(a,b) _gpgrt_realloc ((a),(b)) + + + const char *_gpg_error_check_version (const char *req_version); gpg_err_code_t _gpgrt_lock_init (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_lock (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_trylock (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_unlock (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_destroy (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_yield (void); /* * Tracing */ /* The trace macro is used this way: * trace (("enter - foo=%d bar=%s", foo, bar)); * Note the double parenthesis, they are important. * To append the current errno to the output, use * trace_errno (EXTPR,("leave - baz=%d", faz)); * If EXPR evaluates to true the output of strerror (errno) * is appended to the output. Note that the trace function does * not modify ERRNO. To enable tracing you need to have this * #define ENABLE_TRACING "modulename" * before you include gpgrt-int.h. */ #ifdef ENABLE_TRACING # define trace(X) do { \ _gpgrt_internal_trace_begin \ (ENABLE_TRACING, __func__, __LINE__, 0); \ _gpgrt_internal_trace X; \ _gpgrt_internal_trace_end (); \ } while (0) # define trace_errno(C,X) do { \ _gpgrt_internal_trace_begin \ (ENABLE_TRACING, __func__, __LINE__, (C)); \ _gpgrt_internal_trace X; \ _gpgrt_internal_trace_end (); \ } while (0) # define trace_start(X) do { \ _gpgrt_internal_trace_begin \ (ENABLE_TRACING, __func__, __LINE__, 0); \ _gpgrt_internal_trace_printf X; \ } while (0) # define trace_append(X) do { \ _gpgrt_internal_trace_printf X; \ } while (0) # define trace_finish(X) do { \ _gpgrt_internal_trace_printf X; \ _gpgrt_internal_trace_end (); \ } while (0) #else # define trace(X) do { } while (0) # define trace_errno(C,X) do { } while (0) # define trace_start(X) do { } while (0) # define trace_append(X) do { } while (0) # define trace_finish(X) do { } while (0) #endif /*!ENABLE_TRACING*/ void _gpgrt_internal_trace_begin (const char *mod, const char *file, int line, int with_errno); void _gpgrt_internal_trace (const char *format, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_internal_trace_printf (const char *format, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_internal_trace_end (void); /* * Local definitions for estream. */ #if HAVE_W32_SYSTEM # ifndef O_NONBLOCK # define O_NONBLOCK 0x40000000 /* FIXME: Is that safe? */ # endif #endif /* * A private cookie function to implement an internal IOCTL service. * and ist IOCTL numbers. */ typedef int (*cookie_ioctl_function_t) (void *cookie, int cmd, void *ptr, size_t *len); #define COOKIE_IOCTL_SNATCH_BUFFER 1 #define COOKIE_IOCTL_NONBLOCK 2 /* An internal variant of gpgrt_cookie_close_function_t with a slot * for the ioctl function. */ struct cookie_io_functions_s { struct _gpgrt_cookie_io_functions public; cookie_ioctl_function_t func_ioctl; }; typedef enum { BACKEND_MEM, BACKEND_FD, BACKEND_W32, BACKEND_FP, BACKEND_USER, BACKEND_W32_POLLABLE } gpgrt_stream_backend_kind_t; /* * A type to hold notification functions. */ struct notify_list_s { struct notify_list_s *next; void (*fnc) (estream_t, void*); /* The notification function. */ void *fnc_value; /* The value to be passed to FNC. */ }; typedef struct notify_list_s *notify_list_t; /* * Buffer management layer. */ #define BUFFER_BLOCK_SIZE BUFSIZ #define BUFFER_UNREAD_SIZE 16 /* * The private object describing a stream. */ struct _gpgrt_stream_internal { unsigned char buffer[BUFFER_BLOCK_SIZE]; unsigned char unread_buffer[BUFFER_UNREAD_SIZE]; gpgrt_lock_t lock; /* Lock. Used by *_stream_lock(). */ gpgrt_stream_backend_kind_t kind; void *cookie; /* Cookie. */ void *opaque; /* Opaque data. */ unsigned int modeflags; /* Flags for the backend. */ char *printable_fname; /* Malloced filename for es_fname_get. */ gpgrt_off_t offset; gpgrt_cookie_read_function_t func_read; gpgrt_cookie_write_function_t func_write; gpgrt_cookie_seek_function_t func_seek; gpgrt_cookie_close_function_t func_close; cookie_ioctl_function_t func_ioctl; int strategy; es_syshd_t syshd; /* A copy of the system handle. */ struct { unsigned int err: 1; unsigned int eof: 1; unsigned int hup: 1; } indicators; unsigned int deallocate_buffer: 1; unsigned int is_stdstream:1; /* This is a standard stream. */ unsigned int stdstream_fd:2; /* 0, 1 or 2 for a standard stream. */ unsigned int printable_fname_inuse: 1; /* es_fname_get has been used. */ unsigned int samethread: 1; /* The "samethread" mode keyword. */ size_t print_ntotal; /* Bytes written from in print_writer. */ notify_list_t onclose; /* On close notify function list. */ }; typedef struct _gpgrt_stream_internal *estream_internal_t; /* * Local prototypes for estream. */ int _gpgrt_estream_init (void); void _gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void)); void _gpgrt_get_syscall_clamp (void (**r_pre)(void), void (**r_post)(void)); gpgrt_stream_t _gpgrt_fopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode); gpgrt_stream_t _gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, void *(*func_realloc) (void *mem, size_t size), void (*func_free) (void *mem), const char *_GPGRT__RESTRICT mode); gpgrt_stream_t _gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode); gpgrt_stream_t _gpgrt_fopenmem_init (size_t memlimit, const char *_GPGRT__RESTRICT mode, const void *data, size_t datalen); gpgrt_stream_t _gpgrt_fdopen (int filedes, const char *mode); gpgrt_stream_t _gpgrt_fdopen_nc (int filedes, const char *mode); gpgrt_stream_t _gpgrt_sysopen (gpgrt_syshd_t *syshd, const char *mode); gpgrt_stream_t _gpgrt_sysopen_nc (gpgrt_syshd_t *syshd, const char *mode); gpgrt_stream_t _gpgrt_fpopen (FILE *fp, const char *mode); gpgrt_stream_t _gpgrt_fpopen_nc (FILE *fp, const char *mode); gpgrt_stream_t _gpgrt_freopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode, gpgrt_stream_t _GPGRT__RESTRICT stream); gpgrt_stream_t _gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie, const char *_GPGRT__RESTRICT mode, gpgrt_cookie_io_functions_t functions); int _gpgrt_fclose (gpgrt_stream_t stream); int _gpgrt_fclose_snatch (gpgrt_stream_t stream, void **r_buffer, size_t *r_buflen); int _gpgrt_onclose (gpgrt_stream_t stream, int mode, void (*fnc) (gpgrt_stream_t, void*), void *fnc_value); int _gpgrt_fileno (gpgrt_stream_t stream); int _gpgrt_fileno_unlocked (gpgrt_stream_t stream); int _gpgrt_syshd (gpgrt_stream_t stream, gpgrt_syshd_t *syshd); int _gpgrt_syshd_unlocked (gpgrt_stream_t stream, gpgrt_syshd_t *syshd); void _gpgrt__set_std_fd (int no, int fd); gpgrt_stream_t _gpgrt__get_std_stream (int fd); /* The es_stderr et al macros are pretty common so that we want to use * them too. This requires that we redefine them. */ #undef es_stdin #define es_stdin _gpgrt__get_std_stream (0) #undef es_stdout #define es_stdout _gpgrt__get_std_stream (1) #undef es_stderr #define es_stderr _gpgrt__get_std_stream (2) void _gpgrt_flockfile (gpgrt_stream_t stream); int _gpgrt_ftrylockfile (gpgrt_stream_t stream); void _gpgrt_funlockfile (gpgrt_stream_t stream); int _gpgrt_feof (gpgrt_stream_t stream); int _gpgrt_feof_unlocked (gpgrt_stream_t stream); int _gpgrt_ferror (gpgrt_stream_t stream); int _gpgrt_ferror_unlocked (gpgrt_stream_t stream); void _gpgrt_clearerr (gpgrt_stream_t stream); void _gpgrt_clearerr_unlocked (gpgrt_stream_t stream); int _gpgrt__pending (gpgrt_stream_t stream); int _gpgrt__pending_unlocked (gpgrt_stream_t stream); int _gpgrt_fflush (gpgrt_stream_t stream); int _gpgrt_fseek (gpgrt_stream_t stream, long int offset, int whence); int _gpgrt_fseeko (gpgrt_stream_t stream, gpgrt_off_t offset, int whence); long int _gpgrt_ftell (gpgrt_stream_t stream); gpgrt_off_t _gpgrt_ftello (gpgrt_stream_t stream); void _gpgrt_rewind (gpgrt_stream_t stream); int _gpgrt_fgetc (gpgrt_stream_t stream); int _gpgrt_fputc (int c, gpgrt_stream_t stream); int _gpgrt__getc_underflow (gpgrt_stream_t stream); int _gpgrt__putc_overflow (int c, gpgrt_stream_t stream); /* Note: Keeps the next two macros in sync with their counterparts in gpg-error.h. */ #define _gpgrt_getc_unlocked(stream) \ (((!(stream)->flags.writing) \ && ((stream)->data_offset < (stream)->data_len) \ && (! (stream)->unread_data_len)) \ ? ((int) (stream)->buffer[((stream)->data_offset)++]) \ : _gpgrt__getc_underflow ((stream))) #define _gpgrt_putc_unlocked(c, stream) \ (((stream)->flags.writing \ && ((stream)->data_offset < (stream)->buffer_size) \ && (c != '\n')) \ ? ((int) ((stream)->buffer[((stream)->data_offset)++] = (c))) \ : _gpgrt__putc_overflow ((c), (stream))) int _gpgrt_ungetc (int c, gpgrt_stream_t stream); int _gpgrt_read (gpgrt_stream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read); int _gpgrt_write (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written); int _gpgrt_write_sanitized (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, const char *delimiters, size_t *_GPGRT__RESTRICT bytes_written); int _gpgrt_write_hexstring (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, int reserved, size_t *_GPGRT__RESTRICT bytes_written); size_t _gpgrt_fread (void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, gpgrt_stream_t _GPGRT__RESTRICT stream); size_t _gpgrt_fwrite (const void *_GPGRT__RESTRICT ptr, size_t size, size_t memb, gpgrt_stream_t _GPGRT__RESTRICT stream); char *_gpgrt_fgets (char *_GPGRT__RESTRICT s, int n, gpgrt_stream_t _GPGRT__RESTRICT stream); int _gpgrt_fputs (const char *_GPGRT__RESTRICT s, gpgrt_stream_t _GPGRT__RESTRICT stream); int _gpgrt_fputs_unlocked (const char *_GPGRT__RESTRICT s, gpgrt_stream_t _GPGRT__RESTRICT stream); gpgrt_ssize_t _gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr, size_t *_GPGRT__RESTRICT n, gpgrt_stream_t stream); gpgrt_ssize_t _gpgrt_read_line (gpgrt_stream_t stream, char **addr_of_buffer, size_t *length_of_buffer, size_t *max_length); int _gpgrt_fprintf (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(2,3); int _gpgrt_fprintf_unlocked (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(2,3); int _gpgrt_vfprintf (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(2,0); int _gpgrt_vfprintf_unlocked (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(2,0); int _gpgrt_setvbuf (gpgrt_stream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf, int mode, size_t size); void _gpgrt_set_binary (gpgrt_stream_t stream); int _gpgrt_set_nonblock (gpgrt_stream_t stream, int onoff); int _gpgrt_get_nonblock (gpgrt_stream_t stream); int _gpgrt_poll (gpgrt_poll_t *fds, unsigned int nfds, int timeout); gpgrt_stream_t _gpgrt_tmpfile (void); void _gpgrt_opaque_set (gpgrt_stream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT opaque); void *_gpgrt_opaque_get (gpgrt_stream_t stream); void _gpgrt_fname_set (gpgrt_stream_t stream, const char *fname); const char *_gpgrt_fname_get (gpgrt_stream_t stream); #include "estream-printf.h" /* Make sure we always use our snprintf */ #define snprintf _gpgrt_estream_snprintf #if HAVE_W32_SYSTEM /* Prototypes for w32-estream.c. */ struct cookie_io_functions_s _gpgrt_functions_w32_pollable; int _gpgrt_w32_pollable_create (void *_GPGRT__RESTRICT *_GPGRT__RESTRICT cookie, unsigned int modeflags, struct cookie_io_functions_s next_functions, void *next_cookie); int _gpgrt_w32_poll (gpgrt_poll_t *fds, size_t nfds, int timeout); #endif /*HAVE_W32_SYSTEM*/ /* * Local prototypes for the encoders. */ gpgrt_b64state_t _gpgrt_b64dec_start (const char *title); gpg_error_t _gpgrt_b64dec_proc (gpgrt_b64state_t state, void *buffer, size_t length, size_t *r_nbytes); gpg_error_t _gpgrt_b64dec_finish (gpgrt_b64state_t state); /* * Local prototypes for logging */ int _gpgrt_get_errorcount (int clear); void _gpgrt_inc_errorcount (void); void _gpgrt_log_set_sink (const char *name, estream_t stream, int fd); void _gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)); void _gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)); void _gpgrt_log_set_prefix (const char *text, unsigned int flags); const char *_gpgrt_log_get_prefix (unsigned int *flags); int _gpgrt_log_test_fd (int fd); int _gpgrt_log_get_fd (void); estream_t _gpgrt_log_get_stream (void); void _gpgrt_log (int level, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); void _gpgrt_logv (int level, const char *fmt, va_list arg_ptr); void _gpgrt_logv_prefix (int level, const char *prefix, const char *fmt, va_list arg_ptr); void _gpgrt_log_string (int level, const char *string); void _gpgrt_log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); void _gpgrt_log_fatal (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); void _gpgrt_log_error (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_info (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_debug (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_debug_string (const char *string, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); void _gpgrt_log_printf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_flush (void); void _gpgrt_logv_printhex (const void *buffer, size_t length, const char *fmt, va_list arg_ptr); void _gpgrt_log_printhex (const void *buffer, size_t length, const char *fmt, ...) GPGRT_ATTR_PRINTF(3,4);; void _gpgrt_logv_clock (const char *fmt, va_list arg_ptr); void _gpgrt_log_clock (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt__log_assert (const char *expr, const char *file, int line, const char *func) GPGRT_ATTR_NORETURN; /* Redefine the assert macro to use our internal function. */ #undef gpgrt_assert #ifdef GPGRT_HAVE_MACRO_FUNCTION #define gpgrt_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt__log_assert (#expr, __FILE__, __LINE__, __FUNCTION__)) #else /*!GPGRT_HAVE_MACRO_FUNCTION*/ /* # define BUG() bug_at( __FILE__ , __LINE__ ) */ #define gpgrt_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt__log_assert (#expr, __FILE__, __LINE__, NULL)) #endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ /* Note: The next function is only to be used by visibility.c. */ int _gpgrt_logv_internal (int level, int ignore_arg_ptr, const char *extrastring, const char *prefmt, const char *fmt, va_list arg_ptr); /* * Internal platform abstraction functions (sysutils.c) */ /* Return true if FD is valid. */ int _gpgrt_fd_valid_p (int fd); + +/* + * Platform specific functions (Windows) + */ +#ifdef HAVE_W32_SYSTEM + +char *_gpgrt_w32_reg_query_string (const char *root, + const char *dir, + const char *name); + + +#endif /*HAVE_W32_SYSTEM*/ + +/* + * Missing functions implemented inline. + */ + +#ifndef HAVE_STPCPY +static GPG_ERR_INLINE char * +_gpgrt_stpcpy (char *a, const char *b) +{ + while (*b) + *a++ = *b++; + *a = 0; + return a; +} +#define stpcpy(a,b) _gpgrt_stpcpy ((a), (b)) +#endif /*!HAVE_STPCPY*/ + + #endif /*_GPGRT_GPGRT_INT_H*/ diff --git a/src/logging.c b/src/logging.c index 2e675f5..dbd8066 100644 --- a/src/logging.c +++ b/src/logging.c @@ -1,1213 +1,1213 @@ /* logging.c - Useful logging functions * Copyright (C) 1998-2001, 2003-2006, 2009-2010, * 2017 Free Software Foundation, Inc. * Copyright (C) 1998-1999, 2001-2006, 2008-2017 Werner Koch * * This file is part of Libgpg-error. * * Libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libgpg-error 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ * * This file was originally a part of GnuPG. */ #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_W32_SYSTEM # ifdef HAVE_WINSOCK2_H # include # endif # include #else /*!HAVE_W32_SYSTEM*/ # include # include # include # include #endif /*!HAVE_W32_SYSTEM*/ #include #include #include /* #include */ #define _GPGRT_NEED_AFLOCAL 1 #include "gpgrt-int.h" #ifdef HAVE_W32_SYSTEM # ifndef S_IRWXG # define S_IRGRP S_IRUSR # define S_IWGRP S_IWUSR # endif # ifndef S_IRWXO # define S_IROTH S_IRUSR # define S_IWOTH S_IWUSR # endif #endif #ifdef HAVE_W32CE_SYSTEM # define isatty(a) (0) #endif #undef WITH_IPV6 #if defined (AF_INET6) && defined(PF_INET) \ && defined (INET6_ADDRSTRLEN) && defined(HAVE_INET_PTON) # define WITH_IPV6 1 #endif #ifndef EAFNOSUPPORT # define EAFNOSUPPORT EINVAL #endif #ifndef INADDR_NONE /* Slowaris is missing that. */ #define INADDR_NONE ((unsigned long)(-1)) #endif /*INADDR_NONE*/ #ifdef HAVE_W32_SYSTEM #define sock_close(a) closesocket(a) #else #define sock_close(a) close(a) #endif static estream_t logstream; static int log_socket = -1; static char prefix_buffer[80]; static int with_time; static int with_prefix; static int with_pid; #ifdef HAVE_W32_SYSTEM static int no_registry; #endif static int (*get_pid_suffix_cb)(unsigned long *r_value); static const char * (*socket_dir_cb)(void); static int running_detached; static int force_prefixes; static int missing_lf; static int errorcount; /* Get the error count as maintained by the log fucntions. With CLEAR * set reset the counter. */ int _gpgrt_get_errorcount (int clear) { int n = errorcount; if (clear) errorcount = 0; return n; } /* Increment the error count as maintainer by the log functions. */ void _gpgrt_inc_errorcount (void) { errorcount++; } /* The following 3 functions are used by _gpgrt_fopencookie to write logs to a socket. */ struct fun_cookie_s { int fd; int quiet; int want_socket; int is_socket; #ifdef HAVE_W32CE_SYSTEM int use_writefile; #endif char name[1]; }; /* Write NBYTES of BUFFER to file descriptor FD. */ static int writen (int fd, const void *buffer, size_t nbytes, int is_socket) { const char *buf = buffer; size_t nleft = nbytes; int nwritten; #ifndef HAVE_W32_SYSTEM (void)is_socket; /* Not required. */ #endif while (nleft > 0) { #ifdef HAVE_W32_SYSTEM if (is_socket) nwritten = send (fd, buf, nleft, 0); else #endif nwritten = write (fd, buf, nleft); if (nwritten < 0 && errno == EINTR) continue; if (nwritten < 0) return -1; nleft -= nwritten; buf = buf + nwritten; } return 0; } /* Returns true if STR represents a valid port number in decimal notation and no garbage is following. */ static int parse_portno (const char *str, unsigned short *r_port) { unsigned int value; for (value=0; *str && (*str >= '0' && *str <= '9'); str++) { value = value * 10 + (*str - '0'); if (value > 65535) return 0; } if (*str || !value) return 0; *r_port = value; return 1; } static gpgrt_ssize_t fun_writer (void *cookie_arg, const void *buffer, size_t size) { struct fun_cookie_s *cookie = cookie_arg; /* FIXME: Use only estream with a callback for socket writing. This avoids the ugly mix of fd and estream code. */ /* Note that we always try to reconnect to the socket but print error messages only the first time an error occurred. If RUNNING_DETACHED is set we don't fall back to stderr and even do not print any error messages. This is needed because detached processes often close stderr and by writing to file descriptor 2 we might send the log message to a file not intended for logging (e.g. a pipe or network connection). */ if (cookie->want_socket && cookie->fd == -1) { #ifdef WITH_IPV6 struct sockaddr_in6 srvr_addr_in6; #endif struct sockaddr_in srvr_addr_in; #ifndef HAVE_W32_SYSTEM struct sockaddr_un srvr_addr_un; #endif const char *name_for_err = ""; size_t addrlen; struct sockaddr *srvr_addr = NULL; unsigned short port = 0; int af = AF_LOCAL; int pf = PF_LOCAL; const char *name = cookie->name; /* Not yet open or meanwhile closed due to an error. */ cookie->is_socket = 0; /* Check whether this is a TCP socket or a local socket. */ if (!strncmp (name, "tcp://", 6) && name[6]) { name += 6; af = AF_INET; pf = PF_INET; } #ifndef HAVE_W32_SYSTEM else if (!strncmp (name, "socket://", 9)) name += 9; #endif if (af == AF_LOCAL) { addrlen = 0; #ifndef HAVE_W32_SYSTEM memset (&srvr_addr, 0, sizeof srvr_addr); srvr_addr_un.sun_family = af; if (!*name && (name = socket_dir_cb ()) && *name) { if (strlen (name) + 7 < sizeof (srvr_addr_un.sun_path)-1) { strncpy (srvr_addr_un.sun_path, name, sizeof (srvr_addr_un.sun_path)-1); strcat (srvr_addr_un.sun_path, "/S.log"); srvr_addr_un.sun_path[sizeof (srvr_addr_un.sun_path)-1] = 0; srvr_addr = (struct sockaddr *)&srvr_addr_un; addrlen = SUN_LEN (&srvr_addr_un); name_for_err = srvr_addr_un.sun_path; } } else { if (*name && strlen (name) < sizeof (srvr_addr_un.sun_path)-1) { strncpy (srvr_addr_un.sun_path, name, sizeof (srvr_addr_un.sun_path)-1); srvr_addr_un.sun_path[sizeof (srvr_addr_un.sun_path)-1] = 0; srvr_addr = (struct sockaddr *)&srvr_addr_un; addrlen = SUN_LEN (&srvr_addr_un); } } #endif /*!HAVE_W32SYSTEM*/ } else { char *addrstr, *p; #ifdef HAVE_INET_PTON void *addrbuf = NULL; #endif /*HAVE_INET_PTON*/ addrstr = _gpgrt_malloc (strlen (name) + 1); if (!addrstr) addrlen = 0; /* This indicates an error. */ else if (*name == '[') { /* Check for IPv6 literal address. */ strcpy (addrstr, name+1); p = strchr (addrstr, ']'); if (!p || p[1] != ':' || !parse_portno (p+2, &port)) { _gpg_err_set_errno (EINVAL); addrlen = 0; } else { *p = 0; #ifdef WITH_IPV6 af = AF_INET6; pf = PF_INET6; memset (&srvr_addr_in6, 0, sizeof srvr_addr_in6); srvr_addr_in6.sin6_family = af; srvr_addr_in6.sin6_port = htons (port); #ifdef HAVE_INET_PTON addrbuf = &srvr_addr_in6.sin6_addr; #endif /*HAVE_INET_PTON*/ srvr_addr = (struct sockaddr *)&srvr_addr_in6; addrlen = sizeof srvr_addr_in6; #else _gpg_err_set_errno (EAFNOSUPPORT); addrlen = 0; #endif } } else { /* Check for IPv4 literal address. */ strcpy (addrstr, name); p = strchr (addrstr, ':'); if (!p || !parse_portno (p+1, &port)) { _gpg_err_set_errno (EINVAL); addrlen = 0; } else { *p = 0; memset (&srvr_addr_in, 0, sizeof srvr_addr_in); srvr_addr_in.sin_family = af; srvr_addr_in.sin_port = htons (port); #ifdef HAVE_INET_PTON addrbuf = &srvr_addr_in.sin_addr; #endif /*HAVE_INET_PTON*/ srvr_addr = (struct sockaddr *)&srvr_addr_in; addrlen = sizeof srvr_addr_in; } } if (addrlen) { #ifdef HAVE_INET_PTON if (inet_pton (af, addrstr, addrbuf) != 1) addrlen = 0; #else /*!HAVE_INET_PTON*/ /* We need to use the old function. If we are here v6 support isn't enabled anyway and thus we can do fine without. Note that Windows has a compatible inet_pton function named inetPton, but only since Vista. */ srvr_addr_in.sin_addr.s_addr = inet_addr (addrstr); if (srvr_addr_in.sin_addr.s_addr == INADDR_NONE) addrlen = 0; #endif /*!HAVE_INET_PTON*/ } _gpgrt_free (addrstr); } cookie->fd = addrlen? socket (pf, SOCK_STREAM, 0) : -1; if (cookie->fd == -1) { if (!cookie->quiet && !running_detached && isatty (_gpgrt_fileno (es_stderr))) _gpgrt_fprintf (es_stderr, "failed to create socket for logging: %s\n", strerror (errno)); } else { if (connect (cookie->fd, srvr_addr, addrlen) == -1) { if (!cookie->quiet && !running_detached && isatty (_gpgrt_fileno (es_stderr))) _gpgrt_fprintf (es_stderr, "can't connect to '%s%s': %s\n", cookie->name, name_for_err, strerror(errno)); sock_close (cookie->fd); cookie->fd = -1; } } if (cookie->fd == -1) { if (!running_detached) { /* Due to all the problems with apps not running detached but being called with stderr closed or used for a different purposes, it does not make sense to switch to stderr. We therefore disable it. */ if (!cookie->quiet) { /* fputs ("switching logging to stderr\n", stderr);*/ cookie->quiet = 1; } cookie->fd = -1; /*fileno (stderr);*/ } } else /* Connection has been established. */ { cookie->quiet = 0; cookie->is_socket = 1; } } log_socket = cookie->fd; if (cookie->fd != -1) { #ifdef HAVE_W32CE_SYSTEM if (cookie->use_writefile) { DWORD nwritten; WriteFile ((HANDLE)cookie->fd, buffer, size, &nwritten, NULL); return (gpgrt_ssize_t)size; /* Okay. */ } #endif if (!writen (cookie->fd, buffer, size, cookie->is_socket)) return (gpgrt_ssize_t)size; /* Okay. */ } if (!running_detached && cookie->fd != -1 && isatty (_gpgrt_fileno (es_stderr))) { if (*cookie->name) _gpgrt_fprintf (es_stderr, "error writing to '%s': %s\n", cookie->name, strerror(errno)); else _gpgrt_fprintf (es_stderr, "error writing to file descriptor %d: %s\n", cookie->fd, strerror(errno)); } if (cookie->is_socket && cookie->fd != -1) { sock_close (cookie->fd); cookie->fd = -1; log_socket = -1; } return (gpgrt_ssize_t)size; } static int fun_closer (void *cookie_arg) { struct fun_cookie_s *cookie = cookie_arg; if (cookie->fd != -1 && cookie->fd != 2) sock_close (cookie->fd); _gpgrt_free (cookie); log_socket = -1; return 0; } /* Common function to either set the logging to a file or a file descriptor. */ static void set_file_fd (const char *name, int fd) { estream_t fp; int want_socket; #ifdef HAVE_W32CE_SYSTEM int use_writefile = 0; #endif struct fun_cookie_s *cookie; /* Close an open log stream. */ if (logstream) { if (logstream != es_stderr) _gpgrt_fclose (logstream); logstream = NULL; } /* Figure out what kind of logging we want. */ if (name && !strcmp (name, "-")) { name = NULL; fd = _gpgrt_fileno (es_stderr); } want_socket = 0; if (name && !strncmp (name, "tcp://", 6) && name[6]) want_socket = 1; #ifndef HAVE_W32_SYSTEM else if (name && !strncmp (name, "socket://", 9)) want_socket = 2; #endif /*HAVE_W32_SYSTEM*/ #ifdef HAVE_W32CE_SYSTEM else if (name && !strcmp (name, "GPG2:")) { HANDLE hd; ActivateDevice (L"Drivers\\"GNUPG_NAME"_Log", 0); /* Ignore a filename and write the debug output to the GPG2: device. */ hd = CreateFile (L"GPG2:", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); fd = (hd == INVALID_HANDLE_VALUE)? -1 : (int)hd; name = NULL; force_prefixes = 1; use_writefile = 1; } #endif /*HAVE_W32CE_SYSTEM*/ /* Setup a new stream. */ cookie = _gpgrt_malloc (sizeof *cookie + (name? strlen (name):0)); if (!cookie) return; /* oops */ strcpy (cookie->name, name? name:""); cookie->quiet = 0; cookie->is_socket = 0; cookie->want_socket = want_socket; #ifdef HAVE_W32CE_SYSTEM cookie->use_writefile = use_writefile; #endif if (!name) cookie->fd = fd; else if (want_socket) cookie->fd = -1; else { do cookie->fd = open (name, O_WRONLY|O_APPEND|O_CREAT, (S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH)); while (cookie->fd == -1 && errno == EINTR); } log_socket = cookie->fd; { es_cookie_io_functions_t io = { NULL }; io.func_write = fun_writer; io.func_close = fun_closer; fp = _gpgrt_fopencookie (cookie, "w", io); } /* On error default to a stderr based estream. */ if (!fp) fp = es_stderr; _gpgrt_setvbuf (fp, NULL, _IOLBF, 0); logstream = fp; /* We always need to print the prefix and the pid for socket mode, so that the server reading the socket can do something meaningful. */ force_prefixes = want_socket; missing_lf = 0; } /* Set the file to write log to. The special names NULL and "-" may * be used to select stderr and names formatted like * "socket:///home/foo/mylogs" may be used to write the logging to the * socket "/home/foo/mylogs". If the connection to the socket fails * or a write error is detected, the function writes to stderr and * tries the next time again to connect the socket. * Warning: This function is not thread-safe. */ void _gpgrt_log_set_sink (const char *name, estream_t stream, int fd) { if (name && !stream && fd == -1) set_file_fd (name, -1); else if (!name && !stream) { if (!_gpgrt_fd_valid_p (fd)) _gpgrt_log_fatal ("gpgrt_log_set_sink: fd is invalid: %s\n", strerror (errno)); set_file_fd (NULL, fd); } else if (!name && stream && fd == -1) { _gpgrt_log_fatal ("gpgrt_log_set_sink: stream arg not yet supported\n"); } else /* default */ set_file_fd ("-", -1); } /* Set a function to retrieve the directory name of a socket if * only "socket://" has been given to log_set_file. * Warning: This function is not thread-safe. */ void _gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)) { socket_dir_cb = fnc; } /* Warning: This function is not thread-safe. */ void _gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)) { get_pid_suffix_cb = cb; } /* Warning: Changing TEXT is not thread-safe. Changing only flags * might be thread-safe. */ void _gpgrt_log_set_prefix (const char *text, unsigned int flags) { if (text) { strncpy (prefix_buffer, text, sizeof (prefix_buffer)-1); prefix_buffer[sizeof (prefix_buffer)-1] = 0; } with_prefix = (flags & GPGRT_LOG_WITH_PREFIX); with_time = (flags & GPGRT_LOG_WITH_TIME); with_pid = (flags & GPGRT_LOG_WITH_PID); running_detached = (flags & GPGRT_LOG_RUN_DETACHED); #ifdef HAVE_W32_SYSTEM no_registry = (flags & GPGRT_LOG_NO_REGISTRY); #endif } const char * _gpgrt_log_get_prefix (unsigned int *flags) { if (flags) { *flags = 0; if (with_prefix) *flags |= GPGRT_LOG_WITH_PREFIX; if (with_time) *flags |= GPGRT_LOG_WITH_TIME; if (with_pid) *flags |= GPGRT_LOG_WITH_PID; if (running_detached) *flags |= GPGRT_LOG_RUN_DETACHED; #ifdef HAVE_W32_SYSTEM if (no_registry) *flags |= GPGRT_LOG_NO_REGISTRY; #endif } return prefix_buffer; } /* This function returns true if the file descriptor FD is in use for * logging. This is preferable over a test using log_get_fd in that * it allows the logging code to use more then one file descriptor. */ int _gpgrt_log_test_fd (int fd) { if (logstream) { int tmp = _gpgrt_fileno (logstream); if ( tmp != -1 && tmp == fd) return 1; } if (log_socket != -1 && log_socket == fd) return 1; return 0; } int _gpgrt_log_get_fd () { return logstream? _gpgrt_fileno (logstream) : -1; } estream_t _gpgrt_log_get_stream () { if (!logstream) { /* Make sure a log stream has been set. */ _gpgrt_log_set_sink (NULL, NULL, -1); assert (logstream); } return logstream; } /* Note: LOGSTREAM is expected to be locked. */ static int print_prefix (int level, int leading_backspace) { int rc; int length = 0; if (level != GPGRT_LOG_CONT) { /* Note this does not work for multiple line logging as we would * need to print to a buffer first */ if (with_time && !force_prefixes) { struct tm *tp; time_t atime = time (NULL); tp = localtime (&atime); rc = _gpgrt_fprintf_unlocked (logstream, "%04d-%02d-%02d %02d:%02d:%02d ", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec ); if (rc > 0) length += rc; } if (with_prefix || force_prefixes) { _gpgrt_fputs_unlocked (prefix_buffer, logstream); length += strlen (prefix_buffer); } if (with_pid || force_prefixes) { unsigned long pidsuf; int pidfmt; if (get_pid_suffix_cb && (pidfmt=get_pid_suffix_cb (&pidsuf))) rc = _gpgrt_fprintf_unlocked (logstream, pidfmt == 1? "[%u.%lu]":"[%u.%lx]", (unsigned int)getpid (), pidsuf); else rc = _gpgrt_fprintf_unlocked (logstream, "[%u]", (unsigned int)getpid ()); if (rc > 0) length += rc; } if ((!with_time && (with_prefix || with_pid)) || force_prefixes) { _gpgrt_putc_unlocked (':', logstream); length++; } /* A leading backspace suppresses the extra space so that we can correctly output, programname, filename and linenumber. */ if (!leading_backspace && (with_time || with_prefix || with_pid || force_prefixes)) { _gpgrt_putc_unlocked (' ', logstream); length++; } } switch (level) { case GPGRT_LOG_BEGIN: break; case GPGRT_LOG_CONT: break; case GPGRT_LOG_INFO: break; case GPGRT_LOG_WARN: break; case GPGRT_LOG_ERROR: break; case GPGRT_LOG_FATAL: _gpgrt_fputs_unlocked ("Fatal: ", logstream); length += 7; break; case GPGRT_LOG_BUG: _gpgrt_fputs_unlocked ("Ohhhh jeeee: ", logstream); length += 13; break; case GPGRT_LOG_DEBUG: _gpgrt_fputs_unlocked ("DBG: ", logstream); length += 5; break; default: rc = _gpgrt_fprintf_unlocked (logstream, "[Unknown log level %d]: ", level); if (rc > 0) length += rc; break; } return length; } /* Internal worker function. Exported so that we can use it in * visibility.c. Returs the number of characters printed or 0 if the * line ends in a LF. */ int _gpgrt_logv_internal (int level, int ignore_arg_ptr, const char *extrastring, const char *prefmt, const char *fmt, va_list arg_ptr) { int leading_backspace = (fmt && *fmt == '\b'); int length; int rc; if (!logstream) { #ifdef HAVE_W32_SYSTEM char *tmp; tmp = (no_registry ? NULL - : read_w32_registry_string (NULL, GNUPG_REGISTRY_DIR, - "DefaultLogFile")); - log_set_file (tmp && *tmp? tmp : NULL); + : _gpgrt_w32_reg_query_string (NULL, "Software\\\\GNU\\\\GnuPG", + "DefaultLogFile")); + _gpgrt_log_set_sink (tmp && *tmp? tmp : NULL, NULL, -1); _gpgrt_free (tmp); #else /* Make sure a log stream has been set. */ _gpgrt_log_set_sink (NULL, NULL, -1); #endif assert (logstream); } _gpgrt_flockfile (logstream); if (missing_lf && level != GPGRT_LOG_CONT) _gpgrt_putc_unlocked ('\n', logstream ); missing_lf = 0; length = print_prefix (level, leading_backspace); if (leading_backspace) fmt++; if (fmt) { if (prefmt) { _gpgrt_fputs_unlocked (prefmt, logstream); length += strlen (prefmt); } if (ignore_arg_ptr) { /* This is used by log_string and comes with the extra * feature that after a LF the next line is indent at the * length of the prefix. Note that we do not yet include * the length of the timestamp and pid in the indent * computation. */ const char *p, *pend; for (p = fmt; (pend = strchr (p, '\n')); p = pend+1) { rc = _gpgrt_fprintf_unlocked (logstream, "%*s%.*s", (int)((p != fmt && (with_prefix || force_prefixes)) ?strlen (prefix_buffer)+2:0), "", (int)(pend - p)+1, p); if (rc > 0) length += rc; } _gpgrt_fputs_unlocked (p, logstream); length += strlen (p); } else { rc = _gpgrt_vfprintf_unlocked (logstream, fmt, arg_ptr); if (rc > 0) length += rc; } if (*fmt && fmt[strlen(fmt)-1] != '\n') missing_lf = 1; } /* If we have an EXTRASTRING print it now while we still hold the * lock on the logstream. */ if (extrastring) { int c; if (missing_lf) { _gpgrt_putc_unlocked ('\n', logstream); missing_lf = 0; length = 0; } length += print_prefix (level, leading_backspace); _gpgrt_fputs_unlocked (">> ", logstream); length += 3; missing_lf = 1; while ((c = *extrastring++)) { missing_lf = 1; if (c == '\\') { _gpgrt_fputs_unlocked ("\\\\", logstream); length += 2; } else if (c == '\r') { _gpgrt_fputs_unlocked ("\\r", logstream); length += 2; } else if (c == '\n') { _gpgrt_fputs_unlocked ("\\n\n", logstream); length = 0; if (*extrastring) { length += print_prefix (level, leading_backspace); _gpgrt_fputs_unlocked (">> ", logstream); length += 3; } else missing_lf = 0; } else { _gpgrt_putc_unlocked (c, logstream); length++; } } if (missing_lf) { _gpgrt_putc_unlocked ('\n', logstream); length = 0; missing_lf = 0; } } if (level == GPGRT_LOG_FATAL) { if (missing_lf) _gpgrt_putc_unlocked ('\n', logstream); _gpgrt_funlockfile (logstream); exit (2); } else if (level == GPGRT_LOG_BUG) { if (missing_lf) _gpgrt_putc_unlocked ('\n', logstream ); _gpgrt_funlockfile (logstream); /* Using backtrace requires a configure test and to pass * -rdynamic to gcc. Thus we do not enable it now. */ /* { */ /* void *btbuf[20]; */ /* int btidx, btlen; */ /* char **btstr; */ /* btlen = backtrace (btbuf, DIM (btbuf)); */ /* btstr = backtrace_symbols (btbuf, btlen); */ /* if (btstr) */ /* for (btidx=0; btidx < btlen; btidx++) */ /* log_debug ("[%d] %s\n", btidx, btstr[btidx]); */ /* } */ abort (); } else _gpgrt_funlockfile (logstream); /* Bumb the error counter for log_error. */ if (level == GPGRT_LOG_ERROR) { /* Protect against counter overflow. */ if (errorcount < 30000) errorcount++; } return length; } void _gpgrt_log (int level, const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt) ; _gpgrt_logv_internal (level, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } void _gpgrt_logv (int level, const char *fmt, va_list arg_ptr) { _gpgrt_logv_internal (level, 0, NULL, NULL, fmt, arg_ptr); } /* Same as log_logv but PREFIX is printed immediately before FMT. * Note that PREFIX is an additional string and independent of the * prefix set by gpgrt_log_set_prefix. */ void _gpgrt_logv_prefix (int level, const char *prefix, const char *fmt, va_list arg_ptr) { _gpgrt_logv_internal (level, 0, NULL, prefix, fmt, arg_ptr); } static void do_log_ignore_arg (int level, const char *str, ...) { va_list arg_ptr; va_start (arg_ptr, str); _gpgrt_logv_internal (level, 1, NULL, NULL, str, arg_ptr); va_end (arg_ptr); } /* Log STRING at LEVEL but indent from the second line on by the * length of the prefix. */ void _gpgrt_log_string (int level, const char *string) { /* We need a dummy arg_ptr, but there is no portable way to create * one. So we call the _gpgrt_logv_internal function through a * variadic wrapper. */ do_log_ignore_arg (level, string); } void _gpgrt_log_info (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOG_INFO, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } void _gpgrt_log_error (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOG_ERROR, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } void _gpgrt_log_fatal (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOG_FATAL, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void _gpgrt_log_bug (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOG_BUG, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void _gpgrt_log_debug (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } /* The same as log_debug but at the end of the output STRING is * printed with LFs expanded to include the prefix and a final --end-- * marker. */ void _gpgrt_log_debug_string (const char *string, const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, string, NULL, fmt, arg_ptr); va_end (arg_ptr); } void _gpgrt_log_printf (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_internal (fmt ? GPGRT_LOG_CONT : GPGRT_LOG_BEGIN, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } /* Flush the log - this is useful to make sure that the trailing linefeed has been printed. */ void _gpgrt_log_flush (void) { do_log_ignore_arg (GPGRT_LOG_CONT, NULL); } /* Print a hexdump of (BUFFER,LENGTH). With FMT passed as NULL print * just the raw dump, with FMT being an empty string, print a trailing * linefeed, otherwise print an entire debug line with the expanded * FMT followed by a possible wrapped hexdump and a final LF. */ void _gpgrt_logv_printhex (const void *buffer, size_t length, const char *fmt, va_list arg_ptr) { int wrap = 0; int cnt = 0; const unsigned char *p; /* FIXME: This printing is not yet protected by _gpgrt_flockfile. */ if (fmt && *fmt) { _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, NULL, fmt, arg_ptr); wrap = 1; } if (length) { if (wrap) _gpgrt_log_printf (" "); for (p = buffer; length--; p++) { _gpgrt_log_printf ("%02x", *p); if (wrap && ++cnt == 32 && length) { cnt = 0; /* (we indicate continuations with a backslash) */ _gpgrt_log_printf (" \\\n"); - _gpgrt_log_debug (""); + _gpgrt_log_debug ("%s", ""); if (fmt && *fmt) _gpgrt_log_printf (" "); } } } if (fmt) _gpgrt_log_printf ("\n"); } /* Print a hexdump of (BUFFER,LENGTH). With FMT passed as NULL print * just the raw dump, with FMT being an empty string, print a trailing * linefeed, otherwise print an entire debug line with the expanded * FMT followed by the hexdump and a final LF. */ void _gpgrt_log_printhex (const void *buffer, size_t length, const char *fmt, ...) { va_list arg_ptr; if (fmt) { va_start (arg_ptr, fmt); _gpgrt_logv_printhex (buffer, length, fmt, arg_ptr); va_end (arg_ptr); } else _gpgrt_logv_printhex (buffer, length, NULL, NULL); } /* Print a microsecond timestamp followed by FMT. */ void _gpgrt_logv_clock (const char *fmt, va_list arg_ptr) { #if ENABLE_LOG_CLOCK static unsigned long long initial; struct timespec tv; unsigned long long now; char clockbuf[50]; if (clock_gettime (CLOCK_REALTIME, &tv)) { log_debug ("error getting the realtime clock value\n"); return; } now = tv.tv_sec * 1000000000ull; now += tv.tv_nsec; if (!initial) initial = now; snprintf (clockbuf, sizeof clockbuf, "[%6llu] ", (now - initial)/1000); _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, clockbuf, fmt, arg_ptr); #else /*!ENABLE_LOG_CLOCK*/ /* You may need to link with -ltr to use the above code. */ _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, "[no clock] ", fmt, arg_ptr); #endif /*!ENABLE_LOG_CLOCK*/ } /* Print a microsecond timestamp followed by FMT. */ void _gpgrt_log_clock (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_clock (fmt, arg_ptr); va_end (arg_ptr); } void _gpgrt__log_assert (const char *expr, const char *file, int line, const char *func) { #ifdef GPGRT_HAVE_MACRO_FUNCTION _gpgrt_log (GPGRT_LOG_BUG, "Assertion \"%s\" in %s failed (%s:%d)\n", expr, func, file, line); #else /*!GPGRT_HAVE_MACRO_FUNCTION*/ _gpgrt_log (GPGRT_LOG_BUG, "Assertion \"%s\" failed (%s:%d)\n", expr, file, line); #endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ abort (); /* Never called; just to make the compiler happy. */ } diff --git a/src/visibility.c b/src/visibility.c index 888492a..01ed2ce 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -1,977 +1,992 @@ /* visibility.c - Wrapper for all public functions. * Copyright (C) 2014 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #include #include #include /* For abort(). */ #define _GPGRT_INCL_BY_VISIBILITY_C 1 #include "gpgrt-int.h" const char * gpg_strerror (gpg_error_t err) { return _gpg_strerror (err); } int gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen) { return _gpg_strerror_r (err, buf, buflen); } const char * gpg_strsource (gpg_error_t err) { return _gpg_strsource (err); } gpg_err_code_t gpg_err_code_from_errno (int err) { return _gpg_err_code_from_errno (err); } int gpg_err_code_to_errno (gpg_err_code_t code) { return _gpg_err_code_to_errno (code); } gpg_err_code_t gpg_err_code_from_syserror (void) { return _gpg_err_code_from_syserror (); } void gpg_err_set_errno (int err) { _gpg_err_set_errno (err); } gpg_error_t gpg_err_init (void) { return _gpg_err_init (); } void gpg_err_deinit (int mode) { _gpg_err_deinit (mode); } const char * gpg_error_check_version (const char *req_version) { return _gpg_error_check_version (req_version); } const char * gpgrt_check_version (const char *req_version) { return _gpg_error_check_version (req_version); } void gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void)) { _gpgrt_set_syscall_clamp (pre, post); } void gpgrt_get_syscall_clamp (void (**r_pre)(void), void (**r_post)(void)) { _gpgrt_get_syscall_clamp (r_pre, r_post); } void gpgrt_set_alloc_func (void *(*f)(void *a, size_t n)) { _gpgrt_set_alloc_func (f); } gpg_err_code_t gpgrt_lock_init (gpgrt_lock_t *lockhd) { return _gpgrt_lock_init (lockhd); } gpg_err_code_t gpgrt_lock_lock (gpgrt_lock_t *lockhd) { return _gpgrt_lock_lock (lockhd); } gpg_err_code_t gpgrt_lock_trylock (gpgrt_lock_t *lockhd) { return _gpgrt_lock_trylock (lockhd); } gpg_err_code_t gpgrt_lock_unlock (gpgrt_lock_t *lockhd) { return _gpgrt_lock_unlock (lockhd); } gpg_err_code_t gpgrt_lock_destroy (gpgrt_lock_t *lockhd) { return _gpgrt_lock_destroy (lockhd); } gpg_err_code_t gpgrt_yield (void) { return _gpgrt_yield (); } estream_t gpgrt_fopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode) { return _gpgrt_fopen (path, mode); } estream_t gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, void *(*func_realloc) (void *mem, size_t size), void (*func_free) (void *mem), const char *_GPGRT__RESTRICT mode) { return _gpgrt_mopen (data, data_n, data_len, grow, func_realloc, func_free, mode); } estream_t gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode) { return _gpgrt_fopenmem (memlimit, mode); } estream_t gpgrt_fopenmem_init (size_t memlimit, const char *_GPGRT__RESTRICT mode, const void *data, size_t datalen) { return _gpgrt_fopenmem_init (memlimit, mode, data, datalen); } estream_t gpgrt_fdopen (int filedes, const char *mode) { return _gpgrt_fdopen (filedes, mode); } estream_t gpgrt_fdopen_nc (int filedes, const char *mode) { return _gpgrt_fdopen_nc (filedes, mode); } estream_t gpgrt_sysopen (es_syshd_t *syshd, const char *mode) { return _gpgrt_sysopen (syshd, mode); } estream_t gpgrt_sysopen_nc (es_syshd_t *syshd, const char *mode) { return _gpgrt_sysopen_nc (syshd, mode); } estream_t gpgrt_fpopen (FILE *fp, const char *mode) { return _gpgrt_fpopen (fp, mode); } estream_t gpgrt_fpopen_nc (FILE *fp, const char *mode) { return _gpgrt_fpopen_nc (fp, mode); } estream_t gpgrt_freopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_freopen (path, mode, stream); } estream_t gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie, const char *_GPGRT__RESTRICT mode, gpgrt_cookie_io_functions_t functions) { return _gpgrt_fopencookie (cookie, mode, functions); } int gpgrt_fclose (estream_t stream) { return _gpgrt_fclose (stream); } int gpgrt_fclose_snatch (estream_t stream, void **r_buffer, size_t *r_buflen) { return _gpgrt_fclose_snatch (stream, r_buffer, r_buflen); } int gpgrt_onclose (estream_t stream, int mode, void (*fnc) (estream_t, void*), void *fnc_value) { return _gpgrt_onclose (stream, mode, fnc, fnc_value); } int gpgrt_fileno (estream_t stream) { return _gpgrt_fileno (stream); } int gpgrt_fileno_unlocked (estream_t stream) { return _gpgrt_fileno_unlocked (stream); } int gpgrt_syshd (estream_t stream, es_syshd_t *syshd) { return _gpgrt_syshd (stream, syshd); } int gpgrt_syshd_unlocked (estream_t stream, es_syshd_t *syshd) { return _gpgrt_syshd_unlocked (stream, syshd); } void _gpgrt_set_std_fd (int no, int fd) { _gpgrt__set_std_fd (no, fd); /* (double dash in name) */ } estream_t _gpgrt_get_std_stream (int fd) { return _gpgrt__get_std_stream (fd); /* (double dash in name) */ } void gpgrt_flockfile (estream_t stream) { _gpgrt_flockfile (stream); } int gpgrt_ftrylockfile (estream_t stream) { return _gpgrt_ftrylockfile (stream); } void gpgrt_funlockfile (estream_t stream) { _gpgrt_funlockfile (stream); } int _gpgrt_pending (estream_t stream) { return _gpgrt__pending (stream); } int _gpgrt_pending_unlocked (estream_t stream) { return _gpgrt__pending_unlocked (stream); } int gpgrt_feof (estream_t stream) { return _gpgrt_feof (stream); } int gpgrt_feof_unlocked (estream_t stream) { return _gpgrt_feof_unlocked (stream); } int gpgrt_ferror (estream_t stream) { return _gpgrt_ferror (stream); } int gpgrt_ferror_unlocked (estream_t stream) { return _gpgrt_ferror_unlocked (stream); } void gpgrt_clearerr (estream_t stream) { _gpgrt_clearerr (stream); } void gpgrt_clearerr_unlocked (estream_t stream) { _gpgrt_clearerr_unlocked (stream); } int gpgrt_fflush (estream_t stream) { return _gpgrt_fflush (stream); } int gpgrt_fseek (estream_t stream, long int offset, int whence) { return _gpgrt_fseek (stream, offset, whence); } int gpgrt_fseeko (estream_t stream, gpgrt_off_t offset, int whence) { return _gpgrt_fseeko (stream, offset, whence); } long int gpgrt_ftell (estream_t stream) { return _gpgrt_ftell (stream); } gpgrt_off_t gpgrt_ftello (estream_t stream) { return _gpgrt_ftello (stream); } void gpgrt_rewind (estream_t stream) { _gpgrt_rewind (stream); } int gpgrt_fgetc (estream_t stream) { return _gpgrt_fgetc (stream); } int _gpgrt_getc_underflow (estream_t stream) { return _gpgrt__getc_underflow (stream); } int gpgrt_fputc (int c, estream_t stream) { return _gpgrt_fputc (c, stream); } int _gpgrt_putc_overflow (int c, estream_t stream) { return _gpgrt__putc_overflow (c, stream); } int gpgrt_ungetc (int c, estream_t stream) { return _gpgrt_ungetc (c, stream); } int gpgrt_read (estream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read) { return _gpgrt_read (stream, buffer, bytes_to_read, bytes_read); } int gpgrt_write (estream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written) { return _gpgrt_write (stream, buffer, bytes_to_write, bytes_written); } int gpgrt_write_sanitized (estream_t _GPGRT__RESTRICT stream, const void * _GPGRT__RESTRICT buffer, size_t length, const char * delimiters, size_t * _GPGRT__RESTRICT bytes_written) { return _gpgrt_write_sanitized (stream, buffer, length, delimiters, bytes_written); } int gpgrt_write_hexstring (estream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, int reserved, size_t *_GPGRT__RESTRICT bytes_written ) { return _gpgrt_write_hexstring (stream, buffer, length, reserved, bytes_written); } size_t gpgrt_fread (void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fread (ptr, size, nitems, stream); } size_t gpgrt_fwrite (const void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fwrite (ptr, size, nitems, stream); } char * gpgrt_fgets (char *_GPGRT__RESTRICT buffer, int length, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fgets (buffer, length, stream); } int gpgrt_fputs (const char *_GPGRT__RESTRICT s, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fputs (s, stream); } int gpgrt_fputs_unlocked (const char *_GPGRT__RESTRICT s, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fputs_unlocked (s, stream); } gpgrt_ssize_t gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr, size_t *_GPGRT__RESTRICT n, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_getline (lineptr, n, stream); } gpgrt_ssize_t gpgrt_read_line (estream_t stream, char **addr_of_buffer, size_t *length_of_buffer, size_t *max_length) { return _gpgrt_read_line (stream, addr_of_buffer, length_of_buffer, max_length); } void * gpgrt_realloc (void *a, size_t n) { return _gpgrt_realloc (a, n); } void * gpgrt_malloc (size_t n) { return _gpgrt_malloc (n); } void * gpgrt_calloc (size_t n, size_t m) { return _gpgrt_calloc (n, m); } char * gpgrt_strdup (const char *string) { return _gpgrt_strdup (string); } char * gpgrt_strconcat (const char *s1, ...) { va_list arg_ptr; char *result; if (!s1) result = _gpgrt_strdup (""); else { va_start (arg_ptr, s1); result = _gpgrt_strconcat_core (s1, arg_ptr); va_end (arg_ptr); } return result; } void gpgrt_free (void *a) { if (a) _gpgrt_free (a); } int gpgrt_vfprintf (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) { return _gpgrt_vfprintf (stream, format, ap); } int gpgrt_vfprintf_unlocked (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) { return _gpgrt_vfprintf_unlocked (stream, format, ap); } int gpgrt_printf (const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf (es_stdout, format, ap); va_end (ap); return rc; } int gpgrt_printf_unlocked (const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf_unlocked (es_stdout, format, ap); va_end (ap); return rc; } int gpgrt_fprintf (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf (stream, format, ap); va_end (ap); return rc; } int gpgrt_fprintf_unlocked (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf_unlocked (stream, format, ap); va_end (ap); return rc; } int gpgrt_setvbuf (estream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf, int type, size_t size) { return _gpgrt_setvbuf (stream, buf, type, size); } void gpgrt_setbuf (estream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf) { _gpgrt_setvbuf (stream, buf, buf? _IOFBF : _IONBF, BUFSIZ); } void gpgrt_set_binary (estream_t stream) { _gpgrt_set_binary (stream); } int gpgrt_set_nonblock (estream_t stream, int onoff) { return _gpgrt_set_nonblock (stream, onoff); } int gpgrt_get_nonblock (estream_t stream) { return _gpgrt_get_nonblock (stream); } int gpgrt_poll (gpgrt_poll_t *fds, unsigned int nfds, int timeout) { return _gpgrt_poll (fds, nfds, timeout); } estream_t gpgrt_tmpfile (void) { return _gpgrt_tmpfile (); } void gpgrt_opaque_set (estream_t stream, void *opaque) { _gpgrt_opaque_set (stream, opaque); } void * gpgrt_opaque_get (estream_t stream) { return _gpgrt_opaque_get (stream); } void gpgrt_fname_set (estream_t stream, const char *fname) { _gpgrt_fname_set (stream, fname); } const char * gpgrt_fname_get (estream_t stream) { return _gpgrt_fname_get (stream); } int gpgrt_asprintf (char **r_buf, const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_estream_vasprintf (r_buf, format, ap); va_end (ap); return rc; } int gpgrt_vasprintf (char **r_buf, const char *_GPGRT__RESTRICT format, va_list ap) { return _gpgrt_estream_vasprintf (r_buf, format, ap); } char * gpgrt_bsprintf (const char *_GPGRT__RESTRICT format, ...) { int rc; va_list ap; char *buf; va_start (ap, format); rc = _gpgrt_estream_vasprintf (&buf, format, ap); va_end (ap); if (rc < 0) return NULL; return buf; } char * gpgrt_vbsprintf (const char *_GPGRT__RESTRICT format, va_list ap) { int rc; char *buf; rc = _gpgrt_estream_vasprintf (&buf, format, ap); if (rc < 0) return NULL; return buf; } int gpgrt_snprintf (char *buf, size_t bufsize, const char *format, ...) { int rc; va_list arg_ptr; va_start (arg_ptr, format); rc = _gpgrt_estream_vsnprintf (buf, bufsize, format, arg_ptr); va_end (arg_ptr); return rc; } int gpgrt_vsnprintf (char *buf, size_t bufsize, const char *format, va_list arg_ptr) { return _gpgrt_estream_vsnprintf (buf, bufsize, format, arg_ptr); } gpgrt_b64state_t gpgrt_b64dec_start (const char *title) { return _gpgrt_b64dec_start (title); } gpg_error_t gpgrt_b64dec_proc (gpgrt_b64state_t state, void *buffer, size_t length, size_t *r_nbytes) { return _gpgrt_b64dec_proc (state, buffer, length, r_nbytes); } gpg_error_t gpgrt_b64dec_finish (gpgrt_b64state_t state) { return _gpgrt_b64dec_finish (state); } int gpgrt_get_errorcount (int clear) { return _gpgrt_get_errorcount (clear); } void gpgrt_inc_errorcount (void) { _gpgrt_inc_errorcount (); } void gpgrt_log_set_sink (const char *name, estream_t stream, int fd) { _gpgrt_log_set_sink (name, stream, fd); } void gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)) { _gpgrt_log_set_socket_dir_cb (fnc); } void gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)) { _gpgrt_log_set_pid_suffix_cb (cb); } void gpgrt_log_set_prefix (const char *text, unsigned int flags) { _gpgrt_log_set_prefix (text, flags); } const char * gpgrt_log_get_prefix (unsigned int *flags) { return _gpgrt_log_get_prefix (flags); } int gpgrt_log_test_fd (int fd) { return _gpgrt_log_test_fd (fd); } int gpgrt_log_get_fd (void) { return _gpgrt_log_get_fd (); } estream_t gpgrt_log_get_stream (void) { return _gpgrt_log_get_stream (); } void gpgrt_log (int level, const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt) ; _gpgrt_logv (level, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_logv (int level, const char *fmt, va_list arg_ptr) { _gpgrt_logv (level, fmt, arg_ptr); } void gpgrt_logv_prefix (int level, const char *prefix, const char *fmt, va_list arg_ptr) { _gpgrt_logv_prefix (level, prefix, fmt, arg_ptr); } void gpgrt_log_string (int level, const char *string) { _gpgrt_log_string (level, string); } void gpgrt_log_info (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOG_INFO, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_error (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOG_ERROR, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_fatal (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOG_FATAL, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void gpgrt_log_bug (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOG_BUG, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void gpgrt_log_debug (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOG_DEBUG, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_debug_string (const char *string, const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, string, NULL, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_printf (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (fmt ? GPGRT_LOG_CONT : GPGRT_LOG_BEGIN, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_flush (void) { _gpgrt_log_flush (); } void gpgrt_log_printhex (const void *buffer, size_t length, const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_printhex (buffer, length, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_clock (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_clock (fmt, arg_ptr); va_end (arg_ptr); } void _gpgrt_log_assert (const char *expr, const char *file, int line, const char *func) { #ifdef GPGRT_HAVE_MACRO_FUNCTION _gpgrt__log_assert (expr, file, line, func); #else _gpgrt__log_assert (expr, file, line); #endif } + + + + +/* For consistency reasons we use function wrappers also for Windows + * specific function despite that they are technically not needed. */ +#ifdef HAVE_W32_SYSTEM + +char * +gpgrt_w32_reg_query_string (const char *root, const char *dir, const char *name) +{ + return _gpgrt_w32_reg_query_string (root, dir, name); +} + +#endif /*HAVE_W32_SYSTEM*/ diff --git a/src/visibility.h b/src/visibility.h index c4f4fd1..416f266 100644 --- a/src/visibility.h +++ b/src/visibility.h @@ -1,331 +1,336 @@ /* visibility.h - Set visibility attribute * Copyright (C) 2014 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #ifndef _GPGRT_VISIBILITY_H #define _GPGRT_VISIBILITY_H /* Include the main header here so that public symbols are mapped to the internal underscored ones. */ #ifdef _GPGRT_INCL_BY_VISIBILITY_C # include "gpgrt-int.h" #endif /* Our use of the ELF visibility feature works by passing -fvisibiliy=hidden on the command line and by explicitly marking all exported functions as visible. NOTE: When adding new functions, please make sure to add them to gpg-error.vers and gpg-error.def.in as well. */ #ifdef _GPGRT_INCL_BY_VISIBILITY_C # ifdef GPGRT_USE_VISIBILITY # define MARK_VISIBLE(name) \ extern __typeof__ (name) name __attribute__ ((visibility("default"))); # else # define MARK_VISIBLE(name) /* */ # endif MARK_VISIBLE (gpg_strerror) MARK_VISIBLE (gpg_strerror_r) MARK_VISIBLE (gpg_strsource) MARK_VISIBLE (gpg_err_code_from_errno) MARK_VISIBLE (gpg_err_code_to_errno) MARK_VISIBLE (gpg_err_code_from_syserror) MARK_VISIBLE (gpg_err_set_errno) MARK_VISIBLE (gpg_err_init) MARK_VISIBLE (gpg_err_deinit) MARK_VISIBLE (gpg_error_check_version) MARK_VISIBLE (gpgrt_check_version) MARK_VISIBLE (gpgrt_lock_init) MARK_VISIBLE (gpgrt_lock_lock) MARK_VISIBLE (gpgrt_lock_unlock) MARK_VISIBLE (gpgrt_lock_destroy) MARK_VISIBLE (gpgrt_yield) MARK_VISIBLE (gpgrt_lock_trylock) MARK_VISIBLE (gpgrt_fopen) MARK_VISIBLE (gpgrt_mopen) MARK_VISIBLE (gpgrt_fopenmem) MARK_VISIBLE (gpgrt_fopenmem_init) MARK_VISIBLE (gpgrt_fdopen) MARK_VISIBLE (gpgrt_fdopen_nc) MARK_VISIBLE (gpgrt_sysopen) MARK_VISIBLE (gpgrt_sysopen_nc) MARK_VISIBLE (gpgrt_fpopen) MARK_VISIBLE (gpgrt_fpopen_nc) MARK_VISIBLE (gpgrt_freopen) MARK_VISIBLE (gpgrt_fopencookie) MARK_VISIBLE (gpgrt_fclose) MARK_VISIBLE (gpgrt_fclose_snatch) MARK_VISIBLE (gpgrt_onclose) MARK_VISIBLE (gpgrt_fileno) MARK_VISIBLE (gpgrt_fileno_unlocked) MARK_VISIBLE (gpgrt_syshd) MARK_VISIBLE (gpgrt_syshd_unlocked) MARK_VISIBLE (_gpgrt_set_std_fd) MARK_VISIBLE (_gpgrt_get_std_stream) MARK_VISIBLE (gpgrt_flockfile) MARK_VISIBLE (gpgrt_ftrylockfile) MARK_VISIBLE (gpgrt_funlockfile) MARK_VISIBLE (_gpgrt_pending) MARK_VISIBLE (_gpgrt_pending_unlocked) MARK_VISIBLE (gpgrt_feof) MARK_VISIBLE (gpgrt_feof_unlocked) MARK_VISIBLE (gpgrt_ferror) MARK_VISIBLE (gpgrt_ferror_unlocked) MARK_VISIBLE (gpgrt_clearerr) MARK_VISIBLE (gpgrt_clearerr_unlocked) MARK_VISIBLE (gpgrt_fflush) MARK_VISIBLE (gpgrt_fseek) MARK_VISIBLE (gpgrt_fseeko) MARK_VISIBLE (gpgrt_ftell) MARK_VISIBLE (gpgrt_ftello) MARK_VISIBLE (gpgrt_rewind) MARK_VISIBLE (gpgrt_fgetc) MARK_VISIBLE (_gpgrt_getc_underflow) MARK_VISIBLE (gpgrt_fputc) MARK_VISIBLE (_gpgrt_putc_overflow) MARK_VISIBLE (gpgrt_ungetc) MARK_VISIBLE (gpgrt_read) MARK_VISIBLE (gpgrt_write) MARK_VISIBLE (gpgrt_write_sanitized) MARK_VISIBLE (gpgrt_write_hexstring) MARK_VISIBLE (gpgrt_fread) MARK_VISIBLE (gpgrt_fwrite) MARK_VISIBLE (gpgrt_fgets) MARK_VISIBLE (gpgrt_fputs) MARK_VISIBLE (gpgrt_fputs_unlocked) MARK_VISIBLE (gpgrt_getline) MARK_VISIBLE (gpgrt_read_line) MARK_VISIBLE (gpgrt_realloc) MARK_VISIBLE (gpgrt_malloc) MARK_VISIBLE (gpgrt_calloc) MARK_VISIBLE (gpgrt_strdup) MARK_VISIBLE (gpgrt_strconcat) MARK_VISIBLE (gpgrt_free) MARK_VISIBLE (gpgrt_fprintf) MARK_VISIBLE (gpgrt_fprintf_unlocked) MARK_VISIBLE (gpgrt_printf) MARK_VISIBLE (gpgrt_printf_unlocked) MARK_VISIBLE (gpgrt_vfprintf) MARK_VISIBLE (gpgrt_vfprintf_unlocked) MARK_VISIBLE (gpgrt_setvbuf) MARK_VISIBLE (gpgrt_setbuf) MARK_VISIBLE (gpgrt_set_binary) MARK_VISIBLE (gpgrt_set_nonblock) MARK_VISIBLE (gpgrt_get_nonblock) MARK_VISIBLE (gpgrt_poll) MARK_VISIBLE (gpgrt_tmpfile) MARK_VISIBLE (gpgrt_opaque_set) MARK_VISIBLE (gpgrt_opaque_get) MARK_VISIBLE (gpgrt_fname_set) MARK_VISIBLE (gpgrt_fname_get) MARK_VISIBLE (gpgrt_asprintf) MARK_VISIBLE (gpgrt_vasprintf) MARK_VISIBLE (gpgrt_bsprintf) MARK_VISIBLE (gpgrt_vbsprintf) MARK_VISIBLE (gpgrt_snprintf) MARK_VISIBLE (gpgrt_vsnprintf) MARK_VISIBLE (gpgrt_set_syscall_clamp) MARK_VISIBLE (gpgrt_get_syscall_clamp) MARK_VISIBLE (gpgrt_set_alloc_func) MARK_VISIBLE (gpgrt_b64dec_start) MARK_VISIBLE (gpgrt_b64dec_proc) MARK_VISIBLE (gpgrt_b64dec_finish) MARK_VISIBLE (gpgrt_get_errorcount) MARK_VISIBLE (gpgrt_inc_errorcount) MARK_VISIBLE (gpgrt_log_set_sink) MARK_VISIBLE (gpgrt_log_set_socket_dir_cb) MARK_VISIBLE (gpgrt_log_set_pid_suffix_cb) MARK_VISIBLE (gpgrt_log_set_prefix) MARK_VISIBLE (gpgrt_log_get_prefix) MARK_VISIBLE (gpgrt_log_test_fd) MARK_VISIBLE (gpgrt_log_get_fd) MARK_VISIBLE (gpgrt_log_get_stream) MARK_VISIBLE (gpgrt_log) MARK_VISIBLE (gpgrt_logv) MARK_VISIBLE (gpgrt_logv_prefix) MARK_VISIBLE (gpgrt_log_string) MARK_VISIBLE (gpgrt_log_bug) MARK_VISIBLE (gpgrt_log_fatal) MARK_VISIBLE (gpgrt_log_error) MARK_VISIBLE (gpgrt_log_info) MARK_VISIBLE (gpgrt_log_debug) MARK_VISIBLE (gpgrt_log_debug_string) MARK_VISIBLE (gpgrt_log_printf) MARK_VISIBLE (gpgrt_log_printhex) MARK_VISIBLE (gpgrt_log_clock) MARK_VISIBLE (gpgrt_log_flush) MARK_VISIBLE (_gpgrt_log_assert) #undef MARK_VISIBLE #else /*!_GPGRT_INCL_BY_VISIBILITY_C*/ /* To avoid accidental use of the public functions inside Libgpg-error, we redefine them to catch such errors. */ #define gpg_strerror _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_strerror_r _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_strsource _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_code_from_errno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_code_to_errno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_code_from_syserror _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_set_errno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_init _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_deinit _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_error_check_version _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_check_version _gpgrt_USE_OTHER_FUNCTION #define gpgrt_lock_init _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_lock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_unlock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_destroy _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_yield _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_trylock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_mopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopenmem _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopenmem_init _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fdopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fdopen_nc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_sysopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_sysopen_nc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fpopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fpopen_nc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_freopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopencookie _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fclose _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fclose_snatch _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_onclose _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fileno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fileno_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_syshd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_syshd_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_set_std_fd _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_get_std_stream _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_flockfile _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ftrylockfile _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_funlockfile _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_pending _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_pending_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_feof _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_feof_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ferror _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ferror_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_clearerr _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_clearerr_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fflush _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fseek _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fseeko _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ftell _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ftello _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_rewind _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fgetc _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_getc_underflow _gpgrt_USE_DBLUNDERSCO_FUNCTION #define gpgrt_fputc _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_putc_overflow _gpgrt_USE_DBLUNDERSCO_FUNCTION #define gpgrt_ungetc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_read _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_write _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_write_sanitized _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_write_hexstring _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fread _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fwrite _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fgets _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fputs _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fputs_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_getline _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_read_line _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_realloc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_malloc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_calloc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_strdup _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_strconcat _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_free _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_printf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_printf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vfprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vfprintf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_setvbuf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_setbuf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_binary _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_nonblock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_get_nonblock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_poll _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_tmpfile _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_opaque_set _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_opaque_get _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fname_set _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fname_get _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_asprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vasprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_bsprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vbsprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_snprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vsnprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_get_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_alloc_func _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64dec_start _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64dec_proc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64dec_finish _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_get_errorcount _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_inc_errorcount _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_sink _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_socket_dir_cb _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_pid_suffix_cb _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_prefix _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_get_prefix _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_test_fd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_get_fd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_get_stream _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_logv _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_logv_prefix _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_string _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_bug _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_fatal _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_error _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_info _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_debug _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_debug_string _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_printf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_printhex _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_clock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_flush _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_log_assert _gpgrt_USE_UNDERSCORED_FUNCTION + +/* Windows specific functions. */ +#define gpgrt_w32_reg_query_string _gpgrt_USE_UNDERSCORED_FUNCTION + + #endif /*!_GPGRT_INCL_BY_VISIBILITY_C*/ #endif /*_GPGRT_VISIBILITY_H*/ diff --git a/src/w32-add.h b/src/w32-add.h index 5db6500..07e3c7d 100644 --- a/src/w32-add.h +++ b/src/w32-add.h @@ -1,58 +1,63 @@ ## w32-add.h - Snippet to be be included into gpg-error.h. ## Comments are indicated by a double hash mark. Due to a ## peculiarity of the script the first used line must not ## start with a hash mark. /* Decide whether to use the format_arg attribute. */ #if _GPG_ERR_GCC_VERSION > 20800 # define _GPG_ERR_ATTR_FORMAT_ARG(a) __attribute__ ((__format_arg__ (a))) #else # define _GPG_ERR_ATTR_FORMAT_ARG(a) #endif /* A lean gettext implementation based on GNU style mo files which are required to be encoded in UTF-8. There is a limit on 65534 entries to save some RAM. Only Germanic plural rules are supported. */ const char *_gpg_w32_bindtextdomain (const char *domainname, const char *dirname); const char *_gpg_w32_textdomain (const char *domainname); const char *_gpg_w32_gettext (const char *msgid) _GPG_ERR_ATTR_FORMAT_ARG (1); const char *_gpg_w32_dgettext (const char *domainname, const char *msgid) _GPG_ERR_ATTR_FORMAT_ARG (2); const char *_gpg_w32_dngettext (const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n) _GPG_ERR_ATTR_FORMAT_ARG (2) _GPG_ERR_ATTR_FORMAT_ARG (3); const char *_gpg_w32_gettext_localename (void); int _gpg_w32_gettext_use_utf8 (int value); #ifdef GPG_ERR_ENABLE_GETTEXT_MACROS # define bindtextdomain(a,b) _gpg_w32_bindtextdomain ((a), (b)) # define textdomain(a) _gpg_w32_textdomain ((a)) # define gettext(a) _gpg_w32_gettext ((a)) # define dgettext(a,b) _gpg_w32_dgettext ((a), (b)) # define ngettext(a,b,c) _gpg_w32_dngettext (NULL, (a), (b), (c)) # define dngettext(a,b,c,d) _gpg_w32_dngettext ((a), (b), (c), (d)) # define gettext_localename() _gpg_w32_gettext_localename () # define gettext_use_utf8(a) _gpg_w32_gettext_use_utf8 (a) #endif /*GPG_ERR_ENABLE_GETTEXT_MACROS*/ /* A simple iconv implementation w/o the need for an extra DLL. */ struct _gpgrt_w32_iconv_s; typedef struct _gpgrt_w32_iconv_s *gpgrt_w32_iconv_t; gpgrt_w32_iconv_t gpgrt_w32_iconv_open (const char *tocode, const char *fromcode); int gpgrt_w32_iconv_close (gpgrt_w32_iconv_t cd); size_t gpgrt_w32_iconv (gpgrt_w32_iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft); #ifdef GPGRT_ENABLE_W32_ICONV_MACROS # define ICONV_CONST const # define iconv_t gpgrt_w32_iconv_t # define iconv_open(a,b) gpgrt_w32_iconv_open ((a), (b)) # define iconv_close(a) gpgrt_w32_iconv_close ((a)) # define iconv(a,b,c,d,e) gpgrt_w32_iconv ((a),(b),(c),(d),(e)) #endif /*GPGRT_ENABLE_W32_ICONV_MACROS*/ + +/* Query a string in the registry. */ +char *gpgrt_w32_reg_query_string (const char *root, + const char *dir, + const char *name); diff --git a/src/w32-reg.c b/src/w32-reg.c new file mode 100644 index 0000000..8aefa52 --- /dev/null +++ b/src/w32-reg.c @@ -0,0 +1,150 @@ +/* w32-reg.c - Windows registry support + * Copyright (C) 2002, 2005, 2010, 2012, 2017 g10 Code GmbH + * + * This file is part of Libgpg-error. + * + * Libgpg-error is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * Libgpg-error 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1+ + */ + +#if HAVE_CONFIG_H +#include +#endif + +#ifndef HAVE_W32_SYSTEM +# error This module may only be build for Windows. +#endif + +#include +#include +#include +#include +#include +#define WIN32_LEAN_AND_MEAN +#include + +#include "gpgrt-int.h" + + +/* Return a string from the W32 Registry or NULL in case of error. + * Caller must release the return value. A NULL for root is an alias + * for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */ +char * +_gpgrt_w32_reg_query_string (const char *root, const char *dir, + const char *name) +{ + HKEY root_key, key_handle; + DWORD n1, nbytes, type; + char *result = NULL; + + if (!root) + root_key = HKEY_CURRENT_USER; + else if (!strcmp( root, "HKEY_CLASSES_ROOT")) + root_key = HKEY_CLASSES_ROOT; + else if (!strcmp( root, "HKEY_CURRENT_USER")) + root_key = HKEY_CURRENT_USER; + else if (!strcmp( root, "HKEY_LOCAL_MACHINE")) + root_key = HKEY_LOCAL_MACHINE; + else if (!strcmp( root, "HKEY_USERS")) + root_key = HKEY_USERS; + else if (!strcmp( root, "HKEY_PERFORMANCE_DATA")) + root_key = HKEY_PERFORMANCE_DATA; + else if (!strcmp( root, "HKEY_CURRENT_CONFIG")) + root_key = HKEY_CURRENT_CONFIG; + else + return NULL; + + if (RegOpenKeyExA (root_key, dir, 0, KEY_READ, &key_handle)) + { + if (root) + return NULL; /* No need for a RegClose, so return direct. */ + /* It seems to be common practise to fall back to HKLM. */ + if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle)) + return NULL; /* still no need for a RegClose, so return direct */ + } + + nbytes = 1; + if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes)) + { + if (root) + goto leave; + /* Try to fallback to HKLM also for a missing value. */ + RegCloseKey (key_handle); + if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle)) + return NULL; /* Nope. */ + if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes)) + goto leave; + } + n1 = nbytes + 1; + result = xtrymalloc (n1); + if (!result) + goto leave; + if (RegQueryValueExA (key_handle, name, 0, &type, (LPBYTE) result, &n1)) + { + xfree (result); + result = NULL; + goto leave; + } + result[nbytes] = 0; /* Make sure it is really a string. */ + +#ifndef HAVE_W32CE_SYSTEM /* (Windows CE has no environment.) */ + if (type == REG_EXPAND_SZ && strchr (result, '%')) + { + char *tmp; + + n1 += 1000; + tmp = xtrymalloc (n1 + 1); + if (!tmp) + goto leave; + nbytes = ExpandEnvironmentStrings (result, tmp, n1); + if (nbytes && nbytes > n1) + { + xfree (tmp); + n1 = nbytes; + tmp = xtrymalloc (n1 + 1); + if (!tmp) + goto leave; + nbytes = ExpandEnvironmentStrings (result, tmp, n1); + if (nbytes && nbytes > n1) { + xfree (tmp); /* Oops - truncated, better don't expand at all. */ + goto leave; + } + tmp[nbytes] = 0; + xfree (result); + result = tmp; + } + else if (nbytes) /* Okay, reduce the length. */ + { + tmp[nbytes] = 0; + xfree (result); + result = xtrymalloc (strlen (tmp)+1); + if (!result) + result = tmp; + else + { + strcpy (result, tmp); + xfree (tmp); + } + } + else /* Error - don't expand. */ + { + xfree (tmp); + } + } +#endif + + leave: + RegCloseKey (key_handle); + return result; +}