summaryrefslogtreecommitdiff
path: root/src/pulsecore/native-common.c
diff options
context:
space:
mode:
authorAhmed S. Darwish <darwish.07@gmail.com>2016-03-13 01:12:18 +0200
committerDavid Henningsson <david.henningsson@canonical.com>2016-04-02 05:55:14 +0200
commit27d0a3b388899a1799be20040d93cded9d74c813 (patch)
treefa83fcde84754b5545d27e3c03a0185ebeb3cbd6 /src/pulsecore/native-common.c
parentee2db622778ae8038c9e178675978ad23f1300a2 (diff)
downloadpulseaudio-27d0a3b388899a1799be20040d93cded9d74c813.tar.gz
pstream: Support memfd blocks transport
Now that we have the necessary infrastructure to memexport and mempimport a memfd memblock, extend that support higher up in the chain with pstreams. A PA endpoint can now _transparently_ send a memfd memblock to the other end by simply calling pa_pstream_send_memblock() – provided the block's memfd pool was earlier registered with the pstream. If the pipe does not support memfd transfers, we fall back to sending the block's full data instead of just its reference. ** Further details: A single pstream connection usually transfers blocks from multiple pools including the server's srbchannel mempool, the client's audio data mempool, and the server's global core mempool. If these mempools are memfd-backed, we now require registering them with the pstream before sending any blocks they cover. This is done to minimize fd passing overhead and avoid fd leaks. Moreover, to support all these pools without hard-coding their number or nature in the Pulse communication protocol itself, a new REGISTER_MEMFD_SHMID command is introduced. That command can be sent _anytime_ during the pstream's lifetime and is used for creating on demand SHM ID to memfd mappings. Suggested-by: David Henningsson <david.henningsson@canonical.com> Signed-off-by: Ahmed S. Darwish <darwish.07@gmail.com>
Diffstat (limited to 'src/pulsecore/native-common.c')
-rw-r--r--src/pulsecore/native-common.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/pulsecore/native-common.c b/src/pulsecore/native-common.c
new file mode 100644
index 000000000..282a4ed3b
--- /dev/null
+++ b/src/pulsecore/native-common.c
@@ -0,0 +1,78 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2016 Ahmed S. Darwish <darwish.07@gmail.com>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/core-util.h>
+#include <pulsecore/creds.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/pdispatch.h>
+#include <pulsecore/pstream.h>
+#include <pulsecore/tagstruct.h>
+
+#include "native-common.h"
+
+/*
+ * Command handlers shared between client and server
+ */
+
+/* Check pa_pstream_register_memfd_mempool() for further details */
+int pa_common_command_register_memfd_shmid(pa_pstream *p, pa_pdispatch *pd, uint32_t version,
+ uint32_t command, pa_tagstruct *t) {
+#if defined(HAVE_CREDS) && defined(HAVE_MEMFD)
+ pa_cmsg_ancil_data *ancil = NULL;
+ unsigned shm_id;
+ int ret = -1;
+
+ pa_assert(pd);
+ pa_assert(command == PA_COMMAND_REGISTER_MEMFD_SHMID);
+ pa_assert(t);
+
+ ancil = pa_pdispatch_take_ancil_data(pd);
+ if (!ancil)
+ goto finish;
+
+ /* Upon fd leaks and reaching our open fd limit, recvmsg(2)
+ * just strips all passed fds from the ancillary data */
+ if (ancil->nfd == 0) {
+ pa_log("Expected 1 memfd fd to be received over pipe; got 0");
+ pa_log("Did we reach our open file descriptors limit?");
+ goto finish;
+ }
+
+ if (ancil->nfd != 1 || ancil->fds[0] == -1)
+ goto finish;
+
+ if (version < 31 || pa_tagstruct_getu32(t, &shm_id) < 0 || !pa_tagstruct_eof(t))
+ goto finish;
+
+ pa_pstream_attach_memfd_shmid(p, shm_id, ancil->fds[0]);
+
+ ret = 0;
+finish:
+ if (ancil)
+ pa_cmsg_ancil_data_close_fds(ancil);
+
+ return ret;
+#else
+ return -1;
+#endif
+}