|
Added
Link Here
|
| 1 |
/*- |
| 2 |
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD |
| 3 |
* |
| 4 |
* Copyright (c) 2020 Daniel O'Connor <darius@dons.net.au> |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer |
| 12 |
* in this position and unchanged. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
* SUCH DAMAGE. |
| 28 |
* |
| 29 |
* $FreeBSD$ |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <sys/wait.h> |
| 37 |
#include <sys/ioctl.h> |
| 38 |
#include <dev/filemon/filemon.h> |
| 39 |
#include <fcntl.h> |
| 40 |
#include <err.h> |
| 41 |
#include <unistd.h> |
| 42 |
|
| 43 |
static void |
| 44 |
usage(void) |
| 45 |
{ |
| 46 |
fputs(" filemon [-f trfile] command [...]\n", stderr); |
| 47 |
exit(1); |
| 48 |
} |
| 49 |
|
| 50 |
int main(int argc, char **argv) { |
| 51 |
char ch; |
| 52 |
int fm_fd, fm_log; |
| 53 |
pid_t child; |
| 54 |
const char *tracefile = "filemon.out"; |
| 55 |
|
| 56 |
while ((ch = getopt(argc,argv,"f:")) != -1) |
| 57 |
switch((char)ch) { |
| 58 |
case 'f': |
| 59 |
tracefile = optarg; |
| 60 |
break; |
| 61 |
default: |
| 62 |
usage(); |
| 63 |
} |
| 64 |
|
| 65 |
argv += optind; |
| 66 |
argc -= optind; |
| 67 |
|
| 68 |
/* Need something to run */ |
| 69 |
if (argc == 0) |
| 70 |
usage(); |
| 71 |
|
| 72 |
if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1) |
| 73 |
err(1, "open(\"/dev/filemon\", O_RDWR)"); |
| 74 |
if ((fm_log = open(tracefile, |
| 75 |
O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, DEFFILEMODE)) == -1) |
| 76 |
err(1, "Cannot open trace file %s", tracefile); |
| 77 |
|
| 78 |
if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1) |
| 79 |
err(1, "Cannot set filemon log file descriptor"); |
| 80 |
|
| 81 |
if ((child = fork()) == 0) { |
| 82 |
child = getpid(); |
| 83 |
if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1) |
| 84 |
err(1, "Cannot set filemon PID"); |
| 85 |
execvp(*argv, argv); |
| 86 |
err(1, "exec of '%s' failed", *argv); |
| 87 |
} else { |
| 88 |
wait(&child); |
| 89 |
close(fm_fd); |
| 90 |
} |
| 91 |
} |