]> code.delx.au - pulseaudio/commitdiff
padsp: wrap __open_2 and __open64_2
authorMichal Schmidt <mschmidt@redhat.com>
Sat, 8 Jan 2011 15:25:15 +0000 (16:25 +0100)
committerColin Guthrie <cguthrie@mandriva.org>
Mon, 10 Jan 2011 20:49:40 +0000 (20:49 +0000)
These functions are used in OSS programs where the "flags" parameter for
open() is not a build-time constant and the build has _FORTIFY_SOURCE
enabled.

src/utils/padsp.c

index fb756d39da0927c8f1063802bf1a87baaa5e21ed..ec0c46d921ca8f2dbb0c75a263ba3eff84aecc82 100644 (file)
@@ -118,6 +118,7 @@ static PA_LLIST_HEAD(fd_info, fd_infos) = NULL;
 static int (*_ioctl)(int, int, void*) = NULL;
 static int (*_close)(int) = NULL;
 static int (*_open)(const char *, int, mode_t) = NULL;
+static int (*___open_2)(const char *, int) = NULL;
 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
 static int (*_stat)(const char *, struct stat *) = NULL;
 #ifdef _STAT_VER
@@ -125,6 +126,7 @@ static int (*___xstat)(int, const char *, struct stat *) = NULL;
 #endif
 #ifdef HAVE_OPEN64
 static int (*_open64)(const char *, int, mode_t) = NULL;
+static int (*___open64_2)(const char *, int) = NULL;
 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
 static int (*_stat64)(const char *, struct stat64 *) = NULL;
 #ifdef _STAT_VER
@@ -157,6 +159,14 @@ do { \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
+#define LOAD___OPEN_2_FUNC() \
+do { \
+    pthread_mutex_lock(&func_mutex); \
+    if (!___open_2) \
+        ___open_2 = (int (*)(const char *, int)) dlsym_fn(RTLD_NEXT, "__open_2"); \
+    pthread_mutex_unlock(&func_mutex); \
+} while(0)
+
 #define LOAD_OPEN64_FUNC() \
 do { \
     pthread_mutex_lock(&func_mutex); \
@@ -165,6 +175,14 @@ do { \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
+#define LOAD___OPEN64_2_FUNC() \
+do { \
+    pthread_mutex_lock(&func_mutex); \
+    if (!___open64_2) \
+        ___open64_2 = (int (*)(const char *, int)) dlsym_fn(RTLD_NEXT, "__open64_2"); \
+    pthread_mutex_unlock(&func_mutex); \
+} while(0)
+
 #define LOAD_CLOSE_FUNC() \
 do { \
     pthread_mutex_lock(&func_mutex); \
@@ -1494,6 +1512,27 @@ int open(const char *filename, int flags, ...) {
     return real_open(filename, flags, mode);
 }
 
+static pa_bool_t is_audio_device_node(const char *path) {
+    return
+        pa_streq(path, "/dev/dsp") ||
+        pa_streq(path, "/dev/adsp") ||
+        pa_streq(path, "/dev/audio") ||
+        pa_streq(path, "/dev/sndstat") ||
+        pa_streq(path, "/dev/mixer");
+}
+
+int __open_2(const char *filename, int flags) {
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": __open_2(%s)\n", filename?filename:"NULL");
+
+    if ((flags & O_CREAT) ||
+        !filename ||
+        !is_audio_device_node(filename)) {
+        LOAD___OPEN_2_FUNC();
+        return ___open_2(filename, flags);
+    }
+    return real_open(filename, flags, 0);
+}
+
 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
     int ret = -1;
 
@@ -2383,15 +2422,6 @@ int close(int fd) {
     return 0;
 }
 
-static pa_bool_t is_audio_device_node(const char *path) {
-    return
-        pa_streq(path, "/dev/dsp") ||
-        pa_streq(path, "/dev/adsp") ||
-        pa_streq(path, "/dev/audio") ||
-        pa_streq(path, "/dev/sndstat") ||
-        pa_streq(path, "/dev/mixer");
-}
-
 int access(const char *pathname, int mode) {
 
     debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname?pathname:"NULL");
@@ -2527,6 +2557,19 @@ int open64(const char *filename, int flags, ...) {
     return real_open(filename, flags, mode);
 }
 
+int __open64_2(const char *filename, int flags) {
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": __open64_2(%s)\n", filename?filename:"NULL");
+
+    if ((flags & O_CREAT) ||
+        !filename ||
+        !is_audio_device_node(filename)) {
+        LOAD___OPEN64_2_FUNC();
+        return ___open64_2(filename, flags);
+    }
+
+    return real_open(filename, flags, 0);
+}
+
 #endif
 
 #ifdef _STAT_VER