xref: /titanic_51/usr/src/cmd/dlmgmtd/dlmgmt_main.c (revision 7ff836697c120cb94bd30d5c2204eb9b74718e4c)
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 2009 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 <strings.h>
49 #include <syslog.h>
50 #include <sys/dld.h>
51 #include <sys/dld_ioc.h>
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <libdladm_impl.h>
56 #include <libdlmgmt.h>
57 #include "dlmgmt_impl.h"
58 
59 const char		*progname;
60 boolean_t		debug;
61 static int		pfds[2];
62 /*
63  * This file descriptor to DLMGMT_DOOR cannot be in the libdladm
64  * handle because the door isn't created when the handle is created.
65  */
66 static int		dlmgmt_door_fd = -1;
67 
68 /*
69  * This libdladm handle is global so that dlmgmt_upcall_linkprop_init()
70  * can pass to libdladm.  The handle is opened during dlmgmt_init_privileges()
71  * with "ALL" privileges.  It is not able to open DLMGMT_DOOR at that time as
72  * it hasn't been created yet.  This door in the handle is opened in the first
73  * call to dladm_door_fd().
74  */
75 dladm_handle_t		dld_handle = NULL;
76 
77 static void		dlmgmtd_exit(int);
78 static int		dlmgmt_init();
79 static void		dlmgmt_fini();
80 static int		dlmgmt_init_privileges();
81 static void		dlmgmt_fini_privileges();
82 
83 static int
84 dlmgmt_set_doorfd(boolean_t start)
85 {
86 	dld_ioc_door_t did;
87 	int err = 0;
88 
89 	assert(dld_handle != NULL);
90 
91 	did.did_start_door = start;
92 
93 	if (ioctl(dladm_dld_fd(dld_handle), DLDIOC_DOORSERVER, &did) == -1)
94 		err = errno;
95 
96 	return (err);
97 }
98 
99 static int
100 dlmgmt_door_init()
101 {
102 	int fd;
103 	int err;
104 
105 	/*
106 	 * Create the door file for dlmgmtd.
107 	 */
108 	if ((fd = open(DLMGMT_DOOR, O_CREAT|O_RDONLY, 0644)) == -1) {
109 		err = errno;
110 		dlmgmt_log(LOG_ERR, "open(%s) failed: %s",
111 		    DLMGMT_DOOR, strerror(err));
112 		return (err);
113 	}
114 	(void) close(fd);
115 
116 	if ((dlmgmt_door_fd = door_create(dlmgmt_handler, NULL,
117 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
118 		err = errno;
119 		dlmgmt_log(LOG_ERR, "door_create() failed: %s",
120 		    strerror(err));
121 		return (err);
122 	}
123 	/*
124 	 * fdetach first in case a previous daemon instance exited
125 	 * ungracefully.
126 	 */
127 	(void) fdetach(DLMGMT_DOOR);
128 	if (fattach(dlmgmt_door_fd, DLMGMT_DOOR) != 0) {
129 		err = errno;
130 		dlmgmt_log(LOG_ERR, "fattach(%s) failed: %s",
131 		    DLMGMT_DOOR, strerror(err));
132 		goto fail;
133 	}
134 	if ((err = dlmgmt_set_doorfd(B_TRUE)) != 0) {
135 		dlmgmt_log(LOG_ERR, "cannot set kernel doorfd: %s",
136 		    strerror(err));
137 		(void) fdetach(DLMGMT_DOOR);
138 		goto fail;
139 	}
140 
141 	return (0);
142 fail:
143 	(void) door_revoke(dlmgmt_door_fd);
144 	dlmgmt_door_fd = -1;
145 	return (err);
146 }
147 
148 static void
149 dlmgmt_door_fini()
150 {
151 	if (dlmgmt_door_fd == -1)
152 		return;
153 
154 	if (door_revoke(dlmgmt_door_fd) == -1) {
155 		dlmgmt_log(LOG_WARNING, "door_revoke(%s) failed: %s",
156 		    DLMGMT_DOOR, strerror(errno));
157 	}
158 
159 	(void) fdetach(DLMGMT_DOOR);
160 	(void) dlmgmt_set_doorfd(B_FALSE);
161 }
162 
163 static int
164 dlmgmt_init()
165 {
166 	int err;
167 
168 	if (signal(SIGTERM, dlmgmtd_exit) == SIG_ERR ||
169 	    signal(SIGINT, dlmgmtd_exit) == SIG_ERR) {
170 		err = errno;
171 		dlmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
172 		    strerror(err));
173 		return (err);
174 	}
175 
176 	if ((err = dlmgmt_linktable_init()) != 0)
177 		return (err);
178 
179 	if ((err = dlmgmt_db_init()) != 0 || (err = dlmgmt_door_init()) != 0)
180 		dlmgmt_linktable_fini();
181 
182 	return (err);
183 }
184 
185 static void
186 dlmgmt_fini()
187 {
188 	dlmgmt_door_fini();
189 	dlmgmt_linktable_fini();
190 }
191 
192 /*
193  * This is called by the child process to inform the parent process to
194  * exit with the given return value.
195  */
196 static void
197 dlmgmt_inform_parent_exit(int rv)
198 {
199 	if (debug)
200 		return;
201 
202 	if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
203 		dlmgmt_log(LOG_WARNING,
204 		    "dlmgmt_inform_parent_exit() failed: %s", strerror(errno));
205 		(void) close(pfds[1]);
206 		exit(EXIT_FAILURE);
207 	}
208 	(void) close(pfds[1]);
209 }
210 
211 /*ARGSUSED*/
212 static void
213 dlmgmtd_exit(int signo)
214 {
215 	(void) close(pfds[1]);
216 	dlmgmt_fini();
217 	dlmgmt_fini_privileges();
218 	exit(EXIT_FAILURE);
219 }
220 
221 static void
222 usage(void)
223 {
224 	(void) fprintf(stderr, "Usage: %s [-d]\n", progname);
225 	exit(EXIT_FAILURE);
226 }
227 
228 /*
229  * Set the uid of this daemon to the "dladm" user. Finish the following
230  * operations before setuid() because they need root privileges:
231  *
232  *    - create the /etc/svc/volatile/dladm directory;
233  *    - change its uid/gid to "dladm"/"sys";
234  *    - open the dld control node
235  */
236 static int
237 dlmgmt_init_privileges()
238 {
239 	struct stat	statbuf;
240 
241 	/*
242 	 * Create the DLMGMT_TMPFS_DIR directory.
243 	 */
244 	if (stat(DLMGMT_TMPFS_DIR, &statbuf) < 0) {
245 		if (mkdir(DLMGMT_TMPFS_DIR, (mode_t)0755) < 0)
246 			return (errno);
247 	} else {
248 		if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
249 			return (ENOTDIR);
250 	}
251 
252 	if ((chmod(DLMGMT_TMPFS_DIR, 0755) < 0) ||
253 	    (chown(DLMGMT_TMPFS_DIR, UID_DLADM, GID_SYS) < 0)) {
254 		return (EPERM);
255 	}
256 
257 	/*
258 	 * When dlmgmtd is started at boot, "ALL" privilege is required
259 	 * to open the dld control node.  The door isn't created yet.
260 	 */
261 	if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
262 		dlmgmt_log(LOG_ERR, "dladm_open() failed");
263 		return (EPERM);
264 	}
265 
266 	/*
267 	 * We need PRIV_SYS_DL_CONFIG for the DLDIOC_DOORSERVER ioctl,
268 	 * and PRIV_SYS_CONFIG to post sysevents.
269 	 */
270 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, UID_DLADM,
271 	    GID_SYS, PRIV_SYS_DL_CONFIG, PRIV_SYS_CONFIG, NULL) == -1) {
272 		dladm_close(dld_handle);
273 		dld_handle = NULL;
274 		return (EPERM);
275 	}
276 
277 
278 	return (0);
279 }
280 
281 static void
282 dlmgmt_fini_privileges()
283 {
284 	if (dld_handle != NULL) {
285 		dladm_close(dld_handle);
286 		dld_handle = NULL;
287 	}
288 }
289 
290 /*
291  * Keep the pfds fd open, close other fds.
292  */
293 /*ARGSUSED*/
294 static int
295 closefunc(void *arg, int fd)
296 {
297 	if (fd != pfds[1])
298 		(void) close(fd);
299 	return (0);
300 }
301 
302 static boolean_t
303 dlmgmt_daemonize(void)
304 {
305 	pid_t pid;
306 	int rv;
307 
308 	if (pipe(pfds) < 0) {
309 		(void) fprintf(stderr, "%s: pipe() failed: %s\n",
310 		    progname, strerror(errno));
311 		exit(EXIT_FAILURE);
312 	}
313 
314 	if ((pid = fork()) == -1) {
315 		(void) fprintf(stderr, "%s: fork() failed: %s\n",
316 		    progname, strerror(errno));
317 		exit(EXIT_FAILURE);
318 	} else if (pid > 0) { /* Parent */
319 		(void) close(pfds[1]);
320 
321 		/*
322 		 * Read the child process's return value from the pfds.
323 		 * If the child process exits unexpected, read() returns -1.
324 		 */
325 		if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
326 			(void) kill(pid, SIGKILL);
327 			rv = EXIT_FAILURE;
328 		}
329 
330 		(void) close(pfds[0]);
331 		exit(rv);
332 	}
333 
334 	/* Child */
335 	(void) close(pfds[0]);
336 	(void) setsid();
337 
338 	/*
339 	 * Close all files except pfds[1].
340 	 */
341 	(void) fdwalk(closefunc, NULL);
342 	(void) chdir("/");
343 	openlog(progname, LOG_PID, LOG_DAEMON);
344 	return (B_TRUE);
345 }
346 
347 int
348 main(int argc, char *argv[])
349 {
350 	int		opt;
351 
352 	progname = strrchr(argv[0], '/');
353 	if (progname != NULL)
354 		progname++;
355 	else
356 		progname = argv[0];
357 
358 	/*
359 	 * Process options.
360 	 */
361 	while ((opt = getopt(argc, argv, "d")) != EOF) {
362 		switch (opt) {
363 		case 'd':
364 			debug = B_TRUE;
365 			break;
366 		default:
367 			usage();
368 		}
369 	}
370 
371 	if (!debug && !dlmgmt_daemonize())
372 		return (EXIT_FAILURE);
373 
374 	if ((errno = dlmgmt_init_privileges()) != 0) {
375 		dlmgmt_log(LOG_ERR, "dlmgmt_init_privileges() failed: %s",
376 		    strerror(errno));
377 		goto child_out;
378 	}
379 
380 	if (dlmgmt_init() != 0) {
381 		dlmgmt_fini_privileges();
382 		goto child_out;
383 	}
384 
385 	/*
386 	 * Inform the parent process that it can successfully exit.
387 	 */
388 	dlmgmt_inform_parent_exit(EXIT_SUCCESS);
389 
390 	for (;;)
391 		(void) pause();
392 
393 child_out:
394 	/* return from main() forcibly exits an MT process */
395 	dlmgmt_inform_parent_exit(EXIT_FAILURE);
396 	return (EXIT_FAILURE);
397 }
398