-
Notifications
You must be signed in to change notification settings - Fork 0
/
disastrOS_open_resource.c
61 lines (52 loc) · 2.04 KB
/
disastrOS_open_resource.c
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
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include "disastrOS.h"
#include "disastrOS_syscalls.h"
#include "disastrOS_resource.h"
#include "disastrOS_descriptor.h"
void internal_openResource(){
//1 get from the PCB the resource id of the resource to open
int id=running->syscall_args[0];
int type=running->syscall_args[1];
int open_mode=running->syscall_args[2];
Resource* res=ResourceList_byId(&resources_list, id);
//2 if the resource is opened in CREATE mode, create the resource
// and return an error if the resource is already existing
// otherwise fetch the resource from the system list, and if you don't find it
// throw an error
//printf ("CREATING id %d, type: %d, open mode %d\n", id, type, open_mode);
if (open_mode&DSOS_CREATE){
if (res) {
running->syscall_retvalue=DSOS_ERESOURCECREATE;
return;
}
res=Resource_alloc(id, type);
List_insert(&resources_list, resources_list.last, (ListItem*) res);
}
// at this point we should have the resource, if not something was wrong
if (! res || res->type!=type) {
running->syscall_retvalue=DSOS_ERESOURCEOPEN;
return;
}
if (open_mode&DSOS_EXCL && res->descriptors.size){
running->syscall_retvalue=DSOS_ERESOURCENOEXCL;
return;
}
//5 create the descriptor for the resource in this process, and add it to
// the process descriptor list. Assign to the resource a new fd
Descriptor* des=Descriptor_alloc(running->last_fd, res, running);
if (! des){
running->syscall_retvalue=DSOS_ERESOURCENOFD;
return;
}
running->last_fd++; // we increment the fd value for the next call
DescriptorPtr* desptr=DescriptorPtr_alloc(des);
List_insert(&running->descriptors, running->descriptors.last, (ListItem*) des);
//6 add to the resource, in the descriptor ptr list, a pointer to the newly
// created descriptor
des->ptr=desptr;
List_insert(&res->descriptors, res->descriptors.last, (ListItem*) desptr);
// return the FD of the new descriptor to the process
running->syscall_retvalue = des->fd;
}