-
Notifications
You must be signed in to change notification settings - Fork 3
/
frelink_app.c
193 lines (147 loc) · 3.89 KB
/
frelink_app.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
/*
* frelink: Recover deleted files that are still open by a process
* or a loop mount.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) Pantelis Koukousoulas 2011
*
* Author: Pantelis Koukousoulas <[email protected]>
*
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "frelink.h"
static int get_module_fd();
static int tst_ioctl(unsigned int devfd);
static int recover_from_fd(unsigned int devfd, const char *const name);
static int recover_from_lo(unsigned int devfd, const char *const name);
static int probe_lo(const char *const devname);
#define starts_with(A,B) (strncmp((A), (B), strlen(B)) == 0)
int main(int argc, char **argv)
{
int devfd;
char *arg;
int ret = 0;
if (argc != 2) {
fputs("Usage: frelink /proc/<pid>/fd/<fd>\n"
"or frelink /dev/loop0\n", stderr);
ret = 0; goto exit;
}
arg = argv[1];
devfd = get_module_fd();
if (devfd < 0) {
perror("open /proc/frelink");
ret = 1; goto exit;
}
if (starts_with(arg, "--test-ioctl")) {
ret = tst_ioctl(devfd);
goto release_devfd;
}
if (starts_with(arg, "/proc")) {
ret = recover_from_fd(devfd, arg);
goto release_devfd;
}
if (starts_with(arg, "/dev/loop")) {
ret = recover_from_lo(devfd, arg);
if (!probe_lo(arg))
fputs("Could not probe loop back device\n",stderr);
goto release_devfd;
}
fputs("illegal argument\n", stderr);
ret = 1;
release_devfd:
close(devfd);
exit:
return ret;
}
static int get_module_fd()
{
int fd = open("/proc/frelink", O_RDONLY, 0444);
if (fd < 0) {
errno = ENODEV;
return -1;
}
return fd;
}
char *recover_name_from_fd(const char *const fdname)
{
#define BUFSIZE 1024
static char namebuf[1024];
ssize_t len = readlink(fdname, namebuf, BUFSIZE-1);
if (len != -1)
namebuf[len] = '\0';
else {
fprintf(stderr, "invalid /proc file name\n");
errno = EBADF;
return NULL;
}
if ( len > 10 && !strcmp(namebuf+len-10, " (deleted)")) {
*(namebuf + len - 10) = '\0';
}
return namebuf;
}
static int tst_ioctl(unsigned int devfd)
{
struct frelink_arg data;
data.id.fd = 1;
return ioctl(devfd, FRELINK_IOCRECTEST, &data);
}
static int recover_from_fd(unsigned int devfd, const char *const name)
{
struct frelink_arg data;
char * newname = recover_name_from_fd(name);
int fd = open(name, O_RDONLY, 0666);
if (fd < 0) {
fprintf(stderr, "open");
perror(name);
return 1;
}
if (!name) {
perror("recover_name_from_fd");
errno = EBADF;
}
data.id.fd = fd;
data.path = newname;
return ioctl(devfd, FRELINK_IOCRECFD, &data);
}
static int recover_from_lo(unsigned int devfd, const char *const name)
{
struct frelink_arg data;
int loidx = -1;
if (sscanf(name, "/dev/loop%d", &loidx) == 0) {
fputs("Bad loop device\n", stderr);
return 1;
}
data.id.loidx = loidx;
return ioctl(devfd, FRELINK_IOCRECLOOP, &data);
}
#define LOOP_GET_STATUS 0x4C03
static int probe_lo(const char *const devname)
{
/* We don't care about the layout, just make it big enough */
char loop_info[256];
int devfd;
errno = 0;
if ((devfd = open(devname, O_RDONLY)) < 0)
return 0;
fprintf(stderr,"Probing loop device %s\n",devname);
//We don't care about the return value at all
ioctl (devfd, LOOP_GET_STATUS, &loop_info);
close(devfd);
return 1;
}