Change ttyhook_register() second argument from thread to process pointer.

Thread was not really needed there, while previous ng_tty implementation
that used thread pointer had locking issues (using sx while holding mutex).
This commit is contained in:
Alexander Motin 2008-12-13 21:17:46 +00:00
parent 68124cc7f8
commit a9385ad10f
4 changed files with 19 additions and 11 deletions

View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <sys/malloc.h> #include <sys/malloc.h>
#include <sys/module.h> #include <sys/module.h>
#include <sys/poll.h> #include <sys/poll.h>
#include <sys/proc.h>
#include <sys/snoop.h> #include <sys/snoop.h>
#include <sys/sx.h> #include <sys/sx.h>
#include <sys/systm.h> #include <sys/systm.h>
@ -246,7 +247,7 @@ snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
sx_xunlock(&snp_register_lock); sx_xunlock(&snp_register_lock);
return (EBUSY); return (EBUSY);
} }
error = ttyhook_register(&ss->snp_tty, td, *(int *)data, error = ttyhook_register(&ss->snp_tty, td->td_proc, *(int *)data,
&snp_hook, ss); &snp_hook, ss);
sx_xunlock(&snp_register_lock); sx_xunlock(&snp_register_lock);
if (error != 0) if (error != 0)

View File

@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
#include <sys/cons.h> #include <sys/cons.h>
#include <sys/fcntl.h> #include <sys/fcntl.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/filio.h> #include <sys/filio.h>
#ifdef COMPAT_43TTY #ifdef COMPAT_43TTY
#include <sys/ioctl_compat.h> #include <sys/ioctl_compat.h>
@ -1673,18 +1674,24 @@ ttyhook_defrint(struct tty *tp, char c, int flags)
} }
int int
ttyhook_register(struct tty **rtp, struct thread *td, int fd, ttyhook_register(struct tty **rtp, struct proc *p, int fd,
struct ttyhook *th, void *softc) struct ttyhook *th, void *softc)
{ {
struct tty *tp; struct tty *tp;
struct file *fp; struct file *fp;
struct cdev *dev; struct cdev *dev;
struct cdevsw *cdp; struct cdevsw *cdp;
struct filedesc *fdp;
int error; int error;
/* Validate the file descriptor. */ /* Validate the file descriptor. */
if (fget(td, fd, &fp) != 0) if ((fdp = p->p_fd) == NULL)
return (EINVAL); return (EBADF);
FILEDESC_SLOCK(fdp);
if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
FILEDESC_SUNLOCK(fdp);
return (EBADF);
}
/* Make sure the vnode is bound to a character device. */ /* Make sure the vnode is bound to a character device. */
error = EINVAL; error = EINVAL;
@ -1723,7 +1730,7 @@ ttyhook_register(struct tty **rtp, struct thread *td, int fd,
done3: tty_unlock(tp); done3: tty_unlock(tp);
done2: dev_relthread(dev); done2: dev_relthread(dev);
done1: fdrop(fp, td); done1: FILEDESC_SUNLOCK(fdp);
return (error); return (error);
} }

View File

@ -252,7 +252,6 @@ static int
ngt_rcvmsg(node_p node, item_p item, hook_p lasthook) ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
{ {
struct proc *p; struct proc *p;
struct thread *td;
const sc_p sc = NG_NODE_PRIVATE(node); const sc_p sc = NG_NODE_PRIVATE(node);
struct ng_mesg *msg, *resp = NULL; struct ng_mesg *msg, *resp = NULL;
int error = 0; int error = 0;
@ -266,12 +265,13 @@ ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
return (EBUSY); return (EBUSY);
p = pfind(((int *)msg->data)[0]); p = pfind(((int *)msg->data)[0]);
if (p == NULL) if (p == NULL || (p->p_flag & P_WEXIT))
return (ESRCH); return (ESRCH);
td = FIRST_THREAD_IN_PROC(p); _PHOLD(p);
error = ttyhook_register(&sc->tp, td, ((int *)msg->data)[1],
&ngt_hook, sc);
PROC_UNLOCK(p); PROC_UNLOCK(p);
error = ttyhook_register(&sc->tp, p, ((int *)msg->data)[1],
&ngt_hook, sc);
PRELE(p);
if (error != 0) if (error != 0)
return (error); return (error);
break; break;

View File

@ -66,7 +66,7 @@ struct ttyhook {
th_close_t *th_close; th_close_t *th_close;
}; };
int ttyhook_register(struct tty **, struct thread *, int, int ttyhook_register(struct tty **, struct proc *, int,
struct ttyhook *, void *); struct ttyhook *, void *);
void ttyhook_unregister(struct tty *); void ttyhook_unregister(struct tty *);
#define ttyhook_softc(tp) ((tp)->t_hooksoftc) #define ttyhook_softc(tp) ((tp)->t_hooksoftc)