File: session.c

package info (click to toggle)
mol 0.9.61-6
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,140 kB
  • ctags: 8,491
  • sloc: ansic: 50,560; asm: 2,826; sh: 458; makefile: 373; perl: 165; lex: 135; yacc: 131
file content (490 lines) | stat: -rw-r--r-- 10,124 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
/* 
 *   Creation Date: <2000/08/26 16:44:40 samuel>
 *   Time-stamp: <2001/06/11 21:44:07 samuel>
 *   
 *	<session.c>
 *	
 *	Session management (fast save/restore)
 *   
 *   Copyright (C) 2000, 2001 Samuel Rydh (samuel@ibrium.se)
 *   
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation
 *   
 */

#include "mol_config.h"
#include "session.h"
#include "verbose.h"
#include "res_manager.h"
#include "version.h"
#include "timer.h"
#include "booter.h"
#include "mol_assert.h"
#include "molcpu.h"
#include "debugger.h"

#define DISABLE_SAVE_SESSION		/* TEMPORARY DISABLED */

SET_VERBOSE_NAME("session");

#define SESSION_FILE_RESOURCE	"session_file"

#define MAX_SAVE_PROCS		5
#define MAX_PREPARE_PROCS	5
#define MAX_SAVE_PROCS_DYN	20
#define MAX_NUM_CHUNKS		40

#define HEAD_VERSION		0x1
#define HEAD_MAGIC		0x4d4f4c20 	/* 'MOL ' */
#define INVALID_MAGIC		0x4d4f4c49 	/* 'MOLI' */

typedef struct {
	int		type;
	int		id;
	off_t		offs;
	size_t		size;
} chunk_t;

typedef struct {
	int		magic;
	int		header_version;
	int		mol_version;

	int		dyn_offs;		/* offset to dynamical data */
	int		num_chunks;
	int		reserved[3];

	/* array of chunk_t follows */ 
} head_t;

typedef struct {
	session_save_fp	save_procs[ MAX_SAVE_PROCS ];
	session_save_fp	save_procs_dyn[ MAX_SAVE_PROCS_DYN ];
	session_prepare_fp prep_procs[ MAX_PREPARE_PROCS ];

	int		nsave_procs, nsave_procs_dyn, nprep_procs;

	int		loading_session;
	int		fd;

	chunk_t		*chunks;

	int		dyn_offs;		/* offset to dynamical data */
	off_t		eof;			/* EOF of data in use */

} session_data_t;

static session_data_t sd;

static int initialized=0;

static int cmd_save_session( int argc, char **argv );

/************************************************************************/
/*	F U N C T I O S							*/
/************************************************************************/

void 
session_init( void )
{
	char *s;
	int fd=-1;
	head_t head;
	
	memset( &sd, 0, sizeof(sd) );
	initialized = 1;
#ifdef DISABLE_SAVE_SESSION
	printm("The session save/restore feature is disabled\n");
	return;
#endif
	add_cmd( "save_session", "save_session\nSave session\n", -1, cmd_save_session );

	if( !(s = get_str_res( SESSION_FILE_RESOURCE )))
		return;
	if( (fd = open(s, O_RDWR)) < 0 )
		return;

	if( read( fd, &head, sizeof(head) ) != sizeof(head)) {
		LOG_ERR("error reading session-file header");
		session_failure(NULL);
	}
	if( head.header_version != HEAD_VERSION )
		session_failure("Session file has bad header version!\n");

	if( head.magic == INVALID_MAGIC ){
		close(fd);
		// unlink(s);
		return;
	}
	if( head.magic != HEAD_MAGIC )
		session_failure("Session file header magic is invalid\n");

	if( head.num_chunks > MAX_NUM_CHUNKS )
		session_failure("Session file contains too many chunks (limit exceeded)\n");
	
	sd.chunks = calloc( MAX_NUM_CHUNKS, sizeof(chunk_t) );
	if( read( fd, sd.chunks, sizeof(chunk_t)*head.num_chunks ) != sizeof(chunk_t)*head.num_chunks ) {
		LOG_ERR("Failed reading chunk headers");
		session_failure(NULL);
	}
	sd.dyn_offs = head.dyn_offs;
	sd.fd = fd;
	sd.loading_session = 1;
}


void
session_cleanup( void )
{
	if( !initialized )
		return;
	if( sd.fd > 0 )
		close( sd.fd );
	if( sd.chunks )
		free( sd.chunks );
}


