Skip to content

Commit f600c1e

Browse files
committed
lib: monkey: scheduler timeout improvement / updates on exception handling and general reports by Coverity tool
mk_server: server: fix unitialized variable reuse port mk_core: config: fix leak on split list mk_server: http_parser: fix method table index mk_server: http_parser: remove unnecessary continue() after parse_next() mk_core: rconf: do not over validate configuration context mk_server: plugin: validate plugin instance mk_server: plugin: close handler on exception mk_server: plugin: fix stage40 call assignation mk_server: http: validate return value of cork_flag func mk_server: rework connections timeout handling Signed-off-by: Eduardo Silva <eduardo@treasure-data.com>
1 parent 1f7c6e2 commit f600c1e

15 files changed

+174
-158
lines changed

lib/monkey/include/monkey/mk_http.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ struct mk_http_session
130130
/* head for mk_http_request list nodes, each request is linked here */
131131
struct mk_list request_list;
132132

133-
/* while a http_request don't complete a request, it's linked here */
134-
struct mk_list request_incomplete;
135-
136133
/* creation time for this HTTP session */
137134
time_t init_time;
138135

@@ -160,12 +157,12 @@ struct mk_http_session
160157
struct mk_http_parser parser;
161158
};
162159

163-
static inline void mk_http_status_completed(struct mk_http_session *cs)
160+
static inline void mk_http_status_completed(struct mk_http_session *cs,
161+
struct mk_sched_conn *conn)
164162
{
165163
mk_bug(cs->status == MK_REQUEST_STATUS_COMPLETED);
166-
167164
cs->status = MK_REQUEST_STATUS_COMPLETED;
168-
mk_list_del(&cs->request_incomplete);
165+
mk_list_del(&conn->timeout_head);
169166
}
170167

