dlmgmt_main.c revision 82a2fc4751cef28c0bdc327d02012bf8796083b9
/*
* 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
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* The dlmgmtd daemon is started by the datalink-management SMF service.
* This daemon is used to manage <link name, linkid> mapping and the
* persistent datalink configuration.
*
* Today, the <link name, linkid> mapping and the persistent configuration
* of datalinks is kept in /etc/dladm/datalink.conf, and the daemon keeps
* a copy of the datalinks in the memory (see dlmgmt_id_avl and
* dlmgmt_name_avl). The active <link name, linkid> mapping is kept in
* when dlmgmtd exits for some reason (e.g., when dlmgmtd is accidentally
* killed).
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <priv_utils.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <syslog.h>
#include <unistd.h>
#include <libdladm_impl.h>
#include <libdlmgmt.h>
#include "dlmgmt_impl.h"
const char *progname;
static int pfds[2];
/*
* This file descriptor to DLMGMT_DOOR cannot be in the libdladm
* handle because the door isn't created when the handle is created.
*/
static int dlmgmt_door_fd = -1;
/*
* This libdladm handle is global so that dlmgmt_upcall_linkprop_init()
* can pass to libdladm. The handle is opened during dlmgmt_init_privileges()
* with "ALL" privileges. It is not able to open DLMGMT_DOOR at that time as
* it hasn't been created yet. This door in the handle is opened in the first
* call to dladm_door_fd().
*/
static void dlmgmtd_exit(int);
static int dlmgmt_init();
static void dlmgmt_fini();
static int dlmgmt_init_privileges();
static void dlmgmt_fini_privileges();
static int
{
int err = 0;
return (err);
}
static int
{
int fd;
int err;
/*
* Create the door file for dlmgmtd.
*/
return (err);
}
return (err);
}
/*
* fdetach first in case a previous daemon instance exited
* ungracefully.
*/
(void) fdetach(DLMGMT_DOOR);
goto fail;
}
(void) fdetach(DLMGMT_DOOR);
goto fail;
}
return (0);
fail:
(void) door_revoke(dlmgmt_door_fd);
dlmgmt_door_fd = -1;
return (err);
}
static void
{
if (dlmgmt_door_fd == -1)
return;
}
(void) fdetach(DLMGMT_DOOR);
(void) dlmgmt_set_doorfd(B_FALSE);
}
static int
{
int err;
return (err);
}
if ((err = dlmgmt_linktable_init()) != 0)
return (err);
return (err);
}
static void
{
}
/*
* This is called by the child process to inform the parent process to
* exit with the given return value.
*/
static void
{
if (debug)
return;
}
}
/*ARGSUSED*/
static void
dlmgmtd_exit(int signo)
{
dlmgmt_fini();
}
static void
usage(void)
{
}
/*
* Set the uid of this daemon to the "dladm" user. Finish the following
* operations before setuid() because they need root privileges:
*
* - open the dld control node
*/
static int
{
/*
* Create the DLMGMT_TMPFS_DIR directory.
*/
return (errno);
} else {
return (ENOTDIR);
}
return (EPERM);
}
/*
* When dlmgmtd is started at boot, "ALL" privilege is required
* to open the dld control node. The door isn't created yet.
*/
return (EPERM);
}
/*
* We need PRIV_SYS_DL_CONFIG for the DLDIOC_DOORSERVER ioctl,
* and PRIV_SYS_CONFIG to post sysevents.
*/
dld_handle = NULL;
return (EPERM);
}
return (0);
}
static void
{
if (dld_handle != NULL) {
dld_handle = NULL;
}
}
/*
* Keep the pfds fd open, close other fds.
*/
/*ARGSUSED*/
static int
{
return (0);
}
static boolean_t
dlmgmt_daemonize(void)
{
int rv;
}
} else if (pid > 0) { /* Parent */
/*
* Read the child process's return value from the pfds.
* If the child process exits unexpected, read() returns -1.
*/
rv = EXIT_FAILURE;
}
}
/* Child */
(void) setsid();
/*
* Close all files except pfds[1].
*/
(void) chdir("/");
return (B_TRUE);
}
int
{
int opt;
progname++;
else
/*
* Process options.
*/
switch (opt) {
case 'd':
break;
default:
usage();
}
}
if (!debug && !dlmgmt_daemonize())
return (EXIT_FAILURE);
if ((errno = dlmgmt_init_privileges()) != 0) {
goto child_out;
}
if (dlmgmt_init() != 0) {
goto child_out;
}
/*
* Inform the parent process that it can successfully exit.
*/
for (;;)
(void) pause();
/* return from main() forcibly exits an MT process */
return (EXIT_FAILURE);
}