由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Linux GNU C, readlink问题
相关主题
two general C++ questioncmake coeblock求教
帮忙找个错大家看看这道C语言题是怎么回事?
C#的formated output有点不方便呀。。。。C++里面把数转成字符串的命令是啥啊?
fork(): why both if and else are executed?what's wrong with this scripts?variable passing?
How difficult is it to write your own sprintf ?how to print 2 exponential digits in windows by using Perl
C一个问题搞不懂关于文件命名
问一个link的问题[合集] 如何只用putchar来实现itoa?
再问个fork的题 (转载)Mysterious PgSQL 8.3 crash
相关话题的讨论汇总
话题: readlink话题: exe话题: gnu话题: buf话题: xt
进入Programming版参与讨论
1 (共1页)
xt
发帖数: 17532
1
现在我有一个soft link
/home/xt/test/bin/a.out -> /home/xt/build/a.out
然后我执行test/bin/a.out,进入/proc/pid/exe,可以看到
exe -> /home/xt/test/bin/a.out
但是我用GNU C的readlink读出来,发现得到的是
/home/xt/build/a.out
我现在需要的是/home/xt/test/bin/a.out
谁知道这个问题怎么解决?
m*****e
发帖数: 4193
2
post your code, will you?

【在 xt 的大作中提到】
: 现在我有一个soft link
: /home/xt/test/bin/a.out -> /home/xt/build/a.out
: 然后我执行test/bin/a.out,进入/proc/pid/exe,可以看到
: exe -> /home/xt/test/bin/a.out
: 但是我用GNU C的readlink读出来,发现得到的是
: /home/xt/build/a.out
: 我现在需要的是/home/xt/test/bin/a.out
: 谁知道这个问题怎么解决?

xt
发帖数: 17532
3
//exec_path = new char[SSIZE_MAX];
exec_path = new char[MAX_DIR_LEN];
char buf[40]= { 0 };
ssize_t length;
pid_t pid = getpid();
sprintf(buf, "/proc/%d/exe", pid);
length = readlink(buf, exec_path, 2048);
buf[length] = 0;
if (length < 0) {
printf("Unable to read link '%s':\n", buf);
printf("errno: %d\n", errno);
return NULL;
}
exec_path[ length ]=0;
printf( "exe

【在 m*****e 的大作中提到】
: post your code, will you?
m*****e
发帖数: 4193
4
the /proc/*/exe points to the real executable too
ln -s /bin/ls
./ls -l /proc/self/exe
lrwxrwxrwx 1 xxx xxx 0 Dec 18 13:28 /proc/self/exe -> /bin/ls

【在 xt 的大作中提到】
: //exec_path = new char[SSIZE_MAX];
: exec_path = new char[MAX_DIR_LEN];
: char buf[40]= { 0 };
: ssize_t length;
: pid_t pid = getpid();
: sprintf(buf, "/proc/%d/exe", pid);
: length = readlink(buf, exec_path, 2048);
: buf[length] = 0;
: if (length < 0) {
: printf("Unable to read link '%s':\n", buf);

xt
发帖数: 17532
5
从我这里看,exe指向的是我执行的那个symlink,而那个
symlink指向的是一个executable.
如果我用shell command readlink,得到的是symlink的
路径,但是如果我用GNU C,得到的是最终的executable
路径

【在 m*****e 的大作中提到】
: the /proc/*/exe points to the real executable too
: ln -s /bin/ls
: ./ls -l /proc/self/exe
: lrwxrwxrwx 1 xxx xxx 0 Dec 18 13:28 /proc/self/exe -> /bin/ls

m*****e
发帖数: 4193
6

I don't think so. Both should point to the real executable.

【在 xt 的大作中提到】
: 从我这里看,exe指向的是我执行的那个symlink,而那个
: symlink指向的是一个executable.
: 如果我用shell command readlink,得到的是symlink的
: 路径,但是如果我用GNU C,得到的是最终的executable
: 路径

xt
发帖数: 17532
7
你自己试一试就知道了。

【在 m*****e 的大作中提到】
:
: I don't think so. Both should point to the real executable.

m*****e
发帖数: 4193
8
My second post already told you how to tell.

【在 xt 的大作中提到】
: 你自己试一试就知道了。
xt
发帖数: 17532
9
反正这不是我得到的结果。我用的是RHEL5.0 x64

【在 m*****e 的大作中提到】
: My second post already told you how to tell.
xt
发帖数: 17532
10
嗯,我又到一台RHEL4-x64上测试了一下,发现没有这个问题,
/proc/pid/exe和readlink()返回的都是那个executable的路径,
而不是link的路径
看来是Redhat 5本身的问题。

【在 m*****e 的大作中提到】
: My second post already told you how to tell.
1 (共1页)
进入Programming版参与讨论
相关主题
Mysterious PgSQL 8.3 crashHow difficult is it to write your own sprintf ?
C语言一个passing variable的问题C一个问题搞不懂
easy problem coconut问一个link的问题
请教Matlab和IDL的处理数据能力差异再问个fork的题 (转载)
two general C++ questioncmake coeblock求教
帮忙找个错大家看看这道C语言题是怎么回事?
C#的formated output有点不方便呀。。。。C++里面把数转成字符串的命令是啥啊?
fork(): why both if and else are executed?what's wrong with this scripts?variable passing?
相关话题的讨论汇总
话题: readlink话题: exe话题: gnu话题: buf话题: xt