171168
int mk_http_error(int http_status, struct mk_http_session *cs,
@@ -197,7 +194,6 @@ int mk_http_handler_write(int socket, struct mk_http_session *cs);
197194
void mk_http_request_free(struct mk_http_request *sr);
198195
void mk_http_request_free_list(struct mk_http_session *cs);
199196

200-
void mk_http_request_ka_next(struct mk_http_session *cs);
201197
void mk_http_request_init(struct mk_http_session *session,
202198
struct mk_http_request *request);
203199
struct mk_http_header *mk_http_header_get(int name, struct mk_http_request *req,

lib/monkey/include/monkey/mk_http_parser.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ enum mk_request_methods {
8585
MK_METHOD_PUT ,
8686
MK_METHOD_DELETE ,
8787
MK_METHOD_OPTIONS ,
88-
MK_METHOD_UNKNOWN ,
89-
MK_METHOD_SIZEOF
88+
MK_METHOD_SIZEOF ,
89+
MK_METHOD_UNKNOWN
9090
};
9191

9292
/*

lib/monkey/include/monkey/mk_scheduler.h

+25-11
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#ifndef MK_SCHEDULER_H
2525
#define MK_SCHEDULER_H
2626

27-
#define MK_SCHEDULER_CONN_AVAILABLE -1
28-
#define MK_SCHEDULER_CONN_PENDING 0
29-
#define MK_SCHEDULER_CONN_PROCESS 1
30-
#define MK_SCHEDULER_SIGNAL_DEADBEEF 0xDEADBEEF
31-
#define MK_SCHEDULER_SIGNAL_FREE_ALL 0xFFEE0000
27+
#define MK_SCHED_CONN_TIMEOUT -1
28+
#define MK_SCHED_CONN_CLOSED -2
29+
30+
#define MK_SCHED_SIGNAL_DEADBEEF 0xDEADBEEF
31+
#define MK_SCHED_SIGNAL_FREE_ALL 0xFFEE0000
3232

3333
/*
3434
* Scheduler balancing mode:
@@ -67,12 +67,12 @@ struct mk_sched_worker
6767
struct rb_root rb_queue;
6868

6969
/*
70-
* The incoming queue represents client connections that
70+
* The timeout queue represents client connections that
7171
* have not initiated it requests or the request status
7272
* is incomplete. This linear lists allows the scheduler
7373
* to perform a fast check upon every timeout.
7474
*/
75-
struct mk_list incoming_queue;
75+
struct mk_list timeout_queue;
7676

7777
short int idx;
7878
unsigned char initialized;
@@ -104,7 +104,7 @@ struct mk_sched_conn
104104
struct mk_sched_handler *protocol; /* protocol handler */
105105
struct mk_plugin_network *net; /* I/O network layer */
106106
struct mk_channel channel; /* stream channel */
107-
struct mk_list status_queue; /* link to the incoming queue */
107+
struct mk_list timeout_head; /* link to the incoming queue */
108108
struct rb_node _rb_head; /* red-black tree head */
109109
};
110110

@@ -203,13 +203,15 @@ static inline struct mk_event_loop *mk_sched_loop()
203203
void mk_sched_update_thread_status(struct mk_sched_worker *sched,
204204
int active, int closed);
205205

206-
int mk_sched_drop_connection(int socket);
206+
int mk_sched_drop_connection(struct mk_sched_conn *conn,
207+
struct mk_sched_worker *sched);
208+
207209
int mk_sched_check_timeouts(struct mk_sched_worker *sched);
208210
struct mk_sched_conn *mk_sched_add_connection(int remote_fd,
209211
struct mk_server_listen *listener,
210212
struct mk_sched_worker *sched);
211-
int mk_sched_remove_client(struct mk_sched_worker *sched,
212-
struct mk_sched_conn *conn);
213+
int mk_sched_remove_client(struct mk_sched_conn *conn,
214+
struct mk_sched_worker *sched);
213215

214216
struct mk_sched_conn *mk_sched_get_connection(struct mk_sched_worker
215217
*sched, int remote_fd);
@@ -229,6 +231,18 @@ int mk_sched_event_close(struct mk_sched_conn *conn,
229231
struct mk_sched_worker *sched,
230232
int type);
231233

234+
static inline void mk_sched_conn_timeout_add(struct mk_sched_conn *conn,
235+
struct mk_sched_worker *sched)
236+
{
237+
mk_list_add(&conn->timeout_head, &sched->timeout_queue);
238+
}
239+
240+
static inline void mk_sched_conn_timeout_del(struct mk_sched_conn *conn)
241+
{
242+
mk_list_del(&conn->timeout_head);
243+
}
244+
245+
232246
#define mk_sched_conn_read(conn, buf, s) \
233247
conn->net->read(conn->event.fd, buf, s)
234248
#define mk_sched_conn_write(ch, buf, s) \

lib/monkey/include/monkey/monkey.h

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
extern const mk_ptr_t mk_monkey_protocol;
6464

6565
struct mk_server_config *mk_server_init();
66+
67+
void mk_server_info();
6668
int mk_server_setup();
6769
void mk_thread_keys_init();
6870
void mk_exit_all();

lib/monkey/mk_bin/monkey.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ int main(int argc, char **argv)
309309
mk_utils_register_pid(mk_config->pid_file_path);
310310

311311
/* Print server details */
312-
mk_details();
312+
mk_server_info();
313313

314314
/* Change process owner */
315315
mk_user_set_uidgid();

lib/monkey/mk_core/mk_rconf.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,10 @@ void mk_rconf_free(struct mk_rconf *conf)
252252
mk_mem_free(section->name);
253253
mk_mem_free(section);
254254
}
255-
if (conf->file) mk_mem_free(conf->file);
256-
if (conf) mk_mem_free(conf);
255+
if (conf->file) {
256+
mk_mem_free(conf->file);
257+
}
258+
mk_mem_free(conf);
257259
}
258260

259261
void mk_rconf_free_entries(struct mk_rconf_section *section)

lib/monkey/mk_server/mk_config.c

+10-46
Original file line numberDiff line numberDiff line change
@@ -122,49 +122,6 @@ void mk_config_free_all()
122122
mk_mem_free(mk_config);
123123
}
124124

