]> code.delx.au - pulseaudio/commitdiff
consolidate close() calls to pa_close(), and make sure on every occasion that we...
authorLennart Poettering <lennart@poettering.net>
Wed, 12 Sep 2007 00:04:21 +0000 (00:04 +0000)
committerLennart Poettering <lennart@poettering.net>
Wed, 12 Sep 2007 00:04:21 +0000 (00:04 +0000)
git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1811 fefdeb5f-60dc-0310-8127-8f9354f1896f

18 files changed:
src/daemon/cpulimit.c
src/daemon/main.c
src/modules/module-esound-compat-spawnfd.c
src/modules/module-mmkbd-evdev.c
src/modules/module-oss.c
src/modules/module-pipe-sink.c
src/modules/module-pipe-source.c
src/modules/oss-util.c
src/pulse/context.c
src/pulse/mainloop.c
src/pulsecore/authkey.c
src/pulsecore/core-util.c
src/pulsecore/fdsem.c
src/pulsecore/pid.c
src/pulsecore/random.c
src/pulsecore/shm.c
src/pulsecore/sound-file-stream.c
src/pulsecore/sound-file.c

index 37f7976b62e673df52a7b7a12db055c6b74d9deb..4f0adc06ac5362af64f042089727ccddd323cd7a 100644 (file)
@@ -223,9 +223,9 @@ void pa_cpu_limit_done(void) {
     }
 
     if (the_pipe[0] >= 0)
-        close(the_pipe[0]);
+        pa_assert_se(pa_close(the_pipe[0]) == 0);
     if (the_pipe[1] >= 0)
