[pulseaudio-discuss] [PATCH 05/10] Apply #ifdefs around functionality not available on win32

Maarten Bosmans mkbosmans at gmail.com
Wed Jan 5 17:39:52 PST 2011


---
 configure.ac                            |    3 +-
 src/daemon/daemon-conf.c                |    5 ++++
 src/daemon/dumpmodules.c                |    2 +
 src/daemon/main.c                       |    2 +
 src/modules/module-augment-properties.c |    2 +
 src/modules/module-cli.c                |    6 ++++-
 src/modules/module-esound-sink.c        |    9 +++++++
 src/pulse/context.c                     |    6 +++++
 src/pulse/util.c                        |   13 ++++++----
 src/pulsecore/core-rtclock.c            |    2 +
 src/pulsecore/core-rtclock.h            |    2 +
 src/pulsecore/core-util.c               |   38 +++++++++++++++++++++++++++++++
 src/pulsecore/lock-autospawn.c          |    5 ++++
 src/pulsecore/memtrap.c                 |    7 +++++
 src/pulsecore/start-child.c             |    2 +
 src/tests/rtstutter.c                   |   27 +++++++++++++++++-----
 16 files changed, 118 insertions(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index 5216c01..df96cd3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -390,6 +390,7 @@ AC_CHECK_HEADERS_ONCE([byteswap.h])
 AC_CHECK_HEADERS_ONCE([sys/syscall.h])
 AC_CHECK_HEADERS_ONCE([sys/eventfd.h])
 AC_CHECK_HEADERS_ONCE([execinfo.h])
+AC_CHECK_HEADERS_ONCE([langinfo.h])
 
 #### Typdefs, structures, etc. ####
 
@@ -470,7 +471,7 @@ AC_FUNC_SELECT_ARGTYPES
 AC_CHECK_FUNCS_ONCE([chmod chown fstat fchown fchmod clock_gettime getaddrinfo getgrgid_r getgrnam_r \
     getpwnam_r getpwuid_r gettimeofday getuid inet_ntop inet_pton mlock nanosleep \
     pipe posix_fadvise posix_madvise posix_memalign setpgid setsid shm_open \
-    sigaction sleep sysconf pthread_setaffinity_np])
+    sigaction sleep symlink sysconf uname pthread_setaffinity_np])
 AC_CHECK_FUNCS([mkfifo], [HAVE_MKFIFO=1], [HAVE_MKFIFO=0])
 
 AM_CONDITIONAL(HAVE_MKFIFO, test "x$HAVE_MKFIFO" = "x1")
diff --git a/src/daemon/daemon-conf.c b/src/daemon/daemon-conf.c
index 74e8135..3339f3b 100644
--- a/src/daemon/daemon-conf.c
+++ b/src/daemon/daemon-conf.c
@@ -28,7 +28,10 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+
+#ifdef HAVE_SCHED_H
 #include <sched.h>
+#endif
 
 #include <pulse/xmalloc.h>
 #include <pulse/timeval.h>
