[Libguestfs] [PATCH nbdkit incomplete 1/5] server: Add general replacements for missing functions using LIBOBJS.

Richard W.M. Jones rjones at redhat.com
Mon Jan 14 12:18:30 UTC 2019


Especially on Windows, some common functions are missing.  Use the
autoconf LIBOBJS mechanism to replace these functions.

This includes replacement functions for:

  Function names     Implementation   Origin

  getdelim, getline  general purpose  NetBSD under a compatible license

  openlog, syslog,   Win32            written by me
  vsyslog

  realpath           Win32            written by me

  strndup            general purpose  written by me

This should do nothing on existing supported platforms.  It is only
intended in preparation for porting nbdkit to Windows.
---
 configure.ac            | 30 +++++++++++++++
 Makefile.am             |  1 +
 lib/getline.h           | 50 ++++++++++++++++++++++++
 lib/realpath.h          | 50 ++++++++++++++++++++++++
 lib/strndup.h           | 49 ++++++++++++++++++++++++
 lib/syslog.h            | 67 ++++++++++++++++++++++++++++++++
 lib/getdelim.c          | 84 +++++++++++++++++++++++++++++++++++++++++
 lib/getline.c           | 46 ++++++++++++++++++++++
 lib/openlog.c           | 62 ++++++++++++++++++++++++++++++
 lib/realpath.c          | 81 +++++++++++++++++++++++++++++++++++++++
 lib/strndup.c           | 64 +++++++++++++++++++++++++++++++
 lib/syslog.c            | 62 ++++++++++++++++++++++++++++++
 lib/vsyslog.c           | 74 ++++++++++++++++++++++++++++++++++++
 server/crypto.c         |  2 +-
 server/log-syslog.c     |  2 +-
 server/main.c           |  2 +
 server/utils.c          |  2 +
 .gitignore              |  4 ++
 lib/Makefile.am         | 53 ++++++++++++++++++++++++++
 lib/win32/Makefile.am   | 46 ++++++++++++++++++++++
 lib/win32/nbdkit-cat.mc |  6 +++
 server/Makefile.am      |  5 ++-
 22 files changed, 839 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 7b52abc..c5a23a5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -90,6 +90,23 @@ if test "x$gcc_warnings" = "xyes"; then
     AC_SUBST([WARNINGS_CFLAGS])
 fi
 
+dnl Is this Windows?
+AC_MSG_CHECKING([if the target is Windows])
+AS_CASE([$host_os],
+    [mingw*], [is_windows=yes],
+    [is_windows=no]
+)
+AC_MSG_RESULT([$is_windows])
+AM_CONDITIONAL([IS_WINDOWS],[test "x$is_windows" = "xyes"])
+
+AS_IF([test "x$is_windows" = "xyes"],[
+    dnl For Windows, look for the mc/windmc utility.
+    dnl XXX Do we need to check for mc.exe as well?
+    AC_CHECK_TOOLS([MC],[windmc mc],[no])
+    AS_IF([test "x$MC" = "xno"],
+          [AC_MSG_ERROR([mc/windmc utility must be available when compiling for Windows])])
+])
+
 dnl On Haiku we must use BSD-compatibility headers to get the endian
 dnl macros we use.
 AC_MSG_CHECKING(whether OS-dependent include paths are required)
@@ -193,6 +210,17 @@ AC_CHECK_FUNCS([\
 	get_current_dir_name \
 	mkostemp])
 
