Linking fix for rtclock on libpulsedsp
[pulseaudio-mirror.git] / src / pulsecore / auth-cookie.c
blob68b01473a06cda7bd864c313998d28a2fb717f23
1 /***
2 This file is part of PulseAudio.
4 Copyright 2008 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 published
8 by the Free Software Foundation; either version 2 of the License,
9 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 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <sys/types.h>
28 #include <pulse/xmalloc.h>
29 #include <pulse/util.h>
31 #include <pulsecore/refcnt.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/shared.h>
35 #include <pulsecore/authkey.h>
37 #include "auth-cookie.h"
39 struct pa_auth_cookie {
40 PA_REFCNT_DECLARE;
41 pa_core *core;
42 char *name;
43 size_t size;
46 pa_auth_cookie* pa_auth_cookie_get(pa_core *core, const char *cn, size_t size) {
47 pa_auth_cookie *c;
48 char *t;
50 pa_assert(core);
51 pa_assert(size > 0);
53 t = pa_sprintf_malloc("auth-cookie%s%s", cn ? "@" : "", cn ? cn : "");
55 if ((c = pa_shared_get(core, t))) {
57 pa_xfree(t);
59 if (c->size != size)
60 return NULL;
62 return pa_auth_cookie_ref(c);
65 c = pa_xmalloc(PA_ALIGN(sizeof(pa_auth_cookie)) + size);
66 PA_REFCNT_INIT(c);
67 c->core = core;
68 c->name = t;
69 c->size = size;
71 pa_assert_se(pa_shared_set(core, t, c) >= 0);
73 if (pa_authkey_load_auto(cn, (uint8_t*) c + PA_ALIGN(sizeof(pa_auth_cookie)), size) < 0) {
74 pa_auth_cookie_unref(c);
75 return NULL;
78 return c;
81 pa_auth_cookie* pa_auth_cookie_ref(pa_auth_cookie *c) {
82 pa_assert(c);
83 pa_assert(PA_REFCNT_VALUE(c) >= 1);
85 PA_REFCNT_INC(c);
87 return c;
90 void pa_auth_cookie_unref(pa_auth_cookie *c) {
91 pa_assert(c);
92 pa_assert(PA_REFCNT_VALUE(c) >= 1);
94 if (PA_REFCNT_DEC(c) > 0)
95 return;
97 pa_assert_se(pa_shared_remove(c->core, c->name) >= 0);
99 pa_xfree(c->name);
100 pa_xfree(c);
103 const uint8_t* pa_auth_cookie_read(pa_auth_cookie *c, size_t size) {
104 pa_assert(c);
105 pa_assert(PA_REFCNT_VALUE(c) >= 1);
106 pa_assert(c->size == size);
108 return (const uint8_t*) c + PA_ALIGN(sizeof(pa_auth_cookie));