Lsof

Linux:判斷文件是否使用 O_SYNC 打開

  • October 16, 2013

有沒有辦法判斷程序是否打開了帶有 O_SYNC 標誌的文件?我在想 lsof 可能能夠做到這一點,但找不到辦法。

這可以使用systemtap腳本來完成。這個取自這裡,完全符合您的要求:


# list_flags.stp
# Copyright (C) 2007 Red Hat, Inc., Eugene Teo 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#

%{ 
#include <linux/file.h>
%}

function list_flags:long (pid:long, fd:long) %{
       struct task_struct *p;
       struct list_head *_p, *_n;

       list_for_each_safe(_p, _n, &current->tasks) {
               p = list_entry(_p, struct task_struct, tasks);
               if (p->pid == (int)THIS->pid) {
                       struct file *filp;
                       struct files_struct *files = p->files;
                       spin_lock(&files->file_lock);
                       filp = fcheck_files(files, (int)THIS->fd);
                       THIS->__retvalue = (!filp ? -1 : filp->f_flags);
                       spin_unlock(&files->file_lock);
                       break;
               }
       }
%}

probe begin {
       flag_str = ( (flags = list_flags($1, $2)) ? _sys_open_flag_str(flags) : "???");
       printf("pid: %d, fd: %d: %s\n", $1, $2, flag_str)
       exit()
}

參考連結中提供了兩個有關如何使用它的範例,我將在此處複製其中一個範例:

[eteo@kerndev ~]$ stap -vg list_flags.stp $$ 3 2>&1 | grep O_DIRECT
pid: 30830, fd: 3: O_RDONLY|O_APPEND|O_CREAT|O_DIRECT|O_DIRECTORY|O_EXCL|O_LARGEFILE|O_NOATIME|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK|O_SYNC|O_TRUNC

您可以根據自己的目的替換O_DIRECTO_SYNC

更多參考資料:

# # # #

引用自:https://serverfault.com/questions/520391