| 1 |
/* |
| 2 |
* Copyright (c) 2009 Kungliga Tekniska H�gskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Portions Copyright (c) 2009 Apple Inc. All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer in the |
| 17 |
* documentation and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 20 |
* may be used to endorse or promote products derived from this software |
| 21 |
* without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 24 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 |
* SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include "hi_locl.h" |
| 37 |
#include <assert.h> |
| 38 |
#include <err.h> |
| 39 |
|
| 40 |
#define MAX_PACKET_SIZE (128 * 1024) |
| 41 |
|
| 42 |
struct heim_sipc { |
| 43 |
int (*release)(heim_sipc ctx); |
| 44 |
heim_ipc_callback callback; |
| 45 |
void *userctx; |
| 46 |
void *mech; |
| 47 |
}; |
| 48 |
|
| 49 |
#if defined(__APPLE__) && defined(HAVE_GCD) |
| 50 |
|
| 51 |
#include "heim_ipcServer.h" |
| 52 |
#include "heim_ipc_reply.h" |
| 53 |
#include "heim_ipc_async.h" |
| 54 |
|
| 55 |
static dispatch_source_t timer; |
| 56 |
static dispatch_queue_t timerq; |
| 57 |
static uint64_t timeoutvalue; |
| 58 |
|
| 59 |
static dispatch_queue_t eventq; |
| 60 |
|
| 61 |
static dispatch_queue_t workq; |
| 62 |
|
| 63 |
static void |
| 64 |
default_timer_ev(void) |
| 65 |
{ |
| 66 |
exit(0); |
| 67 |
} |
| 68 |
|
| 69 |
static void (*timer_ev)(void) = default_timer_ev; |
| 70 |
|
| 71 |
static void |
| 72 |
set_timer(void) |
| 73 |
{ |
| 74 |
dispatch_source_set_timer(timer, |
| 75 |
dispatch_time(DISPATCH_TIME_NOW, |
| 76 |
timeoutvalue * NSEC_PER_SEC), |
| 77 |
timeoutvalue * NSEC_PER_SEC, 1000000); |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
init_globals(void) |
| 82 |
{ |
| 83 |
static dispatch_once_t once; |
| 84 |
dispatch_once(&once, ^{ |
| 85 |
timerq = dispatch_queue_create("hiem-sipc-timer-q", NULL); |
| 86 |
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timerq); |
| 87 |
dispatch_source_set_event_handler(timer, ^{ timer_ev(); } ); |
| 88 |
|
| 89 |
workq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
| 90 |
eventq = dispatch_queue_create("heim-ipc.event-queue", NULL); |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
suspend_timer(void) |
| 96 |
{ |
| 97 |
dispatch_suspend(timer); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
restart_timer(void) |
| 102 |
{ |
| 103 |
dispatch_sync(timerq, ^{ set_timer(); }); |
| 104 |
dispatch_resume(timer); |
| 105 |
} |
| 106 |
|
| 107 |
struct mach_service { |
| 108 |
mach_port_t sport; |
| 109 |
dispatch_source_t source; |
| 110 |
dispatch_queue_t queue; |
| 111 |
}; |
| 112 |
|
| 113 |
struct mach_call_ctx { |
| 114 |
mach_port_t reply_port; |
| 115 |
heim_icred cred; |
| 116 |
heim_idata req; |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
static void |
| 121 |
mach_complete_sync(heim_sipc_call ctx, int returnvalue, heim_idata *reply) |
| 122 |
{ |
| 123 |
struct mach_call_ctx *s = (struct mach_call_ctx *)ctx; |
| 124 |
heim_ipc_message_inband_t replyin; |
| 125 |
mach_msg_type_number_t replyinCnt; |
| 126 |
heim_ipc_message_outband_t replyout; |
| 127 |
mach_msg_type_number_t replyoutCnt; |
| 128 |
|
| 129 |
|
| 130 |
if (returnvalue) { |
| 131 |
/* on error, no reply */ |
| 132 |
replyinCnt = 0; |
| 133 |
replyout = 0; replyoutCnt = 0; |
| 134 |
} else if (reply->length < 2048) { |
| 135 |
replyinCnt = reply->length; |
| 136 |
memcpy(replyin, reply->data, replyinCnt); |
| 137 |
replyout = 0; replyoutCnt = 0; |
| 138 |
} else { |
| 139 |
replyinCnt = 0; |
| 140 |
vm_read(mach_task_self(), |
| 141 |
(vm_address_t)reply->data, reply->length, |
| 142 |
(vm_address_t *)&replyout, &replyoutCnt); |
| 143 |
} |
| 144 |
|
| 145 |
mheim_ripc_call_reply(s->reply_port, returnvalue, |
| 146 |
replyin, replyinCnt, |
| 147 |
replyout, replyoutCnt); |
| 148 |
|
| 149 |
heim_ipc_free_cred(s->cred); |
| 150 |
free(s->req.data); |
| 151 |
free(s); |
| 152 |
restart_timer(); |
| 153 |
} |
| 154 |
|
| 155 |
static void |
| 156 |
mach_complete_async(heim_sipc_call ctx, int returnvalue, heim_idata *reply) |
| 157 |
{ |
| 158 |
struct mach_call_ctx *s = (struct mach_call_ctx *)ctx; |
| 159 |
heim_ipc_message_inband_t replyin; |
| 160 |
mach_msg_type_number_t replyinCnt; |
| 161 |
heim_ipc_message_outband_t replyout; |
| 162 |
mach_msg_type_number_t replyoutCnt; |
| 163 |
|
| 164 |
if (returnvalue) { |
| 165 |
/* on error, no reply */ |
| 166 |
replyinCnt = 0; |
| 167 |
replyout = 0; replyoutCnt = 0; |
| 168 |
} else if (reply->length < 2048) { |
| 169 |
replyinCnt = reply->length; |
| 170 |
memcpy(replyin, reply->data, replyinCnt); |
| 171 |
replyout = 0; replyoutCnt = 0; |
| 172 |
} else { |
| 173 |
replyinCnt = 0; |
| 174 |
vm_read(mach_task_self(), |
| 175 |
(vm_address_t)reply->data, reply->length, |
| 176 |
(vm_address_t *)&replyout, &replyoutCnt); |
| 177 |
} |
| 178 |
|
| 179 |
mheim_aipc_acall_reply(s->reply_port, returnvalue, |
| 180 |
replyin, replyinCnt, |
| 181 |
replyout, replyoutCnt); |
| 182 |
heim_ipc_free_cred(s->cred); |
| 183 |
free(s->req.data); |
| 184 |
free(s); |
| 185 |
restart_timer(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
kern_return_t |
| 190 |
mheim_do_call(mach_port_t server_port, |
| 191 |
audit_token_t client_creds, |
| 192 |
mach_port_t reply_port, |
| 193 |
heim_ipc_message_inband_t requestin, |
| 194 |
mach_msg_type_number_t requestinCnt, |
| 195 |
heim_ipc_message_outband_t requestout, |
| 196 |
mach_msg_type_number_t requestoutCnt, |
| 197 |
int *returnvalue, |
| 198 |
heim_ipc_message_inband_t replyin, |
| 199 |
mach_msg_type_number_t *replyinCnt, |
| 200 |
heim_ipc_message_outband_t *replyout, |
| 201 |
mach_msg_type_number_t *replyoutCnt) |
| 202 |
{ |
| 203 |
heim_sipc ctx = dispatch_get_context(dispatch_get_current_queue()); |
| 204 |
struct mach_call_ctx *s; |
| 205 |
kern_return_t kr; |
| 206 |
uid_t uid; |
| 207 |
gid_t gid; |
| 208 |
pid_t pid; |
| 209 |
au_asid_t session; |
| 210 |
|
| 211 |
*replyout = NULL; |
| 212 |
*replyoutCnt = 0; |
| 213 |
*replyinCnt = 0; |
| 214 |
|
| 215 |
s = malloc(sizeof(*s)); |
| 216 |
if (s == NULL) |
| 217 |
return KERN_MEMORY_FAILURE; /* XXX */ |
| 218 |
|
| 219 |
s->reply_port = reply_port; |
| 220 |
|
| 221 |
audit_token_to_au32(client_creds, NULL, &uid, &gid, NULL, NULL, &pid, &session, NULL); |
| 222 |
|
| 223 |
kr = _heim_ipc_create_cred(uid, gid, pid, session, &s->cred); |
| 224 |
if (kr) { |
| 225 |
free(s); |
| 226 |
return kr; |
| 227 |
} |
| 228 |
|
| 229 |
suspend_timer(); |
| 230 |
|
| 231 |
if (requestinCnt) { |
| 232 |
s->req.data = malloc(requestinCnt); |
| 233 |
memcpy(s->req.data, requestin, requestinCnt); |
| 234 |
s->req.length = requestinCnt; |
| 235 |
} else { |
| 236 |
s->req.data = malloc(requestoutCnt); |
| 237 |
memcpy(s->req.data, requestout, requestoutCnt); |
| 238 |
s->req.length = requestoutCnt; |
| 239 |
} |
| 240 |
|
| 241 |
dispatch_async(workq, ^{ |
| 242 |
(ctx->callback)(ctx->userctx, &s->req, s->cred, |
| 243 |
mach_complete_sync, (heim_sipc_call)s); |
| 244 |
}); |
| 245 |
|
| 246 |
return MIG_NO_REPLY; |
| 247 |
} |
| 248 |
|
| 249 |
kern_return_t |
| 250 |
mheim_do_call_request(mach_port_t server_port, |
| 251 |
audit_token_t client_creds, |
| 252 |
mach_port_t reply_port, |
| 253 |
heim_ipc_message_inband_t requestin, |
| 254 |
mach_msg_type_number_t requestinCnt, |
| 255 |
heim_ipc_message_outband_t requestout, |
| 256 |
mach_msg_type_number_t requestoutCnt) |
| 257 |
{ |
| 258 |
heim_sipc ctx = dispatch_get_context(dispatch_get_current_queue()); |
| 259 |
struct mach_call_ctx *s; |
| 260 |
kern_return_t kr; |
| 261 |
uid_t uid; |
| 262 |
gid_t gid; |
| 263 |
pid_t pid; |
| 264 |
au_asid_t session; |
| 265 |
|
| 266 |
s = malloc(sizeof(*s)); |
| 267 |
if (s == NULL) |
| 268 |
return KERN_MEMORY_FAILURE; /* XXX */ |
| 269 |
|
| 270 |
s->reply_port = reply_port; |
| 271 |
|
| 272 |
audit_token_to_au32(client_creds, NULL, &uid, &gid, NULL, NULL, &pid, &session, NULL); |
| 273 |
|
| 274 |
kr = _heim_ipc_create_cred(uid, gid, pid, session, &s->cred); |
| 275 |
if (kr) { |
| 276 |
free(s); |
| 277 |
return kr; |
| 278 |
} |
| 279 |
|
| 280 |
suspend_timer(); |
| 281 |
|
| 282 |
if (requestinCnt) { |
| 283 |
s->req.data = malloc(requestinCnt); |
| 284 |
memcpy(s->req.data, requestin, requestinCnt); |
| 285 |
s->req.length = requestinCnt; |
| 286 |
} else { |
| 287 |
s->req.data = malloc(requestoutCnt); |
| 288 |
memcpy(s->req.data, requestout, requestoutCnt); |
| 289 |
s->req.length = requestoutCnt; |
| 290 |
} |
| 291 |
|
| 292 |
dispatch_async(workq, ^{ |
| 293 |
(ctx->callback)(ctx->userctx, &s->req, s->cred, |
| 294 |
mach_complete_async, (heim_sipc_call)s); |
| 295 |
}); |
| 296 |
|
| 297 |
return KERN_SUCCESS; |
| 298 |
} |
| 299 |
|
| 300 |
static int |
| 301 |
mach_init(const char *service, mach_port_t sport, heim_sipc ctx) |
| 302 |
{ |
| 303 |
struct mach_service *s; |
| 304 |
char *name; |
| 305 |
|
| 306 |
init_globals(); |
| 307 |
|
| 308 |
s = calloc(1, sizeof(*s)); |
| 309 |
if (s == NULL) |
| 310 |
return ENOMEM; |
| 311 |
|
| 312 |
asprintf(&name, "heim-ipc-mach-%s", service); |
| 313 |
|
| 314 |
s->queue = dispatch_queue_create(name, NULL); |
| 315 |
free(name); |
| 316 |
s->sport = sport; |
| 317 |
|
| 318 |
s->source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, |
| 319 |
s->sport, 0, s->queue); |
| 320 |
if (s->source == NULL) { |
| 321 |
dispatch_release(s->queue); |
| 322 |
free(s); |
| 323 |
return ENOMEM; |
| 324 |
} |
| 325 |
ctx->mech = s; |
| 326 |
|
| 327 |
dispatch_set_context(s->queue, ctx); |
| 328 |
dispatch_set_context(s->source, s); |
| 329 |
|
| 330 |
dispatch_source_set_event_handler(s->source, ^{ |
| 331 |
dispatch_mig_server(s->source, sizeof(union __RequestUnion__mheim_do_mheim_ipc_subsystem), mheim_ipc_server); |
| 332 |
}); |
| 333 |
|
| 334 |
dispatch_source_set_cancel_handler(s->source, ^{ |
| 335 |
heim_sipc sctx = dispatch_get_context(dispatch_get_current_queue()); |
| 336 |
struct mach_service *st = sctx->mech; |
| 337 |
mach_port_mod_refs(mach_task_self(), st->sport, |
| 338 |
MACH_PORT_RIGHT_RECEIVE, -1); |
| 339 |
dispatch_release(st->queue); |
| 340 |
dispatch_release(st->source); |
| 341 |
free(st); |
| 342 |
free(sctx); |
| 343 |
}); |
| 344 |
|
| 345 |
dispatch_resume(s->source); |
| 346 |
|
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
static int |
| 351 |
mach_release(heim_sipc ctx) |
| 352 |
{ |
| 353 |
struct mach_service *s = ctx->mech; |
| 354 |
dispatch_source_cancel(s->source); |
| 355 |
dispatch_release(s->source); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
static mach_port_t |
| 360 |
mach_checkin_or_register(const char *service) |
| 361 |
{ |
| 362 |
mach_port_t mp; |
| 363 |
kern_return_t kr; |
| 364 |
|
| 365 |
kr = bootstrap_check_in(bootstrap_port, service, &mp); |
| 366 |
if (kr == KERN_SUCCESS) |
| 367 |
return mp; |
| 368 |
|
| 369 |
#if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1050 |
| 370 |
/* Pre SnowLeopard version */ |
| 371 |
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp); |
| 372 |
if (kr != KERN_SUCCESS) |
| 373 |
return MACH_PORT_NULL; |
| 374 |
|
| 375 |
kr = mach_port_insert_right(mach_task_self(), mp, mp, |
| 376 |
MACH_MSG_TYPE_MAKE_SEND); |
| 377 |
if (kr != KERN_SUCCESS) { |
| 378 |
mach_port_destroy(mach_task_self(), mp); |
| 379 |
return MACH_PORT_NULL; |
| 380 |
} |
| 381 |
|
| 382 |
kr = bootstrap_register(bootstrap_port, rk_UNCONST(service), mp); |
| 383 |
if (kr != KERN_SUCCESS) { |
| 384 |
mach_port_destroy(mach_task_self(), mp); |
| 385 |
return MACH_PORT_NULL; |
| 386 |
} |
| 387 |
|
| 388 |
return mp; |
| 389 |
#else |
| 390 |
return MACH_PORT_NULL; |
| 391 |
#endif |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
#endif /* __APPLE__ && HAVE_GCD */ |
| 396 |
|
| 397 |
|
| 398 |
int |
| 399 |
heim_sipc_launchd_mach_init(const char *service, |
| 400 |
heim_ipc_callback callback, |
| 401 |
void *user, heim_sipc *ctx) |
| 402 |
{ |
| 403 |
#if defined(__APPLE__) && defined(HAVE_GCD) |
| 404 |
mach_port_t sport = MACH_PORT_NULL; |
| 405 |
heim_sipc c = NULL; |
| 406 |
int ret; |
| 407 |
|
| 408 |
*ctx = NULL; |
| 409 |
|
| 410 |
sport = mach_checkin_or_register(service); |
| 411 |
if (sport == MACH_PORT_NULL) { |
| 412 |
ret = ENOENT; |
| 413 |
goto error; |
| 414 |
} |
| 415 |
|
| 416 |
c = calloc(1, sizeof(*c)); |
| 417 |
if (c == NULL) { |
| 418 |
ret = ENOMEM; |
| 419 |
goto error; |
| 420 |
} |
| 421 |
c->release = mach_release; |
| 422 |
c->userctx = user; |
| 423 |
c->callback = callback; |
| 424 |
|
| 425 |
ret = mach_init(service, sport, c); |
| 426 |
if (ret) |
| 427 |
goto error; |
| 428 |
|
| 429 |
*ctx = c; |
| 430 |
return 0; |
| 431 |
error: |
| 432 |
if (c) |
| 433 |
free(c); |
| 434 |
if (sport != MACH_PORT_NULL) |
| 435 |
mach_port_mod_refs(mach_task_self(), sport, |
| 436 |
MACH_PORT_RIGHT_RECEIVE, -1); |
| 437 |
return ret; |
| 438 |
#else /* !(__APPLE__ && HAVE_GCD) */ |
| 439 |
*ctx = NULL; |
| 440 |
return EINVAL; |
| 441 |
#endif /* __APPLE__ && HAVE_GCD */ |
| 442 |
} |
| 443 |
|
| 444 |
struct client { |
| 445 |
int fd; |
| 446 |
heim_ipc_callback callback; |
| 447 |
void *userctx; |
| 448 |
int flags; |
| 449 |
#define LISTEN_SOCKET 1 |
| 450 |
#define WAITING_READ 2 |
| 451 |
#define WAITING_WRITE 4 |
| 452 |
#define WAITING_CLOSE 8 |
| 453 |
|
| 454 |
#define HTTP_REPLY 16 |
| 455 |
|
| 456 |
#define INHERIT_MASK 0xffff0000 |
| 457 |
#define INCLUDE_ERROR_CODE (1 << 16) |
| 458 |
#define ALLOW_HTTP (1<<17) |
| 459 |
#define UNIX_SOCKET (1<<18) |
| 460 |
unsigned calls; |
| 461 |
size_t ptr, len; |
| 462 |
uint8_t *inmsg; |
| 463 |
size_t olen; |
| 464 |
uint8_t *outmsg; |
| 465 |
#ifdef HAVE_GCD |
| 466 |
dispatch_source_t in; |
| 467 |
dispatch_source_t out; |
| 468 |
#endif |
| 469 |
struct { |
| 470 |
uid_t uid; |
| 471 |
gid_t gid; |
| 472 |
pid_t pid; |
| 473 |
} unixrights; |
| 474 |
}; |
| 475 |
|
| 476 |
#ifndef HAVE_GCD |
| 477 |
static unsigned num_clients = 0; |
| 478 |
static struct client **clients = NULL; |
| 479 |
#endif |
| 480 |
|
| 481 |
static void handle_read(struct client *); |
| 482 |
static void handle_write(struct client *); |
| 483 |
static int maybe_close(struct client *); |
| 484 |
|
| 485 |
/* |
| 486 |
* Update peer credentials from socket. |
| 487 |
* |
| 488 |
* SCM_CREDS can only be updated the first time there is read data to |
| 489 |
* read from the filedescriptor, so if we read do it before this |
| 490 |
* point, the cred data might not be is not there yet. |
| 491 |
*/ |
| 492 |
|
| 493 |
static int |
| 494 |
update_client_creds(struct client *c) |
| 495 |
{ |
| 496 |
#ifdef HAVE_GETPEERUCRED |
| 497 |
/* Solaris 10 */ |
| 498 |
{ |
| 499 |
ucred_t *peercred; |
| 500 |
|
| 501 |
if (getpeerucred(c->fd, &peercred) != 0) { |
| 502 |
c->unixrights.uid = ucred_geteuid(peercred); |
| 503 |
c->unixrights.gid = ucred_getegid(peercred); |
| 504 |
c->unixrights.pid = 0; |
| 505 |
ucred_free(peercred); |
| 506 |
return 1; |
| 507 |
} |
| 508 |
} |
| 509 |
#endif |
| 510 |
#ifdef HAVE_GETPEEREID |
| 511 |
/* FreeBSD, OpenBSD */ |
| 512 |
{ |
| 513 |
uid_t uid; |
| 514 |
gid_t gid; |
| 515 |
|
| 516 |
if (getpeereid(c->fd, &uid, &gid) == 0) { |
| 517 |
c->unixrights.uid = uid; |
| 518 |
c->unixrights.gid = gid; |
| 519 |
c->unixrights.pid = 0; |
| 520 |
return 1; |
| 521 |
} |
| 522 |
} |
| 523 |
#endif |
| 524 |
#if defined(SO_PEERCRED) && defined(__linux__) |
| 525 |
/* Linux */ |
| 526 |
{ |
| 527 |
struct ucred pc; |
| 528 |
socklen_t pclen = sizeof(pc); |
| 529 |
|
| 530 |
if (getsockopt(c->fd, SOL_SOCKET, SO_PEERCRED, (void *)&pc, &pclen) == 0) { |
| 531 |
c->unixrights.uid = pc.uid; |
| 532 |
c->unixrights.gid = pc.gid; |
| 533 |
c->unixrights.pid = pc.pid; |
| 534 |
return 1; |
| 535 |
} |
| 536 |
} |
| 537 |
#endif |
| 538 |
#if defined(LOCAL_PEERCRED) && defined(XUCRED_VERSION) |
| 539 |
{ |
| 540 |
struct xucred peercred; |
| 541 |
socklen_t peercredlen = sizeof(peercred); |
| 542 |
|
| 543 |
if (getsockopt(c->fd, LOCAL_PEERCRED, 1, |
| 544 |
(void *)&peercred, &peercredlen) == 0 |
| 545 |
&& peercred.cr_version == XUCRED_VERSION) |
| 546 |
{ |
| 547 |
c->unixrights.uid = peercred.cr_uid; |
| 548 |
c->unixrights.gid = peercred.cr_gid; |
| 549 |
c->unixrights.pid = 0; |
| 550 |
return 1; |
| 551 |
} |
| 552 |
} |
| 553 |
#endif |
| 554 |
#if defined(SOCKCREDSIZE) && defined(SCM_CREDS) |
| 555 |
/* NetBSD */ |
| 556 |
if (c->unixrights.uid == (uid_t)-1) { |
| 557 |
struct msghdr msg; |
| 558 |
socklen_t crmsgsize; |
| 559 |
void *crmsg; |
| 560 |
struct cmsghdr *cmp; |
| 561 |
struct sockcred *sc; |
| 562 |
|
| 563 |
memset(&msg, 0, sizeof(msg)); |
| 564 |
crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS)); |
| 565 |
if (crmsgsize == 0) |
| 566 |
return 1 ; |
| 567 |
|
| 568 |
crmsg = malloc(crmsgsize); |
| 569 |
if (crmsg == NULL) |
| 570 |
goto failed_scm_creds; |
| 571 |
|
| 572 |
memset(crmsg, 0, crmsgsize); |
| 573 |
|
| 574 |
msg.msg_control = crmsg; |
| 575 |
msg.msg_controllen = crmsgsize; |
| 576 |
|
| 577 |
if (recvmsg(c->fd, &msg, 0) < 0) { |
| 578 |
free(crmsg); |
| 579 |
goto failed_scm_creds; |
| 580 |
} |
| 581 |
|
| 582 |
if (msg.msg_controllen == 0 || (msg.msg_flags & MSG_CTRUNC) != 0) { |
| 583 |
free(crmsg); |
| 584 |
goto failed_scm_creds; |
| 585 |
} |
| 586 |
|
| 587 |
cmp = CMSG_FIRSTHDR(&msg); |
| 588 |
if (cmp->cmsg_level != SOL_SOCKET || cmp->cmsg_type != SCM_CREDS) { |
| 589 |
free(crmsg); |
| 590 |
goto failed_scm_creds; |
| 591 |
} |
| 592 |
|
| 593 |
sc = (struct sockcred *)(void *)CMSG_DATA(cmp); |
| 594 |
|
| 595 |
c->unixrights.uid = sc->sc_euid; |
| 596 |
c->unixrights.gid = sc->sc_egid; |
| 597 |
c->unixrights.pid = 0; |
| 598 |
|
| 599 |
free(crmsg); |
| 600 |
return 1; |
| 601 |
} else { |
| 602 |
/* we already got the cred, just return it */ |
| 603 |
return 1; |
| 604 |
} |
| 605 |
failed_scm_creds: |
| 606 |
#endif |
| 607 |
return 0; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
static struct client * |
| 612 |
add_new_socket(int fd, |
| 613 |
int flags, |
| 614 |
heim_ipc_callback callback, |
| 615 |
void *userctx) |
| 616 |
{ |
| 617 |
struct client *c; |
| 618 |
int fileflags; |
| 619 |
|
| 620 |
c = calloc(1, sizeof(*c)); |
| 621 |
if (c == NULL) |
| 622 |
return NULL; |
| 623 |
|
| 624 |
if (flags & LISTEN_SOCKET) { |
| 625 |
c->fd = fd; |
| 626 |
} else { |
| 627 |
c->fd = accept(fd, NULL, NULL); |
| 628 |
if(c->fd < 0) { |
| 629 |
free(c); |
| 630 |
return NULL; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
c->flags = flags; |
| 635 |
c->callback = callback; |
| 636 |
c->userctx = userctx; |
| 637 |
|
| 638 |
fileflags = fcntl(c->fd, F_GETFL, 0); |
| 639 |
fcntl(c->fd, F_SETFL, fileflags | O_NONBLOCK); |
| 640 |
|
| 641 |
#ifdef HAVE_GCD |
| 642 |
init_globals(); |
| 643 |
|
| 644 |
c->in = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, |
| 645 |
c->fd, 0, eventq); |
| 646 |
c->out = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE, |
| 647 |
c->fd, 0, eventq); |
| 648 |
|
| 649 |
dispatch_source_set_event_handler(c->in, ^{ |
| 650 |
int rw = (c->flags & WAITING_WRITE); |
| 651 |
handle_read(c); |
| 652 |
if (rw == 0 && (c->flags & WAITING_WRITE)) |
| 653 |
dispatch_resume(c->out); |
| 654 |
if ((c->flags & WAITING_READ) == 0) |
| 655 |
dispatch_suspend(c->in); |
| 656 |
maybe_close(c); |
| 657 |
}); |
| 658 |
dispatch_source_set_event_handler(c->out, ^{ |
| 659 |
handle_write(c); |
| 660 |
if ((c->flags & WAITING_WRITE) == 0) { |
| 661 |
dispatch_suspend(c->out); |
| 662 |
} |
| 663 |
maybe_close(c); |
| 664 |
}); |
| 665 |
|
| 666 |
dispatch_resume(c->in); |
| 667 |
#else |
| 668 |
clients = erealloc(clients, sizeof(clients[0]) * (num_clients + 1)); |
| 669 |
clients[num_clients] = c; |
| 670 |
num_clients++; |
| 671 |
#endif |
| 672 |
|
| 673 |
return c; |
| 674 |
} |
| 675 |
|
| 676 |
static int |
| 677 |
maybe_close(struct client *c) |
| 678 |
{ |
| 679 |
if (c->calls != 0) |
| 680 |
return 0; |
| 681 |
if (c->flags & (WAITING_READ|WAITING_WRITE)) |
| 682 |
return 0; |
| 683 |
|
| 684 |
#ifdef HAVE_GCD |
| 685 |
dispatch_source_cancel(c->in); |
| 686 |
if ((c->flags & WAITING_READ) == 0) |
| 687 |
dispatch_resume(c->in); |
| 688 |
dispatch_release(c->in); |
| 689 |
|
| 690 |
dispatch_source_cancel(c->out); |
| 691 |
if ((c->flags & WAITING_WRITE) == 0) |
| 692 |
dispatch_resume(c->out); |
| 693 |
dispatch_release(c->out); |
| 694 |
#endif |
| 695 |
close(c->fd); /* ref count fd close */ |
| 696 |
free(c); |
| 697 |
return 1; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
struct socket_call { |
| 702 |
heim_idata in; |
| 703 |
struct client *c; |
| 704 |
heim_icred cred; |
| 705 |
}; |
| 706 |
|
| 707 |
static void |
| 708 |
output_data(struct client *c, const void *data, size_t len) |
| 709 |
{ |
| 710 |
if (c->olen + len < c->olen) |
| 711 |
abort(); |
| 712 |
c->outmsg = erealloc(c->outmsg, c->olen + len); |
| 713 |
memcpy(&c->outmsg[c->olen], data, len); |
| 714 |
c->olen += len; |
| 715 |
c->flags |= WAITING_WRITE; |
| 716 |
} |
| 717 |
|
| 718 |
static void |
| 719 |
socket_complete(heim_sipc_call ctx, int returnvalue, heim_idata *reply) |
| 720 |
{ |
| 721 |
struct socket_call *sc = (struct socket_call *)ctx; |
| 722 |
struct client *c = sc->c; |
| 723 |
|
| 724 |
/* double complete ? */ |
| 725 |
if (c == NULL) |
| 726 |
abort(); |
| 727 |
|
| 728 |
if ((c->flags & WAITING_CLOSE) == 0) { |
| 729 |
uint32_t u32; |
| 730 |
|
| 731 |
/* length */ |
| 732 |
u32 = htonl(reply->length); |
| 733 |
output_data(c, &u32, sizeof(u32)); |
| 734 |
|
| 735 |
/* return value */ |
| 736 |
if (c->flags & INCLUDE_ERROR_CODE) { |
| 737 |
u32 = htonl(returnvalue); |
| 738 |
output_data(c, &u32, sizeof(u32)); |
| 739 |
} |
| 740 |
|
| 741 |
/* data */ |
| 742 |
output_data(c, reply->data, reply->length); |
| 743 |
|
| 744 |
/* if HTTP, close connection */ |
| 745 |
if (c->flags & HTTP_REPLY) { |
| 746 |
c->flags |= WAITING_CLOSE; |
| 747 |
c->flags &= ~WAITING_READ; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
c->calls--; |
| 752 |
if (sc->cred) |
| 753 |
heim_ipc_free_cred(sc->cred); |
| 754 |
free(sc->in.data); |
| 755 |
sc->c = NULL; /* so we can catch double complete */ |
| 756 |
free(sc); |
| 757 |
|
| 758 |
maybe_close(c); |
| 759 |
} |
| 760 |
|
| 761 |
/* remove HTTP %-quoting from buf */ |
| 762 |
static int |
| 763 |
de_http(char *buf) |
| 764 |
{ |
| 765 |
unsigned char *p, *q; |
| 766 |
for(p = q = (unsigned char *)buf; *p; p++, q++) { |
| 767 |
if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) { |
| 768 |
unsigned int x; |
| 769 |
if(sscanf((char *)p + 1, "%2x", &x) != 1) |
| 770 |
return -1; |
| 771 |
*q = x; |
| 772 |
p += 2; |
| 773 |
} else |
| 774 |
*q = *p; |
| 775 |
} |
| 776 |
*q = '\0'; |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
static struct socket_call * |
| 781 |
handle_http_tcp(struct client *c) |
| 782 |
{ |
| 783 |
struct socket_call *cs; |
| 784 |
char *s, *p, *t; |
| 785 |
void *data; |
| 786 |
char *proto; |
| 787 |
int len; |
| 788 |
|
| 789 |
s = (char *)c->inmsg; |
| 790 |
|
| 791 |
p = strstr(s, "\r\n"); |
| 792 |
if (p == NULL) |
| 793 |
return NULL; |
| 794 |
|
| 795 |
*p = 0; |
| 796 |
|
| 797 |
p = NULL; |
| 798 |
t = strtok_r(s, " \t", &p); |
| 799 |
if (t == NULL) |
| 800 |
return NULL; |
| 801 |
|
| 802 |
t = strtok_r(NULL, " \t", &p); |
| 803 |
if (t == NULL) |
| 804 |
return NULL; |
| 805 |
|
| 806 |
data = malloc(strlen(t)); |
| 807 |
if (data == NULL) |
| 808 |
return NULL; |
| 809 |
|
| 810 |
if(*t == '/') |
| 811 |
t++; |
| 812 |
if(de_http(t) != 0) { |
| 813 |
free(data); |
| 814 |
return NULL; |
| 815 |
} |
| 816 |
proto = strtok_r(NULL, " \t", &p); |
| 817 |
if (proto == NULL) { |
| 818 |
free(data); |
| 819 |
return NULL; |
| 820 |
} |
| 821 |
len = rk_base64_decode(t, data); |
| 822 |
if(len <= 0){ |
| 823 |
const char *msg = |
| 824 |
" 404 Not found\r\n" |
| 825 |
"Server: Heimdal/" VERSION "\r\n" |
| 826 |
"Cache-Control: no-cache\r\n" |
| 827 |
"Pragma: no-cache\r\n" |
| 828 |
"Content-type: text/html\r\n" |
| 829 |
"Content-transfer-encoding: 8bit\r\n\r\n" |
| 830 |
"<TITLE>404 Not found</TITLE>\r\n" |
| 831 |
"<H1>404 Not found</H1>\r\n" |
| 832 |
"That page doesn't exist, maybe you are looking for " |
| 833 |
"<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n"; |
| 834 |
free(data); |
| 835 |
output_data(c, proto, strlen(proto)); |
| 836 |
output_data(c, msg, strlen(msg)); |
| 837 |
return NULL; |
| 838 |
} |
| 839 |
|
| 840 |
cs = emalloc(sizeof(*cs)); |
| 841 |
cs->c = c; |
| 842 |
cs->in.data = data; |
| 843 |
cs->in.length = len; |
| 844 |
c->ptr = 0; |
| 845 |
|
| 846 |
{ |
| 847 |
const char *msg = |
| 848 |
" 200 OK\r\n" |
| 849 |
"Server: Heimdal/" VERSION "\r\n" |
| 850 |
"Cache-Control: no-cache\r\n" |
| 851 |
"Pragma: no-cache\r\n" |
| 852 |
"Content-type: application/octet-stream\r\n" |
| 853 |
"Content-transfer-encoding: binary\r\n\r\n"; |
| 854 |
output_data(c, proto, strlen(proto)); |
| 855 |
output_data(c, msg, strlen(msg)); |
| 856 |
} |
| 857 |
|
| 858 |
return cs; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
static void |
| 863 |
handle_read(struct client *c) |
| 864 |
{ |
| 865 |
ssize_t len; |
| 866 |
uint32_t dlen; |
| 867 |
|
| 868 |
if (c->flags & LISTEN_SOCKET) { |
| 869 |
add_new_socket(c->fd, |
| 870 |
WAITING_READ | (c->flags & INHERIT_MASK), |
| 871 |
c->callback, |
| 872 |
c->userctx); |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
if (c->ptr - c->len < 1024) { |
| 877 |
c->inmsg = erealloc(c->inmsg, |
| 878 |
c->len + 1024); |
| 879 |
c->len += 1024; |
| 880 |
} |
| 881 |
|
| 882 |
len = read(c->fd, c->inmsg + c->ptr, c->len - c->ptr); |
| 883 |
if (len <= 0) { |
| 884 |
c->flags |= WAITING_CLOSE; |
| 885 |
c->flags &= ~WAITING_READ; |
| 886 |
return; |
| 887 |
} |
| 888 |
c->ptr += len; |
| 889 |
if (c->ptr > c->len) |
| 890 |
abort(); |
| 891 |
|
| 892 |
while (c->ptr >= sizeof(dlen)) { |
| 893 |
struct socket_call *cs; |
| 894 |
|
| 895 |
if((c->flags & ALLOW_HTTP) && c->ptr >= 4 && |
| 896 |
strncmp((char *)c->inmsg, "GET ", 4) == 0 && |
| 897 |
strncmp((char *)c->inmsg + c->ptr - 4, "\r\n\r\n", 4) == 0) { |
| 898 |
|
| 899 |
/* remove the trailing \r\n\r\n so the string is NUL terminated */ |
| 900 |
c->inmsg[c->ptr - 4] = '\0'; |
| 901 |
|
| 902 |
c->flags |= HTTP_REPLY; |
| 903 |
|
| 904 |
cs = handle_http_tcp(c); |
| 905 |
if (cs == NULL) { |
| 906 |
c->flags |= WAITING_CLOSE; |
| 907 |
c->flags &= ~WAITING_READ; |
| 908 |
break; |
| 909 |
} |
| 910 |
} else { |
| 911 |
memcpy(&dlen, c->inmsg, sizeof(dlen)); |
| 912 |
dlen = ntohl(dlen); |
| 913 |
|
| 914 |
if (dlen > MAX_PACKET_SIZE) { |
| 915 |
c->flags |= WAITING_CLOSE; |
| 916 |
c->flags &= ~WAITING_READ; |
| 917 |
return; |
| 918 |
} |
| 919 |
if (dlen > c->ptr - sizeof(dlen)) { |
| 920 |
break; |
| 921 |
} |
| 922 |
|
| 923 |
cs = emalloc(sizeof(*cs)); |
| 924 |
cs->c = c; |
| 925 |
cs->in.data = emalloc(dlen); |
| 926 |
memcpy(cs->in.data, c->inmsg + sizeof(dlen), dlen); |
| 927 |
cs->in.length = dlen; |
| 928 |
|
| 929 |
c->ptr -= sizeof(dlen) + dlen; |
| 930 |
memmove(c->inmsg, |
| 931 |
c->inmsg + sizeof(dlen) + dlen, |
| 932 |
c->ptr); |
| 933 |
} |
| 934 |
|
| 935 |
c->calls++; |
| 936 |
|
| 937 |
if ((c->flags & UNIX_SOCKET) != 0) { |
| 938 |
if (update_client_creds(c)) |
| 939 |
_heim_ipc_create_cred(c->unixrights.uid, c->unixrights.gid, |
| 940 |
c->unixrights.pid, -1, &cs->cred); |
| 941 |
} |
| 942 |
|
| 943 |
c->callback(c->userctx, &cs->in, |
| 944 |
cs->cred, socket_complete, |
| 945 |
(heim_sipc_call)cs); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
static void |
| 950 |
handle_write(struct client *c) |
| 951 |
{ |
| 952 |
ssize_t len; |
| 953 |
|
| 954 |
len = write(c->fd, c->outmsg, c->olen); |
| 955 |
if (len <= 0) { |
| 956 |
c->flags |= WAITING_CLOSE; |
| 957 |
c->flags &= ~(WAITING_WRITE); |
| 958 |
} else if (c->olen != (size_t)len) { |
| 959 |
memmove(&c->outmsg[0], &c->outmsg[len], c->olen - len); |
| 960 |
c->olen -= len; |
| 961 |
} else { |
| 962 |
c->olen = 0; |
| 963 |
free(c->outmsg); |
| 964 |
c->outmsg = NULL; |
| 965 |
c->flags &= ~(WAITING_WRITE); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
#ifndef HAVE_GCD |
| 971 |
|
| 972 |
static void |
| 973 |
process_loop(void) |
| 974 |
{ |
| 975 |
struct pollfd *fds; |
| 976 |
unsigned n; |
| 977 |
unsigned num_fds; |
| 978 |
|
| 979 |
while (num_clients > 0) { |
| 980 |
|
| 981 |
fds = malloc(num_clients * sizeof(fds[0])); |
| 982 |
if(fds == NULL) |
| 983 |
abort(); |
| 984 |
|
| 985 |
num_fds = num_clients; |
| 986 |
|
| 987 |
for (n = 0 ; n < num_fds; n++) { |
| 988 |
fds[n].fd = clients[n]->fd; |
| 989 |
fds[n].events = 0; |
| 990 |
if (clients[n]->flags & WAITING_READ) |
| 991 |
fds[n].events |= POLLIN; |
| 992 |
if (clients[n]->flags & WAITING_WRITE) |
| 993 |
fds[n].events |= POLLOUT; |
| 994 |
|
| 995 |
fds[n].revents = 0; |
| 996 |
} |
| 997 |
|
| 998 |
while (poll(fds, num_fds, -1) == -1) { |
| 999 |
if (errno == EINTR || errno == EAGAIN) |
| 1000 |
continue; |
| 1001 |
err(1, "poll(2) failed"); |
| 1002 |
} |
| 1003 |
|
| 1004 |
for (n = 0 ; n < num_fds; n++) { |
| 1005 |
if (clients[n] == NULL) |
| 1006 |
continue; |
| 1007 |
if (fds[n].revents & POLLERR) { |
| 1008 |
clients[n]->flags |= WAITING_CLOSE; |
| 1009 |
continue; |
| 1010 |
} |
| 1011 |
|
| 1012 |
if (fds[n].revents & POLLIN) |
| 1013 |
handle_read(clients[n]); |
| 1014 |
if (fds[n].revents & POLLOUT) |
| 1015 |
handle_write(clients[n]); |
| 1016 |
} |
| 1017 |
|
| 1018 |
n = 0; |
| 1019 |
while (n < num_clients) { |
| 1020 |
struct client *c = clients[n]; |
| 1021 |
if (maybe_close(c)) { |
| 1022 |
if (n < num_clients - 1) |
| 1023 |
clients[n] = clients[num_clients - 1]; |
| 1024 |
num_clients--; |
| 1025 |
} else |
| 1026 |
n++; |
| 1027 |
} |
| 1028 |
|
| 1029 |
free(fds); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
#endif |
| 1034 |
|
| 1035 |
static int |
| 1036 |
socket_release(heim_sipc ctx) |
| 1037 |
{ |
| 1038 |
struct client *c = ctx->mech; |
| 1039 |
c->flags |= WAITING_CLOSE; |
| 1040 |
return 0; |
| 1041 |
} |
| 1042 |
|
| 1043 |
int |
| 1044 |
heim_sipc_stream_listener(int fd, int type, |
| 1045 |
heim_ipc_callback callback, |
| 1046 |
void *user, heim_sipc *ctx) |
| 1047 |
{ |
| 1048 |
heim_sipc ct = calloc(1, sizeof(*ct)); |
| 1049 |
struct client *c; |
| 1050 |
|
| 1051 |
if ((type & HEIM_SIPC_TYPE_IPC) && (type & (HEIM_SIPC_TYPE_UINT32|HEIM_SIPC_TYPE_HTTP))) |
| 1052 |
return EINVAL; |
| 1053 |
|
| 1054 |
switch (type) { |
| 1055 |
case HEIM_SIPC_TYPE_IPC: |
| 1056 |
c = add_new_socket(fd, LISTEN_SOCKET|WAITING_READ|INCLUDE_ERROR_CODE, callback, user); |
| 1057 |
break; |
| 1058 |
case HEIM_SIPC_TYPE_UINT32: |
| 1059 |
c = add_new_socket(fd, LISTEN_SOCKET|WAITING_READ, callback, user); |
| 1060 |
break; |
| 1061 |
case HEIM_SIPC_TYPE_HTTP: |
| 1062 |
case HEIM_SIPC_TYPE_UINT32|HEIM_SIPC_TYPE_HTTP: |
| 1063 |
c = add_new_socket(fd, LISTEN_SOCKET|WAITING_READ|ALLOW_HTTP, callback, user); |
| 1064 |
break; |
| 1065 |
default: |
| 1066 |
free(ct); |
| 1067 |
return EINVAL; |
| 1068 |
} |
| 1069 |
|
| 1070 |
ct->mech = c; |
| 1071 |
ct->release = socket_release; |
| 1072 |
|
| 1073 |
c->unixrights.uid = (uid_t) -1; |
| 1074 |
c->unixrights.gid = (gid_t) -1; |
| 1075 |
c->unixrights.pid = (pid_t) 0; |
| 1076 |
|
| 1077 |
*ctx = ct; |
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
|
| 1081 |
int |
| 1082 |
heim_sipc_service_unix(const char *service, |
| 1083 |
heim_ipc_callback callback, |
| 1084 |
void *user, heim_sipc *ctx) |
| 1085 |
{ |
| 1086 |
struct sockaddr_un un; |
| 1087 |
int fd, ret; |
| 1088 |
|
| 1089 |
un.sun_family = AF_UNIX; |
| 1090 |
|
| 1091 |
snprintf(un.sun_path, sizeof(un.sun_path), |
| 1092 |
"/var/run/.heim_%s-socket", service); |
| 1093 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1094 |
if (fd < 0) |
| 1095 |
return errno; |
| 1096 |
|
| 1097 |
socket_set_reuseaddr(fd, 1); |
| 1098 |
#ifdef LOCAL_CREDS |
| 1099 |
{ |
| 1100 |
int one = 1; |
| 1101 |
setsockopt(fd, 0, LOCAL_CREDS, (void *)&one, sizeof(one)); |
| 1102 |
} |
| 1103 |
#endif |
| 1104 |
|
| 1105 |
unlink(un.sun_path); |
| 1106 |
|
| 1107 |
if (bind(fd, (struct sockaddr *)&un, sizeof(un)) < 0) { |
| 1108 |
close(fd); |
| 1109 |
return errno; |
| 1110 |
} |
| 1111 |
|
| 1112 |
if (listen(fd, SOMAXCONN) < 0) { |
| 1113 |
close(fd); |
| 1114 |
return errno; |
| 1115 |
} |
| 1116 |
|
| 1117 |
chmod(un.sun_path, 0666); |
| 1118 |
|
| 1119 |
ret = heim_sipc_stream_listener(fd, HEIM_SIPC_TYPE_IPC, |
| 1120 |
callback, user, ctx); |
| 1121 |
if (ret == 0) { |
| 1122 |
struct client *c = (*ctx)->mech; |
| 1123 |
c->flags |= UNIX_SOCKET; |
| 1124 |
} |
| 1125 |
|
| 1126 |
return ret; |
| 1127 |
} |
| 1128 |
|
| 1129 |
/** |
| 1130 |
* Set the idle timeout value |
| 1131 |
|
| 1132 |
* The timeout event handler is triggered recurrently every idle |
| 1133 |
* period `t'. The default action is rather draconian and just calls |
| 1134 |
* exit(0), so you might want to change this to something more |
| 1135 |
* graceful using heim_sipc_set_timeout_handler(). |
| 1136 |
*/ |
| 1137 |
|
| 1138 |
void |
| 1139 |
heim_sipc_timeout(time_t t) |
| 1140 |
{ |
| 1141 |
#ifdef HAVE_GCD |
| 1142 |
static dispatch_once_t timeoutonce; |
| 1143 |
init_globals(); |
| 1144 |
dispatch_sync(timerq, ^{ |
| 1145 |
timeoutvalue = t; |
| 1146 |
set_timer(); |
| 1147 |
}); |
| 1148 |
dispatch_once(&timeoutonce, ^{ dispatch_resume(timer); }); |
| 1149 |
#else |
| 1150 |
abort(); |
| 1151 |
#endif |
| 1152 |
} |
| 1153 |
|
| 1154 |
/** |
| 1155 |
* Set the timeout event handler |
| 1156 |
* |
| 1157 |
* Replaces the default idle timeout action. |
| 1158 |
*/ |
| 1159 |
|
| 1160 |
void |
| 1161 |
heim_sipc_set_timeout_handler(void (*func)(void)) |
| 1162 |
{ |
| 1163 |
#ifdef HAVE_GCD |
| 1164 |
init_globals(); |
| 1165 |
dispatch_sync(timerq, ^{ timer_ev = func; }); |
| 1166 |
#else |
| 1167 |
abort(); |
| 1168 |
#endif |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
void |
| 1173 |
heim_sipc_free_context(heim_sipc ctx) |
| 1174 |
{ |
| 1175 |
(ctx->release)(ctx); |
| 1176 |
} |
| 1177 |
|
| 1178 |
void |
| 1179 |
heim_ipc_main(void) |
| 1180 |
{ |
| 1181 |
#ifdef HAVE_GCD |
| 1182 |
dispatch_main(); |
| 1183 |
#else |
| 1184 |
process_loop(); |
| 1185 |
#endif |
| 1186 |
} |