summaryrefslogtreecommitdiff
path: root/c/sources/ezipc.h
blob: ff69fe17dffd3ee589225d1fbd64ce7992417a87 (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * BSD 3-Clause License (BSD-3-Clause)
 *
 * Copyright (C) 2022 - dweller <dweller@cabin.digital>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


/*
 *                                    EZIPC
 *          EZ(easy) Inter-Process Communication single-file library
 *
 *
 * At the moment, this is just a wrapper around POSIX named pipes (aka FIFOs)
 * that creates duplex communication channel between 2 processes.
 *
 *
 *                              Brief Description
 *
 * Just create the connections with ezi_create() on a "server" and ezi_connect()
 * on a "client" process. Then use ezi_{send,recv}() functions to push data
 * around. There is no functional difference between "server" and "client"
 * except that "server" is ultimately responsible for OS resources (the created
 * named pipes) clean up.
 * When you're done, ezi_destroy() the connection on the "server" and
 * ezi_disconnect() it on the "client" side.
 *
 * The library provides a reliable way to send sized frames between 2 processes.
 * The data size is enforced, but other than that there are no assumptions about
 * possible higher-level usage of this code. If the reader cannot receive full
 * frame at once, it is truncated and the reset is discarded. Send and receive
 * failures are considered fatal and restart (disconnected, reconnect) of the
 * connection is required.
 *
 * All functions are blocking on their specific action. Create/Connect wait for
 * the other end of the connection, send waits for there to be room in the OS
 * buffers, recv waits for something to read from OS buffers.
 *
 * While more than 2 process are able to read from same connection
 * (read: named pipes), THIS IS *HIGHLY* INADVISABLE, as there are no guarantees
 * about data interleaving in such case.
 *
 *
 *                             Single-file Usage
 *
 * Define "EZIPC_IMPL" before this header inclusion into any *.c file to get
 * the implementation code injected into said .c file. Compile and run.
 *
 *
 *                                 Examples
 *
 * See files server.c, client.c, and common.h for a simple usage example.
 *
 *
 *                               Symbol List
 *
 * EZIPC_VERSION
 *
 * typedef struct ezi_conn_s ezi_conn
 *
 * ezi_conn* ezi_create(const char* conn_path)
 * void ezi_destroy(ezi_conn* conn)
 *
 * ezi_conn* ezi_connect(const char* conn_path)
 * void ezi_disconnect(ezi_conn* conn)
 *
 * bool ezi_send(ezi_conn* conn, const void* data, size_t size)
 * bool ezi_recv(ezi_conn* conn, void* data, size_t* size);
 *
 * See bellow for specific function descriptions.
 *
 *
 *                                Compliance
 *
 * This code adheres to C99 standard, and was tested with following command:
 * > $ gcc -std=c99 -Wall -Wextra -pedantic
 *
 *
 *                               Known Issues
 *
 *  1. When "server" abruptly disconnects "client" gets SIGPIPE "broken pipe"
 *     signal, which is annoying because it would be way better to just return
 *     false from last called function and perror() for information.
 *
 *     Workaround: "client" can insert a signal handler and have a callback, or
 *                 accept quick death as a positive.
 *
 *
 *                                 Changelog
 *
 * v1.2 - ezi_recv() now fails on receiving a size of 0 bytes
 * v1.1 - C++ support (extern and void* casts)
 * v1   - release
 */


#ifndef EZIPC_H
#define EZIPC_H


#define EZIPC_VERSION "v1.2"


#include <stddef.h>
#include <stdbool.h>


#ifdef __cplusplus
extern "C" {
#endif


/*
 * Opaque pointer to the structure representing the connection.
 */
typedef struct ezi_conn_s ezi_conn;


/*
 * Creates the "server" (controlling/initial) end of the connection. This end
 * of the connection may create underlying OS resources if they do not exist,
 * AND IS responsible for cleaning them.
 * resources.
 *
 * BLOCKS: waits for other end to connect.
 *
 * Arguments:
 *   IN `conn_path` - path to store the implementation-related resources,
 *                    doubles as a connection identifier.
 *
 * Returns the connection object as an opaque pointer.
 */
extern ezi_conn* ezi_create(const char* conn_path);

/*
 * Destroys the connection and frees the underlying OS resources if connection
 * was established with `ezi_create()` function.
 *
 * Arguments:
 *   IN `conn` - connection object that represents the connection.
 *
 *  Returns: nothing.
 */
extern void ezi_destroy(ezi_conn* conn);

/*
 * Creates the "client" end of the connection. This end of the connection may
 * create underlying OS resources if they do not exist, but IS NOT responsible
 * for them.
 *
 * BLOCKS: waits for other end to connect.
 *
 * Arguments:
 *   IN `conn_path` - path to store the implementation-related resources,
 *                    doubles as a connection identifier.
 *
 * Returns the connection object as an opaque pointer.
 */

extern ezi_conn* ezi_connect(const char* conn_path);

/*
 * Destroys the connection and frees the underlying OS resources if connection
 * was established with `ezi_create()` function.
 * Is an alias of `ezi_destroy`. Exists for symmetry in the API.
 *
 * Arguments:
 *   IN `conn` - connection object that represents the connection.
 *
 *  Returns: nothing.
 */
#define ezi_disconnect(conn) ezi_destroy(conn)


/*
 * Sends `size` bytes to the other endpoint.
 *
 * BLOCKS: waits for room in the underlying buffer.
 *
 * Arguments:
 *   IN `conn` - connection object that represents the connection.
 *   IN `data` - buffer of `size` bytes to be sent.
 *   IN `size` - size of the data to be sent, in bytes.
 *
 * Returns: true on success, false on failure. If failed, the connection is
 *          thought to be in undefined state and has to be closed and reopened
 *          for communication to continue.
 */
extern bool ezi_send(ezi_conn* conn, const void* data, size_t size);

/*
 * Receives up to `size` bytes from the other endpoint. If there is more data
 * available that `size`, it is *discarded*!
 *
 * BLOCKS: waits for data in the underlying buffer.
 *
 * Arguments:
 *   IN    `conn` - connection object that represents the connection.
 *   OUT   `data` - buffer of `size` bytes to be filled with incoming data;
 *                  on success holds the received data, which may be truncated
 *                       to the input `size`;
 *                  on failure data is undefined.
 *   INOUT `size` - as an input: specifies the size of `data` buffer in bytes;
 *                  as an output:
 *                    on success holds the size of the received data, which may
 *                          be smaller than the `data` buffer size passed as an
 *                          input;
 *                    on failure is unchanged from input.
 *
 * Returns: true on success, false on failure. If failed, the connection is
 *          thought to be in undefined state and has to be closed and reopened
 *          for communication to continue.
 */
extern bool ezi_recv(ezi_conn* conn, void* data, size_t* size);

#ifdef __cplusplus
}
#endif // EXTERN C


#endif // EZIPC_H


//
// =========================================================================
//
// What follows are implementation details
//
//

#ifdef EZIPC_IMPL
#ifndef EZIPC_IMPLEMENTED
#define EZIPC_IMPLEMENTED // redefinition guard
#ifdef __linux__

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>


#ifdef __cplusplus
extern "C" {
#endif


struct ezi_conn_s
{
    char* path_c2s;
    char* path_s2c;
    int c2s, s2c;

    bool server;
};


static ezi_conn* _ezi_internal_create(const char* conn_path,
                                      const mode_t modes[2])
{
    if(!conn_path) return NULL;

    ezi_conn* conn = (ezi_conn*)calloc(1, sizeof(*conn));
    if(!conn) return NULL;

    conn->c2s = -1;
    conn->s2c = -1;

    // Populate path strings
    {
        const size_t path_len = strlen(conn_path);

        conn->path_c2s = (char*)malloc(path_len + 5);
        if(!conn->path_c2s) goto cleanup;

        strcat(conn->path_c2s, conn_path);
        strcat(conn->path_c2s, "_c2s");

        conn->path_s2c = (char*)malloc(path_len + 5);
        if(!conn->path_s2c) goto cleanup;

        strcat(conn->path_s2c, conn_path);
        strcat(conn->path_s2c, "_s2c");
    }


    // Create POSIX FIFOs
    {
        if(mkfifo(conn->path_c2s, 0660) != 0)
        {
            if(errno != EEXIST)
            {
                perror("[EZIPC] Could not create client->server pipe");
                goto cleanup;
            }
        }

        if(mkfifo(conn->path_s2c, 0660) != 0)
        {
            if(errno != EEXIST)
            {
                perror("[EZIPC] Could not create server->client pipe");
                goto cleanup;
            }
        }
    }

    // Open the FIFOs
    {
        conn->c2s = open(conn->path_c2s, modes[0]);
        if(conn->c2s < 0)
        {
            perror("[EZIPC] Could not open client->server pipe");
            goto cleanup;
        }

        conn->s2c = open(conn->path_s2c, modes[1]);
        if(conn->s2c < 0)
        {
            perror("[EZIPC] Could not open server->client pipe");
            goto cleanup;
        }
    }

    return conn;

cleanup:

    if(conn)
    {
        ezi_destroy(conn);

        if(conn->path_c2s) free(conn->path_c2s);
        if(conn->path_s2c) free(conn->path_s2c);

        free(conn);
    }

    return NULL;
}


ezi_conn* ezi_create(const char* conn_path)
{
    const mode_t modes[2] = { O_RDONLY, O_WRONLY};

    ezi_conn* conn = _ezi_internal_create(conn_path, modes);
    if(conn) conn->server = true;

    return conn;
}

ezi_conn* ezi_connect(const char* conn_path)
{
    const mode_t modes[2] = { O_WRONLY, O_RDONLY};

    ezi_conn* conn = _ezi_internal_create(conn_path, modes);
    if(conn) conn->server = false;

    return conn;
}

void ezi_destroy(ezi_conn* conn)
{
    if(!conn) return;

    // NOTE: even if close() errors out, DO NOT retry (even if EINTR) as per
    //       man 2 close. Just report the error and clobber the FDs.

    if(conn->c2s >= 0)
    {
        int rc = close(conn->c2s);
        if(rc < 0) perror("[EZIPC] Could not close client->server pipe");

        conn->c2s = -1;
    }

    if(conn->s2c >= 0)
    {
        int rc = close(conn->s2c);
        if(rc < 0) perror("[EZIPC] Could not close server->client pipe");

        conn->s2c = -1;
    }

    // I could check and handle remove error here, but I'd rather tell the user
    // and not accidentally delete something else

    if(conn->server)
    {
        if(conn->path_c2s && (remove(conn->path_c2s) != 0))
            perror("[EZIPC] Could not delete client->server pipe");

        if(conn->path_s2c && (remove(conn->path_s2c) != 0))
            perror("[EZIPC] Could not delete server->client pipe");
    }
}

// wrapper over write() to handle interrupted syscall
static bool _ezi_write(int ep, const void* buffer, size_t size)
{
    const uint8_t* bytes = (uint8_t*)buffer;

    size_t written = 0;
    while(written < size)
    {
        size_t got = write(ep, bytes + written, size - written);
        if(got <= 0)// FIXME: technically < 0 but I don't want an infinite loop
        {
            perror("[EZIPC] Could not write to pipe");
            return false;
        }

        written += size;
    }

    return true;
}


bool ezi_send(ezi_conn* conn, const void* data, size_t size)
{
    if(!conn) return false;
    if(!data) return false;
    if(size == 0) return true;

    const int ep = conn->server ? conn->s2c : conn->c2s;
    if(ep >= 0)
    {
        const uint64_t out_size = (uint64_t)size;
        if(!_ezi_write(ep, &out_size, sizeof(out_size))) return false;
        if(!_ezi_write(ep, data, size)) return false;

        return true;
    }
    else return false;

}

// wrapper over read() to handle interrupted syscall
static bool _ezi_read(int ep, void* buffer, size_t size)
{
    uint8_t* bytes = (uint8_t*)buffer;

    size_t readback = 0;
    while(readback < size)
    {
        ssize_t got = read(ep, bytes + readback, size - readback);
        if(got == 0)
        {
            // NOTE: I think reading 0 bytes in non-blocking mode is fine,
            //       but since I don't provide non-blocking mode, this is
            //       still an error.
            return false;
        }
        else if(got < 0)
        {
            perror("[EZIPC] Could not read from pipe");
            return false;
        }

        readback += got;
    }

    return true;
}


bool ezi_recv(ezi_conn* conn, void* data, size_t* size)
{
    if(!conn) return false;
    if(!data) return false;
    if(!size) return false;
    if(*size == 0) return true;

    const int ep = conn->server ? conn->c2s : conn->s2c;
    if(ep >= 0)
    {
        // NOTE: since this is IPC, we're on the same machine so no need to
        //       ensure endianness
        uint64_t in_size = 0;
        if(!_ezi_read(ep, &in_size, sizeof(in_size))) return false;

        if(in_size == 0) return false;

        const uint64_t read_size = in_size > *size ? *size : in_size;
        if(!_ezi_read(ep, data, read_size)) return false;

        // If we have left over, get rid of it to guarantee correct read next
        // time
        for(uint64_t i = 0; i < (in_size - read_size); i++)
        {
            uint8_t temp; // I know I could read in bigger chunks, but honestly
                          // I don't care, and assume this is an edge case
            if(!_ezi_read(ep, &temp, 1)) return false;
        }

        *size = read_size;
        return true;

    } else return false;

}

#ifdef __cplusplus
}
#endif // EXTERN C

#else // __linux__
    #error "EZ IPC library is not supported on your platform. Want to port?"
#endif // platforms
#endif // EZIPC_IMPLEMENTED
#endif // EZIPC_IMPL