problem on /proc/self/exe and /proc/num/exe
In my project, I encountered a strange problem. As you know readlink(2) can be used to get the real running program by /proc/self/exe. For example:
on RHELU4 and RH9 test.c: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { char elf_file[65536]; pid_t pid; char string[100]; pid = getpid(); printf("pid is %ld\n", pid); sprintf(string, "/proc/%ld/exe", pid); printf("string is %s\n", string); sleep(20); if (readlink(string, elf_file, 65536) == -1) { perror("readlink"); return -1; } printf("real file is %s\n", elf_file); } compiled by "gcc test.c -o test -static". On ia64, x86_64 and i386, it's all OK. But if I run test as a ld, compiled with "gcc -Wl,--dynamic-linker test hello.c -o hello". It will fail on i386 and x86_64, but OK on ia64. [root@hpc-rm ld]# ./hello pid is 22226 string is /proc/22226/exe readlink: No such file or directory [root@hpc-rm ld]# ./test pid is 22239 string is /proc/22239/exe real file is /home/baiwd/ld/test And on i386 and x86_64, when I "ll /proc/pid[num]/", there is /proc/pid/exe, but when I "ll /proc/pid[num]/exe", it reports "No such file or directory". Could you please tell me why? Thank you very much |
Try using code tags around you source in the posts, makes it much easier to read.
readlink() does not place a null terminator on the end, you have to do that yourself using the return code as the index, after range checking it of course. See the man 2 readlink. |
Thank you. But I am afraid that maybe it's not caused by readlink(2). In fact the "/proc/pidnumber/exe" does not exist, because when using command "ll /proc/pidnumber/exe", it reports "No such file or directory" too. So readlink failed.
|
Your program works for me after I fixed the readlink() problem.
I don't know what error you are seeing. Code:
#include <stdio.h> |
if I run the program test directly, it's all right, every thing is OK. But I want to use test as a faked linker and I want to figure out which program is running in this faked ld. But the /proc/<pid>/exe does not exist.
Quote:
|
Hmm, sorry, the ld man page says this about --dynamic-linker
Code:
The default dynamic linker is normally correct; Maybe there's someone else that can assist you. |
Thank you for your sparing time. Now another nice person give me the answer:
Quote:
|
All times are GMT -5. The time now is 02:40 PM. |