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);
120 filp_close(file, files);
121 fdt = files_fdtable(files);
122 fdt->fd[eventfd_copy.source_fd] = NULL;
123 spin_unlock(&files->file_lock);
126 * Find the file struct associated with the target fd.
129 files = get_files_struct(task_target);
131 pr_debug("Failed to get files struct\n");
136 file = fcheck_files(files, eventfd_copy.target_fd);
138 if (file->f_mode & FMODE_PATH ||
139 !atomic_long_inc_not_zero(&file->f_count))
143 put_files_struct(files);
146 pr_debug("Failed to get file from target pid\n");
151 * Install the file struct from the target process into the
152 * file desciptor of the source process,
155 fd_install(eventfd_copy.source_fd, file);
164 static const struct file_operations eventfd_link_fops = {
165 .owner = THIS_MODULE,
166 .unlocked_ioctl = eventfd_link_ioctl,
170 static struct miscdevice eventfd_link_misc = {
171 .name = "eventfd-link",
172 .fops = &eventfd_link_fops,
176 eventfd_link_init(void)
178 return misc_register(&eventfd_link_misc);
181 module_init(eventfd_link_init);
184 eventfd_link_exit(void)
186 misc_deregister(&eventfd_link_misc);
189 module_exit(eventfd_link_exit);
191 MODULE_VERSION("0.0.1");
192 MODULE_LICENSE("GPL v2");
193 MODULE_AUTHOR("Anthony Fee");
194 MODULE_DESCRIPTION("Link eventfd");
195 MODULE_ALIAS("devname:eventfd-link");