| 1 | #ifndef FANOS_SHARED_H
 | 
| 2 | #define FANOS_SHARED_H
 | 
| 3 | 
 | 
| 4 | // FANOS: File descriptors And Netstrings Over Sockets.
 | 
| 5 | //
 | 
| 6 | // This library is shared between cpp/ and pyext/.
 | 
| 7 | 
 | 
| 8 | // Callers should initialize
 | 
| 9 | //   FanosError to { 0, NULL }, and
 | 
| 10 | //   FanosResult to { NULL, FANOS_INVALID_LEN }
 | 
| 11 | 
 | 
| 12 | // Callers should check for BOTH I/O errors and protocol errors.
 | 
| 13 | struct FanosError {
 | 
| 14 |   int err_code;           // errno for IOError
 | 
| 15 |   char const* value_err;  // caller must not free; it's global
 | 
| 16 | };
 | 
| 17 | 
 | 
| 18 | #define FANOS_INVALID_LEN -1
 | 
| 19 | #define FANOS_EOF -2
 | 
| 20 | 
 | 
| 21 | struct FanosResult {
 | 
| 22 |   char* data;  // caller must free if non-NULL
 | 
| 23 |   int len;
 | 
| 24 | };
 | 
| 25 | 
 | 
| 26 | // We send or receive 3 file descriptors at a time (for stdin, stdout, stderr)
 | 
| 27 | 
 | 
| 28 | #define FANOS_NUM_FDS 3
 | 
| 29 | 
 | 
| 30 | // Send a byte string and optional FDs to a Unix socket.
 | 
| 31 | //
 | 
| 32 | // Upon failure `err` may be populated with an error message. The caller does
 | 
| 33 | // NOT have to free the message.
 | 
| 34 | void fanos_send(int sock_fd, char* blob, int blob_len, const int* fds,
 | 
| 35 |                 struct FanosError* err);
 | 
| 36 | 
 | 
| 37 | // Receive a byte string and possibly FDs from a Unix socket.
 | 
| 38 | //
 | 
| 39 | // If a message is received, result_out->data is set to a malloc()'d buffer.
 | 
| 40 | // The caller must fre() it.
 | 
| 41 | //
 | 
| 42 | // If there are no more messages, then the result_out->len is set to FANOS_EOF.
 | 
| 43 | //
 | 
| 44 | // Upon failure `err` may be populated with an error message. The caller does
 | 
| 45 | // NOT have to free the message.
 | 
| 46 | void fanos_recv(int sock_fd, int* fd_out, struct FanosResult* result_out,
 | 
| 47 |                 struct FanosError* err);
 | 
| 48 | 
 | 
| 49 | #endif  // FANOS_SHARED_H
 |