-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmessage_sender.c
More file actions
56 lines (49 loc) · 1.14 KB
/
message_sender.c
File metadata and controls
56 lines (49 loc) · 1.14 KB
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
//
// Created by sid on 07/12/2020.
//
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h> /* open */
#include <unistd.h> /* exit */
#include <sys/ioctl.h> /* ioctl */
#include "message_slot.h"
void clean(int fd) {
close(fd);
}
void exitAndClean(int fd) {
clean(fd);
exit(errno);
}
int main(int c, char **args) {
char *deviceFile, *msg;
long channelId;
int fd;
if (c != 4) {
perror("wrong amount of arguments given");
exit(1);
}
deviceFile = args[1];
channelId = strtol(args[2], NULL, 10);
if (channelId == UINT_MAX && errno == ERANGE) {
perror("wrong channelId given");
exit(1);
}
msg = args[3];
fd = open(deviceFile, O_RDWR);
if (fd < 0) {
perror("could not open file");
clean(fd);
}
if (ioctl(fd, MSG_SLOT_CHANNEL, (unsigned long) channelId) < 0) {
perror("ioctl failed");
exitAndClean(fd);
}
if (write(fd, msg, strlen(msg)) < 0) {
perror("write did not return correct amount of bytes");
exitAndClean(fd);
}
close(fd);
}