#include <sys/types.h>
#include <sys/errno.h>
#include <sys/t_lock.h>
#include <sys/modctl.h>
#include <sys/systm.h>
#include <sys/syscall.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/cred.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/kmem.h>
#include <sys/cladm.h>
#include <sys/sunddi.h>
#include <sys/dditypes.h>
#include <sys/instance.h>
#include <sys/debug.h>
#include <sys/policy.h>
static int in_sync_sys(char *pathname, uint_t flags);
static struct sysent in_sync_sysent = {
2,
SE_ARGC | SE_32RVAL1,
in_sync_sys,
(krwlock_t *)0
};
static struct modlsys modlsys = {
&mod_syscallops, "instance binding syscall", &in_sync_sysent
};
#ifdef _SYSCALL32_IMPL
static struct modlsys modlsys32 = {
&mod_syscallops32, "32-bit instance binding syscall", &in_sync_sysent
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
static int in_write_instance(struct vnode *vp);
static int inst_sync_disable = 0;
static int
in_sync_sys(char *pathname, uint_t flags)
{
struct vnode *vp;
int error;
if (inst_sync_disable)
return (0);
if ((error = secpolicy_sys_devices(CRED())) != 0)
return (set_errno(error));
if (flags != INST_SYNC_ALWAYS && flags != INST_SYNC_IF_REQUIRED)
return (set_errno(EINVAL));
e_ddi_enter_instance();
if (e_ddi_instance_is_clean() && flags != INST_SYNC_ALWAYS) {
error = EALREADY;
goto end;
}
if ((error = vn_open(pathname, UIO_USERSPACE,
FCREAT, 0444, &vp, CRCREAT, 0)) != 0) {
if (error == EISDIR)
error = EACCES;
goto end;
}
error = in_write_instance(vp);
if (error)
(void) vn_remove(pathname, UIO_USERSPACE, RMFILE);
else
e_ddi_instance_set_clean();
end:
e_ddi_exit_instance();
return (error ? set_errno(error) : 0);
}
#define FBUFSIZE 512
typedef struct _File {
char *ptr;
int count;
char buf[FBUFSIZE];
vnode_t *vp;
offset_t voffset;
} File;
static int
in_write(struct vnode *vp, offset_t *vo, caddr_t buf, int count)
{
int error;
ssize_t resid;
rlim64_t rlimit = *vo + count + 1;
error = vn_rdwr(UIO_WRITE, vp, buf, count, *vo,
UIO_SYSSPACE, 0, rlimit, CRED(), &resid);
*vo += (offset_t)(count - resid);
return (error);
}
static File *
in_fvpopen(struct vnode *vp)
{
File *fp;
fp = kmem_zalloc(sizeof (File), KM_SLEEP);
fp->vp = vp;
fp->ptr = fp->buf;
return (fp);
}
static int
in_fclose(File *fp)
{
int error;
error = VOP_CLOSE(fp->vp, FCREAT, 1, (offset_t)0, CRED(), NULL);
VN_RELE(fp->vp);
kmem_free(fp, sizeof (File));
return (error);
}
static int
in_fflush(File *fp)
{
int error = 0;
if (fp->count)
error = in_write(fp->vp, &fp->voffset, fp->buf, fp->count);
if (error == 0)
error = VOP_FSYNC(fp->vp, FSYNC, CRED(), NULL);
return (error);
}
static int
in_fputs(File *fp, char *buf)
{
int error = 0;
while (*buf) {
*fp->ptr++ = *buf++;
if (++fp->count == FBUFSIZE) {
error = in_write(fp->vp, &fp->voffset, fp->buf,
fp->count);
if (error)
break;
fp->count = 0;
fp->ptr = fp->buf;
}
}
return (error);
}
static File *in_fp;
#define DRVNAMELEN (1 + 256)
static char linebuffer[MAXPATHLEN + 1 + 1 + 1 + 1 + 10 + 1 + DRVNAMELEN];
static int
in_walktree(in_node_t *np, char *this)
{
char *next;
int error = 0;
in_drv_t *dp;
for (error = 0; np; np = np->in_sibling) {
if (np->in_drivers == NULL)
continue;
if (np->in_unit_addr[0] == '\0')
(void) sprintf(this, "/%s", np->in_node_name);
else
(void) sprintf(this, "/%s@%s", np->in_node_name,
np->in_unit_addr);
next = this + strlen(this);
ASSERT(np->in_drivers);
for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
uint_t inst_val = dp->ind_instance;
if (dp->ind_state != IN_PERMANENT)
continue;
(void) sprintf(next, "\" %d \"%s\"\n", inst_val,
dp->ind_driver_name);
if (error = in_fputs(in_fp, linebuffer))
return (error);
}
if (np->in_child)
if (error = in_walktree(np->in_child, next))
break;
}
return (error);
}
static int
in_write_instance(struct vnode *vp)
{
int error;
char *cp;
in_fp = in_fvpopen(vp);
error = in_fputs(in_fp,
"#\n#\tCaution! This file contains critical kernel state\n#\n");
if (error == 0) {
in_node_t *root = e_ddi_instance_root();
cp = linebuffer;
*cp++ = '\"';
error = in_walktree(root->in_child, cp);
}
if (error == 0) {
if ((error = in_fflush(in_fp)) == 0)
error = in_fclose(in_fp);
} else
(void) in_fclose(in_fp);
return (error);
}