1 #include <linux/version.h>
2 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
3 #include <linux/config.h>
6 #if defined(MODVERSIONS)
7 #include <linux/modversions.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
13 #include <linux/tqueue.h>
15 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
16 #include <linux/devfs_fs_kernel.h>
20 #include <linux/wait.h>
21 #include <linux/signal.h>
22 #include <linux/interrupt.h>
25 #include <asm/semaphore.h>
26 #include <linux/smp_lock.h>
28 #include "../include/kthread.h"
30 /* private functions */
31 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
32 static void kthread_launcher(void *data)
34 static void kthread_launcher(unsigned long data)
37 kthread_t *kthread = (kthread_t *)data;
39 kernel_thread((int (*)(void *))kthread->function, (void *)kthread, 0);
42 /* public functions */
44 /* create a new kernel thread. Called by the creator. */
45 void start_kthread(void (*func)(kthread_t *), kthread_t *kthread)
47 /* initialize the semaphore:
48 we start with the semaphore locked. The new kernel
49 thread will setup its stuff and unlock it. This
50 control flow (the one that creates the thread) blocks
51 in the down operation below until the thread has reached
54 sema_init(&kthread->startstop_sem, 0);
56 /* store the function to be executed in the data passed to
58 kthread->function=func;
60 /* create the new thread my running a task through keventd */
62 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
64 /* initialize the task queue structure */
66 INIT_LIST_HEAD(&kthread->tq.list);
67 kthread->tq.routine = kthread_launcher;
68 kthread->tq.data = kthread;
70 /* and schedule it for execution */
71 schedule_task(&kthread->tq);
73 /* initialize tasklet */
74 tasklet_init(&kthread->tq, kthread_launcher, (unsigned long)kthread);
75 /* and schedule it for execution */
76 tasklet_schedule(&kthread->tq);
79 /* wait till it has reached the setup_thread routine */
80 down(&kthread->startstop_sem);
83 /* stop a kernel thread. Called by the removing instance */
84 void stop_kthread(kthread_t *kthread)
86 if (kthread->thread == NULL)
88 printk("stop_kthread: killing non existing thread!\n");
92 /* this function needs to be protected with the big
93 kernel lock (lock_kernel()). The lock must be
94 grabbed before changing the terminate
95 flag and released after the down() call. */
98 /* initialize the semaphore. We lock it here, the
99 leave_thread call of the thread to be terminated
100 will unlock it. As soon as we see the semaphore
101 unlocked, we know that the thread has exited.
103 sema_init(&kthread->startstop_sem,0);
105 /* We need to do a memory barrier here to be sure that
106 the flags are visible on all CPUs.
110 /* set flag to request thread termination */
111 kthread->terminate = 1;
113 /* We need to do a memory barrier here to be sure that
114 the flags are visible on all CPUs.
117 kill_proc(kthread->thread->pid, SIGKILL, 1);
119 /* block till thread terminated */
120 down(&kthread->startstop_sem);
122 /* release the big kernel lock */
125 /* now we are sure the thread is in zombie state. We
126 notify keventd to clean the process up.
128 kill_proc(2, SIGCHLD, 1);
132 /* initialize new created thread. Called by the new thread. */
133 void init_kthread(kthread_t *kthread, char *name)
135 /* fill in thread structure */
136 kthread->thread = current;
138 /* initialise termination flag */
139 kthread->terminate = 0;
141 /* initialise wait queue */
142 init_waitqueue_head(&kthread->queue);
144 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40))
145 /* lock the kernel. A new kernel thread starts without
146 the big kernel lock, regardless of the lock state
147 of the creator (the lock level is *not* inheritated)
152 /* set signal mask to what we want to respond */
153 siginitsetinv(¤t->blocked, sigmask(SIGKILL)|sigmask(SIGINT)|sigmask(SIGTERM));
155 /* set name of this process (max 15 chars + 0 !) */
156 sprintf(current->comm, name);
165 /* tell the creator that we are ready and let him continue */
166 up(&kthread->startstop_sem);
169 /* cleanup of thread. Called by the exiting thread. */
170 void exit_kthread(kthread_t *kthread)
172 /* we are terminating */
174 /* lock the kernel, the exit will unlock it */
176 kthread->thread = NULL;
179 /* notify the stop_kthread() routine that we are terminating. */
180 up(&kthread->startstop_sem);
181 /* the kernel_thread that called clone() does a do_exit here. */
183 /* there is no race here between execution of the "killer" and real termination
184 of the thread (race window between up and do_exit), since both the
185 thread and the "killer" function are running with the kernel lock held.
186 The kernel lock will be freed after the thread exited, so the code
187 is really not executed anymore as soon as the unload functions gets
188 the kernel lock back.
189 The init process may not have made the cleanup of the process here,
190 but the cleanup can be done safely with the module unloaded.