[英]Get the absolute path of current running program in kernel
为了检索正在运行的程序的文件权限,我需要在当前正在运行的程序上执行kstat
。 然后,我需要获取加载的ELF图像的绝对路径。
那可能吗? current->comm
只记录没有路径的程序名称。
或还有其他方法可以做到?
您可以使用/proc/self/exe
的路径readlink(2)
,在您的情况下,这将是指向ELF的链接。 使用readlink(1)
:
$ readlink /proc/self/exe
/bin/readlink
据我所知,仅Linux。
proc
文件系统在/proc/<pid>/exe
返回此信息,因此您需要执行类似的操作。
proc
在fs/proc/base.c
中的proc_exe_link
函数中获取进程的路径。 此函数改编自proc_exe_link
:
char *get_current_proc_path(char *buf, int buflen)
{
struct file *exe_file;
char *result = ERR_PTR(-ENOENT);
struct mm_struct *mm;
mm = get_task_mm(current);
if (!mm) {
goto out;
}
down_read(&mm->mmap_sem);
exe_file = mm->exe_file;
if (exe_file) {
get_file(exe_file);
path_get(&exe_file->f_path);
}
up_read(&mm->mmap_sem);
mmput(mm);
if (exe_file) {
result = d_path(&exe_file->f_path, buf, buflen);
path_put(&exe_file->f_path);
fput(exe_file);
}
out:
return result;
}
这会将路径放置在buf
(不一定是缓冲区的开始),并返回指向该路径的指针。 上的错误,它会返回一个ERR_PTR
,所以检查指针是有效IS_ERR
。
您是说getcwd()吗? http://man7.org/linux/man-pages/man2/getcwd.2.html
#include <unistd.h>
char * getcwd(char * buf, size_t size);
- - - - - - - - - 编辑 - - - - - - - - - - -
您想检查另一个正在运行的程序的路径,可以在/ proc / $ PID / exe中找到它:
ll /proc/$PID/exe
要么
ll /proc/$PID | grep exe
要么
readlink /proc/$PID/exe
Table 1-5: Kernel info in /proc ..............................................................................
File Content
apm Advanced power management info
buddyinfo Kernel memory allocator information (see text) (2.5)
bus Directory containing bus specific information
**cmdline** Kernel command line
例如
[root@localhost ~]# more /proc/364/cmdline
/usr/sbin/smbd
[root@localhost ~]# more /proc/364/common
/proc/364/common: No such file or directory
[root@localhost ~]# more /proc/364/comm
smbd
[root@localhost ~]#
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.