smbd_nicmon.c revision 7b59d02d2a384be9a08087b14defadd214b3c1dd
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This is the SMB NIC monitoring module.
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <syslog.h>
#include <smbsrv/libsmbns.h>
static pthread_t smb_nicmon_thread;
static void smb_nicmon_setup_rtsock(int, int *);
static int smb_nicmon_needscan(int);
static void *smb_nicmon_daemon(void *);
static int smb_nicmon_setup_eventpipe(int *, int *);
/* Use this to stop monitoring */
static int eventpipe_write = -1;
/*
* Start the nic monitor thread.
*/
int
smb_nicmon_start(void)
{
int rc = 0;
if ((rc = smb_nic_init()) != 0) {
return (rc);
}
if (rc != 0) {
return (rc);
}
return (rc);
}
/*
* Stop the nic monitor.
*/
void
smb_nicmon_stop(void)
{
if (eventpipe_write < 0)
return;
smb_nic_fini();
}
/*
* Call this to do stuff after ifs changed.
*/
void
smb_nicmon_reconfig(void)
{
/* Clear rev zone before creating if list */
if (ddns_enabled) {
if (dyndns_clear_rev_zone() != 0) {
"failed to clear DNS reverse lookup zone");
}
}
/* re-initialize NIC table */
if (smb_nic_init() != 0)
"failed to get NIC information");
if (ddns_enabled) {
if (dyndns_update() != 0) {
"failed to update dynamic DNS");
}
}
}
/*
* Setup routing socket for getting RTM messages.
*/
static void
smb_nicmon_setup_rtsock(int af, int *s)
{
int flags;
if (*s == -1) {
"create routing socket");
return;
}
"failed to fcntl F_GETFL");
(void) close(*s);
*s = -1;
return;
}
"failed to fcntl F_SETFL");
(void) close(*s);
*s = -1;
return;
}
}
static int
smb_nicmon_needscan(int sock)
{
int nbytes;
int need_if_scan = 0;
/* Read as many messages as possible and try to empty the sockets */
for (;;) {
if (nbytes <= 0) {
break;
}
continue;
}
continue;
}
case RTM_NEWADDR:
case RTM_DELADDR:
case RTM_IFINFO:
need_if_scan = 1;
break;
default:
break;
}
}
return (need_if_scan);
}
/*
* Create pipe for signal delivery and set up signal handlers.
*/
static int
{
int fds[2];
return (1);
}
return (0);
}
/*ARGSUSED*/
static void *
smb_nicmon_daemon(void *args)
{
int pollfd_num = 2;
int i, nic_changed;
/* AF_INET routing socket add AF_INET6 when we support IPv6 */
static int rtsock_v4;
static int eventpipe_read = -1;
/*
* Create the global routing socket. We use this to
* monitor changes in NIC interfaces. We are only interested
*/
if (rtsock_v4 == -1) {
"cannot open routing socket");
return (NULL);
}
!= 0) {
return (NULL);
}
/*
* Keep listening for activity on any of the sockets.
*/
for (;;) {
nic_changed = 0;
continue;
"poll failed with errno %d", errno);
break;
}
for (i = 0; i < pollfd_num; i++) {
continue;
goto done;
}
/*
* If anything changed do refresh our
* nic list and other configs.
*/
if (nic_changed)
}
done:
/* Close sockets */
(void) close(eventpipe_read);
(void) close(eventpipe_write);
eventpipe_write = -1;
return (NULL);
}