LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2006, 04:27 AM   #1
snowing
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Rep: Reputation: 15
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
 
Old 08-30-2006, 08:12 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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.
 
Old 08-30-2006, 08:30 PM   #3
snowing
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Original Poster
Rep: Reputation: 15
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.
 
Old 08-30-2006, 09:20 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Your program works for me after I fixed the readlink() problem.
I don't know what error you are seeing.
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main() {
    char elf_file[65536];
    pid_t pid;
    char string[100];
    int res;


    pid=getpid();
    printf("pid is %d\n", pid);
    snprintf(string,sizeof(string),"/proc/%d/exe",pid);
    printf("string is %s\n", string);
    sleep(20);
    res=readlink(string,elf_file,sizeof(elf_file));
    if (res<=0 || res>=sizeof(elf_file)) {
        perror("readlink");
        return 1;
    }
    elf_file[res]='\0';
    printf("real file is %s\n",elf_file);
    return 0;
}
 
Old 08-30-2006, 10:04 PM   #5
snowing
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Original Poster
Rep: Reputation: 15
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:
[baiwd@hpc-rm ld]$ gcc -Wl,--dynamic-linker test hello.c -o hello
[baiwd@hpc-rm ld]$ cat hello.c
#include <stdio.h>
int main()
{
printf("hello\n");
return 0;
}
[baiwd@hpc-rm ld]$ ./hello
pid is 19738
string is /proc/19738/exe
readlink: No such file or directory
[root@hpc-rm ld]# ll /proc/19738/exe
ls: cannot read symbolic link /proc/19738/exe: No such file or directory
lrwxrwxrwx 1 baiwd baiwd 0 Aug 31 11:01 /proc/19738/exe
 
Old 08-30-2006, 11:09 PM   #6
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Hmm, sorry, the ld man page says this about --dynamic-linker
Code:
The default dynamic linker is normally correct;
don't use this unless you know what you are doing.
Since I never heard of this option, I probably don't know what I'm doing.
Maybe there's someone else that can assist you.
 
Old 08-31-2006, 01:17 AM   #7
snowing
LQ Newbie
 
Registered: Jul 2005
Posts: 26

Original Poster
Rep: Reputation: 15
Thank you for your sparing time. Now another nice person give me the answer:
Quote:
The problem is that your "test" ld is linked to load at the same
address as hello:

$ objdump -p ./hello | grep LOAD | head -1
LOAD off 0x00000000 vaddr 0x08048000 paddr 0x08048000 align 2**12
$ objdump -p ./test | grep LOAD | head -1
LOAD off 0x00000000 vaddr 0x08048000 paddr 0x08048000 align 2**12


You have to get your "test ld" out of the way by linking it at a
different address (using a linker script), or get your "hello"
executable out of the way by building it as a position-independent:


$ gcc -g junk.c -o test -static &&
gcc junk.c -Wl,--dynamic-linker=./test &&
./a.out
pid is 16060
string is /proc/16060/exe
readlink: No such file or directory


$ gcc -fPIC -pie junk.c -Wl,--dynamic-linker=./test &&
./a.out
pid is 16066
string is /proc/16066/exe
real file is /home/paul/a.out
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
hidden directories under proc... what are they for? : (/proc/.23142) syssyphus Linux - General 1 04-10-2006 03:23 PM
Correlation between /proc/devices and /proc/modules ColinLadyka Linux - General 1 02-13-2006 05:25 PM
SETUP.EXE/autorun.exe don't run in wine mst3kman Linux - Games 3 01-22-2006 02:20 PM
Problem with exe files 999113H Mandriva 1 10-26-2003 08:38 PM
What /proc proc file do I need? GoboFraggle Programming 1 02-04-2003 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration