File: libcenv.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (541 lines) | stat: -rw-r--r-- 12,216 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * Copyright (c) 1996, 1998, 1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * Locking?
 */

/*
 * COM interface for the default implementation of the LIBC environment.
 */
#include <oskit/com.h>
#include <oskit/dev/dev.h>
#include <oskit/com/services.h>
#include <oskit/com/libcenv.h>
#include <oskit/com/mem.h>
#include <oskit/com/stream.h>
#include <oskit/io/ttystream.h>
#include <fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>

#ifdef PTHREADS
#include <oskit/threads/pthread.h>
#define	oskit_libcenv_create	oskit_libcenv_create_pthreads
#else
#include <oskit/com/listener.h>
#include <oskit/dev/clock.h>
#include <oskit/dev/timer.h>
#include <oskit/dev/osenv_sleep.h>
#endif

/* The default console */
extern struct oskit_stream *default_console_stream;

/* A common symbol that is overridden by the kernel library */
void (*oskit_libc_exit)(int);

/* The default signal init function. */
extern void
oskit_sendsig_init(int (*deliver_function)(int, int, void *));

static struct oskit_libcenv_ops libcenv_ops;

#ifdef CPUNITS
oskit_libcenv_t	*initial_clientos_libcenv;
#endif

/*
 * This is the interface object.
 */
struct genv {
	oskit_libcenv_t		libcenvi;	/* COM interface */
	int			count;		/* Reference count */
	oskit_fsnamespace_t	*fsn;
	char			hostname[256];
	oskit_ttystream_t	*console;
	void			(*exit)(int);
	void			(*siginit)(int (*func)(int, int, void *));
#ifndef PTHREADS
	/*
	 * This stuff for sleep/wakeup support in single threaded mode.
	 * Must request these interfaces dynamically to avoid linktime
	 * dependencies.
	 */
	oskit_timer_t		*sleep_timer;
	oskit_osenv_sleep_t	*sleep_iface;
	osenv_sleeprec_t	*sleeprec;
#endif	
};

/*
 * The initial object is statically constructed.
 */
static struct genv	default_libcenv;

static OSKIT_COMDECL 
libcenv_query(oskit_libcenv_t *s, const oskit_iid_t *iid, void **out_ihandle)
{ 
	struct genv	*g = (struct genv *) s;

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_libcenv_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &g->libcenvi;
		g->count++;
                return 0; 
        }
  
        *out_ihandle = 0;
        return OSKIT_E_NOINTERFACE;
};

static OSKIT_COMDECL_U
libcenv_addref(oskit_libcenv_t *s)
{
	struct genv	*g = (struct genv *) s;

	assert(g->count);
	return ++g->count;
}

static OSKIT_COMDECL_U
libcenv_release(oskit_libcenv_t *s)
{
	struct genv	*g = (struct genv *) s;

	assert(g->count);

	if (--g->count == 0) {
		if (g->fsn)
			oskit_fsnamespace_release(g->fsn);
		if (g->console)
			oskit_ttystream_release(g->console);
#ifndef PTHREADS
		if (g->sleep_timer)
			oskit_timer_release(g->sleep_timer);
		if (g->sleep_iface)
			oskit_osenv_sleep_release(g->sleep_iface);
#endif
		sfree(g, sizeof(g));
	}
	return g->count;
}

/*
 * Get the root directory.
 *
 * NOTE: We do not add a reference for it. Let the caller deal with it.
 */
static OSKIT_COMDECL
libcenv_getfsnamespace(oskit_libcenv_t *s, oskit_fsnamespace_t **intf)
{
	struct genv	*g = (struct genv *) s;

	if (g->fsn) {
		*intf = g->fsn;
		return 0;
	}
	return OSKIT_E_NOINTERFACE;
}

/*
 * Set the root directory. Clear out the old one.
 */
static OSKIT_COMDECL
libcenv_setfsnamespace(oskit_libcenv_t *s, oskit_fsnamespace_t *fsn)
{
	struct genv	*g = (struct genv *) s;

	if (g->fsn) {
		oskit_fsnamespace_release(g->fsn);
		g->fsn = 0;
	}
	if (fsn) {
		g->fsn = fsn;
		oskit_fsnamespace_addref(fsn);
	}
	return 0;
}

/*
 * Get the hostname.
 */
static OSKIT_COMDECL
libcenv_gethostname(oskit_libcenv_t *s, char *name, int len)
{
	struct genv	*g = (struct genv *) s;

	strncpy(name, g->hostname, len);
	return 0;
}

/*
 * Set the hostname
 */
static OSKIT_COMDECL
libcenv_sethostname(oskit_libcenv_t *s, const char *name, int len)
{
	struct genv	*g = (struct genv *) s;

	if (len > 256)
		return OSKIT_EINVAL;

	strncpy(g->hostname, name, 256);
	return 0;
}

/*
 * Call the kernel exit function.
 */
