-
Notifications
You must be signed in to change notification settings - Fork 73
/
reredirect.c
223 lines (205 loc) · 7.17 KB
/
reredirect.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (C) 2011 by Nelson Elhage
* Copyright (C) 2014 by Jérôme Pouiller <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/limits.h>
#include "reredirect.h"
static int verbose = 0;
static void usage() {
char *me = program_invocation_short_name;
fprintf(stderr, "Usage: %s [-m FILE|-o FILE|-e FILE|-O FD|-E FD] [-N] [-d] PID\n", me);
fprintf(stderr, "%s redirect outputs of a running process to a file.\n", me);
fprintf(stderr, " PID Process to reattach\n");
fprintf(stderr, " -o FILE File to redirect stdout. \n");
fprintf(stderr, " -e FILE File to redirect stderr.\n");
fprintf(stderr, " -m FILE Same than -o FILE -e FILE.\n");
fprintf(stderr, " -O FD Redirect stdout to this file descriptor. Mainly used to restore\n");
fprintf(stderr, " process outputs.\n");
fprintf(stderr, " -E FD Redirect stderr to this file descriptor. Mainly used to restore\n");
fprintf(stderr, " process outputs.\n");
fprintf(stderr, " -N Do not save previous stream.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Notice you can redirect to another program using name pipe. For example:\n");
fprintf(stderr, " mkfifo /tmp/fifo\n");
fprintf(stderr, " tee /tmp/log < /tmp/fifo\n");
fprintf(stderr, " %s PID -m /tmp/fifo\n", me);
}
static void _debug(const char *pfx, const char *msg, va_list ap) {
if (pfx)
fprintf(stderr, "%s", pfx);
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
}
void debug(const char *msg, ...) {
va_list ap;
if (!verbose)
return;
va_start(ap, msg);
_debug("[+] ", msg, ap);
va_end(ap);
}
void error(const char *msg, ...) {
va_list ap;
va_start(ap, msg);
_debug("[-] ", msg, ap);
va_end(ap);
}
void die(const char *msg, ...) {
va_list ap;
va_start(ap, msg);
_debug("[!] ", msg, ap);
va_end(ap);
exit(1);
}
void usage_die(const char *msg, ...) {
va_list ap;
va_start(ap, msg);
_debug("[!] ", msg, ap);
va_end(ap);
usage();
exit(1);
}
static void check_yama_ptrace_scope(void) {
int fd = open("/proc/sys/kernel/yama/ptrace_scope", O_RDONLY);
if (fd >= 0) {
char buf[256];
int n;
n = read(fd, buf, sizeof buf);
close(fd);
if (n > 0) {
if (!atoi(buf)) {
return;
}
}
} else if (errno == ENOENT)
return;
fprintf(stderr, "The kernel denied permission while attaching. If your uid matches\n");
fprintf(stderr, "the target's, check the value of /proc/sys/kernel/yama/ptrace_scope.\n");
fprintf(stderr, "For more information, see /etc/sysctl.d/10-ptrace.conf\n");
}
int main(int argc, char **argv) {
int no_restore = 0;
int fde = -1;
int fdo = -1;
int fdi = -1;
int fde_orig = -1;
int fdo_orig = -1;
int fdi_orig = -1;
const char *filei = NULL;
const char *fileo = NULL;
const char *filee = NULL;
pid_t pid;
int opt;
int err;
unsigned long scratch_page = (unsigned long) -1;
struct ptrace_child child;
while ((opt = getopt(argc, argv, "m:i:o:e:I:O:E:s:dNvh")) != -1) {
switch (opt) {
case 'I':
if (filei || fdi >= 0)
usage_die("-i and -I are exclusive\n");
fdi = atoi(optarg);
break;
case 'O':
if (fileo || fdo >= 0)
usage_die("-m, -o and -O are exclusive\n");
fdo = atoi(optarg);
break;
case 'E':
if (filee || fde >= 0)
usage_die("-m, -e and -E are exclusive\n");
fde = atoi(optarg);
break;
case 'i':
if (filei || fdi >= 0)
usage_die("-i and -I are exclusive\n");
filei = optarg;
break;
case 'o':
if (fileo || fdo >= 0)
usage_die("-m, -o and -O are exclusive\n");
fileo = optarg;
break;
case 'e':
if (filee || fde >= 0)
usage_die("-m, -e and -E are exclusive\n");
filee = optarg;
break;
case 'm':
if (filee || fde >= 0 || fileo || fdo >= 0)
usage_die("-m is exclusive with -o, -e, -O and -E\n");
fileo = filee = optarg;
break;
case 'N':
no_restore = 1;
break;
case 'h':
usage(argv[0]);
exit(0);
break;
case 'v':
verbose = 1;
break;
case 'V':
printf("This is reredirect version %s.\n", REREDIRECT_VERSION);
printf("http://github.com/jerome-pouiller/reredirect/\n");
exit(0);
default: /* '?' */
usage_die("Unknown option\n");
break;
}
}
if (optind >= argc)
usage_die("No pid specified to attach\n");
pid = atoi(argv[optind]);
err = child_attach(pid, &child, &scratch_page);
if (err) {
fprintf(stderr, "Unable to attach to pid %d: %s\n", pid, strerror(err));
if (err == EPERM)
check_yama_ptrace_scope();
exit(1);
}
if (filei)
fdi = child_open(&child, scratch_page, filei);
if (fileo)
fdo = child_open(&child, scratch_page, fileo);
if (filee)
fde = child_open(&child, scratch_page, filee);
if (fdi >= 0)
fdi_orig = child_dup(&child, fdi, 0, !no_restore);
if (fdo >= 0)
fdo_orig = child_dup(&child, fdo, 1, !no_restore);
if (fde >= 0)
fde_orig = child_dup(&child, fde, 2, !no_restore);
child_detach(&child, scratch_page);
if (!no_restore) {
printf("# Previous state saved. To restore, use:\n");
printf("%s -N -I %d -O %d -E %d %d\n", program_invocation_name, fdi_orig, fdo_orig, fde_orig, pid);
}
return 0;
}