解析/proc/maps? [英] Parsing /proc/maps?
问题描述
我用C语言编写了以下代码.到目前为止,我要做的是打开proc目录并读取内部的进程-不包含数字字符的字典被忽略了,因为我只想查看数字目录.我要做的是打印proc目录中具有读写权限的所有进程的PID.我的问题是如何从proc/.../maps文件中提取进程的权限?
I wrote the following code in C. What I have done so far is open the proc directory and read the processes inside - dictories that did not consist of numerical characters were simply disregarded as I only want to look at the numeric directories. What I want to do is to print the PID of all the processes in the proc directory that have read and write permissions. My question is how can I extract the permissions of a process from the proc/.../maps file?
这是我遇到麻烦的代码片段:
Here is a snippet of my code where I am having trouble:
sprintf(buf, "/proc/%d/maps", tgid->d_name);
file = fopen(buf, "r");
while (fgets(buf, sizeof(buf), file)) {
sscanf(buf, "%x-%x %4c %x %x:%x", &from, &to, flags, &offset, &major, &minor);
}
fclose(file);
if (flags[0] == 'r' && flags[1] == 'w') {
printf("%d\n", tgid->d_name);
}
推荐答案
我要做的是打印所有可读写进程的PID.
What I want to do is to print the PID of all the processes that are read and writable
流程是某些文件描述符来访问文件(并且您可能会扫描其/proc/1234/fd/
目录)
A process is an abstraction provided by some operating system. It makes no sense to speak of a "readable" process (or a "writable" one), or one with "read" permissions or "write" permissions, since it is about file system permissions. A process is not a file, and its /proc/1234/
directory is just a view into that process given by the kernel. A process is using file descriptors to access files (and you might scan its /proc/1234/fd/
directory).
proc(5)文件系统是Linux专用的伪文件系统. /proc/$pid/maps
是文本视图,其中显示虚拟地址空间一个过程.您可以顺序读取该伪文件.
The proc(5) file system is a pseudo file system specific to Linux. The /proc/$pid/maps
is a textual view showing the virtual address space of a process. You can read that pseudo-file sequentially.
要更改其虚拟地址空间,Linux进程将使用系统调用,例如 mmap(2)和munmap
, mprotect(2),等等...
To change its virtual address space, a Linux process would use system calls like execve(2) (which initializes a fresh virtual address space for a new program), mmap(2) and munmap
, mprotect(2), etc...
要以编程方式扫描目录(例如/proc/1234/fd/
甚至/proc/
),请使用closedir ="nofollow noreferrer"> opendir(3)与closedir
="nofollow noreferrer"> readdir(3)(您可能还会使用 stat(2)在您构建的文件路径上),您可以使用
To scan a directory (such as /proc/1234/fd/
or even /proc/
) programmatically, use opendir(3) with closedir
after having looped on readdir(3) (you'll probably also use stat(2) on a file path that you have constructed) You could use nftw(3) in some cases.
还请阅读 操作系统:三件简单的书 全面了解操作系统.
Read also Operating Systems: Three Easy Pieces to get a broad view about OSes.
我要做的是打印proc目录中具有读写权限的所有进程的PID
What I want to do is to print the PID of all the processes in the proc directory that have read and write permissions
那没有道理. 一个进程没有权限,但是确实有 凭据(7) (用于定义允许该进程访问哪些文件).您可以解析/proc/1234/status
来查找pid 1234进程的凭据(尤其是该伪文件中以Uid:
和Gid:
开头的行).
That has no sense. A process don't have permissions, but it does have credentials(7) (which define what file accesses are permitted to the process). You may parse /proc/1234/status
to find the credentials of process of pid 1234 (in particular, the lines starting with Uid:
and Gid:
from that pseudo-file).
阅读一些不错的Linux编程书,也许是旧的 ALP .另请阅读 syscalls(2), inode(7),系统调用,例如打开(2), read(2) ,等等...(同样也是 errno(3) ).花时间仔细阅读 proc(5) .
Read some good Linux programming book, perhaps the old ALP. Read also syscalls(2), execve(2), inode(7), path_resolution(7) and also the failure cases of file related system calls such as open(2), read(2), etc... (so also errno(3)). Take time to read carefully proc(5).
我的问题是如何从
/proc/.../maps
文件中提取进程的权限?
My question is how can I extract the permissions of a process from the
/proc/.../maps
file?
您不能从该文件中提取此类信息. /proc/1234/maps
伪文件描述了进程1234的虚拟地址空间.在/proc/1234/status
中提供了凭据(以及其他状态信息).谈论过程的权限是毫无意义的.
You cannot extract such information from that file. The /proc/1234/maps
pseudo-file describes the virtual address space of process 1234. The credentials are given (with other status information) in /proc/1234/status
. And it makes no sense to speak of the permissions of a process.
这篇关于解析/proc/maps?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!