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