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