• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   Copyright 2004-2006 Lennart Poettering
5 
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.
10 
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.
15 
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/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
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>
30 
31 #include "pstream-util.h"
32 
33 #include "log/audio_log.h"
34 
35 #define PA_SNPRINTF_COMMAND_STR_LENGTH 256
36 
pa_pstream_send_tagstruct_with_ancil_data(pa_pstream * p,pa_tagstruct * t,pa_cmsg_ancil_data * ancil_data)37 static void pa_pstream_send_tagstruct_with_ancil_data(pa_pstream *p, pa_tagstruct *t, pa_cmsg_ancil_data *ancil_data) {
38     size_t length;
39     const uint8_t *data;
40     pa_packet *packet;
41 
42     pa_assert(p);
43     pa_assert(t);
44 
45     uint32_t command;
46     if (ReadCommand(t, &command) == 0) {
47         char t[PA_SNPRINTF_COMMAND_STR_LENGTH] = {0};
48         pa_snprintf(t, sizeof(t), "PA_SEND_CMD[%u]", p, command);
49         CallStart(t);
50         CallEnd();
51     }
52 
53     pa_assert_se(data = pa_tagstruct_data(t, &length));
54     pa_assert_se(packet = pa_packet_new_data(data, length));
55     pa_tagstruct_free(t);
56 
57     pa_pstream_send_packet(p, packet, ancil_data);
58     pa_packet_unref(packet);
59 }
60 
61 #ifdef HAVE_CREDS
62 
pa_pstream_send_tagstruct_with_creds(pa_pstream * p,pa_tagstruct * t,const pa_creds * creds)63 void pa_pstream_send_tagstruct_with_creds(pa_pstream *p, pa_tagstruct *t, const pa_creds *creds) {
64     if (creds) {
65         pa_cmsg_ancil_data a;
66 
67         a.nfd = 0;
68         a.creds_valid = true;
69         a.creds = *creds;
70         pa_pstream_send_tagstruct_with_ancil_data(p, t, &a);
71     }
72     else
73         pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
74 }
75 
76 /* @close_fds: If set then the pstreams code, after invoking a sendmsg(),
77  * will close all passed fds.
78  *
79  * Such fds cannot be closed here as this might lead to freeing them
80  * before they're actually passed to the other end. The internally-used
81  * pa_pstream_send_packet() does not do any actual writes and just
82  * defers write events over the pstream. */
pa_pstream_send_tagstruct_with_fds(pa_pstream * p,pa_tagstruct * t,int nfd,const int * fds,bool close_fds)83 void pa_pstream_send_tagstruct_with_fds(pa_pstream *p, pa_tagstruct *t, int nfd, const int *fds,
84                                         bool close_fds) {
85     if (nfd > 0) {
86         pa_cmsg_ancil_data a;
87 
88         a.nfd = nfd;
89         a.creds_valid = false;
90         a.close_fds_on_cleanup = close_fds;
91         pa_assert(nfd <= MAX_ANCIL_DATA_FDS);
92         memcpy(a.fds, fds, sizeof(int) * nfd);
93         pa_pstream_send_tagstruct_with_ancil_data(p, t, &a);
94     }
95     else
96         pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
97 }
98 
99 #else
100 
pa_pstream_send_tagstruct_with_creds(pa_pstream * p,pa_tagstruct * t,const pa_creds * creds)101 void pa_pstream_send_tagstruct_with_creds(pa_pstream *p, pa_tagstruct *t, const pa_creds *creds) {
102     pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
103 }
104 
pa_pstream_send_tagstruct_with_fds(pa_pstream * p,pa_tagstruct * t,int nfd,const int * fds,bool close_fds)105 void PA_GCC_NORETURN pa_pstream_send_tagstruct_with_fds(pa_pstream *p, pa_tagstruct *t, int nfd, const int *fds,
106                                                         bool close_fds) {
107     pa_assert_not_reached();
108 }
109 
110 #endif
111 
pa_pstream_send_error(pa_pstream * p,uint32_t tag,uint32_t error)112 void pa_pstream_send_error(pa_pstream *p, uint32_t tag, uint32_t error) {
113     pa_tagstruct *t;
114 
115     pa_assert_se(t = pa_tagstruct_new());
116     pa_tagstruct_putu32(t, PA_COMMAND_ERROR);
117     pa_tagstruct_putu32(t, tag);
118     pa_tagstruct_putu32(t, error);
119     pa_pstream_send_tagstruct(p, t);
120 }
121 
pa_pstream_send_simple_ack(pa_pstream * p,uint32_t tag)122 void pa_pstream_send_simple_ack(pa_pstream *p, uint32_t tag) {
123     pa_tagstruct *t;
124 
125     pa_assert_se(t = pa_tagstruct_new());
126     pa_tagstruct_putu32(t, PA_COMMAND_REPLY);
127     pa_tagstruct_putu32(t, tag);
128     pa_pstream_send_tagstruct(p, t);
129 }
130 
131 /* Before sending blocks from a memfd-backed pool over the pipe, we
132  * must call this method first.
133  *
134  * This is needed to transfer memfd blocks without passing their fd
135  * every time, thus minimizing overhead and avoiding fd leaks.
136  *
137  * On registration a packet is sent with the memfd fd as ancil data;
138  * such packet has an ID that uniquely identifies the pool's memfd
139  * region. Upon arrival the other end creates a permanent mapping
140  * between that ID and the passed memfd memory area.
141  *
142  * By doing so, we won't need to reference the pool's memfd fd any
143  * further - just its ID. Both endpoints can then close their fds. */
pa_pstream_register_memfd_mempool(pa_pstream * p,pa_mempool * pool,const char ** fail_reason)144 int pa_pstream_register_memfd_mempool(pa_pstream *p, pa_mempool *pool, const char **fail_reason) {
145 #if defined(HAVE_CREDS) && defined(HAVE_MEMFD)
146     unsigned shm_id;
147     int memfd_fd, ret = -1;
148     pa_tagstruct *t;
149     bool per_client_mempool;
150 
151     pa_assert(p);
152     pa_assert(fail_reason);
153 
154     *fail_reason = NULL;
155     per_client_mempool = pa_mempool_is_per_client(pool);
156 
157     pa_pstream_ref(p);
158 
159     if (!pa_mempool_is_shared(pool)) {
160         *fail_reason = "mempool is not shared";
161         goto finish;
162     }
163 
164     if (!pa_mempool_is_memfd_backed(pool)) {
165         *fail_reason = "mempool is not memfd-backed";
166         goto finish;
167     }
168 
169     if (pa_mempool_get_shm_id(pool, &shm_id)) {
170         *fail_reason = "could not extract pool SHM ID";
171         goto finish;
172     }
173 
174     if (!pa_pstream_get_memfd(p)) {
175         *fail_reason = "pipe does not support memfd transport";
176         goto finish;
177     }
178 
179     memfd_fd = (per_client_mempool) ? pa_mempool_take_memfd_fd(pool) :
180                                       pa_mempool_get_memfd_fd(pool);
181 
182     /* Note! For per-client mempools we've taken ownership of the memfd
183      * fd, and we're thus the sole code path responsible for closing it.
184      * In case of any failure, it MUST be closed. */
185 
186     if (pa_pstream_attach_memfd_shmid(p, shm_id, memfd_fd)) {
187         *fail_reason = "could not attach memfd SHM ID to pipe";
188 
189         if (per_client_mempool)
190             pa_assert_se(pa_close(memfd_fd) == 0);
191         goto finish;
192     }
193 
194     t = pa_tagstruct_new();
195     pa_tagstruct_putu32(t, PA_COMMAND_REGISTER_MEMFD_SHMID);
196     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
197     pa_tagstruct_putu32(t, shm_id);
198     pa_pstream_send_tagstruct_with_fds(p, t, 1, &memfd_fd, per_client_mempool);
199 
200     ret = 0;
201 finish:
202     pa_pstream_unref(p);
203     return ret;
204 
205 #else
206     pa_assert(fail_reason);
207     *fail_reason = "memfd support not compiled in";
208     return -1;
209 #endif
210 }
211