4 * Compile with: gcc -D _GNU_SOURCE -shared -fPIC -o pathrewrite.so pathrewrite.c
6 * Usage: LD_PRELOAD=./pathrewrite.so PATHREWRITE_FROM=/usr/bin PATHREWRITE_TO=/usr/local/bin someprogramm ...
13 #include <bits/posix1_lim.h>
16 void pathrewrite_rewrite(const char *orig
, char *rw
, const char *caller_name
)
18 char temp
[_POSIX_PATH_MAX
];
24 pattern
= getenv("PATHREWRITE_FROM");
25 newstring
= getenv("PATHREWRITE_TO");
27 if(pattern
!= NULL
&& newstring
!= NULL
)
29 index
= strstr(rw
, pattern
);
32 //fprintf(stderr, "index = %s\n", index);
33 strcpy(temp
, &index
[strlen(pattern
)]);
34 strcpy(index
, newstring
);
35 strcpy(&index
[strlen(newstring
)], temp
);
36 fprintf(stderr
, "pathrewrite.so: %s(%s -> %s)\n", caller_name
, orig
, rw
);
42 int __xstat(int ver
, const char *path
, struct stat
*buf
)
44 char pathrw
[_POSIX_PATH_MAX
];
46 pathrewrite_rewrite(path
, pathrw
, __func__
);
48 int (*orig___xstat
)(int, const char *, struct stat
*) = dlsym(RTLD_NEXT
, "__xstat");
49 return orig___xstat(ver
, pathrw
, buf
);
52 int __lxstat(int ver
, const char *path
, struct stat
*buf
)
54 char pathrw
[_POSIX_PATH_MAX
];
56 pathrewrite_rewrite(path
, pathrw
, __func__
);
58 int (*orig___lxstat
)(int, const char *, struct stat
*) = dlsym(RTLD_NEXT
, "__lxstat");
59 return orig___lxstat(ver
, pathrw
, buf
);
63 int open(const char *pathname
, int flags
, mode_t mode
)
65 char pathrw
[_POSIX_PATH_MAX
];
67 pathrewrite_rewrite(pathname
, pathrw
, __func__
);
69 int (*orig_open
)(const char *, int, mode_t
) = dlsym(RTLD_NEXT
, "open");
70 return orig_open(pathrw
, flags
, mode
);
73 FILE *fopen(__const
char *__restrict pathname
, __const
char *__restrict mode
)
75 char pathrw
[_POSIX_PATH_MAX
];
77 pathrewrite_rewrite(pathname
, pathrw
, __func__
);
79 FILE *(*orig_fopen
)(__const
char *__restrict
, __const
char *__restrict
) = dlsym(RTLD_NEXT
, "fopen");
80 return orig_fopen(pathrw
, mode
);