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_copy(unsigned long arg)
83 void __user *argp = (void __user *) arg;
84 struct task_struct *task_target = NULL;
86 struct files_struct *files;
88 struct eventfd_copy eventfd_copy;
91 if (copy_from_user(&eventfd_copy, argp, sizeof(struct eventfd_copy)))
95 * Find the task struct for the target pid
100 get_pid_task(find_vpid(eventfd_copy.target_pid), PIDTYPE_PID);
101 if (task_target == NULL) {
102 pr_info("Unable to find pid %d\n", eventfd_copy.target_pid);
107 files = get_files_struct(current);
109 pr_info("Failed to get current files struct\n");
114 file = fget_from_files(files, eventfd_copy.source_fd);
117 pr_info("Failed to get fd %d from source\n",
118 eventfd_copy.source_fd);
119 put_files_struct(files);
124 * Release the existing eventfd in the source process
126 spin_lock(&files->file_lock);
128 filp_close(file, files);
129 fdt = files_fdtable(files);
130 fdt->fd[eventfd_copy.source_fd] = NULL;
131 spin_unlock(&files->file_lock);
133 put_files_struct(files);
136 * Find the file struct associated with the target fd.
140 files = get_files_struct(task_target);
142 pr_info("Failed to get target files struct\n");
147 file = fget_from_files(files, eventfd_copy.target_fd);
148 put_files_struct(files);
151 pr_info("Failed to get fd %d from target\n",
152 eventfd_copy.target_fd);
157 * Install the file struct from the target process into the
158 * file desciptor of the source process,
161 fd_install(eventfd_copy.source_fd, file);
165 put_task_struct(task_target);
171 eventfd_link_ioctl(struct file *f, unsigned int ioctl, unsigned long arg)
173 long ret = -ENOIOCTLCMD;
177 ret = eventfd_link_ioctl_copy(arg);
184 static const struct file_operations eventfd_link_fops = {
185 .owner = THIS_MODULE,
186 .unlocked_ioctl = eventfd_link_ioctl,
190 static struct miscdevice eventfd_link_misc = {
191 .name = "eventfd-link",
192 .fops = &eventfd_link_fops,
196 eventfd_link_init(void)
198 return misc_register(&eventfd_link_misc);
201 module_init(eventfd_link_init);
204 eventfd_link_exit(void)
206 misc_deregister(&eventfd_link_misc);
209 module_exit(eventfd_link_exit);
211 MODULE_VERSION("0.0.1");
212 MODULE_LICENSE("GPL v2");
213 MODULE_AUTHOR("Anthony Fee");
214 MODULE_DESCRIPTION("Link eventfd");
215 MODULE_ALIAS("devname:eventfd-link");