summaryrefslogtreecommitdiff
path: root/src/pulsecore/lock-autospawn.c
blob: d36b669ea536584d7c3d4e0bc0a5884e49e1f165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/***
  This file is part of PulseAudio.

  Copyright 2008 Lennart Poettering

  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 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
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/poll.h>
#include <signal.h>
#include <pthread.h>

#include <pulse/i18n.h>
#include <pulse/xmalloc.h>

#include <pulsecore/mutex.h>
#include <pulsecore/thread.h>
#include <pulsecore/core-util.h>

#include "lock-autospawn.h"

/* So, why do we have this complex code here with threads and pipes
 * and stuff? For two reasons: POSIX file locks are per-process, not
 * per-file descriptor. That means that two contexts within the same
 * process that try to create the autospawn lock might end up assuming
 * they both managed to lock the file. And then, POSIX locking
 * operations are synchronous. If two contexts run from the same event
 * loop it must be made sure that they do not block each other, but
 * that the locking operation can happen asynchronously. */

#define AUTOSPAWN_LOCK "autospawn.lock"

static pa_mutex *mutex;

static unsigned n_ref = 0;
static int lock_fd = -1;
static pa_mutex *lock_fd_mutex = NULL;
static pa_bool_t taken = FALSE;
static pa_thread *thread;
static int pipe_fd[2] = { -1, -1 };

static void destroy_mutex(void) PA_GCC_DESTRUCTOR;

static int ref(void) {

    if (n_ref > 0) {

        pa_assert(pipe_fd[0] >= 0);
        pa_assert(pipe_fd[1] >= 0);

        n_ref++;

        return 0;
    }

    pa_assert(lock_fd < 0);
    pa_assert(!lock_fd_mutex);
    pa_assert(!taken);
    pa_assert(!thread);
    pa_assert(pipe_fd[0] < 0);
    pa_assert(pipe_fd[1] < 0);

    if (pipe(pipe_fd) < 0)
        return -1;

    lock_fd_mutex = pa_mutex_new(FALSE, FALSE);

    pa_make_fd_cloexec(pipe_fd[0]);
    pa_make_fd_cloexec(pipe_fd[1]);

    pa_make_fd_nonblock(pipe_fd[1]);
    pa_make_fd_nonblock(pipe_fd[0]);

    n_ref = 1;
    return 0;
}

static void unref(pa_bool_t after_fork) {

    pa_assert(n_ref > 0);
    pa_assert(pipe_fd[0] >= 0);
    pa_assert(pipe_fd[1] >= 0);
    pa_assert(lock_fd_mutex);

    n_ref--;

    if (n_ref > 0)
        return;

    pa_assert(!taken);

    if (thread) {
        pa_thread_free(thread);
        thread = NULL;
    }

    pa_mutex_lock(lock_fd_mutex);
    if (lock_fd >= 0) {

        if (after_fork)
            pa_close(lock_fd);
        else {
            char *lf;

            if (!(lf = pa_runtime_path(AUTOSPAWN_LOCK)))
                pa_log_warn(_("Cannot access autospawn lock."));

            pa_unlock_lockfile(lf, lock_fd);
            pa_xfree(lf);

            lock_fd = -1;
        }
    }
    pa_mutex_unlock(lock_fd_mutex);

    pa_mutex_free(lock_fd_mutex);
    lock_fd_mutex = NULL;

    pa_close(pipe_fd[0]);
    pa_close(pipe_fd[1]);
    pipe_fd[0] = pipe_fd[1] = -1;
}

static void ping(void) {
    ssize_t s;

    pa_assert(pipe_fd[1] >= 0);

    for (;;) {
        char x = 'x';

        if ((s = write(pipe_fd[1], &x, 1)) == 1)
            break;

        pa_assert(s < 0);

        if (errno == EAGAIN)
            break;

        pa_assert(errno == EINTR);
    }
}

static void wait_for_ping(void) {
    ssize_t s;
    char x;
    struct pollfd pfd;
    int k;

    pa_assert(pipe_fd[0] >= 0);

    memset(&pfd, 0, sizeof(pfd));
    pfd.fd = pipe_fd[0];
    pfd.events = POLLIN;

    if ((k = poll(&pfd, 1, -1)) != 1) {
        pa_assert(k < 0);
        pa_assert(errno == EINTR);
    } else if ((s = read(pipe_fd[0], &x, 1)) != 1) {
        pa_assert(s < 0);
        pa_assert(errno == EAGAIN);
    }
}

static void empty_pipe(void) {
    char x[16];
    ssize_t s;

    pa_assert(pipe_fd[0] >= 0);

    if ((s = read(pipe_fd[0], &x, sizeof(x))) < 1) {
        pa_assert(s < 0);
        pa_assert(errno == EAGAIN);
    }
}

static void thread_func(void *u) {
    int fd;
    char *lf;
    sigset_t fullset;

    /* No signals in this thread please */
    sigfillset(&fullset);
    pthread_sigmask(SIG_BLOCK, &fullset, NULL);

    if (!(lf = pa_runtime_path(AUTOSPAWN_LOCK))) {
        pa_log_warn(_("Cannot access autospawn lock."));
        goto finish;
    }

    if ((fd = pa_lock_lockfile(lf)) < 0)
        goto finish;

    pa_mutex_lock(lock_fd_mutex);
    pa_assert(lock_fd < 0);
    lock_fd = fd;
    pa_mutex_unlock(lock_fd_mutex);

finish:
    pa_xfree(lf);

    ping();
}

static int start_thread(void) {

    if (!thread)
        if (!(thread = pa_thread_new(thread_func, NULL)))
            return -1;

    return 0;
}

static void create_mutex(void) {
    PA_ONCE_BEGIN {
        mutex = pa_mutex_new(FALSE, FALSE);
    } PA_ONCE_END;
}

static void destroy_mutex(void) {

    if (mutex)
        pa_mutex_free(mutex);
}


int pa_autospawn_lock_init(void) {
    int ret = -1;

    create_mutex();
    pa_mutex_lock(mutex);

    if (ref() < 0)
        ret = -1;
    else
        ret = pipe_fd[0];

    pa_mutex_unlock(mutex);

    return ret;
}

int pa_autospawn_lock_acquire(pa_bool_t block) {
    int ret = -1;

    create_mutex();
    pa_mutex_lock(mutex);
    pa_assert(n_ref >= 1);

    pa_mutex_lock(lock_fd_mutex);

    for (;;) {

        empty_pipe();

        if (lock_fd >= 0 && !taken) {
            taken = TRUE;
            ret = 1;
            break;
        }

        if (lock_fd < 0)
            if (start_thread() < 0)
                break;

        if (!block) {
            ret = 0;
            break;
        }

        pa_mutex_unlock(lock_fd_mutex);
        pa_mutex_unlock(mutex);

        wait_for_ping();

        pa_mutex_lock(mutex);
        pa_mutex_lock(lock_fd_mutex);
    }

    pa_mutex_unlock(lock_fd_mutex);

    pa_mutex_unlock(mutex);

    return ret;
}

void pa_autospawn_lock_release(void) {

    create_mutex();
    pa_mutex_lock(mutex);
    pa_assert(n_ref >= 1);

    pa_assert(taken);
    taken = FALSE;

    ping();

    pa_mutex_unlock(mutex);
}

void pa_autospawn_lock_done(pa_bool_t after_fork) {

    create_mutex();
    pa_mutex_lock(mutex);
    pa_assert(n_ref >= 1);

    unref(after_fork);

    pa_mutex_unlock(mutex);
}