Thread Synch AOSV 2020  1.2
LKM for exchanging messages between threads
thread_synch.h
Go to the documentation of this file.
1 
12 #ifndef THREAD_SYNCH_H
13 #define THREAD_SYNCH_H
14 
15 
16 #ifndef _GNU_SOURCE
17 #define _GNU_SOURCE 1
18 #endif
19 
20 
21 
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/stat.h>
25 //#include <sys/poll.h>
26 
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <stdbool.h>
33 
34 #define ATTR_BUFF_SIZE 64
35 #define DEVICE_NAME_SIZE 64
37 #define GROUP_CLASS_NAME "group_synch"
38 #define CLASS_NAME "thread_synch"
40 #define D_DEV_NAME "main_thread_sync"
41 #define D_DEV_MAJOR (0)
42 #define D_DEV_MINOR (0)
45 #define GRP_MIN_ID 0
46 #define GRP_MAX_ID 255
50 #define NO_MSG_PRESENT 1
51 
52 #define ALLOC_ERR -2
53 #define USER_COPY_ERR -3
54 #define SEM_INIT_ERR -4
55 #define MSG_INVALID_FORMAT -11
56 #define MSG_SIZE_ERROR -12
57 #define MEMORY_ERROR -13
58 #define STORAGE_SIZE_ERR -14
59 
60 
61 
62 
63 #define TS_NOT_FOUND -50
64 #define TS_OPEN_ERR -51
65 #define UNAUTHORIZED -52
66 #define PERMISSION_ERR -53
67 #define GROUP_CLOSED -54
68 
69 
70 
71 #define BUFF_SIZE 512
72 #define POLL_TIMEOUT 5
73 #define PARAM_NUMBER 3
74 
78 #define IOCTL_GET_GROUP_DESC _IOR('Q', 1, group_t*)
79 
80 
81 #define IOCTL_SET_SEND_DELAY _IOW('Y', 0, long)
82 #define IOCTL_REVOKE_DELAYED_MESSAGES _IO('Y', 1)
83 #define IOCTL_CANCEL_DELAY _IO('Y', 2)
84 
85 
86 #define IOCTL_SLEEP_ON_BARRIER _IO('Z', 0)
87 #define IOCTL_AWAKE_BARRIER _IO('Z', 1)
88 
89 #define IOCTL_INSTALL_GROUP _IOW('X', 99, group_t*)
90 #define IOCTL_GET_GROUP_ID _IOW('X', 100, group_t*)
91 
92 #define IOCTL_CHANGE_OWNER _IOW('Q', 102, uid_t)
93 #define IOCTL_SET_STRICT_MODE _IOW('Q', 101, bool)
94 
95 
99 typedef struct t_message{
100  pid_t author;
101  void *buffer;
102  size_t size;
103 } msg_t;
104 
108 typedef struct group_t {
109  char *group_name;
110  ssize_t name_len;
111 } group_t;
112 
113 
114 
118 typedef struct T_THREAD_SYNCH {
122  size_t path_len;
124  short initialized;
125 } thread_synch_t;
126 
127 
131 typedef struct T_THREAD_GROUP {
132 
136  unsigned int group_id;
138  char *group_path;
139  size_t path_len;
141  //struct pollfd param_watcher[PARAM_NUMBER];
142 
143 } thread_group_t;
144 
145 
146 
147 static const char *param_default_path = "/sys/class/group_synch/synch!group%d/group_parameters/";
148 static const char *group_default_path = "/dev/synch/group%d";
149 
150 
151 
152 int initThreadSyncher(thread_synch_t *main_syncher);
153 thread_group_t* installGroup(const group_t group_descriptor, thread_synch_t *main_synch);
154 
155 int readGroupInfo(thread_synch_t *main_syncher);
156 
157 
158 int openGroup(thread_group_t* group);
159 int readMessage(void *buffer, size_t len, thread_group_t *group);
160 int writeMessage(const void *buffer, size_t len, thread_group_t *group);
161 
162 int setDelay(const long _delay, thread_group_t *group);
163 int revokeDelay(thread_group_t *group);
164 int cancelDelay(thread_group_t *group);
165 
166 int sleepOnBarrier(thread_group_t *group);
167 int awakeBarrier(thread_group_t *group);
168 
169 unsigned long getCurrentStorageSize(thread_group_t *group);
170 unsigned long getMaxStorageSize(thread_group_t *group);
171 unsigned long getMaxMessageSize(thread_group_t *group);
172 
173 int setMaxMessageSize(thread_group_t *group, unsigned long val);
174 int setMaxStorageSize(thread_group_t *group, unsigned long val);
175 int setGarbageCollectorRatio(thread_group_t *group, unsigned long val);
176 int includeStructureSize(thread_group_t *group, const bool value);
177 
178 int enableStrictMode(thread_group_t *group);
179 int disableStrictMode(thread_group_t *group);
180 int changeOwner(thread_group_t *group, const uid_t new_owner);
181 int becomeOwner(thread_group_t *group);
182 
183 
184 
185 
186 
187 
188 thread_group_t *loadGroupFromDescriptor(const group_t *descriptor, thread_synch_t *main_syncher);
189 thread_group_t* loadGroupFromID(const int group_id);
190 
191 #endif //THREAD_SYNCH_H
openGroup
int openGroup(thread_group_t *group)
Open a group, the thread PID is added to group's active members.
Definition: thread_synch.c:404
disableStrictMode
int disableStrictMode(thread_group_t *group)
Disable strict mode on a group.
Definition: thread_synch.c:833
T_THREAD_GROUP::group_id
unsigned int group_id
Definition: thread_synch.h:136
awakeBarrier
int awakeBarrier(thread_group_t *group)
Awake all the process present in a wait queue for a given group.
Definition: thread_synch.c:639
enableStrictMode
int enableStrictMode(thread_group_t *group)
Enable strict mode on a group.
Definition: thread_synch.c:808
T_THREAD_SYNCH
User-level handler of the thread-synch main device.
Definition: thread_synch.h:118
loadGroupFromID
thread_group_t * loadGroupFromID(const int group_id)
Load a group structure given a group ID.
Definition: thread_synch.c:1000
revokeDelay
int revokeDelay(thread_group_t *group)
Revoke all the delayed messaged on a given group and deliver it.
Definition: thread_synch.c:599
getMaxStorageSize
unsigned long getMaxStorageSize(thread_group_t *group)
Get the maximum storage size value for a given group.
Definition: thread_synch.c:683
setGarbageCollectorRatio
int setGarbageCollectorRatio(thread_group_t *group, unsigned long val)
Set the garbage collector ratio value for a given group.
Definition: thread_synch.c:783
T_THREAD_SYNCH::main_file_descriptor
int main_file_descriptor
Definition: thread_synch.h:119
changeOwner
int changeOwner(thread_group_t *group, const uid_t new_owner)
Change the owner of a group.
Definition: thread_synch.c:887
initThreadSyncher
int initThreadSyncher(thread_synch_t *main_syncher)
Initialize a 'thread_synch_t' structure.
Definition: thread_synch.c:356
loadGroupFromDescriptor
thread_group_t * loadGroupFromDescriptor(const group_t *descriptor, thread_synch_t *main_syncher)
Load a group's structure given a group descriptor.
Definition: thread_synch.c:927
setDelay
int setDelay(const long _delay, thread_group_t *group)
Set the delay value for a given group.
Definition: thread_synch.c:579
t_message::buffer
void * buffer
Definition: types.h:45
T_THREAD_GROUP::descriptor
group_t descriptor
Definition: thread_synch.h:135
writeMessage
int writeMessage(const void *buffer, size_t len, thread_group_t *group)
Write a message in a given group.
Definition: thread_synch.c:557
setMaxMessageSize
int setMaxMessageSize(thread_group_t *group, unsigned long val)
Set the maximum message size value for a given group.
Definition: thread_synch.c:739
sleepOnBarrier
int sleepOnBarrier(thread_group_t *group)
Put the process which call this function on sleep.
Definition: thread_synch.c:619
T_THREAD_SYNCH::initialized
short initialized
Definition: thread_synch.h:124
t_message::author
pid_t author
Definition: types.h:44
setMaxStorageSize
int setMaxStorageSize(thread_group_t *group, unsigned long val)
Set the maximum storage size value for a given group.
Definition: thread_synch.c:761
T_THREAD_SYNCH::path_len
size_t path_len
Definition: thread_synch.h:122
T_THREAD_GROUP::group_path
char * group_path
Definition: thread_synch.h:138
includeStructureSize
int includeStructureSize(thread_group_t *group, const bool value)
Set a flag to count also supporting structure size in storage calculation.
Definition: thread_synch.c:860
T_THREAD_GROUP
User-level handler of a group.
Definition: thread_synch.h:131
T_THREAD_GROUP::path_len
size_t path_len
Definition: thread_synch.h:139
getMaxMessageSize
unsigned long getMaxMessageSize(thread_group_t *group)
Get the maximum message size value for a given group.
Definition: thread_synch.c:659
t_message
Basic structure to represent a message.
Definition: types.h:43
t_message::size
size_t size
Definition: types.h:46
T_THREAD_GROUP::file_descriptor
int file_descriptor
Definition: thread_synch.h:133
group_t
System-wide descriptor of a group.
Definition: types.h:139
getCurrentStorageSize
unsigned long getCurrentStorageSize(thread_group_t *group)
Get the current storage size value for a given group.
Definition: thread_synch.c:710
becomeOwner
int becomeOwner(thread_group_t *group)
Change the owner of a group to the calling thread's UID.
Definition: thread_synch.c:912
readMessage
int readMessage(void *buffer, size_t len, thread_group_t *group)
Read a message from a given group.
Definition: thread_synch.c:524
cancelDelay
int cancelDelay(thread_group_t *group)
Deliver immediately all delayed messages.
Definition: thread_synch.c:1054
installGroup
thread_group_t * installGroup(const group_t group_descriptor, thread_synch_t *main_synch)
Install a group in the system given a group descriptor.
Definition: thread_synch.c:442
T_THREAD_SYNCH::main_device_path
char * main_device_path
Definition: thread_synch.h:121