CMakeLists.txt
project(open-hook)
cmake_minimum_required(VERSION 3.4)
add_library(open3-hook SHARED
open_hook.c)
target_link_libraries(open3-hook -ldl)
open_hook.c
// Copyright (c) 2022 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <dlfcn.h>
static int g_inited = 0;
typedef int (*open3_fn_t)(const char *path, int flag, int mode);
static open3_fn_t true_open3 = NULL;
static void* load_sym(char* symname) {
void* funcptr = dlsym(RTLD_NEXT, symname);
if (!funcptr) {
fprintf(stderr, "Cannot load symbol '%s' %s\n", symname, dlerror());
exit(1);
} else {
printf("loaded symbol '%s'" " real addr %p\n", symname, funcptr);
}
if (funcptr == NULL) {
fprintf(stderr, "funcptr is null!\n");
abort();
}
return funcptr;
}
static void init_symbols() {
if (g_inited == 0) {
write(1, "init_symbols()\n", 16);
true_open3 = load_sym("open");
g_inited = 1;
}
}
static int sys_open(const char* path, int flags, mode_t mode) {
printf("sys_open()\n");
return openat(AT_FDCWD, path, flags, mode);
}
int open(const char* path, int flags, ...) {
printf("open3(%s)\n", path);
mode_t mode = 0;
init_symbols();
if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
}
return sys_open(path, flags, mode);
//return true_open3(path, flags, mode);
}
build.sh
#!/usr/bin/env bash
# leehom Chen clh021@gmail.com
cmake -Bbuild
cmake --build build
test.sh
#!/usr/bin/env bash
# leehom Chen clh021@gmail.com
export LD_PRELOAD=$(pwd)/build/libopen3-hook.so
# $HOME/Desktop/lithium/lithium https://bing.com
$HOME/Desktop/lithium/lithium-engine \
-test \
$HOME/.cache/lithium_runtimes/runtimes/7662198e44f82b9480c2f31b94052fb4/application.ini \
https://bing.com "{}"