2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
24 #include <pulsecore/core-util.h>
25 #include <pulsecore/macro.h>
26 #include <pulsecore/native-common.h>
27 #include <pulsecore/pstream.h>
28 #include <pulsecore/refcnt.h>
29 #include <pulse/xmalloc.h>
31 #include "pstream-util.h"
33 static void pa_pstream_send_tagstruct_with_ancil_data(pa_pstream *p, pa_tagstruct *t, pa_cmsg_ancil_data *ancil_data) {
41 pa_assert_se(data = pa_tagstruct_data(t, &length));
42 pa_assert_se(packet = pa_packet_new_data(data, length));
45 pa_pstream_send_packet(p, packet, ancil_data);
46 pa_packet_unref(packet);
51 void pa_pstream_send_tagstruct_with_creds(pa_pstream *p, pa_tagstruct *t, const pa_creds *creds) {
58 pa_pstream_send_tagstruct_with_ancil_data(p, t, &a);
61 pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
64 /* @close_fds: If set then the pstreams code, after invoking a sendmsg(),
65 * will close all passed fds.
67 * Such fds cannot be closed here as this might lead to freeing them
68 * before they're actually passed to the other end. The internally-used
69 * pa_pstream_send_packet() does not do any actual writes and just
70 * defers write events over the pstream. */
71 void pa_pstream_send_tagstruct_with_fds(pa_pstream *p, pa_tagstruct *t, int nfd, const int *fds,
77 a.creds_valid = false;
78 a.close_fds_on_cleanup = close_fds;
79 pa_assert(nfd <= MAX_ANCIL_DATA_FDS);
80 memcpy(a.fds, fds, sizeof(int) * nfd);
81 pa_pstream_send_tagstruct_with_ancil_data(p, t, &a);
84 pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
89 void pa_pstream_send_tagstruct_with_creds(pa_pstream *p, pa_tagstruct *t, const pa_creds *creds) {
90 pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
93 void PA_GCC_NORETURN pa_pstream_send_tagstruct_with_fds(pa_pstream *p, pa_tagstruct *t, int nfd, const int *fds,
95 pa_assert_not_reached();
100 void pa_pstream_send_error(pa_pstream *p, uint32_t tag, uint32_t error) {
103 pa_assert_se(t = pa_tagstruct_new());
104 pa_tagstruct_putu32(t, PA_COMMAND_ERROR);
105 pa_tagstruct_putu32(t, tag);
106 pa_tagstruct_putu32(t, error);
107 pa_pstream_send_tagstruct(p, t);
110 void pa_pstream_send_simple_ack(pa_pstream *p, uint32_t tag) {
113 pa_assert_se(t = pa_tagstruct_new());
114 pa_tagstruct_putu32(t, PA_COMMAND_REPLY);
115 pa_tagstruct_putu32(t, tag);
116 pa_pstream_send_tagstruct(p, t);
119 /* Before sending blocks from a memfd-backed pool over the pipe, we
120 * must call this method first.
122 * This is needed to transfer memfd blocks without passing their fd
123 * every time, thus minimizing overhead and avoiding fd leaks.
125 * On registration a packet is sent with the memfd fd as ancil data;
126 * such packet has an ID that uniquely identifies the pool's memfd
127 * region. Upon arrival the other end creates a permanent mapping
128 * between that ID and the passed memfd memory area.
130 * By doing so, we won't need to reference the pool's memfd fd any
131 * further - just its ID. Both endpoints can then close their fds. */
132 int pa_pstream_register_memfd_mempool(pa_pstream *p, pa_mempool *pool, const char **fail_reason) {
133 #if defined(HAVE_CREDS) && defined(HAVE_MEMFD)
135 int memfd_fd, ret = -1;
137 bool per_client_mempool;
140 pa_assert(fail_reason);
143 per_client_mempool = pa_mempool_is_per_client(pool);
147 if (!pa_mempool_is_shared(pool)) {
148 *fail_reason = "mempool is not shared";
152 if (!pa_mempool_is_memfd_backed(pool)) {
153 *fail_reason = "mempool is not memfd-backed";
157 if (pa_mempool_get_shm_id(pool, &shm_id)) {
158 *fail_reason = "could not extract pool SHM ID";
162 if (!pa_pstream_get_memfd(p)) {
163 *fail_reason = "pipe does not support memfd transport";
167 memfd_fd = (per_client_mempool) ? pa_mempool_take_memfd_fd(pool) :
168 pa_mempool_get_memfd_fd(pool);
170 /* Note! For per-client mempools we've taken ownership of the memfd
171 * fd, and we're thus the sole code path responsible for closing it.
172 * In case of any failure, it MUST be closed. */
174 if (pa_pstream_attach_memfd_shmid(p, shm_id, memfd_fd)) {
175 *fail_reason = "could not attach memfd SHM ID to pipe";
177 if (per_client_mempool)
178 pa_assert_se(pa_close(memfd_fd) == 0);
182 t = pa_tagstruct_new();
183 pa_tagstruct_putu32(t, PA_COMMAND_REGISTER_MEMFD_SHMID);
184 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
185 pa_tagstruct_putu32(t, shm_id);
186 pa_pstream_send_tagstruct_with_fds(p, t, 1, &memfd_fd, per_client_mempool);
194 pa_assert(fail_reason);
195 *fail_reason = "memfd support not compiled in";