1*241bea01Schristos /* $NetBSD: common.c,v 1.3 2019/12/15 22:50:50 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2009 Kungliga Tekniska H�gskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ca1c9b0cSelric *
10ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
11ca1c9b0cSelric * modification, are permitted provided that the following conditions
12ca1c9b0cSelric * are met:
13ca1c9b0cSelric *
14ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
15ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
16ca1c9b0cSelric *
17ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
18ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
19ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
20ca1c9b0cSelric *
21ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
22ca1c9b0cSelric * may be used to endorse or promote products derived from this software
23ca1c9b0cSelric * without specific prior written permission.
24ca1c9b0cSelric *
25ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ca1c9b0cSelric * SUCH DAMAGE.
36ca1c9b0cSelric */
37ca1c9b0cSelric
38ca1c9b0cSelric #include "hi_locl.h"
39ca1c9b0cSelric #ifdef HAVE_GCD
40ca1c9b0cSelric #include <dispatch/dispatch.h>
41ca1c9b0cSelric #else
42ca1c9b0cSelric #include "heim_threads.h"
43ca1c9b0cSelric #endif
44ca1c9b0cSelric
45ca1c9b0cSelric struct heim_icred {
46ca1c9b0cSelric uid_t uid;
47ca1c9b0cSelric gid_t gid;
48ca1c9b0cSelric pid_t pid;
49ca1c9b0cSelric pid_t session;
50ca1c9b0cSelric };
51ca1c9b0cSelric
52ca1c9b0cSelric void
heim_ipc_free_cred(heim_icred cred)53ca1c9b0cSelric heim_ipc_free_cred(heim_icred cred)
54ca1c9b0cSelric {
55ca1c9b0cSelric free(cred);
56ca1c9b0cSelric }
57ca1c9b0cSelric
58ca1c9b0cSelric uid_t
heim_ipc_cred_get_uid(heim_icred cred)59ca1c9b0cSelric heim_ipc_cred_get_uid(heim_icred cred)
60ca1c9b0cSelric {
61ca1c9b0cSelric return cred->uid;
62ca1c9b0cSelric }
63ca1c9b0cSelric
64ca1c9b0cSelric gid_t
heim_ipc_cred_get_gid(heim_icred cred)65ca1c9b0cSelric heim_ipc_cred_get_gid(heim_icred cred)
66ca1c9b0cSelric {
67ca1c9b0cSelric return cred->gid;
68ca1c9b0cSelric }
69ca1c9b0cSelric
70ca1c9b0cSelric pid_t
heim_ipc_cred_get_pid(heim_icred cred)71ca1c9b0cSelric heim_ipc_cred_get_pid(heim_icred cred)
72ca1c9b0cSelric {
73ca1c9b0cSelric return cred->pid;
74ca1c9b0cSelric }
75ca1c9b0cSelric
76ca1c9b0cSelric pid_t
heim_ipc_cred_get_session(heim_icred cred)77ca1c9b0cSelric heim_ipc_cred_get_session(heim_icred cred)
78ca1c9b0cSelric {
79ca1c9b0cSelric return cred->session;
80ca1c9b0cSelric }
81ca1c9b0cSelric
82ca1c9b0cSelric
83ca1c9b0cSelric int
_heim_ipc_create_cred(uid_t uid,gid_t gid,pid_t pid,pid_t session,heim_icred * cred)84ca1c9b0cSelric _heim_ipc_create_cred(uid_t uid, gid_t gid, pid_t pid, pid_t session, heim_icred *cred)
85ca1c9b0cSelric {
86ca1c9b0cSelric *cred = calloc(1, sizeof(**cred));
87ca1c9b0cSelric if (*cred == NULL)
88ca1c9b0cSelric return ENOMEM;
89ca1c9b0cSelric (*cred)->uid = uid;
90ca1c9b0cSelric (*cred)->gid = gid;
91ca1c9b0cSelric (*cred)->pid = pid;
92ca1c9b0cSelric (*cred)->session = session;
93ca1c9b0cSelric return 0;
94ca1c9b0cSelric }
95ca1c9b0cSelric
96ca1c9b0cSelric #ifndef HAVE_GCD
97ca1c9b0cSelric struct heim_isemaphore {
98ca1c9b0cSelric HEIMDAL_MUTEX mutex;
99*241bea01Schristos #ifdef ENABLE_PTHREAD_SUPPORT
100ca1c9b0cSelric pthread_cond_t cond;
101*241bea01Schristos #endif
102ca1c9b0cSelric long counter;
103ca1c9b0cSelric };
104ca1c9b0cSelric #endif
105ca1c9b0cSelric
106ca1c9b0cSelric heim_isemaphore
heim_ipc_semaphore_create(long value)107ca1c9b0cSelric heim_ipc_semaphore_create(long value)
108ca1c9b0cSelric {
109ca1c9b0cSelric #ifdef HAVE_GCD
110ca1c9b0cSelric return (heim_isemaphore)dispatch_semaphore_create(value);
111ca1c9b0cSelric #elif !defined(ENABLE_PTHREAD_SUPPORT)
112ca1c9b0cSelric heim_assert(0, "no semaphore support w/o pthreads");
113ca1c9b0cSelric return NULL;
114ca1c9b0cSelric #else
115ca1c9b0cSelric heim_isemaphore s = malloc(sizeof(*s));
116ca1c9b0cSelric if (s == NULL)
117ca1c9b0cSelric return NULL;
118ca1c9b0cSelric HEIMDAL_MUTEX_init(&s->mutex);
119ca1c9b0cSelric pthread_cond_init(&s->cond, NULL);
120ca1c9b0cSelric s->counter = value;
121ca1c9b0cSelric return s;
122ca1c9b0cSelric #endif
123ca1c9b0cSelric }
124ca1c9b0cSelric
125ca1c9b0cSelric long
heim_ipc_semaphore_wait(heim_isemaphore s,time_t t)126ca1c9b0cSelric heim_ipc_semaphore_wait(heim_isemaphore s, time_t t)
127ca1c9b0cSelric {
128ca1c9b0cSelric #ifdef HAVE_GCD
129ca1c9b0cSelric uint64_t timeout;
130ca1c9b0cSelric if (t == HEIM_IPC_WAIT_FOREVER)
131ca1c9b0cSelric timeout = DISPATCH_TIME_FOREVER;
132ca1c9b0cSelric else
133ca1c9b0cSelric timeout = (uint64_t)t * NSEC_PER_SEC;
134ca1c9b0cSelric
135ca1c9b0cSelric return dispatch_semaphore_wait((dispatch_semaphore_t)s, timeout);
136ca1c9b0cSelric #elif !defined(ENABLE_PTHREAD_SUPPORT)
137ca1c9b0cSelric heim_assert(0, "no semaphore support w/o pthreads");
138ca1c9b0cSelric return 0;
139ca1c9b0cSelric #else
140ca1c9b0cSelric HEIMDAL_MUTEX_lock(&s->mutex);
141ca1c9b0cSelric /* if counter hits below zero, we get to wait */
142ca1c9b0cSelric if (--s->counter < 0) {
143ca1c9b0cSelric int ret;
144ca1c9b0cSelric
145ca1c9b0cSelric if (t == HEIM_IPC_WAIT_FOREVER)
146ca1c9b0cSelric ret = pthread_cond_wait(&s->cond, &s->mutex);
147ca1c9b0cSelric else {
148ca1c9b0cSelric struct timespec ts;
149ca1c9b0cSelric ts.tv_sec = t;
150ca1c9b0cSelric ts.tv_nsec = 0;
151ca1c9b0cSelric ret = pthread_cond_timedwait(&s->cond, &s->mutex, &ts);
152ca1c9b0cSelric }
153ca1c9b0cSelric if (ret) {
154ca1c9b0cSelric HEIMDAL_MUTEX_unlock(&s->mutex);
155ca1c9b0cSelric return errno;
156ca1c9b0cSelric }
157ca1c9b0cSelric }
158ca1c9b0cSelric HEIMDAL_MUTEX_unlock(&s->mutex);
159ca1c9b0cSelric
160ca1c9b0cSelric return 0;
161ca1c9b0cSelric #endif
162ca1c9b0cSelric }
163ca1c9b0cSelric
164ca1c9b0cSelric long
heim_ipc_semaphore_signal(heim_isemaphore s)165ca1c9b0cSelric heim_ipc_semaphore_signal(heim_isemaphore s)
166ca1c9b0cSelric {
167ca1c9b0cSelric #ifdef HAVE_GCD
168ca1c9b0cSelric return dispatch_semaphore_signal((dispatch_semaphore_t)s);
169ca1c9b0cSelric #elif !defined(ENABLE_PTHREAD_SUPPORT)
170ca1c9b0cSelric heim_assert(0, "no semaphore support w/o pthreads");
171ca1c9b0cSelric return EINVAL;
172ca1c9b0cSelric #else
173ca1c9b0cSelric int wakeup;
174ca1c9b0cSelric HEIMDAL_MUTEX_lock(&s->mutex);
175ca1c9b0cSelric wakeup = (++s->counter == 0) ;
176ca1c9b0cSelric HEIMDAL_MUTEX_unlock(&s->mutex);
177ca1c9b0cSelric if (wakeup)
178ca1c9b0cSelric pthread_cond_signal(&s->cond);
179ca1c9b0cSelric return 0;
180ca1c9b0cSelric #endif
181ca1c9b0cSelric }
182ca1c9b0cSelric
183ca1c9b0cSelric void
heim_ipc_semaphore_release(heim_isemaphore s)184ca1c9b0cSelric heim_ipc_semaphore_release(heim_isemaphore s)
185ca1c9b0cSelric {
186ca1c9b0cSelric #ifdef HAVE_GCD
187ca1c9b0cSelric dispatch_release((dispatch_semaphore_t)s);
188ca1c9b0cSelric #elif !defined(ENABLE_PTHREAD_SUPPORT)
189ca1c9b0cSelric heim_assert(0, "no semaphore support w/o pthreads");
190ca1c9b0cSelric #else
191ca1c9b0cSelric HEIMDAL_MUTEX_lock(&s->mutex);
192ca1c9b0cSelric if (s->counter != 0)
193ca1c9b0cSelric abort();
194ca1c9b0cSelric HEIMDAL_MUTEX_unlock(&s->mutex);
195ca1c9b0cSelric HEIMDAL_MUTEX_destroy(&s->mutex);
196ca1c9b0cSelric pthread_cond_destroy(&s->cond);
197ca1c9b0cSelric free(s);
198ca1c9b0cSelric #endif
199ca1c9b0cSelric }
200ca1c9b0cSelric
201ca1c9b0cSelric void
heim_ipc_free_data(heim_idata * data)202ca1c9b0cSelric heim_ipc_free_data(heim_idata *data)
203ca1c9b0cSelric {
204ca1c9b0cSelric if (data->data)
205ca1c9b0cSelric free(data->data);
206ca1c9b0cSelric data->data = NULL;
207ca1c9b0cSelric data->length = 0;
208ca1c9b0cSelric }
209