+dnl Replacement functions that we provide for some platforms.
+AC_CONFIG_LIBOBJ_DIR([lib])
+AC_REPLACE_FUNCS([\
+	getdelim \
+	getline \
+	openlog \
+	realpath \
+	strndup \
+	syslog \
+	vsyslog])
+
 dnl Check whether printf("%m") works
 AC_CACHE_CHECK([whether the printf family supports %m],
   [nbdkit_cv_func_printf_percent_m],
@@ -817,6 +845,8 @@ AC_CONFIG_FILES([Makefile
                  common/sparse/Makefile
                  docs/Makefile
                  include/Makefile
+                 lib/Makefile
+                 lib/win32/Makefile
                  plugins/Makefile
                  plugins/curl/Makefile
                  plugins/data/Makefile
diff --git a/Makefile.am b/Makefile.am
index 273accb..b551c48 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -64,6 +64,7 @@ SUBDIRS = \
 	docs \
 	fuzzing \
 	valgrind \
+	lib \
 	include \
 	common/include \
 	server
diff --git a/lib/getline.h b/lib/getline.h
new file mode 100644
index 0000000..a9e1024
--- /dev/null
+++ b/lib/getline.h
@@ -0,0 +1,50 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_GETLINE_H
+#define NBDKIT_GETLINE_H
+
+#include <config.h>
+
+#ifdef HAVE_GETLINE
+
+#include <stdio.h>
+
+#else
+
+ssize_t getline (char **lineptr, size_t *n, FILE *stream);
+ssize_t getdelim (char **lineptr, size_t *n, int delim, FILE *stream);
+
+#endif
+
+#endif /* NBDKIT_GETLINE_H */
diff --git a/lib/realpath.h b/lib/realpath.h
new file mode 100644
index 0000000..14fee81
--- /dev/null
+++ b/lib/realpath.h
@@ -0,0 +1,50 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_REALPATH_H
+#define NBDKIT_REALPATH_H
+
+#include <config.h>
+
+#ifdef HAVE_REALPATH
+
+#include <limits.h>
+#include <stdlib.h>
+
+#else
+
+char *realpath (const char *path, char *out);
+
+#endif
+
+#endif /* NBDKIT_REALPATH_H */
diff --git a/lib/strndup.h b/lib/strndup.h
new file mode 100644
index 0000000..464ce95
--- /dev/null
+++ b/lib/strndup.h
@@ -0,0 +1,49 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_STRNDUP_H
+#define NBDKIT_STRNDUP_H
+
+#include <config.h>
+
+#ifdef HAVE_STRNDUP
+
+#include <string.h>
+
+#else
+
+char *strndup(const char *s, size_t n);
+
+#endif
+
+#endif /* NBDKIT_STRNDUP_H */
diff --git a/lib/syslog.h b/lib/syslog.h
new file mode 100644
index 0000000..e4d7667
--- /dev/null
+++ b/lib/syslog.h
@@ -0,0 +1,67 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Replacement for syslog for platforms which lack it.  Note this only
+ * implements "just enough syslog" for nbdkit.  It's not a general
+ * replacement.
+ */
+
+#ifndef NBDKIT_SYSLOG_H
+#define NBDKIT_SYSLOG_H
+
+#include <config.h>
+
+#ifdef HAVE_SYSLOG_H
+
+#include_next <syslog.h>
+
+#else
+
+#include <stdarg.h>
+
+/* Since the replacement function ignores the priority field, we only
+ * need to define these to any integer.
+ */
+#define LOG_PID 0
+#define LOG_ERR 0
+#define LOG_DAEMON 0
+
+extern void openlog (const char *ident, int option, int facility);
+extern void syslog (int pri, const char *fmt, ...)
+  __attribute__ ((format (printf, 2, 3)));
+extern void vsyslog (int pri, const char *fmt, va_list args)
+  __attribute__ ((format (printf, 2, 0)));
+
+#endif /* !HAVE_SYSLOG_H */
+
+#endif /* NBDKIT_SYSLOG_H */
diff --git a/lib/getdelim.c b/lib/getdelim.c
new file mode 100644
index 0000000..97421cb
--- /dev/null
+++ b/lib/getdelim.c
@@ -0,0 +1,84 @@
+/*	$NetBSD: getdelim.c,v 1.2 2015/12/25 20:12:46 joerg Exp $	*/
+/*	NetBSD-src: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp 	*/
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifndef HAVE_GETDELIM
+
+#include "getline.h"
+
+ssize_t
+getdelim (char **buf, size_t *bufsiz, int delimiter, FILE *fp)
+{
+  char *ptr, *eptr;
+
+  if (*buf == NULL || *bufsiz == 0) {
+    *bufsiz = BUFSIZ;
+    if ((*buf = malloc (*bufsiz)) == NULL)
+      return -1;
+  }
+
+  for (ptr = *buf, eptr = *buf + *bufsiz;;) {
+    int c = fgetc (fp);
+    if (c == -1) {
+      if (feof (fp)) {
+        ssize_t diff = (ssize_t)(ptr - *buf);
+        if (diff != 0) {
+          *ptr = '\0';
+          return diff;
+        }
+      }
+      return -1;
+    }
+    *ptr++ = c;
+    if (c == delimiter) {
+      *ptr = '\0';
+      return ptr - *buf;
+    }
+    if (ptr + 2 >= eptr) {
+      char *nbuf;
+      size_t nbufsiz = *bufsiz * 2;
+      ssize_t d = ptr - *buf;
+      if ((nbuf = realloc (*buf, nbufsiz)) == NULL)
+        return -1;
+      *buf = nbuf;
+      *bufsiz = nbufsiz;
+      eptr = nbuf + nbufsiz;
+      ptr = nbuf + d;
+    }
+  }
+}
+
+#endif
diff --git a/lib/getline.c b/lib/getline.c
new file mode 100644
index 0000000..e9caf49
--- /dev/null
+++ b/lib/getline.c
@@ -0,0 +1,46 @@
+/*	$NetBSD: getline.c,v 1.2 2015/12/25 20:12:46 joerg Exp $	*/
+/*	NetBSD-src: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp	*/
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <config.h>
+#include <stdio.h>
+
+#ifndef HAVE_GETLINE
+
+#include "getline.h"
+
+ssize_t
+getline (char **buf, size_t *bufsiz, FILE *fp)
+{
+  return getdelim (buf, bufsiz, '\n', fp);
+}
+
+#endif
diff --git a/lib/openlog.c b/lib/openlog.c
new file mode 100644
index 0000000..2f60165
--- /dev/null
+++ b/lib/openlog.c
@@ -0,0 +1,62 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#ifndef HAVE_OPENLOG_H
+
+#include "syslog.h"
+
+#ifdef WIN32
+
+/* Replacement openlog for Win32. */
+
+#include <windows.h>
+
+HANDLE event_source = INVALID_HANDLE_VALUE;
+
+void
+openlog (const char *ident, int option, int facility)
+{
+  event_source = RegisterEventSource (NULL, ident);
+}
+
+#else /* !WIN32 */
+#error "no replacement openlog is available on this platform"
+#endif
+
+#endif /* !HAVE_SYSLOG_H */
diff --git a/lib/realpath.c b/lib/realpath.c
new file mode 100644
index 0000000..622ee7e
--- /dev/null
+++ b/lib/realpath.c
@@ -0,0 +1,81 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Replacement for realpath for platforms which lack this function. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#ifndef HAVE_REALPATH
+
+#include "realpath.h"
+
+#ifdef WIN32
+
+/* Replacement realpath for Win32.
+ *
+ * Note this is not thread-safe, but should be fine for all the
+ * uses in the server and ordinary plugins.
+ */
+
+#include <windows.h>
+
+char *
+realpath (const char *path, char *out)
+{
+  TCHAR buf[MAX_PATH];
+  int r;
+
+  r = GetFullPathNameA (path, MAX_PATH, buf, NULL);
+  if (r == 0) {
+    errno = GetLastError ();
+    return NULL;
+  }
+
+  out = malloc (r + 1);
+  if (out == NULL)
+    return NULL;
+  memcpy (out, buf, r);
+  out[r] = '\0';
+
+  return out;
+}
+
+#else /* !WIN32 */
+#error "no replacement realpath is available on this platform"
+#endif
+
+#endif /* !HAVE_REALPATH */
diff --git a/lib/strndup.c b/lib/strndup.c
new file mode 100644
index 0000000..de1c27c
--- /dev/null
+++ b/lib/strndup.c
@@ -0,0 +1,64 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Replacement for strndup for platforms which lack this function. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef HAVE_STRNDUP
+
+#include "strndup.h"
+
+char *
+strndup(const char *s, size_t n)
+{
+  size_t len = strlen (s);
+  char *ret;
+
+  if (len > n)
+    len = n;
+
+  ret = malloc (len+1);
+  if (ret == NULL)
+    return NULL;
+  memcpy (ret, s, len);
+  ret[len] = '\0';
+
+  return ret;
+}
+
+#endif /* !HAVE_STRNDUP */
diff --git a/lib/syslog.c b/lib/syslog.c
new file mode 100644
index 0000000..6f41cbd
--- /dev/null
+++ b/lib/syslog.c
@@ -0,0 +1,62 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#ifndef HAVE_SYSLOG_H
+
+#include "syslog.h"
+
+#ifdef WIN32
+
+/* Replacement syslog for Win32. */
+
+void
+syslog (int pri, const char *fmt, ...)
+{
+  va_list args;
+
+  va_start (args, fmt);
+  vsyslog (pri, fmt, args);
+  va_end (args);
+}
+
+#else /* !WIN32 */
+#error "no replacement syslog is available on this platform"
+#endif
+
+#endif /* !HAVE_SYSLOG_H */
diff --git a/lib/vsyslog.c b/lib/vsyslog.c
new file mode 100644
index 0000000..331aba0
--- /dev/null
+++ b/lib/vsyslog.c
@@ -0,0 +1,74 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#ifndef HAVE_SYSLOG_H
+
+#include "syslog.h"
+
+#ifdef WIN32
+
+/* Replacement vsyslog for Win32. */
+
+#include <windows.h>
+
+#include "win32/nbdkit-cat.h"
+
+extern HANDLE event_source;
+
+void
+vsyslog (int pri, const char *fmt, va_list args)
+{
+  char *str;
+  const char *strs[1];
+
+  if (vasprintf (&str, fmt, args) == -1)
+    return;
+  strs[0] = str;
+
+  ReportEventA (event_source, EVENTLOG_ERROR_TYPE, 0,
+                NBDKIT_SYSLOG_ERROR, NULL, 1, 0, strs, NULL);
+
+  free (str);
+}
+
+#else /* !WIN32 */
+#error "no replacement vsyslog is available on this platform"
+#endif
+
+#endif /* !HAVE_SYSLOG_H */
diff --git a/server/crypto.c b/server/crypto.c
index 4638a69..711e929 100644
--- a/server/crypto.c
+++ b/server/crypto.c
@@ -42,12 +42,12 @@
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <limits.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <assert.h>
 
 #include "internal.h"
+#include "realpath.h"
 
 #ifdef HAVE_GNUTLS
 
diff --git a/server/log-syslog.c b/server/log-syslog.c
index 611ed04..0c58b75 100644
--- a/server/log-syslog.c
+++ b/server/log-syslog.c
@@ -38,9 +38,9 @@
 #include <stdarg.h>
 #include <string.h>
 #include <errno.h>
-#include <syslog.h>
 
 #include "internal.h"
+#include "syslog.h"
 
 /* Tempted to use LOG_FTP instead of LOG_DAEMON! */
 static const int PRIORITY = LOG_DAEMON|LOG_ERR;
diff --git a/server/main.c b/server/main.c
index 130bf11..5ba4d0d 100644
--- a/server/main.c
+++ b/server/main.c
@@ -56,6 +56,8 @@
 #include <dlfcn.h>
 
 #include "internal.h"
+#include "strndup.h"
+#include "syslog.h"
 #include "options.h"
 #include "exit-with-parent.h"
 
diff --git a/server/utils.c b/server/utils.c
index 18011fd..3799490 100644
--- a/server/utils.c
+++ b/server/utils.c
@@ -47,6 +47,8 @@
 #include "get-current-dir-name.h"
 
 #include "internal.h"
+#include "realpath.h"
+#include "getline.h"
 
 char *
 nbdkit_absolute_path (const char *path)
diff --git a/.gitignore b/.gitignore
index 5eb5d59..34a5657 100644
--- a/.gitignore
+++ b/.gitignore
@@ -47,6 +47,10 @@ Makefile.in
 /html/*.html
 /INSTALL
 /install-sh
+/lib/libcompat.a
+/lib/win32/MSG00001.bin
+/lib/win32/nbdkit-cat.h
+/lib/win32/nbdkit-cat.rc
 /libtool
 /ltmain.sh
 /missing
diff --git a/lib/Makefile.am b/lib/Makefile.am
new file mode 100644
index 0000000..5051099
--- /dev/null
+++ b/lib/Makefile.am
@@ -0,0 +1,53 @@
+# nbdkit
+# Copyright (C) 2019 Red Hat Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+include $(top_srcdir)/common-rules.mk
+
+SUBDIRS = win32
+
+noinst_LTLIBRARIES = libcompat.la
+# sources should be empty
+libcompat_la_SOURCES =
+libcompat_la_LIBADD = $(LTLIBOBJS)
+
+EXTRA_DIST = \
+	getdelim.c \
+	getline.c \
+	getline.h \
+	openlog.c \
+	realpath.c \
+	realpath.h \
+	strndup.c \
+	strndup.h \
+	syslog.c \
+	syslog.h \
+	vsyslog.c
diff --git a/lib/win32/Makefile.am b/lib/win32/Makefile.am
new file mode 100644
index 0000000..f4ec025
--- /dev/null
+++ b/lib/win32/Makefile.am
@@ -0,0 +1,46 @@
+# nbdkit
+# Copyright (C) 2019 Red Hat Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+include $(top_srcdir)/common-rules.mk
+
+EXTRA_DIST = nbdkit-cat.mc
+
+if IS_WINDOWS
+
+# Build the message catalog.
+noinst_DATA = MSG00001.bin nbdkit-cat.h nbdkit-cat.rc
+
+$(noinst_DATA): nbdkit-cat.mc
+	rm -f $@
+	$(MC) $<
+
+endif
diff --git a/lib/win32/nbdkit-cat.mc b/lib/win32/nbdkit-cat.mc
new file mode 100644
index 0000000..687a835
--- /dev/null
+++ b/lib/win32/nbdkit-cat.mc
@@ -0,0 +1,6 @@
+MessageId=1
+Severity=Error
+SymbolicName=NBDKIT_SYSLOG_ERROR
+Language=English
+%1
+.
diff --git a/server/Makefile.am b/server/Makefile.am
index 9469530..80efef5 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -67,6 +67,7 @@ nbdkit_CPPFLAGS = \
 	-Dfilterdir=\"$(filterdir)\" \
 	-Dsbindir=\"$(sbindir)\" \
 	-Dsysconfdir=\"$(sysconfdir)\" \
+	-I$(top_srcdir)/lib \
 	-I$(top_srcdir)/include \
 	-I$(top_srcdir)/common/include
 nbdkit_CFLAGS = \
@@ -76,7 +77,8 @@ nbdkit_CFLAGS = \
 	$(VALGRIND_CFLAGS)
 nbdkit_LDADD = \
 	$(GNUTLS_LIBS) \
-	$(DL_LIBS)
+	$(DL_LIBS) \
+	../lib/libcompat.la
 nbdkit_LDFLAGS = \
 	$(PTHREAD_LIBS) \
 	$(DL_LDFLAGS)
@@ -125,6 +127,7 @@ test_utils_SOURCES = \
 	utils.c \
 	cleanup.c
 test_utils_CPPFLAGS = \
+	-I$(top_srcdir)/lib \
 	-I$(top_srcdir)/include \
 	-I$(top_srcdir)/common/include
 test_utils_CFLAGS = $(WARNINGS_CFLAGS) $(VALGRIND_CFLAGS)
-- 
2.20.1




More information about the Libguestfs mailing list