<?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>Mon, 13 Feb 2012 10:12:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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>
]]></content:encoded>
			<wfw:commentRss>http://cyher.net/programming/linux-process-absolute-path/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