@@ -460,10 +463,12 @@ static int parse_rtprio(const char *filename, unsigned line, const char *section
     pa_assert(rvalue);
     pa_assert(data);
 
+#ifdef HAVE_SCHED_H
     if (pa_atoi(rvalue, &rtprio) < 0 || rtprio < sched_get_priority_min(SCHED_FIFO) || rtprio > sched_get_priority_max(SCHED_FIFO)) {
         pa_log("[%s:%u] Invalid realtime priority '%s'.", filename, line, rvalue);
         return -1;
     }
+#endif
 
     c->realtime_priority = (int) rtprio;
     return 0;
diff --git a/src/daemon/dumpmodules.c b/src/daemon/dumpmodules.c
index 92470b4..d0504dc 100644
--- a/src/daemon/dumpmodules.c
+++ b/src/daemon/dumpmodules.c
@@ -90,7 +90,9 @@ static void show_info(const char *name, const char *path, void (*info)(const cha
     }
 }
 
+#ifndef OS_IS_WIN32
 extern const lt_dlsymlist lt_preloaded_symbols[];
+#endif
 
 static int is_preloaded(const char *name) {
     const lt_dlsymlist *l;
diff --git a/src/daemon/main.c b/src/daemon/main.c
index cab275f..5aeb9bc 100644
--- a/src/daemon/main.c
+++ b/src/daemon/main.c
@@ -654,6 +654,7 @@ int main(int argc, char *argv[]) {
         goto finish;
     }
 
+#ifdef HAVE_GETUID
     if (getuid() == 0 && !conf->system_instance)
         pa_log_warn(_("This program is not intended to be run as root (unless --system is specified)."));
 #ifndef HAVE_DBUS /* A similar, only a notice worthy check was done earlier, if D-Bus is enabled. */
@@ -662,6 +663,7 @@ int main(int argc, char *argv[]) {
         goto finish;
     }
 #endif
+#endif  /* HAVE_GETUID */
 
     if (conf->cmd == PA_CMD_START && conf->system_instance) {
         pa_log(_("--start not supported for system instances."));
diff --git a/src/modules/module-augment-properties.c b/src/modules/module-augment-properties.c
index 30df69d..bdeee29 100644
--- a/src/modules/module-augment-properties.c
+++ b/src/modules/module-augment-properties.c
@@ -180,6 +180,7 @@ static void update_rule(struct rule *r) {
         DIR *desktopfiles_dir;
         struct dirent *dir;
 
+#ifdef DT_DIR
         /* Let's try a more aggressive search, but only one level */
         if ((desktopfiles_dir = opendir(DESKTOPFILEDIR))) {
             while ((dir = readdir(desktopfiles_dir))) {
@@ -200,6 +201,7 @@ static void update_rule(struct rule *r) {
             }
             closedir(desktopfiles_dir);
         }
+#endif
     }
     if (!found) {
         r->good = FALSE;
diff --git a/src/modules/module-cli.c b/src/modules/module-cli.c
index c5ff456..90ce3b6 100644
--- a/src/modules/module-cli.c
+++ b/src/modules/module-cli.c
@@ -105,10 +105,14 @@ int pa__init(pa_module*m) {
      * of log messages, particularly because if stdout and stderr are
      * dup'ed they share the same O_NDELAY, too. */
 
+#ifndef OS_IS_WIN32
     if ((fd = pa_open_cloexec("/dev/tty", O_RDWR|O_NONBLOCK, 0)) >= 0) {
         io = pa_iochannel_new(m->core->mainloop, fd, fd);
         pa_log_debug("Managed to open /dev/tty.");
-    } else {
+    }
+    else
+#endif
+    {
         io = pa_iochannel_new(m->core->mainloop, STDIN_FILENO, STDOUT_FILENO);
         pa_iochannel_set_noclose(io, TRUE);
         pa_log_debug("Failed to open /dev/tty, using stdin/stdout fds instead.");
diff --git a/src/modules/module-esound-sink.c b/src/modules/module-esound-sink.c
index 9eba8ac..5a1391d 100644
--- a/src/modules/module-esound-sink.c
+++ b/src/modules/module-esound-sink.c
@@ -31,9 +31,18 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <limits.h>
+
+#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
+#endif
+
+#ifdef HAVE_NETINET_TCP_H
 #include <netinet/tcp.h>
+#endif
+
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
+#endif
 
 #ifdef HAVE_LINUX_SOCKIOS_H
 #include <linux/sockios.h>
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 7ede8a2..df24eca 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -585,10 +585,12 @@ static char *get_old_legacy_runtime_dir(void) {
         return NULL;
     }
 
+#ifdef HAVE_GETUID
     if (st.st_uid != getuid()) {
         pa_xfree(p);
         return NULL;
     }
+#endif
 
     return p;
 }
@@ -607,10 +609,12 @@ static char *get_very_old_legacy_runtime_dir(void) {
         return NULL;
     }
 
+#ifdef HAVE_GETUID
     if (st.st_uid != getuid()) {
         pa_xfree(p);
         return NULL;
     }
+#endif
 
     return p;
 }
@@ -997,6 +1001,7 @@ int pa_context_connect(
     /* Set up autospawning */
     if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
 
+#ifdef HAVE_GETUID
         if (getuid() == 0)
             pa_log_debug("Not doing autospawn since we are root.");
         else {
@@ -1005,6 +1010,7 @@ int pa_context_connect(
             if (api)
                 c->spawn_api = *api;
         }
+#endif
     }
 
     pa_context_set_state(c, PA_CONTEXT_CONNECTING);
diff --git a/src/pulse/util.c b/src/pulse/util.c
index 4924bf5..77145dd 100644
--- a/src/pulse/util.c
+++ b/src/pulse/util.c
@@ -75,11 +75,14 @@ char *pa_get_user_name(char *s, size_t l) {
     pa_assert(s);
     pa_assert(l > 0);
 
-    if ((p = (getuid() == 0 ? "root" : NULL)) ||
-        (p = getenv("USER")) ||
-        (p = getenv("LOGNAME")) ||
-        (p = getenv("USERNAME")))
-    {
+    p = NULL;
+#ifdef HAVE_GETUID
+    p = getuid() == 0 ? "root" : NULL;
+#endif
+    if (!p)
+      (p = getenv("USER")) || (p = getenv("LOGNAME")) || (p = getenv("USERNAME"));
+
+    if (p) {
         name = pa_strlcpy(s, p, l);
     } else {
 #ifdef HAVE_PWD_H
diff --git a/src/pulsecore/core-rtclock.c b/src/pulsecore/core-rtclock.c
index 110158b..a69b466 100644
--- a/src/pulsecore/core-rtclock.c
+++ b/src/pulsecore/core-rtclock.c
@@ -174,6 +174,7 @@ struct timeval* pa_rtclock_from_wallclock(struct timeval *tv) {
     return tv;
 }
 
+#ifdef HAVE_CLOCK_GETTIME
 pa_usec_t pa_timespec_load(const struct timespec *ts) {
 
     if (PA_UNLIKELY(!ts))
@@ -198,6 +199,7 @@ struct timespec* pa_timespec_store(struct timespec *ts, pa_usec_t v) {
 
     return ts;
 }
+#endif
 
 static struct timeval* wallclock_from_rtclock(struct timeval *tv) {
 
diff --git a/src/pulsecore/core-rtclock.h b/src/pulsecore/core-rtclock.h
index 3b393ed..6253536 100644
--- a/src/pulsecore/core-rtclock.h
+++ b/src/pulsecore/core-rtclock.h
@@ -43,8 +43,10 @@ void pa_rtclock_hrtimer_enable(void);
 
 struct timeval* pa_rtclock_from_wallclock(struct timeval *tv);
 
+#ifdef HAVE_CLOCK_GETTIME
 pa_usec_t pa_timespec_load(const struct timespec *ts);
 struct timespec* pa_timespec_store(struct timespec *ts, pa_usec_t v);
+#endif
 
 struct timeval* pa_timeval_rtstore(struct timeval *tv, pa_usec_t v, pa_bool_t rtclock);
 
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index ecbbd3b..335140f 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -41,8 +41,14 @@
 #include <sys/time.h>
 #include <dirent.h>
 #include <regex.h>
+
+#ifdef HAVE_LANGINFO_H
 #include <langinfo.h>
+#endif
+
+#ifdef HAVE_UNAME
 #include <sys/utsname.h>
+#endif
 
 #ifdef HAVE_STRTOF_L
 #include <locale.h>
@@ -610,6 +616,7 @@ char *pa_strlcpy(char *b, const char *s, size_t l) {
 }
 
 static int set_scheduler(int rtprio) {
+#ifdef HAVE_SCHED_H
     struct sched_param sp;
 #ifdef HAVE_DBUS
     int r;
@@ -633,6 +640,7 @@ static int set_scheduler(int rtprio) {
         pa_log_debug("SCHED_RR worked.");
         return 0;
     }
+#endif  /* HAVE_SCHED_H */
 
 #ifdef HAVE_DBUS
     /* Try to talk to RealtimeKit */
@@ -702,10 +710,12 @@ static int set_nice(int nice_level) {
     dbus_error_init(&error);
 #endif
 
+#ifdef HAVE_SYS_RESOURCE_H
     if (setpriority(PRIO_PROCESS, 0, nice_level) >= 0) {
         pa_log_debug("setpriority() worked.");
         return 0;
     }
+#endif
 
 #ifdef HAVE_DBUS
     /* Try to talk to RealtimeKit */
@@ -826,6 +836,7 @@ int pa_parse_boolean(const char *v) {
     else if (!strcmp(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
         return 0;
 
+#ifdef HAVE_LANGINFO_H
     /* And then we check language dependant */
     if ((expr = nl_langinfo(YESEXPR)))
         if (expr[0])
@@ -836,6 +847,7 @@ int pa_parse_boolean(const char *v) {
         if (expr[0])
             if (pa_match(expr, v) > 0)
                 return 0;
+#endif
 
     errno = EINVAL;
     return -1;
@@ -1338,11 +1350,13 @@ static char *get_pulse_home(void) {
         goto finish;
     }
 
+#ifdef HAVE_GETUID
     if (st.st_uid != getuid()) {
         pa_log_error("Home directory %s not ours.", h);
         errno = EACCES;
         goto finish;
     }
+#endif
 
     ret = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse", h);
 
@@ -1440,7 +1454,11 @@ static char* make_random_dir(mode_t m) {
             fn[i] = table[rand() % (sizeof(table)-1)];
 
         u = umask((~m) & 0777);
+#ifndef OS_IS_WIN32
         r = mkdir(fn, m);
+#else
+        r = mkdir(fn);
+#endif
 
         saved_errno = errno;
         umask(u);
@@ -1463,6 +1481,7 @@ static int make_random_dir_and_link(mode_t m, const char *k) {
     if (!(p = make_random_dir(m)))
         return -1;
 
+#ifdef HAVE_SYMLINK
     if (symlink(p, k) < 0) {
         int saved_errno = errno;
 
@@ -1475,6 +1494,7 @@ static int make_random_dir_and_link(mode_t m, const char *k) {
         errno = saved_errno;
         return -1;
     }
+#endif
 
     pa_xfree(p);
     return 0;
@@ -1559,6 +1579,7 @@ char *pa_get_runtime_dir(void) {
         /* Hmm, so this symlink is still around, make sure nobody fools
          * us */
 
+#ifdef HAVE_LSTAT
         if (lstat(p, &st) < 0) {
 
             if (errno != ENOENT) {
@@ -1578,6 +1599,7 @@ char *pa_get_runtime_dir(void) {
 
             pa_log_info("Hmm, runtime path exists, but points to an invalid directory. Changing runtime directory.");
         }
+#endif
 
         pa_xfree(p);
         p = NULL;
@@ -2217,6 +2239,7 @@ void pa_close_pipe(int fds[2]) {
 }
 
 char *pa_readlink(const char *p) {
+#ifdef HAVE_READLINK
     size_t l = 100;
 
     for (;;) {
@@ -2238,6 +2261,9 @@ char *pa_readlink(const char *p) {
         pa_xfree(c);
         l *= 2;
     }
+#else
+    return NULL;
+#endif
 }
 
 int pa_close_all(int except_fd, ...) {
@@ -2276,6 +2302,7 @@ int pa_close_all(int except_fd, ...) {
 }
 
 int pa_close_allv(const int except_fds[]) {
+#ifndef OS_IS_WIN32
     struct rlimit rl;
     int maxfd, fd;
 
@@ -2365,6 +2392,7 @@ int pa_close_allv(const int except_fds[]) {
         if (pa_close(fd) < 0 && errno != EBADF)
             return -1;
     }
+#endif  /* !OS_IS_WIN32 */
 
     return 0;
 }
@@ -2405,6 +2433,7 @@ int pa_unblock_sigs(int except, ...) {
 }
 
 int pa_unblock_sigsv(const int except[]) {
+#ifndef OS_IS_WIN32
     int i;
     sigset_t ss;
 
@@ -2416,6 +2445,9 @@ int pa_unblock_sigsv(const int except[]) {
             return -1;
 
     return sigprocmask(SIG_SETMASK, &ss, NULL);
+#else
+    return 0;
+#endif
 }
 
 int pa_reset_sigs(int except, ...) {
@@ -2454,6 +2486,7 @@ int pa_reset_sigs(int except, ...) {
 }
 
 int pa_reset_sigsv(const int except[]) {
+#ifndef OS_IS_WIN32
     int sig;
 
     for (sig = 1; sig < NSIG; sig++) {
@@ -2490,6 +2523,7 @@ int pa_reset_sigsv(const int except[]) {
                     return -1;
         }
     }
+#endif
 
     return 0;
 }
@@ -2624,9 +2658,13 @@ char *pa_machine_id(void) {
     if ((h = pa_get_host_name_malloc()))
         return h;
 
+#ifndef OS_IS_WIN32
     /* If no hostname was set we use the POSIX hostid. It's usually
      * the IPv4 address.  Might not be that stable. */
     return pa_sprintf_malloc("%08lx", (unsigned long) gethostid);
+#else
+    return NULL;
+#endif
 }
 
 char *pa_session_id(void) {
diff --git a/src/pulsecore/lock-autospawn.c b/src/pulsecore/lock-autospawn.c
index b515057..7fda123 100644
--- a/src/pulsecore/lock-autospawn.c
+++ b/src/pulsecore/lock-autospawn.c
@@ -27,7 +27,10 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+
+#ifdef HAVE_PTHREAD
 #include <pthread.h>
+#endif
 
 #include <pulse/i18n.h>
 #include <pulse/xmalloc.h>
@@ -208,9 +211,11 @@ static void thread_func(void *u) {
     char *lf;
     sigset_t fullset;
 
+#ifdef HAVE_PTHREAD
     /* No signals in this thread please */
     sigfillset(&fullset);
     pthread_sigmask(SIG_BLOCK, &fullset, NULL);
+#endif
 
     if (!(lf = pa_runtime_path(AUTOSPAWN_LOCK))) {
         pa_log_warn(_("Cannot access autospawn lock."));
diff --git a/src/pulsecore/memtrap.c b/src/pulsecore/memtrap.c
index 4fc1821..b56d806 100644
--- a/src/pulsecore/memtrap.c
+++ b/src/pulsecore/memtrap.c
@@ -24,7 +24,10 @@
 #endif
 
 #include <signal.h>
+
+#ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
+#endif
 
 /* This is deprecated on glibc but is still used by FreeBSD */
 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
@@ -68,6 +71,7 @@ static void sigsafe_error(const char *s) {
     (void) write(STDERR_FILENO, s, strlen(s));
 }
 
+#ifdef HAVE_SIGACTION
 static void signal_handler(int sig, siginfo_t* si, void *data) {
     unsigned j;
     pa_memtrap *m;
@@ -102,6 +106,7 @@ fail:
     sigsafe_error("Failed to handle SIGBUS.\n");
     abort();
 }
+#endif
 
 static void memtrap_link(pa_memtrap *m, unsigned j) {
     pa_assert(m);
@@ -221,6 +226,7 @@ unlock:
 }
 
 void pa_memtrap_install(void) {
+#ifdef HAVE_SIGACTION
     struct sigaction sa;
 
     allocate_aupdate();
@@ -230,4 +236,5 @@ void pa_memtrap_install(void) {
     sa.sa_flags = SA_RESTART|SA_SIGINFO;
 
     pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
+#endif
 }
diff --git a/src/pulsecore/start-child.c b/src/pulsecore/start-child.c
index 4a70aea..dabcfa5 100644
--- a/src/pulsecore/start-child.c
+++ b/src/pulsecore/start-child.c
@@ -47,6 +47,7 @@ int pa_start_child_for_read(const char *name, const char *argv1, pid_t *pid) {
     pid_t child;
     int pipe_fds[2] = { -1, -1 };
 
+#ifdef HAVE_FORK
     if (pipe(pipe_fds) < 0) {
         pa_log("pipe() failed: %s", pa_cstrerror(errno));
         goto fail;
@@ -104,6 +105,7 @@ int pa_start_child_for_read(const char *name, const char *argv1, pid_t *pid) {
         execl(name, name, argv1, NULL);
         _exit(1);
     }
+#endif
 
 fail:
     pa_close_pipe(pipe_fds);
diff --git a/src/tests/rtstutter.c b/src/tests/rtstutter.c
index a82c47b..dd2696b 100644
--- a/src/tests/rtstutter.c
+++ b/src/tests/rtstutter.c
@@ -26,10 +26,16 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
-#include <sched.h>
 #include <inttypes.h>
 #include <string.h>
+
+#ifdef HAVE_SCHED_H
+#include <sched.h>
+#endif
+
+#ifdef HAVE_PTHREAD
 #include <pthread.h>
+#endif
 
 #include <pulse/util.h>
 #include <pulse/timeval.h>
@@ -44,24 +50,30 @@ static int msec_lower, msec_upper;
 static void* work(void *p) PA_GCC_NORETURN;
 
 static void* work(void *p) {
-#ifdef HAVE_PTHREAD_SETAFFINITY_NP
-    cpu_set_t mask;
-#endif
-    struct sched_param param;
-
     pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_UINT(p));
 
+#ifdef HAVE_SCHED_H
+{
+    struct sched_param param;
+
     memset(&param, 0, sizeof(param));
     param.sched_priority = 12;
     pa_assert_se(pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0);
+}
+#endif
 
 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
+{
+    cpu_set_t mask;
+
     CPU_ZERO(&mask);
     CPU_SET((size_t) PA_PTR_TO_UINT(p), &mask);
     pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
+}
 #endif
 
     for (;;) {
+#ifdef HAVE_CLOCK_GETTIME
         struct timespec now, end;
         uint64_t nsec;
 
@@ -92,6 +104,7 @@ static void* work(void *p) {
 #endif
         } while (now.tv_sec < end.tv_sec ||
                  (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
+#endif
     }
 }
 
@@ -119,8 +132,10 @@ int main(int argc, char*argv[]) {
     pa_log_notice("Creating random latencies in the range of %ims to  %ims.", msec_lower, msec_upper);
 
     for (n = 1; n < pa_ncpus(); n++) {
+#ifdef HAVE_PTHREAD
         pthread_t t;
         pa_assert_se(pthread_create(&t, NULL, work, PA_UINT_TO_PTR(n)) == 0);
+#endif
     }
 
     work(PA_INT_TO_PTR(0));
-- 
1.7.1




More information about the pulseaudio-discuss mailing list