1 #include <linux/config.h>
2 #include <linux/version.h>
4 #if defined(MODVERSIONS)
5 #include <linux/modversions.h>
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
10 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
11 #include <linux/tqueue.h>
13 #include <linux/devfs_fs_kernel.h>
16 #include <linux/wait.h>
17 #include <linux/signal.h>
18 #include <linux/interrupt.h>
21 #include <asm/semaphore.h>
22 #include <linux/smp_lock.h>
24 #include "../include/kthread.h"
26 /* private functions */
27 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
28 static void kthread_launcher(void *data)
30 static void kthread_launcher(unsigned long data)
33 kthread_t *kthread = (kthread_t *)data;
35 kernel_thread((int (*)(void *))kthread->function, (void *)kthread, 0);
38 /* public functions */
40 /* create a new kernel thread. Called by the creator. */
41 void start_kthread(void (*func)(kthread_t *), kthread_t *kthread)
43 /* initialize the semaphore:
44 we start with the semaphore locked. The new kernel
45 thread will setup its stuff and unlock it. This
46 control flow (the one that creates the thread) blocks
47 in the down operation below until the thread has reached
50 init_MUTEX_LOCKED(&kthread->startstop_sem);
52 /* store the function to be executed in the data passed to
54 kthread->function=func;
56 /* create the new thread my running a task through keventd */
58 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
60 /* initialize the task queue structure */
62 INIT_LIST_HEAD(&kthread->tq.list);
63 kthread->tq.routine = kthread_launcher;
64 kthread->tq.data = kthread;
66 /* and schedule it for execution */
67 schedule_task(&kthread->tq);
69 /* initialize tasklet */
70 tasklet_init(&kthread->tq, kthread_launcher, (unsigned long)kthread);
71 /* and schedule it for execution */
72 tasklet_schedule(&kthread->tq);
75 /* wait till it has reached the setup_thread routine */
76 down(&kthread->startstop_sem);
79 /* stop a kernel thread. Called by the removing instance */
80 void stop_kthread(kthread_t *kthread)
82 if (kthread->thread == NULL)
84 printk("stop_kthread: killing non existing thread!\n");
88 /* this function needs to be protected with the big
89 kernel lock (lock_kernel()). The lock must be
90 grabbed before changing the terminate
91 flag and released after the down() call. */
94 /* initialize the semaphore. We lock it here, the
95 leave_thread call of the thread to be terminated
96 will unlock it. As soon as we see the semaphore
97 unlocked, we know that the thread has exited.
99 init_MUTEX_LOCKED(&kthread->startstop_sem);
101 /* We need to do a memory barrier here to be sure that
102 the flags are visible on all CPUs.
106 /* set flag to request thread termination */
107 kthread->terminate = 1;
109 /* We need to do a memory barrier here to be sure that
110 the flags are visible on all CPUs.
113 kill_proc(kthread->thread->pid, SIGKILL, 1);
115 /* block till thread terminated */
116 down(&kthread->startstop_sem);
118 /* release the big kernel lock */
121 /* now we are sure the thread is in zombie state. We
122 notify keventd to clean the process up.
124 kill_proc(2, SIGCHLD, 1);
128 /* initialize new created thread. Called by the new thread. */
129 void init_kthread(kthread_t *kthread, char *name)
131 /* fill in thread structure */
132 kthread->thread = current;
134 /* initialise termination flag */
135 kthread->terminate = 0;
137 /* initialise wait queue */
138 init_waitqueue_head(&kthread->queue);
140 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
141 /* lock the kernel. A new kernel thread starts without
142 the big kernel lock, regardless of the lock state
143 of the creator (the lock level is *not* inheritated)
148 /* set signal mask to what we want to respond */
149 siginitsetinv(¤t->blocked, sigmask(SIGKILL)|sigmask(SIGINT)|sigmask(SIGTERM));
151 /* set name of this process (max 15 chars + 0 !) */
152 sprintf(current->comm, name);
161 /* tell the creator that we are ready and let him continue */
162 up(&kthread->startstop_sem);
165 /* cleanup of thread. Called by the exiting thread. */
166 void exit_kthread(kthread_t *kthread)
168 /* we are terminating */
170 /* lock the kernel, the exit will unlock it */
172 kthread->thread = NULL;
175 /* notify the stop_kthread() routine that we are terminating. */
176 up(&kthread->startstop_sem);
177 /* the kernel_thread that called clone() does a do_exit here. */
179 /* there is no race here between execution of the "killer" and real termination
180 of the thread (race window between up and do_exit), since both the
181 thread and the "killer" function are running with the kernel lock held.
182 The kernel lock will be freed after the thread exited, so the code
183 is really not executed anymore as soon as the unload functions gets
184 the kernel lock back.
185 The init process may not have made the cleanup of the process here,
186 but the cleanup can be done safely with the module unloaded.