Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there a way to tell if a process opened a file with the O_SYNC flag? I'm thinking lsof might be able to do this, but cannot find a way.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

This can be done using a systemtap script. This one has been taken from here, and does exactly what you want:


# 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()
}

Two examples on how to use it are provided in the referenced link, I'll reproduce one of them here:

[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

You can replace O_DIRECT with O_SYNC for your purpose.

Further references:

# # # # # #

share|improve this answer
    
Awesome, I didn't now about this tool! I had to edit your post as some of the characters were being escaped by serverfault. –  Dave Jul 3 '13 at 18:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.