-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdpkg.c
300 lines (257 loc) · 7.24 KB
/
tdpkg.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright © 2010 Luca Bruno
This file is part of tdpkg.
tdpkg 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 3 of the License, or
(at your option) any later version.
tdpkg 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 tdpkg. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include "cache.h"
typedef int (*open_t)(const char *path, int oflag, ...);
static int _tdpkg_open (const char *path, int oflag, int mode);
/* real functions */
static open_t realopen;
static open_t realopen64;
static int (*real__fxstat)(int ver, int fd, struct stat* buf);
static int (*real__fxstat64)(int ver, int fd, struct stat64* buf);
static ssize_t (*realread)(int fildes, void *buf, size_t nbyte);
static int (*realclose)(int fd);
static int (*realrename)(const char *old, const char *new);
static int (*realunlink)(const char* pathname);
/* handle open() of dpkg/src/filesdb.c */
#define FAKE_FD 4321
static struct OpenState
{
int fd;
char* contents;
size_t len;
size_t read;
char *fn;
} open_state;
static int cache_initialized;
/* called once library is preloaded */
void _init (void)
{
static int initialized = 0;
if (!initialized)
initialized = 1;
else
return;
/* use absolute path for current library */
char *ld_preload = getenv ("LD_PRELOAD");
if (ld_preload)
{
char *abspath = realpath (ld_preload, NULL);
if (abspath)
{
setenv ("LD_PRELOAD", abspath, 1);
free (abspath);
}
}
memset (&open_state, '\0', sizeof (open_state));
open_state.fd = -1;
realopen = dlsym (RTLD_NEXT, "open");
realopen64 = dlsym (RTLD_NEXT, "open64");
real__fxstat = dlsym (RTLD_NEXT, "__fxstat");
real__fxstat64 = dlsym (RTLD_NEXT, "__fxstat64");
realread = dlsym (RTLD_NEXT, "read");
realclose = dlsym (RTLD_NEXT, "close");
realrename = dlsym (RTLD_NEXT, "rename");
realunlink = dlsym (RTLD_NEXT, "unlink");
if (!tdpkg_cache_initialize ())
cache_initialized = 1;
else
fprintf (stderr, "tdpkg: cache initialization failed, no wrapping\n");
}
static int
is_list_file (const char* path)
{
return strstr (path, "/var/lib/dpkg/info/") && strstr (path, ".list");
}
int
rename (const char *old, const char *new)
{
int result = realrename (old, new);
if (!result && is_list_file (new))
{
if (tdpkg_cache_write_filename (new))
{
fprintf (stderr, "tdpkg: can't update cache for file %s, no wrapping\n", new);
tdpkg_cache_finalize ();
cache_initialized = 0;
}
}
return result;
}
int
unlink (const char* pathname)
{
int result = realunlink (pathname);
if (!result && is_list_file (pathname))
{
if (tdpkg_cache_delete_filename (pathname))
{
fprintf (stderr, "tdpkg: can't delete %s from cache, no wrapping\n", pathname);
tdpkg_cache_finalize ();
cache_initialized = 0;
}
}
return result;
}
static int
_tdpkg_open (const char *path, int oflag, int mode)
{
if (!cache_initialized)
return realopen (path, oflag, mode);
if (!is_list_file (path) || (oflag & O_RDONLY) != O_RDONLY)
return realopen (path, oflag, mode);
// sometimes dpkg calls FIGETBSZ first time, open_state.fn fixes it
if (open_state.fd >= 0 && open_state.fn != path)
{
#ifdef TDPKG_INFO
fprintf (stderr, "tdpkg: nested open(%s, %d, %d) detected, no wrapping\n", path, oflag, mode);
#endif
return realopen (path, oflag, mode);
}
open_state.fn = (char*)path;
open_state.contents = tdpkg_cache_read_filename (path);
if (!open_state.contents)
{
#ifdef TDPKG_INFO
fprintf (stderr, "tdpkg: file %s not up-to-date in cache, rebuild cache\n", path);
#endif
if (tdpkg_cache_rebuild ())
{
fprintf (stderr, "tdpkg: can't rebuild cache, no wrapping\n");
tdpkg_cache_finalize ();
cache_initialized = 0;
return realopen (path, oflag, mode);
}
open_state.contents = tdpkg_cache_read_filename (path);
if (!open_state.contents)
{
fprintf (stderr, "tdpkg: path %s not being indexed, no wrapping\n", path);
return realopen (path, oflag, mode);
}
}
open_state.fd = FAKE_FD;
open_state.len = strlen (open_state.contents);
return open_state.fd;
}
int
open (const char *path, int oflag, ...)
{
va_list ap;
va_start (ap, oflag);
int mode = va_arg (ap, int);
va_end (ap);
return _tdpkg_open (path, oflag, mode);
}
int
open64 (const char *path, int oflag, ...)
{
va_list ap;
va_start (ap, oflag);
int mode = va_arg (ap, int);
va_end (ap);
return _tdpkg_open (path, oflag, mode);
}
int
__fxstat (int ver, int fd, struct stat* buf)
{
if (!cache_initialized)
return real__fxstat (ver, fd, buf);
if (open_state.fd < 0)
return real__fxstat (ver, fd, buf);
if (open_state.fd != fd)
{
#ifdef TDPKG_INFO
fprintf (stderr, "tdpkg: nested __fxstat(%d) detected, no wrapping\n", fd);
#endif
return real__fxstat (ver, fd, buf);
}
buf->st_size = open_state.len;
buf->st_mode = S_IFREG;
return 0;
}
int
__fxstat64 (int ver, int fd, struct stat64* buf)
{
if (!cache_initialized)
return real__fxstat64 (ver, fd, buf);
if (open_state.fd < 0)
return real__fxstat64 (ver, fd, buf);
if (open_state.fd != fd)
{
#ifdef TDPKG_INFO
fprintf (stderr, "tdpkg: nested __fxstat64(%d) detected, no wrapping\n", fd);
#endif
return real__fxstat64 (ver, fd, buf);
}
buf->st_size = open_state.len;
buf->st_mode = S_IFREG;
return 0;
}
ssize_t
read (int fildes, void *buf, size_t nbyte)
{
if (!cache_initialized)
return realread (fildes, buf, nbyte);
if (open_state.fd < 0)
return realread (fildes, buf, nbyte);
if (open_state.fd != fildes)
{
#ifdef TDPKG_INFO
fprintf (stderr, "tdpkg: nested read(%d) detected, no wrapping\n", fildes);
#endif
return realread (fildes, buf, nbyte);
}
if (open_state.read > open_state.len)
{
#ifdef TDPKG_INFO
fprintf (stderr, "tdpkg: useless read(%d) detected, returning 0\n", open_state.fd);
#endif
return 0;
}
size_t nowread = (open_state.len-open_state.read) > nbyte ? nbyte : (open_state.len-open_state.read);
memcpy (buf, open_state.contents+open_state.read, nowread);
open_state.read += nowread;
return nowread;
}
int
close (int fd)
{
if (!cache_initialized)
return realclose (fd);
if (open_state.fd < 0)
return realclose (fd);
if (open_state.fd != fd)
{
#ifdef TDPKG_INFO
fprintf (stderr, "tdpkg: close() on unknown fd %d, no wrapping\n", fd);
#endif
return realclose (fd);
}
open_state.fd = -1;
if (open_state.contents)
free (open_state.contents);
open_state.contents = NULL;
open_state.len = 0;
open_state.read = 0;
return 0;
}