Become a daemon, discarding the controlling terminal. */
| 1511 | |
| 1512 | /* Become a daemon, discarding the controlling terminal. */ |
| 1513 | static void become_daemon(void) |
| 1514 | { |
| 1515 | int i; |
| 1516 | pid_t pid = fork(); |
| 1517 | |
| 1518 | if (pid) { |
| 1519 | if (pid < 0) { |
| 1520 | fprintf(stderr, "failed to fork: %s\n", strerror(errno)); |
| 1521 | exit_cleanup(RERR_FILEIO); |
| 1522 | } |
| 1523 | _exit(0); |
| 1524 | } |
| 1525 | |
| 1526 | create_pid_file(); |
| 1527 | |
| 1528 | /* detach from the terminal */ |
| 1529 | #ifdef HAVE_SETSID |
| 1530 | setsid(); |
| 1531 | #elif defined TIOCNOTTY |
| 1532 | i = open("/dev/tty", O_RDWR); |
| 1533 | if (i >= 0) { |
| 1534 | ioctl(i, (int)TIOCNOTTY, (char *)0); |
| 1535 | close(i); |
| 1536 | } |
| 1537 | #endif |
| 1538 | /* make sure that stdin, stdout an stderr don't stuff things |
| 1539 | * up (library functions, for example) */ |
| 1540 | for (i = 0; i < 3; i++) { |
| 1541 | close(i); |
| 1542 | open("/dev/null", O_RDWR); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | int daemon_main(void) |
| 1547 | { |
no test coverage detected