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