44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal/*
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * CDDL HEADER START
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal *
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * The contents of this file are subject to the terms of the
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * Common Development and Distribution License (the "License").
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * You may not use this file except in compliance with the License.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal *
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * or http://www.opensolaris.org/os/licensing.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * See the License for the specific language governing permissions
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * and limitations under the License.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal *
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * When distributing Covered Code, include this CDDL HEADER in each
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * If applicable, add the following below this CDDL HEADER, with the
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * fields enclosed by brackets "[]" replaced with your own identifying
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * information: Portions Copyright [yyyy] [name of copyright owner]
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal *
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * CDDL HEADER END
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal */
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal/*
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * Use is subject to license terms.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal */
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include "lint.h"
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include "file64.h"
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include "mtlib.h"
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include <stdio.h>
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include <stdlib.h>
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include <unistd.h>
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include <fcntl.h>
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal#include "stdiom.h"
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal/*
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * Use fork/setsid/fork to go into background and permanently remove
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * controlling terminal.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal */
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotalint
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotaldaemon(int nochdir, int noclose)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal{
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal int retv, fd;
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal /*
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * By the first fork+setsid, we disconnect from our current controlling
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * terminal and become a session group leader.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal */
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal retv = fork();
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (retv == -1)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (-1);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (retv != 0)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal _exit(EXIT_SUCCESS);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (setsid() == -1)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (-1);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal /*
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * By forking again without calling setsid again, we make certain
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * that we are not the session group leader and can never reacquire
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * a controlling terminal.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal */
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal retv = fork();
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (retv == -1)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (-1);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (retv != 0)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal _exit(EXIT_SUCCESS);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (nochdir == 0)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal (void) chdir("/");
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (noclose == 0) {
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal /*
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * Missing the PRIV_FILE_READ privilege may be one of the
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * reasons that prevent the opening of /dev/null to succeed.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal */
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if ((fd = open("/dev/null", O_RDWR)) == -1)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (-1);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal /*
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * Also, if any of the descriptor redirects fails we should
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * return with error to signal to the caller that his request
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * cannot be fulfilled properly. It is up to the caller to
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal * do the cleanup.
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal */
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if ((fd != STDIN_FILENO) && (dup2(fd, STDIN_FILENO) < 0)) {
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal (void) close(fd);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (-1);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal }
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if ((fd != STDOUT_FILENO) && (dup2(fd, STDOUT_FILENO) < 0)) {
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal (void) close(fd);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (-1);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal }
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if ((fd != STDERR_FILENO) && (dup2(fd, STDERR_FILENO) < 0)) {
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal (void) close(fd);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (-1);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal }
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal if (fd > STDERR_FILENO)
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal (void) close(fd);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal }
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal return (0);
44991a1c1bb35cccb6bf99cb6dce14864dcee19cVladimir Kotal}