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/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/file.h>
28 #include <linux/fdtable.h>
29 #include <linux/syscalls.h>
31 #include "eventfd_link.h"
35 * get_files_struct is copied from fs/file.c
38 get_files_struct(struct task_struct *task)
40 struct files_struct *files;
45 atomic_inc(&files->count);
52 * put_files_struct is extracted from fs/file.c
55 put_files_struct(struct files_struct *files)
57 if (atomic_dec_and_test(&files->count))
62 fget_from_files(struct files_struct *files, unsigned fd)
67 file = fcheck_files(files, fd);
69 if (file->f_mode & FMODE_PATH ||
70 !atomic_long_inc_not_zero(&file->f_count)) {
81 eventfd_link_ioctl_copy2(unsigned long arg)
83 void __user *argp = (void __user *) arg;
84 struct task_struct *task_target = NULL;
86 struct files_struct *files;
87 struct eventfd_copy2 eventfd_copy2;
90 if (copy_from_user(&eventfd_copy2, argp, sizeof(struct eventfd_copy2)))
94 * Find the task struct for the target pid
99 get_pid_task(find_vpid(eventfd_copy2.pid), PIDTYPE_PID);
100 if (task_target == NULL) {
101 pr_info("Unable to find pid %d\n", eventfd_copy2.pid);
106 files = get_files_struct(task_target);
108 pr_info("Failed to get target files struct\n");
113 file = fget_from_files(files, eventfd_copy2.fd);
114 put_files_struct(files);
117 pr_info("Failed to get fd %d from target\n", eventfd_copy2.fd);
122 * Install the file struct from the target process into the
123 * newly allocated file desciptor of the source process.
125 ret = get_unused_fd_flags(eventfd_copy2.flags);
130 fd_install(ret, file);
133 put_task_struct(task_target);
139 eventfd_link_ioctl_copy(unsigned long arg)
141 void __user *argp = (void __user *) arg;
142 struct task_struct *task_target = NULL;
144 struct files_struct *files;
146 struct eventfd_copy eventfd_copy;
149 if (copy_from_user(&eventfd_copy, argp, sizeof(struct eventfd_copy)))
153 * Find the task struct for the target pid
158 get_pid_task(find_vpid(eventfd_copy.target_pid), PIDTYPE_PID);
159 if (task_target == NULL) {
160 pr_info("Unable to find pid %d\n", eventfd_copy.target_pid);
165 files = get_files_struct(current);
167 pr_info("Failed to get current files struct\n");
172 file = fget_from_files(files, eventfd_copy.source_fd);
175 pr_info("Failed to get fd %d from source\n",
176 eventfd_copy.source_fd);
177 put_files_struct(files);
182 * Release the existing eventfd in the source process
184 spin_lock(&files->file_lock);
186 filp_close(file, files);
187 fdt = files_fdtable(files);
188 fdt->fd[eventfd_copy.source_fd] = NULL;
189 spin_unlock(&files->file_lock);
191 put_files_struct(files);
194 * Find the file struct associated with the target fd.
198 files = get_files_struct(task_target);
200 pr_info("Failed to get target files struct\n");
205 file = fget_from_files(files, eventfd_copy.target_fd);
206 put_files_struct(files);
209 pr_info("Failed to get fd %d from target\n",
210 eventfd_copy.target_fd);
215 * Install the file struct from the target process into the
216 * file desciptor of the source process,
219 fd_install(eventfd_copy.source_fd, file);
223 put_task_struct(task_target);
229 eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
231 long ret = -ENOIOCTLCMD;
235 ret = eventfd_link_ioctl_copy(arg);
238 ret = eventfd_link_ioctl_copy2(arg);
245 static const struct file_operations eventfd_link_fops = {
246 .owner = THIS_MODULE,
247 .unlocked_ioctl = eventfd_link_ioctl,
251 static struct miscdevice eventfd_link_misc = {
252 .name = "eventfd-link",
253 .fops = &eventfd_link_fops,
257 eventfd_link_init(void)
259 return misc_register(&eventfd_link_misc);
262 module_init(eventfd_link_init);
265 eventfd_link_exit(void)
267 misc_deregister(&eventfd_link_misc);
270 module_exit(eventfd_link_exit);
272 MODULE_VERSION("0.0.1");
273 MODULE_LICENSE("GPL v2");
274 MODULE_AUTHOR("Anthony Fee");
275 MODULE_DESCRIPTION("Link eventfd");
276 MODULE_ALIAS("devname:eventfd-link");