1 2 /* 3 * Copyright (C) NGINX, Inc. 4 */ 5 6 #ifndef _NXT_UNIT_H_INCLUDED_ 7 #define _NXT_UNIT_H_INCLUDED_ 8 9 10 #include <inttypes.h> 11 #include <sys/types.h> 12 #include <sys/uio.h> 13 #include <string.h> 14 15 #include "nxt_version.h" 16 #include "nxt_unit_typedefs.h" 17 18 19 enum { 20 NXT_UNIT_OK = 0, 21 NXT_UNIT_ERROR = 1, 22 }; 23 24 enum { 25 NXT_UNIT_LOG_ALERT = 0, 26 NXT_UNIT_LOG_ERR = 1, 27 NXT_UNIT_LOG_WARN = 2, 28 NXT_UNIT_LOG_NOTICE = 3, 29 NXT_UNIT_LOG_INFO = 4, 30 NXT_UNIT_LOG_DEBUG = 5, 31 }; 32 33 #define NXT_UNIT_INIT_ENV "NXT_UNIT_INIT" 34 35 /* 36 * Mostly opaque structure with library state. 37 * 38 * Only user defined 'data' pointer exposed here. The rest is unit 39 * implementation specific and hidden. 40 */ 41 struct nxt_unit_s { 42 void *data; /* User defined data. */ 43 }; 44 45 /* 46 * Thread context. 47 * 48 * First (main) context is provided 'for free'. To receive and process 49 * requests in other thread, one need to allocate context and use it 50 * further in this thread. 51 */ 52 struct nxt_unit_ctx_s { 53 void *data; /* User context-specific data. */ 54 nxt_unit_t *unit; 55 }; 56 57 /* 58 * Unit port identification structure. 59 * 60 * Each port can be uniquely identified by listen process id (pid) and port id. 61 * This identification is required to refer the port from different process. 62 */ 63 struct nxt_unit_port_id_s { 64 pid_t pid; 65 uint32_t hash; 66 uint16_t id; 67 }; 68 69 /* 70 * unit provides port storage which is able to store and find the following 71 * data structures. 72 */ 73 struct nxt_unit_port_s { 74 nxt_unit_port_id_t id; 75 76 int in_fd; 77 int out_fd; 78 79 void *data; 80 }; 81 82 83 struct nxt_unit_buf_s { 84 char *start; 85 char *free; 86 char *end; 87 }; 88 89 90 struct nxt_unit_request_info_s { 91 nxt_unit_t *unit; 92 nxt_unit_ctx_t *ctx; 93 94 nxt_unit_port_t *response_port; 95 96 nxt_unit_request_t *request; 97 nxt_unit_buf_t *request_buf; 98 99 nxt_unit_response_t *response; 100 nxt_unit_buf_t *response_buf; 101 uint32_t response_max_fields; 102 103 nxt_unit_buf_t *content_buf; 104 uint64_t content_length; 105 int content_fd; 106 107 void *data; 108 }; 109 110 111 /* 112 * Set of application-specific callbacks. Application may leave all optional 113 * callbacks as NULL. 114 */ 115 struct nxt_unit_callbacks_s { 116 /* 117 * Process request. Unlike all other callback, this callback 118 * need to be defined by application. 119 */ 120 void (*request_handler)(nxt_unit_request_info_t *req); 121 122 /* Process websocket frame. */ 123 void (*websocket_handler)(nxt_unit_websocket_frame_t *ws); 124 125 /* Connection closed. */ 126 void (*close_handler)(nxt_unit_request_info_t *req); 127 128 /* Add new Unit port to communicate with process pid. Optional. */ 129 int (*add_port)(nxt_unit_ctx_t *, nxt_unit_port_t *port); 130 131 /* Remove previously added port. Optional. */ 132 void (*remove_port)(nxt_unit_t *, nxt_unit_port_t *port); 133 134 /* Remove all data associated with process pid including ports. Optional. */ 135 void (*remove_pid)(nxt_unit_t *, pid_t pid); 136 137 /* Gracefully quit the application. Optional. */ 138 void (*quit)(nxt_unit_ctx_t *); 139 140 /* Shared memory release acknowledgement. Optional. */ 141 void (*shm_ack_handler)(nxt_unit_ctx_t *); 142 143 /* Send data and control to process pid using port id. Optional. */ 144 ssize_t (*port_send)(nxt_unit_ctx_t *, nxt_unit_port_t *port, 145 const void *buf, size_t buf_size, 146 const void *oob, size_t oob_size); 147 148 /* Receive data on port id. Optional. */ 149 ssize_t (*port_recv)(nxt_unit_ctx_t *, nxt_unit_port_t *port, 150 void *buf, size_t buf_size, void *oob, size_t oob_size); 151 }; 152 153 154 struct nxt_unit_init_s { 155 void *data; /* Opaque pointer to user-defined data. */ 156 void *ctx_data; /* Opaque pointer to user-defined data. */ 157 int max_pending_requests; 158 159 uint32_t request_data_size; 160 uint32_t shm_limit; 161 162 nxt_unit_callbacks_t callbacks; 163 164 nxt_unit_port_t ready_port; 165 uint32_t ready_stream; 166 nxt_unit_port_t router_port; 167 nxt_unit_port_t read_port; 168 int log_fd; 169 }; 170 171 172 typedef ssize_t (*nxt_unit_read_func_t)(nxt_unit_read_info_t *read_info, 173 void *dst, size_t size); 174 175 176 struct nxt_unit_read_info_s { 177 nxt_unit_read_func_t read; 178 int eof; 179 uint32_t buf_size; 180 void *data; 181 }; 182 183 184 /* 185 * Initialize Unit application library with necessary callbacks and 186 * ready/reply port parameters, send 'READY' response to master. 187 */ 188 nxt_unit_ctx_t *nxt_unit_init(nxt_unit_init_t *); 189 190 /* 191 * Process received message, invoke configured callbacks. 192 * 193 * If application implements it's own event loop, each datagram received 194 * from port socket should be initially processed by unit. This function 195 * may invoke other application-defined callback for message processing. 196 */ 197 int nxt_unit_process_msg(nxt_unit_ctx_t *, 198 void *buf, size_t buf_size, void *oob, size_t oob_size); 199 200 /* 201 * Main function useful in case when application does not have it's own 202 * event loop. nxt_unit_run() starts infinite message wait and process loop. 203 * 204 * for (;;) { 205 * app_lib->port_recv(...); 206 * nxt_unit_process_msg(...); 207 * } 208 * 209 * The normally function returns when QUIT message received from Unit. 210 */ 211 int nxt_unit_run(nxt_unit_ctx_t *); 212 213 int nxt_unit_run_once(nxt_unit_ctx_t *ctx); 214 215 /* Destroy application library object. */ 216 void nxt_unit_done(nxt_unit_ctx_t *); 217 218 /* 219 * Allocate and initialize new execution context with new listen port to 220 * process requests in other thread. 221 */ 222 nxt_unit_ctx_t *nxt_unit_ctx_alloc(nxt_unit_ctx_t *, void *); 223 224 /* Initialize port_id, calculate hash. */ 225 void nxt_unit_port_id_init(nxt_unit_port_id_t *port_id, pid_t pid, uint16_t id); 226 227 /* Calculates hash for given field name. */ 228 uint16_t nxt_unit_field_hash(const char* name, size_t name_length); 229 230 /* Split host for server name and port. */ 231 void nxt_unit_split_host(char *host_start, uint32_t host_length, 232 char **name, uint32_t *name_length, char **port, uint32_t *port_length); 233 234 /* Group duplicate fields for easy enumeration. */ 235 void nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req); 236 237 /* 238 * Allocate response structure capable to store limited numer of fields. 239 * The structure may be accessed directly via req->response pointer or 240 * filled step-by-step using functions add_field and add_content. 241 */ 242 int nxt_unit_response_init(nxt_unit_request_info_t *req, 243 uint16_t status, uint32_t max_fields_count, uint32_t max_fields_size); 244 245 int nxt_unit_response_realloc(nxt_unit_request_info_t *req, 246 uint32_t max_fields_count, uint32_t max_fields_size); 247 248 int nxt_unit_response_is_init(nxt_unit_request_info_t *req); 249 250 int nxt_unit_response_add_field(nxt_unit_request_info_t *req, 251 const char* name, uint8_t name_length, 252 const char* value, uint32_t value_length); 253 254 int nxt_unit_response_add_content(nxt_unit_request_info_t *req, 255 const void* src, uint32_t size); 256 257 /* 258 * Send prepared response to Unit server. Response structure destroyed during 259 * this call. 260 */ 261 int nxt_unit_response_send(nxt_unit_request_info_t *req); 262 263 int nxt_unit_response_is_sent(nxt_unit_request_info_t *req); 264 265 nxt_unit_buf_t *nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req, 266 uint32_t size); 267 268 int nxt_unit_request_is_websocket_handshake(nxt_unit_request_info_t *req); 269 270 int nxt_unit_response_upgrade(nxt_unit_request_info_t *req); 271 272 int nxt_unit_response_is_websocket(nxt_unit_request_info_t *req); 273 274 nxt_unit_request_info_t *nxt_unit_get_request_info_from_data(void *data); 275 276 int nxt_unit_buf_send(nxt_unit_buf_t *buf); 277 278 void nxt_unit_buf_free(nxt_unit_buf_t *buf); 279 280 nxt_unit_buf_t *nxt_unit_buf_next(nxt_unit_buf_t *buf); 281 282 uint32_t nxt_unit_buf_max(void); 283 284 uint32_t nxt_unit_buf_min(void); 285 286 int nxt_unit_response_write(nxt_unit_request_info_t *req, const void *start, 287 size_t size); 288 289 ssize_t nxt_unit_response_write_nb(nxt_unit_request_info_t *req, 290 const void *start, size_t size, size_t min_size); 291 292 int nxt_unit_response_write_cb(nxt_unit_request_info_t *req, 293 nxt_unit_read_info_t *read_info); 294 295 ssize_t nxt_unit_request_read(nxt_unit_request_info_t *req, void *dst, 296 size_t size); 297 298 ssize_t nxt_unit_request_readline_size(nxt_unit_request_info_t *req, 299 size_t max_size); 300 301 void nxt_unit_request_done(nxt_unit_request_info_t *req, int rc); 302 303 304 int nxt_unit_websocket_send(nxt_unit_request_info_t *req, uint8_t opcode, 305 uint8_t last, const void *start, size_t size); 306 307 int nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, 308 uint8_t last, const struct iovec *iov, int iovcnt); 309 310 ssize_t nxt_unit_websocket_read(nxt_unit_websocket_frame_t *ws, void *dst, 311 size_t size); 312 313 int nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws); 314 315 void nxt_unit_websocket_done(nxt_unit_websocket_frame_t *ws); 316 317 318 #if defined __has_attribute 319 320 #if __has_attribute(format) 321 322 #define NXT_ATTR_FORMAT __attribute__((format(printf, 3, 4))) 323 324 #endif 325 326 #endif 327 328 329 #if !defined(NXT_ATTR_FORMAT) 330 331 #define NXT_ATTR_FORMAT 332 333 #endif 334 335 336 void nxt_unit_log(nxt_unit_ctx_t *ctx, int level, const char* fmt, ...) 337 NXT_ATTR_FORMAT; 338 339 void nxt_unit_req_log(nxt_unit_request_info_t *req, int level, 340 const char* fmt, ...) NXT_ATTR_FORMAT; 341 342 #if (NXT_DEBUG) 343 344 #define nxt_unit_debug(ctx, fmt, ARGS...) \ 345 nxt_unit_log(ctx, NXT_UNIT_LOG_DEBUG, fmt, ##ARGS) 346 347 #define nxt_unit_req_debug(req, fmt, ARGS...) \ 348 nxt_unit_req_log(req, NXT_UNIT_LOG_DEBUG, fmt, ##ARGS) 349 350 #else 351 352 #define nxt_unit_debug(ctx, fmt, ARGS...) 353 354 #define nxt_unit_req_debug(req, fmt, ARGS...) 355 356 #endif 357 358 359 #define nxt_unit_warn(ctx, fmt, ARGS...) \ 360 nxt_unit_log(ctx, NXT_UNIT_LOG_WARN, fmt, ##ARGS) 361 362 #define nxt_unit_req_warn(req, fmt, ARGS...) \ 363 nxt_unit_req_log(req, NXT_UNIT_LOG_WARN, fmt, ##ARGS) 364 365 #define nxt_unit_error(ctx, fmt, ARGS...) \ 366 nxt_unit_log(ctx, NXT_UNIT_LOG_ERR, fmt, ##ARGS) 367 368 #define nxt_unit_req_error(req, fmt, ARGS...) \ 369 nxt_unit_req_log(req, NXT_UNIT_LOG_ERR, fmt, ##ARGS) 370 371 #define nxt_unit_alert(ctx, fmt, ARGS...) \ 372 nxt_unit_log(ctx, NXT_UNIT_LOG_ALERT, fmt, ##ARGS) 373 374 #define nxt_unit_req_alert(req, fmt, ARGS...) \ 375 nxt_unit_req_log(req, NXT_UNIT_LOG_ALERT, fmt, ##ARGS) 376 377 378 #endif /* _NXT_UNIT_H_INCLUDED_ */ 379