-
Notifications
You must be signed in to change notification settings - Fork 0
/
child_messages.c
151 lines (133 loc) · 3.64 KB
/
child_messages.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
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
148
149
150
151
/*
* Copyright (c) 2017 Max Fillinger
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <imsg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "child_errors.h"
#include "child_messages.h"
#include "message_types.h"
#define IMSG_FAILURE (-1)
#define IMSG_GET_NO_MESSAGES (0)
#define IMSG_MAX_MESSAGE_LENGTH (UINT16_MAX)
static struct imsgbuf ibuf;
static int is_invalid_message_type(MESSAGE_TYPE);
void
initialize_ipc(int fd)
{
/*
* Initialize the data structures for communicating with the parent
* process.
*/
imsg_init(&ibuf, fd);
}
void
send_messages(void)
{
/* Send the enqueued messages to the parent. */
if (imsg_flush(&ibuf) == IMSG_FAILURE) {
ipc_error("imsg_flush");
}
}
void
receive_messages(void)
{
/*
* Receive messages from the parent and make them available for
* get_next_message().
*/
if (imsg_read(&ibuf) == IMSG_FAILURE) {
ipc_error("imsg_read");
}
}
void
enqueue_message(MESSAGE_TYPE type, char *message)
{
/*
* Enqueue a message to the parent. The message has to be a
* null-terminated string and will be truncated if it exceeds the
* maximum length for imsg_compose (64 kb, so this should not actually
* happen).
*/
if (is_invalid_message_type(type)) {
child_fatalx("Invalid MESSAGE_TYPE in enqueue_message.");
}
/*
* Calculate the length of the message, including the terminating
* null-byte.
*/
size_t message_length = strlen(message) + 1;
/* Truncate the message, if necessary. */
if (IMSG_MAX_MESSAGE_LENGTH < message_length) {
message_length = IMSG_MAX_MESSAGE_LENGTH;
message[message_length-1] = '\0';
}
/*
* Enqueue the message. Casting message_length to uint16_t is ok
* because it can't be larger after truncation.
*/
if (imsg_compose(&ibuf, (uint32_t)type, 0, getpid(), -1, message,
(uint16_t)message_length) == IMSG_FAILURE)
ipc_error("Error in imsg_compose.");
}
GET_NEXT_MESSAGE_STATUS
get_next_message(struct message *message)
{
/*
* Grab the next message from the buffer and return NO_MESSAGES if
* there are none.
*/
struct imsg imessage;
ssize_t imsg_get_status;
imsg_get_status = imsg_get(&ibuf, &imessage);
if (imsg_get_status == IMSG_FAILURE) {
ipc_error("Error in imsg_get.");
}
if (imsg_get_status == IMSG_GET_NO_MESSAGES) {
return (0);
}
/*
* Check if the message type is valid and extract additional data if
* applicable.
*/
switch (imessage.hdr.type) {
case (CMD_NEW_INPUT_FILE):
message->type = imessage.hdr.type;
message->data.fd = imessage.fd;
break;
case (CMD_EXIT):
case (CMD_META):
case (CMD_PLAY):
case (CMD_PAUSE):
message->type = imessage.hdr.type;
/* Set message->data to a dummy value. */
message->data.fd = -1;
break;
default:
child_fatalx("Invalid CMD_MESSAGE_TYPE received.");
}
imsg_free(&imessage);
return (1);
}
static int
is_invalid_message_type(MESSAGE_TYPE type)
{
return (type < 0 || MSG_SENTINEL <= type);
}