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