linux-rx-kernel-listener-kill-task-directly-20080205
[openafs.git] / src / afs / afs_osi.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID
14     ("$Header$");
15
16 #include "afs/sysincludes.h"    /* Standard vendor system headers */
17 #include "afsincludes.h"        /* Afs-based standard headers */
18 #include "afs/afs_stats.h"      /* afs statistics */
19 #ifdef AFS_AIX_ENV
20 #include <sys/adspace.h>        /* for vm_att(), vm_det() */
21 #endif
22
23 /* osi_Init -- do once per kernel installation initialization.
24  *     -- On Solaris this is called from modload initialization.
25  *     -- On AIX called from afs_config.
26  *     -- On HP called from afsc_link.
27  *     -- On SGI called from afs_init. */
28
29 afs_lock_t afs_ftf;             /* flush text lock */
30
31 #ifdef AFS_SGI53_ENV
32 lock_t afs_event_lock;
33 #endif
34
35 #ifdef AFS_SGI64_ENV
36 flid_t osi_flid;
37 #endif
38
39 struct AFS_UCRED *afs_osi_credp;
40
41 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
42 kmutex_t afs_global_lock;
43 kmutex_t afs_rxglobal_lock;
44 #endif
45
46 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
47 long afs_global_owner;
48 #endif
49
50 #if defined(AFS_OSF_ENV)
51 simple_lock_data_t afs_global_lock;
52 #endif
53
54 #if defined(AFS_DARWIN_ENV) 
55 #ifdef AFS_DARWIN80_ENV
56 lck_mtx_t  *afs_global_lock;
57 #else
58 struct lock__bsd__ afs_global_lock;
59 #endif
60 #endif
61
62 #if defined(AFS_XBSD_ENV) && !defined(AFS_FBSD50_ENV)
63 struct lock afs_global_lock;
64 struct proc *afs_global_owner;
65 #endif
66 #ifdef AFS_FBSD50_ENV
67 struct mtx afs_global_mtx;
68 #endif
69
70 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV)
71 thread_t afs_global_owner;
72 #endif /* AFS_OSF_ENV */
73
74 #if defined(AFS_AIX41_ENV)
75 simple_lock_data afs_global_lock;
76 #endif
77
78 void
79 osi_Init(void)
80 {
81     static int once = 0;
82     if (once++ > 0)             /* just in case */
83         return;
84 #if     defined(AFS_HPUX_ENV)
85     osi_InitGlock();
86 #else /* AFS_HPUX_ENV */
87 #if defined(AFS_GLOBAL_SUNLOCK)
88 #if defined(AFS_SGI62_ENV)
89     mutex_init(&afs_global_lock, MUTEX_DEFAULT, "afs_global_lock");
90 #elif defined(AFS_OSF_ENV)
91     usimple_lock_init(&afs_global_lock);
92     afs_global_owner = (thread_t) 0;
93 #elif defined(AFS_FBSD50_ENV)
94     mtx_init(&afs_global_mtx, "AFS global lock", NULL, MTX_DEF);
95 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
96 #if !defined(AFS_DARWIN80_ENV)
97     lockinit(&afs_global_lock, PLOCK, "afs global lock", 0, 0);
98 #endif
99     afs_global_owner = 0;
100 #elif defined(AFS_AIX41_ENV)
101     lock_alloc((void *)&afs_global_lock, LOCK_ALLOC_PIN, 1, 1);
102     simple_lock_init((void *)&afs_global_lock);
103 #elif !defined(AFS_LINUX22_ENV)
104     /* Linux initialization in osi directory. Should move the others. */
105     mutex_init(&afs_global_lock, "afs_global_lock", MUTEX_DEFAULT, NULL);
106 #endif
107 #endif /* AFS_GLOBAL_SUNLOCK */
108 #endif /* AFS_HPUX_ENV */
109
110     if (!afs_osicred_initialized) {
111 #if defined(AFS_DARWIN80_ENV)
112         afs_osi_ctxtp_initialized = 0;
113         afs_osi_ctxtp = NULL; /* initialized in afs_Daemon since it has
114                                   a proc reference that cannot be changed */
115 #endif
116 #if defined(AFS_XBSD_ENV)
117         /* Can't just invent one, must use crget() because of mutex */
118         afs_osi_credp = crdup(osi_curcred());
119 #else
120         memset(&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
121 #if defined(AFS_LINUX26_ENV)
122         afs_osi_cred.cr_group_info = groups_alloc(0);
123 #endif
124 #if defined(AFS_DARWIN80_ENV)
125         afs_osi_cred.cr_ref = 1; /* kauth_cred_get_ref needs 1 existing ref */
126 #else
127         crhold(&afs_osi_cred);  /* don't let it evaporate */
128 #endif
129
130         afs_osi_credp = &afs_osi_cred;
131 #endif
132         afs_osicred_initialized = 1;
133     }
134 #ifdef AFS_SGI64_ENV
135     osi_flid.fl_pid = osi_flid.fl_sysid = 0;
136 #endif
137
138     init_et_to_sys_error();
139 }
140
141 /* mask signals in afsds */
142 void
143 afs_osi_MaskSignals(void)
144 {
145 #ifdef AFS_LINUX22_ENV
146     osi_linux_mask();
147 #endif
148 }
149
150 /* unmask signals in rxk listener */
151 void
152 afs_osi_UnmaskRxkSignals(void)
153 {
154 }
155
156 /* Two hacks to try and fix afsdb */
157 void 
158 afs_osi_MaskUserLoop()
159 {
160 #ifdef AFS_DARWIN_ENV
161     afs_osi_Invisible();
162     afs_osi_fullSigMask();
163 #else
164     afs_osi_MaskSignals();
165 #endif
166 }
167
168 void 
169 afs_osi_UnmaskUserLoop()
170 {
171 #ifdef AFS_DARWIN_ENV
172     afs_osi_fullSigRestore();
173 #endif
174 }
175
176 /* register rxk listener proc info */
177 void
178 afs_osi_RxkRegister(void)
179 {
180 }
181
182 /* procedure for making our processes as invisible as we can */
183 void
184 afs_osi_Invisible(void)
185 {
186 #ifdef AFS_LINUX22_ENV
187     afs_osi_MaskSignals();
188 #elif defined(AFS_SUN5_ENV)
189     curproc->p_flag |= SSYS;
190 #elif defined(AFS_HPUX101_ENV) && !defined(AFS_HPUX1123_ENV)
191     set_system_proc(u.u_procp);
192 #elif defined(AFS_DARWIN80_ENV)
193 #elif defined(AFS_DARWIN_ENV)
194     /* maybe call init_process instead? */
195     current_proc()->p_flag |= P_SYSTEM;
196 #elif defined(AFS_XBSD_ENV)
197     curproc->p_flag |= P_SYSTEM;
198 #elif defined(AFS_SGI_ENV)
199     vrelvm();
200 #endif
201
202     AFS_STATCNT(osi_Invisible);
203 }
204
205 void
206 afs_osi_Visible(void)
207 {
208 #if defined(AFS_SUN5_ENV)
209     curproc->p_flag &= ~SSYS;
210 #elif defined(AFS_DARWIN80_ENV)
211 #elif defined(AFS_DARWIN_ENV)
212     /* maybe call init_process instead? */
213     current_proc()->p_flag &= ~P_SYSTEM;
214 #elif defined(AFS_XBSD_ENV)
215     curproc->p_flag &= ~P_SYSTEM;
216 #endif
217 }
218
219 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
220 /* set the real time */
221 void
222 afs_osi_SetTime(osi_timeval_t * atv)
223 {
224 #if defined(AFS_AIX32_ENV)
225     struct timestruc_t t;
226
227     t.tv_sec = atv->tv_sec;
228     t.tv_nsec = atv->tv_usec * 1000;
229     ksettimer(&t);              /*  Was -> settimer(TIMEOFDAY, &t); */
230 #elif defined(AFS_SUN55_ENV)
231     stime(atv->tv_sec);
232 #elif defined(AFS_SUN5_ENV)
233     /*
234      * To get more than second resolution we can use adjtime. The problem
235      * is that the usecs from the server are wrong (by now) so it isn't
236      * worth complicating the following code.
237      */
238     struct stimea {
239         time_t time;
240     } sta;
241
242     sta.time = atv->tv_sec;
243
244     stime(&sta, NULL);
245 #elif defined(AFS_SGI_ENV)
246     struct stimea {
247         sysarg_t time;
248     } sta;
249
250     AFS_GUNLOCK();
251     sta.time = atv->tv_sec;
252     stime(&sta);
253     AFS_GLOCK();
254 #elif defined(AFS_DARWIN_ENV)
255 #ifndef AFS_DARWIN80_ENV
256     AFS_GUNLOCK();
257     setthetime(atv);
258     AFS_GLOCK();
259 #endif
260 #else
261     /* stolen from kern_time.c */
262 #ifndef AFS_AUX_ENV
263     boottime.tv_sec += atv->tv_sec - time.tv_sec;
264 #endif
265 #ifdef AFS_HPUX_ENV
266     {
267 #if !defined(AFS_HPUX1122_ENV)
268         /* drop the setting of the clock for now. spl7 is not
269          * known on hpux11.22
270          */
271         register ulong_t s;
272         struct timeval t;
273         t.tv_sec = atv->tv_sec;
274         t.tv_usec = atv->tv_usec;
275         s = spl7();
276         time = t;
277         (void)splx(s);
278         resettodr(atv);
279 #endif
280     }
281 #else
282     {
283         register int s;
284         s = splclock();
285         time = *atv;
286         (void)splx(s);
287     }
288     resettodr();
289 #endif
290 #ifdef  AFS_AUX_ENV
291     logtchg(atv->tv_sec);
292 #endif
293 #endif /* AFS_DARWIN_ENV */
294     AFS_STATCNT(osi_SetTime);
295 }
296 #endif /* AFS_LINUX20_ENV */
297
298
299 void
300 shutdown_osi(void)
301 {
302     AFS_STATCNT(shutdown_osi);
303 #ifdef AFS_DARWIN80_ENV
304     if (afs_osi_ctxtp_initialized && afs_osi_ctxtp) {
305        vfs_context_rele(afs_osi_ctxtp);
306        afs_osi_ctxtp = NULL;
307        afs_osi_ctxtp_initialized = 0;
308     }
309     shutdown_osisleep();
310 #endif
311     if (afs_cold_shutdown) {
312         LOCK_INIT(&afs_ftf, "afs_ftf");
313     }
314 }
315
316 #ifndef AFS_OBSD_ENV
317 int
318 afs_osi_suser(void *cr)
319 {
320 #if defined(AFS_SUN510_ENV)
321     return (priv_policy(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL) == 0);
322 #elif defined(AFS_SUN5_ENV)
323     return afs_suser(cr);
324 #else
325     return afs_suser(NULL);
326 #endif
327 }
328 #endif