125-
static void mk_details_listen(struct mk_list *listen)
126-
{
127-
128-
struct mk_list *head;
129-
struct mk_config_listener *l;
130-
131-
mk_list_foreach(head, listen) {
132-
l = mk_list_entry(head, struct mk_config_listener, _head);
133-
printf(MK_BANNER_ENTRY "Server listening on %s:%s\n",
134-
l->address, l->port);
135-
}
136-
}
137-
138-
void mk_details(void)
139-
{
140-
struct mk_list *head;
141-
struct mk_plugin *p;
142-
143-
printf(MK_BANNER_ENTRY "Process ID is %i\n", getpid());
144-
mk_details_listen(&mk_config->listeners);
145-
printf(MK_BANNER_ENTRY
146-
"%i threads, may handle up to %i client connections\n",
147-
mk_config->workers, mk_config->server_capacity);
148-
149-
/* List loaded plugins */
150-
printf(MK_BANNER_ENTRY "Loaded Plugins: ");
151-
mk_list_foreach(head, &mk_config->plugins) {
152-
p = mk_list_entry(head, struct mk_plugin, _head);
153-
printf("%s ", p->shortname);
154-
}
155-
printf("\n");
156-
157-
#ifdef __linux__
158-
char tmp[64];
159-
160-
if (mk_kernel_features_print(tmp, sizeof(tmp)) > 0) {
161-
printf(MK_BANNER_ENTRY "Linux Features: %s\n", tmp);
162-
}
163-
#endif
164-
165-
fflush(stdout);
166-
}
167-
168125
/* Print a specific error */
169126
static void mk_config_print_error_msg(char *variable, char *path)
170127
{
@@ -211,7 +168,7 @@ static int mk_config_listen_read(struct mk_rconf_section *section)
211168
char *address = NULL;
212169
char *port = NULL;
213170
char *divider;
214-
struct mk_list *list;
171+
struct mk_list *list = NULL;
215172
struct mk_list *cur;
216173
struct mk_string_line *listener;
217174
struct mk_rconf_entry *entry;
@@ -289,8 +246,15 @@ static int mk_config_listen_read(struct mk_rconf_section *section)
289246
}
290247

291248
error:
292-
if (address) mk_mem_free(address);
293-
if (port) mk_mem_free(port);
249+
if (address) {
250+
mk_mem_free(address);
251+
}
252+
if (port) {
253+
mk_mem_free(port);
254+
}
255+
if (list) {
256+
mk_string_split_free(list);
257+
}
294258

295259

