-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgctl.c
More file actions
147 lines (126 loc) · 2.79 KB
/
msgctl.c
File metadata and controls
147 lines (126 loc) · 2.79 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* A command line interface to sys v message queues.
* msgctl create <path>
* msgctl delete <id>
* msgctl send -i <id> [num] msg
* msgctl receive -i <id> [num]
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/msg.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
struct msg {
long type;
char text[1];
};
static void exit_usage() {
puts("msgctl create");
puts("msgctl delete <id>");
puts("msgctl send -n<num> <id> <msg>");
puts("msgctl receive -n<num> <id>");
exit(EXIT_FAILURE);
}
static void exit_error(char *reason) {
printf("%s - (%d)%s\n", reason, errno, strerror(errno));
exit(EXIT_FAILURE);
}
static void create() {
int id;
if ((id = msgget(IPC_PRIVATE, S_IRWXU)) == -1)
exit_error("msgget");
printf("%d\n", id);
}
static void delete(int argc, char **argv) {
int id;
char *ep;
if (argc != 1)
exit_usage();
id = (int)strtol(argv[0], &ep, 10);
if (*ep != '\0')
exit_usage();
if (msgctl(id, IPC_RMID, NULL) == -1)
exit_error("msgctl");
printf("Removed %d\n", id);
}
static void send(int num, int argc, char **argv) {
int id;
char *ep;
size_t len;
struct msg *msg;
if (argc != 2)
exit_usage();
id = (int)strtol(argv[0], &ep, 10);
if (*ep != '\0')
exit_usage();
if (num == 0)
num++;
len = strlen(argv[1]) + 1;
if ((msg = malloc(sizeof(struct msg) + len)) == NULL)
exit_error("malloc");
msg->type = num;
strncpy(msg->text, argv[1], len);
if (msgsnd(id, msg, len, 0) == -1)
exit_error("msgsnd");
}
static void receive(int num, int argc, char **argv) {
int id;
char *ep;
size_t len;
struct msg *msg;
struct msqid_ds msqbuf;
if (argc != 1)
exit_usage();
id = (int)strtol(argv[0], &ep, 10);
if (*ep != '\0')
exit_usage();
if (msgctl(id, IPC_STAT, &msqbuf) == -1)
exit_error("msgctl");
msg = calloc(1, msqbuf.msg_qbytes + sizeof(struct msg));
if (msg == NULL)
exit_error("malloc");
if (msgrcv(id, msg, msqbuf.msg_qbytes, num, IPC_NOWAIT) == -1)
exit_error("msgrcv");
msg->text[msqbuf.msg_qbytes - 1] = '\0';
printf("%s\n", msg->text);
}
int main(int argc, char **argv) {
char *operation, *ep;
int ch, num, has_num;
if (argc < 2)
exit_usage();
has_num = 0;
while ((ch = getopt(argc, argv, "n:")) != -1) {
switch (ch) {
case 'n':
has_num = 1;
num = strtol(optarg, &ep, 10);
if (*ep != '\0')
exit_usage();
break;
case '?':
exit_usage();
}
}
operation = argv[1];
if (!has_num) {
num = 0;
optind++;
}
argc -= optind;
argv += optind;
if (strcmp("create", operation) == 0) {
create();
} else if (strcmp("delete", operation) == 0) {
delete(argc, argv);
} else if (strcmp("send", operation) == 0) {
send(num, argc, argv);
} else if (strcmp("receive", operation) == 0) {
receive(num, argc, argv);
}
exit(EXIT_SUCCESS);
}