/* setlocale_hook.c
*
* Build: cc -shared -fPIC -O2 -o setlocale_hook.so setlocale_hook.c
* Use: LD_PRELOAD=/full/path/setlocale_hook.so
*/
#include <sys/types.h>
#include <sys/sysctl.h>
#include <dlfcn.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define TARGET_NAME "packagekitd"
#define EXIT_CODE 0
/* Returns 1 if the running executable is TARGET_NAME. */
static int
is_target(void)
{
static int cached = -1;
char path[PATH_MAX];
size_t len = sizeof(path);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
if (cached != -1)
return cached;
/* Prefer the real executable path; argv[0]/getprogname() can be spoofed. */
if (sysctl(mib, 4, path, &len, NULL, 0) == 0) {
char *base = basename(path);
cached = (base != NULL && strcmp(base, TARGET_NAME) == 0);
} else {
const char *pn = getprogname();
cached = (pn != NULL && strcmp(pn, TARGET_NAME) == 0);
}
return cached;
}
char *
setlocale(int category, const char *locale)
{
static char *(*real_setlocale)(int, const char *) = NULL;
if (is_target())
_exit(EXIT_CODE); /* use exit() if you want atexit handlers to run */
if (real_setlocale == NULL) {
real_setlocale = (char *(*)(int, const char *))
dlsym(RTLD_NEXT, "setlocale");
if (real_setlocale == NULL)
_exit(127);
}
return real_setlocale(category, locale);
}