296260
if (mk_list_is_empty(&mk_config->listeners) == 0) {

lib/monkey/mk_server/mk_http.c

+23-22
Original file line numberDiff line numberDiff line change
@@ -680,14 +680,18 @@ mk_ptr_t mk_http_index_file(char *pathfile, char *file_aux,
680680
#if defined (__linux__)
681681
static inline void mk_http_cb_file_on_consume(struct mk_stream *stream, long bytes)
682682
{
683+
int ret;
683684
(void) bytes;
684685

685686
/*
686687
* This callback is invoked just once as we want to turn off
687688
* the TCP Cork. We do this just overriding the callback for
688689
* the file stream.
689690
*/
690-
mk_server_cork_flag(stream->channel->fd, TCP_CORK_OFF);
691+
ret = mk_server_cork_flag(stream->channel->fd, TCP_CORK_OFF);
692+
if (ret == -1) {
693+
mk_warn("Could not set TCP_CORK/TCP_NOPUSH off");
694+
}
691695
stream->cb_bytes_consumed = NULL;
692696
}
693697
#endif
@@ -1043,6 +1047,19 @@ int mk_http_keepalive_check(struct mk_http_session *cs,
10431047
return 0;
10441048
}
10451049

1050+
static inline void mk_http_request_ka_next(struct mk_http_session *cs)
1051+
{
1052+
cs->body_length = 0;
1053+
cs->counter_connections++;
1054+
1055+
/* Update data for scheduler */
1056+
cs->init_time = log_current_utime;
1057+
cs->status = MK_REQUEST_STATUS_INCOMPLETE;
1058+
1059+
/* Initialize parser */
1060+
mk_http_parser_init(&cs->parser);
1061+
}
1062+
10461063
static inline int mk_http_request_end(struct mk_sched_conn *conn,
10471064
struct mk_sched_worker *sched)
10481065
{
@@ -1089,6 +1106,7 @@ static inline int mk_http_request_end(struct mk_sched_conn *conn,
10891106
else {
10901107
mk_http_request_free_list(cs);
10911108
mk_http_request_ka_next(cs);
1109+
mk_sched_conn_timeout_add(conn, sched);
10921110
return 0;
10931111
}
10941112

@@ -1257,9 +1275,6 @@ void mk_http_session_remove(struct mk_http_session *cs)
12571275
if (cs->body != cs->body_fixed) {
12581276
mk_mem_free(cs->body);
12591277
}
1260-
if (cs->status == MK_REQUEST_STATUS_INCOMPLETE) {
1261-
mk_list_del(&cs->request_incomplete);
1262-
}
12631278
mk_http_request_free_list(cs);
12641279
mk_list_del(&cs->request_list);
12651280

@@ -1296,7 +1311,6 @@ int mk_http_session_init(struct mk_http_session *cs, struct mk_sched_conn *conn)
12961311
cs->close_now = MK_FALSE;
12971312
cs->socket = conn->event.fd;
12981313
cs->status = MK_REQUEST_STATUS_INCOMPLETE;
1299-
mk_list_add(&cs->request_incomplete, cs_incomplete);
13001314

13011315
/* Map the channel, just for protocol-handler internal stuff */
13021316
cs->channel = &conn->channel;
@@ -1365,18 +1379,6 @@ void mk_http_request_free_list(struct mk_http_session *cs)
13651379
}
13661380
}
13671381

1368-
void mk_http_request_ka_next(struct mk_http_session *cs)
1369-
{
1370-
cs->body_length = 0;
1371-
cs->counter_connections++;
1372-
1373-
/* Update data for scheduler */
1374-
cs->init_time = log_current_utime;
1375-
cs->status = MK_REQUEST_STATUS_INCOMPLETE;
1376-
mk_list_add(&cs->request_incomplete, cs_incomplete);
1377-
mk_http_parser_init(&cs->parser);
1378-
}
1379-
13801382
/*
13811383
* Lookup a known header or a non-known header. For unknown headers
13821384
* set the 'key' value wth a lowercase string
@@ -1415,7 +1417,6 @@ struct mk_http_header *mk_http_header_get(int name, struct mk_http_request *req,
14151417
/*
14161418
* Main callbacks for the Scheduler
14171419
*/
1418-
14191420
int mk_http_sched_read(struct mk_sched_conn *conn,
14201421
struct mk_sched_worker *worker)
14211422
{
@@ -1453,7 +1454,7 @@ int mk_http_sched_read(struct mk_sched_conn *conn,
14531454
cs->body, cs->body_length);
14541455
if (status == MK_HTTP_PARSER_OK) {
14551456
MK_TRACE("[FD %i] HTTP_PARSER_OK", socket);
1456-
mk_http_status_completed(cs);
1457+
mk_http_status_completed(cs, conn);
14571458
mk_http_request_prepare(cs, sr);
14581459
}
14591460
else if (status == MK_HTTP_PARSER_ERROR) {
@@ -1474,20 +1475,20 @@ int mk_http_sched_read(struct mk_sched_conn *conn,
14741475
}
14751476

14761477
int mk_http_sched_close(struct mk_sched_conn *conn,
1477-
struct mk_sched_worker *worker,
1478+
struct mk_sched_worker *sched,
14781479
int type)
14791480
{
14801481
struct mk_http_session *cs;
1481-
(void) worker;
1482+
(void) sched;
14821483

14831484
#ifdef TRACE
14841485
MK_TRACE("[FD %i] HTTP sched close (type=%i)", conn->event.fd, type);
14851486
#else
14861487
(void) type;
14871488
#endif
1488-
14891489
cs = mk_http_session_get(conn);
14901490
mk_http_session_remove(cs);
1491+
14911492
return 0;
14921493
}
14931494

lib/monkey/mk_server/mk_http_parser.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static inline int str_searchr(char *buf, char c, int len)
9393
static inline int method_lookup(struct mk_http_request *req,
9494
struct mk_http_parser *p, char *buffer)
9595
{
96-
int i;
96+
int i = 0;
9797
int len;
9898

9999
/* Method lenght */
@@ -385,7 +385,6 @@ int mk_http_parser(struct mk_http_request *req, struct mk_http_parser *p,
385385
}
386386
request_set(&req->uri, p, buffer);
387387
parse_next();
388-
continue;
389388
}
390389
else if (buffer[i] == '?') {
391390
mark_end();

0 commit comments

Comments
 (0)