Thu Jan 24 17:47:59 2013 UTC ()
use O_CLOEXEC.


(christos)
diff -r1.45 -r1.46 src/usr.bin/ktrace/ktrace.c

cvs diff -r1.45 -r1.46 src/usr.bin/ktrace/ktrace.c (expand / switch to unified diff)

--- src/usr.bin/ktrace/ktrace.c 2011/09/16 15:39:26 1.45
+++ src/usr.bin/ktrace/ktrace.c 2013/01/24 17:47:58 1.46
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ktrace.c,v 1.45 2011/09/16 15:39:26 joerg Exp $ */ 1/* $NetBSD: ktrace.c,v 1.46 2013/01/24 17:47:58 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1988, 1993 4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -29,27 +29,27 @@ @@ -29,27 +29,27 @@
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1988, 1993\ 34__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 The Regents of the University of California. All rights reserved."); 35 The Regents of the University of California. All rights reserved.");
36#endif /* not lint */ 36#endif /* not lint */
37 37
38#ifndef lint 38#ifndef lint
39#if 0 39#if 0
40static char sccsid[] = "@(#)ktrace.c 8.2 (Berkeley) 4/28/95"; 40static char sccsid[] = "@(#)ktrace.c 8.2 (Berkeley) 4/28/95";
41#else 41#else
42__RCSID("$NetBSD: ktrace.c,v 1.45 2011/09/16 15:39:26 joerg Exp $"); 42__RCSID("$NetBSD: ktrace.c,v 1.46 2013/01/24 17:47:58 christos Exp $");
43#endif 43#endif
44#endif /* not lint */ 44#endif /* not lint */
45 45
46#include <sys/param.h> 46#include <sys/param.h>
47#include <sys/stat.h> 47#include <sys/stat.h>
48#include <sys/wait.h> 48#include <sys/wait.h>
49#include <sys/file.h> 49#include <sys/file.h>
50#include <sys/time.h> 50#include <sys/time.h>
51#include <sys/uio.h> 51#include <sys/uio.h>
52#include <sys/ktrace.h> 52#include <sys/ktrace.h>
53#include <sys/socket.h> 53#include <sys/socket.h>
54 54
55#include <err.h> 55#include <err.h>
@@ -60,27 +60,26 @@ __RCSID("$NetBSD: ktrace.c,v 1.45 2011/0 @@ -60,27 +60,26 @@ __RCSID("$NetBSD: ktrace.c,v 1.45 2011/0
60#include <unistd.h> 60#include <unistd.h>
61#include <signal.h> 61#include <signal.h>
62 62
63#include "ktrace.h" 63#include "ktrace.h"
64 64
65#ifdef KTRUSS 65#ifdef KTRUSS
66#include "setemul.h" 66#include "setemul.h"
67#endif 67#endif
68 68
69static int rpid(char *); 69static int rpid(char *);
70__dead static void usage(void); 70__dead static void usage(void);
71static int do_ktrace(const char *, int, int, int, int, int); 71static int do_ktrace(const char *, int, int, int, int, int);
72__dead static void no_ktrace(int); 72__dead static void no_ktrace(int);
73static void fset(int fd, int flag); 
74static void fclear(int fd, int flag); 73static void fclear(int fd, int flag);
75 74
76#ifdef KTRUSS 75#ifdef KTRUSS
77extern int timestamp, decimal, fancy, tail, maxdata; 76extern int timestamp, decimal, fancy, tail, maxdata;
78#endif 77#endif
79 78
80int 79int
81main(int argc, char *argv[]) 80main(int argc, char *argv[])
82{ 81{
83 enum { NOTSET, CLEAR, CLEARALL } clear; 82 enum { NOTSET, CLEAR, CLEARALL } clear;
84 int block, append, ch, fd, trset, ops, pid, pidset, synclog, trpoints; 83 int block, append, ch, fd, trset, ops, pid, pidset, synclog, trpoints;
85 int vers; 84 int vers;
86 const char *outfile; 85 const char *outfile;
@@ -260,37 +259,26 @@ rpid(char *p) @@ -260,37 +259,26 @@ rpid(char *p)
260 259
261 if (first++) { 260 if (first++) {
262 warnx("only one -g or -p flag is permitted."); 261 warnx("only one -g or -p flag is permitted.");
263 usage(); 262 usage();
264 } 263 }
265 if (!*p) { 264 if (!*p) {
266 warnx("illegal process id."); 265 warnx("illegal process id.");
267 usage(); 266 usage();
268 } 267 }
269 return (atoi(p)); 268 return (atoi(p));
270} 269}
271 270
272static void 271static void
273fset(int fd, int flag) 
274{ 
275 int oflag = fcntl(fd, F_GETFL, 0); 
276 
277 if (oflag == -1) 
278 err(EXIT_FAILURE, "Cannot get file flags"); 
279 if (fcntl(fd, F_SETFL, oflag | flag) == -1) 
280 err(EXIT_FAILURE, "Cannot set file flags"); 
281} 
282 
283static void 
284fclear(int fd, int flag) 272fclear(int fd, int flag)
285{ 273{
286 int oflag = fcntl(fd, F_GETFL, 0); 274 int oflag = fcntl(fd, F_GETFL, 0);
287 275
288 if (oflag == -1) 276 if (oflag == -1)
289 err(EXIT_FAILURE, "Cannot get file flags"); 277 err(EXIT_FAILURE, "Cannot get file flags");
290 if (fcntl(fd, F_SETFL, oflag & ~flag) == -1) 278 if (fcntl(fd, F_SETFL, oflag & ~flag) == -1)
291 err(EXIT_FAILURE, "Cannot set file flags"); 279 err(EXIT_FAILURE, "Cannot set file flags");
292} 280}
293 281
294static void 282static void
295usage(void) 283usage(void)
296{ 284{
@@ -330,31 +318,29 @@ no_ktrace(int sig) @@ -330,31 +318,29 @@ no_ktrace(int sig)
330} 318}
331 319
332static int 320static int
333do_ktrace(const char *tracefile, int vers, int ops, int trpoints, int pid, 321do_ktrace(const char *tracefile, int vers, int ops, int trpoints, int pid,
334 int block) 322 int block)
335{ 323{
336 int ret; 324 int ret;
337 ops |= vers << KTRFAC_VER_SHIFT; 325 ops |= vers << KTRFAC_VER_SHIFT;
338 326
339 if (KTROP(ops) == KTROP_SET && 327 if (KTROP(ops) == KTROP_SET &&
340 (!tracefile || strcmp(tracefile, "-") == 0)) { 328 (!tracefile || strcmp(tracefile, "-") == 0)) {
341 int pi[2], dofork; 329 int pi[2], dofork;
342 330
343 if (pipe(pi) < 0) 331 if (pipe2(pi, O_CLOEXEC) == -1)
344 err(EXIT_FAILURE, "pipe(2)"); 332 err(EXIT_FAILURE, "pipe(2)");
345 333
346 fset(pi[0], FD_CLOEXEC); 
347 fset(pi[1], FD_CLOEXEC); 
348 dofork = (pid == getpid()); 334 dofork = (pid == getpid());
349 335
350 if (dofork) { 336 if (dofork) {
351#ifdef KTRUSS 337#ifdef KTRUSS
352 /* 338 /*
353 * Create a child process and trace it. 339 * Create a child process and trace it.
354 */ 340 */
355 pid = fork(); 341 pid = fork();
356 if (pid == -1) 342 if (pid == -1)
357 err(EXIT_FAILURE, "fork"); 343 err(EXIT_FAILURE, "fork");
358 else if (pid == 0) { 344 else if (pid == 0) {
359 pid = getpid(); 345 pid = getpid();
360 goto trace_and_exec; 346 goto trace_and_exec;