static OSKIT_COMDECL_V
libcenv_exit(oskit_libcenv_t *s, oskit_u32_t exitval)
{
	struct genv	*g = (struct genv *) s;

	if (g->exit)
		g->exit(exitval);
	else {
		printf("Exited with return code %d.\n"
		       "Looping forever...\n", exitval);
		while (1)
			;
	}
}

/*
 * Set the exit function.
 */
static OSKIT_COMDECL
libcenv_setexit(oskit_libcenv_t *s, void (*exitfunc)(int))
{
	struct genv	*g = (struct genv *) s;

	g->exit = exitfunc;
	return 0;
}

/*
 * Get the console object. 
 */
static OSKIT_COMDECL
libcenv_getconsole(oskit_libcenv_t *s, oskit_ttystream_t **out_intf)
{
	struct genv	*g = (struct genv *) s;
	
	if (!g->console)
		return OSKIT_E_NOINTERFACE;

	*out_intf = g->console;
	return 0;
}

/*
 * Set the console object. 
 */
static OSKIT_COMDECL
libcenv_setconsole(oskit_libcenv_t *s, oskit_ttystream_t *console)
{
	struct genv	*g = (struct genv *) s;
	
	g->console = console;
	oskit_ttystream_addref(g->console);
	return 0;
}

/*
 * Call the signal init function.
 */
static OSKIT_COMDECL
libcenv_signals_init(oskit_libcenv_t *s, int (*func)(int, int, void *))
{
	struct genv	*g = (struct genv *) s;

	if (g->siginit)
		g->siginit(func);

	return 0;
}

/*
 * Set the signal init function.
 */
static OSKIT_COMDECL
libcenv_setsiginit(oskit_libcenv_t *s,
		   void (*sigfunc)(int (*func)(int, int, void *)))
{
	struct genv	*g = (struct genv *) s;

	g->siginit = sigfunc;
	return 0;
}

/*
 * Sleep and wakeup support for the C/POSIX library. Using the osenv
 * interfaces directly is wrong. This mimics the osenv_sleep stuff,
 * but since the C/POSIX library is not expected to be holding the
 * process lock when it calls sleep, we cannot use the osenv interfaces
 * directly, except when running single threaded, in which case it does
 * not really matter since there is no process lock.
 *
 * I do not like this arrangement much. The interface needs to be made
 * distinct from the osenv_sleep interfaces. 
 */
#ifndef PTHREADS
/*
 * Single threaded kernel. Can use the osenv interfaces since they do
 * not muck with the process lock.
 */
static oskit_error_t
timeout_handler(oskit_iunknown_t *ioobj, void *arg)
{
	struct genv	*g = (struct genv *) arg;
	
	oskit_osenv_sleep_wakeup(g->sleep_iface,
				 g->sleeprec, OSENV_SLEEP_WAKEUP);
	return 0;
}

static OSKIT_COMDECL_V
libcenv_sleep_init(oskit_libcenv_t *s, osenv_sleeprec_t *sleeprec)
{
	struct genv	*g = (struct genv *) s;

	if (!g->sleep_timer) {
		oskit_clock_t		*sys_clock = (oskit_clock_t *) NULL;
		oskit_listener_t	*l;
		oskit_timer_t		*timeout_timer;
		oskit_osenv_t		*osenv;
		
		oskit_lookup_first(&oskit_clock_iid, (void *) &sys_clock);

		if (!sys_clock)
			panic("libcenv_sleep_init: No system clock");

		oskit_lookup_first(&oskit_osenv_iid, (void *) &osenv);
		if (!osenv)
			panic("libcenv_sleep_init: No osenv interface");

		oskit_osenv_lookup(osenv, &oskit_osenv_sleep_iid,
				   (void *) &(g->sleep_iface));
		if (!g->sleep_iface)
			panic("libcenv_sleep_init: No sleep interface");
		oskit_osenv_release(osenv);

		oskit_clock_createtimer(sys_clock, &timeout_timer);
		l = oskit_create_listener(timeout_handler, (void *) g);

		g->sleep_timer = timeout_timer;
		oskit_timer_setlistener(timeout_timer, l);
		oskit_listener_release(l);
		oskit_clock_release(sys_clock);
	}

	g->sleeprec = sleeprec;
	oskit_osenv_sleep_init(g->sleep_iface, sleeprec);
}

static OSKIT_COMDECL_U
libcenv_sleep(oskit_libcenv_t *s,
	      osenv_sleeprec_t *sleeprec, oskit_timespec_t *timeout)
{
	struct genv	*g = (struct genv *) s;
	int		rval;
	
	if (timeout) {
		oskit_itimerspec_t	to;

		to.it_interval.tv_sec  = 0;
		to.it_interval.tv_nsec = 0;
		to.it_value.tv_sec     = timeout->tv_sec;
		to.it_value.tv_nsec    = timeout->tv_nsec;

		oskit_timer_settime(g->sleep_timer, 0, &to);
	}

	rval = oskit_osenv_sleep_sleep(g->sleep_iface, sleeprec);

	if (timeout) {
		oskit_itimerspec_t	zero = { {0}, {0} };
			
		oskit_timer_settime(g->sleep_timer, 0, &zero);
	}

	return ((rval == OSENV_SLEEP_CANCELED) ? ETIMEDOUT : 0);
}

