summaryrefslogtreecommitdiffstats
path: root/extras/test/ld-preload-test/ld-preload-lib.c
blob: 8a7504e9c093b59acef0e4b544f3bed4935cf169 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
   Copyright (c) 2007-2009 Z RESEARCH, Inc. <http://www.zresearch.com>
   This file is part of GlusterFS.

   GlusterFS 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.

   GlusterFS 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, see
   <http://www.gnu.org/licenses/>.
*/


/* LD PRELOAD'able library
 * A very simple library that intercepts booster supported system calls
 * and prints a log message to stdout.
 *
 * Combined with the ld-preload-test, we cam determine whether all system calls
 * are getting redirected into this library when LD_PRELOAD'ed. This helps us
 * conduct a basic test to ensure that the required syscalls actually will
 * be intercepted by the booster library.
 */

#include <dlfcn.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <utime.h>
#include <sys/statfs.h>
#include <sys/statvfs.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <attr/xattr.h>
#include <sys/sendfile.h>

/* Err number that is assigned to errno so that test application can
 * verify that the function was intercepted correctly.
 */
#define PRELOAD_ERRNO_VERF      6449
#define set_errno()             (errno = PRELOAD_ERRNO_VERF)

inline void
intercept (char *call, int tabs)
{
        while (tabs > 0) {
                fprintf (stdout, "\t");
                --tabs;
        }

        fprintf (stdout, "Intercepted by %s", call);
}

int
creat64 (const char *pathname, mode_t mode)
{
        intercept ("creat64", 2);
        set_errno ();
        return -1;
}

int
creat (const char *pathname, mode_t mode)
{
        intercept ("creat", 2);
        set_errno ();
        return -1;
}


int
close (int fd)
{
        intercept ("close", 2);
        set_errno ();
        return -1;
}

int
open64 (const char *pathname, int flags, ...)
{
        intercept ("open64", 2);
        set_errno ();
        return -1;
}


int
open (const char *pathname, int flags, ...)
{
        intercept ("open", 2);
        set_errno ();
        return -1;
}

ssize_t
read (int fd, void *buf, size_t count)
{
        intercept ("read", 2);
        set_errno ();
        return -1;
}

ssize_t
readv (int fd, const struct iovec *vector, int count)
{
        intercept ("readv", 2);
        set_errno ();
        return -1;
}

ssize_t
pread (int fd, void *buf, size_t count, unsigned long offset)
{
        intercept ("pread", 2);
        set_errno ();
        return -1;
}


ssize_t
pread64 (int fd, void *buf, size_t count, uint64_t offset)
{
        intercept ("pread64", 2);
        set_errno ();
        return -1;
}

ssize_t
write (int fd, const void *buf, size_t count)
{
        intercept ("write", 2);
        set_errno ();
        return -1;
}

ssize_t
writev (int fd, const struct iovec *vector, int count)
{
        intercept ("writev", 2);
        set_errno ();
        return -1;
}

ssize_t
pwrite (int fd, const void *buf, size_t count, unsigned long offset)
{
        intercept ("pwrite", 2);
        set_errno ();
        return -1;
}

ssize_t
pwrite64 (int fd, const void *buf, size_t count, uint64_t offset)
{
        intercept ("pwrite64", 2);
        set_errno ();
        return -1;
}


off_t
lseek (int fildes, unsigned long offset, int whence)
{
        intercept ("lseek", 2);
        set_errno ();
        return -1;
}

off_t
lseek64 (int fildes, uint64_t offset, int whence)
{
        intercept ("lseek64", 2);
        set_errno ();
        return -1;
}


int
dup (int fd)
{
        intercept ("dup", 2);
        set_errno ();
        return -1;
}

int
dup2 (int oldfd, int newfd)
{
        intercept ("dup2", 2);
        set_errno ();
        return -1;
}

int
mkdir (const char *pathname, mode_t mode)
{
        intercept ("mkdir", 2);
        set_errno ();
        return -1;
}

int
rmdir (const char *pathname)
{
        intercept ("rmdir", 2);
        set_errno ();
        return -1;
}

int
chmod (const char *pathname, mode_t mode)
{
        intercept ("chmod", 2);
        set_errno ();
        return -1;
}

int
chown (const char *pathname, uid_t owner, gid_t group)
{
        intercept ("chown", 2);
        set_errno ();
        return -1;
}

int
fchmod (int fd, mode_t mode)
{
        intercept ("fchmod", 2);
        set_errno ();
        return -1;
}

int
fchown (int fd, uid_t uid, gid_t gid)
{
        intercept ("fchown", 2);
        set_errno ();
        return -1;
}

int fsync (int fd)
{
        intercept ("fsync", 2);
        set_errno ();
        return -1;
}


int
ftruncate (int fd, off_t length)
{
        intercept ("ftruncate", 1);
        set_errno ();
        return -1;
}


int
ftruncate64 (int fd, off_t length)
{
        intercept ("ftruncate64", 1);
        set_errno ();
        return -1;
}

int
link (const char *oldpath, const char *newname)
{
        intercept ("link", 2);
        set_errno ();
        return -1;
}

int
rename (const char *oldpath, const char *newpath)
{
        intercept ("rename", 2);
        set_errno ();
        return -1;
}

int
utimes (const char *path, const struct timeval times[2])
{
        intercept ("utimes", 2);
        set_errno ();
        return -1;
}

int
utime (const char *path, const struct utimbuf *buf)
{
        intercept ("utime", 2);
        set_errno ();
        return -1;
}


