From 4f128bd9f0e06c914dddce7e7ff7bccded1a1061 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 15 Mar 2018 17:20:56 +0900 Subject: [PATCH] Fix runtime directory mechanism on MinGW environment pwd.h is not provided in mingw32 enrionment. This bug applies to current git e8d569045c7d224e94836edd77856823aadf8267 as well as to current release 0.22.10. The build fails because of: common/runtime.c:40:17: fatal error: pwd.h: No such file or directory #include ^ compilation terminated. Makefile:3123: recipe for target 'common/runtime.lo' failed make[2]: *** [common/runtime.lo] Error 1 mingw32 environment does not get_uid() and its related Unix password database functionality to obtain user specific runtime directory. This functionality is possibly to be replaced with SHGetFolderPath WIN32API. --- common/compat.h | 4 ++++ common/runtime.c | 27 ++++++++++++++++++++++++++- configure.ac | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/common/compat.h b/common/compat.h index a9d2fe1..e7a8670 100644 --- a/common/compat.h +++ b/common/compat.h @@ -110,6 +110,10 @@ void p11_dl_close (void * dl); #define WIN32_LEAN_AND_MEAN 1 #include +#ifndef CSIDL_APPDATA +#define CSIDL_APPDATA 0x001A +#endif + #include /* Oh ... my ... god */ diff --git a/common/runtime.c b/common/runtime.c index f713e7d..3c379ac 100644 --- a/common/runtime.c +++ b/common/runtime.c @@ -37,28 +37,41 @@ #include "runtime.h" #include "compat.h" +#ifdef HAVE_PWD_H #include +#endif +#ifdef OS_WIN32 +#include +#include +#endif #include #include #include #include #include +#ifdef HAVE_PWD_H static const char * const _p11_runtime_bases_default[] = { "/run", "/var/run", NULL }; const char * const *_p11_runtime_bases = _p11_runtime_bases_default; +#endif CK_RV p11_get_runtime_directory (char **directoryp) { const char *envvar; + char *directory; +#ifdef HAVE_PWD_H const char * const *bases = _p11_runtime_bases; char prefix[13 + 1 + 20 + 6 + 1]; - char *directory; uid_t uid; struct stat sb; struct passwd pwbuf, *pw; char buf[1024]; int i; +#endif +#ifdef OS_WIN32 + char homeDirStr[MAX_PATH]; +#endif /* We can't always assume the XDG_RUNTIME_DIR envvar here, * because the PKCS#11 module can be loaded by a program that @@ -74,6 +87,7 @@ p11_get_runtime_directory (char **directoryp) return CKR_OK; } +#ifdef HAVE_PWD_H uid = getuid (); for (i = 0; bases[i] != NULL; i++) { @@ -87,6 +101,7 @@ p11_get_runtime_directory (char **directoryp) return CKR_OK; } } +#endif /* We can't use /run/user/, fallback to ~/.cache. */ envvar = secure_getenv ("XDG_CACHE_HOME"); @@ -100,12 +115,22 @@ p11_get_runtime_directory (char **directoryp) return CKR_OK; } +#ifdef OS_WIN32 + if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, homeDirStr))) { + return CKR_HOST_MEMORY; + } + if (asprintf (&directory, "%s/p11kit/.cache", homeDirStr) < 0) + return CKR_HOST_MEMORY; +#endif + +#ifdef HAVE_PWD_H if (getpwuid_r (uid, &pwbuf, buf, sizeof buf, &pw) < 0 || pw == NULL || pw->pw_dir == NULL || *pw->pw_dir != '/') return CKR_GENERAL_ERROR; if (asprintf (&directory, "%s/.cache", pw->pw_dir) < 0) return CKR_HOST_MEMORY; +#endif *directoryp = directory; return CKR_OK; } diff --git a/configure.ac b/configure.ac index bbb81e9..ab10452 100644 --- a/configure.ac +++ b/configure.ac @@ -106,6 +106,7 @@ if test "$os_unix" = "yes"; then AC_CHECK_FUNCS([setenv]) AC_CHECK_FUNCS([getpeereid]) AC_CHECK_FUNCS([getpeerucred]) + AC_CHECK_HEADERS([pwd.h]) # Required functions AC_CHECK_FUNCS([gmtime_r], -- 2.11.0