#include <sys/param.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/strsubr.h>
#include <sys/dlpi.h>
#include <sys/vnode.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/cmn_err.h>
#include <net/if.h>
#include <sys/sad.h>
#include <sys/kstr.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunldi.h>
#include <sys/cred.h>
#include <sys/sysmacros.h>
#include <sys/modctl.h>
int
kstr_open(major_t maj, minor_t min, vnode_t **vpp, int *fd)
{
vnode_t *vp;
int error;
vp = makespecvp(makedevice(maj, min), VCHR);
if (fd != NULL)
error = fassign(&vp, FREAD|FWRITE, fd);
else
error = VOP_OPEN(&vp, FREAD|FWRITE, CRED(), NULL);
if (vpp != NULL)
*vpp = vp;
return (error);
}
int
kstr_plink(vnode_t *vp, int fd, int *mux_id)
{
int id;
int error;
if (error = strioctl(vp, I_PLINK, (intptr_t)fd, 0, K_TO_K, CRED(), &id))
return (error);
if (mux_id)
*mux_id = id;
return (0);
}
int
kstr_unplink(vnode_t *vp, int mux_id)
{
int rval;
return (strioctl(vp, I_PUNLINK, (intptr_t)mux_id, 0,
K_TO_K, CRED(), &rval));
}
int
kstr_push(vnode_t *vp, char *mod)
{
int rval;
return (strioctl(vp, I_PUSH, (intptr_t)mod, 0, K_TO_K, CRED(), &rval));
}
int
kstr_pop(vnode_t *vp)
{
int rval;
return (strioctl(vp, I_POP, 0, 0, K_TO_K, CRED(), &rval));
}
int
kstr_close(vnode_t *vp, int fd)
{
int ret;
if (vp == (vnode_t *)NULL && fd == -1)
return (EINVAL);
if (fd != -1) {
if (closeandsetf(fd, NULL) == 0) {
return (0);
} else {
return (EINVAL);
}
} else {
ret = VOP_CLOSE(vp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL);
VN_RELE(vp);
return (ret);
}
}
int
kstr_ioctl(struct vnode *vp, int cmd, intptr_t arg)
{
int rval;
return (strioctl(vp, cmd, arg, 0, K_TO_K, CRED(), &rval));
}
int
kstr_msg(vnode_t *vp, mblk_t *smp, mblk_t **rmp, timestruc_t *timeo)
{
int error;
clock_t timout;
uchar_t pri;
int pflag;
rval_t rval;
if (rmp == NULL && timeo != NULL &&
(timeo->tv_sec != 0 || timeo->tv_nsec != 0))
return (EINVAL);
if (smp == NULL && rmp == NULL)
return (EINVAL);
if (smp != NULL) {
(void) kstrputmsg(vp, smp, NULL, 0, 0,
MSG_BAND | MSG_HOLDSIG | MSG_IGNERROR, 0);
}
if (rmp == NULL) {
return (0);
}
if (timeo != NULL) {
timout = timeo->tv_sec * 1000 + timeo->tv_nsec / 1000000;
if (timout > INT_MAX)
return (EINVAL);
} else
timout = -1;
pflag = MSG_ANY;
pri = 0;
*rmp = NULL;
error = kstrgetmsg(vp, rmp, NULL, &pri, &pflag, timout, &rval);
if (error == ETIME)
error = 0;
return (error);
}
#define SAD_ADM "/devices/pseudo/sad@0:admin"
#define SAD_USR "/devices/pseudo/sad@0:user"
int
kstr_autopush(int op, major_t *maj, minor_t *min, minor_t *lastmin,
uint_t *anchor, char *mods[])
{
ldi_handle_t lh;
ldi_ident_t li;
struct strapush push;
int i, error, rval;
li = ldi_ident_from_anon();
if (op == SET_AUTOPUSH || op == CLR_AUTOPUSH) {
error = ldi_open_by_name(SAD_ADM, FREAD|FWRITE,
kcred, &lh, li);
if (error) {
printf("kstr_autopush: open failed error %d\n", error);
ldi_ident_release(li);
return (error);
}
} else {
error = ldi_open_by_name(SAD_USR, FREAD|FWRITE,
kcred, &lh, li);
if (error) {
printf("kstr_autopush: open failed error %d\n", error);
ldi_ident_release(li);
return (error);
}
}
ldi_ident_release(li);
switch (op) {
case GET_AUTOPUSH:
push.sap_major = *maj;
push.sap_minor = *min;
error = ldi_ioctl(lh, SAD_GAP, (intptr_t)&push,
FKIOCTL, kcred, &rval);
if (error) {
printf("kstr_autopush: "
"ioctl(GET_AUTOPUSH) failed, error %d\n", error);
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (error);
}
switch (push.sap_cmd) {
case SAP_ONE:
*maj = push.sap_major;
*min = push.sap_minor;
*lastmin = 0;
break;
case SAP_RANGE:
*maj = push.sap_major;
*min = push.sap_minor;
*lastmin = push.sap_lastminor;
break;
case SAP_ALL:
*maj = push.sap_major;
*min = (minor_t)-1;
break;
}
if (anchor != NULL)
*anchor = push.sap_anchor;
if (push.sap_npush > 1) {
for (i = 0; i < push.sap_npush &&
mods[i] != NULL; i++)
(void) strcpy(mods[i], push.sap_list[i]);
mods[i] = NULL;
}
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (0);
case CLR_AUTOPUSH:
push.sap_cmd = SAP_CLEAR;
push.sap_minor = *min;
push.sap_major = *maj;
error = ldi_ioctl(lh, SAD_SAP, (intptr_t)&push,
FKIOCTL, kcred, &rval);
if (error) {
printf("kstr_autopush: "
"ioctl(CLR_AUTOPUSH) failed, error %d\n", error);
}
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (error);
case SET_AUTOPUSH:
if (*min == (minor_t)-1) {
push.sap_cmd = SAP_ALL;
} else if (*lastmin == 0) {
push.sap_cmd = SAP_ONE;
} else {
push.sap_cmd = SAP_RANGE;
}
if (anchor != NULL)
push.sap_anchor = *anchor;
else
push.sap_anchor = 0;
push.sap_minor = *min;
push.sap_major = *maj;
if (lastmin)
push.sap_lastminor = *lastmin;
else
push.sap_lastminor = 0;
for (i = 0; i < MAXAPUSH && mods[i] != (char *)NULL; i++) {
(void) strcpy(push.sap_list[i], mods[i]);
}
push.sap_npush = i;
push.sap_list[i][0] = '\0';
error = ldi_ioctl(lh, SAD_SAP, (intptr_t)&push,
FKIOCTL, kcred, &rval);
if (error) {
printf("kstr_autopush: "
"ioctl(SET_AUTOPUSH) failed, error %d\n", error);
}
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (error);
default:
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (EINVAL);
}
}