main.c revision 634e26ec75c89095090605284938356a3145f2b8
/*
* 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.
*/
/*
* nwamd - NetWork Auto-Magic Daemon
*/
#include <fcntl.h>
#include <priv.h>
#include <pthread.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>
#include <locale.h>
#include <libintl.h>
#include <errno.h>
#include "defines.h"
#include "structures.h"
#include "functions.h"
#include "variables.h"
const char *OUR_PG = "nwamd";
static sigset_t sigwaitset;
char zonename[ZONENAME_MAX];
/*
* nwamd
*
* This is the Network Auto-Magic daemon. For further high level information
* see the Network Auto-Magic project and the Approachability communities
* on opensolaris.org, and nwamd(1M).
*
* The general structure of the code is as a set of threads collecting
* system events which are fed into a state machine which alters system
* state based on configuration.
*
* signal management
* Due to being threaded, a simple set of signal handlers would not work
* very well for nwamd. Instead nwamd blocks signals at startup and
* then starts a thread which sits in sigwait(2) waiting for signals.
* When a signal is received the signal handling thread dispatches it.
* It handles:
* - shutting down, done by creating an event which is passed through the
* system allowing the various subsystems to do any necessary cleanup.
* - SIGALRM for timers.
* - SIGHUP for instance refresh, which tells us to look up various
* properties from SMF(5).
*
* subprocess management
* nwamd starts several different subprocesses to manage the system. Some
* of those start other processes (e.g. `ifconfig <if> dhcp` ends up starting
* dhcpagent if necessary). Due to the way we manage signals if we started
* those up without doing anything special their signal mask would mostly
* block signals. So we restore the signal mask when we start subprocesses.
* This is especially important with respect to DHCP as later when we exit
* we need to kill the dhcpagent process which we started; for details, see
* the block comment in state_machine.c in its cleanup() function.
*/
/*
* In this file there are several utility functions which might otherwise
* belong in util.c, but since they are only called from main(), they can
* live here as static functions:
* - syslog set-up
* - daemonizing
* - looking up SMF(5) properties
* - signal handling
* - managing privileges(5)
*/
static void
start_logging(void)
{
}
static void
daemonize(void)
{
/*
* A little bit of magic here. By the first fork+setsid, we
* disconnect from our current controlling terminal and become
* a session group leader. By forking again without calling
* setsid again, we make certain that we are not the session
* group leader and can never reacquire a controlling terminal.
*/
}
if (pid != 0) {
_exit(0);
}
}
}
if (pid != 0) {
_exit(0);
}
(void) chdir("/");
(void) umask(022);
}
/*
* Look up nwamd property values and set daemon variables appropriately.
* This function will be called on startup and via the signal handling
* thread on receiving a HUP (which occurs when the nwam service is
* refreshed).
*/
static void
lookup_daemon_properties(void)
{
&strict_bssid_set) == 0)
dprintf("Read daemon configuration properties.");
}
/* ARGSUSED */
static void *
sighandler(void *arg)
{
while (!shutting_down) {
switch (sig) {
case SIGALRM:
/*
* We may have multiple interfaces with
* scheduled timers; walk the list and
* create a timer event for each one.
*/
break;
case SIGHUP:
/*
* Refresh action - reread configuration properties.
*/
/*
* Check if user restarted scanning.
*/
if (scan == 0 && wlan_scan_interval != 0) {
if (err != 0) {
"pthread_create wireless scan: %s",
} else {
dprintf("wireless scan thread: %d",
scan);
}
}
break;
case SIGINT:
/*
* Undocumented "print debug status" signal.
*/
break;
case SIGTHAW:
/*
* It seems unlikely that this is helpful, but it can't
* hurt: when waking up from a sleep, check if the
* wireless interface is still viable. There've been
* bugs in this area.
*/
if (pthread_mutex_lock(&machine_lock) == 0) {
if (link_layer_profile != NULL &&
IF_WIRELESS) {
}
(void) pthread_mutex_unlock(&machine_lock);
}
break;
case SIGTERM:
dprintf("could not allocate shutdown event");
cleanup();
}
break;
default:
break;
}
}
return (NULL);
}
static void
init_signalhandling(void)
{
int err;
/*
* Construct the set of signals that we explicitly want
* to deal with. These will be blocked now, while we're
* still single-threaded; this block will be inherited by
* all the threads we create. The signal handling thread
* will then sigwait() this same set of signals, and will
* thus receive and process any that are sent to the process.
*/
(void) sigemptyset(&sigwaitset);
/*
* now start the signal handling thread...
*/
(void) pthread_attr_init(&attr);
} else {
}
(void) pthread_attr_destroy(&attr);
}
static void
change_user_set_privs(void)
{
priv_set = priv_allocset();
} else {
char *p;
free(p);
}
/* always start with the basic set */
}
}
}
}
static void
init_machine_mutex(void)
{
(void) pthread_mutexattr_init(&attrs);
}
(void) pthread_mutexattr_destroy(&attrs);
}
int
{
int c;
int scan_lev;
struct np_event *e;
enum np_event_type etype;
(void) textdomain(TEXT_DOMAIN);
switch (c) {
case 'f':
break;
case 's':
if (scan_lev >= DLADM_WLAN_STRENGTH_VERY_WEAK &&
} else {
"strength: %s", optarg);
}
break;
default:
optopt);
break;
}
}
/*
* The dladm handle *must* be opened before privileges are dropped
* by nwamd. The device privilege requirements from
* /etc/security/device_policy may not be loaded yet. These are
* loaded by svc:/system/filesystem/root, which comes online after
*/
}
if (!fg)
daemonize();
(void) start_event_collection();
while ((e = np_queue_get_event()) != NULL) {
if (etype == EV_SHUTDOWN)
if (pthread_mutex_lock(&machine_lock) != 0) {
}
state_machine(e);
(void) pthread_mutex_unlock(&machine_lock);
free_event(e);
if (etype == EV_SHUTDOWN)
break;
}
(void) pthread_cancel(routing);
if (scan != 0) {
(void) pthread_cancel(scan);
}
return (EXIT_SUCCESS);
}