#include <sys/types.h>
#include <sys/stat.h>
#include <sys/note.h>
#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>
#include <netsmb/smb_lib.h>
#define DPRINT(...) do \
{ \
if (smb_debug) \
fprintf(stderr, __VA_ARGS__); \
_NOTE(CONSTCOND) \
} while (0)
mutex_t iod_mutex = DEFAULTMUTEX;
int iod_thr_count;
int iod_terminating;
int iod_alarm_time = 30;
void iod_dispatch(void *cookie, char *argp, size_t argsz,
door_desc_t *dp, uint_t n_desc);
int iod_newvc(smb_iod_ssn_t *clnt_ssn);
void * iod_work(void *arg);
int
main(int argc, char **argv)
{
sigset_t oldmask, tmpmask;
char *env, *door_path = NULL;
int door_fd = -1;
int err, sig;
int rc = SMF_EXIT_ERR_FATAL;
boolean_t attached = B_FALSE;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
if ((env = getenv("SMBFS_DEBUG")) != NULL) {
smb_debug = atoi(env);
if (smb_debug < 1)
smb_debug = 1;
iod_alarm_time = 300;
}
err = smb_iod_open_door(&door_fd);
if (err == 0) {
close(door_fd);
door_fd = -1;
DPRINT("%s: already running\n", argv[0]);
exit(SMF_EXIT_OK);
}
sigfillset(&tmpmask);
sigprocmask(SIG_BLOCK, &tmpmask, &oldmask);
door_path = smb_iod_door_path();
door_fd = door_create(iod_dispatch, NULL,
DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
if (door_fd == -1) {
perror("iod door_create");
goto out;
}
fdetach(door_path);
if (fattach(door_fd, door_path) < 0) {
fprintf(stderr, "%s: fattach failed, %s\n",
door_path, strerror(errno));
goto out;
}
attached = B_TRUE;
rc = SMF_EXIT_OK;
alarm(iod_alarm_time);
again:
sig = sigwait(&tmpmask);
DPRINT("main: sig=%d\n", sig);
switch (sig) {
case SIGCONT:
goto again;
case SIGALRM:
mutex_lock(&iod_mutex);
if (iod_thr_count > 0) {
mutex_unlock(&iod_mutex);
goto again;
}
iod_terminating = 1;
mutex_unlock(&iod_mutex);
break;
case SIGINT:
case SIGTERM:
break;
default:
fprintf(stderr, "iod_main: unexpected sig=%d\n", sig);
break;
}
out:
iod_terminating = 1;
if (attached)
fdetach(door_path);
if (door_fd != -1)
door_revoke(door_fd);
free(NULL);
return (rc);
}
void
iod_dispatch(void *cookie, char *argp, size_t argsz,
door_desc_t *dp, uint_t n_desc)
{
smb_iod_ssn_t *ssn;
ucred_t *ucred;
uid_t cl_uid;
int rc;
ucred = NULL;
if (door_ucred(&ucred) != 0) {
rc = EACCES;
goto out;
}
cl_uid = ucred_getruid(ucred);
ucred_free(ucred);
ucred = NULL;
if (cl_uid != getuid()) {
DPRINT("iod_dispatch: wrong UID\n");
rc = EACCES;
goto out;
}
if (argp == NULL) {
rc = 0;
goto out;
}
if (argsz != sizeof (*ssn)) {
rc = EINVAL;
goto out;
}
mutex_lock(&iod_mutex);
if (iod_terminating) {
mutex_unlock(&iod_mutex);
DPRINT("iod_dispatch: terminating\n");
rc = EINTR;
goto out;
}
if (iod_thr_count++ == 0) {
alarm(0);
DPRINT("iod_dispatch: cancelled alarm\n");
}
mutex_unlock(&iod_mutex);
ssn = (void *) argp;
rc = iod_newvc(ssn);
mutex_lock(&iod_mutex);
if (--iod_thr_count == 0) {
DPRINT("iod_dispatch: schedule alarm\n");
alarm(iod_alarm_time);
}
mutex_unlock(&iod_mutex);
out:
door_return((void *)&rc, sizeof (rc), NULL, 0);
}
int
iod_newvc(smb_iod_ssn_t *clnt_ssn)
{
smb_ctx_t *ctx;
thread_t tid;
int err;
err = smb_ctx_alloc(&ctx);
if (err)
return (err);
bcopy(clnt_ssn, &ctx->ct_iod_ssn, sizeof (ctx->ct_iod_ssn));
if ((err = smb_ctx_gethandle(ctx)) != 0)
goto out;
if (ioctl(ctx->ct_dev_fd, SMBIOC_SSN_CREATE, &ctx->ct_ssn) < 0) {
err = errno;
if (err == EEXIST)
err = 0;
goto out;
}
err = smb_iod_connect(ctx);
if (err != 0)
goto out;
err = thr_create(NULL, 0, iod_work, ctx, THR_DETACHED, &tid);
if (err == 0) {
ctx = NULL;
}
out:
if (ctx)
smb_ctx_free(ctx);
return (err);
}
void *
iod_work(void *arg)
{
smb_ctx_t *ctx = arg;
mutex_lock(&iod_mutex);
if (iod_thr_count++ == 0) {
alarm(0);
DPRINT("iod_work: cancelled alarm\n");
}
mutex_unlock(&iod_mutex);
(void) smb_iod_work(ctx);
mutex_lock(&iod_mutex);
if (--iod_thr_count == 0) {
DPRINT("iod_work: schedule alarm\n");
alarm(iod_alarm_time);
}
mutex_unlock(&iod_mutex);
smb_ctx_free(ctx);
return (NULL);
}