sink-input: adjust log level of empty-pop operations
[platform/upstream/pulseaudio.git] / src / pulsecore / pstream-util.c
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 static void pa_pstream_send_tagstruct_with_ancil_data(pa_pstream *p, pa_tagstruct *t, pa_cmsg_ancil_data *ancil_data) {
34     size_t length;
35     const uint8_t *data;
36     pa_packet *packet;
37
38     pa_assert(p);
39     pa_assert(t);
40
41     pa_assert_se(data = pa_tagstruct_data(t, &length));
42     pa_assert_se(packet = pa_packet_new_data(data, length));
43     pa_tagstruct_free(t);
44
45     pa_pstream_send_packet(p, packet, ancil_data);
46     pa_packet_unref(packet);
47 }
48
49 #ifdef HAVE_CREDS
50
51 void pa_pstream_send_tagstruct_with_creds(pa_pstream *p, pa_tagstruct *t, const pa_creds *creds) {
52     if (creds) {
53         pa_cmsg_ancil_data a;
54
55         a.nfd = 0;
56         a.creds_valid = true;
57         a.creds = *creds;
58         pa_pstream_send_tagstruct_with_ancil_data(p, t, &a);
59     }
60     else
61         pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
62 }
63
64 /* @close_fds: If set then the pstreams code, after invoking a sendmsg(),
65  * will close all passed fds.
66  *
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,
72                                         bool close_fds) {
73     if (nfd > 0) {
74         pa_cmsg_ancil_data a;
75
76         a.nfd = nfd;
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);
82     }
83     else
84         pa_pstream_send_tagstruct_with_ancil_data(p, t, NULL);
85 }
86
87 #else
88
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);
91 }
92
93 void PA_GCC_NORETURN pa_pstream_send_tagstruct_with_fds(pa_pstream *p, pa_tagstruct *t, int nfd, const int *fds,
94                                                         bool close_fds) {
95     pa_assert_not_reached();
96 }
97
98 #endif
99
100 void pa_pstream_send_error(pa_pstream *p, uint32_t tag, uint32_t error) {
101     pa_tagstruct *t;
102
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);
108 }
109
110 void pa_pstream_send_simple_ack(pa_pstream *p, uint32_t tag) {
111     pa_tagstruct *t;
112
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);
117 }
118
119 /* Before sending blocks from a memfd-backed pool over the pipe, we
120  * must call this method first.
121  *
122  * This is needed to transfer memfd blocks without passing their fd
123  * every time, thus minimizing overhead and avoiding fd leaks.
124  *
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.
129  *
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)
134     unsigned shm_id;
135     int memfd_fd, ret = -1;
136     pa_tagstruct *t;
137     bool per_client_mempool;
138
139     pa_assert(p);
140     pa_assert(fail_reason);
141
142     *fail_reason = NULL;
143     per_client_mempool = pa_mempool_is_per_client(pool);
144
145     pa_pstream_ref(p);
146
147     if (!pa_mempool_is_shared(pool)) {
148         *fail_reason = "mempool is not shared";
149         goto finish;
150     }
151
152     if (!pa_mempool_is_memfd_backed(pool)) {
153         *fail_reason = "mempool is not memfd-backed";
154         goto finish;
155     }
156
157     if (pa_mempool_get_shm_id(pool, &shm_id)) {
158         *fail_reason = "could not extract pool SHM ID";
159         goto finish;
160     }
161
162     if (!pa_pstream_get_memfd(p)) {
163         *fail_reason = "pipe does not support memfd transport";
164         goto finish;
165     }
166
167     memfd_fd = (per_client_mempool) ? pa_mempool_take_memfd_fd(pool) :
168                                       pa_mempool_get_memfd_fd(pool);
169
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. */
173
174     if (pa_pstream_attach_memfd_shmid(p, shm_id, memfd_fd)) {
175         *fail_reason = "could not attach memfd SHM ID to pipe";
176
177         if (per_client_mempool)
178             pa_assert_se(pa_close(memfd_fd) == 0);
179         goto finish;
180     }
181
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);
187
188     ret = 0;
189 finish:
190     pa_pstream_unref(p);
191     return ret;
192
193 #else
194     pa_assert(fail_reason);
195     *fail_reason = "memfd support not compiled in";
196     return -1;
197 #endif
198 }