- static int hand[2];
- if (control_block.background)
- {
- /* create pipe so that parent waits until child has created
- PID (or failed) */
- if (pipe(hand) < 0)
- {
- yaz_log(YLOG_FATAL|YLOG_ERRNO, "pipe");
- return 1;
- }
- switch (fork())
- {
- case 0:
- break;
- case -1:
- return 1;
- default:
- close(hand[1]);
- while(1)
- {
- char dummy[1];
- int res = read(hand[0], dummy, 1);
- if (res < 0 && yaz_errno() != EINTR)
- {
- yaz_log(YLOG_FATAL|YLOG_ERRNO, "read fork handshake");
- break;
- }
- else if (res >= 0)
- break;
- }
- close(hand[0]);
- _exit(0);
- }
- /* child */
- close(hand[0]);
- if (setsid() < 0)
- return 1;
-
- close(0);
- close(1);
- close(2);
- open("/dev/null", O_RDWR);
- dup(0); dup(0);
- }