-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.c
More file actions
88 lines (79 loc) · 2.34 KB
/
chat.c
File metadata and controls
88 lines (79 loc) · 2.34 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
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
/**
* A simple local chat program making use of posix message queues
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#define MLEN 200
#define QPERM S_IRWXU | S_IRWXO
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
void mreceived(int sig, siginfo_t *info, void *data) {
/*
Can't actually see any way of getting mqd from info as the book
suggests... Only way I can think of getting it without already
knowing it is by mounting the message queue filesystem and
checking that.
*/
printf("Siginfo:\n");
printf("si_signo: %d\n", info->si_signo);
printf("si_pid: %d\n", info->si_pid);
printf("si_uid: %d\n", info->si_uid);
printf("si_code: %d\n", info->si_code);
printf("sival_ptr: %p\n", info->si_value.sival_ptr);
}
int main(int argc, char **argv) {
mqd_t q, qrcv, qsnd;
char qnam[PATH_MAX], msg[MLEN];
struct mq_attr mattr;
struct sigevent sev;
struct sigaction sigact;
if (argc < 2)
exit_err("usage: ./chat <queue identifier>");
snprintf(qnam, PATH_MAX, "%s_aux", argv[1]);
mattr.mq_maxmsg = 10;
mattr.mq_msgsize = MLEN;
q = mq_open(argv[1], O_RDWR | O_CREAT | O_EXCL | O_NONBLOCK, QPERM, &mattr);
if (q == (mqd_t)-1 && errno != EEXIST) {
exit_err("mq_open 1");
} else if (q == (mqd_t)-1) {
if ((qsnd = mq_open(argv[1], O_RDWR)) == (mqd_t)-1)
exit_err("mq_open2");
if ((qrcv = mq_open(qnam, O_RDWR | O_CREAT | O_NONBLOCK, QPERM, &mattr)) == (mqd_t)-1)
exit_err("mq_open3");
} else {
qrcv = q;
if ((qsnd = mq_open(qnam, O_RDWR | O_CREAT | O_NONBLOCK, QPERM, &mattr)) == (mqd_t)-1)
exit_err("mq_open4");
}
printf("Receiving mq descriptor: %d\n", qrcv);
sigemptyset(&sigact.sa_mask);
sigact.sa_sigaction = mreceived;
sigact.sa_flags = SA_SIGINFO;
if (sigaction(SIGUSR1, &sigact, NULL) == -1)
exit_err("sigaction");
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGUSR1;
while (1) {
if (mq_notify(qrcv, &sev) == -1 && errno != EBUSY)
exit_err("mq_notify");
while (mq_receive(qrcv, msg, MLEN, NULL) != -1) {
printf("(them): %.*s\n", MLEN, msg);
}
if (fgets(msg, MLEN, stdin) != NULL) {
if (mq_send(qsnd, msg, MLEN, 1) == -1)
exit_err("mq_send msg");
printf("\033[1A(you): %.*s\n", MLEN, msg);
}
}
exit(EXIT_SUCCESS);
}