forked from gregkh/ndas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsal_thread.c
More file actions
executable file
·180 lines (161 loc) · 5.24 KB
/
sal_thread.c
File metadata and controls
executable file
·180 lines (161 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
-------------------------------------------------------------------------
Copyright (c) 2012 IOCELL Networks, Plainsboro, NJ, USA.
All rights reserved.
LICENSE TERMS
The free distribution and use of this software in both source and binary
form is allowed (with or without changes) provided that:
1. distributions of this source code include the above copyright
notice, this list of conditions and the following disclaimer;
2. distributions in binary form include the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other associated materials;
3. the copyright holder's name is not used to endorse products
built using this software without specific written permission.
ALTERNATIVELY, provided that this notice is retained in full, this product
may be distributed under the terms of the GNU General Public License (GPL v2),
in which case the provisions of the GPL apply INSTEAD OF those given above.
DISCLAIMER
This software is provided 'as is' with no explcit or implied warranties
in respect of any properties, including, but not limited to, correctness
and fitness for purpose.
-------------------------------------------------------------------------
*/
#include <linux/version.h>
#include <linux/module.h> // EXPORT_SYMBOL
#include <linux/sched.h> // kernel_thread
#include <linux/kthread.h> // kthread
#include <linux/errno.h> // /usr/include/errno.h , ENOMEM
#include "linux_ver.h"
#include "inc/sal/types.h"
#include "inc/sal/thread.h"
#include "inc/sal/time.h"
#include "inc/sal/debug.h"
#include "inc/ndasuser/ndaserr.h"
NDAS_SAL_API
int
sal_thread_create(
sal_thread_id *tid,
char *name,
int prio,
int stacksize,
sal_thread_func f,
void *arg)
{
// unsigned long flags = 0L; // ignored in 2.4 - 2.6
// pid_t ret = 0;
// if ((ret = kernel_thread((int (*)(void*))f, (void *)arg, flags)) < 0) {
/*
err=sal_thread_create(
sal_thread_id *tid = &udev->thread.tid,
char *name, = "ndiod_thread",
int prio, = -1,
int stacksize, = 0,
sal_thread_func f, = (void*(*)(void*)) ndiod_thread,
void *arg = udev);
*/
struct task_struct *tsk;
printk("ndas: sal_thread_create: sal_thread\n");
tsk = kthread_create((int (*)(void*))f, (void *)arg , "sal_thread");
if (IS_ERR(tsk)) {
printk("ndas: sal_thread_create\n");
printk("ndas: failed to create kthread: reason unkown.\n");
return NDAS_ERROR_INTERNAL;
}
wake_up_process(tsk);
if (tid){
printk("ndas: sal_thread_create: set tid; tsk=%ld\n", (long)tsk);
*tid = (sal_thread_id)(long)tsk;
}
printk("ndas: sal_thread_created: tid=%d\n", tid);
return NDAS_OK;
}
EXPORT_SYMBOL(sal_thread_create);
NDAS_SAL_API
int
sal_thread_yield(void)
{
schedule();
return 0;
}
EXPORT_SYMBOL(sal_thread_yield);
NDAS_SAL_API
sal_thread_id
sal_thread_self(void)
{
return (sal_thread_id) (long)(current->pid);
}
EXPORT_SYMBOL(sal_thread_self);
NDAS_SAL_API
void
sal_thread_exit(int ret)
{
// do_nothing
}
EXPORT_SYMBOL(sal_thread_exit);
NDAS_SAL_API
void sal_thread_block_signals()
{
unsigned long flags;
// 2.4.20-8(RH9.0)
#if (!defined(NDAS_SUSE) && LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20)) || \
(defined(NDAS_SUSE) && (LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,21)) ||\
(defined(NDAS_MVISTA) && LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,20))) || \
defined(NDAS_SIGPENDING_OLD) ||\
(defined(NDAS_MSHARE) && LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,26)) ||\
defined(NDAS_EM86XX)
spin_lock_irqsave(¤t->sigmask_lock, flags);
// flush_signals(current);
// siginitsetinv(¤t->blocked, sigmask(SIGKILL)|sigmask(SIGTERM));
sigfillset(¤t->blocked);
recalc_sigpending(current);
spin_unlock_irqrestore(¤t->sigmask_lock, flags);
#else
spin_lock_irqsave(¤t->sighand->siglock, flags);
//flush_signals(current);
//siginitsetinv(¤t->blocked, sigmask(SIGKILL)|sigmask(SIGTERM));
sigfillset(¤t->blocked);
recalc_sigpending();
spin_unlock_irqrestore(¤t->sighand->siglock, flags);
#endif
}
EXPORT_SYMBOL(sal_thread_block_signals);
NDAS_SAL_API
void sal_thread_flush_signals()
{
if(signal_pending(current)) {
flush_signals(current);
}
}
EXPORT_SYMBOL(sal_thread_flush_signals);
/* 16 for 2.4 and 2.6 */
#define SIZE_COMM sizeof(((struct task_struct*)0)->comm)
NDAS_SAL_API
void sal_thread_daemonize(const char* name, ...)
{
va_list args;
#if LINUX_VERSION_25_ABOVE
char buffer[SIZE_COMM];
va_start(args, name);
vsnprintf(buffer,SIZE_COMM,name,args);
va_end(args);
// daemonize(buffer);
#else
va_start(args, name);
vsnprintf(current->comm,sizeof(current->comm),name, args);
va_end(args);
daemonize();
#if LINUX_VERSION_HAS_REPARENT_TO_INIT
reparent_to_init(); // kernel over 2.5 don't need this because daemonize call this function.
#endif
#endif
}
EXPORT_SYMBOL(sal_thread_daemonize);
NDAS_SAL_API
void sal_thread_nice(long nice)
{
#if LINUX_VERSION_25_ABOVE
set_user_nice(current, nice);
#endif
}
EXPORT_SYMBOL(sal_thread_nice);