2-Cent Tips
2 cent tip: Determining dynamic shared libraries loaded during run-time of a program
Mulyadi Santosa [mulyadi.santosa at gmail.com]
Wed, 15 Aug 2007 20:34:22 +0700
Good day LG readers!
How do you find out the library dependency of your program? Sure, ldd helps but doesn't always help in every circumstances. Some programs load the needed dynamic libraries with the help of dlopen() function, making ldd unaware of them. But you need to track them all, let's say in order to setup a chroot jail. So, how do you detect them?
To explain this concept, take a look on the below codes (taken from man dlopen):
#include <stdio.h> #include <dlfcn.h>
int main(int argc, char **argv) { void *handle; double (*cosine) (double); char *error;
handle = dlopen("libm.so", RTLD_LAZY); if (!handle) { fprintf(stderr, "%s\n", dlerror()); exit(1); }
cosine = dlsym(handle, "cos"); if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", error); exit(1); }
printf("%f\n", (*cosine) (1.0)); dlclose(handle); return 0; } Assume you save it as trace.c. Later you compile it with: $ gcc -ldl -o trace trace.c
ldd shows you this: $ ldd ./trace libdl.so.2 => /lib/libdl.so.2 (0x40029000) libc.so.6 => /lib/tls/libc.so.6 (0x42000000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
OK, where's that libm.so? It doesn't show up. This won't be a serious problem if you have its source code, but what if not? ltrace comes to rescue. You just need to tell ltrace to find the occurence of dlopen(): $ ltrace -f -e dlopen ./trace dlopen("libm.so", 1) ....
-f tells ltrace to trace its forked children, not just the parent process. Now, the rest is just a matter of finding those libraries in library search path i.e any paths that are mentioned in /etc/ld.so.conf or LD_LIBRARY_PATH environment variable.
reference: man dlopen
regards,
Mulyadi
[ Thread continues here (4 messages/5.96kB) ]
2 cent tip: Prevent Vim to accidentally write to opened file
Mulyadi Santosa [mulyadi.santosa at gmail.com]
Wed, 15 Aug 2007 16:46:51 +0700
Hello everybody
Most of us use "view" or "vim -R" in order to execute Vim in readonly mode. However, sometimes we accidentally force writing by issuing ":w!" command.
To prevent this, you can try: $ vim -m <file>
It really stops you from writing the modified buffer back to the backing file. The only exception is when you manually turn write mode on via: :set write
have fun!
NB: Mr. Okopnik, please include this tip in the next LG release if you think this tip is useful for the LG audience. Thanks in advance.
regards,
Mulyadi.
[ Thread continues here (2 messages/2.12kB) ]