#include "mt.h"
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/stream.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/timod.h>
#include <xti.h>
#include <signal.h>
#include <syslog.h>
#include <assert.h>
#include "tx.h"
int
_tx_accept(
int fd,
int resfd,
const struct t_call *call,
int api_semantics
)
{
struct T_conn_res *cres;
struct strfdinsert strfdinsert;
int size, retval, sv_errno;
struct _ti_user *tiptr;
struct _ti_user *restiptr;
sigset_t mask;
struct strbuf ctlbuf;
int didalloc;
t_scalar_t conn_res_prim;
if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
return (-1);
if ((restiptr = _t_checkfd(resfd, 0, api_semantics)) == NULL)
return (-1);
(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
sig_mutex_lock(&tiptr->ti_lock);
if (tiptr->ti_servtype == T_CLTS) {
t_errno = TNOTSUPPORT;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
if (_T_IS_XTI(api_semantics)) {
if ((fd == resfd && tiptr->ti_state != T_INCON) ||
(fd != resfd &&
((tiptr->ti_state != T_INCON) ||
!(restiptr->ti_state == T_IDLE ||
restiptr->ti_state == T_UNBND)))) {
t_errno = TOUTSTATE;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
if (fd != resfd && restiptr->ti_state == T_IDLE &&
restiptr->ti_qlen > 0) {
t_errno = TRESQLEN;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
if (fd == resfd && tiptr->ti_ocnt > 1) {
t_errno = TINDOUT;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
}
if (fd != resfd) {
if ((retval = ioctl(resfd, I_NREAD, &size)) < 0) {
sv_errno = errno;
t_errno = TSYSERR;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
errno = sv_errno;
return (-1);
}
if (retval > 0) {
t_errno = TBADF;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (-1);
}
}
if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
sv_errno = errno;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
errno = sv_errno;
return (-1);
}
if (_T_API_VER_LT(api_semantics, TX_XTI_XNS5_API)) {
if (_t_is_event(fd, tiptr) < 0)
goto err_out;
}
cres = (struct T_conn_res *)ctlbuf.buf;
cres->OPT_length = call->opt.len;
cres->OPT_offset = 0;
cres->SEQ_number = call->sequence;
if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
cres->ACCEPTOR_id = restiptr->acceptor_id;
cres->PRIM_type = conn_res_prim = T_CONN_RES;
} else {
cres->ACCEPTOR_id = 0;
cres->PRIM_type = conn_res_prim = O_T_CONN_RES;
}
size = (int)sizeof (struct T_conn_res);
if (call->opt.len) {
if (_t_aligned_copy(&ctlbuf, call->opt.len, size,
call->opt.buf, &cres->OPT_offset) < 0) {
t_errno = TBADOPT;
goto err_out;
}
size = cres->OPT_offset + cres->OPT_length;
}
if (call->udata.len) {
if ((tiptr->ti_cdatasize == T_INVALID ) ||
((tiptr->ti_cdatasize != T_INFINITE ) &&
(call->udata.len > (uint32_t)tiptr->ti_cdatasize))) {
t_errno = TBADDATA;
goto err_out;
}
}
ctlbuf.len = size;
if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) {
if (putmsg(fd, &ctlbuf,
(struct strbuf *)(call->udata.len? &call->udata: NULL), 0) <
0) {
if (errno == EAGAIN)
t_errno = TFLOW;
else
t_errno = TSYSERR;
goto err_out;
}
} else {
strfdinsert.ctlbuf.maxlen = ctlbuf.maxlen;
strfdinsert.ctlbuf.len = ctlbuf.len;
strfdinsert.ctlbuf.buf = ctlbuf.buf;
strfdinsert.databuf.maxlen = call->udata.maxlen;
strfdinsert.databuf.len =
(call->udata.len? call->udata.len: -1);
strfdinsert.databuf.buf = call->udata.buf;
strfdinsert.fildes = resfd;
strfdinsert.offset = (int)sizeof (t_scalar_t);
strfdinsert.flags = 0;
if (ioctl(fd, I_FDINSERT, &strfdinsert) < 0) {
if (errno == EAGAIN)
t_errno = TFLOW;
else
t_errno = TSYSERR;
goto err_out;
}
}
if (_t_is_ok(fd, tiptr, conn_res_prim) < 0) {
if (_T_IS_TLI(api_semantics) && t_errno == TPROVMISMATCH) {
t_errno = TBADF;
}
goto err_out;
}
if (tiptr->ti_ocnt == 1) {
if (fd == resfd) {
_T_TX_NEXTSTATE(T_ACCEPT1, tiptr,
"t_accept: invalid state event T_ACCEPT1");
} else {
_T_TX_NEXTSTATE(T_ACCEPT2, tiptr,
"t_accept: invalid state event T_ACCEPT2");
sig_mutex_lock(&restiptr->ti_lock);
_T_TX_NEXTSTATE(T_PASSCON, restiptr,
"t_accept: invalid state event T_PASSCON");
sig_mutex_unlock(&restiptr->ti_lock);
}
} else {
_T_TX_NEXTSTATE(T_ACCEPT3, tiptr,
"t_accept: invalid state event T_ACCEPT3");
if (fd != resfd)
sig_mutex_lock(&restiptr->ti_lock);
_T_TX_NEXTSTATE(T_PASSCON, restiptr,
"t_accept: invalid state event T_PASSCON");
if (fd != resfd)
sig_mutex_unlock(&restiptr->ti_lock);
}
tiptr->ti_ocnt--;
tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
if (restiptr->ti_tsdusize != 0) {
if (_t_do_postconn_sync(resfd, restiptr) < 0)
goto err_out;
}
if (didalloc)
free(ctlbuf.buf);
else
tiptr->ti_ctlbuf = ctlbuf.buf;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
return (0);
err_out:
sv_errno = errno;
if (didalloc)
free(ctlbuf.buf);
else
tiptr->ti_ctlbuf = ctlbuf.buf;
sig_mutex_unlock(&tiptr->ti_lock);
(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
errno = sv_errno;
return (-1);
}