1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28#include <fcntl.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <door.h>
32#include <libintl.h>
33#include <string.h>
34#include <errno.h>
35#include <signal.h>
36#include <libscf.h>
37
38#include <cryptoutil.h>
39#include <sys/crypto/elfsign.h>
40#include "cryptoadm.h"
41
42int
43start_daemon(void)
44{
45	closefrom(0);
46	(void) open("/dev/null", O_RDONLY);
47	(void) open("/dev/null", O_WRONLY);
48	(void) dup(1);
49	(void) setsid();
50
51	return (execl(_PATH_KCFD, _PATH_KCFD, (char *)0));
52}
53
54int
55stop_daemon(void)
56{
57	int fd = -1;
58	int err = 0;
59	struct door_info dinfo;
60
61	/* read PID of kcfd process from kcfd lock file */
62	if ((fd = open(_PATH_KCFD_DOOR, O_RDONLY)) == -1) {
63		err = errno;
64		cryptodebug("Can not open %s: %s", _PATH_KCFD_DOOR,
65		    strerror(err));
66		goto stop_fail;
67	}
68
69	if (door_info(fd, &dinfo) == -1 || dinfo.di_target == -1) {
70		err = ENOENT;	/* no errno if di_target == -1 */
71		cryptodebug("no door server listening on %s", _PATH_KCFD_DOOR);
72		goto stop_fail;
73	}
74
75	cryptodebug("Sending SIGINT to %d", dinfo.di_target);
76	/* send a signal to kcfd process */
77	if ((kill(dinfo.di_target, SIGINT)) != 0) {
78		err = errno;
79		cryptodebug("failed to send a signal to kcfd: %s",
80		    strerror(errno));
81		goto stop_fail;
82	}
83
84stop_fail:
85	if (fd != -1)
86		(void) close(fd);
87
88	if (err != 0)  {
89		cryptoerror(LOG_STDERR, gettext(
90		    "no kcfd available to stop - %s."),
91		    strerror(err));
92		/*
93		 * We return with SMF_EXIT_OK because this was a request
94		 * to stop something that wasn't running.
95		 */
96		return (SMF_EXIT_OK);
97	}
98
99	return (SMF_EXIT_OK);
100}
101