Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 31 Jul 2009 11:32:32 +0000 (UTC)
From:      Stanislav Sedov <stas@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r195990 - in projects/libprocstat: sys/kern sys/sys usr.bin/fstat
Message-ID:  <200907311132.n6VBWWbI067760@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: stas
Date: Fri Jul 31 11:32:32 2009
New Revision: 195990
URL: http://svn.freebsd.org/changeset/base/195990

Log:
  - Return information about the controlling terminal vnode in procstat_getfiles.
    This is required for fuser support.

Modified:
  projects/libprocstat/sys/kern/kern_descrip.c
  projects/libprocstat/sys/sys/user.h
  projects/libprocstat/usr.bin/fstat/fuser.c
  projects/libprocstat/usr.bin/fstat/libprocstat.c
  projects/libprocstat/usr.bin/fstat/libprocstat.h

Modified: projects/libprocstat/sys/kern/kern_descrip.c
==============================================================================
--- projects/libprocstat/sys/kern/kern_descrip.c	Fri Jul 31 09:57:42 2009	(r195989)
+++ projects/libprocstat/sys/kern/kern_descrip.c	Fri Jul 31 11:32:32 2009	(r195990)
@@ -2975,7 +2975,7 @@ sysctl_kern_proc_filedesc(SYSCTL_HANDLER
 	struct filedesc *fdp;
 	struct kinfo_file *kif;
 	struct proc *p;
-	struct vnode *tracevp, *textvp;
+	struct vnode *cttyvp, *textvp, *tracevp;
 	size_t oldidx;
 	int64_t offset;
 	void *data;
@@ -2997,6 +2997,12 @@ sysctl_kern_proc_filedesc(SYSCTL_HANDLER
 	textvp = p->p_textvp;
 	if (textvp != NULL)
 		vref(textvp);
+	/* Controlling tty. */
+	cttyvp = NULL;
+	if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
+		cttyvp = p->p_pgrp->pg_session.s_ttyvp;
+		vref(cttyvp);
+	}
 	fdp = fdhold(p);
 	PROC_UNLOCK(p);
 	kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
@@ -3006,6 +3012,9 @@ sysctl_kern_proc_filedesc(SYSCTL_HANDLER
 	if (textvp != NULL)
 		export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
 		    FREAD, -1, -1, kif, req);
+	if (cttyvp != NULL)
+		export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
+		    FREAD | FWRITE, -1, -1, kif, req);
 	if (fdp == NULL)
 		goto fail;
 	FILEDESC_SLOCK(fdp);

