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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
/*
* Copyright (c) 2009 Kungliga Tekniska Hgskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Portions Copyright (c) 2009 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "hi_locl.h"
#if defined(__APPLE__) && defined(HAVE_GCD)
#include "heim_ipc.h"
#include "heim_ipc_asyncServer.h"
#include <dispatch/dispatch.h>
#include <mach/mach.h>
static dispatch_once_t jobqinited = 0;
static dispatch_queue_t jobq = NULL;
static dispatch_queue_t syncq;
struct mach_ctx {
mach_port_t server;
char *name;
};
static int
mach_release(void *ctx);
static int
mach_init(const char *service, void **ctx)
{
struct mach_ctx *ipc;
mach_port_t sport;
int ret;
dispatch_once(&jobqinited, ^{
jobq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
syncq = dispatch_queue_create("heim-ipc-syncq", NULL);
});
ret = bootstrap_look_up(bootstrap_port, service, &sport);
if (ret)
return ret;
ipc = malloc(sizeof(*ipc));
if (ipc == NULL) {
mach_port_destroy(mach_task_self(), sport);
return ENOMEM;
}
ipc->server = sport;
ipc->name = strdup(service);
if (ipc->name == NULL) {
mach_release(ipc);
return ENOMEM;
}
*ctx = ipc;
return 0;
}
static int
mach_ipc(void *ctx,
const heim_idata *request, heim_idata *response,
heim_icred *cred)
{
struct mach_ctx *ipc = ctx;
heim_ipc_message_inband_t requestin;
mach_msg_type_number_t requestin_length = 0;
heim_ipc_message_outband_t requestout = NULL;
mach_msg_type_number_t requestout_length = 0;
heim_ipc_message_inband_t replyin;
mach_msg_type_number_t replyin_length;
heim_ipc_message_outband_t replyout;
mach_msg_type_number_t replyout_length;
int ret, errorcode, retries = 0;
memcpy(requestin, request->data, request->length);
requestin_length = request->length;
while (retries < 2) {
__block mach_port_t sport;
dispatch_sync(syncq, ^{ sport = ipc->server; });
ret = mheim_ipc_call(sport,
requestin, requestin_length,
requestout, requestout_length,
&errorcode,
replyin, &replyin_length,
&replyout, &replyout_length);
if (ret == MACH_SEND_INVALID_DEST) {
mach_port_t nport;
/* race other threads to get a new port */
ret = bootstrap_look_up(bootstrap_port, ipc->name, &nport);
if (ret)
return ret;
dispatch_sync(syncq, ^{
/* check if we lost the race to lookup the port */
if (sport != ipc->server) {
mach_port_deallocate(mach_task_self(), nport);
} else {
mach_port_deallocate(mach_task_self(), ipc->server);
ipc->server = nport;
}
});
retries++;
} else if (ret) {
return ret;
} else
break;
}
if (retries >= 2)
return EINVAL;
if (errorcode) {
if (replyout_length)
vm_deallocate (mach_task_self (), (vm_address_t) replyout,
replyout_length);
return errorcode;
}
if (replyout_length) {
response->data = malloc(replyout_length);
if (response->data == NULL) {
vm_deallocate (mach_task_self (), (vm_address_t) replyout,
replyout_length);
return ENOMEM;
}
memcpy(response->data, replyout, replyout_length);
response->length = replyout_length;
vm_deallocate (mach_task_self (), (vm_address_t) replyout,
replyout_length);
} else {
response->data = malloc(replyin_length);
if (response->data == NULL)
return ENOMEM;
memcpy(response->data, replyin, replyin_length);
response->length = replyin_length;
}
return 0;
}
struct async_client {
mach_port_t mp;
dispatch_source_t source;
dispatch_queue_t queue;
void (*func)(void *, int, heim_idata *, heim_icred);
void *userctx;
};
kern_return_t
mheim_ado_acall_reply(mach_port_t server_port,
audit_token_t client_creds,
int returnvalue,
heim_ipc_message_inband_t replyin,
mach_msg_type_number_t replyinCnt,
heim_ipc_message_outband_t replyout,
mach_msg_type_number_t replyoutCnt)
{
struct async_client *c = dispatch_get_context(dispatch_get_current_queue());
heim_idata response;
if (returnvalue) {
response.data = NULL;
response.length = 0;
} else if (replyoutCnt) {
response.data = replyout;
response.length = replyoutCnt;
} else {
response.data = replyin;
response.length = replyinCnt;
}
(*c->func)(c->userctx, returnvalue, &response, NULL);
if (replyoutCnt)
vm_deallocate (mach_task_self (), (vm_address_t) replyout, replyoutCnt);
dispatch_source_cancel(c->source);
return 0;
}
static int
mach_async(void *ctx, const heim_idata *request, void *userctx,
void (*func)(void *, int, heim_idata *, heim_icred))
{
struct mach_ctx *ipc = ctx;
heim_ipc_message_inband_t requestin;
mach_msg_type_number_t requestin_length = 0;
heim_ipc_message_outband_t requestout = NULL;
mach_msg_type_number_t requestout_length = 0;
int ret, retries = 0;
kern_return_t kr;
struct async_client *c;
/* first create the service that will catch the reply from the server */
/* XXX these object should be cached and reused */
c = malloc(sizeof(*c));
if (c == NULL)
return ENOMEM;
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &c->mp);
if (kr != KERN_SUCCESS)
return EINVAL;
c->queue = dispatch_queue_create("heim-ipc-async-client", NULL);
c->source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, c->mp, 0, c->queue);
dispatch_set_context(c->queue, c);
dispatch_source_set_event_handler(c->source, ^{
dispatch_mig_server(c->source,
sizeof(union __RequestUnion__mheim_ado_mheim_aipc_subsystem),
mheim_aipc_server);
});
dispatch_source_set_cancel_handler(c->source, ^{
mach_port_mod_refs(mach_task_self(), c->mp,
MACH_PORT_RIGHT_RECEIVE, -1);
dispatch_release(c->queue);
dispatch_release(c->source);
free(c);
});
c->func = func;
c->userctx = userctx;
dispatch_resume(c->source);
/* ok, send the message */
memcpy(requestin, request->data, request->length);
requestin_length = request->length;
while (retries < 2) {
__block mach_port_t sport;
dispatch_sync(syncq, ^{ sport = ipc->server; });
ret = mheim_ipc_call_request(sport, c->mp,
requestin, requestin_length,
requestout, requestout_length);
if (ret == MACH_SEND_INVALID_DEST) {
ret = bootstrap_look_up(bootstrap_port, ipc->name, &sport);
if (ret) {
dispatch_source_cancel(c->source);
return ret;
}
mach_port_deallocate(mach_task_self(), ipc->server);
ipc->server = sport;
retries++;
} else if (ret) {
dispatch_source_cancel(c->source);
return ret;
} else
break;
}
if (retries >= 2) {
dispatch_source_cancel(c->source);
return EINVAL;
}
return 0;
}
static int
mach_release(void *ctx)
{
struct mach_ctx *ipc = ctx;
if (ipc->server != MACH_PORT_NULL)
mach_port_deallocate(mach_task_self(), ipc->server);
free(ipc->name);
free(ipc);
return 0;
}
#endif
struct path_ctx {
char *path;
int fd;
};
static int common_release(void *);
static int
connect_unix(struct path_ctx *s)
{
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strlcpy(addr.sun_path, s->path, sizeof(addr.sun_path));
s->fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (s->fd < 0)
return errno;
rk_cloexec(s->fd);
if (connect(s->fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
close(s->fd);
return errno;
}
return 0;
}
static int
common_path_init(const char *service,
const char *file,
void **ctx)
{
struct path_ctx *s;
s = malloc(sizeof(*s));
if (s == NULL)
return ENOMEM;
s->fd = -1;
if (asprintf(&s->path, "/var/run/.heim_%s-%s", service, file) == -1) {
free(s);
return ENOMEM;
}
*ctx = s;
return 0;
}
static int
unix_socket_init(const char *service,
void **ctx)
{
int ret;
ret = common_path_init(service, "socket", ctx);
if (ret)
return ret;
ret = connect_unix(*ctx);
if (ret)
common_release(*ctx);
return ret;
}
static int
unix_socket_ipc(void *ctx,
const heim_idata *req, heim_idata *rep,
heim_icred *cred)
{
struct path_ctx *s = ctx;
uint32_t len = htonl(req->length);
uint32_t rv;
int retval;
if (cred)
*cred = NULL;
rep->data = NULL;
rep->length = 0;
if (net_write(s->fd, &len, sizeof(len)) != sizeof(len))
return -1;
if (net_write(s->fd, req->data, req->length) != (ssize_t)req->length)
return -1;
if (net_read(s->fd, &len, sizeof(len)) != sizeof(len))
return -1;
if (net_read(s->fd, &rv, sizeof(rv)) != sizeof(rv))
return -1;
retval = ntohl(rv);
rep->length = ntohl(len);
if (rep->length > 0) {
rep->data = malloc(rep->length);
if (rep->data == NULL)
return -1;
if (net_read(s->fd, rep->data, rep->length) != (ssize_t)rep->length)
return -1;
} else
rep->data = NULL;
return retval;
}
int
common_release(void *ctx)
{
struct path_ctx *s = ctx;
if (s->fd >= 0)
close(s->fd);
free(s->path);
free(s);
return 0;
}
#ifdef HAVE_DOOR
static int
door_init(const char *service,
void **ctx)
{
ret = common_path_init(context, service, "door", ctx);
if (ret)
return ret;
ret = connect_door(*ctx);
if (ret)
common_release(*ctx);
return ret;
}
static int
door_ipc(void *ctx,
const heim_idata *request, heim_idata *response,
heim_icred *cred)
{
door_arg_t arg;
int ret;
arg.data_ptr = request->data;
arg.data_size = request->length;
arg.desc_ptr = NULL;
arg.desc_num = 0;
arg.rbuf = NULL;
arg.rsize = 0;
ret = door_call(fd, &arg);
close(fd);
if (ret != 0)
return errno;
response->data = malloc(arg.rsize);
if (response->data == NULL) {
munmap(arg.rbuf, arg.rsize);
return ENOMEM;
}
memcpy(response->data, arg.rbuf, arg.rsize);
response->length = arg.rsize;
munmap(arg.rbuf, arg.rsize);
return ret;
}
#endif
struct hipc_ops {
const char *prefix;
int (*init)(const char *, void **);
int (*release)(void *);
int (*ipc)(void *,const heim_idata *, heim_idata *, heim_icred *);
int (*async)(void *, const heim_idata *, void *,
void (*)(void *, int, heim_idata *, heim_icred));
};
struct hipc_ops ipcs[] = {
#if defined(__APPLE__) && defined(HAVE_GCD)
{ "MACH", mach_init, mach_release, mach_ipc, mach_async },
#endif
#ifdef HAVE_DOOR
{ "DOOR", door_init, common_release, door_ipc, NULL }
#endif
{ "UNIX", unix_socket_init, common_release, unix_socket_ipc, NULL }
};
struct heim_ipc {
struct hipc_ops *ops;
void *ctx;
};
int
heim_ipc_init_context(const char *name, heim_ipc *ctx)
{
unsigned int i;
int ret, any = 0;
for(i = 0; i < sizeof(ipcs)/sizeof(ipcs[0]); i++) {
size_t prefix_len = strlen(ipcs[i].prefix);
heim_ipc c;
if(strncmp(ipcs[i].prefix, name, prefix_len) == 0
&& name[prefix_len] == ':') {
} else if (strncmp("ANY:", name, 4) == 0) {
prefix_len = 3;
any = 1;
} else
continue;
c = calloc(1, sizeof(*c));
if (c == NULL)
return ENOMEM;
c->ops = &ipcs[i];
ret = (c->ops->init)(name + prefix_len + 1, &c->ctx);
if (ret) {
free(c);
if (any)
continue;
return ret;
}
*ctx = c;
return 0;
}
return ENOENT;
}
void
heim_ipc_free_context(heim_ipc ctx)
{
(ctx->ops->release)(ctx->ctx);
free(ctx);
}
int
heim_ipc_call(heim_ipc ctx, const heim_idata *snd, heim_idata *rcv,
heim_icred *cred)
{
if (cred)
*cred = NULL;
return (ctx->ops->ipc)(ctx->ctx, snd, rcv, cred);
}
int
heim_ipc_async(heim_ipc ctx, const heim_idata *snd, void *userctx,
void (*func)(void *, int, heim_idata *, heim_icred))
{
if (ctx->ops->async == NULL) {
heim_idata rcv;
heim_icred cred = NULL;
int ret;
ret = (ctx->ops->ipc)(ctx->ctx, snd, &rcv, &cred);
(*func)(userctx, ret, &rcv, cred);
heim_ipc_free_cred(cred);
free(rcv.data);
return ret;
} else {
return (ctx->ops->async)(ctx->ctx, snd, userctx, func);
}
}
|