int
mknod (const char *path, mode_t mode, dev_t dev)
{
        intercept ("mknod", 2);
        set_errno ();
        return -1;
}

int
__xmknod (int ver, const char *path, mode_t mode, dev_t *dev)
{
        intercept ("__xmknod", 2);
        set_errno ();
        return -1;
}

int
mkfifo (const char *path, mode_t mode)
{
        intercept ("mkfifo", 2);
        set_errno ();
        return -1;
}

int
unlink (const char *path)
{
        intercept ("unlink", 2);
        set_errno ();
        return -1;
}


int
symlink (const char *oldpath, const char *newpath)
{
        intercept ("symlink", 2);
        set_errno ();
        return -1;
}

int
readlink (const char *path, char *buf, size_t bufsize)
{
        intercept ("readlink", 1);
        set_errno ();
        return -1;
}


char *
realpath (const char *path, char *resolved)
{
        intercept ("realpath", 1);
        set_errno ();
        return NULL;
}


DIR *
opendir (const char *path)
{
        intercept ("opendir", 2);
        set_errno ();
        return NULL;
}


struct dirent *
readdir (DIR *dir)
{
        intercept ("readdir\t", 2);
        set_errno ();
        return NULL;
}

struct dirent *
readdir64 (DIR *dir)
{
        intercept ("readdir64", 2);
        set_errno ();
        return NULL;
}


int
readdir_r (DIR *dir, struct dirent *entry, struct dirent **result)
{
        intercept ("readdir_r", 1);
        set_errno ();
        return -1;
}

int
readdir64_r (DIR *dir, struct dirent *entry, struct dirent **result)
{
        intercept ("readdir64_r", 1);
        set_errno ();
        return -1;
}


int
closedir (DIR *dh)
{
        intercept ("closedir", 1);
        set_errno ();
        return -1;
}

int
__xstat (int ver, const char *path, struct stat *buf)
{
        intercept ("__xstat\t", 2);
        set_errno ();
        return -1;
}


int
__xstat64 (int ver, const char *path, struct stat *buf)
{
        intercept ("__xstat64", 2);
        set_errno ();
        return -1;
}

int
stat (const char *path, struct stat *buf)
{
        intercept ("stat", 2);
        set_errno ();
        return -1;
}

int
stat64 (const char *path, struct stat *buf)
{
        intercept ("stat64", 2);
        set_errno ();
        return -1;
}

int
__fxstat (int ver, int fd, struct stat *buf)
{
        intercept ("__fxstat\t", 2);
        set_errno ();
        return -1;
}


int
__fxstat64 (int ver, int fd, struct stat *buf)
{
        intercept ("__fxstat64", 2);
        set_errno ();
        return -1;
}

int
fstat (int fd, struct stat *buf)
{
        intercept ("fstat", 2);
        set_errno ();
        return -1;
}

int
fstat64 (int fd , struct stat *buf)
{
        intercept ("fstat64", 2);
        set_errno ();
        return -1;
}

int
__lxstat (int ver, const char *path, struct stat *buf)
{
        intercept ("__lxstat\t", 2);
        set_errno ();
        return -1;
}

int
__lxstat64 (int ver, const char *path, struct stat *buf)
{
        intercept ("__lxstat64", 2);
        set_errno ();
        return -1;
}

int
lstat (const char *path, struct stat *buf)
{
        intercept ("lstat", 2);
        set_errno ();
        return -1;
}

int
lstat64 (const char *path, struct stat *buf)
{
        intercept ("lstat64", 2);
        set_errno ();
        return -1;
}

int
statfs (const char *path, struct statfs *buf)
{
        intercept ("statfs", 2);
        set_errno ();
        return -1;
}


int
statfs64 (const char *path, struct statfs *buf)
{
        intercept ("statfs64", 2);
        set_errno ();
        return -1;
}

int
statvfs (const char *path, struct statvfs *buf)
{
        intercept ("statvfs\t", 2);
        set_errno ();
        return -1;
}


int
statvfs64 (const char *path, struct statvfs *buf)
{
        intercept ("statvfs64", 2);
        set_errno ();
        return -1;
}

ssize_t
getxattr (const char *path, const char *name, void *value, size_t size)
{
        intercept ("getxattr", 1);
        set_errno ();
        return -1;
}

ssize_t
lgetxattr (const char *path, const char *name, void *value, size_t size)
{
        intercept ("lgetxattr", 1);
        set_errno ();
        return -1;
}


int
remove (const char* path)
{
        intercept ("remove", 2);
        set_errno ();
        return -1;
}

int
lchown (const char *path, uid_t owner, gid_t group)
{
        intercept ("lchown", 2);
        set_errno ();
        return -1;
}

void
rewinddir (DIR *dirp)
{
        intercept ("rewinddir", 1);
        set_errno ();
        return;
}

void
seekdir (DIR *dirp, off_t offset)
{
        intercept ("seekdir", 2);
        set_errno ();
        return;
}

off_t
telldir (DIR *dirp)
{
        intercept ("telldir", 2);
        set_errno ();
        return -1;
}

ssize_t
sendfile (int out_fd, int in_fd, off_t *offset, size_t count)
{
        intercept ("sendfile\t", 1);
        set_errno ();
        return -1;
}

ssize_t
sendfile64 (int out_fd, int in_fd, off_t *offset, size_t count)
{
        intercept ("sendfile64", 1);
        set_errno ();
        return -1;
}


int
fcntl (int fd, int cmd, ...)
{
        intercept ("fcntl", 2);
        set_errno ();
        return -1;
}