void 
session_save_proc( session_save_fp proc, session_prepare_fp prep, int dynamic_data ) 
{
	assert( initialized );
	
	if( sd.nsave_procs_dyn >= MAX_SAVE_PROCS_DYN ||
	    sd.nsave_procs >= MAX_SAVE_PROCS ||
	    sd.nprep_procs >= MAX_PREPARE_PROCS )
	{
		LOG("Number of save-procs exceeded (%d)\n", dynamic_data );
		exit(1);
	}
	
	if( dynamic_data && proc )
		sd.save_procs_dyn[ sd.nsave_procs_dyn++ ] = proc;
	if( !dynamic_data && proc )
		sd.save_procs[ sd.nsave_procs++ ] = proc;
	if( prep )
		sd.prep_procs[ sd.nprep_procs++ ] = prep;
}

static int
chunk_compress( void )
{
	chunk_t *k1, *k2;
	int i, num=0;
	
	for(k1=k2=sd.chunks, i=0; i<MAX_NUM_CHUNKS; i++, k1++ ){
		if( k1->type ) {
			*k2++ = *k1;
			num++;
		}
	}
	return num;
}

static void
save_session_( ulong dummy_id, void *dummy )
{
	save_session();
}

void
schedule_save_session( int usecs ) 
{
	if( is_oldworld_boot() )
		printm("Session support is only available in the newworld setting\n");
	else
		settimer_usecs( usecs, save_session_, NULL );
}

/* Ret: 0 success, 1 try later, -1 error */
int
save_session_now( void )
{
	int 	i, err=0;
	char 	*name = get_str_res( SESSION_FILE_RESOURCE );
	head_t	head;
	size_t	size;
	chunk_t *k;

#ifdef DISABLE_SAVE_SESSION
	printm("Session support is unavailable (temporary disabled)\n");
	return -1;
#endif
	
	if( is_oldworld_boot() ){
		printm("Session support is not available in the oldworld setting\n");
		return -1;
	}

	if( !name ) {
		printm("Resource %s is missing\n", SESSION_FILE_RESOURCE );
		return -1;
	}

	/* First check that we are in a recoverable state */
	for(i=0; i<sd.nprep_procs; i++ )
		if( (*sd.prep_procs[i])() )
			return 1;
	
	/* Create/open session file if needed */ 
	if( sd.fd <= 0 ){
		if( (sd.fd = open( name, O_RDWR | O_CREAT, 00644 )) < 0 ) {
			LOG_ERR("Could not create/open session file '%s'", name );
			return -1;
		}
		sd.eof = sizeof(head_t) + MAX_NUM_CHUNKS*sizeof(chunk_t);
		sd.dyn_offs = sd.eof;

		sd.chunks = calloc( MAX_NUM_CHUNKS, sizeof(chunk_t) );
	}
	
	/* Clear out dynamic chunks */
	for(i=0, k=sd.chunks; i<MAX_NUM_CHUNKS; i++, k++ )
		if( k->offs + k->size > sd.dyn_offs )
			k->type = 0;
	
	sd.eof = sd.dyn_offs;

	/* First run procs whose data must be static (e.g. file-mapped RAM) */
	for( i=0; !err && i<sd.nsave_procs; i++ )
		err = (*sd.save_procs[i])();

	sd.dyn_offs = sd.eof;

	for( i=0; !err && i<sd.nsave_procs_dyn; i++ )
		err = (*sd.save_procs_dyn[i])();

	memset( &head, 0, sizeof(head) );
	head.magic = HEAD_MAGIC;
	head.header_version = HEAD_VERSION;
	head.mol_version = MOL_VERSION;
	head.dyn_offs = sd.dyn_offs;
	head.num_chunks = chunk_compress();
	lseek( sd.fd, 0, SEEK_SET );
	err = err || write( sd.fd, &head, sizeof(head) ) != sizeof(head);
	size = sizeof(chunk_t)*head.num_chunks;
	err = err || write( sd.fd, sd.chunks, size ) != size;

	if( err ) {
		printm("Failed saving the session\n");
		close(sd.fd);
		sd.fd = -1;
		unlink( name );
		return -1;
	}
	return 0;
}

static chunk_t *
find_chunk( int type, int id )
{
	int 	i;
	chunk_t *k;
	
	for(k=sd.chunks, i=0; i<MAX_NUM_CHUNKS; i++, k++ )
		if( k->type && k->type == type && k->id == id )
			return k;
	return NULL;
}


