【问题标题】:printing open files of current process打印当前进程的打开文件
【发布时间】:2011-11-13 22:34:40
【问题描述】:

我需要在 C Linux 中找到当前进程的打开文件。到目前为止,我能想到的只是当前 -> task_struct ...然后没有资源日志...最终我应该到达open_fds?另外,端点是位图文件吗?您将如何从位图结构或其他一些奇怪的结构中获取打开的文件?

【问题讨论】:

  • 你需要在内核中这样做吗?如果没有,请查看/proc/$pid/fd。
  • 或简单地(从您的进程内部)在/proc/self/fd/ 或/proc/self/fdinfo/- 两者都是[pseudo-] 目录。
  • @user963070: urfffff... 你为什么不在问题中告诉我们你想要做什么,并向我们展示你所在的代码有current->task_struct 这样我们就不必通灵并猜测您忘记添加内核标签了吗?
  • 击中 tast_struct *task; for (task = current; task != &init_task; task = task->parent){ printk("%s", task->xxxxopenFileNamexxxxxx); } 不知道 x 里面应该是什么

标签: c linux process linux-kernel file-descriptor


【解决方案1】:

默认情况下,内核允许每个进程打开NR_OPEN_DEFAULT 文件。此值已定义 在 include/linux/sched.h 中,默认设置为 BITS_PER_LONG。因此,在 32 位系统上,初始文件数为 32; 64位系统可以同时处理64个文件。

在文件.h中

    struct files_struct {
    42  /*
    43   * read mostly part
    44   */
    45        atomic_t count;
    46        struct fdtable *fdt;
    47        struct fdtable fdtab;
    48  /*
    49   * written part on a separate cache line in SMP
    50   */
    51        spinlock_t file_lock ____cacheline_aligned_in_smp;
    52        int next_fd;
    53        struct embedded_fd_set close_on_exec_init;
    54        struct embedded_fd_set open_fds_init;
    55        struct file * fd_array[NR_OPEN_DEFAULT];
    56};

在内核中,每个打开的文件都由一个文件描述符表示,该文件描述符充当位置索引 特定于进程的数组(task_struct->files->fd_array)。该数组包含上述文件结构的一个实例,其中包含每个打开文件的所有必要文件信息。

通过循环fd_array,可以得到进程所有打开文件的信息。

【讨论】:

    【解决方案2】:

    在命令行lsof

    在 C 中,类似这样:

    这是一个程序的注释代码,它在屏幕上打印一个它自己打开的文件列表:

    #include <unistd.h>
    #include <stdio.h>
    #include <dirent.h>
    
    int main() {
        // the directory we are going to open
        DIR           *d;
    
        // max length of strings
        int maxpathlength=256;
    
        // the buffer for the full path
        char path[maxpathlength];
    
        // /proc/PID/fs contains the list of the open file descriptors among the respective filenames
        sprintf(path,"/proc/%i/fd/",getpid() );
    
        printf("List of %s:\n",path);
    
        struct dirent *dir;
        d = opendir(path);
        if (d) {
            //loop for each file inside d
            while1 != NULL) {
    
                //let's check if it is a symbolic link
                if (dir->d_type == DT_LNK) {
    
                    const int maxlength = 256;
    
                    # string returned by readlink()
                    char hardfile[maxlength];
    
                    #string length returned by readlink()
                    int len;
    
                    # tempath will contain the current filename among the fullpath
                    char tempath[maxlength];
    
                    sprintf(tempath,"%s%s",path,dir->d_name);
                    if2!=-1) {
                        hardfile[len]='\0';
                            printf("%s -> %s\n", dir->d_name,hardfile);
    
                    } else
                        printf("error when executing readlink() on %s\n",tempath);
    
                }
            }
    
            closedir(d);
        }
        return 0;
    }
    

    这段代码来自:http://mydebian.blogdns.org/?p=229,缓存在这里:http://tinyurl.com/6qlv2nj

    见:

    How to use lsof(List Opened Files) in a C/C++ application?

    http://www.linuxquestions.org/questions/linux-security-4/c-library-for-lsof-183332/

    您也可以通过popen 调用来使用lsof 命令。

    【讨论】:

    • 可能不会。原始海报提到“C 中的当前进程”。
    • @BasileStarynkevitch :那是“它自己打开的文件的列表”......这就是代码示例所做的。
    • @BasileStarynkevitch:没有比这更多的 C。这简直是​​一个很好的答案。
    • 给定的代码是错误的。构造while1在C中不存在,'dir'指针未赋值。
    • 是的 - 看起来像是错字
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多