xref: /titanic_51/usr/src/cmd/dlmgmtd/dlmgmt_main.c (revision e61a481d2b9ca0c0f387e11b15dcabfc982bb464)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * The dlmgmtd daemon is started by the datalink-management SMF service.
29  * This daemon is used to manage <link name, linkid> mapping and the
30  * persistent datalink configuration.
31  *
32  * Today, the <link name, linkid> mapping and the persistent configuration
33  * of datalinks is kept in /etc/dladm/datalink.conf, and the daemon keeps
34  * a copy of the datalinks in the memory (see dlmgmt_id_avl and
35  * dlmgmt_name_avl). The active <link name, linkid> mapping is kept in
36  * /etc/svc/volatile/dladm cache file, so that the mapping can be recovered
37  * when dlmgmtd exits for some reason (e.g., when dlmgmtd is accidentally
38  * killed).
39  */
40 
41 #include <assert.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <priv_utils.h>
45 #include <signal.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <stropts.h>
49 #include <strings.h>
50 #include <syslog.h>
51 #include <sys/dld.h>
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <libdlmgmt.h>
56 #include "dlmgmt_impl.h"
57 
58 const char		*progname;
59 boolean_t		debug;
60 static int		pfds[2];
61 static int		dlmgmt_door_fd = -1;
62 static int		dld_control_fd = -1;
63 
64 static void		dlmgmtd_exit(int);
65 static int		dlmgmt_init();
66 static void		dlmgmt_fini();
67 static int		dlmgmt_init_privileges();
68 static void		dlmgmt_fini_privileges();
69 
70 static int
71 dlmgmt_set_doorfd(boolean_t start)
72 {
73 	dld_ioc_door_t did;
74 	struct strioctl iocb;
75 	int err = 0;
76 
77 	assert(dld_control_fd != -1);
78 
79 	did.did_start_door = start;
80 
81 	iocb.ic_cmd	= DLDIOC_DOORSERVER;
82 	iocb.ic_timout	= 0;
83 	iocb.ic_len	= sizeof (did);
84 	iocb.ic_dp	= (char *)&did;
85 
86 	if (ioctl(dld_control_fd, I_STR, &iocb) == -1)
87 		err = errno;
88 
89 	return (err);
90 }
91 
92 static int
93 dlmgmt_door_init()
94 {
95 	int fd;
96 	int err;
97 
98 	/*
99 	 * Create the door file for dlmgmtd.
100 	 */
101 	if ((fd = open(DLMGMT_DOOR, O_CREAT|O_RDONLY, 0644)) == -1) {
102 		err = errno;
103 		dlmgmt_log(LOG_ERR, "open(%s) failed: %s",
104 		    DLMGMT_DOOR, strerror(err));
105 		return (err);
106 	}
107 	(void) close(fd);
108 
109 	if ((dlmgmt_door_fd = door_create(dlmgmt_handler, NULL,
110 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
111 		err = errno;
112 		dlmgmt_log(LOG_ERR, "door_create() failed: %s",
113 		    strerror(err));
114 		return (err);
115 	}
116 	if (fattach(dlmgmt_door_fd, DLMGMT_DOOR) != 0) {
117 		err = errno;
118 		dlmgmt_log(LOG_ERR, "fattach(%s) failed: %s",
119 		    DLMGMT_DOOR, strerror(err));
120 		goto fail;
121 	}
122 	if ((err = dlmgmt_set_doorfd(B_TRUE)) != 0) {
123 		dlmgmt_log(LOG_ERR, "cannot set kernel doorfd: %s",
124 		    strerror(err));
125 		(void) fdetach(DLMGMT_DOOR);
126 		goto fail;
127 	}
128 
129 	return (0);
130 fail:
131 	(void) door_revoke(dlmgmt_door_fd);
132 	dlmgmt_door_fd = -1;
133 	return (err);
134 }
135 
136 static void
137 dlmgmt_door_fini()
138 {
139 	if (dlmgmt_door_fd == -1)
140 		return;
141 
142 	if (door_revoke(dlmgmt_door_fd) == -1) {
143 		dlmgmt_log(LOG_WARNING, "door_revoke(%s) failed: %s",
144 		    DLMGMT_DOOR, strerror(errno));
145 	}
146 
147 	(void) fdetach(DLMGMT_DOOR);
148 	(void) dlmgmt_set_doorfd(B_FALSE);
149 }
150 
151 static int
152 dlmgmt_init()
153 {
154 	int		err;
155 
156 	if (signal(SIGTERM, dlmgmtd_exit) == SIG_ERR ||
157 	    signal(SIGINT, dlmgmtd_exit) == SIG_ERR) {
158 		err = errno;
159 		dlmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
160 		    strerror(err));
161 		return (err);
162 	}
163 
164 	if ((err = dlmgmt_linktable_init()) != 0)
165 		return (err);
166 
167 	if ((err = dlmgmt_db_init()) != 0 || (err = dlmgmt_door_init()) != 0)
168 		dlmgmt_linktable_fini();
169 
170 	return (err);
171 }
172 
173 static void
174 dlmgmt_fini()
175 {
176 	dlmgmt_door_fini();
177 	dlmgmt_linktable_fini();
178 }
179 
180 /*
181  * This is called by the child process to inform the parent process to
182  * exit with the given return value.
183  */
184 static void
185 dlmgmt_inform_parent_exit(int rv)
186 {
187 	if (debug)
188 		return;
189 
190 	if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
191 		dlmgmt_log(LOG_WARNING,
192 		    "dlmgmt_inform_parent_exit() failed: %s", strerror(errno));
193 		(void) close(pfds[1]);
194 		exit(EXIT_FAILURE);
195 	}
196 	(void) close(pfds[1]);
197 }
198 
199 /*ARGSUSED*/
200 static void
201 dlmgmtd_exit(int signo)
202 {
203 	(void) close(pfds[1]);
204 	dlmgmt_fini();
205 	dlmgmt_fini_privileges();
206 	exit(EXIT_FAILURE);
207 }
208 
209 static void
210 usage(void)
211 {
212 	(void) fprintf(stderr, "Usage: %s [-d]\n", progname);
213 	exit(EXIT_FAILURE);
214 }
215 
216 /*
217  * Set the uid of this daemon to the "dladm" user. Finish the following
218  * operations before setuid() because they need root privileges:
219  *
220  *    - create the /etc/svc/volatile/dladm directory;
221  *    - change its uid/gid to "dladm"/"sys";
222  *    - open the dld control node
223  */
224 static int
225 dlmgmt_init_privileges()
226 {
227 	struct stat	statbuf;
228 
229 	/*
230 	 * Create the DLMGMT_TMPFS_DIR directory.
231 	 */
232 	if (stat(DLMGMT_TMPFS_DIR, &statbuf) < 0) {
233 		if (mkdir(DLMGMT_TMPFS_DIR, (mode_t)0755) < 0)
234 			return (errno);
235 	} else {
236 		if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
237 			return (ENOTDIR);
238 	}
239 
240 	if ((chmod(DLMGMT_TMPFS_DIR, 0755) < 0) ||
241 	    (chown(DLMGMT_TMPFS_DIR, UID_DLADM, GID_SYS) < 0)) {
242 		return (EPERM);
243 	}
244 
245 	/*
246 	 * When dlmgmtd is started at boot, "ALL" privilege is required
247 	 * to open the dld control node.
248 	 */
249 	if ((dld_control_fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
250 		return (errno);
251 
252 	/*
253 	 * The PRIV_SYS_CONFIG privilege is needed to post sysevents.
254 	 */
255 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, UID_DLADM,
256 	    GID_SYS, PRIV_SYS_NET_CONFIG, PRIV_SYS_CONFIG, NULL) == -1) {
257 		(void) close(dld_control_fd);
258 		dld_control_fd = -1;
259 		return (EPERM);
260 	}
261 
262 	return (0);
263 }
264 
265 static void
266 dlmgmt_fini_privileges()
267 {
268 	if (dld_control_fd != -1) {
269 		(void) close(dld_control_fd);
270 		dld_control_fd = -1;
271 	}
272 }
273 
274 /*
275  * Keep the pfds fd open, close other fds.
276  */
277 /*ARGSUSED*/
278 static int
279 closefunc(void *arg, int fd)
280 {
281 	if (fd != pfds[1])
282 		(void) close(fd);
283 	return (0);
284 }
285 
286 static boolean_t
287 dlmgmt_daemonize(void)
288 {
289 	pid_t pid;
290 	int rv;
291 
292 	if (pipe(pfds) < 0) {
293 		(void) fprintf(stderr, "%s: pipe() failed: %s\n",
294 		    progname, strerror(errno));
295 		exit(EXIT_FAILURE);
296 	}
297 
298 	if ((pid = fork()) == -1) {
299 		(void) fprintf(stderr, "%s: fork() failed: %s\n",
300 		    progname, strerror(errno));
301 		exit(EXIT_FAILURE);
302 	} else if (pid > 0) { /* Parent */
303 		(void) close(pfds[1]);
304 
305 		/*
306 		 * Read the child process's return value from the pfds.
307 		 * If the child process exits unexpected, read() returns -1.
308 		 */
309 		if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
310 			(void) kill(pid, SIGKILL);
311 			rv = EXIT_FAILURE;
312 		}
313 
314 		(void) close(pfds[0]);
315 		exit(rv);
316 	}
317 
318 	/* Child */
319 	(void) close(pfds[0]);
320 	(void) setsid();
321 
322 	/*
323 	 * Close all files except pfds[1].
324 	 */
325 	(void) fdwalk(closefunc, NULL);
326 	(void) chdir("/");
327 	openlog(progname, LOG_PID, LOG_DAEMON);
328 	return (B_TRUE);
329 }
330 
331 int
332 main(int argc, char *argv[])
333 {
334 	int		opt;
335 
336 	progname = strrchr(argv[0], '/');
337 	if (progname != NULL)
338 		progname++;
339 	else
340 		progname = argv[0];
341 
342 	/*
343 	 * Process options.
344 	 */
345 	while ((opt = getopt(argc, argv, "d")) != EOF) {
346 		switch (opt) {
347 		case 'd':
348 			debug = B_TRUE;
349 			break;
350 		default:
351 			usage();
352 		}
353 	}
354 
355 	if (!debug && !dlmgmt_daemonize())
356 		return (EXIT_FAILURE);
357 
358 	if ((errno = dlmgmt_init_privileges()) != 0) {
359 		dlmgmt_log(LOG_ERR, "dlmgmt_init_privileges() failed: %s",
360 		    strerror(errno));
361 		goto child_out;
362 	}
363 
364 	if (dlmgmt_init() != 0) {
365 		dlmgmt_fini_privileges();
366 		goto child_out;
367 	}
368 
369 	/*
370 	 * Inform the parent process that it can successfully exit.
371 	 */
372 	dlmgmt_inform_parent_exit(EXIT_SUCCESS);
373 
374 	for (;;)
375 		(void) pause();
376 
377 child_out:
378 	/* return from main() forcibly exits an MT process */
379 	dlmgmt_inform_parent_exit(EXIT_FAILURE);
380 	return (EXIT_FAILURE);
381 }
382