最近在L1nux下學習進程管理,遇到了一些問題,想在這里請教一下大家:
1.在/proc/<pid>/stat里有一個字段是sessi0n id,這個值和/proc/<pid>/sessi0nid里的值不一樣,這是為什么?到底哪一個是正確的? 我在man proc里沒有找到關於文件sessi0nid的解釋;
2.如何能夠判斷一個進程是32或64 bit的?我查看了一些資料,似乎通過一些配置,32bit的程序在64bit平台上市可以運行起來的;
3.通過lsof -p,怎樣可以判斷哪些是進程動態加載的庫?雖然通過文件名與路徑可以判斷,但是我覺得這樣不是一個很穩妥的方式.我認為所有FD為mem,TYPE為REG的文件,就應該是動態庫文件了,請問一下大家怎么覺得呢?
我剛接觸Linux不久,可能問題比較幼稚.之前在網上搜索也沒有找到期望的答案,很多搜索到的解釋都比較籠統,所以在這里提問,希望大家能不吝賜教 ^.^
6 个解决方案
1、
/proc/%d/sessionid 這個確實是sessionid
對應內核函數
static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
size_t count, loff_t *ppos)
{
...
length = scnprintf(tmpbuf, TMPBUFLEN, "%u", audit_get_sessionid(task));
...
}
/proc/%d/stat 里面並沒有找到什么代表sessionid的東西,不過有一個sid,你可能把sessionid和sid搞混了
對應內核函數
static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,struct pid *pid, struct task_struct *task, int whole)
{
...
sid = task_session_nr_ns(task, ns);
...
seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
%lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
pid_nr_ns(pid, ns),
tcomm,
state,
ppid,
pgid,
sid,
tty_nr,
tty_pgrp,
task->flags,
min_flt,
cmin_flt,
maj_flt,
cmaj_flt,
cputime_to_clock_t(utime),
cputime_to_clock_t(stime),
cputime_to_clock_t(cutime),
cputime_to_clock_t(cstime),
priority,
nice,
num_threads,
start_time,
vsize,
mm ? get_mm_rss(mm) : 0,
rsslim,
mm ? mm->start_code : 0,
mm ? mm->end_code : 0,
(permitted && mm) ? mm->start_stack : 0,
esp,
eip,
/* The signal information here is obsolete.
* It must be decimal for Linux 2.0 compatibility.
* Use /proc/#/status for real-time signals.
*/
task->pending.signal.sig[0] & 0x7fffffffUL,
task->blocked.sig[0] & 0x7fffffffUL,
sigign .sig[0] & 0x7fffffffUL,
sigcatch .sig[0] & 0x7fffffffUL,
wchan,
0UL,
0UL,
task->exit_signal,
task_cpu(task),
task->rt_priority,
task->policy,
(unsigned long long)delayacct_blkio_ticks(task),
cputime_to_clock_t(gtime),
cputime_to_clock_t(cgtime));
...
}
所以歸根到底你的問題應該是sessionid和sid有什么區別
看內核結構體
struct task_struct {
...
/* namespaces */
struct nsproxy *nsproxy;
...
#ifdef CONFIG_AUDITSYSCALL
uid_t loginuid;
unsigned int sessionid;
#endif
...
}
audit_get_sessionid 直接返回的是task_struct 的sessionid; task_session_nr_ns 返回的是 nsproxy里的pid_ns;
可以看出sessionid 只有在CONFIG_AUDITSYSCALL開啟時才會有,sid總是存在,2者並沒有什么必然聯系。
1. 在我的機器上,以當前的終端打開的bash為例:
$ ps -eo sess,pid,fname | grep bash
30789 30789 bash
所以bash的session id和pid都為30789;
2. 查看sessionid
$cat /proc/30789/sessionid
2
這個2不知道是什么意思?查看/proc/30789/stat中的確實是session id,這個sessionid的文件保存的不知道是什么。
問題3
如果你僅僅想知道那些是進程需要的動態庫
在它還是程序的時候就能用ldd 查看
lsof -p 查看的話
我贊同動態庫都是FD為mem,TYPE為REG的文件
但還是不確定所有FD為mem,TYPE為REG的文件都是動態庫
問題2 不會
引用 3 樓 的回復:
問題3
如果你僅僅想知道那些是進程需要的動態庫
在它還是程序的時候就能用ldd 查看
lsof -p 查看的話
我贊同動態庫都是FD為mem,TYPE為REG的文件
但還是不確定所有FD為mem,TYPE為REG的文件都是動態庫
問題1
按照man proc的說法,stat里的是session id
/proc/[pid]/stat
Status information about the process. This is used by ps(1).
It is defined in /usr/src/linux/fs/proc/array.c.
The fields, in order, with their proper scanf(3) format speci-
fiers, are:
pid %d The process ID.
comm %s The filename of the executable, in parentheses.
This is visible whether or not the executable is
swapped out.
state %c One character from the string "RSDZTW" where R is
running, S is sleeping in an interruptible wait, D
is waiting in uninterruptible disk sleep, Z is zom-
bie, T is traced or stopped (on a signal), and W is
paging.
ppid %d The PID of the parent.
pgrp %d The process group ID of the process.
session %d The session ID of the process.
通過ps看到的sessionid也是stat里的數值(如2樓所說),同時如果在程序中調用getsid(),獲得的也與上述一致.所以現在我仍然對這兩個session id的作用有所疑惑,也許stat中的針對進程,而seesionid中的針對用戶?
問題3
ldd似乎只能查看到隱式調用的庫(在進程剛開始運行時就需要動態加載的庫),而我也需要查看dlopen顯式調用的庫,所以才想到用lsof.
非常感謝您用這么久時間來幫我解答^.^
問題1
無論如何,用戶查看到/proc都是由內核給出的
內核態和用戶態,可能同一個東西叫類似的名字但不完全相同。
/proc/[pid]/stat 得到的,getsid() ,setsid() 操作的都是同一個東西
這個東西在用戶態可以叫做session id ,
幾個進程組可以合並成一個會話(session),這個特性用在終端程序設計中,我知道的不多。
但是能明確/proc/[pid]/stat 得到的,getsid() ,setsid() 操作的都是同一個東西
/proc/%d/sessionid 在內核態叫sessionid,在用戶態不知道叫什么。但是跟上面說的session id不是一個東西。/proc/%d/sessionid 是用在審計控制的。
雖然名字類似,但他們最終的作用不同,所以值可以不同。