- pid_t pid;
-
- /* Get parent pid of this process. */
- child_ppid = getppid();
-
- pid = fork();
- if (pid < 0) {
- PERROR("fork");
- goto error;
- } else if (pid == 0) {
- int fd;
- pid_t sid;
-
- /* Child */
-
- /*
- * Get the newly created parent pid so we can signal that process when
- * we are ready to operate.
- */
- child_ppid = getppid();
-
- sid = setsid();
- if (sid < 0) {
- PERROR("setsid");
- goto error;
- }
-
- /* Try to change directory to /. If we can't well at least notify. */
- ret = chdir("/");
- if (ret < 0) {
- PERROR("chdir");
- }
-
- fd = open(_PATH_DEVNULL, O_RDWR, 0);
- if (fd < 0) {
- PERROR("open %s", _PATH_DEVNULL);
- /* Let 0, 1 and 2 open since we can't bind them to /dev/null. */
- } else {
- (void) dup2(fd, STDIN_FILENO);
- (void) dup2(fd, STDOUT_FILENO);
- (void) dup2(fd, STDERR_FILENO);
- if (fd > 2) {
- ret = close(fd);
- if (ret < 0) {
- PERROR("close");
- }
- }
- }
- goto end;
- } else {
- /* Parent */
-
- /*
- * Waiting for child to notify this parent that it can exit. Note that
- * sleep() is interrupted before the 1 second delay as soon as the
- * signal is received, so it will not cause visible delay for the
- * user.
- */
- while (!CMM_LOAD_SHARED(recv_child_signal)) {
- int status;
- pid_t ret;