4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * The full GNU General Public License is included in this distribution
19 * in the file called LICENSE.GPL.
21 * Contact Information:
25 #include <linux/eventfd.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/rcupdate.h>
30 #include <linux/file.h>
31 #include <linux/slab.h>
33 #include <linux/mmu_context.h>
34 #include <linux/sched.h>
35 #include <asm/mmu_context.h>
36 #include <linux/fdtable.h>
38 #include "eventfd_link.h"
42 * get_files_struct is copied from fs/file.c
45 get_files_struct(struct task_struct *task)
47 struct files_struct *files;
52 atomic_inc(&files->count);
59 * put_files_struct is extracted from fs/file.c
62 put_files_struct(struct files_struct *files)
64 if (atomic_dec_and_test(&files->count))
70 eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
72 void __user *argp = (void __user *) arg;
73 struct task_struct *task_target = NULL;
75 struct files_struct *files;
77 struct eventfd_copy eventfd_copy;
81 if (copy_from_user(&eventfd_copy, argp,
82 sizeof(struct eventfd_copy)))
86 * Find the task struct for the target pid
89 pid_task(find_vpid(eventfd_copy.target_pid), PIDTYPE_PID);
90 if (task_target == NULL) {
91 pr_debug("Failed to get mem ctx for target pid\n");
95 files = get_files_struct(current);
97 pr_debug("Failed to get files struct\n");
102 file = fcheck_files(files, eventfd_copy.source_fd);
104 if (file->f_mode & FMODE_PATH ||
105 !atomic_long_inc_not_zero(&file->f_count))
109 put_files_struct(files);
112 pr_debug("Failed to get file from source pid\n");
117 * Release the existing eventfd in the source process
119 spin_lock(&files->file_lock);
121 filp_close(file, files);
122 fdt = files_fdtable(files);
123 fdt->fd[eventfd_copy.source_fd] = NULL;
124 spin_unlock(&files->file_lock);
127 * Find the file struct associated with the target fd.
130 files = get_files_struct(task_target);
132 pr_debug("Failed to get files struct\n");
137 file = fcheck_files(files, eventfd_copy.target_fd);
139 if (file->f_mode & FMODE_PATH ||
140 !atomic_long_inc_not_zero(&file->f_count))
144 put_files_struct(files);
147 pr_debug("Failed to get file from target pid\n");
152 * Install the file struct from the target process into the
153 * file desciptor of the source process,
156 fd_install(eventfd_copy.source_fd, file);
165 static const struct file_operations eventfd_link_fops = {
166 .owner = THIS_MODULE,
167 .unlocked_ioctl = eventfd_link_ioctl,
171 static struct miscdevice eventfd_link_misc = {
172 .name = "eventfd-link",
173 .fops = &eventfd_link_fops,
177 eventfd_link_init(void)
179 return misc_register(&eventfd_link_misc);
182 module_init(eventfd_link_init);
185 eventfd_link_exit(void)
187 misc_deregister(&eventfd_link_misc);
190 module_exit(eventfd_link_exit);
192 MODULE_VERSION("0.0.1");
193 MODULE_LICENSE("GPL v2");
194 MODULE_AUTHOR("Anthony Fee");
195 MODULE_DESCRIPTION("Link eventfd");
196 MODULE_ALIAS("devname:eventfd-link");