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