250a073308fb9258903f57b76eeb2470c6926efeth/*
250a073308fb9258903f57b76eeb2470c6926efeth * CDDL HEADER START
250a073308fb9258903f57b76eeb2470c6926efeth *
250a073308fb9258903f57b76eeb2470c6926efeth * The contents of this file are subject to the terms of the
250a073308fb9258903f57b76eeb2470c6926efeth * Common Development and Distribution License (the "License").
250a073308fb9258903f57b76eeb2470c6926efeth * You may not use this file except in compliance with the License.
250a073308fb9258903f57b76eeb2470c6926efeth *
250a073308fb9258903f57b76eeb2470c6926efeth * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
250a073308fb9258903f57b76eeb2470c6926efeth * or http://www.opensolaris.org/os/licensing.
250a073308fb9258903f57b76eeb2470c6926efeth * See the License for the specific language governing permissions
250a073308fb9258903f57b76eeb2470c6926efeth * and limitations under the License.
250a073308fb9258903f57b76eeb2470c6926efeth *
250a073308fb9258903f57b76eeb2470c6926efeth * When distributing Covered Code, include this CDDL HEADER in each
250a073308fb9258903f57b76eeb2470c6926efeth * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
250a073308fb9258903f57b76eeb2470c6926efeth * If applicable, add the following below this CDDL HEADER, with the
250a073308fb9258903f57b76eeb2470c6926efeth * fields enclosed by brackets "[]" replaced with your own identifying
250a073308fb9258903f57b76eeb2470c6926efeth * information: Portions Copyright [yyyy] [name of copyright owner]
250a073308fb9258903f57b76eeb2470c6926efeth *
250a073308fb9258903f57b76eeb2470c6926efeth * CDDL HEADER END
250a073308fb9258903f57b76eeb2470c6926efeth */
250a073308fb9258903f57b76eeb2470c6926efeth/*
250a073308fb9258903f57b76eeb2470c6926efeth * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
250a073308fb9258903f57b76eeb2470c6926efeth * Use is subject to license terms.
250a073308fb9258903f57b76eeb2470c6926efeth */
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth/* LINTLIBRARY */
250a073308fb9258903f57b76eeb2470c6926efeth/* PROTOLIB1 */
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth#pragma ident "%Z%%M% %I% %E% SMI"
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth/* NFS server */
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth#include <sys/param.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <sys/types.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <errno.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <stdio.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <stdio_ext.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <stdlib.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <signal.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <unistd.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <sys/wait.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <sys/stat.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <fcntl.h>
250a073308fb9258903f57b76eeb2470c6926efeth#include <libscf.h>
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth/*
250a073308fb9258903f57b76eeb2470c6926efeth * The parent never returns from this function. It sits
250a073308fb9258903f57b76eeb2470c6926efeth * and waits for the child to send status on whether it
250a073308fb9258903f57b76eeb2470c6926efeth * loaded or not.
250a073308fb9258903f57b76eeb2470c6926efeth *
250a073308fb9258903f57b76eeb2470c6926efeth * We do not close down the standard file descriptors until
250a073308fb9258903f57b76eeb2470c6926efeth * we know the child is going to run.
250a073308fb9258903f57b76eeb2470c6926efeth */
250a073308fb9258903f57b76eeb2470c6926efethint
250a073308fb9258903f57b76eeb2470c6926efethdaemonize_init(void)
250a073308fb9258903f57b76eeb2470c6926efeth{
250a073308fb9258903f57b76eeb2470c6926efeth int status, pfds[2];
250a073308fb9258903f57b76eeb2470c6926efeth sigset_t set, oset;
250a073308fb9258903f57b76eeb2470c6926efeth pid_t pid;
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth /*
250a073308fb9258903f57b76eeb2470c6926efeth * Block all signals prior to the fork and leave them blocked in the
250a073308fb9258903f57b76eeb2470c6926efeth * parent so we don't get in a situation where the parent gets SIGINT
250a073308fb9258903f57b76eeb2470c6926efeth * and returns non-zero exit status and the child is actually running.
250a073308fb9258903f57b76eeb2470c6926efeth * In the child, restore the signal mask once we've done our setsid().
250a073308fb9258903f57b76eeb2470c6926efeth */
250a073308fb9258903f57b76eeb2470c6926efeth (void) sigfillset(&set);
250a073308fb9258903f57b76eeb2470c6926efeth (void) sigdelset(&set, SIGABRT);
250a073308fb9258903f57b76eeb2470c6926efeth (void) sigprocmask(SIG_BLOCK, &set, &oset);
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth /*
250a073308fb9258903f57b76eeb2470c6926efeth * We need to do this before we open the pipe - it makes things
250a073308fb9258903f57b76eeb2470c6926efeth * easier in the long run.
250a073308fb9258903f57b76eeb2470c6926efeth */
250a073308fb9258903f57b76eeb2470c6926efeth closefrom(STDERR_FILENO + 1);
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth if (pipe(pfds) == -1) {
250a073308fb9258903f57b76eeb2470c6926efeth fprintf(stderr, "failed to create pipe for daemonize");
250a073308fb9258903f57b76eeb2470c6926efeth exit(SMF_EXIT_ERR_FATAL);
250a073308fb9258903f57b76eeb2470c6926efeth }
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth if ((pid = fork()) == -1) {
250a073308fb9258903f57b76eeb2470c6926efeth fprintf(stderr, "failed to fork into background");
250a073308fb9258903f57b76eeb2470c6926efeth exit(SMF_EXIT_ERR_FATAL);
250a073308fb9258903f57b76eeb2470c6926efeth }
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth if (pid != 0) {
250a073308fb9258903f57b76eeb2470c6926efeth (void) close(pfds[1]);
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
250a073308fb9258903f57b76eeb2470c6926efeth exit(status);
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
250a073308fb9258903f57b76eeb2470c6926efeth exit(WEXITSTATUS(status));
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth exit(SMF_EXIT_ERR_FATAL);
250a073308fb9258903f57b76eeb2470c6926efeth }
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth /*
250a073308fb9258903f57b76eeb2470c6926efeth * All child from here on...
250a073308fb9258903f57b76eeb2470c6926efeth */
250a073308fb9258903f57b76eeb2470c6926efeth (void) setsid();
250a073308fb9258903f57b76eeb2470c6926efeth (void) sigprocmask(SIG_SETMASK, &oset, NULL);
250a073308fb9258903f57b76eeb2470c6926efeth (void) close(pfds[0]);
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth return (pfds[1]);
250a073308fb9258903f57b76eeb2470c6926efeth}
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth/*
250a073308fb9258903f57b76eeb2470c6926efeth * We are only a daemon if the file descriptor is valid.
250a073308fb9258903f57b76eeb2470c6926efeth */
250a073308fb9258903f57b76eeb2470c6926efethvoid
250a073308fb9258903f57b76eeb2470c6926efethdaemonize_fini(int fd)
250a073308fb9258903f57b76eeb2470c6926efeth{
250a073308fb9258903f57b76eeb2470c6926efeth int status = 0;
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth if (fd != -1) {
250a073308fb9258903f57b76eeb2470c6926efeth (void) write(fd, &status, sizeof (status));
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth (void) close(fd);
250a073308fb9258903f57b76eeb2470c6926efeth
250a073308fb9258903f57b76eeb2470c6926efeth if ((fd = open("/dev/null", O_RDWR)) >= 0) {
250a073308fb9258903f57b76eeb2470c6926efeth (void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
250a073308fb9258903f57b76eeb2470c6926efeth (void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
250a073308fb9258903f57b76eeb2470c6926efeth (void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
250a073308fb9258903f57b76eeb2470c6926efeth (void) close(fd);
250a073308fb9258903f57b76eeb2470c6926efeth }
250a073308fb9258903f57b76eeb2470c6926efeth }
250a073308fb9258903f57b76eeb2470c6926efeth}