forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indidriverio.c
283 lines (236 loc) · 7.65 KB
/
indidriverio.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <pthread.h>
#include "indidriver.h"
#include "userio.h"
#include "indiuserio.h"
#include "indidriverio.h"
/* Buffer size. Must be ^ 2 */
#define OUTPUTBUFF_ALLOC 32768
/* Dump whole buffer when growing over this */
#define OUTPUTBUFF_FLUSH_THRESOLD 65536
#define MAXFD_PER_MESSAGE 16
static void driverio_flush(driverio * dio, const void * additional, size_t add_size);
static pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Return the buffer size required for storage (rounded to next OUTPUTBUFF_ALLOC) */
static unsigned int outBuffRequired(unsigned int storage) {
return (storage + OUTPUTBUFF_ALLOC - 1) & ~(OUTPUTBUFF_ALLOC - 1);
}
static int outBuffAllocated(struct driverio * dio) {
return outBuffRequired(dio->outPos);
}
static void outBuffGrow(struct driverio * dio, int required) {
dio->outBuff = realloc(dio->outBuff, required);
if (dio->outBuff == NULL) {
perror("malloc");
_exit(1);
}
}
static size_t driverio_write(void *user, const void * ptr, size_t count)
{
struct driverio * dio = (struct driverio*) user;
if (dio->outPos + count > OUTPUTBUFF_FLUSH_THRESOLD) {
driverio_flush(dio, ptr, count);
} else {
unsigned int allocated = outBuffAllocated(dio);
unsigned int required = outBuffRequired(dio->outPos + count);
if (required != allocated) {
outBuffGrow(dio, required);
}
memcpy(dio->outBuff + dio->outPos, ptr, count);
dio->outPos += count;
}
return count;
}
static int driverio_vprintf(void *user, const char * fmt, va_list arg)
{
struct driverio * dio = (struct driverio*) user;
int available;
int size = 0;
unsigned int allocated = outBuffAllocated(dio);
while(1) {
available = allocated - dio->outPos;
/* Determine required size */
size = vsnprintf(dio->outBuff + dio->outPos, available, fmt, arg);
if (size < 0)
return size;
if (size < available) {
break;
}
allocated = outBuffRequired(available + size + 1);
outBuffGrow(dio, allocated);
}
dio->outPos += size;
return size;
}
static void driverio_join(void * user, const char * xml, void * blob, size_t bloblen)
{
struct driverio * dio = (struct driverio*) user;
dio->joinCount++;
dio->joins = (void **)realloc((void*)dio->joins, sizeof(void*) * dio->joinCount);
dio->joinSizes = (size_t *)realloc((void*)dio->joinSizes, sizeof(size_t) * dio->joinCount);
dio->joins[dio->joinCount - 1] = blob;
dio->joinSizes[dio->joinCount - 1] = bloblen;
driverio_write(user, xml, strlen(xml));
}
static void driverio_flush(driverio * dio, const void * additional, size_t add_size) {
struct msghdr msgh;
struct iovec iov[2];
int cmsghdrlength;
struct cmsghdr * cmsgh;
if (dio->outPos + add_size) {
int ret = -1;
void ** temporaryBuffers = NULL;
int fdCount = dio->joinCount;
if (fdCount > 0) {
if (dio->joinCount > MAXFD_PER_MESSAGE) {
errno = EMSGSIZE;
perror("sendmsg");
exit(1);
}
cmsghdrlength = CMSG_SPACE((fdCount * sizeof(int)));
cmsgh = (struct cmsghdr*)malloc(cmsghdrlength);
// FIXME: abort on alloc error here
temporaryBuffers = (void**)malloc(sizeof(void*)*fdCount);
/* Write the fd as ancillary data */
cmsgh->cmsg_len = CMSG_LEN(sizeof(int));
cmsgh->cmsg_level = SOL_SOCKET;
cmsgh->cmsg_type = SCM_RIGHTS;
msgh.msg_control = cmsgh;
msgh.msg_controllen = cmsghdrlength;
for(int i = 0; i < fdCount; ++i) {
void * blob = dio->joins[i];
size_t size = dio->joinSizes[i];
int fd = IDSharedBlobGetFd(blob);
if (fd == -1) {
// Can't avoid a copy here. Update the driver to change that
temporaryBuffers[i] = IDSharedBlobAlloc(size);
memcpy(temporaryBuffers[i], blob, size);
fd = IDSharedBlobGetFd(temporaryBuffers[i]);
} else {
temporaryBuffers[i] = NULL;
}
((int *) CMSG_DATA(CMSG_FIRSTHDR(&msgh)))[i] = fd;
}
} else {
cmsgh = NULL;
cmsghdrlength = 0;
msgh.msg_control = cmsgh;
msgh.msg_controllen = cmsghdrlength;
}
iov[0].iov_base = dio->outBuff;
iov[0].iov_len = dio->outPos;
if (add_size) {
iov[1].iov_base = (void*)additional;
iov[1].iov_len = add_size;
}
msgh.msg_flags = 0;
msgh.msg_name = NULL;
msgh.msg_namelen = 0;
msgh.msg_iov = iov;
msgh.msg_iovlen = add_size ? 2 : 1;
if (!dio->locked) {
pthread_mutex_lock(&stdout_mutex);
dio->locked = 1;
}
ret = sendmsg(1, &msgh, 0);
if (ret == -1) {
perror("sendmsg");
// FIXME: exiting the driver seems abrupt. Is this the right thing to do ? what about cleanup ?
exit(1);
} else if ((unsigned)ret != dio->outPos + add_size) {
// This is not expected on blocking socket
fprintf(stderr, "short write\n");
exit(1);
}
if (fdCount > 0) {
for(int i = 0; i < fdCount; ++i) {
if (temporaryBuffers[i] != NULL) {
IDSharedBlobFree(temporaryBuffers[i]);
}
}
free(cmsgh);
free(temporaryBuffers);
}
}
if (dio->joins != NULL) {
free(dio->joins);
}
dio->joins = NULL;
if (dio->joinSizes != NULL) {
free(dio->joinSizes);
}
dio->joinSizes = NULL;
if (dio->outBuff != NULL) {
free(dio->outBuff);
}
dio->outPos = 0;
}
static int driverio_is_unix = -1;
static int is_unix_io() {
if (driverio_is_unix != -1) {
return driverio_is_unix;
}
int domain;
socklen_t result = sizeof(domain);
if (getsockopt(1, SOL_SOCKET, SO_DOMAIN, (void*)&domain, &result) == -1) {
driverio_is_unix = 0;
} else if (result != sizeof(domain) || domain != AF_UNIX) {
driverio_is_unix = 0;
} else {
driverio_is_unix = 1;
}
return driverio_is_unix;
}
/* Unix io allow attaching buffer in ancillary data. */
static void driverio_init_unix(driverio * dio) {
dio->userio.vprintf = &driverio_vprintf;
dio->userio.write = &driverio_write;
dio->userio.joinbuff = &driverio_join;
dio->user = (void*)dio;
dio->joins = NULL;
dio->joinSizes = NULL;
dio->locked = 0;
dio->joinCount = 0;
dio->outBuff = NULL;
dio->outPos = 0;
}
static void driverio_finish_unix(driverio * dio) {
driverio_flush(dio, NULL, 0);
if (dio->locked) {
pthread_mutex_unlock(&stdout_mutex);
dio->locked = 0;
}
}
static void driverio_init_stdout(driverio * dio) {
dio->userio = *userio_file();
dio->user = stdout;
pthread_mutex_lock(&stdout_mutex);
}
static void driverio_finish_stdout(driverio * dio) {
(void)dio;
fflush(stdout);
pthread_mutex_unlock(&stdout_mutex);
}
void driverio_init(driverio * dio)
{
if (is_unix_io()) {
driverio_init_unix(dio);
} else {
driverio_init_stdout(dio);
}
}
void driverio_finish(driverio * dio)
{
if (is_unix_io()) {
driverio_finish_unix(dio);
} else {
driverio_finish_stdout(dio);
}
}