Subject: bin/17643: ktrace leaves a stale 'ktrace.out' if there is no KTRACE support in the kernel
To: None <gnats-bugs@gnats.netbsd.org>
From: None <jmcneill@netbsd.org>
List: netbsd-bugs
Date: 07/19/2002 02:14:55
>Number:         17643
>Category:       bin
>Synopsis:       ktrace creates an empty 'ktrace.out' if KTRACE isn't built into the kernel
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jul 18 22:15:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Jared D. McNeill
>Release:        NetBSD 1.6C
>Organization:
	
>Environment:
	
	
System: NetBSD sun.invisible.ca 1.6C NetBSD 1.6C (SUN) #3: Sun Jul 7 15:18:47 ADT 2002 jmcneill@sun.invisible.ca:/export/sources/current/src/sys/arch/i386/compile/SUN i386
Architecture: i386
Machine: i386
>Description:
	When running 'ktrace' on a kernel without options KTRACE, the file
	'ktrace.out' is still created.
	
>How-To-Repeat:
	1. Boot a kernel without options KTRACE
	2. $ ktrace ls
	3. The message 'ktrace() system call is not supported in the running
	   kernel [...]' is displayed.
	4. ls -l ktrace.out
	   This file now exists.
	
>Fix:
	Judging by the code, 'ktrace.out' is created before the application
	checks for ktrace() functionality. ktrace overwrites any existing
	'ktrace.out' file when it creates its' new one, so will simply
	unlinking the newly created 'ktrace.out' be acceptable?

	Can someone with more experience in this area please review this
	patch for me before I commit it? Thanks.
		- Jared

Index: ktrace.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/ktrace/ktrace.c,v
retrieving revision 1.24
diff -u -r1.24 ktrace.c
--- ktrace.c	2001/09/02 23:18:01	1.24
+++ ktrace.c	2002/07/19 05:07:17
@@ -79,6 +79,8 @@
 extern int timestamp, decimal, fancy, tail, maxdata;
 #endif
 
+static const char *ptracefile = NULL;
+
 int
 main(argc, argv)
 	int argc;
@@ -207,6 +209,12 @@
 	if (inherit)
 		trpoints |= KTRFAC_INHERIT;
 
+	/*
+	 * Incase we need to remove the newly-created tracefile later,
+	 * store a pointer to its' filename
+	 */
+	ptracefile = outfile;
+
 	(void)signal(SIGSYS, no_ktrace);
 	if (clear != NOTSET) {
 		if (clear == CLEARALL) {
@@ -284,6 +292,12 @@
 {
         (void)fprintf(stderr,
 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
+	if (ptracefile != NULL) {
+		(void)fprintf(stderr, "\tremoving %s\n", ptracefile);
+		if (unlink(ptracefile)) {
+			perror("unlink");
+		}
+	}
         exit(1);
 }
 
>Release-Note:
>Audit-Trail:
>Unformatted: