about summary refs log tree commit diff
path: root/frida_mode/src/output.c
blob: 8a222b25e200a0049fb86b2f16eea3eb6cfb38bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include "frida-gum.h"

#include "debug.h"

#include "output.h"

static int output_fd = -1;

static void output_redirect(int fd, char *variable) {

  char *filename = getenv(variable);
  char *path = NULL;

  if (filename == NULL) { return; }

  path = g_canonicalize_filename(filename, g_get_current_dir());

  OKF("Redirect %d -> '%s'", fd, path);

  output_fd = open(path, O_RDWR | O_CREAT | O_TRUNC,
                   S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);

  g_free(path);

  if (output_fd < 0) { FATAL("Failed to open fd(%d) error %d", fd, errno); }

  if (dup2(output_fd, fd) < 0) {

    FATAL("Failed to set fd(%d) error %d", fd, errno);

  }

}

void output_init(void) {

  output_redirect(STDOUT_FILENO, "AFL_FRIDA_OUTPUT_STDOUT");
  output_redirect(STDERR_FILENO, "AFL_FRIDA_OUTPUT_STDERR");

}