diff options
-rw-r--r-- | common/runtime.c | 29 | ||||
-rw-r--r-- | common/test-runtime.c | 14 |
2 files changed, 33 insertions, 10 deletions
diff --git a/common/runtime.c b/common/runtime.c index f713e7d..316c5be 100644 --- a/common/runtime.c +++ b/common/runtime.c @@ -37,28 +37,34 @@ #include "runtime.h" #include "compat.h" -#include <pwd.h> + #include <stdio.h> #include <string.h> + +#ifdef OS_UNIX +#include <pwd.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.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 OS_UNIX 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 /* 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 +80,7 @@ p11_get_runtime_directory (char **directoryp) return CKR_OK; } +#ifdef OS_UNIX uid = getuid (); for (i = 0; bases[i] != NULL; i++) { @@ -87,6 +94,7 @@ p11_get_runtime_directory (char **directoryp) return CKR_OK; } } +#endif /* We can't use /run/user/<UID>, fallback to ~/.cache. */ envvar = secure_getenv ("XDG_CACHE_HOME"); @@ -100,12 +108,15 @@ p11_get_runtime_directory (char **directoryp) return CKR_OK; } - if (getpwuid_r (uid, &pwbuf, buf, sizeof buf, &pw) < 0 || - pw == NULL || pw->pw_dir == NULL || *pw->pw_dir != '/') - return CKR_GENERAL_ERROR; +#ifdef OS_UNIX + if (getpwuid_r (uid, &pwbuf, buf, sizeof buf, &pw) == 0 && + pw != NULL && pw->pw_dir != NULL && *pw->pw_dir == '/') { + if (asprintf (&directory, "%s/.cache", pw->pw_dir) < 0) + return CKR_HOST_MEMORY; + *directoryp = directory; + return CKR_OK; + } +#endif - if (asprintf (&directory, "%s/.cache", pw->pw_dir) < 0) - return CKR_HOST_MEMORY; - *directoryp = directory; - return CKR_OK; + return CKR_GENERAL_ERROR; } diff --git a/common/test-runtime.c b/common/test-runtime.c index 294a7ad..1594a9c 100644 --- a/common/test-runtime.c +++ b/common/test-runtime.c @@ -41,6 +41,11 @@ #include <stdio.h> #include <stdlib.h> +#ifdef OS_UNIX +#include <unistd.h> +#include <sys/types.h> +#endif + static struct { char *directory; } test; @@ -71,6 +76,7 @@ test_xdg_runtime_dir (void) free (directory); } +#ifdef OS_UNIX static void test_bases (void) { @@ -103,17 +109,21 @@ test_bases (void) free (path); free (directory); } +#endif static void test_xdg_cache_home (void) { char *directory; +#ifdef OS_UNIX const char * bases[] = { NULL }; _p11_runtime_bases = bases; +#endif - unsetenv ("XDG_RUNTIME_DIR"); + /* MinGW doesn't have unsetenv */ + setenv ("XDG_RUNTIME_DIR", "", 1); setenv ("XDG_CACHE_HOME", "/cache", 1); p11_get_runtime_directory (&directory); assert_str_eq ("/cache", directory); @@ -126,7 +136,9 @@ main (int argc, { p11_fixture (setup, teardown); p11_test (test_xdg_runtime_dir, "/runtime/xdg-runtime-dir"); +#ifdef OS_UNIX p11_test (test_bases, "/runtime/bases"); +#endif p11_test (test_xdg_cache_home, "/runtime/xdg-cache-home"); p11_test_run (argc, argv); } |