| 1 |
#pragma once |
| 2 |
|
| 3 |
/* |
| 4 |
* D-Bus Input/Output Queues |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <c-list.h> |
| 8 |
#include <c-stdaux.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include "util/fdlist.h" |
| 11 |
#include "util/user.h" |
| 12 |
|
| 13 |
typedef struct IQueue IQueue; |
| 14 |
|
| 15 |
#define IQUEUE_LINE_MAX (16UL * 1024UL) /* taken from dbus-daemon(1) */ |
| 16 |
#define IQUEUE_RECV_MAX (2UL * 1024UL) /* based on average message size */ |
| 17 |
|
| 18 |
enum { |
| 19 |
_IQUEUE_E_SUCCESS, |
| 20 |
|
| 21 |
IQUEUE_E_PENDING, |
| 22 |
IQUEUE_E_QUOTA, |
| 23 |
IQUEUE_E_VIOLATION, |
| 24 |
}; |
| 25 |
|
| 26 |
struct IQueue { |
| 27 |
User *user; |
| 28 |
|
| 29 |
UserCharge charge_data; |
| 30 |
UserCharge charge_fds; |
| 31 |
char *data; |
| 32 |
size_t data_size; |
| 33 |
size_t data_start; |
| 34 |
size_t data_end; |
| 35 |
size_t data_cursor; |
| 36 |
FDList *fds; |
| 37 |
|
| 38 |
struct { |
| 39 |
UserCharge charge_data; |
| 40 |
UserCharge charge_fds; |
| 41 |
void *data; |
| 42 |
size_t n_data; |
| 43 |
size_t n_copied; |
| 44 |
FDList *fds; |
| 45 |
} pending; |
| 46 |
|
| 47 |
char buffer[IQUEUE_RECV_MAX]; |
| 48 |
}; |
| 49 |
|
| 50 |
#define IQUEUE_NULL(_x) { \ |
| 51 |
.charge_data = USER_CHARGE_INIT, \ |
| 52 |
.charge_fds = USER_CHARGE_INIT, \ |
| 53 |
.data = (_x).buffer, \ |
| 54 |
.data_size = sizeof((_x).buffer), \ |
| 55 |
.pending.charge_data = USER_CHARGE_INIT, \ |
| 56 |
.pending.charge_fds = USER_CHARGE_INIT, \ |
| 57 |
} |
| 58 |
|
| 59 |
struct OQueue { |
| 60 |
User *user; |
| 61 |
CList list_buffers; |
| 62 |
CList list_inflight; |
| 63 |
}; |
| 64 |
|
| 65 |
#define OQUEUE_NULL(_x) { \ |
| 66 |
.list_buffers = C_LIST_INIT((_x).list_buffers), \ |
| 67 |
.list_inflight = C_LIST_INIT((_x).list_inflight), \ |
| 68 |
} |
| 69 |
|
| 70 |
/* input queue */ |
| 71 |
|
| 72 |
void iqueue_init(IQueue *iq, User *user); |
| 73 |
void iqueue_deinit(IQueue *iq); |
| 74 |
|
| 75 |
void iqueue_flush(IQueue *iq); |
| 76 |
int iqueue_set_target(IQueue *iq, void *data, size_t n_data); |
| 77 |
int iqueue_get_cursor(IQueue *iq, |
| 78 |
void **bufferp, |
| 79 |
size_t **fromp, |
| 80 |
size_t *top, |
| 81 |
FDList ***fdsp, |
| 82 |
UserCharge **charge_fdsp); |
| 83 |
|
| 84 |
int iqueue_pop_line(IQueue *iq, const char **linep, size_t *np); |
| 85 |
int iqueue_pop_data(IQueue *iq, FDList **fds); |
| 86 |
|
| 87 |
/* inline helpers */ |
| 88 |
|
| 89 |
static inline void *iqueue_get_target(IQueue *iq) { |
| 90 |
return iq->pending.data; |
| 91 |
} |
| 92 |
|
| 93 |
static inline bool iqueue_is_eof(IQueue *iq) { |
| 94 |
return iq->data_cursor >= iq->data_end && |
| 95 |
(!iq->pending.data || iq->pending.n_copied < iq->pending.n_data); |
| 96 |
} |