<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cyher.NET &#187; proc</title>
	<atom:link href="http://cyher.net/tag/proc/feed" rel="self" type="application/rss+xml" />
	<link>http://cyher.net</link>
	<description>Technology, Life, Code, OpenSource, *nix, Mac and iphone, Mobile development</description>
	<lastBuildDate>Sun, 25 Jul 2010 08:43:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>linux进程可执行文件的绝对路径</title>
		<link>http://cyher.net/programming/linux-process-absolute-path</link>
		<comments>http://cyher.net/programming/linux-process-absolute-path#comments</comments>
		<pubDate>Thu, 24 Sep 2009 08:55:46 +0000</pubDate>
		<dc:creator>cyher</dc:creator>
				<category><![CDATA[GNU/linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[proc]]></category>

		<guid isPermaLink="false">http://cyher.net/?p=637006</guid>
		<description><![CDATA[这种情况很少遇到，我们需要一个运行中进程的绝对路径，可惜的是glibc中或者系统调用中我都没有找到类似的函数。但是linux绝对不会不给你这个机会的，那就是proc文件系统了。在proc文件系统中那些数字都是运行中的进程，进入一个文件名为数字的文件夹以后，我们就可以发现以下类似文件目录结构。

&#91;cyher@cyher ~&#93;$ ls /proc/3355/
attr             cpuset   io        mountinfo   pagemap      smaps    task
auxv             cwd      latency   mounts      personality  stack    wchan
cgroup           environ  limits    mountstats  root         stat
clear_refs       exe      loginuid  net         sched        statm
cmdline          fd       maps      oom_adj     schedstat    status
coredump_filter  fdinfo   mem       oom_score   sessionid    syscall

这里就是一个进程所有的信息了大名鼎鼎的ps命令就是读取这里的内容解析出信息的，这里是ps的官方网站 http://procps.sourceforge.net/
那好了，就用这里给的信息来解析出执行文件的绝对路径吧

/*
 * =====================================================================================
 *
 *       Filename:  get_exe_path.c
 *
 *    [...]]]></description>
			<content:encoded><![CDATA[<p>这种情况很少遇到，我们需要一个运行中进程的绝对路径，可惜的是glibc中或者系统调用中我都没有找到类似的函数。但是linux绝对不会不给你这个机会的，那就是proc文件系统了。在proc文件系统中那些数字都是运行中的进程，进入一个文件名为数字的文件夹以后，我们就可以发现以下类似文件目录结构。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>cyher@cyher ~<span style="color: #009900;">&#93;</span>$ ls <span style="color: #339933;">/</span>proc<span style="color: #339933;">/</span><span style="color: #0000dd;">3355</span><span style="color: #339933;">/</span>
attr             cpuset   io        mountinfo   pagemap      smaps    task
auxv             cwd      latency   mounts      personality  stack    wchan
cgroup           environ  limits    mountstats  root         stat
clear_refs       exe      loginuid  net         sched        statm
cmdline          fd       maps      oom_adj     schedstat    status
coredump_filter  fdinfo   mem       oom_score   sessionid    syscall</pre></div></div>

<p>这里就是一个进程所有的信息了大名鼎鼎的ps命令就是读取这里的内容解析出信息的，这里是ps的官方网站 <a href="http://procps.sourceforge.net/" target="_blank">http://procps.sourceforge.net/</a></p>
<p>那好了，就用这里给的信息来解析出执行文件的绝对路径吧</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
 * =====================================================================================
 *
 *       Filename:  get_exe_path.c
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  2009年09月23日 17时07分17秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  cyher (), cyher.net@gmail.com
 *        Company:  cyher.net
 *
 * =====================================================================================
 */</span>
&nbsp;
<span style="color: #339933;">#include</span>
<span style="color: #339933;">#include</span>
<span style="color: #339933;">#include</span>
<span style="color: #339933;">#define BUF 128</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> agrc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> buf<span style="color: #009900;">&#91;</span>BUF<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> proc<span style="color: #009900;">&#91;</span>BUF<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>p<span style="color: #339933;">;</span>
&nbsp;
    sprintf<span style="color: #009900;">&#40;</span>proc<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;/proc/%d/exe&quot;</span><span style="color: #339933;">,</span> atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    readlink<span style="color: #009900;">&#40;</span>proc<span style="color: #339933;">,</span>buf<span style="color: #339933;">,</span> BUF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*proc/pid/exe 是一个链接，用readlink读*/</span>
    p <span style="color: #339933;">=</span> strchr<span style="color: #009900;">&#40;</span>buf<span style="color: #339933;">,</span><span style="color: #ff0000;">'('</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*读出的路径后面有可能会有 (deleted)字样，删去*/</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>p ！<span style="color: #339933;">=</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        p<span style="color: #339933;">--;</span>
        <span style="color: #339933;">*</span>p <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    puts<span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>这样就能读取出绝对路径了，不过你首先要知道pid啊 呵呵。</p>

	标签：<a href="http://cyher.net/tag/c" title="c" rel="tag">c</a>, <a href="http://cyher.net/tag/code" title="code" rel="tag">code</a>, <a href="http://cyher.net/tag/linux" title="linux" rel="tag">linux</a>, <a href="http://cyher.net/tag/proc" title="proc" rel="tag">proc</a><br />

	<h4>相关日志</h4>
	<ul class="st-related-posts">
	<li><a href="http://cyher.net/gnulinux/%e5%ad%a6%e4%b9%a0%e7%bb%8f%e9%aa%8c" title="自己的嵌入式学习经验, 想不到别人给拍成视频了 (2009年08月9号)">自己的嵌入式学习经验, 想不到别人给拍成视频了</a> (0)</li>
	<li><a href="http://cyher.net/gnulinux/%e8%87%aa%e5%b7%b1%e6%9b%be%e7%bb%8f%e4%bd%9c%e8%bf%87%e7%9a%84%e5%b0%8f%e5%ae%9e%e9%aa%8c" title="自己曾经作过的小实验 (2009年08月16号)">自己曾经作过的小实验</a> (0)</li>
	<li><a href="http://cyher.net/apple/macos-x/technology_is_a_kind_of_belief_appl" title="科技是一种信仰(apple&#038;linux) (2009年01月12号)">科技是一种信仰(apple&#038;linux)</a> (5)</li>
	<li><a href="http://cyher.net/gnulinux/%e6%97%b6%e9%9a%94%e5%87%a0%e4%b8%aa%e6%9c%88%ef%bc%8c%e6%b2%a1%e6%83%b3%e5%88%b0%e6%88%91%e5%8f%88%e7%94%a8%e4%b8%8alinux%e4%ba%86%e3%80%82%e3%80%82%e3%80%82" title="时隔几个月，没想到我又用上linux了。。。 (2008年06月30号)">时隔几个月，没想到我又用上linux了。。。</a> (0)</li>
	<li><a href="http://cyher.net/hardware/%e5%a4%a7%e6%95%8c%e5%bd%93%e5%89%8d-%e5%a1%9e%e7%8f%ad%e5%92%8c%e5%be%ae%e8%bd%af%e5%9d%87%e5%8f%97ubuntu%e5%a8%81%e8%83%81" title="大敌当前 塞班和微软均受Ubuntu威胁 (2007年04月21号)">大敌当前 塞班和微软均受Ubuntu威胁</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://cyher.net/programming/linux-process-absolute-path/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
