File: sfs_peropen.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 (636 lines) | stat: -rw-r--r-- 13,175 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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
 * Copyright (c) 1999 The University of Utah and the Flux Group. 
 * All rights reserved. 
 * 
 * Contributed by the Computer Security Research division,
 * INFOSEC Research and Technology Office, NSA.
 * 
 * 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.
 */

#include "sfs.h"

static struct oskit_openfile_ops ofile_ops;
static struct oskit_absio_ops ofile_absio_ops;
static struct oskit_comsid_ops sid_ops;

static inline struct sopenfile *
add_openfile(struct sopenfile * list,
	     struct sopenfile * item)
{
	if (list == NULL) {
		item->prev = item->next = item;
	} else {
		item->prev = list->prev;
		item->next = list;
		list->prev->next = item;
		list->prev = item;
	}
	return (item);
}

static inline struct sopenfile *
remove_openfile(struct sopenfile * list,
		struct sopenfile * item)
{
	if (item->next == item->prev) {
		return (NULL);
	} else {
		item->prev->next = item->next;
		item->next->prev = item->prev;
		return (item->next);
	}
}


oskit_error_t 
sopenfile_create(struct sfiledir * sfile,
		 oskit_openfile_t * ofile,
		 oskit_oflags_t flags,
		 oskit_security_id_t sid,
		 struct sopenfile ** out_sofile)
{
	struct sopenfile *sofile;
	oskit_error_t   rc;


	sofile = 
	  oskit_osenv_mem_alloc(sfile->sfs->mem,sizeof(struct sopenfile),0,0);
	if (!sofile)
		return OSKIT_ENOMEM;

	sofile->ofilei.ops = &ofile_ops;
	sofile->absioi.ops = &ofile_absio_ops;
	sofile->sidi.ops = &sid_ops;
	sofile->count = 1;
	sofile->flags = flags;
	sofile->sid = sid;
	sofile->sfile = sfile;
	oskit_file_addref(&sfile->filei);

	sofile->ofile = ofile;
	oskit_openfile_addref(ofile);

	rc = oskit_openfile_query(ofile, &oskit_absio_iid, (void **) &sofile->absio);
	if (rc)
		sofile->absio = NULL;

	sfile->sopenfile_list = add_openfile(sfile->sopenfile_list, sofile);

	OSKIT_AVC_ENTRY_REF_INIT(&sofile->avcr);
	OSKIT_AVC_ENTRY_REF_CPY(&sofile->file_avcr, &sfile->avcr);

	*out_sofile = sofile;

	return 0;
}


/*
 * oskit_openfile methods
 */

