summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/lib/netcfgd/netcfgd.c
blob: 5002e9e808e4c3b7f5a7d547310a84fa11303ef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * netcfgd - network configuration daemon.  At present, this daemon implements
 * the configuration backend for libnwam (via calls to nwam_backend_init()
 * and nwam_backend_fini()).  Initialization of the backend creates a  door
 * that libnwam calls use to read, update and destroy persistent configuration.
 *
 * More long-term, netcfgd will be used to manage other sources of configuration
 * data and the backend functionality currently contained in libnwam will be
 * generalized.
 */

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wait.h>
#include <libnwam_priv.h>
#include <libnwam.h>

static const char *progname;
static boolean_t fg = B_FALSE;

/*
 * This function allows you to drop a dtrace probe here and trace
 * complete strings (not just those containing formatting).  It's
 * important that we actually format the strings so we could trace them
 * even if we choose not to log them.
 */
static void
log_out(int severity, const char *str)
{
	if (fg) {
		(void) fprintf(stderr, "%s: %s\n", progname, str);
	} else {
		syslog(severity, str);
	}
}

/* PRINTFLIKE2 */
void
nlog(int severity, const char *fmt, ...)
{
	va_list ap;
	char *vbuf;

	va_start(ap, fmt);
	if (vasprintf(&vbuf, fmt, ap) != -1) {
		log_out(severity, vbuf);
		free(vbuf);
	}
	va_end(ap);
}

static void
start_logging(void)
{
	if (!fg)
		openlog(progname, LOG_PID, LOG_DAEMON);

	nlog(LOG_DEBUG, "%s started", progname);
}

static void
daemonize(void)
{
	pid_t pid;

	/*
	 * A little bit of magic here.  By the first fork+setsid, we
	 * disconnect from our current controlling terminal and become
	 * a session group leader.  By forking again without calling
	 * setsid again, we make certain that we are not the session
	 * group leader and can never reacquire a controlling terminal.
	 */
	if ((pid = fork()) == -1) {
		nlog(LOG_ERR, "fork 1 failed");
		exit(EXIT_FAILURE);
	}
	if (pid != 0) {
		(void) wait(NULL);
		nlog(LOG_DEBUG, "child %ld exited, daemonizing", pid);
		_exit(0);
	}
	if (setsid() == (pid_t)-1) {
		nlog(LOG_ERR, "setsid");
		exit(EXIT_FAILURE);
	}
	if ((pid = fork()) == -1) {
		nlog(LOG_ERR, "fork 2 failed");
		exit(EXIT_FAILURE);
	}
	if (pid != 0) {
		_exit(0);
	}
	(void) chdir("/");
	(void) umask(022);
}

/* ARGSUSED */
static void
graceful_shutdown(int signo)
{
	nwam_backend_fini();
	exit(EXIT_SUCCESS);
}

int
main(int argc, char *argv[])
{
	int c;
	nwam_error_t err;

	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		progname++;

	while ((c = getopt(argc, argv, "f")) != -1) {
		switch (c) {
		case 'f':
			fg = B_TRUE;
			break;
		default:
			(void) fprintf(stderr, "%s: unrecognized option %c\n",
			    progname, optopt);
			exit(EXIT_FAILURE);
		}
	}
	start_logging();

	if (!fg)
		daemonize();

	(void) signal(SIGTERM, graceful_shutdown);
	(void) signal(SIGQUIT, graceful_shutdown);
	(void) signal(SIGPIPE, SIG_IGN);
	(void) signal(SIGCHLD, SIG_IGN);
	(void) atexit(nwam_backend_fini);

	if ((err = nwam_backend_init()) != NWAM_SUCCESS) {
		nlog(LOG_ERR,
		    "couldn't initialize libnwam backend: %s",
		    nwam_strerror(err));
		exit(EXIT_FAILURE);
	}

	for (;;)
		(void) pause();

	/* NOTREACHED */
	return (EXIT_SUCCESS);
}