dlmgmt_main.c revision 6ba597c56d749c61b4f783157f63196d7b2445f0
/*
* 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 2010 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.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <syslog.h>
#include <zone.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 with "ALL" privileges, before
* privileges are dropped in dlmgmt_drop_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_set_privileges();
static int
{
int err = 0;
return (err);
}
static int
dlmgmt_door_init(void)
{
int err = 0;
return (err);
}
return (err);
}
static void
dlmgmt_door_fini(void)
{
if (dlmgmt_door_fd == -1)
return;
}
(void) dlmgmt_set_doorfd(B_FALSE);
dlmgmt_door_fd = -1;
}
int
{
int fd;
int err = 0;
char doorpath[MAXPATHLEN];
/*
* Create the door file for dlmgmtd.
*/
return (err);
}
return (errno);
/*
* fdetach first in case a previous daemon instance exited
* ungracefully.
*/
} else if (zoneid == GLOBAL_ZONEID) {
}
}
return (err);
}
/*
* datalink.conf data for this zone, and create/attach the door rendezvous
* file.
*/
int
{
int err;
if (zoneid == GLOBAL_ZONEID) {
rootdir[0] = '\0';
sizeof (rootdir)) < 0) {
return (errno);
}
/*
* Create the DLMGMT_TMPFS_DIR directory.
*/
return (errno);
return (ENOTDIR);
}
return (EPERM);
}
return (err);
}
/*
* Initialize each running zone.
*/
static int
dlmgmt_allzones_init(void)
{
int err, i;
return (errno);
nzids *= 2;
return (errno);
nzids_saved = nzids;
return (errno);
}
if (nzids > nzids_saved) {
goto again;
}
for (i = 0; i < nzids; i++) {
break;
}
return (err);
}
static int
dlmgmt_init(void)
{
int err;
char *fmri, *c;
char filename[MAXPATHLEN];
return (EPERM);
}
return (err);
}
/*
* First derive the name of the cache file from the FMRI name. This
* cache name is used to keep active datalink configuration.
*/
if (debug) {
} else {
"service and should not be run from the command "
"line.");
return (EINVAL);
}
/*
* The FMRI name is in the form of
* prefix "svc:/" and replace '/' with '-'. The cache file
* name is in the form of "service:instance.cache".
*/
c++;
else
c = fmri;
c = filename;
*c = '-';
}
if ((err = dlmgmt_door_init()) != 0)
goto done;
/*
* Load datalink configuration and create dlmgmtd door files for all
* currently running zones.
*/
if ((err = dlmgmt_allzones_init()) != 0)
done:
if (err != 0)
return (err);
}
static void
dlmgmt_fini(void)
{
if (dld_handle != NULL) {
dld_handle = NULL;
}
}
/*
* 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)
{
}
/*
* Restrict privileges to only those needed.
*/
int
dlmgmt_drop_privileges(void)
{
int err = 0;
return (errno);
/*
* The global zone needs PRIV_PROC_FORK so that it can fork() when it
* issues db ops in non-global zones, PRIV_SYS_CONFIG to post
* sysevents, and PRIV_SYS_DL_CONFIG to initialize link properties in
* dlmgmt_upcall_linkprop_init().
*
* We remove non-basic privileges from the permitted (and thus
* effective) set. When executing in a non-global zone, dlmgmtd
* only needs to read and write to files that it already owns.
*/
if (zoneid == GLOBAL_ZONEID) {
} else {
}
done:
return (err);
}
int
{
int err = 0;
return (errno);
return (err);
}
/*
* Set the uid of this daemon to the "dladm" user and drop privileges to only
* those needed.
*/
static int
dlmgmt_set_privileges(void)
{
int err;
else
done:
return (err);
}
/*
* 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
{
progname++;
else
/*
* Process options.
*/
switch (opt) {
case 'd':
break;
default:
usage();
}
}
if (!debug && !dlmgmt_daemonize())
return (EXIT_FAILURE);
if ((err = dlmgmt_init()) != 0) {
goto child_out;
} else if ((err = dlmgmt_set_privileges()) != 0) {
dlmgmt_fini();
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);
}