smb_nicmon.c revision 6537f381d2d9e7b4e2f7b29c3e7a3f13be036f2e
/*
* 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;
/* Use this to refresh service instance */
static char *smb_nicmon_caller_fmri = NULL;
/*
* Start the nic monitor thread.
*/
int
smb_nicmon_start(const char *svc_fmri)
{
int rc = 0;
if ((rc = smb_nic_init()) != 0) {
return (rc);
}
if (rc != 0) {
return (rc);
}
if (svc_fmri)
smb_nicmon_caller_fmri = (char *)svc_fmri;
return (rc);
}
/*
* Stop the nic monitor.
*/
void
smb_nicmon_stop(void)
{
if (eventpipe_write < 0)
return;
smb_nic_fini();
}
/*
* 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 the instance
* of the registered SMF service.
*/
if (nic_changed && smb_nicmon_caller_fmri)
if (smf_refresh_instance(smb_nicmon_caller_fmri) != 0)
"failed to refresh SMF instance %s",
}
done:
/* Close sockets */
(void) close(eventpipe_read);
(void) close(eventpipe_write);
eventpipe_write = -1;
return (NULL);
}