Modified: projects/libprocstat/sys/sys/user.h
==============================================================================
--- projects/libprocstat/sys/sys/user.h	Fri Jul 31 09:57:42 2009	(r195989)
+++ projects/libprocstat/sys/sys/user.h	Fri Jul 31 11:32:32 2009	(r195990)
@@ -278,6 +278,7 @@ struct user {
 #define	KF_FD_TYPE_JAIL	-3	/* Jail directory */
 #define	KF_FD_TYPE_TRACE	-4	/* ptrace vnode */
 #define	KF_FD_TYPE_TEXT	-5	/* Text vnode */
+#define	KF_FD_TYPE_CTTY	-6	/* Controlling terminal */
 
 #define	KF_FLAG_READ		0x00000001
 #define	KF_FLAG_WRITE		0x00000002

Modified: projects/libprocstat/usr.bin/fstat/fuser.c
==============================================================================
--- projects/libprocstat/usr.bin/fstat/fuser.c	Fri Jul 31 09:57:42 2009	(r195989)
+++ projects/libprocstat/usr.bin/fstat/fuser.c	Fri Jul 31 11:32:32 2009	(r195990)
@@ -77,7 +77,8 @@ struct {
 	{PS_FST_UFLAG_JAIL,	'j'},
 	{PS_FST_UFLAG_TRACE,	't'},
 	{PS_FST_UFLAG_TEXT,	'x'},
-	{PS_FST_UFLAG_MMAP,	'm'}
+	{PS_FST_UFLAG_MMAP,	'm'},
+	{PS_FST_UFLAG_CTTY,	'y'}
 };
 #define	NUFLAGS	(sizeof(uflags) / sizeof(*uflags))
 
@@ -135,7 +136,6 @@ printflags(struct consumer *cons)
 	for (i = 0; i < NFFLAGS; i++)
 		if ((cons->flags & fflags[i].flag) != 0)
 			fputc(fflags[i].ch, stderr);
-	/* XXX: add controlling tty support. */
 }
 
 /*

Modified: projects/libprocstat/usr.bin/fstat/libprocstat.c
==============================================================================
--- projects/libprocstat/usr.bin/fstat/libprocstat.c	Fri Jul 31 09:57:42 2009	(r195989)
+++ projects/libprocstat/usr.bin/fstat/libprocstat.c	Fri Jul 31 11:32:32 2009	(r195990)
@@ -278,6 +278,41 @@ filestat_new_entry(void *typedep, int ty
 	return (entry);
 }
 
+static struct vnode *
+getctty(kvm_t *kd, struct kinfo_proc *kp)
+{
+	struct pgrp pgrp;
+	struct proc proc;
+	struct session sess;
+	int error;
+                        
+	assert(kp);
+	error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
+	    sizeof(proc));
+	if (error == 0) {
+		warnx("can't read proc struct at %p for pid %d",
+		    kp->ki_paddr, kp->ki_pid);
+		return (NULL);
+	}
+	if (proc.p_pgrp == NULL)
+		return (NULL);
+	error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
+	    sizeof(pgrp));
+	if (error == 0) {
+		warnx("can't read pgrp struct at %p for pid %d",
+		    proc.p_pgrp, kp->ki_pid);
+		return (NULL);
+	}
+	error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
+	    sizeof(sess));
+	if (error == 0) {
+		warnx("can't read session struct at %p for pid %d",
+		    pgrp.pg_session, kp->ki_pid);
+		return (NULL);
+	}
+	return (sess.s_ttyvp);
+}
+
 static struct filestat_list *
 procstat_getfiles_kvm(kvm_t *kd, struct kinfo_proc *kp, int mmapped)
 {
@@ -286,6 +321,7 @@ procstat_getfiles_kvm(kvm_t *kd, struct 
 	struct vm_map_entry vmentry;
 	struct vm_object object;
 	struct vmspace vmspace;
+	struct vnode *vp;
 	vm_map_entry_t entryp;
 	vm_map_t map;
 	vm_object_t objp;
@@ -349,6 +385,13 @@ procstat_getfiles_kvm(kvm_t *kd, struct 
 		if (entry != NULL)
 			STAILQ_INSERT_TAIL(head, entry, next);
 	}
+	/* Controlling terminal. */
+	if ((vp = getctty(kd, kp)) != NULL) {
+		entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1,
+		    PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE, PS_FST_UFLAG_CTTY);
+		if (entry != NULL)
+			STAILQ_INSERT_TAIL(head, entry, next);
+	}
 
 	nfiles = filed.fd_lastfile + 1;
 	ofiles = malloc(nfiles * sizeof(struct file *));
@@ -541,16 +584,18 @@ kinfo_uflags2fst(int fd)
 {
 
 	switch (fd) {
+	case KF_FD_TYPE_CTTY:
+		return (PS_FST_UFLAG_CTTY);
 	case KF_FD_TYPE_CWD:
 		return (PS_FST_UFLAG_CDIR);
-	case KF_FD_TYPE_ROOT:
-		return (PS_FST_UFLAG_RDIR);
 	case KF_FD_TYPE_JAIL:
 		return (PS_FST_UFLAG_JAIL);
-	case KF_FD_TYPE_TRACE:
-		return (PS_FST_UFLAG_TRACE);
 	case KF_FD_TYPE_TEXT:
 		return (PS_FST_UFLAG_TEXT);
+	case KF_FD_TYPE_TRACE:
+		return (PS_FST_UFLAG_TRACE);
+	case KF_FD_TYPE_ROOT:
+		return (PS_FST_UFLAG_RDIR);
 	}
 	return (0);
 }

Modified: projects/libprocstat/usr.bin/fstat/libprocstat.h
==============================================================================
--- projects/libprocstat/usr.bin/fstat/libprocstat.h	Fri Jul 31 09:57:42 2009	(r195989)
+++ projects/libprocstat/usr.bin/fstat/libprocstat.h	Fri Jul 31 11:32:32 2009	(r195990)
@@ -68,6 +68,7 @@
 #define	PS_FST_UFLAG_TRACE	0x0008
 #define	PS_FST_UFLAG_TEXT	0x0010
 #define	PS_FST_UFLAG_MMAP	0x0020
+#define	PS_FST_UFLAG_CTTY	0x0040
 
 /*
  * Descriptor flags.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200907311132.n6VBWWbI067760>