tech-kern archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
kern/42030 - tracking of file descriptors by ktrace/kdump
Hi,
Can I commit the patches from my kern/42030?
From kern/42030:
> Sometimes it's useful to know a list of opened file descriptors on
> program start. For instance, the output below shows that mailwrapper
> inherits 4 extra descriptors from its parent:
>
>
> $ env -i ./ktrace -id -tafv mail xxx%yyy.zzz@localhost
> $ kdump
> 686 1 ktrace ARG "mail"
> 686 1 ktrace ARG "xxx%yyy.zzz@localhost"
> 686 1 mail FD file 0
> 686 1 mail FD file 1
> 686 1 mail FD file 2
> 687 1 mail ARG "sendmail"
> 687 1 mail ARG "-i"
> 687 1 mail ARG "xxx%yyy.zzz@localhost"
> 687 1 mailwrapper FD file 0
> 687 1 mailwrapper FD file 1
> 687 1 mailwrapper FD file 2
> 687 1 mailwrapper FD file 3
> 687 1 mailwrapper FD file 4
> 687 1 mailwrapper FD file 5
> 687 1 mailwrapper FD file 6
> 687 1 mailwrapper ARG "sendmail"
> 687 1 mailwrapper ARG "-i"
> 687 1 mailwrapper ARG "xxx%yyy.zzz@localhost"
> 687 1 sendmail FD file 0
> 687 1 sendmail FD file 1
> 687 1 sendmail FD file 2
> 687 1 sendmail FD file 3
> 687 1 sendmail FD file 4
> 687 1 sendmail FD file 5
> 687 1 sendmail FD file 6
> 434 1 sendmail ARG "/usr/sbin/postdrop"
> 434 1 sendmail ARG "-r"
> 434 1 sendmail ENV "PATH=/usr/bin:/bin:/usr/pkg/bin:/usr/local/bin"
> 434 1 postdrop FD socket 0
> 434 1 postdrop FD socket 1
> 434 1 postdrop FD file 2
Thanks,
Alex
Index: sys/sys/filedesc.h
===================================================================
RCS file: /cvsroot/src/sys/sys/filedesc.h,v
retrieving revision 1.59
diff -p -u -u -r1.59 filedesc.h
--- sys/sys/filedesc.h 10 Apr 2011 15:45:33 -0000 1.59
+++ sys/sys/filedesc.h 30 May 2011 23:06:49 -0000
@@ -193,6 +193,7 @@ void fd_share(proc_t *);
void fd_hold(lwp_t *);
void fd_free(void);
void fd_closeexec(void);
+void fd_ktrexecfd(void);
int fd_checkstd(void);
file_t *fd_getfile(unsigned);
file_t *fd_getfile2(proc_t *, unsigned);
Index: sys/sys/ktrace.h
===================================================================
RCS file: /cvsroot/src/sys/sys/ktrace.h,v
retrieving revision 1.57
diff -p -u -u -r1.57 ktrace.h
--- sys/sys/ktrace.h 27 Apr 2011 00:00:46 -0000 1.57
+++ sys/sys/ktrace.h 30 May 2011 23:06:49 -0000
@@ -217,6 +217,14 @@ struct ktr_saupcall {
#define KTR_MIB 14
/* Record contains MIB name */
+/*
+ * KTR_EXEC_FD - Opened file descriptor from exec
+ */
+#define KTR_EXEC_FD 15
+struct ktr_execfd {
+ int ktr_fd;
+ u_int ktr_dtype; /* one of DTYPE_* constants */
+};
/*
* kernel trace points (in p_traceflag)
@@ -234,6 +242,7 @@ struct ktr_saupcall {
#define KTRFAC_EXEC_ENV (1<<KTR_EXEC_ENV)
#define KTRFAC_SAUPCALL (1<<KTR_SAUPCALL)
#define KTRFAC_MIB (1<<KTR_MIB)
+#define KTRFAC_EXEC_FD (1<<KTR_EXEC_FD)
/*
* trace flags (also in p_traceflags)
*/
@@ -286,6 +295,7 @@ void ktr_kuser(const char *, void *, siz
void ktr_mib(const int *a , u_int b);
void ktr_execarg(const void *, size_t);
void ktr_execenv(const void *, size_t);
+void ktr_execfd(int, u_int);
void ktr_saupcall(struct lwp *, int, int, int, void *, void *, void *);
static inline bool
@@ -393,6 +403,13 @@ ktrexecenv(const void *a, size_t b)
}
static inline void
+ktrexecfd(int fd, u_int dtype)
+{
+ if (__predict_false(ktrace_on))
+ ktr_execfd(fd, dtype);
+}
+
+static inline void
ktrsaupcall(struct lwp *a, int b, int c, int d, void *e, void *f, void *g)
{
if (__predict_false(ktrace_on))
Index: sys/kern/kern_exec.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_exec.c,v
retrieving revision 1.314
diff -p -u -u -r1.314 kern_exec.c
--- sys/kern/kern_exec.c 26 Apr 2011 16:36:42 -0000 1.314
+++ sys/kern/kern_exec.c 30 May 2011 23:06:49 -0000
@@ -979,6 +979,10 @@ execve1(struct lwp *l, const char *path,
cwdexec(p);
fd_closeexec(); /* handle close on exec */
+
+ if (__predict_false(ktrace_on))
+ fd_ktrexecfd();
+
execsigs(p); /* reset catched signals */
l->l_ctxlink = NULL; /* reset ucontext link */
Index: sys/kern/kern_ktrace.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_ktrace.c,v
retrieving revision 1.156
diff -p -u -u -r1.156 kern_ktrace.c
--- sys/kern/kern_ktrace.c 27 Apr 2011 00:00:46 -0000 1.156
+++ sys/kern/kern_ktrace.c 30 May 2011 23:06:49 -0000
@@ -673,6 +673,25 @@ ktr_execenv(const void *bf, size_t len)
ktr_kmem(l, KTR_EXEC_ENV, bf, len);
}
+void
+ktr_execfd(int fd, u_int dtype)
+{
+ struct ktrace_entry *kte;
+ struct ktr_execfd* ktp;
+
+ lwp_t *l = curlwp;
+
+ if (!KTRPOINT(l->l_proc, KTR_EXEC_FD))
+ return;
+
+ if (ktealloc(&kte, (void *)&ktp, l, KTR_EXEC_FD, sizeof(*ktp)))
+ return;
+
+ ktp->ktr_fd = fd;
+ ktp->ktr_dtype = dtype;
+ ktraddentry(l, kte, KTA_WAITOK);
+}
+
static void
ktr_kmem(lwp_t *l, int type, const void *bf, size_t len)
{
Index: sys/kern/kern_stub.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_stub.c,v
retrieving revision 1.31
diff -p -u -u -r1.31 kern_stub.c
--- sys/kern/kern_stub.c 27 Apr 2011 00:00:46 -0000 1.31
+++ sys/kern/kern_stub.c 30 May 2011 23:06:49 -0000
@@ -110,6 +110,7 @@ __weak_alias(ktr_kuser,nullop);
__weak_alias(ktr_mib,nullop);
__weak_alias(ktr_execarg,nullop);
__weak_alias(ktr_execenv,nullop);
+__weak_alias(ktr_execfd,nullop);
__weak_alias(sys_fktrace,sys_nosys); /* Syscalls */
__weak_alias(sys_ktrace,sys_nosys);
Index: sys/kern/subr_exec_fd.c
===================================================================
RCS file: /cvsroot/src/sys/kern/subr_exec_fd.c,v
retrieving revision 1.5
diff -p -u -u -r1.5 subr_exec_fd.c
--- sys/kern/subr_exec_fd.c 15 Feb 2011 15:54:28 -0000 1.5
+++ sys/kern/subr_exec_fd.c 30 May 2011 23:06:49 -0000
@@ -36,6 +36,35 @@ __KERNEL_RCSID(0, "$NetBSD: subr_exec_fd
#include <sys/namei.h>
#include <sys/syslog.h>
#include <sys/vnode.h>
+#include <sys/ktrace.h>
+
+void
+fd_ktrexecfd(void)
+{
+ proc_t *p;
+ filedesc_t *fdp;
+ fdfile_t *ff;
+ lwp_t *l;
+ fdtab_t *dt;
+ int fd;
+
+ l = curlwp;
+ p = l->l_proc;
+ fdp = p->p_fd;
+ dt = fdp->fd_dt;
+
+ for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
+ if ((ff = dt->dt_ff[fd]) == NULL) {
+ KASSERT(fd >= NDFDFILE);
+ continue;
+ }
+ KASSERT(fd >= NDFDFILE ||
+ ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
+ if (ff->ff_file == NULL)
+ continue;
+ ktr_execfd(fd, ff->ff_file->f_type);
+ }
+}
/*
* It is unsafe for set[ug]id processes to be started with file
Index: usr.bin/ktrace/ktrace.1
===================================================================
RCS file: /cvsroot/src/usr.bin/ktrace/ktrace.1,v
retrieving revision 1.40
diff -p -u -u -r1.40 ktrace.1
--- usr.bin/ktrace/ktrace.1 27 Apr 2011 00:00:47 -0000 1.40
+++ usr.bin/ktrace/ktrace.1 30 May 2011 23:07:14 -0000
@@ -181,6 +181,8 @@ trace exec arguments
trace system calls
.It Cm e
trace emulation changes
+.It Cm f
+trace open file descriptors after exec
.It Cm i
trace
.Tn I/O
Index: usr.bin/ktrace/subr.c
===================================================================
RCS file: /cvsroot/src/usr.bin/ktrace/subr.c,v
retrieving revision 1.17
diff -p -u -u -r1.17 subr.c
--- usr.bin/ktrace/subr.c 27 Apr 2011 00:00:47 -0000 1.17
+++ usr.bin/ktrace/subr.c 30 May 2011 23:07:14 -0000
@@ -73,6 +73,9 @@ getpoints(int facs, char *s)
case 'e':
fac = KTRFAC_EMUL;
break;
+ case 'f':
+ fac = KTRFAC_EXEC_FD;
+ break;
case 'i':
fac = KTRFAC_GENIO;
break;
Index: usr.bin/kdump/kdump.c
===================================================================
RCS file: /cvsroot/src/usr.bin/kdump/kdump.c,v
retrieving revision 1.111
diff -p -u -u -r1.111 kdump.c
--- usr.bin/kdump/kdump.c 27 Apr 2011 00:00:47 -0000 1.111
+++ usr.bin/kdump/kdump.c 30 May 2011 23:07:15 -0000
@@ -44,7 +44,9 @@ __RCSID("$NetBSD: kdump.c,v 1.111 2011/0
#endif /* not lint */
#include <sys/param.h>
+#include <sys/proc.h> /* XXX #include <sys/file.h> fails without this header */
#define _KERNEL
+#include <sys/file.h>
#include <sys/errno.h>
#undef _KERNEL
#include <sys/time.h>
@@ -115,6 +117,7 @@ static void ktrpsig(void *, int);
static void ktrcsw(struct ktr_csw *);
static void ktruser(struct ktr_user *, int);
static void ktrmib(int *, int);
+static void ktrexecfd(struct ktr_execfd *);
static void usage(void) __dead;
static void eprint(int);
static void rprint(register_t);
@@ -294,6 +297,9 @@ main(int argc, char **argv)
case KTR_EXEC_ENV:
visdump_buf(m, ktrlen, col);
break;
+ case KTR_EXEC_FD:
+ ktrexecfd(m);
+ break;
case KTR_MIB:
ktrmib(m, ktrlen);
break;
@@ -363,6 +369,9 @@ dumpheader(struct ktr_header *kth)
case KTR_EXEC_ARG:
type = "ARG";
break;
+ case KTR_EXEC_FD:
+ type = "FD";
+ break;
case KTR_SAUPCALL:
type = "SAU";
break;
@@ -632,6 +641,17 @@ ktrsysret(struct ktr_sysret *ktr, int le
}
static void
+ktrexecfd(struct ktr_execfd *ktr)
+{
+ static const char *dnames[] = { DTYPE_NAMES };
+ const size_t dnames_sz = sizeof(dnames) / sizeof(dnames[0]);
+ if (ktr->ktr_dtype > 0 && ktr->ktr_dtype < dnames_sz)
+ printf("%s %d\n", dnames[ktr->ktr_dtype], ktr->ktr_fd);
+ else
+ printf("%d\n", ktr->ktr_fd);
+}
+
+static void
rprint(register_t ret)
{
Home |
Main Index |
Thread Index |
Old Index