smbiod.c revision 15359501f7d4b9abebd7b7bf6efd5982a8e7eb27
/*
* 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
*/
/*
*/
/*
* SMBFS I/O Deamon (smbiod)
*/
#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 <thread.h>
#define EXIT_FAIL 1
#define EXIT_OK 0
#define DPRINT(...) do \
{ \
if (smb_debug) \
} while (0)
#else
#define DPRINT(...) ((void)0)
#endif
int iod_thr_count; /* threads, excluding main */
int iod_terminating;
int
{
static const int door_attrs =
/* Debugging support. */
if (smb_debug < 1)
smb_debug = 1;
iod_alarm_time = 300;
}
/*
* Find out if an IOD is already running.
* If so, we lost a harmless startup race.
* An IOD did start, so exit success.
*/
if (err == 0) {
door_fd = -1;
DPRINT("main: already running\n");
}
/*
* Create a file for the door.
*/
if (tmp_fd < 0) {
}
tmp_fd = -1;
/*
* Close FDs 0,1,2 so we don't have a TTY, and
* used for device handles (etc.) later, and
* we don't have to worry about printf calls
* or whatever going to these FDs.
*/
for (i = 0; i < 3; i++) {
/* Exception: If smb_debug, keep stderr */
if (smb_debug && i == 2)
break;
close(i);
if (tmp_fd < 0)
if (tmp_fd != i)
}
/*
* Become session leader.
*/
setsid();
/*
* Create door service threads with signals blocked.
*/
/* Setup the door service. */
if (door_fd < 0) {
goto errout;
}
goto errout;
}
/*
* Post the initial alarm, and then just
* wait for signals.
*/
/*
* If a door call races with the alarm, ignore the alarm.
* It will be rescheduled when the threads go away.
*/
goto again;
}
iod_terminating = 1;
door_fd = -1;
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 deamon 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);
/*
* Do the initial connection setup here, so we can
* report the outcome to the door client.
*/
if (err != 0)
goto out;
/*
* Create the driver session now, so we don't
* race with the door client findvc call.
*/
goto out;
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);
}