static OSKIT_COMDECL 
ofile_query(oskit_openfile_t * f,
	    const struct oskit_guid * iid,
	    void **out_ihandle)
{
	struct sopenfile *sofile;

	sofile = (struct sopenfile *) f;

	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_stream_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_openfile_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &sofile->ofilei;
		++sofile->count;
		return 0;
	}
	if (memcmp(iid, &oskit_comsid_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &sofile->sidi;
		++sofile->count;
		return 0;
	}
	if (sofile->absio && memcmp(iid, &oskit_absio_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &sofile->absioi;
		++sofile->count;
		return 0;
	}
	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}


static OSKIT_COMDECL_U 
ofile_addref(oskit_openfile_t * f)
{
	struct sopenfile *sofile;

	sofile = (struct sopenfile *) f;

	return ++sofile->count;
}


static OSKIT_COMDECL_U 
ofile_release(oskit_openfile_t * f)
{
	struct sopenfile *sofile;
	struct sopenfile *openfile_list;
	unsigned        newcount;

	sofile = (struct sopenfile *) f;

	newcount = --sofile->count;
	if (newcount == 0) {
		openfile_list = sofile->sfile->sopenfile_list;
		sofile->sfile->sopenfile_list = remove_openfile(openfile_list, sofile);

		oskit_file_release(&sofile->sfile->filei);
		oskit_openfile_release(sofile->ofile);
		if (sofile->absio)
			oskit_absio_release(sofile->absio);

		oskit_osenv_mem_free(sofile->sfile->sfs->mem, sofile, 0, 
				     sizeof(struct sopenfile));
	}
	return newcount;
}


static inline oskit_error_t 
ofile_read_checks(struct sopenfile * sofile)
{
	oskit_security_id_t   csid;
	oskit_error_t   rc;

	if (!(sofile->flags & OSKIT_O_RDONLY))
		return OSKIT_EBADF;

	CSID(&csid);
	if (csid != sofile->sid) {
		rc = SFS_FD_HAS_PERM(csid, sofile, SETATTR);
		if (rc)
			return rc;

		rc = SFS_FD_FILE_HAS_PERM(csid, sofile, READ);
		if (rc)
			return rc;
	}
	return 0;
}


static OSKIT_COMDECL 
ofile_read(oskit_openfile_t * f, void *buf, oskit_u32_t len,
	   oskit_u32_t * out_actual)
{
	struct sopenfile *sofile;
	oskit_error_t   rc;


	sofile = (struct sopenfile *) f;
	if (!sofile || !sofile->count)
		return OSKIT_E_INVALIDARG;

	rc = ofile_read_checks(sofile);
	if (rc)
		return rc;

	return oskit_openfile_read(sofile->ofile, buf, len, out_actual);
}


static inline oskit_error_t 
ofile_write_checks(struct sopenfile * sofile)
{
	oskit_security_id_t   csid;
	oskit_error_t   rc;

	if (!(sofile->flags & OSKIT_O_WRONLY))
		return OSKIT_EBADF;

	CSID(&csid);
	if (csid != sofile->sid) {
		rc = SFS_FD_HAS_PERM(csid, sofile, SETATTR);
		if (rc)
			return rc;

		rc = SFS_FD_FILE_HAS_PERM(csid, sofile, WRITE);
		if (rc) {
			if (sofile->flags & OSKIT_O_APPEND) {
				rc = SFS_FD_FILE_HAS_PERM(csid, sofile, APPEND);
			}
		}
		
		if (rc)
			return rc;
	}
	return 0;
}


static OSKIT_COMDECL 
ofile_write(oskit_openfile_t * f, const void *buf,
	    oskit_u32_t len, oskit_u32_t * out_actual)
{
	struct sopenfile *sofile;
	oskit_error_t   rc;

	sofile = (struct sopenfile *) f;
	if (!sofile || !sofile->count)
		return OSKIT_E_INVALIDARG;

	rc = ofile_write_checks(sofile);
	if (rc)
		return rc;

	return oskit_openfile_write(sofile->ofile, buf, len, out_actual);
}


static OSKIT_COMDECL 
ofile_seek(oskit_openfile_t * f, oskit_s64_t ofs,
	   oskit_seek_t whence, oskit_u64_t * out_newpos)
{
	oskit_security_id_t   csid;
	struct sopenfile *sofile;
	oskit_error_t   rc;


	sofile = (struct sopenfile *) f;


	if (!sofile || !sofile->count)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	if (csid != sofile->sid) {
		rc = SFS_FD_HAS_PERM(csid, sofile, SETATTR);
		if (rc)
			return rc;
	}
	return oskit_openfile_seek(sofile->ofile, ofs, whence, out_newpos);
}


static OSKIT_COMDECL 
ofile_setsize(oskit_openfile_t * f, oskit_u64_t new_size)
{
	struct sopenfile *sofile;
	oskit_security_id_t   csid;
	oskit_error_t   rc;

	sofile = (struct sopenfile *) f;
	if (!sofile || !sofile->count)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FD_FILE_HAS_PERM(csid, sofile, SETATTR);
	if (rc)
		return rc;

	return oskit_openfile_setsize(sofile->ofile, new_size);
}


static OSKIT_COMDECL 
ofile_copy_to(oskit_openfile_t * f,
	      oskit_stream_t * dst,
	      oskit_u64_t size,
	      oskit_u64_t * out_read,
	      oskit_u64_t * out_written)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
ofile_commit(oskit_openfile_t * f, oskit_u32_t commit_flags)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
ofile_revert(oskit_openfile_t * f)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
ofile_lock_region(oskit_openfile_t * f,
		  oskit_u64_t offset, oskit_u64_t size,
		  oskit_u32_t lock_type)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
ofile_unlock_region(oskit_openfile_t * f,
		    oskit_u64_t offset, oskit_u64_t size,
		    oskit_u32_t lock_type)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
ofile_stat(oskit_openfile_t * f, oskit_stream_stat_t * out_stat,
	   oskit_u32_t stat_flags)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
ofile_clone(oskit_openfile_t * f, oskit_openfile_t ** out_stream)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
ofile_getfile(oskit_openfile_t * f,
	      struct oskit_file ** out_file)
{
	struct sopenfile *sofile;


	sofile = (struct sopenfile *) f;
	if (!sofile || !sofile->count)
		return OSKIT_E_INVALIDARG;

	oskit_file_addref(&sofile->sfile->filei);
	*out_file = &sofile->sfile->filei;

	return 0;
}


static struct oskit_openfile_ops ofile_ops = {
	ofile_query,
	ofile_addref,
	ofile_release,
	ofile_read,
	ofile_write,
	ofile_seek,
	ofile_setsize,
	ofile_copy_to,
	ofile_commit,
	ofile_revert,
	ofile_lock_region,
	ofile_unlock_region,
	ofile_stat,
	ofile_clone,
	ofile_getfile
};




/*
 * oskit_openfile absolute I/O methods
 */

static OSKIT_COMDECL 
afile_query(oskit_absio_t * io,
	    const struct oskit_guid * iid,
	    void **out_ihandle)
{
	struct sopenfile *ofile;

	if (!io || io->ops != &ofile_absio_ops)
		return OSKIT_E_INVALIDARG;

	ofile = (struct sopenfile *) ((char *) io -
				      offsetof(struct sopenfile, absioi));
	return oskit_openfile_query(&ofile->ofilei, iid, out_ihandle);
}


static OSKIT_COMDECL_U 
afile_addref(oskit_absio_t * io)
{
	struct sopenfile *ofile;


	if (!io || io->ops != &ofile_absio_ops)
		return OSKIT_E_INVALIDARG;

	ofile = (struct sopenfile *) ((char *) io -
				      offsetof(struct sopenfile, absioi));
	return oskit_openfile_addref(&ofile->ofilei);
}


static OSKIT_COMDECL_U 
afile_release(oskit_absio_t * io)
{
	struct sopenfile *ofile;

	if (!io || io->ops != &ofile_absio_ops)
		return OSKIT_E_INVALIDARG;

	ofile = (struct sopenfile *) ((char *) io -
				      offsetof(struct sopenfile, absioi));
	return oskit_openfile_release(&ofile->ofilei);
}


static OSKIT_COMDECL 
afile_read(oskit_absio_t * io, void *buf,
	   oskit_off_t offset, oskit_size_t amount,
	   oskit_size_t * out_actual)
{
	struct sopenfile *sofile;
	oskit_error_t   rc;

	if (!io || io->ops != &ofile_absio_ops)
		return OSKIT_E_INVALIDARG;

	sofile = (struct sopenfile *) ((char *) io -
				       offsetof(struct sopenfile, absioi));
	if (!sofile->count)
		return OSKIT_E_INVALIDARG;

	rc = ofile_read_checks(sofile);
	if (rc)
		return rc;

	return oskit_absio_read(sofile->absio, buf, offset, amount, out_actual);
}


static OSKIT_COMDECL 
afile_write(oskit_absio_t * io, const void *buf,
	    oskit_off_t offset, oskit_size_t amount,
	    oskit_size_t * out_actual)
{
	struct sopenfile *sofile;
	oskit_error_t   rc;

	if (!io || io->ops != &ofile_absio_ops)
		return OSKIT_E_INVALIDARG;

	sofile = (struct sopenfile *) ((char *) io -
				       offsetof(struct sopenfile, absioi));
	if (!sofile->count)
		return OSKIT_E_INVALIDARG;

	rc = ofile_write_checks(sofile);
	if (rc)
		return rc;

	return oskit_absio_write(sofile->absio, buf, offset, amount, out_actual);
}


static OSKIT_COMDECL 
afile_get_size(oskit_absio_t * io, oskit_off_t * out_size)
{
	struct sopenfile *sofile;
	oskit_security_id_t   csid;
	oskit_error_t   rc;

	if (!io || io->ops != &ofile_absio_ops)
		return OSKIT_E_INVALIDARG;

	sofile = (struct sopenfile *) ((char *) io -
				       offsetof(struct sopenfile, absioi));
	if (!sofile->count)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FD_FILE_HAS_PERM(csid, sofile, GETATTR);
	if (rc)
		return rc;

	return oskit_absio_getsize(sofile->absio, out_size);
}


static OSKIT_COMDECL 
afile_set_size(oskit_absio_t * io, oskit_off_t new_size)
{
	struct sopenfile *sofile;
	oskit_security_id_t   csid;
	oskit_error_t   rc;

	if (!io || io->ops != &ofile_absio_ops)
		return OSKIT_E_INVALIDARG;

	sofile = (struct sopenfile *) ((char *) io -
				       offsetof(struct sopenfile, absioi));
	if (!sofile->count)
		return OSKIT_E_INVALIDARG;

	CSID(&csid);
	rc = SFS_FD_FILE_HAS_PERM(csid, sofile, SETATTR);
	if (rc)
		return rc;

	return oskit_absio_setsize(sofile->absio, new_size);
}


static struct oskit_absio_ops ofile_absio_ops = {
	afile_query,
	afile_addref,
	afile_release,
	(void *) 0,
	afile_read,
	afile_write,
	afile_get_size,
	afile_set_size
};




/*
 * oskit_comsid methods
 */

static OSKIT_COMDECL 
sid_query(oskit_comsid_t * s,
	  const struct oskit_guid * iid,
	  void **out_ihandle)
{
	struct sopenfile *sofile;

	sofile = (struct sopenfile *) ((char *) s -
				       offsetof(struct sopenfile, sidi));

	return oskit_openfile_query(&sofile->ofilei, iid, out_ihandle);
}


static OSKIT_COMDECL_U 
sid_addref(oskit_comsid_t * s)
{
	struct sopenfile *sofile;

	sofile = (struct sopenfile *) ((char *) s -
				       offsetof(struct sopenfile, sidi));
	return oskit_openfile_addref(&sofile->ofilei);
}


static OSKIT_COMDECL_U 
sid_release(oskit_comsid_t * s)
{
	struct sopenfile *sofile;

	sofile = (struct sopenfile *) ((char *) s -
				       offsetof(struct sopenfile, sidi));
	return oskit_openfile_release(&sofile->ofilei);
}


static OSKIT_COMDECL 
sid_get(oskit_comsid_t * s,
	oskit_security_class_t * outclass,
	oskit_security_id_t * outsid)
{
	struct sopenfile *sofile;

	sofile = (struct sopenfile *) ((char *) s -
				       offsetof(struct sopenfile, sidi));
	*outclass = OSKIT_SECCLASS_FD;
	*outsid = sofile->sid;
	return 0;
}


static OSKIT_COMDECL 
sid_set(oskit_comsid_t * s,
	oskit_security_id_t newsid)
{
	struct sopenfile *sofile;

	sofile = (struct sopenfile *) ((char *) s -
				       offsetof(struct sopenfile, sidi));
	return OSKIT_E_NOTIMPL;
}


static struct oskit_comsid_ops sid_ops = {
	sid_query,
	sid_addref,
	sid_release,
	sid_get,
	sid_set
};