title: change file open mode with a kernel module
body: <p>I'm doing some test with a kernel module for change a file open mode of a process from write to readonly with the following code, I take the file descriptor number from <code>lsof -p &lt;pid of process&gt;</code> and compile the module &amp;&amp; insmod changefilemode.ko it works on fedora 19, but if Ido it on RedHat 5, the file descriptor is closed.</p>
  
  <pre><code>  #include &lt;linux/kernel.h&gt;
    #include &lt;linux/module.h&gt;
    #include &lt;linux/init.h&gt;
    #include &lt;linux/sched.h&gt;
    #include &lt;linux/file.h&gt;
    #include &lt;linux/fs.h&gt;
  
  
    struct files_struct *get_files_struct(struct task_struct *task)
    {
         struct files_struct *files;
  
         task_lock(task);
         files = task-&gt;files;
         if (files)
                atomic_inc(&amp;files-&gt;count);
         task_unlock(task);
  
         return files;
     }
  
  
     MODULE_LICENSE("GPL");
  
     static int __init myinit(){
           struct task_struct *tsk;
           for_each_process(tsk){
  
           if(tsk-&gt;pid == 11923){
                      struct files_struct *files = get_files_struct(tsk);
                      task_lock(tsk);
                      printk("\tpid %d - file mode %d\n",tsk-&gt;pid, files-&gt;fd_array[6]-&gt;f_mode);
                      files-&gt;fd_array[6]-&gt;f_mode = FMODE_READ;
                      printk("\tpid %d - file mode %d\n",tsk-&gt;pid, files-&gt;fd_array[6]-&gt;f_mode);
                      task_unlock(tsk);
                 }
           }
        return 0;
     }
  
  static void  __exit myexit(){
      printk("Good Bye from exit");
  }
  
  
  module_init(myinit);
  module_exit(myexit);
  </code></pre>