-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathoutput.c
More file actions
101 lines (83 loc) · 2.15 KB
/
output.c
File metadata and controls
101 lines (83 loc) · 2.15 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include "arg-decoder.h"
#include "pids.h"
#include "params.h" // verbosity
#include "shm.h"
#define BUFSIZE 1024 // decoded syscall args are fprintf'd directly, this is for everything else.
/* Set once at child init to avoid repeated getpid() syscalls in the output hot path. */
static pid_t my_pid = 0;
void output_set_pid(pid_t pid)
{
my_pid = pid;
}
/*
* level defines whether it gets displayed to the screen.
* verbosity defaults to 1 (only level 0 prints).
* Each -v increases verbosity: -v shows 0+1, -vv shows 0+1+2.
* 0 = important (errors, taint, startup info, syscall counts)
* 1 = operational (fd generation, socket cache, done parsing)
* 2 = debug (device details, per-socket info, map details)
*/
void output(int level, const char *fmt, ...)
{
va_list args;
int n;
pid_t pid;
char outputbuf[BUFSIZE];
char *prefix = NULL;
char main_prefix[]="[main] ";
char continuationtxt[]="";
char child_prefix[32];
if (level >= verbosity)
return;
if (level == CONT) {
prefix = continuationtxt;
goto skip_pid;
}
/* prefix preparation */
pid = my_pid ? my_pid : getpid();
if (pid == mainpid)
prefix = main_prefix;
else {
int childno;
childno = find_childno(pid);
snprintf(child_prefix, sizeof(child_prefix), "[child%d:%d] ", childno, pid);
prefix = child_prefix;
}
skip_pid:
/* formatting output */
va_start(args, fmt);
n = vsnprintf(outputbuf, sizeof(outputbuf), fmt, args);
va_end(args);
if (n < 0) {
outputerr("## Something went wrong in output() [%d]\n", n);
exit(EXIT_FAILURE);
}
printf("%s%s", prefix, outputbuf);
}
/*
* Used as a way to consolidated all printf calls if someones one to redirect it to somewhere else.
* note: this function ignores verbosity since it main purpose is error output.
*/
void outputerr(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void outputstd(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stdout, fmt, args);
va_end(args);
}
void output_rendered_buffer(char *buffer)
{
if (verbosity > 1)
fprintf(stdout, "%s", buffer);
}