Subject: test change to ktruss please
To: None <current-users@netbsd.org>
From: Darren Reed <darrenr@reed.wattle.id.au>
List: current-users
Date: 07/29/1999 01:46:36
Could a few people please take the time to test the patch below.
It changes the behaviour of ktruss to wait until the process being
trace'd has died when doing 'ktruss ls' (current behaviour sees
it return and the shell prompt print somewhere up the screen in
the middle of the output).

I'm also interested if this change has any impact on expected behaviour
of ktrace.  My own testing says no, but I'd like other opinions too.

Thanks,
darren

*** ktrace.c.dist	Wed Jul 28 23:19:59 1999
--- ktrace.c	Thu Jul 29 01:25:46 1999
***************
*** 49,54 ****
--- 49,55 ----
  
  #include <sys/param.h>
  #include <sys/stat.h>
+ #include <sys/wait.h>
  #include <sys/file.h>
  #include <sys/time.h>
  #include <sys/uio.h>
***************
*** 198,206 ****
  	}
  
  	if (*argv) { 
! 		(void)do_ktrace(outfile, ops, trpoints, getpid());
! 		execvp(argv[0], &argv[0]);
! 		err(1, "exec of '%s' failed", argv[0]);
  	} else
  		(void)do_ktrace(outfile, ops, trpoints, pid);
  	exit(0);
--- 199,208 ----
  	}
  
  	if (*argv) { 
! 		if (do_ktrace(outfile, ops, trpoints, getpid()) == 1) {
! 			execvp(argv[0], &argv[0]);
! 			err(1, "exec of '%s' failed", argv[0]);
! 		}
  	} else
  		(void)do_ktrace(outfile, ops, trpoints, pid);
  	exit(0);
***************
*** 260,285 ****
  	int ret;
  
  	if (!tracefile || strcmp(tracefile, "-") == 0) {
! 		int pi[2], nofork, fpid;
  
  		if (pipe(pi) < 0)
  			err(1, "pipe(2)");
  		fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
  		fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
  
! 		nofork = (pid != getpid());
  
! 		if (!nofork)
  			fpid = fork();
  		else
! 			fpid = 0;	/* XXX: Gcc */
! 		if (nofork || !fpid) {
! 			if (nofork)
! 				ret = fktrace(pi[1], ops, trpoints, pid);
  			else
  				close(pi[1]);
  #ifdef KTRUSS
  			dumpfile(NULL, pi[0], trpoints);
  #else
  			{
  				char	buf[512];
--- 262,288 ----
  	int ret;
  
  	if (!tracefile || strcmp(tracefile, "-") == 0) {
! 		int pi[2], dofork, fpid;
  
  		if (pipe(pi) < 0)
  			err(1, "pipe(2)");
  		fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
  		fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
  
! 		dofork = (pid == getpid());
  
! 		if (dofork)
  			fpid = fork();
  		else
! 			fpid = pid;	/* XXX: Gcc */
! 		if (fpid) {
! 			if (!dofork)
! 				ret = fktrace(pi[1], ops, trpoints, fpid);
  			else
  				close(pi[1]);
  #ifdef KTRUSS
  			dumpfile(NULL, pi[0], trpoints);
+ 			waitpid(fpid, NULL, 0);
  #else
  			{
  				char	buf[512];
***************
*** 291,309 ****
  				}
  			}
  #endif
- 			if (!nofork)
- #ifdef KTRUSS
- 				exit(0);	/* flush stdio needed */
- #else
- 				_exit(0);
- #endif
  			return 0;
  		}
  		close(pi[0]);
! 		ret = fktrace(pi[1], ops, trpoints, pid);
  	} else
  		ret = ktrace(tracefile, ops, trpoints, pid);
  	if (ret < 0)
  		err(1, tracefile);
! 	return ret;
  }
--- 294,309 ----
  				}
  			}
  #endif
  			return 0;
  		}
  		close(pi[0]);
! 		if (dofork && !fpid) {
! 			ret = fktrace(pi[1], ops, trpoints, getpid());
! 			return 1;
! 		}
  	} else
  		ret = ktrace(tracefile, ops, trpoints, pid);
  	if (ret < 0)
  		err(1, tracefile);
! 	return 1;
  }