Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Background
I have been working on ld.so.preload to capture open and fopen. Following code is my own c code that is compiled to .so
Code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <sys/syscall.h>
#include <unistd.h>
//this will be used for logging to a file
static void log_it(const char *fmt, ...) {
int test_val = 0;
static int initialized;
static int logfd = -1;
va_list ap;
static int (*open_orig)(const char *, int, mode_t);
if (access("/usr/filelog.txt", F_OK) != -1) {
if (logfd == -1 && !initialized) {
open_orig = dlsym(RTLD_NEXT, "open");
initialized = 1;
if(test_val == 1) {
logfd = open_orig("./filelog.txt", O_WRONLY|O_APPEND|O_CREAT, 0666);
} else {
logfd = open_orig("/usr/filelog.txt", O_WRONLY|O_APPEND|O_CREAT, 0666);
}
}
if (logfd != -1) {
va_start(ap, fmt);
vdprintf(logfd, fmt, ap);
va_end(ap);
}
}
}
static const char *redirect_name(const char *name) {
return name;
}
int open(const char *pathname, int flags, ...){
static int (*open_orig)(const char *, int, mode_t);
int ret;
va_list ap;
mode_t mode;
extern char *program_invocation_name;
extern char *program_invocation_short_name;
if (!open_orig) {
open_orig = dlsym(RTLD_NEXT, "open");
}
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
ret = open_orig(redirect_name(pathname), flags, mode);
log_it("%s is being opened by %s\n", pathname, program_invocation_short_name);
return ret;
}
FILE *fopen(const char *pathname, const char *mode) {
extern char *program_invocation_name;
FILE *(*original_fopen)(const char*, const char*);
original_fopen = dlsym(RTLD_NEXT, "fopen");
log_it("%s is being fopened by %s\n", pathname, program_invocation_name);
return (*original_fopen)(pathname, mode);
}
Furthermore I moved this compiled .so file into /lib64/ and created ld.so.preload inside of /etc/ folder
However, When I run following command
Code:
$openstack server create --flavor m1.tiny --image cirros-0.3.6-x86_64-disk --nic net-id=9453c04e-d739-403e-b5e5-06fed3f4e7e2 --security-group default vm
and looks at the /usr/filelog.txt (since my custom ld.so.preload will capture open and fopen call and save process name inside of the filelog.txt.
I wanted to to be sure so I also ran following line to verify that my ld.so.preload actually loads up and captures all the open and fopen calls
Code:
$strace -f -o ./log.txt openstack server create --flavor m1.tiny --image cirros-0.3.6-x86_64-disk --nic net-id=9453c04e-d739-403e-b5e5-06fed3f4e7e2 --security-group default vm
I also did following command to extract only the open system call from strace output ./log.txt
Code:
$cat ./log.txt | grep "open" >> ./log_open.txt
I compared ./log_open.txt and filelog.txt (created by ld.so.preload) seems like ld.so.preload doesn't actually captures open and fopen calls. It is missing out a lot of the calls.
Question
So the question is that why ld.so.preload can't capture some of the open call but strace can?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.