static OSKIT_COMDECL_V
libcenv_wakeup(oskit_libcenv_t *s, osenv_sleeprec_t *sleeprec)
{
	struct genv	*g = (struct genv *) s;

	oskit_osenv_sleep_wakeup(g->sleep_iface, sleeprec, OSENV_SLEEP_WAKEUP);
}
#else
/*
 * Now it gets trickier. Not SMP safe, but this will work for the
 * uniprocessor pthreads code. We could make this SMP safe by using
 * a condition variable, but maybe later.
 */
static OSKIT_COMDECL_V
libcenv_sleep_init(oskit_libcenv_t *s, osenv_sleeprec_t *sleeprec)
{
	assert(sleeprec);

	sleeprec->data[0] = OSENV_SLEEP_WAKEUP;
	sleeprec->data[1] = (void *) -1;	/* Pre-sleep. NOT SMP SAFE! */
}

static OSKIT_COMDECL_U
libcenv_sleep(oskit_libcenv_t *s,
	      osenv_sleeprec_t *sleeprec, oskit_timespec_t *timeout)
{
	volatile osenv_sleeprec_t *vsr = sleeprec;
	int			   enabled = osenv_intr_enabled();
	int			   millis = 0;
	
	if (enabled)
		osenv_intr_disable();

	if (timeout) {
		millis = (timeout->tv_sec * 1000) +
			 (timeout->tv_nsec / 1000000);
	}

	if (vsr->data[1]) {
		vsr->data[1] = pthread_self();
		if (oskit_pthread_sleep(millis) == ETIMEDOUT) {
			vsr->data[0] = (void *) OSENV_SLEEP_CANCELED;
			vsr->data[1] = 0;
		}
	}

	if (enabled)
		osenv_intr_enable();

	return ((vsr->data[0] == (void *)OSENV_SLEEP_CANCELED) ?
		ETIMEDOUT : 0);
}

static OSKIT_COMDECL_V
libcenv_wakeup(oskit_libcenv_t *s, osenv_sleeprec_t *sleeprec)
{
	pthread_t	tid;
	int		enabled = osenv_intr_enabled();
	
	if (enabled)
		osenv_intr_disable();

	/* Return immediately on spurious wakeup */
	if ((tid = (pthread_t) sleeprec->data[1]) == (pthread_t) NULL)
		goto done;

	sleeprec->data[0] = (void *)OSENV_SLEEP_WAKEUP;
	sleeprec->data[1] = (void *)0;
	
	/* Look for pre-sleep wakeup. NOT SMP SAFE! */
	if (tid == (pthread_t) -1)
		goto done;

	/* Okay, really asleep */
	oskit_pthread_wakeup(tid);

   done:	
	if (enabled)
		osenv_intr_enable();
}
#endif

/*
 * Clone an interface object.
 */
static OSKIT_COMDECL
libcenv_clone(oskit_libcenv_t *s, oskit_libcenv_t **out_intf)
{
	struct genv	*g = (struct genv *) s;
	struct genv	*newg;
	
	newg = (struct genv *) smalloc(sizeof(*newg));
	if (newg == NULL)
		return OSKIT_E_OUTOFMEMORY;

	newg->libcenvi.ops = &libcenv_ops;
	newg->count        = 1;

	if (g->fsn) {
		newg->fsn = g->fsn;
		oskit_fsnamespace_addref(newg->fsn);
	}
	if (g->console) {
		newg->console = g->console;
		oskit_ttystream_addref(newg->console);
	}
	strcpy(newg->hostname, g->hostname);
	newg->exit      = g->exit;
	newg->siginit   = g->siginit;

	*out_intf = &newg->libcenvi;
	return 0;
}


static struct oskit_libcenv_ops libcenv_ops = {
	libcenv_query,
	libcenv_addref,
	libcenv_release,
	libcenv_getfsnamespace,
	libcenv_setfsnamespace,
	libcenv_gethostname,
	libcenv_sethostname,
	libcenv_exit,
	libcenv_setexit,
	libcenv_getconsole,
	libcenv_setconsole,
	libcenv_signals_init,
	libcenv_setsiginit,
	libcenv_sleep_init,
	libcenv_sleep,
	libcenv_wakeup,
	libcenv_clone,
};

/*
 * This initializes the default (or initial) libcenv structure. It is
 * static.
 */
oskit_error_t
oskit_libcenv_create(oskit_libcenv_t **out_iface)
{
	struct genv	*g = &default_libcenv;

	if (g->count) {
		*out_iface = &g->libcenvi;
		return 0;
	}
		
	g->libcenvi.ops = &libcenv_ops;
	g->count        = 10000;
	g->console      = (oskit_ttystream_t *) default_console_stream;
	g->exit         = oskit_libc_exit;
	g->siginit      = oskit_sendsig_init;

	initial_clientos_libcenv = *out_iface = &g->libcenvi;

	return 0;
}