/*
* 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 2012 Nexenta Systems, Inc. All rights reserved.
*/
/*
* SMBFS I/O Daemon (Per-user IOD)
*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <synch.h>
#include <time.h>
#include <unistd.h>
#include <ucred.h>
#include <err.h>
#include <door.h>
#include <libscf.h>
#include <locale.h>
#include <thread.h>
#define DPRINT(...) do \
{ \
if (smb_debug) \
} while (0)
int iod_terminating;
int
{
/* set locale and text domain for i18n */
(void) textdomain(TEXT_DOMAIN);
/* Debugging support. */
if (smb_debug < 1)
smb_debug = 1;
iod_alarm_time = 300;
}
/*
* If a user runs this command (i.e. by accident)
* don't interfere with any already running IOD.
*/
if (err == 0) {
door_fd = -1;
}
/*
* Want all signals blocked, as we're doing
* synchronous delivery via sigwait below.
*/
/* Setup the door service. */
if (door_fd == -1) {
perror("iod door_create");
goto out;
}
goto out;
}
/* Initializations done. */
rc = SMF_EXIT_OK;
/*
* Post the initial alarm, and then just
* wait for signals.
*/
switch (sig) {
case SIGCONT:
goto again;
case SIGALRM:
/* No threads active for a while. */
if (iod_thr_count > 0) {
/*
* Door call thread creation raced with
* the alarm. Ignore this alaram.
*/
goto again;
}
/* Prevent a race with iod_thr_count */
iod_terminating = 1;
break;
case SIGINT:
case SIGTERM:
break; /* normal termination */
default:
/* Unexpected signal. */
break;
}
out:
iod_terminating = 1;
if (attached)
if (door_fd != -1)
/*
* We need a reference in -lumem to satisfy check_rtime,
* else we get build hoise. This is sufficient.
*/
return (rc);
}
/*ARGSUSED*/
void
{
int rc;
/*
* Verify that the calling process has the same UID.
* Paranoia: The door we created has mode 0600, so
* this check is probably redundant.
*/
if (door_ucred(&ucred) != 0) {
goto out;
}
DPRINT("iod_dispatch: wrong UID\n");
goto out;
}
/*
* The library uses a NULL arg call to check if
* the daemon is running. Just return zero.
*/
rc = 0;
goto out;
}
/*
* Otherwise, the arg must be the (fixed size)
* smb_iod_ssn_t
*/
goto out;
}
if (iod_terminating) {
DPRINT("iod_dispatch: terminating\n");
goto out;
}
if (iod_thr_count++ == 0) {
alarm(0);
DPRINT("iod_dispatch: cancelled alarm\n");
}
if (--iod_thr_count == 0) {
DPRINT("iod_dispatch: schedule alarm\n");
}
out:
}
/*
* Try making a connection with the server described by
* the info in the smb_iod_ssn_t arg. If successful,
* start an IOD thread to service it, then return to
* the client side of the door.
*/
int
{
int err;
/*
* This needs to essentially "clone" the smb_ctx_t
* from the client side of the door, or at least
* as much of it as we need while creating a VC.
*/
if (err)
return (err);
/*
* Create the driver session first, so that any subsequent
* requests for the same session will find this one and
* wait, the same as when a reconnect is triggered.
*
* There is still an inherent race here, where two callers
* both find no VC in the driver, and both come here trying
* to create the VC. In this case, we want the first one
* to actually do the VC setup, and the second to proceed
* as if the VC had been found in the driver. The second
* caller gets an EEXIST error from the ioctl in this case,
* which we therefore ignore here so that the caller will
* go ahead and look again in the driver for the new VC.
*/
goto out;
err = 0; /* see above */
goto out;
}
/*
* Do the initial connection setup here, so we can
* report the outcome to the door client.
*/
if (err != 0)
goto out;
/* The rest happens in the iod_work thread. */
if (err == 0) {
/*
* Given to the new thread.
* free at end of iod_work
*/
}
out:
if (ctx)
return (err);
}
/*
* Be the reader thread for some VC.
*
* This is started by a door call thread, which means
* this is always at least the 2nd thread, therefore
* it should never see thr_count==0 or terminating.
*/
void *
{
if (iod_thr_count++ == 0) {
alarm(0);
DPRINT("iod_work: cancelled alarm\n");
}
(void) smb_iod_work(ctx);
if (--iod_thr_count == 0) {
DPRINT("iod_work: schedule alarm\n");
}
return (NULL);
}