From 300526f51b02288d0d7654260915579ac2f1a254 Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sun, 8 Jul 2007 15:05:18 -0400 Subject: [PATCH] Some portability fixes: Check for the mmap() MAP_ANONYMOUS flag and use MAP_ANON if it's not available (FreeBSD 4.1). Define MMAP_ANONYMOUS_SHARED by default. If it's Linux < 2.4 undefine it. Include both and . --- configure.ac | 29 +++++++++++++++++++++++++---- src/common.h | 3 +++ src/pwmd.c | 27 +++++++++++++++++---------- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/configure.ac b/configure.ac index d94a3219..6db91991 100644 --- a/configure.ac +++ b/configure.ac @@ -13,20 +13,25 @@ case "$target_os" in kver=`uname -r 2>&1` case "$kver" in - dnl Is this a bug? The [[ and ]] are needed. Shouldn't it just be - dnl [ and ]? 2.[[0-3]]* | 1.*) + LIBS="$LIBS -lrt" + mmap_anonymous_shared=no ;; *) - AC_DEFINE([MMAP_ANONYMOUS_SHARED],, \ - [Define if your mmap() supports MAP_ANONYMOUS with MAP_SHARED.]) + mmap_anonymous_shared=yes ;; esac ;; *) + mmap_anonymous_shared=yes ;; esac +if test "$mmap_anonymous_shared" = "yes"; then + AC_DEFINE([MMAP_ANONYMOUS_SHARED], 1, \ + [Define if your mmap() supports MAP_ANONYMOUS with MAP_SHARED.]) +fi + CFLAGS="$CFLAGS -D_GNU_SOURCE" dnl Checks for programs. @@ -69,6 +74,22 @@ AC_CHECK_FUNCS([mkdir strerror memset select socket strcasecmp strdup \ strncasecmp strchr setrlimit mlock mlockall ftruncate \ getcwd memmove munmap strrchr strtol strstr alarm]) +AC_TRY_COMPILE([ + #include + #include ], [ + int main() + { + void *p = mmap(NULL, 1024, PROT_READ, + MAP_SHARED|MAP_ANONYMOUS, 0, 0); + exit(0); + }], + [have_mmap_anonymous=yes], [have_mmap_anonymous=no]) + +if test "$have_mmap_anonymous" = "yes"; then + AC_DEFINE([MMAP_ANONYMOUS], 1, \ + [Define if your mmap() supports the MAP_ANONYMOUS flag.]) +fi + AC_DEFUN([AC_DEBUG], [ if test "$1"; then diff --git a/src/common.h b/src/common.h index a4e18731..ca932c98 100644 --- a/src/common.h +++ b/src/common.h @@ -19,6 +19,9 @@ #ifndef COMMON_H #define COMMON_H +#include +#include + #define _ASSUAN_ONLY_GPG_ERRORS 1 #include diff --git a/src/pwmd.c b/src/pwmd.c index 0c861084..dafb9bb9 100644 --- a/src/pwmd.c +++ b/src/pwmd.c @@ -898,6 +898,9 @@ int main(int argc, char *argv[]) struct sockaddr_un addr; struct passwd *pw = getpwuid(getuid()); gchar buf[PATH_MAX]; +#ifndef MMAP_ANONYMOUS_SHARED + gchar shm_path[PATH_MAX]; +#endif gchar *socketpath = NULL, *socketdir, *socketname = NULL; gchar *socketarg = NULL; gchar *datadir = NULL; @@ -1071,28 +1074,32 @@ int main(int argc, char *argv[]) #ifdef MMAP_ANONYMOUS_SHARED if ((shm_data = mmap(NULL, cache_size, PROT_READ|PROT_WRITE, +#ifdef MMAP_ANONYMOUS MAP_SHARED|MAP_ANONYMOUS, -1, 0)) == NULL) { +#else + MAP_SHARED|MAP_ANON, -1, 0)) == NULL) { +#endif err(EXIT_FAILURE, "mmap()"); } #else - snprintf(buf, sizeof(buf), "pwmd.%i", pw->pw_uid); + snprintf(shm_path, sizeof(shm_path), "/pwmd.%i", pw->pw_uid); - if ((fd = shm_open(buf, O_CREAT|O_RDWR|O_EXCL, 0600)) == -1) - err(EXIT_FAILURE, "shm_open(): %s", buf); + if ((fd = shm_open(shm_path, O_CREAT|O_RDWR|O_EXCL, 0600)) == -1) + err(EXIT_FAILURE, "shm_open(): %s", shm_path); /* * Should be enough for the file cache. */ if (ftruncate(fd, cache_size) == -1) { warn("ftruncate()"); - shm_unlink(buf); + shm_unlink(shm_path); exit(EXIT_FAILURE); } if ((shm_data = mmap(NULL, cache_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == NULL) { warn("mmap()"); - shm_unlink(buf); + shm_unlink(shm_path); exit(EXIT_FAILURE); } @@ -1113,8 +1120,8 @@ int main(int argc, char *argv[]) log_write("munmap(): %s", strerror(errno)); #ifndef MMAP_ANONYMOUS_SHARED - if (shm_unlink(buf) == -1) - log_write("shm_unlink(): %s: %s", buf, strerror(errno)); + if (shm_unlink(shm_path) == -1) + log_write("shm_unlink(): %s: %s", shm_path, strerror(errno)); #endif exit((opt) == FALSE ? EXIT_FAILURE : EXIT_SUCCESS); @@ -1215,7 +1222,7 @@ int main(int argc, char *argv[]) struct sockaddr_un raddr; pid_t pid; - if ((fd = accept(sfd, (struct sockaddr_un *)&raddr, &slen)) == -1) { + if ((fd = accept(sfd, (struct sockaddr *)&raddr, &slen)) == -1) { if (quit) break; @@ -1259,8 +1266,8 @@ do_exit: log_write("munmap(): %s", strerror(errno)); #ifndef MMAP_ANONYMOUS_SHARED - if (shm_unlink(buf) == -1) - log_write("shm_unlink(): %s: %s", buf, strerror(errno)); + if (shm_unlink(shm_path) == -1) + log_write("shm_unlink(): %s: %s", shm_path, strerror(errno)); #endif if (estatus == EXIT_SUCCESS) -- 2.11.4.GIT