diff options
Diffstat (limited to 'usr.bin/ktrace/ktrace.c')
-rw-r--r-- | usr.bin/ktrace/ktrace.c | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/usr.bin/ktrace/ktrace.c b/usr.bin/ktrace/ktrace.c index f40dbaf..f96c4fd 100644 --- a/usr.bin/ktrace/ktrace.c +++ b/usr.bin/ktrace/ktrace.c @@ -38,7 +38,11 @@ static char copyright[] = #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)ktrace.c 8.2 (Berkeley) 4/28/95"; +#if 0 +static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93"; +#endif +static const char rcsid[] = + "$Id: ktrace.c,v 1.10 1997/03/29 04:30:16 imp Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -62,15 +66,19 @@ main(argc, argv) int argc; char **argv; { + extern int optind; + extern char *optarg; enum { NOTSET, CLEAR, CLEARALL } clear; int append, ch, fd, inherit, ops, pid, pidset, trpoints; char *tracefile; + mode_t omask; + struct stat sb; clear = NOTSET; append = ops = pidset = inherit = 0; trpoints = DEF_POINTS; tracefile = DEF_TRACEFILE; - while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF) + while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1) switch((char)ch) { case 'a': append = 1; @@ -132,9 +140,21 @@ main(argc, argv) exit(0); } - if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC), - DEFFILEMODE)) < 0) - err(1, tracefile); + omask = umask(S_IRWXG|S_IRWXO); + if (append) { + if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0) + err(1, tracefile); + if (fstat(fd, &sb) != 0 || sb.st_uid != getuid()) + errx(1, "Refuse to append to %s not owned by you.", + tracefile); + } else { + if (unlink(tracefile) == -1 && errno != ENOENT) + err(1, "unlink %s", tracefile); + if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY, + DEFFILEMODE)) < 0) + err(1, tracefile); + } + (void)umask(omask); (void)close(fd); if (*argv) { @@ -167,8 +187,9 @@ rpid(p) void usage() { - (void)fprintf(stderr, -"usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n"); + (void)fprintf(stderr, "%s\n%s\n", +"usage: ktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [cnisuv]", +" ktrace [-aCcid] [-f trfile] [-t [cnisuw] command"); exit(1); } |