-        close(the_pipe[1]);
+        pa_assert_se(pa_close(the_pipe[1]) == 0);
     the_pipe[0] = the_pipe[1] = -1;
 
     if (installed) {
index 4509e7f9256645c094eaf0fb803c0d2673585eab..0bbddffa6455e9aa7b7f9baca579741dcc84b6a6 100644 (file)
@@ -160,9 +160,9 @@ static void signal_callback(pa_mainloop_api*m, PA_GCC_UNUSED pa_signal_event *e,
 
 static void close_pipe(int p[2]) {
     if (p[0] != -1)
-        close(p[0]);
+        pa_assert_se(pa_close(p[0]) == 0);
     if (p[1] != -1)
-        close(p[1]);
+        pa_assert_se(pa_close(p[1]) == 0);
     p[0] = p[1] = -1;
 }
 
@@ -537,7 +537,7 @@ int main(int argc, char *argv[]) {
         if (child != 0) {
             /* Father */
 
-            close(daemon_pipe[1]);
+            pa_assert_se(pa_close(daemon_pipe[1]) == 0);
             daemon_pipe[1] = -1;
 
             if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL) != sizeof(retval)) {
@@ -553,7 +553,7 @@ int main(int argc, char *argv[]) {
             goto finish;
         }
 
-        close(daemon_pipe[0]);
+        pa_assert_se(pa_close(daemon_pipe[0]) == 0);
         daemon_pipe[0] = -1;
 #endif
 
@@ -568,9 +568,9 @@ int main(int argc, char *argv[]) {
 #endif
 
 #ifndef OS_IS_WIN32
-        close(0);
-        close(1);
-        close(2);
+        pa_close(0);
+        pa_close(1);
+        pa_close(2);
 
         open("/dev/null", O_RDONLY);
         open("/dev/null", O_WRONLY);
@@ -592,7 +592,7 @@ int main(int argc, char *argv[]) {
 #ifdef TIOCNOTTY
         if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
             ioctl(tty_fd, TIOCNOTTY, (char*) 0);
-            close(tty_fd);
+            pa_assert_se(pa_close(tty_fd) == 0);
         }
 #endif
     }
index 890ebb164e8c40bc0a90b944bff9729f9c794c31..46235c09f3fcacc734be4bcbd9f31959e9c91eb6 100644 (file)
@@ -65,7 +65,7 @@ int pa__init(pa_module*m) {
     if (pa_loop_write(fd, &x, sizeof(x), NULL) != sizeof(x))
         pa_log_warn("WARNING: write(%u, 1, 1) failed: %s", fd, pa_cstrerror(errno));
 
-    close(fd);
+    pa_assert_se(pa_close(fd) == 0);
 
     pa_module_unload_request(m);
 
index 03394c0a25f362b047bd980ead5dc85b6629779c..dc0b1c1acd2ab08e7c6cf664655f5cb3fc6fa920 100644 (file)
@@ -254,7 +254,7 @@ void pa__done(pa_module*m) {
         m->core->mainloop->io_free(u->io);
 
     if (u->fd >= 0)
-        close(u->fd);
+        pa_assert_se(pa_close(u->fd) == 0);
 
     pa_xfree(u->sink_name);
     pa_xfree(u);
index 9273a393cb29a113c8829d22ca7bd0dfeb24f293..ffecd394ac8d6e4724aaf76742e75e05b87be939 100644 (file)
@@ -476,7 +476,7 @@ static int suspend(struct userdata *u) {
 
     /* Let's suspend */
     ioctl(u->fd, SNDCTL_DSP_SYNC, NULL);
-    close(u->fd);
+    pa_close(u->fd);
     u->fd = -1;
 
     if (u->rtpoll_item) {
@@ -588,7 +588,7 @@ static int unsuspend(struct userdata *u) {
     return 0;
 
 fail:
-    close(u->fd);
+    pa_close(u->fd);
     u->fd = -1;
     return -1;
 }
@@ -1324,7 +1324,7 @@ fail:
     if (u)
         pa__done(m);
     else if (fd >= 0)
-        close(fd);
+        pa_close(fd);
 
     if (ma)
         pa_modargs_free(ma);
@@ -1391,7 +1391,7 @@ void pa__done(pa_module*m) {
         munmap(u->out_mmap, u->out_hwbuf_size);
     
     if (u->fd >= 0)
-        close(u->fd);
+        pa_close(u->fd);
 
     pa_xfree(u->device_name);
     
index 5cafaada395489f7d1df7a131061bd64964370b7..61454261226c94df7880c9499a1b92ddcf1d7e79 100644 (file)
@@ -335,7 +335,7 @@ void pa__done(pa_module*m) {
     }
 
     if (u->fd >= 0)
-        close(u->fd);
+        pa_assert_se(pa_close(u->fd) == 0);
 
     pa_xfree(u);
 }
index 291010e182ac3565b4863cb22a0bbd10b6597e0a..68c89a0dc63e778390d3441f7796ca448d168512 100644 (file)
@@ -313,7 +313,7 @@ void pa__done(pa_module*m) {
     }
 
     if (u->fd >= 0)
-        close(u->fd);
+        pa_assert_se(pa_close(u->fd) == 0);
 
     pa_xfree(u);
 }
index 5a939cf85306618541feb30be7944cb51777a565..364988092c541952f8180a4d69e2e6885cbb6cde 100644 (file)
@@ -68,7 +68,7 @@ int pa_oss_open(const char *device, int *mode, int* pcaps) {
 
             pa_log_warn("'%s' doesn't support full duplex", device);
 
-            close(fd);
+            pa_close(fd);
         }
 
         if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY|O_NOCTTY)) < 0) {
@@ -147,7 +147,7 @@ success:
 
 fail:
     if (fd >= 0)
-        close(fd);
+        pa_close(fd);
     return -1;
 }
 
index ab06aeb985f6bed5322036b8bb7cd5d2abed1a89..1ed250f9c79c0b635065a9e1490aa286689343e3 100644 (file)
@@ -524,7 +524,7 @@ static int context_connect_spawn(pa_context *c) {
         int n;
 
         /* Not required, since fds[0] has CLOEXEC enabled anyway */
-        close(fds[0]);
+        pa_assert_se(pa_close(fds[0]) == 0);
 
         if (c->spawn_api.atfork)
             c->spawn_api.atfork();
@@ -571,7 +571,7 @@ static int context_connect_spawn(pa_context *c) {
         goto fail;
     }
 
-    close(fds[1]);
+    pa_assert_se(pa_close(fds[1]) == 0);
 
     c->is_local = 1;
 
@@ -586,9 +586,9 @@ static int context_connect_spawn(pa_context *c) {
 
 fail:
     if (fds[0] != -1)
-        close(fds[0]);
+        pa_assert_se(pa_close(fds[0]) == 0);
     if (fds[1] != -1)
-        close(fds[1]);
+        pa_assert_se(pa_close(fds[1]) == 0);
 
     unlock_autospawn_lock_file(c);
 
index 419b74d63b02d8d63e228b4a1b1694783c2313d1..fc373d978453d03ab93c2e53854393d1da72aa71 100644 (file)
@@ -606,9 +606,9 @@ void pa_mainloop_free(pa_mainloop* m) {
     pa_xfree(m->pollfds);
 
     if (m->wakeup_pipe[0] >= 0)
-        close(m->wakeup_pipe[0]);
+        pa_assert_se(pa_close(m->wakeup_pipe[0]) == 0);
     if (m->wakeup_pipe[1] >= 0)
-        close(m->wakeup_pipe[1]);
+        pa_assert_se(pa_close(m->wakeup_pipe[1]) == 0);
 
     pa_xfree(m);
 }
index d8056247a5e507a06dcd8632606aa5757bd9dda5..4d9bfd3a99af5de00492a6bf25c7e190a1f3c5e5 100644 (file)
@@ -120,7 +120,10 @@ finish:
         if (unlock)
             pa_lock_fd(fd, 0);
 
-        close(fd);
+        if (pa_close(fd) < 0) {
+            pa_log_warn("Failed to close cookie file: %s", pa_cstrerror(errno));
+            ret = -1;
+        }
     }
 
     return ret;
@@ -221,7 +224,10 @@ finish:
         if (unlock)
             pa_lock_fd(fd, 0);
 
-        close(fd);
+        if (pa_close(fd) < 0) {
+            pa_log_warn("Failed to close cookie file: %s", pa_cstrerror(errno));
+            ret = -1;
+        }
     }
 
     return ret;
index c0dd0d68f548c04381ca8a2edd129175d917ede7..62a63761389930625133d7fec16c910778fb0933 100644 (file)
@@ -360,11 +360,11 @@ ssize_t pa_loop_write(int fd, const void*data, size_t size, int *type) {
 /** Platform independent read function. Necessary since not all
  * systems treat all file descriptors equal. */
 int pa_close(int fd) {
+
 #ifdef OS_IS_WIN32
     int ret;
 
-    ret = closesocket(fd);
-    if (ret == 0)
+    if ((ret = closesocket(fd)) == 0)
         return 0;
 
     if (WSAGetLastError() != WSAENOTSOCK) {
@@ -838,8 +838,7 @@ int pa_lock_fd(int fd, int b) {
             return 0;
     }
 
-    pa_log("%slock: %s", !b? "un" : "",
-        pa_cstrerror(errno));
+    pa_log("%slock: %s", !b? "un" : "", pa_cstrerror(errno));
 #endif
 
 #ifdef OS_IS_WIN32
@@ -873,32 +872,33 @@ int pa_lock_lockfile(const char *fn) {
         struct stat st;
 
         if ((fd = open(fn, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
-            pa_log("failed to create lock file '%s': %s", fn,
-                pa_cstrerror(errno));
+            pa_log_warn("Failed to create lock file '%s': %s", fn, pa_cstrerror(errno));
             goto fail;
         }
 
         if (pa_lock_fd(fd, 1) < 0) {
-            pa_log("failed to lock file '%s'.", fn);
+            pa_log_warn("Failed to lock file '%s'.", fn);
             goto fail;
         }
 
         if (fstat(fd, &st) < 0) {
-            pa_log("failed to fstat() file '%s'.", fn);
+            pa_log_warn("Failed to fstat() file '%s': %s", fn, pa_cstrerror(errno));
             goto fail;
         }
 
-        /* Check wheter the file has been removed meanwhile. When yes, restart this loop, otherwise, we're done */
+        /* Check wheter the file has been removed meanwhile. When yes,
+         * restart this loop, otherwise, we're done */
         if (st.st_nlink >= 1)
             break;
 
         if (pa_lock_fd(fd, 0) < 0) {
-            pa_log("failed to unlock file '%s'.", fn);
+            pa_log_warn("Failed to unlock file '%s'.", fn);
             goto fail;
         }
 
-        if (close(fd) < 0) {
-            pa_log("failed to close file '%s'.", fn);
+        if (pa_close(fd) < 0) {
+            pa_log_warn("Failed to close file '%s': %s", fn, pa_cstrerror(errno));
+            fd = -1;
             goto fail;
         }
 
@@ -910,7 +910,7 @@ int pa_lock_lockfile(const char *fn) {
 fail:
 
     if (fd >= 0)
-        close(fd);
+        pa_close(fd);
 
     return -1;
 }
@@ -922,19 +922,17 @@ int pa_unlock_lockfile(const char *fn, int fd) {
     pa_assert(fd >= 0);
 
     if (unlink(fn) < 0) {
-        pa_log_warn("WARNING: unable to remove lock file '%s': %s",
-            fn, pa_cstrerror(errno));
+        pa_log_warn("Unable to remove lock file '%s': %s", fn, pa_cstrerror(errno));
         r = -1;
     }
 
     if (pa_lock_fd(fd, 0) < 0) {
-        pa_log_warn("WARNING: failed to unlock file '%s'.", fn);
+        pa_log_warn("Failed to unlock file '%s'.", fn);
         r = -1;
     }
 
-    if (close(fd) < 0) {
-        pa_log_warn("WARNING: failed to close lock file '%s': %s",
-            fn, pa_cstrerror(errno));
+    if (pa_close(fd) < 0) {
+        pa_log_warn("Failed to close '%s': %s", fn, pa_cstrerror(errno));
         r = -1;
     }
 
index 5a358e2bbf7502593f8dda2aed056ebc4c785d9c..710a74f5bd130dbab6417cce458ffe243c2dfc34 100644 (file)
@@ -67,8 +67,8 @@ pa_fdsem *pa_fdsem_new(void) {
 void pa_fdsem_free(pa_fdsem *f) {
     pa_assert(f);
 
-    close(f->fds[0]);
-    close(f->fds[1]);
+    pa_assert_se(pa_close(f->fds[0]) == 0);
+    pa_assert_se(pa_close(f->fds[1]) == 0);
 
     pa_xfree(f);
 }
index 22ceae89b09daa7670c603333a3fa0557bdc99bb..6bc9f06ab20226a6174f66a487bfa8005067215f 100644 (file)
@@ -61,8 +61,7 @@ static pid_t read_pid(const char *fn, int fd) {
     pa_assert(fd >= 0);
 
     if ((r = pa_loop_read(fd, t, sizeof(t)-1, NULL)) < 0) {
-        pa_log_warn("WARNING: failed to read PID file '%s': %s",
-            fn, pa_cstrerror(errno));
+        pa_log_warn("Failed to read PID file '%s': %s", fn, pa_cstrerror(errno));
         return (pid_t) -1;
     }
 
@@ -74,7 +73,7 @@ static pid_t read_pid(const char *fn, int fd) {
         *e = 0;
 
     if (pa_atou(t, &pid) < 0) {
-        pa_log_warn("WARNING: failed to parse PID file '%s'", fn);
+        pa_log_warn("Failed to parse PID file '%s'", fn);
         return (pid_t) -1;
     }
 
@@ -91,8 +90,7 @@ static int open_pid_file(const char *fn, int mode) {
 
         if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
             if (mode != O_RDONLY || errno != ENOENT)
-                pa_log_warn("WARNING: failed to open PID file '%s': %s",
-                    fn, pa_cstrerror(errno));
+                pa_log_warn("WARNING: failed to open PID file '%s': %s", fn, pa_cstrerror(errno));
             goto fail;
         }
 
@@ -101,8 +99,7 @@ static int open_pid_file(const char *fn, int mode) {
             goto fail;
 
         if (fstat(fd, &st) < 0) {
-            pa_log_warn("WARNING: failed to fstat() PID file '%s': %s",
-                fn, pa_cstrerror(errno));
+            pa_log_warn("Failed to fstat() PID file '%s': %s", fn, pa_cstrerror(errno));
             goto fail;
         }
 
@@ -113,9 +110,9 @@ static int open_pid_file(const char *fn, int mode) {
         if (pa_lock_fd(fd, 0) < 0)
             goto fail;
 
-        if (close(fd) < 0) {
-            pa_log_warn("WARNING: failed to close file '%s': %s",
-                fn, pa_cstrerror(errno));
+        if (pa_close(fd) < 0) {
+            pa_log_warn("Failed to close file '%s': %s", fn, pa_cstrerror(errno));
+            fd = -1;
             goto fail;
         }
 
@@ -128,7 +125,7 @@ fail:
 
     if (fd >= 0) {
         pa_lock_fd(fd, 0);
-        close(fd);
+        pa_close(fd);
     }
 
     return -1;
@@ -153,7 +150,7 @@ int pa_pid_file_create(void) {
         goto fail;
 
     if ((pid = read_pid(fn, fd)) == (pid_t) -1)
-        pa_log("corrupt PID file, overwriting.");
+        pa_log_warn("Corrupt PID file, overwriting.");
     else if (pid > 0) {
 #ifdef OS_IS_WIN32
         if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
@@ -161,17 +158,16 @@ int pa_pid_file_create(void) {
 #else
         if (kill(pid, 0) >= 0 || errno != ESRCH) {
 #endif
-            pa_log("daemon already running.");
+            pa_log("Daemon already running.");
             goto fail;
         }
 
-        pa_log("stale PID file, overwriting.");
+        pa_log_warn("Stale PID file, overwriting.");
     }
 
     /* Overwrite the current PID file */
     if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
-        pa_log("failed to truncate PID file '%s': %s",
-            fn, pa_cstrerror(errno));
+        pa_log("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
         goto fail;
     }
 
@@ -179,7 +175,7 @@ int pa_pid_file_create(void) {
     l = strlen(t);
 
     if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) {
-        pa_log("failed to write PID file.");
+        pa_log("Failed to write PID file.");
         goto fail;
     }
 
@@ -188,7 +184,11 @@ int pa_pid_file_create(void) {
 fail:
     if (fd >= 0) {
         pa_lock_fd(fd, 0);
-        close(fd);
+        
+        if (pa_close(fd) < 0) {
+            pa_log("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
+            ret = -1;
+        }
     }
 
     return ret;
@@ -213,13 +213,12 @@ int pa_pid_file_remove(void) {
         goto fail;
 
     if (pid != getpid()) {
-        pa_log("WARNING: PID file '%s' not mine!", fn);
+        pa_log("PID file '%s' not mine!", fn);
         goto fail;
     }
 
     if (ftruncate(fd, 0) < 0) {
-        pa_log_warn("WARNING: failed to truncate PID file '%s': %s",
-            fn, pa_cstrerror(errno));
+        pa_log_warn("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
         goto fail;
     }
 
@@ -230,8 +229,7 @@ int pa_pid_file_remove(void) {
 #endif
 
     if (unlink(fn) < 0) {
-        pa_log_warn("WARNING: failed to remove PID file '%s': %s",
-            fn, pa_cstrerror(errno));
+        pa_log_warn("Failed to remove PID file '%s': %s", fn, pa_cstrerror(errno));
         goto fail;
     }
 
@@ -241,7 +239,11 @@ fail:
 
     if (fd >= 0) {
         pa_lock_fd(fd, 0);
-        pa_assert_se(close(fd) == 0);
+
+        if (pa_close(fd) < 0) {
+            pa_log_warn("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
+            ret = -1;
+        }
     }
 
     return ret;
@@ -283,7 +285,7 @@ fail:
 
     if (fd >= 0) {
         pa_lock_fd(fd, 0);
-        pa_assert_se(close(fd) == 0);
+        pa_close(fd);
     }
 
     return ret;
index b0b4926ce0ff9df90919047041726f1f1f9e772e..87afebfa6bf91bb6784309ac4e121be0bc5639ec 100644 (file)
@@ -69,7 +69,7 @@ static int random_proper(void *ret_data, size_t length) {
             if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
                 ret = -1;
 
-            close(fd);
+            pa_close(fd);
         } else
             ret = -1;
 
index f17d94606f99762f6b24d4fc90cac48cd53c2613..02f6a7bdc651e6897b57910ad6a115f102053abc 100644 (file)
@@ -154,7 +154,7 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) {
         pa_atomic_store(&marker->pid, (int) getpid());
         pa_atomic_store(&marker->marker, SHM_MARKER);
         
-        close(fd);
+        pa_assert_se(close(fd) == 0);
         m->do_unlink = 1;
 #else
         return -1;
@@ -170,7 +170,7 @@ fail:
 #ifdef HAVE_SHM_OPEN
     if (fd >= 0) {
         shm_unlink(fn);
-        pa_assert_se(close(fd) >= 0);
+        pa_close(fd);
     }
 #endif
 
@@ -297,13 +297,13 @@ int pa_shm_attach_ro(pa_shm *m, unsigned id) {
     m->do_unlink = 0;
     m->shared = 1;
 
-    pa_assert_se(close(fd) >= 0);
+    pa_assert_se(pa_close(fd) == 0);
 
     return 0;
 
 fail:
     if (fd >= 0)
-        pa_assert_se(close(fd) >= 0);
+        pa_close(fd);
 
     return -1;
 }
index d5523a6477c5eda78335bb6319f2d4a1f9de855e..8a04b8215b5029cf305149466e63b4f475f99e19 100644 (file)
@@ -40,6 +40,7 @@
 #include <pulsecore/sink-input.h>
 #include <pulsecore/log.h>
 #include <pulsecore/thread-mq.h>
+#include <pulsecore/core-util.h>
 
 #include "sound-file-stream.h"
 
@@ -271,7 +272,7 @@ int pa_play_file(
     
     if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
         pa_log("Failed to open file %s", fname);
-        close(fd);
+        pa_close(fd);
         goto fail;
     }
 
index 8727ba15a0d5cbb6d4a5023c92d4e53083352a84..b1c509f25474d2133a4566004c88399e4b027fdb 100644 (file)
@@ -36,6 +36,7 @@
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/core-error.h>
+#include <pulsecore/core-util.h>
 
 #include "sound-file.h"
 #include "core-scache.h"
@@ -77,7 +78,7 @@ int pa_sound_file_load(
     
     if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
         pa_log("Failed to open file %s", fname);
-        close(fd);
+        pa_close(fd);
         goto finish;
     }