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