-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisastrOS_mqclose.c
64 lines (52 loc) · 1.91 KB
/
disastrOS_mqclose.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
62
63
64
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include "disastrOS.h"
#include "disastrOS_mq.h"
#include "disastrOS_mqdescriptor.h"
#include "disastrOS_syscalls.h"
#define PID disastrOS_getpid()
/*
mqClose closes the message queue referred to file descriptor fd. This means that
the process cannot access in any way the message queue unless a new open occurs.
Notice that unless the last process does not call an unlink, even if everyone
has closed the message queue, this remains alive in the system. Upon success,
mqdescriptor and mqdescriptor pointer are freed, last_mq_fd is decreased and 0
is returned. Upon error, a negative integer is returned.
*/
void internal_mqClose() {
int fd = running->syscall_args[0];
// maybe not necessary
if (fd < 0 || fd > MAX_NUM_MQDESCRIPTORS_PER_PROCESS) {
disastrOS_debug("[%d:MQCLOSE] fd out of bounds\n", PID);
running->syscall_retvalue = DSOS_EMQCLOSE;
return;
}
// fetching mqDescriptor associated to fd
MQDescriptor* mqDes = MQDescriptorList_byFd(&running->mq_descriptors, fd);
if (!mqDes) {
disastrOS_debug("[%d:MQCLOSE] no mqDes found\n", PID);
running->syscall_retvalue = DSOS_EMQCLOSE;
return;
}
// retrieving mq from mqDescriptor
MessageQueue* mq = mqDes->mq;
if (!mq) {
disastrOS_debug("[%d:MQCLOSE] no mq associated with mqDes\n", PID);
running->syscall_retvalue = DSOS_EMQCLOSE;
return;
}
List_detach(&running->mq_descriptors, (ListItem*)mqDes);
MQDescriptorPtr* mqDesPtr = mqDes->ptr;
if (!mqDesPtr) {
disastrOS_debug("[%d:MQCLOSE] no mqDesPtr associated with mqDes\n", PID);
running->syscall_retvalue = DSOS_EMQCLOSE;
return;
}
List_detach(&mq->descriptors, (ListItem*)mqDesPtr);
MQDescriptor_free(mqDes);
MQDescriptorPtr_free(mqDesPtr);
running->last_mq_fd--;
disastrOS_debug("[%d:MQCLOSE] descriptors destroyed, file descriptor descreased\n", PID);
running->syscall_retvalue = 0;
}