/************************************************************************/
/*	data read/write							*/
/************************************************************************/

static int
stype_to_type( char *stype )
{
	if( !stype || ((strlen(stype) != 4 && strlen(stype) != 3)) ) {
		printm("Bad stype %s (must be three or four charachter long)\n", stype );
		return 0;
	}
	return *(int*)stype;
}

int
loading_session( void )
{
	assert( initialized );
	return sd.loading_session;
}

void
session_is_loaded( void )
{
	head_t head;

	if( !loading_session() )
		return;

#if 1
	lseek( sd.fd, 0, SEEK_SET );
	read( sd.fd, &head, sizeof(head) );

	head.magic = INVALID_MAGIC;

	lseek( sd.fd, 0, SEEK_SET );
	write( sd.fd, &head, sizeof(head) );
#else
	printm("*** should invalidate session file ***\n");
#endif
}

int
read_session_data( char *stype, int id, char *ptr, size_t size ) 
{
	chunk_t *k;
	int s, type = stype_to_type(stype);

	if( sd.fd <= 0 ){
		LOG("Internal error in read_session_data\n");
		return -1;
	}
	if( !(k = find_chunk(type, id)) )
		return 1;
 	if( size != k->size ) {
		LOG("Size mismatch\n");
		return -1;
	}

	lseek( sd.fd, k->offs, SEEK_SET );
	s=read( sd.fd, ptr, size );

	if( s != size ){
		LOG_ERR("read_session_data");
		return -1;
	}
	return 0;
}

int
get_session_data_fd( char *stype, int id, int *fd, off_t *offs, int *size )
{
	int type = stype_to_type(stype);
	chunk_t *k;
	if( !(k = find_chunk( type, id )))
		return -1;
	*fd = sd.fd;
	if( offs )
		*offs = k->offs;
	if( size )
		*size = k->size;
	return 0;
}

int
get_session_data_size( char *stype, int id )
{
	int type = stype_to_type(stype);
	chunk_t *k;

	if( !(k = find_chunk( type, id )) )
		return -1;
	return k->size;
}

void
align_session_data( int boundary )
{
	if( (sd.eof & (boundary-1)) )
		sd.eof = (sd.eof & ~(boundary-1)) + boundary;
}

int
write_session_data( char *stype, int id, char *ptr, ssize_t size )
{
	int type = stype_to_type(stype);
	chunk_t *k;
	
	if( sd.fd <= 0 ) {
		LOG("Internal error in write_session_data!\n");
		return -1;
	}
	k = find_chunk( type, id );

	if( k && k->size < size ) {
		LOG("*** Static key '%s' increased in size! ***\n", stype );
		k->type = 0;
		k = NULL;
	}

	if( !k ) {
		int i;
		for(i=0, k=sd.chunks; i<MAX_NUM_CHUNKS && k->type; i++, k++ )
			;
		if( i >= MAX_NUM_CHUNKS ) {
			LOG("number of chunks exceeded!\n");
			return -1;
		}
		k->type = type;
		k->id = id;
		k->offs = sd.eof;
		sd.eof += size;
	}
	k->size = size;
	if( !ptr )
		return 0;

	if( lseek( sd.fd, k->offs, SEEK_SET ) < 0 ) {
		LOG_ERR("lseek");
		return -1;
	}
	if( write( sd.fd, ptr, k->size ) < 0 ){
		LOG_ERR("write");
		return -1;
	}
	return 0;
}

ulong
calc_str_checksum( char *str )
{
	if( !str )
		return 0;
	return calc_checksum( str, strlen(str) );
}

ulong
calc_checksum( char *ptr, int size )
{
	int i, ret=0;

	for(i=0; i<size; i++ )
		ret += ptr[i] * (i%19+i);
	return ret;
}

void
session_failure( char *err )
{
	char *name = get_str_res( SESSION_FILE_RESOURCE );

	if( err )
		printm("*** %s", err );
	printm("*** Failed to restore the saved session.\n");
	printm("*** Delete the session file '%s' to do a normal boot.\n", name );

	exit(1);
}


/************************************************************************/
/*	Debugger CMDs							*/
/************************************************************************/

static int
cmd_save_session( int argc, char **argv )
{
	int err;
	if( argc != 1 )
		return 1;

	if( !(err=save_session_now() )) {
		printm("Session saved successfully\n");
	} else if( err==1 ) {
		printm("Unsafe state - session not saved\n");
	}
	return 0;
}