|
Lines 386-409
stdnull(void)
Link Here
|
| 386 |
} |
386 |
} |
| 387 |
|
387 |
|
| 388 |
static void |
388 |
static void |
| 389 |
service_clean(int sock, int procfd, uint64_t flags) |
389 |
fix_fd_nums(int *fdp) |
|
|
390 |
{ |
| 391 |
int nullfd, nfd; |
| 392 |
|
| 393 |
if (*fdp > STDERR_FILENO) |
| 394 |
return; |
| 395 |
|
| 396 |
nullfd = open(_PATH_DEVNULL, O_RDWR); |
| 397 |
if (nullfd == -1) |
| 398 |
errx(1, "Unable to open %s", _PATH_DEVNULL); |
| 399 |
|
| 400 |
while (*fdp <= STDERR_FILENO) { |
| 401 |
nfd = dup(*fdp); |
| 402 |
if (nfd == -1) |
| 403 |
errx(1, "Unable to secure fd"); |
| 404 |
if (dup2(nullfd, *fdp) == -1) |
| 405 |
errx(1, "Unable to secure fd"); |
| 406 |
*fdp = nfd; |
| 407 |
} |
| 408 |
|
| 409 |
close(nullfd); |
| 410 |
} |
| 411 |
|
| 412 |
static void |
| 413 |
service_clean(int *sockp, int *procfdp, uint64_t flags) |
| 390 |
{ |
414 |
{ |
| 391 |
int fd, maxfd, minfd; |
415 |
int fd, maxfd, minfd; |
| 392 |
|
416 |
|
| 393 |
assert(sock > STDERR_FILENO); |
417 |
fix_fd_nums(sockp); |
| 394 |
assert(procfd > STDERR_FILENO); |
418 |
fix_fd_nums(procfdp); |
| 395 |
assert(sock != procfd); |
419 |
|
|
|
420 |
assert(*sockp > STDERR_FILENO); |
| 421 |
assert(*procfdp > STDERR_FILENO); |
| 422 |
assert(*sockp != *procfdp); |
| 396 |
|
423 |
|
| 397 |
if ((flags & CASPER_SERVICE_STDIO) == 0) |
424 |
if ((flags & CASPER_SERVICE_STDIO) == 0) |
| 398 |
stdnull(); |
425 |
stdnull(); |
| 399 |
|
426 |
|
| 400 |
if ((flags & CASPER_SERVICE_FD) == 0) { |
427 |
if ((flags & CASPER_SERVICE_FD) == 0) { |
| 401 |
if (procfd > sock) { |
428 |
if (*procfdp > *sockp) { |
| 402 |
maxfd = procfd; |
429 |
maxfd = *procfdp; |
| 403 |
minfd = sock; |
430 |
minfd = *sockp; |
| 404 |
} else { |
431 |
} else { |
| 405 |
maxfd = sock; |
432 |
maxfd = *sockp; |
| 406 |
minfd = procfd; |
433 |
minfd = *procfdp; |
| 407 |
} |
434 |
} |
| 408 |
|
435 |
|
| 409 |
for (fd = STDERR_FILENO + 1; fd < maxfd; fd++) { |
436 |
for (fd = STDERR_FILENO + 1; fd < maxfd; fd++) { |
|
Lines 424-430
service_start(struct service *service, int sock, int procfd)
Link Here
|
| 424 |
assert(service != NULL); |
451 |
assert(service != NULL); |
| 425 |
assert(service->s_magic == SERVICE_MAGIC); |
452 |
assert(service->s_magic == SERVICE_MAGIC); |
| 426 |
setproctitle("%s", service->s_name); |
453 |
setproctitle("%s", service->s_name); |
| 427 |
service_clean(sock, procfd, service->s_flags); |
454 |
service_clean(&sock, &procfd, service->s_flags); |
| 428 |
|
455 |
|
| 429 |
if (service_connection_add(service, sock, NULL) == NULL) |
456 |
if (service_connection_add(service, sock, NULL) == NULL) |
| 430 |
_exit(1); |
457 |
_exit(1); |