mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-10-04 16:08:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d4172a271 | ||
|
|
e842a181aa | ||
|
|
1ba0935579 | ||
|
|
920d1e5cfe | ||
|
|
473b4f1e57 | ||
|
|
b5c19bc1f8 | ||
|
|
79222adfe7 | ||
|
|
8b90efc567 | ||
|
|
4667eedc2b | ||
|
|
752252c9e4 | ||
|
|
6d91b7317d | ||
|
|
cddd1148fd | ||
|
|
c2096e8373 | ||
|
|
4f57ef33af | ||
|
|
aa34beade1 | ||
|
|
1149e60125 | ||
|
|
7def85636e | ||
|
|
d44692b268 | ||
|
|
46322b84b4 | ||
|
|
5785c61a13 | ||
|
|
594880fb46 | ||
|
|
4373306f78 | ||
|
|
f3e164b397 | ||
|
|
a60bb56b18 | ||
|
|
d08d08f39b | ||
|
|
4c4c2bb0fe | ||
|
|
cf79ce40e4 | ||
|
|
c153c26c18 | ||
|
|
82f5d1f988 | ||
|
|
5de83201ea | ||
|
|
6d69c01651 | ||
|
|
61f3d162a4 | ||
|
|
0a025ef93a | ||
|
|
f0951d3f41 | ||
|
|
f2e4eabf42 | ||
|
|
4fc99b3a3e | ||
|
|
1efb3f4096 | ||
|
|
9c29d6899f | ||
|
|
9a7524b55d | ||
|
|
04dfc3ae19 | ||
|
|
fc10411adb | ||
|
|
2378db4520 | ||
|
|
bdbd484db5 | ||
|
|
a2f6dd4ee2 | ||
|
|
ac001beb41 | ||
|
|
05895c40f7 | ||
|
|
fbf9a5b27c | ||
|
|
9c84e9f6cc | ||
|
|
486796bdf9 | ||
|
|
e795cd8912 | ||
|
|
643fc3019b | ||
|
|
f409e8b501 | ||
|
|
361ec8bcd4 | ||
|
|
ba3ac9ad09 | ||
|
|
419f1801cd | ||
|
|
d3c5ccc061 | ||
|
|
be6f61aa1b | ||
|
|
eb6c35aa95 | ||
|
|
797b14b23e | ||
|
|
44e7be13e2 | ||
|
|
18f6660603 | ||
|
|
c3f64af2df | ||
|
|
2278f98318 | ||
|
|
b4ee27b524 | ||
|
|
85ee422dcb | ||
|
|
15f70b2444 | ||
|
|
b7b126e89f | ||
|
|
56e284e18a | ||
|
|
0d4f4a2411 | ||
|
|
68049a5177 | ||
|
|
fc2355e5be | ||
|
|
49925e95d4 | ||
|
|
bee171e5da | ||
|
|
df809c8e34 | ||
|
|
3b64838324 | ||
|
|
f1f51948eb | ||
|
|
906da5cb9a | ||
|
|
dacad8d9b9 | ||
|
|
39fbea6d48 | ||
|
|
4796f85277 | ||
|
|
01ee66d1f6 | ||
|
|
6c61ac9c7e | ||
|
|
77c279b3a9 | ||
|
|
34430ceac4 | ||
|
|
fd1f79d154 | ||
|
|
56f7d09b2b | ||
|
|
479df7c2cc | ||
|
|
69c41026a4 | ||
|
|
7a305704f1 | ||
|
|
8d44dff97e | ||
|
|
c9324c06ca | ||
|
|
3446c2d322 | ||
|
|
6704d1a95a | ||
|
|
0e539f25cd | ||
|
|
00eb37f724 | ||
|
|
30c6ac8cb8 | ||
|
|
faa13228a5 | ||
|
|
e1941a49b8 | ||
|
|
d32f7b997d | ||
|
|
972333941d | ||
|
|
1691263744 | ||
|
|
debfe5b2b9 | ||
|
|
d2ce28f441 | ||
|
|
b9170b2c85 | ||
|
|
33a6195e44 | ||
|
|
933ef85cf2 | ||
|
|
0b27d41712 | ||
|
|
2124b930fd | ||
|
|
b5a2a89630 | ||
|
|
22bf9b33b0 | ||
|
|
b44c2208d9 | ||
|
|
44136aa572 | ||
|
|
5dd62ef909 | ||
|
|
5ea95c9e89 | ||
|
|
8095a9108c | ||
|
|
1cbc423afc | ||
|
|
4e260fa8a1 | ||
|
|
bb4be12fda | ||
|
|
672fb0eaa6 | ||
|
|
def274f28c | ||
|
|
be9f1578cc | ||
|
|
8d8f3bdc51 | ||
|
|
4d9720e4b5 | ||
|
|
6e2adf9cf3 | ||
|
|
7f004c5d6d | ||
|
|
13e7b53c54 | ||
|
|
1c89522519 | ||
|
|
3b4581a581 | ||
|
|
dfaa1d9313 | ||
|
|
795721b91e | ||
|
|
6bd0095513 |
@@ -1,441 +0,0 @@
|
||||
#include "../../uwsgi.h"
|
||||
#include "erlang.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
struct uwsgi_erlang uerl;
|
||||
|
||||
struct uwsgi_option erlang_options[] = {
|
||||
{"erlang", required_argument, 0, "spawn an erlang c-node", uwsgi_opt_set_str, &uerl.name, UWSGI_OPT_MASTER},
|
||||
{"erlang-cookie", required_argument, 0, "set erlang cookie", uwsgi_opt_set_str, &uerl.cookie, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
|
||||
void dump_eterm(ei_x_buff *x) {
|
||||
|
||||
int etype, esize, arity;
|
||||
long long num;
|
||||
char *atom;
|
||||
int i;
|
||||
char *binary;
|
||||
long bin_size;
|
||||
double fnum;
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
uwsgi_log("etype: %d/%c esize: %d\n", etype, etype, esize);
|
||||
|
||||
switch(etype) {
|
||||
case ERL_SMALL_INTEGER_EXT:
|
||||
case ERL_INTEGER_EXT:
|
||||
case ERL_SMALL_BIG_EXT:
|
||||
case ERL_LARGE_BIG_EXT:
|
||||
ei_decode_longlong(x->buff, &x->index, &num);
|
||||
uwsgi_log("num: %lu\n", num);
|
||||
break;
|
||||
case ERL_FLOAT_EXT:
|
||||
ei_decode_double(x->buff, &x->index, &fnum);
|
||||
uwsgi_log("float: %f\n", fnum);
|
||||
break;
|
||||
case ERL_STRING_EXT:
|
||||
atom = uwsgi_malloc(esize+1);
|
||||
ei_decode_string(x->buff, &x->index, atom);
|
||||
uwsgi_log("string: %s\n", atom);
|
||||
free(atom);
|
||||
break;
|
||||
case ERL_ATOM_EXT:
|
||||
atom = uwsgi_malloc(esize+1);
|
||||
ei_decode_atom(x->buff, &x->index, atom);
|
||||
uwsgi_log("atom: %s\n", atom);
|
||||
free(atom);
|
||||
break;
|
||||
case ERL_SMALL_TUPLE_EXT:
|
||||
case ERL_LARGE_TUPLE_EXT:
|
||||
ei_decode_tuple_header(x->buff, &x->index, &arity);
|
||||
for(i=0;i<arity;i++) {
|
||||
dump_eterm(x);
|
||||
}
|
||||
break;
|
||||
case ERL_LIST_EXT:
|
||||
case ERL_NIL_EXT:
|
||||
ei_decode_list_header(x->buff, &x->index, &arity);
|
||||
if (arity == 0) {
|
||||
uwsgi_log("nil value\n");
|
||||
break;
|
||||
}
|
||||
for(i=0;i<arity+1;i++) {
|
||||
dump_eterm(x);
|
||||
}
|
||||
break;
|
||||
case ERL_BINARY_EXT:
|
||||
binary = uwsgi_malloc(esize);
|
||||
ei_decode_binary(x->buff, &x->index, binary, &bin_size);
|
||||
uwsgi_log("binary data of %d bytes\n", bin_size);
|
||||
free(binary);
|
||||
break;
|
||||
default:
|
||||
uwsgi_log("ignored...\n");
|
||||
ei_skip_term(x->buff, &x->index);
|
||||
break;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_erlang_rpc(int fd, erlang_pid *from, ei_x_buff *x) {
|
||||
|
||||
int etype, esize;
|
||||
int arity;
|
||||
|
||||
char *gen_call;
|
||||
char *module;
|
||||
char *call;
|
||||
char buffer[0xffff];
|
||||
|
||||
char *argv[256] ;
|
||||
uint16_t argvs[256] ;
|
||||
int argc = 0;
|
||||
uint16_t ret;
|
||||
ei_x_buff xr;
|
||||
|
||||
erlang_ref eref;
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("%d %c %c %c\n", etype, etype, ERL_SMALL_TUPLE_EXT, ERL_LARGE_TUPLE_EXT);
|
||||
#endif
|
||||
if (etype != ERL_SMALL_TUPLE_EXT && etype != ERL_LARGE_TUPLE_EXT) return;
|
||||
|
||||
ei_decode_tuple_header(x->buff, &x->index, &arity);
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("rpc arity %d\n", arity);
|
||||
#endif
|
||||
if (arity != 3) return ;
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
if (etype != ERL_ATOM_EXT && etype != ERL_STRING_EXT) return ;
|
||||
|
||||
gen_call = uwsgi_malloc(esize);
|
||||
|
||||
if (etype == ERL_ATOM_EXT) {
|
||||
ei_decode_atom(x->buff, &x->index, gen_call);
|
||||
}
|
||||
else {
|
||||
ei_decode_string(x->buff, &x->index, gen_call);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("gen call = %s\n", gen_call);
|
||||
#endif
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
if (etype != ERL_SMALL_TUPLE_EXT) return ;
|
||||
|
||||
ei_decode_tuple_header(x->buff, &x->index, &arity);
|
||||
if (arity != 2) return ;
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
ei_skip_term(x->buff, &x->index);
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
ei_decode_ref(x->buff, &x->index, &eref);
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
module = uwsgi_malloc(esize);
|
||||
|
||||
if (etype == ERL_ATOM_EXT) {
|
||||
ei_decode_atom(x->buff, &x->index, module);
|
||||
}
|
||||
else {
|
||||
ei_decode_string(x->buff, &x->index, module);
|
||||
}
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
if (etype != ERL_SMALL_TUPLE_EXT) return ;
|
||||
|
||||
ei_decode_tuple_header(x->buff, &x->index, &arity);
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("arity: %d\n", arity);
|
||||
#endif
|
||||
if (arity != 5) return ;
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
char *method = uwsgi_malloc(esize);
|
||||
|
||||
if (etype == ERL_ATOM_EXT) {
|
||||
ei_decode_atom(x->buff, &x->index, method);
|
||||
}
|
||||
else {
|
||||
ei_decode_string(x->buff, &x->index, method);
|
||||
}
|
||||
|
||||
if (strcmp(method, "call")) return;
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
if (etype != ERL_ATOM_EXT && etype != ERL_STRING_EXT) return ;
|
||||
|
||||
module = uwsgi_malloc(esize);
|
||||
|
||||
if (etype == ERL_ATOM_EXT) {
|
||||
ei_decode_atom(x->buff, &x->index, module);
|
||||
}
|
||||
else {
|
||||
ei_decode_string(x->buff, &x->index, module);
|
||||
}
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
if (etype != ERL_ATOM_EXT && etype != ERL_STRING_EXT) return ;
|
||||
|
||||
call = uwsgi_malloc(esize);
|
||||
|
||||
if (etype == ERL_ATOM_EXT) {
|
||||
ei_decode_atom(x->buff, &x->index, call);
|
||||
}
|
||||
else {
|
||||
ei_decode_string(x->buff, &x->index, call);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("RPC %s %s\n", module, call);
|
||||
#endif
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
if (etype == ERL_ATOM_EXT) {
|
||||
argc = 1;
|
||||
argv[0] = uwsgi_malloc(esize+1);
|
||||
ei_decode_atom(x->buff, &x->index, argv[0]);
|
||||
argvs[1] = esize;
|
||||
}
|
||||
else if (etype == ERL_STRING_EXT) {
|
||||
argc = 1;
|
||||
argv[0] = uwsgi_malloc(esize+1);
|
||||
ei_decode_string(x->buff, &x->index, argv[0]);
|
||||
argvs[1] = esize;
|
||||
}
|
||||
|
||||
ret = uwsgi_rpc(call, argc, argv, argvs, buffer);
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("buffer: %.*s\n", ret, buffer);
|
||||
#endif
|
||||
|
||||
ei_x_new_with_version(&xr);
|
||||
|
||||
ei_x_encode_tuple_header(&xr, 2);
|
||||
//ei_x_encode_atom(&xr, "rex");
|
||||
ei_x_encode_ref(&xr, &eref);
|
||||
ei_x_encode_string_len(&xr, buffer, ret);
|
||||
|
||||
uwsgi_log("ei_send to %d %s %d %d %d: %d %d\n", fd, from->node, from->num , from->serial, from->creation, xr.index, ei_send(fd, from, xr.buff, xr.index));
|
||||
//uwsgi_log("ei_send to %d %s %d %d %d: %d %d\n", fd, from->node, from->num , from->serial, from->creation, xr.index, ei_reg_send(&uerl.cnode, fd, "rex", xr.buff, xr.index));
|
||||
|
||||
|
||||
}
|
||||
|
||||
void erlang_loop(int id, void *data) {
|
||||
|
||||
ErlConnect econn;
|
||||
//ErlMessage em;
|
||||
erlang_msg em;
|
||||
int fd;
|
||||
|
||||
int eversion;
|
||||
|
||||
ei_x_buff x, xr;
|
||||
|
||||
ei_x_new(&x);
|
||||
ei_x_new(&xr);
|
||||
|
||||
|
||||
/*
|
||||
int fd0 = ei_connect(&uerl.cnode, "anothernode@maverick64");
|
||||
uwsgi_log("fd0: %d\n", fd0);
|
||||
|
||||
ei_x_encode_list_header(&x, 0);
|
||||
|
||||
ei_rpc_to(&uerl.cnode, fd0, "erlang", "node", x.buff, x.index);
|
||||
|
||||
ei_rpc_from(&uerl.cnode, fd0, 10000, &em, &xr);
|
||||
|
||||
uwsgi_log("From: %s To: %s RegName: %s\n", em.from.node, em.to.node, em.toname);
|
||||
|
||||
xr.index = 0;
|
||||
ei_decode_version(xr.buff, &xr.index, &eversion);
|
||||
uwsgi_log("eversion: %d\n", eversion);
|
||||
|
||||
dump_eterm(&xr);
|
||||
*/
|
||||
|
||||
for(;;) {
|
||||
|
||||
fd = ei_accept(&uerl.cnode, uerl.fd, &econn);
|
||||
|
||||
if (fd >= 0) {
|
||||
|
||||
for (;;) {
|
||||
if (ei_xreceive_msg(fd, &em, &x) == ERL_MSG) {
|
||||
|
||||
if (em.msgtype == ERL_TICK)
|
||||
continue;
|
||||
|
||||
uwsgi_log("[erlang] message From: %s To (process): %s\n", em.from.node, em.toname);
|
||||
|
||||
|
||||
|
||||
x.index = 0;
|
||||
ei_decode_version(x.buff, &x.index, &eversion);
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("eversion: %d\n", eversion);
|
||||
#endif
|
||||
|
||||
if (!strcmp(em.toname, "rex")) {
|
||||
uwsgi_erlang_rpc(fd, &em.from, &x);
|
||||
}
|
||||
else {
|
||||
struct uwsgi_erlang_process *uep = uerl.uep;
|
||||
while(uep) {
|
||||
if (!strcmp(uep->name, em.toname)) {
|
||||
if (uep->plugin) {
|
||||
uep->plugin(uep->func, &x);
|
||||
}
|
||||
break;
|
||||
}
|
||||
uep = uep->next;
|
||||
}
|
||||
|
||||
if (!uep) {
|
||||
uwsgi_log("!!! unregistered erlang process requested, dumping it !!!\n");
|
||||
dump_eterm(&x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
if (em.msgtype) {
|
||||
dump_erl_obj(em.msg);
|
||||
erl_free_compound(em.msg);
|
||||
}
|
||||
if (em.to) {
|
||||
uwsgi_log("*** TO ***\n");
|
||||
dump_erl_obj(em.to);
|
||||
erl_free_compound(em.to);
|
||||
}
|
||||
|
||||
if (em.from) {
|
||||
uwsgi_log("*** FROM ***\n");
|
||||
dump_erl_obj(em.from);
|
||||
erl_free_compound(em.from);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int erlang_init() {
|
||||
|
||||
char *host;
|
||||
struct sockaddr_in sin;
|
||||
socklen_t slen = sizeof(struct sockaddr_in);
|
||||
char *ip = NULL;
|
||||
char *nodename;
|
||||
struct in_addr addr;
|
||||
|
||||
uerl.lock = uwsgi_lock_init("erlang");
|
||||
|
||||
if (uerl.name) {
|
||||
|
||||
|
||||
host = strchr(uerl.name, '@');
|
||||
|
||||
if (!host) {
|
||||
if (ei_connect_init(&uerl.cnode, uerl.name, uerl.cookie, 0) < 0) {
|
||||
uwsgi_log("unable to initialize erlang connection\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
nodename = uwsgi_concat2n(uerl.name, host-uerl.name, "",0);
|
||||
ip = uwsgi_resolve_ip(host+1);
|
||||
if (ip) {
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("ip: %s\n", ip);
|
||||
#endif
|
||||
addr.s_addr = inet_addr(ip);
|
||||
if (ei_connect_xinit(&uerl.cnode, host+1, nodename, uerl.name, &addr, uerl.cookie, 0) < 0) {
|
||||
uwsgi_log("unable to initialize erlang connection\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (ei_connect_init(&uerl.cnode, nodename, uerl.cookie, 0) < 0) {
|
||||
uwsgi_log("unable to initialize erlang connection\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
free(nodename);
|
||||
}
|
||||
|
||||
if (ip) {
|
||||
uerl.fd = bind_to_tcp(ip, uwsgi.listen_queue, NULL);
|
||||
}
|
||||
else {
|
||||
uerl.fd = bind_to_tcp("", uwsgi.listen_queue, NULL);
|
||||
}
|
||||
|
||||
if (uerl.fd < 0) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (getsockname(uerl.fd, (struct sockaddr *) &sin, &slen)) {
|
||||
uwsgi_error("getsockname()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (ei_publish(&uerl.cnode, ntohs(sin.sin_port)) < 0) {
|
||||
uwsgi_log( "*** unable to subscribe with EPMD ***\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uwsgi_log("Erlang C-Node %s registered on port %d\n", ei_thisnodename(&uerl.cnode), ntohs(sin.sin_port));
|
||||
|
||||
|
||||
if (register_gateway("uWSGI erlang c-node", erlang_loop, NULL) == NULL) {
|
||||
uwsgi_log("unable to register the erlang gateway\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct uwsgi_plugin erlang_plugin = {
|
||||
|
||||
.name = "erlang",
|
||||
.options = erlang_options,
|
||||
.init = erlang_init,
|
||||
};
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
#include <ei.h>
|
||||
|
||||
struct uwsgi_erlang_process {
|
||||
|
||||
char name[0xff];
|
||||
void (*plugin)(void *, ei_x_buff *);
|
||||
void *func;
|
||||
|
||||
struct uwsgi_erlang_process *next;
|
||||
};
|
||||
|
||||
struct uwsgi_erlang {
|
||||
|
||||
ei_cnode cnode;
|
||||
char *name;
|
||||
char *cookie;
|
||||
|
||||
int fd;
|
||||
|
||||
void *lock;
|
||||
|
||||
struct uwsgi_erlang_process *uep;
|
||||
};
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import os
|
||||
|
||||
NAME='erlang'
|
||||
|
||||
ERLANGPATH = os.environ.get('UWSGICONFIG_ERLANGPATH', 'erl')
|
||||
|
||||
includedir = os.popen(ERLANGPATH + " -noshell -noinput -eval \"io:format('~s~n', [code:lib_dir(erl_interface, include)])\" -s erlang halt").read().rstrip()
|
||||
libpath = os.popen(ERLANGPATH + " -noshell -noinput -eval \"io:format('~s~n', [code:lib_dir(erl_interface, lib)])\" -s erlang halt").read().rstrip()
|
||||
|
||||
CFLAGS = [ '-I' + includedir ]
|
||||
LDFLAGS = [ '-L' + libpath ]
|
||||
|
||||
LIBS = ['-lei']
|
||||
|
||||
GCC_LIST = ['erlang']
|
||||
@@ -1,116 +0,0 @@
|
||||
#include <uwsgi.h>
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
void (*uwsgi_go_helper_post_fork_c)();
|
||||
void (*uwsgi_go_helper_post_init_c)();
|
||||
void * (*uwsgi_go_helper_env_new_c)(struct wsgi_request *);
|
||||
void (*uwsgi_go_helper_env_add_c)(void *, char *, int, char *, int);
|
||||
void (*uwsgi_go_helper_request_c)(void *, struct wsgi_request *);
|
||||
int (*uwsgi_go_helper_signal_handler_c)(int, void *);
|
||||
void (*uwsgi_go_helper_run_core_c)(int);
|
||||
char *(*uwsgi_go_helper_version_c)();
|
||||
|
||||
void uwsgi_go_post_fork() {
|
||||
uwsgi_go_helper_post_fork_c();
|
||||
}
|
||||
|
||||
void uwsgi_opt_setup_goroutines(char *opt, char *value, void *foobar) {
|
||||
// set async mode
|
||||
uwsgi_opt_set_int(opt, value, &uwsgi.async);
|
||||
// set loop engine
|
||||
uwsgi.loop = "goroutines";
|
||||
}
|
||||
|
||||
struct uwsgi_option uwsgi_go_options[] = {
|
||||
{"goroutines", required_argument, 0, "a shortcut setting optimal options for goroutine-based apps, takes the number of goroutines to spawn as argument", uwsgi_opt_setup_goroutines, NULL, UWSGI_OPT_THREADS},
|
||||
{0, 0, 0, 0, 0, 0, 0},
|
||||
|
||||
};
|
||||
|
||||
#define uwsgi_go_get_symbol(x) x ## _c = dlsym(RTLD_DEFAULT, #x);\
|
||||
if (!x ## _c) {\
|
||||
uwsgi_log("[uwsgi-go] unable to load " #x " function\n"); exit(1);\
|
||||
}
|
||||
|
||||
static int uwsgi_go_init() {
|
||||
|
||||
// build the functions table
|
||||
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_post_fork)
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_post_init)
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_env_new)
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_env_add)
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_request)
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_signal_handler)
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_run_core)
|
||||
uwsgi_go_get_symbol(uwsgi_go_helper_version)
|
||||
|
||||
uwsgi_log("Go version \"%s\" initialized\n", uwsgi_go_helper_version_c());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_go_request(struct wsgi_request *wsgi_req) {
|
||||
/* Standard GO request */
|
||||
if (!wsgi_req->uh->pktsize) {
|
||||
uwsgi_log("Empty GO request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uwsgi_parse_vars(wsgi_req)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
wsgi_req->async_environ = uwsgi_go_helper_env_new_c(wsgi_req);
|
||||
int i;
|
||||
for(i=0;i<wsgi_req->var_cnt;i++) {
|
||||
uwsgi_go_helper_env_add_c(wsgi_req->async_environ, wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len,
|
||||
wsgi_req->hvec[i+1].iov_base, wsgi_req->hvec[i+1].iov_len);
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
uwsgi_go_helper_request_c(wsgi_req->async_environ, wsgi_req);
|
||||
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
static void uwsgi_go_after_request(struct wsgi_request *wsgi_req) {
|
||||
log_request(wsgi_req);
|
||||
}
|
||||
|
||||
static int uwsgi_go_signal_handler(uint8_t signum, void *handler) {
|
||||
return uwsgi_go_helper_signal_handler_c((int)signum, handler);
|
||||
}
|
||||
|
||||
static void goroutines_loop() {
|
||||
int i;
|
||||
for (i = 1; i < uwsgi.async; i++) {
|
||||
uwsgi_go_helper_run_core_c(i);
|
||||
}
|
||||
simple_loop_run_int(0);
|
||||
}
|
||||
|
||||
static void uwsgi_go_on_load() {
|
||||
uwsgi_register_loop("goroutines", goroutines_loop);
|
||||
}
|
||||
|
||||
static void uwsgi_go_call_init_hook() {
|
||||
if (uwsgi_go_helper_post_init_c) {
|
||||
uwsgi_go_helper_post_init_c();
|
||||
}
|
||||
}
|
||||
|
||||
struct uwsgi_plugin go_plugin = {
|
||||
.name = "go",
|
||||
.modifier1 = 11,
|
||||
.request = uwsgi_go_request,
|
||||
.after_request = uwsgi_go_after_request,
|
||||
.preinit_apps = uwsgi_go_call_init_hook,
|
||||
.post_fork = uwsgi_go_post_fork,
|
||||
.init = uwsgi_go_init,
|
||||
.signal_handler = uwsgi_go_signal_handler,
|
||||
.on_load = uwsgi_go_on_load,
|
||||
.options = uwsgi_go_options,
|
||||
};
|
||||
@@ -1,420 +0,0 @@
|
||||
/*
|
||||
uWSGI go integration package
|
||||
*/
|
||||
|
||||
package uwsgi
|
||||
|
||||
/*
|
||||
#include <uwsgi.h>
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
// commodity functions to simulate argc/argv
|
||||
|
||||
static char ** uwsgi_go_helper_create_argv(int len) {
|
||||
return uwsgi_calloc(sizeof(char *) * len);
|
||||
}
|
||||
|
||||
static void uwsgi_go_helper_set_argv(char **argv, int pos, char *item) {
|
||||
argv[pos] = item;
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
"net/http/cgi"
|
||||
"unsafe"
|
||||
"strings"
|
||||
"strconv"
|
||||
"io"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// this stores the modifier used by the go plugin (default 11)
|
||||
var uwsgi_modifier1 int = -1;
|
||||
// the following to objects are used to implement a sort of GC to avoid request environ and
|
||||
// signal handlers to be garbage collected
|
||||
var uwsgi_env_gc = make(map[*C.struct_wsgi_request](*map[string]string))
|
||||
var uwsgi_signals_gc = make([]*func(int), 256)
|
||||
|
||||
var uwsgi_default_request_handler func(http.ResponseWriter, *http.Request) = nil
|
||||
var uwsgi_default_handler http.Handler = nil
|
||||
var uwsgi_post_fork_hook func() = nil
|
||||
var uwsgi_post_init_hook func() = nil
|
||||
|
||||
|
||||
/*
|
||||
|
||||
uWSGI api functions
|
||||
|
||||
*/
|
||||
|
||||
// raise a uWSGI signal
|
||||
func Signal(signum int) {
|
||||
if C.uwsgi.master_process == 0 {
|
||||
return
|
||||
}
|
||||
C.uwsgi_signal_send(C.uwsgi.signal_socket, C.uint8_t(signum))
|
||||
}
|
||||
|
||||
// set a user lock
|
||||
func Lock(num int) {
|
||||
C.uwsgi_user_lock(C.int(num));
|
||||
}
|
||||
|
||||
// unset a user lock
|
||||
func Unlock(num int) {
|
||||
C.uwsgi_user_unlock(C.int(num));
|
||||
}
|
||||
|
||||
// add a timer
|
||||
func AddTimer(signum int, seconds int) bool {
|
||||
if C.uwsgi.master_process == 0 {
|
||||
return false
|
||||
}
|
||||
if int(C.uwsgi_add_timer(C.uint8_t(signum), C.int(seconds))) == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// add a red black timer
|
||||
func AddRbTimer(signum int, seconds int) bool {
|
||||
if C.uwsgi.master_process == 0 {
|
||||
return false
|
||||
}
|
||||
if int(C.uwsgi_signal_add_rb_timer(C.uint8_t(signum), C.int(seconds), C.int(0))) == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// check if a signal is registered
|
||||
func SignalRegistered(signum int) bool {
|
||||
if C.uwsgi.master_process == 0 {
|
||||
return false
|
||||
}
|
||||
if int(C.uwsgi_signal_registered(C.uint8_t(signum))) == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// register a signal
|
||||
func RegisterSignal(signum int, who string, handler func(int)) bool {
|
||||
if C.uwsgi.master_process == 0 {
|
||||
return false
|
||||
}
|
||||
if uwsgi_modifier1 == -1 {
|
||||
c_go := C.CString("go")
|
||||
defer C.free(unsafe.Pointer(c_go))
|
||||
uwsgi_modifier1 = int(C.uwsgi_plugin_modifier1(c_go))
|
||||
if uwsgi_modifier1 == -1 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
c_who := C.CString(who)
|
||||
defer C.free(unsafe.Pointer(c_who))
|
||||
if int(C.uwsgi_register_signal(C.uint8_t(signum), c_who, unsafe.Pointer(&handler), C.uint8_t(uwsgi_modifier1))) == 0 {
|
||||
uwsgi_signals_gc[signum] = &handler
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Alarm(alarm string, msg string) {
|
||||
a := C.CString(alarm)
|
||||
defer C.free(unsafe.Pointer(a))
|
||||
m := C.CString(msg)
|
||||
defer C.free(unsafe.Pointer(m))
|
||||
ml := len(msg)
|
||||
C.uwsgi_alarm_trigger(a, m, C.size_t(ml))
|
||||
}
|
||||
|
||||
// get an item from the cache
|
||||
func CacheGet(key string, cache string) []byte {
|
||||
|
||||
k := C.CString(key)
|
||||
defer C.free(unsafe.Pointer(k))
|
||||
kl := len(key)
|
||||
var vl C.uint64_t = C.uint64_t(0)
|
||||
|
||||
|
||||
c := (*C.char)(nil)
|
||||
if len(cache) > 0 {
|
||||
c = C.CString(cache)
|
||||
defer C.free(unsafe.Pointer(c))
|
||||
}
|
||||
|
||||
var p []byte
|
||||
|
||||
c_value := C.uwsgi_cache_magic_get(k, C.uint16_t(kl), &vl, (*C.uint64_t)(nil), c)
|
||||
|
||||
if c_value != nil {
|
||||
p = C.GoBytes((unsafe.Pointer)(c_value), C.int(vl))
|
||||
} else {
|
||||
p = nil
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// remove an intem from the cache
|
||||
func CacheDel(key string, cache string) bool {
|
||||
|
||||
k := C.CString(key)
|
||||
defer C.free(unsafe.Pointer(k))
|
||||
kl := len(key)
|
||||
|
||||
c := (*C.char)(nil)
|
||||
if len(cache) > 0 {
|
||||
c = C.CString(cache)
|
||||
defer C.free(unsafe.Pointer(c))
|
||||
}
|
||||
|
||||
if int(C.uwsgi_cache_magic_del(k, C.uint16_t(kl), c)) < 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// check if an item exists in the cache
|
||||
func CacheExists(key string, cache string) bool {
|
||||
k := C.CString(key)
|
||||
defer C.free(unsafe.Pointer(k))
|
||||
kl := len(key)
|
||||
|
||||
c := (*C.char)(nil)
|
||||
if len(cache) > 0 {
|
||||
c = C.CString(cache)
|
||||
defer C.free(unsafe.Pointer(c))
|
||||
}
|
||||
|
||||
if int(C.uwsgi_cache_magic_exists(k, C.uint16_t(kl), c)) == 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// put an item in the cache
|
||||
func CacheSetFlags(key string, p []byte, expires uint64, flags int, cache string) bool {
|
||||
|
||||
k := C.CString(key)
|
||||
defer C.free(unsafe.Pointer(k))
|
||||
kl := len(key)
|
||||
v := unsafe.Pointer(&p[0])
|
||||
vl := len(p)
|
||||
|
||||
c := (*C.char)(nil)
|
||||
if len(cache) > 0 {
|
||||
c = C.CString(cache)
|
||||
defer C.free(unsafe.Pointer(c))
|
||||
}
|
||||
|
||||
if int(C.uwsgi_cache_magic_set(k, C.uint16_t(kl), (*C.char)(v), C.uint64_t(vl), C.uint64_t(expires), C.uint64_t(flags), c)) < 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func CacheSet(key string, p []byte, expires uint64, cache string) bool {
|
||||
return CacheSetFlags(key, p, expires, 0, cache);
|
||||
}
|
||||
|
||||
func CacheUpdate(key string, p []byte, expires uint64, cache string) bool {
|
||||
return CacheSetFlags(key, p, expires, 2, cache);
|
||||
}
|
||||
|
||||
// get the current worker id
|
||||
func WorkerId() int {
|
||||
return int(C.uwsgi.mywid)
|
||||
}
|
||||
|
||||
// get the current mule id
|
||||
func MuleId() int {
|
||||
return int(C.uwsgi.muleid)
|
||||
}
|
||||
|
||||
// get the current logsize (if available)
|
||||
func LogSize() int64 {
|
||||
return int64(C.uwsgi.shared.logsize)
|
||||
}
|
||||
|
||||
func PostFork(hook func()) {
|
||||
uwsgi_post_fork_hook = hook
|
||||
}
|
||||
|
||||
func PostInit(hook func()) {
|
||||
uwsgi_post_init_hook = hook
|
||||
}
|
||||
|
||||
func RequestHandler(hook func(http.ResponseWriter, *http.Request)) {
|
||||
uwsgi_default_request_handler = hook
|
||||
}
|
||||
|
||||
func Handler(handler http.Handler) {
|
||||
uwsgi_default_handler = handler
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
C -> go and go -> C bridges
|
||||
|
||||
*/
|
||||
|
||||
//export uwsgi_go_helper_post_fork
|
||||
func uwsgi_go_helper_post_fork() {
|
||||
if uwsgi_post_fork_hook != nil {
|
||||
uwsgi_post_fork_hook()
|
||||
}
|
||||
}
|
||||
|
||||
//export uwsgi_go_helper_post_init
|
||||
func uwsgi_go_helper_post_init() {
|
||||
if uwsgi_post_init_hook != nil {
|
||||
uwsgi_post_init_hook()
|
||||
}
|
||||
}
|
||||
|
||||
//export uwsgi_go_helper_env_new
|
||||
func uwsgi_go_helper_env_new(wsgi_req *C.struct_wsgi_request) *map[string]string {
|
||||
var env map[string]string
|
||||
env = make(map[string]string)
|
||||
// track env to avoid it being garbage collected...
|
||||
uwsgi_env_gc[wsgi_req] = &env
|
||||
return &env
|
||||
}
|
||||
|
||||
//export uwsgi_go_helper_env_add
|
||||
func uwsgi_go_helper_env_add(env *map[string]string, k *C.char, kl C.int, v *C.char, vl C.int) {
|
||||
var mk string = C.GoStringN(k, kl)
|
||||
var mv string = C.GoStringN(v, vl)
|
||||
(*env)[mk] = mv
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
http.* implementations
|
||||
|
||||
*/
|
||||
|
||||
type ResponseWriter struct {
|
||||
r *http.Request
|
||||
wsgi_req *C.struct_wsgi_request
|
||||
headers http.Header
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (w *ResponseWriter) Write(p []byte) (n int, err error) {
|
||||
if !w.wroteHeader {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
m := len(p)
|
||||
C.uwsgi_response_write_body_do(w.wsgi_req, (*C.char)(unsafe.Pointer(&p[0])), C.size_t(m))
|
||||
return m+n, err
|
||||
}
|
||||
|
||||
// TODO fix it !!!
|
||||
func (w *ResponseWriter) WriteHeader(status int) {
|
||||
codestring := http.StatusText(status)
|
||||
var tmp_buf string = strconv.Itoa(status) + " " + codestring
|
||||
c_status := C.CString(tmp_buf)
|
||||
defer C.free(unsafe.Pointer(c_status))
|
||||
C.uwsgi_response_prepare_headers(w.wsgi_req, c_status, C.uint16_t(len(tmp_buf)) )
|
||||
if w.headers.Get("Content-Type") == "" {
|
||||
w.headers.Set("Content-Type", "text/html; charset=utf-8")
|
||||
}
|
||||
for k := range w.headers {
|
||||
hk_c := C.CString(k)
|
||||
defer C.free(unsafe.Pointer(hk_c))
|
||||
for _, v := range w.headers[k] {
|
||||
v = strings.NewReplacer("\n", " ", "\r", " ").Replace(v)
|
||||
v = strings.TrimSpace(v)
|
||||
hv_c := C.CString(v)
|
||||
defer C.free(unsafe.Pointer(hv_c))
|
||||
C.uwsgi_response_add_header(w.wsgi_req, hk_c, C.uint16_t(len(k)), hv_c, C.uint16_t(len(v)))
|
||||
}
|
||||
}
|
||||
w.wroteHeader = true
|
||||
}
|
||||
|
||||
func (w *ResponseWriter) Header() http.Header {
|
||||
return w.headers
|
||||
}
|
||||
|
||||
|
||||
type BodyReader struct {
|
||||
wsgi_req *C.struct_wsgi_request
|
||||
}
|
||||
|
||||
// there is no close in request body
|
||||
func (br *BodyReader) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (br *BodyReader) Read(p []byte) (n int, err error) {
|
||||
m := len(p)
|
||||
var rlen C.ssize_t = C.ssize_t(0)
|
||||
c_body := C.uwsgi_request_body_read(br.wsgi_req, C.ssize_t(m), &rlen)
|
||||
if (c_body == C.uwsgi.empty) {
|
||||
err = io.EOF;
|
||||
return 0, err
|
||||
} else if (c_body != nil) {
|
||||
C.memcpy(unsafe.Pointer(&p[0]), unsafe.Pointer(c_body), C.size_t(rlen))
|
||||
return int(rlen), err
|
||||
}
|
||||
err = io.ErrUnexpectedEOF
|
||||
rlen = 0
|
||||
return int(rlen), err
|
||||
}
|
||||
|
||||
//export uwsgi_go_helper_request
|
||||
func uwsgi_go_helper_request(env *map[string]string, wsgi_req *C.struct_wsgi_request) {
|
||||
httpReq, err := cgi.RequestFromMap(*env)
|
||||
if err != nil {
|
||||
} else {
|
||||
httpReq.Body = &BodyReader{wsgi_req}
|
||||
w := ResponseWriter{httpReq, wsgi_req,http.Header{},false}
|
||||
if uwsgi_default_request_handler != nil {
|
||||
uwsgi_default_request_handler(&w, httpReq)
|
||||
} else if uwsgi_default_handler != nil {
|
||||
uwsgi_default_handler.ServeHTTP(&w, httpReq)
|
||||
} else {
|
||||
http.DefaultServeMux.ServeHTTP(&w, httpReq)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//export uwsgi_go_helper_version
|
||||
func uwsgi_go_helper_version() *C.char {
|
||||
return C.CString(runtime.Version())
|
||||
}
|
||||
|
||||
//export uwsgi_go_helper_signal_handler
|
||||
func uwsgi_go_helper_signal_handler(signum int, handler *func(int)) int {
|
||||
(*handler)(signum)
|
||||
return 0;
|
||||
}
|
||||
|
||||
//export uwsgi_go_helper_run_core
|
||||
func uwsgi_go_helper_run_core(core_id int) {
|
||||
go C.simple_loop_run_int(C.int(core_id))
|
||||
}
|
||||
|
||||
/*
|
||||
the main function, running the uWSGI server via libuwsgi.so
|
||||
*/
|
||||
func Run() {
|
||||
argc := len(os.Args) + 1
|
||||
argv := C.uwsgi_go_helper_create_argv(C.int(argc))
|
||||
for i, s := range os.Args {
|
||||
C.uwsgi_go_helper_set_argv(argv, C.int(i), C.CString(s))
|
||||
}
|
||||
C.uwsgi_init(C.int(argc-1), argv, nil)
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import os
|
||||
NAME='go'
|
||||
|
||||
CFLAGS = []
|
||||
LDFLAGS = []
|
||||
LIBS = []
|
||||
GCC_LIST = ['go_plugin']
|
||||
|
||||
def post_build(config):
|
||||
os.environ['GOPATH'] = 'plugins/go'
|
||||
cflags = ['-I'+os.getcwd()]
|
||||
for c in config.cflags:
|
||||
if not c.startswith('-DUWSGI_BUILD_DATE') and not c.startswith('-DUWSGI_CFLAGS'):
|
||||
cflags.append(c)
|
||||
|
||||
cflags.append('-Wno-implicit-function-declaration')
|
||||
cflags.append('-Wno-implicit-int')
|
||||
cflags.append('-Wno-unused-function')
|
||||
cflags.append('-Wno-unused-variable')
|
||||
|
||||
os.environ['CGO_CFLAGS'] = ' '.join(cflags).replace('\\"', '')
|
||||
base = os.path.dirname(config.get('bin_name'))
|
||||
if not base:
|
||||
base = "."
|
||||
os.environ['CGO_LDFLAGS'] = '-L' + base + ' -L'+os.getcwd() + ' -luwsgi'
|
||||
if os.system("go install uwsgi") != 0:
|
||||
os._exit(1)
|
||||
print("*** uwsgi go module available in $GOPATH %s/plugins/go ***" % os.getcwd())
|
||||
@@ -1,543 +0,0 @@
|
||||
#include "../erlang/erlang.h"
|
||||
#include "../python/uwsgi_python.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
extern struct uwsgi_erlang uerl;
|
||||
extern struct uwsgi_python up;
|
||||
|
||||
ei_cnode *pyerl_cnode;
|
||||
|
||||
PyObject *pyerl_close(PyObject * self, PyObject * args) {
|
||||
|
||||
int fd;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i:erlang_close", &fd)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *pyerl_connect(PyObject * self, PyObject * args) {
|
||||
|
||||
char *node = NULL;
|
||||
int fd;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s:erlang_connect", &node)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fd = ei_connect(pyerl_cnode, node);
|
||||
|
||||
if (fd < 0) {
|
||||
return PyErr_Format(PyExc_ValueError, "Unable to connect to erlang node");
|
||||
}
|
||||
|
||||
return PyInt_FromLong(fd);
|
||||
|
||||
}
|
||||
|
||||
int py_to_erl(PyObject *, ei_x_buff*);
|
||||
|
||||
PyObject *erl_to_py(ei_x_buff* x) {
|
||||
|
||||
int etype, esize, arity;
|
||||
long long num;
|
||||
double fnum;
|
||||
char *atom, *binary;
|
||||
long bin_size;
|
||||
PyObject *pobj;
|
||||
PyObject *zero;
|
||||
erlang_pid epid;
|
||||
int i;
|
||||
|
||||
ei_get_type(x->buff, &x->index, &etype, &esize);
|
||||
|
||||
switch(etype) {
|
||||
case ERL_SMALL_INTEGER_EXT:
|
||||
case ERL_INTEGER_EXT:
|
||||
case ERL_SMALL_BIG_EXT:
|
||||
case ERL_LARGE_BIG_EXT:
|
||||
ei_decode_longlong(x->buff, &x->index, &num);
|
||||
return PyLong_FromLong(num);
|
||||
case ERL_FLOAT_EXT:
|
||||
ei_decode_double(x->buff, &x->index, &fnum);
|
||||
return PyFloat_FromDouble(fnum);
|
||||
case ERL_STRING_EXT:
|
||||
atom = uwsgi_malloc(esize+1);
|
||||
ei_decode_string(x->buff, &x->index, atom);
|
||||
pobj = PyString_FromString(atom);
|
||||
free(atom);
|
||||
Py_INCREF(pobj);
|
||||
return pobj;
|
||||
case ERL_ATOM_EXT:
|
||||
atom = uwsgi_malloc(esize+1);
|
||||
ei_decode_atom(x->buff, &x->index, atom);
|
||||
#ifndef PyUnicode_FromString
|
||||
zero = PyString_FromString(atom);
|
||||
pobj = PyUnicode_FromObject(zero);
|
||||
Py_DECREF(zero);
|
||||
#else
|
||||
pobj = PyUnicode_FromString(atom);
|
||||
#endif
|
||||
free(atom);
|
||||
Py_INCREF(pobj);
|
||||
return pobj;
|
||||
case ERL_SMALL_TUPLE_EXT:
|
||||
case ERL_LARGE_TUPLE_EXT:
|
||||
ei_decode_tuple_header(x->buff, &x->index, &arity);
|
||||
pobj = PyTuple_New(arity);
|
||||
for(i=0;i<arity;i++) {
|
||||
zero = erl_to_py(x);
|
||||
PyTuple_SetItem(pobj, i, zero);
|
||||
Py_DECREF(zero);
|
||||
}
|
||||
Py_INCREF(pobj);
|
||||
return pobj;
|
||||
case ERL_LIST_EXT:
|
||||
case ERL_NIL_EXT:
|
||||
ei_decode_list_header(x->buff, &x->index, &arity);
|
||||
if (!arity) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
pobj = PyList_New(0);
|
||||
for(i=0;i<arity+1;i++) {
|
||||
zero = erl_to_py(x);
|
||||
PyList_Append(pobj, zero);
|
||||
Py_DECREF(zero);
|
||||
}
|
||||
Py_INCREF(pobj);
|
||||
return pobj;
|
||||
case ERL_BINARY_EXT:
|
||||
binary = uwsgi_malloc(esize);
|
||||
ei_decode_binary(x->buff, &x->index, binary, &bin_size);
|
||||
pobj = PyString_FromStringAndSize(binary, bin_size);
|
||||
free(binary);
|
||||
Py_INCREF(pobj);
|
||||
return pobj;
|
||||
case ERL_PID_EXT:
|
||||
ei_decode_pid(x->buff, &x->index, &epid);
|
||||
pobj = PyTuple_New(3);
|
||||
PyTuple_SetItem(pobj, 0, PyInt_FromLong(epid.num));
|
||||
PyTuple_SetItem(pobj, 1, PyInt_FromLong(epid.serial));
|
||||
PyTuple_SetItem(pobj, 2, PyInt_FromLong(epid.creation));
|
||||
Py_INCREF(pobj);
|
||||
return pobj;
|
||||
default:
|
||||
ei_skip_term(x->buff, &x->index);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
||||
}
|
||||
|
||||
PyObject *pyerl_lock(PyObject * self, PyObject * args) {
|
||||
|
||||
uwsgi_lock(uerl.lock);
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *pyerl_unlock(PyObject * self, PyObject * args) {
|
||||
|
||||
uwsgi_unlock(uerl.lock);
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *pyerl_send(PyObject * self, PyObject * args) {
|
||||
PyObject *node;
|
||||
PyObject *reg;
|
||||
char *cnode;
|
||||
PyObject *pobj;
|
||||
ei_x_buff x;
|
||||
int fd;
|
||||
int close_fd = 0;
|
||||
erlang_pid epid;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OOO:erlang_send", &node, ®, &pobj)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (PyString_Check(node)) {
|
||||
cnode = PyString_AsString(node);
|
||||
fd = ei_connect(pyerl_cnode, cnode);
|
||||
close_fd = 1;
|
||||
}
|
||||
else if (PyInt_Check(node)) {
|
||||
fd = PyInt_AsLong(node);
|
||||
}
|
||||
else {
|
||||
return PyErr_Format(PyExc_ValueError, "invalid erlang node/descriptor");
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
return PyErr_Format(PyExc_ValueError, "Unable to connect to erlang node");
|
||||
}
|
||||
|
||||
|
||||
ei_x_new_with_version(&x);
|
||||
|
||||
if (py_to_erl(pobj, &x) < 0) {
|
||||
ei_x_free(&x);
|
||||
if (close_fd) close(fd);
|
||||
return PyErr_Format(PyExc_ValueError, "Unsupported object in Python->Erlang translation");
|
||||
}
|
||||
|
||||
|
||||
if (PyTuple_Check(reg) && PyTuple_Size(reg) == 3) {
|
||||
epid.num = PyInt_AsLong( PyTuple_GetItem(reg, 0) );
|
||||
epid.serial = PyInt_AsLong( PyTuple_GetItem(reg, 1) );
|
||||
epid.creation = PyInt_AsLong( PyTuple_GetItem(reg, 2) );
|
||||
ei_send(fd, &epid, x.buff, x.index);
|
||||
}
|
||||
else if (PyString_Check(reg)) {
|
||||
ei_reg_send(pyerl_cnode, fd, PyString_AsString(reg), x.buff, x.index);
|
||||
}
|
||||
else {
|
||||
ei_x_free(&x);
|
||||
if (close_fd) close(fd);
|
||||
return PyErr_Format(PyExc_ValueError, "Invalid Erlang process");
|
||||
}
|
||||
|
||||
return PyInt_FromLong(fd);
|
||||
}
|
||||
|
||||
PyObject *pyerl_sr(PyObject * self, PyObject * args) {
|
||||
|
||||
PyObject *node;
|
||||
PyObject *reg;
|
||||
char *cnode;
|
||||
PyObject *pobj;
|
||||
ei_x_buff x;
|
||||
int fd;
|
||||
int close_fd = 0;
|
||||
erlang_msg em;
|
||||
erlang_pid epid;
|
||||
int eversion;
|
||||
PyObject *res;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OOO:erlang_sr", &node, ®, &pobj)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (PyString_Check(node)) {
|
||||
cnode = PyString_AsString(node);
|
||||
fd = ei_connect(pyerl_cnode, cnode);
|
||||
close_fd = 1;
|
||||
}
|
||||
else if (PyInt_Check(node)) {
|
||||
fd = PyInt_AsLong(node);
|
||||
}
|
||||
else {
|
||||
return PyErr_Format(PyExc_ValueError, "invalid erlang node/descriptor");
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
return PyErr_Format(PyExc_ValueError, "Unable to connect to erlang node");
|
||||
}
|
||||
|
||||
|
||||
|
||||
ei_x_new_with_version(&x);
|
||||
|
||||
if (py_to_erl(pobj, &x) < 0) {
|
||||
ei_x_free(&x);
|
||||
if (close_fd) close(fd);
|
||||
return PyErr_Format(PyExc_ValueError, "Unsupported object in Python->Erlang translation");
|
||||
}
|
||||
|
||||
|
||||
if (PyTuple_Check(reg) && PyTuple_Size(reg) == 3) {
|
||||
epid.num = PyInt_AsLong( PyTuple_GetItem(reg, 0) );
|
||||
epid.serial = PyInt_AsLong( PyTuple_GetItem(reg, 1) );
|
||||
epid.creation = PyInt_AsLong( PyTuple_GetItem(reg, 2) );
|
||||
ei_send(fd, &epid, x.buff, x.index);
|
||||
}
|
||||
else if (PyString_Check(reg)) {
|
||||
ei_reg_send(pyerl_cnode, fd, PyString_AsString(reg), x.buff, x.index);
|
||||
}
|
||||
else {
|
||||
ei_x_free(&x);
|
||||
if (close_fd) close(fd);
|
||||
return PyErr_Format(PyExc_ValueError, "Invalid Erlang process");
|
||||
}
|
||||
|
||||
recv:
|
||||
ei_x_free(&x);
|
||||
|
||||
ei_x_new(&x);
|
||||
|
||||
if (ei_xreceive_msg(fd, &em, &x) == ERL_MSG) {
|
||||
|
||||
if (em.msgtype == ERL_TICK) {
|
||||
goto recv;
|
||||
}
|
||||
x.index = 0;
|
||||
ei_decode_version(x.buff, &x.index, &eversion);
|
||||
res = erl_to_py(&x);
|
||||
ei_x_free(&x);
|
||||
if (close_fd) close(fd);
|
||||
return res;
|
||||
}
|
||||
|
||||
ei_x_free(&x);
|
||||
if (close_fd) close(fd);
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
void pyerl_call_registered(void *func, ei_x_buff *x) {
|
||||
|
||||
PyObject *pyargs = PyTuple_New(1);
|
||||
|
||||
PyTuple_SetItem(pyargs, 0, erl_to_py(x));
|
||||
|
||||
python_call((PyObject *) func, pyargs, 0, NULL);
|
||||
}
|
||||
|
||||
PyObject *pyerl_register_process(PyObject * self, PyObject * args) {
|
||||
|
||||
char *name;
|
||||
PyObject *callable;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "sO:erlang_register_process", &name, &callable)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strlen(name) > 0xff-1)
|
||||
return PyErr_Format(PyExc_ValueError, "Invalid erlang process name");
|
||||
|
||||
struct uwsgi_erlang_process *uep = uerl.uep, *old_uep;
|
||||
|
||||
if (!uep) {
|
||||
uerl.uep = uwsgi_malloc(sizeof(struct uwsgi_erlang_process));
|
||||
uep = uerl.uep;
|
||||
}
|
||||
else {
|
||||
while(uep) {
|
||||
old_uep = uep;
|
||||
uep = uep->next;
|
||||
}
|
||||
|
||||
uep = uwsgi_malloc(sizeof(struct uwsgi_erlang_process));
|
||||
old_uep->next = uep;
|
||||
}
|
||||
|
||||
strcpy(uep->name, name);
|
||||
uep->plugin = pyerl_call_registered;
|
||||
uep->func = callable;
|
||||
uep->next = NULL;
|
||||
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
||||
}
|
||||
|
||||
PyObject *pyerl_recv(PyObject * self, PyObject * args) {
|
||||
|
||||
ei_x_buff x;
|
||||
erlang_msg em;
|
||||
PyObject *res;
|
||||
int eversion;
|
||||
int fd;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i:erlang_recv", &fd)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
recv:
|
||||
ei_x_new(&x);
|
||||
|
||||
if (ei_xreceive_msg(fd, &em, &x) == ERL_MSG) {
|
||||
|
||||
if (em.msgtype == ERL_TICK) {
|
||||
ei_x_free(&x);
|
||||
goto recv;
|
||||
}
|
||||
x.index = 0;
|
||||
ei_decode_version(x.buff, &x.index, &eversion);
|
||||
res = erl_to_py(&x);
|
||||
ei_x_free(&x);
|
||||
return res;
|
||||
}
|
||||
|
||||
ei_x_free(&x);
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *pyerl_rpc(PyObject * self, PyObject * args) {
|
||||
|
||||
PyObject *node;
|
||||
char *mod, *fun;
|
||||
char *cnode;
|
||||
PyObject *pobj;
|
||||
ei_x_buff x;
|
||||
ei_x_buff xr;
|
||||
int fd;
|
||||
int close_fd = 0;
|
||||
int eversion;
|
||||
PyObject *res;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OssO:erlang_rpc", &node, &mod, &fun, &pobj)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (PyString_Check(node)) {
|
||||
cnode = PyString_AsString(node);
|
||||
fd = ei_connect(pyerl_cnode, cnode);
|
||||
close_fd = 1;
|
||||
}
|
||||
else if (PyInt_Check(node)) {
|
||||
fd = PyInt_AsLong(node);
|
||||
}
|
||||
else {
|
||||
return PyErr_Format(PyExc_ValueError, "Invalid erlang node/descriptor");
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
return PyErr_Format(PyExc_ValueError, "Unable to connect to erlang node");
|
||||
}
|
||||
|
||||
ei_x_new(&x);
|
||||
|
||||
if (py_to_erl(pobj, &x) < 0) {
|
||||
ei_x_free(&x);
|
||||
if (close_fd) close(fd);
|
||||
return PyErr_Format(PyExc_ValueError, "Unsupported object in Python->Erlang translation");
|
||||
}
|
||||
|
||||
|
||||
ei_x_new(&xr);
|
||||
if (ei_rpc(pyerl_cnode, fd, mod, fun, x.buff, x.index, &xr) < 0) {
|
||||
if (close_fd) close(fd);
|
||||
ei_x_free(&x);
|
||||
ei_x_free(&xr);
|
||||
return PyErr_Format(PyExc_ValueError, "Error in Erlang rpc");
|
||||
}
|
||||
|
||||
xr.index = 0;
|
||||
ei_decode_version(xr.buff, &xr.index, &eversion);
|
||||
|
||||
res = erl_to_py(&xr);
|
||||
|
||||
if (close_fd) close(fd);
|
||||
ei_x_free(&x);
|
||||
ei_x_free(&xr);
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
static PyMethodDef uwsgi_pyerl_methods[] = {
|
||||
{"erlang_connect", pyerl_connect, METH_VARARGS, ""},
|
||||
{"erlang_close", pyerl_close, METH_VARARGS, ""},
|
||||
{"erlang_send_message", pyerl_send, METH_VARARGS, ""},
|
||||
{"erlang_send", pyerl_send, METH_VARARGS, ""},
|
||||
{"erlang_recv_message", pyerl_recv, METH_VARARGS, ""},
|
||||
{"erlang_recv", pyerl_recv, METH_VARARGS, ""},
|
||||
{"erlang_sr", pyerl_sr, METH_VARARGS, ""},
|
||||
{"erlang_rpc", pyerl_rpc, METH_VARARGS, ""},
|
||||
{"erlang_lock", pyerl_lock, METH_VARARGS, ""},
|
||||
{"erlang_unlock", pyerl_unlock, METH_VARARGS, ""},
|
||||
{"erlang_register_process", pyerl_register_process, METH_VARARGS, ""},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
void py_erl_init_functions() {
|
||||
|
||||
PyMethodDef *uwsgi_function;
|
||||
|
||||
PyDict_SetItemString(up.embedded_dict, "erlang_node", PyString_FromString(ei_thisnodename(pyerl_cnode)));
|
||||
|
||||
for (uwsgi_function = uwsgi_pyerl_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) {
|
||||
PyObject *func = PyCFunction_New(uwsgi_function, NULL);
|
||||
PyDict_SetItemString(up.embedded_dict, uwsgi_function->ml_name, func);
|
||||
Py_DECREF(func);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int py_to_erl(PyObject *pobj, ei_x_buff *x) {
|
||||
int i;
|
||||
PyObject *pobj2;
|
||||
|
||||
if (pobj == NULL || pobj == Py_None) {
|
||||
ei_x_encode_empty_list(x);
|
||||
}
|
||||
else if (PyString_Check(pobj)) {
|
||||
ei_x_encode_binary(x, PyString_AsString(pobj), PyString_Size(pobj));
|
||||
}
|
||||
else if (PyUnicode_Check(pobj)) {
|
||||
ei_x_encode_atom(x, PyString_AsString(pobj));
|
||||
}
|
||||
else if (PyInt_Check(pobj)) {
|
||||
ei_x_encode_long(x, PyInt_AsLong(pobj));
|
||||
}
|
||||
else if (PyList_Check(pobj)) {
|
||||
if (PyList_Size(pobj) > 0) {
|
||||
ei_x_encode_list_header(x, PyList_Size(pobj));
|
||||
for (i = 0; i < PyList_Size(pobj); i++) {
|
||||
pobj2 = PyList_GetItem(pobj, i);
|
||||
if (py_to_erl(pobj2, x) < 0) return -1;
|
||||
}
|
||||
}
|
||||
ei_x_encode_empty_list(x);
|
||||
}
|
||||
else if (PyTuple_Check(pobj)) {
|
||||
ei_x_encode_tuple_header(x, PyTuple_Size(pobj));
|
||||
for (i = 0; i < PyTuple_Size(pobj); i++) {
|
||||
pobj2 = PyTuple_GetItem(pobj, i);
|
||||
if (py_to_erl(pobj2, x) < 0) return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return x->index;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void pyerl_init() {
|
||||
|
||||
up.extension = py_erl_init_functions;
|
||||
|
||||
if (!uerl.name) {
|
||||
pyerl_cnode = uwsgi_malloc(sizeof(ei_cnode));
|
||||
memset(pyerl_cnode, 0, sizeof(ei_cnode));
|
||||
if (ei_connect_init(pyerl_cnode, "uwsgi", NULL, 0) < 0) {
|
||||
uwsgi_log("unable to initialize erlang connection\n");
|
||||
exit(1);
|
||||
}
|
||||
uwsgi_log("Erlang C-Node name: %s\n",ei_thisnodename(pyerl_cnode));
|
||||
}
|
||||
else {
|
||||
pyerl_cnode = &uerl.cnode;
|
||||
}
|
||||
|
||||
uwsgi_log("enabled Python<->Erlang bridge\n");
|
||||
|
||||
}
|
||||
|
||||
struct uwsgi_plugin pyerl_plugin = {
|
||||
|
||||
.post_init = pyerl_init,
|
||||
};
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import os
|
||||
from distutils import sysconfig
|
||||
|
||||
NAME='pyerl'
|
||||
|
||||
ERLANGPATH = os.environ.get('UWSGICONFIG_ERLANGPATH', 'erl')
|
||||
|
||||
includedir = os.popen(ERLANGPATH + " -noshell -noinput -eval \"io:format('~s~n', [code:lib_dir(erl_interface, include)])\" -s erlang halt").read().rstrip()
|
||||
libpath = os.popen(ERLANGPATH + " -noshell -noinput -eval \"io:format('~s~n', [code:lib_dir(erl_interface, lib)])\" -s erlang halt").read().rstrip()
|
||||
|
||||
CFLAGS = [ '-I' + includedir, '-I' + sysconfig.get_python_inc(), '-I' + sysconfig.get_python_inc(plat_specific=True)]
|
||||
LDFLAGS = [ '-L' + libpath ]
|
||||
|
||||
LIBS = ['-lei']
|
||||
|
||||
GCC_LIST = ['pyerl']
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
[uwsgi]
|
||||
main_plugin = python,gevent,psgi,lua,php,rack,jvm,jwsgi,ring,mono,transformation_toupper,coroae,v8,cgi,xslt,webdav,ssi,ldap,gccgo,rados,pypy
|
||||
main_plugin = python,gevent,psgi,lua,php,rack,jvm,jwsgi,ring,mono,transformation_toupper,coroae,v8,cgi,xslt,webdav,ssi,ldap,gccgo,rados,pypy,zabbix,curl_cron,tornado,tuntap,pty,mongrel2,alarm_curl,router_radius,airbrake
|
||||
inherit = base
|
||||
|
||||
@@ -7,8 +7,6 @@ pcre = auto
|
||||
routing = auto
|
||||
debug = false
|
||||
unbit = false
|
||||
xml_implementation = libxml2
|
||||
yaml_implementation = auto
|
||||
malloc_implementation = libc
|
||||
extras =
|
||||
plugins =
|
||||
|
||||
@@ -7,8 +7,6 @@ pcre = false
|
||||
routing = false
|
||||
debug = false
|
||||
unbit = false
|
||||
xml_implementation = none
|
||||
yaml_implementation = none
|
||||
malloc_implementation = libc
|
||||
extras =
|
||||
plugins =
|
||||
|
||||
+2
-4
@@ -1,14 +1,12 @@
|
||||
[uwsgi]
|
||||
xml = true
|
||||
yaml = true
|
||||
xml = libxml2
|
||||
yaml = libyaml
|
||||
json = true
|
||||
ssl = true
|
||||
pcre = true
|
||||
routing = true
|
||||
debug = false
|
||||
unbit = false
|
||||
xml_implementation = libxml2
|
||||
yaml_implementation = libyaml
|
||||
malloc_implementation = libc
|
||||
extras =
|
||||
plugins =
|
||||
|
||||
+2
-4
@@ -1,14 +1,12 @@
|
||||
[uwsgi]
|
||||
xml = true
|
||||
yaml = true
|
||||
xml = expat
|
||||
yaml = embedded
|
||||
json = false
|
||||
ssl = true
|
||||
pcre = true
|
||||
routing = true
|
||||
debug = false
|
||||
unbit = true
|
||||
xml_implementation = expat
|
||||
yaml_implementation = auto
|
||||
malloc_implementation = libc
|
||||
extras =
|
||||
plugins =
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
[uwsgi]
|
||||
xml = expat
|
||||
yaml = embedded
|
||||
json = true
|
||||
ssl = true
|
||||
pcre = true
|
||||
routing = true
|
||||
debug = false
|
||||
unbit = false
|
||||
malloc_implementation = libc
|
||||
extras =
|
||||
plugins =
|
||||
bin_name = uwsgi
|
||||
append_version =
|
||||
plugin_dir = /opt/unbit/uwsgi/plugins
|
||||
embedded_plugins =
|
||||
as_shared_library = false
|
||||
|
||||
locking = auto
|
||||
event = auto
|
||||
timer = auto
|
||||
filemonitor = auto
|
||||
|
||||
blacklist =
|
||||
whitelist =
|
||||
|
||||
embed_files =
|
||||
|
||||
embed_config =
|
||||
+3
-2
@@ -360,7 +360,8 @@ void async_schedule_to_req_green(void) {
|
||||
}
|
||||
#endif
|
||||
for(;;) {
|
||||
if (uwsgi.p[wsgi_req->uh->modifier1]->request(wsgi_req) <= UWSGI_OK) {
|
||||
wsgi_req->async_status = uwsgi.p[wsgi_req->uh->modifier1]->request(wsgi_req);
|
||||
if (wsgi_req->async_status <= UWSGI_OK) {
|
||||
break;
|
||||
}
|
||||
wsgi_req->switches++;
|
||||
@@ -532,7 +533,7 @@ void async_loop() {
|
||||
continue;
|
||||
}
|
||||
// re-add timer
|
||||
async_add_timeout(uwsgi.wsgi_req, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
async_add_timeout(uwsgi.wsgi_req, uwsgi.socket_timeout);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -64,7 +64,7 @@ int uwsgi_buffer_insert_chunked(struct uwsgi_buffer *ub, size_t pos, size_t len)
|
||||
// 0xFFFFFFFFFFFFFFFF\r\n\0
|
||||
char chunked[19];
|
||||
int ret = snprintf(chunked, 19, "%X\r\n", (unsigned int) len);
|
||||
if (ret <= 0 || ret > 19) {
|
||||
if (ret <= 0 || ret >= 19) {
|
||||
return -1;
|
||||
}
|
||||
return uwsgi_buffer_insert(ub, pos, chunked, ret);
|
||||
@@ -74,7 +74,7 @@ int uwsgi_buffer_append_chunked(struct uwsgi_buffer *ub, size_t len) {
|
||||
// 0xFFFFFFFFFFFFFFFF\r\n\0
|
||||
char chunked[19];
|
||||
int ret = snprintf(chunked, 19, "%X\r\n", (unsigned int) len);
|
||||
if (ret <= 0 || ret > 19) {
|
||||
if (ret <= 0 || ret >= 19) {
|
||||
return -1;
|
||||
}
|
||||
return uwsgi_buffer_append(ub, chunked, ret);
|
||||
@@ -245,7 +245,7 @@ int uwsgi_buffer_append_ipv4(struct uwsgi_buffer *ub, void *addr) {
|
||||
int uwsgi_buffer_num64(struct uwsgi_buffer *ub, int64_t num) {
|
||||
char buf[sizeof(UMAX64_STR)+1];
|
||||
int ret = snprintf(buf, sizeof(UMAX64_STR)+1, "%lld", (long long) num);
|
||||
if (ret <= 0 || ret > (int) (sizeof(UMAX64_STR)+1)) {
|
||||
if (ret <= 0 || ret >= (int) (sizeof(UMAX64_STR)+1)) {
|
||||
return -1;
|
||||
}
|
||||
return uwsgi_buffer_append(ub, buf, ret);
|
||||
@@ -268,7 +268,7 @@ int uwsgi_buffer_append_keyval32(struct uwsgi_buffer *ub, char *key, uint32_t ke
|
||||
int uwsgi_buffer_append_keynum(struct uwsgi_buffer *ub, char *key, uint16_t keylen, int64_t num) {
|
||||
char buf[sizeof(UMAX64_STR)+1];
|
||||
int ret = snprintf(buf, (sizeof(UMAX64_STR)+1), "%lld", (long long) num);
|
||||
if (ret <= 0 || ret > (int) (sizeof(UMAX64_STR)+1)) {
|
||||
if (ret <= 0 || ret >= (int) (sizeof(UMAX64_STR)+1)) {
|
||||
return -1;
|
||||
}
|
||||
if (uwsgi_buffer_u16le(ub, keylen)) return -1;
|
||||
@@ -280,7 +280,7 @@ int uwsgi_buffer_append_keynum(struct uwsgi_buffer *ub, char *key, uint16_t keyl
|
||||
int uwsgi_buffer_append_valnum(struct uwsgi_buffer *ub, int64_t num) {
|
||||
char buf[sizeof(UMAX64_STR)+1];
|
||||
int ret = snprintf(buf, (sizeof(UMAX64_STR)+1), "%lld", (long long) num);
|
||||
if (ret <= 0 || ret > (int) (sizeof(UMAX64_STR)+1)) {
|
||||
if (ret <= 0 || ret >= (int) (sizeof(UMAX64_STR)+1)) {
|
||||
return -1;
|
||||
}
|
||||
if (uwsgi_buffer_u16le(ub, ret)) return -1;
|
||||
@@ -340,7 +340,7 @@ int uwsgi_buffer_send(struct uwsgi_buffer *ub, int fd) {
|
||||
char *ptr = ub->buf;
|
||||
|
||||
while (remains > 0) {
|
||||
int ret = uwsgi_waitfd_write(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi_waitfd_write(fd, uwsgi.socket_timeout);
|
||||
if (ret > 0) {
|
||||
ssize_t len = write(fd, ptr, remains);
|
||||
if (len > 0) {
|
||||
|
||||
+14
-14
@@ -1483,7 +1483,7 @@ char *uwsgi_cache_magic_get(char *key, uint16_t keylen, uint64_t *vallen, uint64
|
||||
int fd = uwsgi_connect(cache_server, 0, 1);
|
||||
if (fd < 0) return NULL;
|
||||
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
@@ -1495,7 +1495,7 @@ char *uwsgi_cache_magic_get(char *key, uint16_t keylen, uint64_t *vallen, uint64
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], &ucmc)) {
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.socket_timeout, &ucmc)) {
|
||||
close(fd);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return NULL;
|
||||
@@ -1526,7 +1526,7 @@ char *uwsgi_cache_magic_get(char *key, uint16_t keylen, uint64_t *vallen, uint64
|
||||
}
|
||||
|
||||
// read the raw value from the socket
|
||||
if (uwsgi_read_whole_true_nb(fd, ub->buf, ucmc.size, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_read_whole_true_nb(fd, ub->buf, ucmc.size, uwsgi.socket_timeout)) {
|
||||
close(fd);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return NULL;
|
||||
@@ -1585,7 +1585,7 @@ int uwsgi_cache_magic_exists(char *key, uint16_t keylen, char *cache) {
|
||||
int fd = uwsgi_connect(cache_server, 0, 1);
|
||||
if (fd < 0) return 0;
|
||||
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
close(fd);
|
||||
return 0;
|
||||
@@ -1597,7 +1597,7 @@ int uwsgi_cache_magic_exists(char *key, uint16_t keylen, char *cache) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], &ucmc)) {
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.socket_timeout, &ucmc)) {
|
||||
close(fd);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return 0;
|
||||
@@ -1652,7 +1652,7 @@ int uwsgi_cache_magic_set(char *key, uint16_t keylen, char *value, uint64_t vall
|
||||
int fd = uwsgi_connect(cache_server, 0, 1);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
@@ -1670,7 +1670,7 @@ int uwsgi_cache_magic_set(char *key, uint16_t keylen, char *value, uint64_t vall
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cache_magic_send_and_manage(fd, ub, value, vallen, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], &ucmc)) {
|
||||
if (cache_magic_send_and_manage(fd, ub, value, vallen, uwsgi.socket_timeout, &ucmc)) {
|
||||
close(fd);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return -1;
|
||||
@@ -1730,7 +1730,7 @@ int uwsgi_cache_magic_del(char *key, uint16_t keylen, char *cache) {
|
||||
int fd = uwsgi_connect(cache_server, 0, 1);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
@@ -1742,7 +1742,7 @@ int uwsgi_cache_magic_del(char *key, uint16_t keylen, char *cache) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], &ucmc)) {
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.socket_timeout, &ucmc)) {
|
||||
close(fd);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return -1;
|
||||
@@ -1803,7 +1803,7 @@ int uwsgi_cache_magic_clear(char *cache) {
|
||||
int fd = uwsgi_connect(cache_server, 0, 1);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
@@ -1815,7 +1815,7 @@ int uwsgi_cache_magic_clear(char *cache) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], &ucmc)) {
|
||||
if (cache_magic_send_and_manage(fd, ub, NULL, 0, uwsgi.socket_timeout, &ucmc)) {
|
||||
close(fd);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return -1;
|
||||
@@ -1859,7 +1859,7 @@ void uwsgi_cache_sync_from_nodes(struct uwsgi_cache *uc) {
|
||||
goto next;
|
||||
}
|
||||
|
||||
if (uwsgi_write_nb(fd, ub->buf, ub->pos, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_write_nb(fd, ub->buf, ub->pos, uwsgi.socket_timeout)) {
|
||||
uwsgi_buffer_destroy(ub);
|
||||
uwsgi_log("[cache-sync] unable to write to the cache server\n");
|
||||
close(fd);
|
||||
@@ -1867,7 +1867,7 @@ void uwsgi_cache_sync_from_nodes(struct uwsgi_cache *uc) {
|
||||
}
|
||||
|
||||
size_t rlen = ub->pos;
|
||||
if (uwsgi_read_with_realloc(fd, &ub->buf, &rlen, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], NULL, NULL)) {
|
||||
if (uwsgi_read_with_realloc(fd, &ub->buf, &rlen, uwsgi.socket_timeout, NULL, NULL)) {
|
||||
uwsgi_buffer_destroy(ub);
|
||||
uwsgi_log("[cache-sync] unable to read from the cache server\n");
|
||||
close(fd);
|
||||
@@ -1876,7 +1876,7 @@ void uwsgi_cache_sync_from_nodes(struct uwsgi_cache *uc) {
|
||||
|
||||
uwsgi_hooked_parse(ub->buf, rlen, cache_sync_hook, uc);
|
||||
|
||||
if (uwsgi_read_nb(fd, (char *) uc->items, uc->filesize, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_read_nb(fd, (char *) uc->items, uc->filesize, uwsgi.socket_timeout)) {
|
||||
uwsgi_buffer_destroy(ub);
|
||||
close(fd);
|
||||
uwsgi_log("[cache-sync] unable to read from the cache server\n");
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ extern struct uwsgi_server uwsgi;
|
||||
static ssize_t uwsgi_chunked_input_recv(struct wsgi_request *wsgi_req, int timeout, int nb) {
|
||||
|
||||
if (timeout == 0) timeout = uwsgi.chunked_input_timeout;
|
||||
if (timeout == 0) timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
if (timeout == 0) timeout = uwsgi.socket_timeout;
|
||||
|
||||
int ret = -1;
|
||||
|
||||
|
||||
+122
-6
@@ -55,7 +55,7 @@ void uwsgi_daemons_smart_check() {
|
||||
if (ud->pid > 0) {
|
||||
if (!kill(ud->pid, 0)) {
|
||||
uwsgi_log("[uwsgi_daemons] stopping legion \"%s\" daemon: %s (pid: %d)\n", ud->legion, ud->command, ud->pid);
|
||||
kill(-ud->pid, SIGTERM);
|
||||
kill(-ud->pid, ud->stop_signal);
|
||||
}
|
||||
else {
|
||||
// pid already died
|
||||
@@ -237,7 +237,7 @@ void uwsgi_detach_daemons() {
|
||||
time_t timeout = uwsgi_now() + (uwsgi.reload_mercy ? uwsgi.reload_mercy : 3);
|
||||
int waitpid_status;
|
||||
while (!kill(ud->pid, 0)) {
|
||||
kill(-ud->pid, SIGTERM);
|
||||
kill(-ud->pid, ud->stop_signal);
|
||||
sleep(1);
|
||||
waitpid(-ud->pid, &waitpid_status, WNOHANG);
|
||||
if (uwsgi_now() >= timeout) {
|
||||
@@ -291,6 +291,20 @@ void uwsgi_spawn_daemon(struct uwsgi_daemon *ud) {
|
||||
uwsgi_close_all_sockets();
|
||||
uwsgi_close_all_fds();
|
||||
|
||||
if (ud->gid) {
|
||||
if (setgid(ud->gid)) {
|
||||
uwsgi_error("uwsgi_spawn_daemon()/setgid()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (ud->uid) {
|
||||
if (setuid(ud->uid)) {
|
||||
uwsgi_error("uwsgi_spawn_daemon()/setuid()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (ud->daemonize) {
|
||||
/* refork... */
|
||||
pid = fork();
|
||||
@@ -304,7 +318,7 @@ void uwsgi_spawn_daemon(struct uwsgi_daemon *ud) {
|
||||
uwsgi_write_pidfile(ud->pidfile);
|
||||
}
|
||||
|
||||
if (!uwsgi.daemons_honour_stdin) {
|
||||
if (!uwsgi.daemons_honour_stdin && !ud->honour_stdin) {
|
||||
// /dev/null will became stdin
|
||||
uwsgi_remap_fd(0, "/dev/null");
|
||||
}
|
||||
@@ -328,7 +342,7 @@ void uwsgi_spawn_daemon(struct uwsgi_daemon *ud) {
|
||||
sleep((unsigned int) throttle);
|
||||
}
|
||||
|
||||
uwsgi_log("[uwsgi-daemons] %sspawning \"%s\"\n", ud->respawns > 0 ? "re" : "", ud->command);
|
||||
uwsgi_log("[uwsgi-daemons] %sspawning \"%s\" (uid: %d gid: %d)\n", ud->respawns > 0 ? "re" : "", ud->command, (int) getuid(), (int) getgid());
|
||||
uwsgi_exec_command_with_args(ud->command);
|
||||
uwsgi_log("[uwsgi-daemons] unable to spawn \"%s\"\n", ud->command);
|
||||
|
||||
@@ -347,6 +361,8 @@ void uwsgi_opt_add_daemon(char *opt, char *value, void *none) {
|
||||
int daemonize = 0;
|
||||
int freq = 10;
|
||||
char *space = NULL;
|
||||
int stop_signal = SIGTERM;
|
||||
int reload_signal = 0;
|
||||
|
||||
char *command = uwsgi_str(value);
|
||||
|
||||
@@ -385,7 +401,7 @@ void uwsgi_opt_add_daemon(char *opt, char *value, void *none) {
|
||||
}
|
||||
|
||||
if (!uwsgi_ud) {
|
||||
uwsgi.daemons = uwsgi_malloc(sizeof(struct uwsgi_daemon));
|
||||
uwsgi.daemons = uwsgi_calloc(sizeof(struct uwsgi_daemon));
|
||||
uwsgi_ud = uwsgi.daemons;
|
||||
}
|
||||
else {
|
||||
@@ -394,7 +410,7 @@ void uwsgi_opt_add_daemon(char *opt, char *value, void *none) {
|
||||
uwsgi_ud = uwsgi_ud->next;
|
||||
}
|
||||
|
||||
uwsgi_ud = uwsgi_malloc(sizeof(struct uwsgi_daemon));
|
||||
uwsgi_ud = uwsgi_calloc(sizeof(struct uwsgi_daemon));
|
||||
old_ud->next = uwsgi_ud;
|
||||
}
|
||||
|
||||
@@ -409,6 +425,8 @@ void uwsgi_opt_add_daemon(char *opt, char *value, void *none) {
|
||||
uwsgi_ud->daemonize = daemonize;
|
||||
uwsgi_ud->pidfile = pidfile;
|
||||
uwsgi_ud->control = 0;
|
||||
uwsgi_ud->stop_signal = stop_signal;
|
||||
uwsgi_ud->reload_signal = reload_signal;
|
||||
if (!strcmp(opt, "attach-control-daemon")) {
|
||||
uwsgi_ud->control = 1;
|
||||
}
|
||||
@@ -419,3 +437,101 @@ void uwsgi_opt_add_daemon(char *opt, char *value, void *none) {
|
||||
uwsgi.daemons_cnt++;
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_opt_add_daemon2(char *opt, char *value, void *none) {
|
||||
|
||||
struct uwsgi_daemon *uwsgi_ud = uwsgi.daemons, *old_ud;
|
||||
|
||||
char *d_command = NULL;
|
||||
char *d_freq = NULL;
|
||||
char *d_pidfile = NULL;
|
||||
char *d_control = NULL;
|
||||
char *d_legion = NULL;
|
||||
char *d_daemonize = NULL;
|
||||
char *d_touch = NULL;
|
||||
char *d_stopsignal = NULL;
|
||||
char *d_reloadsignal = NULL;
|
||||
char *d_stdin = NULL;
|
||||
char *d_uid = NULL;
|
||||
char *d_gid = NULL;
|
||||
|
||||
char *arg = uwsgi_str(value);
|
||||
|
||||
if (uwsgi_kvlist_parse(arg, strlen(arg), ',', '=',
|
||||
"command", &d_command,
|
||||
"cmd", &d_command,
|
||||
"exec", &d_command,
|
||||
"freq", &d_freq,
|
||||
"pidfile", &d_pidfile,
|
||||
"control", &d_control,
|
||||
"daemonize", &d_daemonize,
|
||||
"daemon", &d_daemonize,
|
||||
"touch", &d_touch,
|
||||
"stopsignal", &d_stopsignal,
|
||||
"stop_signal", &d_stopsignal,
|
||||
"reloadsignal", &d_reloadsignal,
|
||||
"reload_signal", &d_reloadsignal,
|
||||
"stdin", &d_stdin,
|
||||
"uid", &d_uid,
|
||||
"gid", &d_gid,
|
||||
NULL)) {
|
||||
uwsgi_log("invalid --%s keyval syntax\n", opt);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!d_command) {
|
||||
uwsgi_log("--%s: you need to specify a 'command' key\n", opt);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#ifndef UWSGI_SSL
|
||||
if (d_legion) {
|
||||
uwsgi_log("legion subsystem is not supported on this uWSGI version, rebuild with ssl support\n");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
if (!uwsgi_ud) {
|
||||
uwsgi.daemons = uwsgi_calloc(sizeof(struct uwsgi_daemon));
|
||||
uwsgi_ud = uwsgi.daemons;
|
||||
}
|
||||
else {
|
||||
while (uwsgi_ud) {
|
||||
old_ud = uwsgi_ud;
|
||||
uwsgi_ud = uwsgi_ud->next;
|
||||
}
|
||||
|
||||
uwsgi_ud = uwsgi_calloc(sizeof(struct uwsgi_daemon));
|
||||
old_ud->next = uwsgi_ud;
|
||||
}
|
||||
uwsgi_ud->command = d_command;
|
||||
uwsgi_ud->freq = d_freq ? atoi(d_freq) : 10;
|
||||
uwsgi_ud->daemonize = d_daemonize ? 1 : 0;
|
||||
uwsgi_ud->pidfile = d_pidfile;
|
||||
uwsgi_ud->stop_signal = d_stopsignal ? atoi(d_stopsignal) : SIGTERM;
|
||||
uwsgi_ud->reload_signal = d_reloadsignal ? atoi(d_reloadsignal) : 0;
|
||||
uwsgi_ud->control = d_control ? 1 : 0;
|
||||
uwsgi_ud->uid = d_uid ? atoi(d_uid) : 0;
|
||||
uwsgi_ud->gid = d_gid ? atoi(d_gid) : 0;
|
||||
uwsgi_ud->honour_stdin = d_stdin ? 1 : 0;
|
||||
#ifdef UWSGI_SSL
|
||||
uwsgi_ud->legion = d_legion;
|
||||
#endif
|
||||
|
||||
if (d_touch) {
|
||||
size_t i,rlen = 0;
|
||||
char **argv = uwsgi_split_quoted(d_touch, strlen(d_touch), ";", &rlen);
|
||||
for(i=0;i<rlen;i++) {
|
||||
uwsgi_string_new_list(&uwsgi_ud->touch, argv[i]);
|
||||
}
|
||||
if (argv) free(argv);
|
||||
}
|
||||
|
||||
uwsgi.daemons_cnt++;
|
||||
|
||||
free(arg);
|
||||
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -701,12 +701,12 @@ void emperor_add(struct uwsgi_emperor_scanner *ues, char *name, time_t born, cha
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
int now = tv.tv_sec;
|
||||
uint64_t micros = (tv.tv_sec * 1000 * 1000) + tv.tv_usec;
|
||||
uint64_t micros = (tv.tv_sec * 1000ULL * 1000ULL) + tv.tv_usec;
|
||||
|
||||
// blacklist check
|
||||
struct uwsgi_emperor_blacklist_item *uebi = uwsgi_emperor_blacklist_check(name);
|
||||
if (uebi) {
|
||||
uint64_t i_micros = (uebi->last_attempt.tv_sec * 1000 * 1000) + uebi->last_attempt.tv_usec + uebi->throttle_level;
|
||||
uint64_t i_micros = (uebi->last_attempt.tv_sec * 1000ULL * 1000ULL) + uebi->last_attempt.tv_usec + uebi->throttle_level;
|
||||
if (i_micros > micros) {
|
||||
return;
|
||||
}
|
||||
@@ -1746,7 +1746,8 @@ void emperor_send_stats(int fd) {
|
||||
goto end0;
|
||||
struct uwsgi_emperor_scanner *ues = emperor_scanners;
|
||||
while (ues) {
|
||||
uwsgi_stats_str(us, ues->arg);
|
||||
if (uwsgi_stats_str(us, ues->arg))
|
||||
goto end0;
|
||||
ues = ues->next;
|
||||
if (ues) {
|
||||
if (uwsgi_stats_comma(us))
|
||||
@@ -1889,7 +1890,7 @@ void emperor_send_stats(int fd) {
|
||||
size_t remains = us->pos;
|
||||
off_t pos = 0;
|
||||
while (remains > 0) {
|
||||
int ret = uwsgi_waitfd_write(client_fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi_waitfd_write(client_fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
goto end0;
|
||||
}
|
||||
@@ -2052,12 +2053,14 @@ void uwsgi_master_manage_emperor() {
|
||||
uwsgi_log_verbose("received message %d from emperor\n", byte);
|
||||
// remove me
|
||||
if (byte == 0) {
|
||||
uwsgi_hooks_run(uwsgi.hook_emperor_stop, "emperor-stop", 0);
|
||||
close(uwsgi.emperor_fd);
|
||||
if (!uwsgi.status.brutally_reloading)
|
||||
kill_them_all(0);
|
||||
}
|
||||
// reload me
|
||||
else if (byte == 1) {
|
||||
uwsgi_hooks_run(uwsgi.hook_emperor_reload, "emperor-reload", 0);
|
||||
// un-lazy the stack to trigger a real reload
|
||||
uwsgi.lazy = 0;
|
||||
uwsgi_block_signal(SIGHUP);
|
||||
@@ -2067,6 +2070,7 @@ void uwsgi_master_manage_emperor() {
|
||||
}
|
||||
else {
|
||||
uwsgi_log("lost connection with my emperor !!!\n");
|
||||
uwsgi_hooks_run(uwsgi.hook_emperor_lost, "emperor-lost", 0);
|
||||
close(uwsgi.emperor_fd);
|
||||
if (!uwsgi.status.brutally_reloading)
|
||||
kill_them_all(0);
|
||||
|
||||
@@ -29,6 +29,8 @@ struct uwsgi_gateway *register_gateway(char *name, void (*loop) (int, void *), v
|
||||
ug->num = num;
|
||||
ug->fullname = fullname;
|
||||
ug->data = data;
|
||||
ug->uid = 0;
|
||||
ug->gid = 0;
|
||||
|
||||
#if defined(SOCK_SEQPACKET) && defined(__linux__)
|
||||
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, ug->internal_subscription_pipe)) {
|
||||
@@ -90,6 +92,22 @@ void gateway_respawn(int id) {
|
||||
signal(SIGSTOP, SIG_IGN);
|
||||
signal(SIGTSTP, SIG_IGN);
|
||||
|
||||
if (ug->gid) {
|
||||
uwsgi_log("%s %d setgid() to %d\n", ug->name, ug->num, (int) ug->gid);
|
||||
if (setgid(ug->gid)) {
|
||||
uwsgi_error("gateway_respawn()/setgid()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (ug->uid) {
|
||||
uwsgi_log("%s %d setuid() to %d\n", ug->name, ug->num, (int) ug->uid);
|
||||
if (setuid(ug->uid)) {
|
||||
uwsgi_error("gateway_respawn()/setuid()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
ug->loop(id, ug->data);
|
||||
// never here !!! (i hope)
|
||||
exit(1);
|
||||
|
||||
+241
-4
@@ -55,6 +55,22 @@ static int uwsgi_hook_chdir(char *arg) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int uwsgi_hook_mkdir(char *arg) {
|
||||
int ret = mkdir(arg, 0777);
|
||||
if (ret) {
|
||||
uwsgi_error("uwsgi_hook_mkdir()");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int uwsgi_hook_putenv(char *arg) {
|
||||
int ret = putenv(arg);
|
||||
if (ret) {
|
||||
uwsgi_error("uwsgi_hook_putenv()");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int uwsgi_hook_exec(char *arg) {
|
||||
int ret = uwsgi_run_command_and_wait(NULL, arg);
|
||||
if (ret != 0) {
|
||||
@@ -121,7 +137,7 @@ static int uwsgi_hook_write(char *arg) {
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
int fd = open(arg, O_WRONLY);
|
||||
int fd = open(arg, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
||||
if (fd < 0) {
|
||||
uwsgi_error_open(arg);
|
||||
*space = ' ';
|
||||
@@ -138,6 +154,202 @@ static int uwsgi_hook_write(char *arg) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_hook_append(char *arg) {
|
||||
char *space = strchr(arg, ' ');
|
||||
if (!space) {
|
||||
uwsgi_log("invalid hook append syntax, must be: <file> <string>\n");
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
int fd = open(arg, O_WRONLY|O_CREAT|O_APPEND, 0666);
|
||||
if (fd < 0) {
|
||||
uwsgi_error_open(arg);
|
||||
*space = ' ';
|
||||
return -1;
|
||||
}
|
||||
*space = ' ';
|
||||
size_t l = strlen(space+1);
|
||||
if (write(fd, space+1, l) != (ssize_t) l) {
|
||||
uwsgi_error("uwsgi_hook_append()/write()");
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int uwsgi_hook_writen(char *arg) {
|
||||
char *space = strchr(arg, ' ');
|
||||
if (!space) {
|
||||
uwsgi_log("invalid hook writen syntax, must be: <file> <string>\n");
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
int fd = open(arg, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
||||
if (fd < 0) {
|
||||
uwsgi_error_open(arg);
|
||||
*space = ' ';
|
||||
return -1;
|
||||
}
|
||||
*space = ' ';
|
||||
size_t l = strlen(space+1);
|
||||
char *buf = uwsgi_malloc(l + 1);
|
||||
memcpy(buf, space+1, l);
|
||||
buf[l] = '\n';
|
||||
if (write(fd, buf, l+1) != (ssize_t) (l+1)) {
|
||||
uwsgi_error("uwsgi_hook_writen()/write()");
|
||||
free(buf);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
free(buf);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_hook_appendn(char *arg) {
|
||||
char *space = strchr(arg, ' ');
|
||||
if (!space) {
|
||||
uwsgi_log("invalid hook appendn syntax, must be: <file> <string>\n");
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
int fd = open(arg, O_WRONLY|O_CREAT|O_APPEND, 0666);
|
||||
if (fd < 0) {
|
||||
uwsgi_error_open(arg);
|
||||
*space = ' ';
|
||||
return -1;
|
||||
}
|
||||
*space = ' ';
|
||||
size_t l = strlen(space+1);
|
||||
char *buf = uwsgi_malloc(l + 1);
|
||||
memcpy(buf, space+1, l);
|
||||
buf[l] = '\n';
|
||||
if (write(fd, buf, l+1) != (ssize_t) (l+1)) {
|
||||
uwsgi_error("uwsgi_hook_appendn()/write()");
|
||||
free(buf);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
free(buf);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int uwsgi_hook_chmod(char *arg) {
|
||||
char *space = strchr(arg, ' ');
|
||||
if (!space) {
|
||||
uwsgi_log("invalid hook chmod syntax, must be: <file> <mode>\n");
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
int error = 0;
|
||||
mode_t mask = uwsgi_mode_t(space+1, &error);
|
||||
if (error) {
|
||||
uwsgi_log("invalid hook chmod mask: %s\n", space+1);
|
||||
*space = ' ';
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = chmod(arg, mask);
|
||||
*space = ' ';
|
||||
if (ret) {
|
||||
uwsgi_error("uwsgi_hook_chmod()/chmod()");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int uwsgi_hook_chown(char *arg) {
|
||||
char *space = strchr(arg, ' ');
|
||||
if (!space) {
|
||||
uwsgi_log("invalid hook chown syntax, must be: <file> <uid> <gid>\n");
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
|
||||
char *space2 = strchr(space+1, ' ');
|
||||
if (!space2) {
|
||||
*space = ' ';
|
||||
uwsgi_log("invalid hook chown syntax, must be: <file> <uid> <gid>\n");
|
||||
return -1;
|
||||
}
|
||||
*space2 = 0;
|
||||
|
||||
struct passwd *pw = getpwnam(space+1);
|
||||
if (!pw) {
|
||||
uwsgi_log("unable to find uid %s\n", space+1);
|
||||
*space = ' ';
|
||||
*space2 = ' ';
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct group *gr = getgrnam(space2+1);
|
||||
if (!gr) {
|
||||
uwsgi_log("unable to find gid %s\n", space2+1);
|
||||
*space = ' ';
|
||||
*space2 = ' ';
|
||||
return -1;
|
||||
}
|
||||
int ret = chown(arg, pw->pw_uid, gr->gr_gid);
|
||||
*space = ' ';
|
||||
*space2 = ' ';
|
||||
if (ret) {
|
||||
uwsgi_error("uwsgi_hook_chown()/chown)");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int uwsgi_hook_chown2(char *arg) {
|
||||
char *space = strchr(arg, ' ');
|
||||
if (!space) {
|
||||
uwsgi_log("invalid hook chown2 syntax, must be: <file> <uid> <gid>\n");
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
|
||||
char *space2 = strchr(space+1, ' ');
|
||||
if (!space2) {
|
||||
*space = ' ';
|
||||
uwsgi_log("invalid hook chown2 syntax, must be: <file> <uid> <gid>\n");
|
||||
return -1;
|
||||
}
|
||||
*space2 = 0;
|
||||
|
||||
if (!is_a_number(space+1)) {
|
||||
uwsgi_log("invalid hook chown2 syntax, uid must be a number\n");
|
||||
*space = ' ';
|
||||
*space2 = ' ';
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!is_a_number(space2+1)) {
|
||||
uwsgi_log("invalid hook chown2 syntax, gid must be a number\n");
|
||||
*space = ' ';
|
||||
*space2 = ' ';
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = chown(arg, atoi(space+1), atoi(space2+1));
|
||||
*space = ' ';
|
||||
*space2 = ' ';
|
||||
if (ret) {
|
||||
uwsgi_error("uwsgi_hook_chown2()/chown)");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int uwsgi_hook_hostname(char *arg) {
|
||||
#ifdef __CYGWIN__
|
||||
return -1;
|
||||
#else
|
||||
return sethostname(arg, strlen(arg));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static int uwsgi_hook_callint(char *arg) {
|
||||
char *space = strchr(arg, ' ');
|
||||
@@ -236,9 +448,20 @@ static int uwsgi_hook_callret(char *arg) {
|
||||
|
||||
void uwsgi_register_base_hooks() {
|
||||
uwsgi_register_hook("cd", uwsgi_hook_chdir);
|
||||
uwsgi_register_hook("chdir", uwsgi_hook_chdir);
|
||||
|
||||
uwsgi_register_hook("mkdir", uwsgi_hook_mkdir);
|
||||
uwsgi_register_hook("putenv", uwsgi_hook_putenv);
|
||||
uwsgi_register_hook("chmod", uwsgi_hook_chmod);
|
||||
uwsgi_register_hook("chown", uwsgi_hook_chown);
|
||||
uwsgi_register_hook("chown2", uwsgi_hook_chown2);
|
||||
|
||||
uwsgi_register_hook("exec", uwsgi_hook_exec);
|
||||
|
||||
uwsgi_register_hook("write", uwsgi_hook_write);
|
||||
uwsgi_register_hook("writen", uwsgi_hook_writen);
|
||||
uwsgi_register_hook("append", uwsgi_hook_append);
|
||||
uwsgi_register_hook("appendn", uwsgi_hook_appendn);
|
||||
uwsgi_register_hook("writefifo", uwsgi_hook_writefifo);
|
||||
uwsgi_register_hook("unlink", uwsgi_hook_unlink);
|
||||
|
||||
@@ -251,6 +474,8 @@ void uwsgi_register_base_hooks() {
|
||||
uwsgi_register_hook("callint", uwsgi_hook_callint);
|
||||
uwsgi_register_hook("callintret", uwsgi_hook_callintret);
|
||||
|
||||
uwsgi_register_hook("hostname", uwsgi_hook_hostname);
|
||||
|
||||
// for testing
|
||||
uwsgi_register_hook("exit", uwsgi_hook_exit);
|
||||
uwsgi_register_hook("print", uwsgi_hook_print);
|
||||
@@ -265,14 +490,26 @@ void uwsgi_hooks_run(struct uwsgi_string_list *l, char *phase, int fatal) {
|
||||
exit(1);
|
||||
}
|
||||
*colon = 0;
|
||||
struct uwsgi_hook *uh = uwsgi_hook_by_name(usl->value);
|
||||
int private = 0;
|
||||
char *action = usl->value;
|
||||
// private hook ?
|
||||
if (action[0] == '!') {
|
||||
action++;
|
||||
private = 1;
|
||||
}
|
||||
struct uwsgi_hook *uh = uwsgi_hook_by_name(action);
|
||||
if (!uh) {
|
||||
uwsgi_log("hook not found: %s\n", usl->value);
|
||||
uwsgi_log("hook action not found: %s\n", action);
|
||||
exit(1);
|
||||
}
|
||||
*colon = ':';
|
||||
|
||||
uwsgi_log("running \"%s\" (%s)...\n", usl->value, phase);
|
||||
if (private) {
|
||||
uwsgi_log("running --- PRIVATE HOOK --- (%s)...\n", phase);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("running \"%s\" (%s)...\n", usl->value, phase);
|
||||
}
|
||||
|
||||
int ret = uh->func(colon+1);
|
||||
if (fatal && ret != 0) {
|
||||
|
||||
+8
-8
@@ -117,11 +117,11 @@ void uwsgi_init_default() {
|
||||
uwsgi.max_vars = MAX_VARS;
|
||||
uwsgi.vec_size = 4 + 1 + (4 * MAX_VARS);
|
||||
|
||||
uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] = 4;
|
||||
uwsgi.shared->options[UWSGI_OPTION_LOGGING] = 1;
|
||||
uwsgi.socket_timeout = 4;
|
||||
uwsgi.logging_options.enabled = 1;
|
||||
|
||||
// a workers hould be running for at least 10 seconds
|
||||
uwsgi.shared->options[UWSGI_OPTION_MIN_WORKER_LIFETIME] = 10;
|
||||
uwsgi.min_worker_lifetime = 10;
|
||||
|
||||
uwsgi.shared->spooler_frequency = 30;
|
||||
|
||||
@@ -196,7 +196,7 @@ void uwsgi_setup_reload() {
|
||||
uwsgi.reloads++;
|
||||
//convert reloads to string
|
||||
int rlen = snprintf(env_reload_buf, 10, "%u", uwsgi.reloads);
|
||||
if (rlen > 0) {
|
||||
if (rlen > 0 && rlen < 10) {
|
||||
env_reload_buf[rlen] = 0;
|
||||
if (setenv("UWSGI_RELOADS", env_reload_buf, 1)) {
|
||||
uwsgi_error("setenv()");
|
||||
@@ -413,7 +413,7 @@ void sanitize_args() {
|
||||
uwsgi.cores = uwsgi.threads;
|
||||
}
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
if (!uwsgi.post_buffering) {
|
||||
uwsgi_log(" *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers *** \n");
|
||||
}
|
||||
@@ -449,13 +449,13 @@ void sanitize_args() {
|
||||
}
|
||||
}
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MAX_WORKER_LIFETIME] > 0 && uwsgi.shared->options[UWSGI_OPTION_MIN_WORKER_LIFETIME] >= uwsgi.shared->options[UWSGI_OPTION_MAX_WORKER_LIFETIME]) {
|
||||
if (uwsgi.max_worker_lifetime > 0 && uwsgi.min_worker_lifetime >= uwsgi.max_worker_lifetime) {
|
||||
uwsgi_log("invalid min-worker-lifetime value (%d), must be lower than max-worker-lifetime (%d)\n",
|
||||
uwsgi.shared->options[UWSGI_OPTION_MIN_WORKER_LIFETIME], uwsgi.shared->options[UWSGI_OPTION_MAX_WORKER_LIFETIME]);
|
||||
uwsgi.min_worker_lifetime, uwsgi.max_worker_lifetime);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (uwsgi.cheaper_rss_limit_soft && uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] != 1 && uwsgi.force_get_memusage != 1) {
|
||||
if (uwsgi.cheaper_rss_limit_soft && uwsgi.logging_options.memory_report != 1 && uwsgi.force_get_memusage != 1) {
|
||||
uwsgi_log("enabling cheaper-rss-limit-soft requires enabling also memory-report\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ int uwsgi_waitfd_event(int fd, int timeout, int event) {
|
||||
struct pollfd upoll;
|
||||
|
||||
if (!timeout)
|
||||
timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
timeout = uwsgi.socket_timeout;
|
||||
|
||||
timeout = timeout * 1000;
|
||||
if (timeout < 0)
|
||||
@@ -586,11 +586,13 @@ int *uwsgi_attach_fd(int fd, int *count_ptr, char *code, size_t code_len) {
|
||||
len = recvmsg(fd, &msg, 0);
|
||||
if (len <= 0) {
|
||||
uwsgi_error("recvmsg()");
|
||||
free(msg_control);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (code && code_len > 0) {
|
||||
if (uwsgi_strncmp(id, code_len, code, code_len)) {
|
||||
free(msg_control);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -606,15 +608,19 @@ int *uwsgi_attach_fd(int fd, int *count_ptr, char *code, size_t code_len) {
|
||||
}
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&msg);
|
||||
if (!cmsg)
|
||||
if (!cmsg) {
|
||||
free(msg_control);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
|
||||
free(msg_control);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((size_t) (cmsg->cmsg_len - ((char *) CMSG_DATA(cmsg) - (char *) cmsg)) > (size_t) (sizeof(int) * (count + 1))) {
|
||||
uwsgi_log("not enough space for sockets data, consider increasing it\n");
|
||||
free(msg_control);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1216,3 +1222,204 @@ void uwsgi_remap_fd(int fd, char *filename) {
|
||||
}
|
||||
}
|
||||
|
||||
int uwsgi_is_connected(int fd) {
|
||||
int soopt;
|
||||
socklen_t solen = sizeof(int);
|
||||
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
return 0;
|
||||
}
|
||||
/* is something bad ? */
|
||||
if (soopt) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int uwsgi_pass_cred(int fd, char *code, size_t code_len) {
|
||||
#ifdef SCM_CREDENTIALS
|
||||
struct msghdr cr_msg;
|
||||
struct cmsghdr *cmsg;
|
||||
struct iovec cr_iov;
|
||||
void *cr_msg_control = uwsgi_calloc(CMSG_SPACE(sizeof(struct ucred)));
|
||||
|
||||
cr_iov.iov_base = code;
|
||||
cr_iov.iov_len = code_len;
|
||||
|
||||
cr_msg.msg_name = NULL;
|
||||
cr_msg.msg_namelen = 0;
|
||||
|
||||
cr_msg.msg_iov = &cr_iov;
|
||||
cr_msg.msg_iovlen = 1;
|
||||
|
||||
cr_msg.msg_flags = 0;
|
||||
cr_msg.msg_control = cr_msg_control;
|
||||
cr_msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&cr_msg);
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_CREDENTIALS;
|
||||
|
||||
struct ucred *u = (struct ucred*) CMSG_DATA(cmsg);
|
||||
u->pid = getpid();
|
||||
u->uid = getuid();
|
||||
u->gid = getgid();
|
||||
|
||||
if (sendmsg(fd, &cr_msg, 0) < 0) {
|
||||
uwsgi_error("uwsgi_pass_cred()/sendmsg()");
|
||||
free(cr_msg_control);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(cr_msg_control);
|
||||
return 0;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int uwsgi_pass_cred2(int fd, char *code, size_t code_len, struct sockaddr *addr, size_t addr_len) {
|
||||
#ifdef SCM_CREDENTIALS
|
||||
struct msghdr cr_msg;
|
||||
struct cmsghdr *cmsg;
|
||||
struct iovec cr_iov;
|
||||
void *cr_msg_control = uwsgi_calloc(CMSG_SPACE(sizeof(struct ucred)));
|
||||
|
||||
cr_iov.iov_base = code;
|
||||
cr_iov.iov_len = code_len;
|
||||
|
||||
cr_msg.msg_name = addr;
|
||||
cr_msg.msg_namelen = addr_len;
|
||||
|
||||
cr_msg.msg_iov = &cr_iov;
|
||||
cr_msg.msg_iovlen = 1;
|
||||
|
||||
cr_msg.msg_flags = 0;
|
||||
cr_msg.msg_control = cr_msg_control;
|
||||
cr_msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&cr_msg);
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_CREDENTIALS;
|
||||
|
||||
struct ucred *u = (struct ucred*) CMSG_DATA(cmsg);
|
||||
u->pid = getpid();
|
||||
u->uid = getuid();
|
||||
u->gid = getgid();
|
||||
|
||||
if (sendmsg(fd, &cr_msg, 0) < 0) {
|
||||
uwsgi_error("uwsgi_pass_cred2()/sendmsg()");
|
||||
free(cr_msg_control);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(cr_msg_control);
|
||||
return 0;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int uwsgi_recv_cred(int fd, char *code, size_t code_len, pid_t *pid, uid_t *uid, gid_t *gid) {
|
||||
#ifdef SCM_CREDENTIALS
|
||||
struct iovec iov;
|
||||
int ret = -1;
|
||||
|
||||
void *msg_control = uwsgi_calloc(CMSG_SPACE(sizeof(struct ucred)));
|
||||
|
||||
iov.iov_base = uwsgi_malloc(code_len);
|
||||
iov.iov_len = code_len;
|
||||
|
||||
struct msghdr msg;
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
|
||||
msg.msg_name = NULL;
|
||||
msg.msg_namelen = 0;
|
||||
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
|
||||
msg.msg_control = msg_control;
|
||||
msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
|
||||
|
||||
ssize_t len = recvmsg(fd, &msg, 0);
|
||||
if (len <= 0) {
|
||||
uwsgi_error("uwsgi_recv_cred()/recvmsg()");
|
||||
goto clear;
|
||||
}
|
||||
|
||||
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
||||
if (!cmsg) goto clear;
|
||||
|
||||
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDENTIALS) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
if (uwsgi_strncmp(code, code_len, iov.iov_base, iov.iov_len)) goto clear;
|
||||
|
||||
struct ucred *u = (struct ucred *) CMSG_DATA(cmsg);
|
||||
*pid = u->pid;
|
||||
*uid = u->uid;
|
||||
*gid = u->gid;
|
||||
ret = 0;
|
||||
|
||||
clear:
|
||||
free(msg_control);
|
||||
free(iov.iov_base);
|
||||
return ret;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t uwsgi_recv_cred2(int fd, char *buf, size_t buf_len, pid_t *pid, uid_t *uid, gid_t *gid) {
|
||||
#ifdef SCM_CREDENTIALS
|
||||
struct iovec iov;
|
||||
ssize_t ret = -1;
|
||||
|
||||
void *msg_control = uwsgi_calloc(CMSG_SPACE(sizeof(struct ucred)));
|
||||
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = buf_len;
|
||||
|
||||
struct msghdr msg;
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
|
||||
msg.msg_name = NULL;
|
||||
msg.msg_namelen = 0;
|
||||
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
|
||||
msg.msg_control = msg_control;
|
||||
msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
|
||||
|
||||
ssize_t len = recvmsg(fd, &msg, 0);
|
||||
if (len <= 0) {
|
||||
uwsgi_error("uwsgi_recv_cred2()/recvmsg()");
|
||||
goto clear;
|
||||
}
|
||||
|
||||
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
||||
if (!cmsg) goto clear;
|
||||
|
||||
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDENTIALS) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
struct ucred *u = (struct ucred *) CMSG_DATA(cmsg);
|
||||
*pid = u->pid;
|
||||
*uid = u->uid;
|
||||
*gid = u->gid;
|
||||
ret = len;
|
||||
|
||||
clear:
|
||||
free(msg_control);
|
||||
return ret;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
+82
-4
@@ -4,13 +4,90 @@
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
#ifdef UWSGI_JSON_YAJL
|
||||
#include <yajl/yajl_tree.h>
|
||||
|
||||
void uwsgi_json_config(char *file, char *magic_table[]) {
|
||||
size_t len = 0;
|
||||
|
||||
char *object_asked = "uwsgi";
|
||||
char *colon;
|
||||
|
||||
if (uwsgi_check_scheme(file)) {
|
||||
colon = uwsgi_get_last_char(file, '/');
|
||||
colon = uwsgi_get_last_char(colon, ':');
|
||||
}
|
||||
else {
|
||||
colon = uwsgi_get_last_char(file, ':');
|
||||
}
|
||||
|
||||
if (colon) {
|
||||
colon[0] = 0;
|
||||
if (colon[1] != 0) {
|
||||
object_asked = colon + 1;
|
||||
}
|
||||
}
|
||||
|
||||
uwsgi_log_initial("[uWSGI] getting JSON configuration from %s\n", file);
|
||||
|
||||
char *json_data = uwsgi_open_and_read(file, &len, 1, magic_table);
|
||||
|
||||
char errbuf[1024];
|
||||
yajl_val node = yajl_tree_parse((const char *)json_data, errbuf, sizeof(errbuf));
|
||||
|
||||
if (!node) {
|
||||
uwsgi_log("error parsing JSON data: %s\n", errbuf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const char * path[] = { object_asked, NULL };
|
||||
yajl_val v = yajl_tree_get(node, path, yajl_t_any);
|
||||
if (!YAJL_IS_OBJECT(v)) {
|
||||
uwsgi_log("you must define a object named %s in your JSON data\n", object_asked);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
size_t i;
|
||||
for(i=0;i<v->u.object.len;i++) {
|
||||
char *key = (char *) v->u.object.keys[i];
|
||||
yajl_val o = v->u.object.values[i];
|
||||
if (YAJL_IS_STRING(o)) {
|
||||
add_exported_option(key, YAJL_GET_STRING(o) , 0);
|
||||
}
|
||||
else if (YAJL_IS_TRUE(o)) {
|
||||
add_exported_option(key, strdup("1"), 0);
|
||||
}
|
||||
else if (YAJL_IS_FALSE(o) || YAJL_IS_NULL(o)) {
|
||||
add_exported_option(key, strdup("0"), 0);
|
||||
}
|
||||
else if (YAJL_IS_NUMBER(o) || YAJL_IS_INTEGER(o)) {
|
||||
add_exported_option(key, YAJL_GET_NUMBER(o), 0);
|
||||
}
|
||||
else if (YAJL_IS_ARRAY(o)) {
|
||||
size_t j;
|
||||
for(j=0;j<o->u.array.len;j++) {
|
||||
yajl_val a_o = o->u.array.values[j];
|
||||
if (YAJL_IS_STRING(a_o)) {
|
||||
add_exported_option(key, YAJL_GET_STRING(a_o) , 0);
|
||||
}
|
||||
else if (YAJL_IS_TRUE(a_o)) {
|
||||
add_exported_option(key, strdup("1"), 0);
|
||||
}
|
||||
else if (YAJL_IS_FALSE(a_o) || YAJL_IS_NULL(a_o)) {
|
||||
add_exported_option(key, strdup("0"), 0);
|
||||
}
|
||||
else if (YAJL_IS_NUMBER(a_o) || YAJL_IS_INTEGER(a_o)) {
|
||||
add_exported_option(key, YAJL_GET_NUMBER(a_o), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
|
||||
/*
|
||||
I really love the jansson library...
|
||||
*/
|
||||
|
||||
void uwsgi_json_config(char *file, char *magic_table[]) {
|
||||
|
||||
size_t len = 0;
|
||||
@@ -111,3 +188,4 @@ void uwsgi_json_config(char *file, char *magic_table[]) {
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
+4
-1
@@ -737,6 +737,7 @@ void uwsgi_legion_add(struct uwsgi_legion *ul) {
|
||||
|
||||
int uwsgi_legion_announce(struct uwsgi_legion *ul) {
|
||||
struct uwsgi_buffer *ub = uwsgi_buffer_new(4096);
|
||||
unsigned char *encrypted = NULL;
|
||||
|
||||
if (uwsgi_buffer_append_keyval(ub, "legion", 6, ul->legion, ul->legion_len))
|
||||
goto err;
|
||||
@@ -769,7 +770,7 @@ int uwsgi_legion_announce(struct uwsgi_legion *ul) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
unsigned char *encrypted = uwsgi_malloc(ub->pos + 4 + EVP_MAX_BLOCK_LENGTH);
|
||||
encrypted = uwsgi_malloc(ub->pos + 4 + EVP_MAX_BLOCK_LENGTH);
|
||||
if (EVP_EncryptInit_ex(ul->encrypt_ctx, NULL, NULL, NULL, NULL) <= 0) {
|
||||
uwsgi_error("[uwsgi-legion] EVP_EncryptInit_ex()");
|
||||
goto err;
|
||||
@@ -808,6 +809,7 @@ int uwsgi_legion_announce(struct uwsgi_legion *ul) {
|
||||
return 0;
|
||||
err:
|
||||
uwsgi_buffer_destroy(ub);
|
||||
free(encrypted);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -824,6 +826,7 @@ void uwsgi_opt_legion_mcast(char *opt, char *value, void *foobar) {
|
||||
exit(1);
|
||||
}
|
||||
uwsgi_legion_register_node(ul, uwsgi_str(ul->addr));
|
||||
free(legion);
|
||||
}
|
||||
|
||||
void uwsgi_opt_legion_node(char *opt, char *value, void *foobar) {
|
||||
|
||||
+49
-23
@@ -119,6 +119,36 @@ void uwsgi_log_verbose(const char *fmt, ...) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
commodity function mainly useful in log rotation
|
||||
*/
|
||||
void uwsgi_logfile_write(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
char logpkt[4096];
|
||||
|
||||
struct timeval tv;
|
||||
char ctime_storage[26];
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
#if defined(__sun__) && !defined(__clang__)
|
||||
ctime_r((const time_t *) &tv.tv_sec, ctime_storage, 26);
|
||||
#else
|
||||
ctime_r((const time_t *) &tv.tv_sec, ctime_storage);
|
||||
#endif
|
||||
memcpy(logpkt, ctime_storage, 24);
|
||||
memcpy(logpkt + 24, " - ", 3);
|
||||
|
||||
int rlen = 24 + 3;
|
||||
|
||||
va_start(ap, fmt);
|
||||
rlen += vsnprintf(logpkt + rlen, 4096 - rlen, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
// do not check for errors
|
||||
rlen = write(uwsgi.original_log_fd, logpkt, rlen);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void fix_logpipe_buf(int *fd) {
|
||||
int so_bufsize;
|
||||
@@ -476,7 +506,6 @@ void uwsgi_check_logrotate(void) {
|
||||
}
|
||||
|
||||
void uwsgi_log_rotate() {
|
||||
char message[1024];
|
||||
if (!uwsgi.logfile) return;
|
||||
char *rot_name = uwsgi.log_backupname;
|
||||
int need_free = 0;
|
||||
@@ -486,24 +515,21 @@ void uwsgi_log_rotate() {
|
||||
free(ts_str);
|
||||
need_free = 1;
|
||||
}
|
||||
int ret = snprintf(message, 1024, "[%d] logsize: %llu, triggering rotation to %s...\n", (int) uwsgi_now(), (unsigned long long) uwsgi.shared->logsize, rot_name);
|
||||
if (ret > 0) {
|
||||
if (write(uwsgi.original_log_fd, message, ret) != ret) {
|
||||
// very probably this will never be printed
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
}
|
||||
// this will be rawly written to the logfile
|
||||
uwsgi_logfile_write("logsize: %llu, triggering rotation to %s...\n", (unsigned long long) uwsgi.shared->logsize, rot_name);
|
||||
if (rename(uwsgi.logfile, rot_name) == 0) {
|
||||
// reopen logfile dup'it and eventually gracefully reload workers;
|
||||
// reopen logfile and dup'it, on dup2 error, exit(1)
|
||||
int fd = open(uwsgi.logfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP);
|
||||
if (fd < 0) {
|
||||
// this will be written to the original file
|
||||
uwsgi_error_open(uwsgi.logfile);
|
||||
grace_them_all(0);
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
if (dup2(fd, uwsgi.original_log_fd) < 0) {
|
||||
uwsgi_error("dup2()");
|
||||
grace_them_all(0);
|
||||
// this could be lost :(
|
||||
uwsgi_error("uwsgi_log_rotate()/dup2()");
|
||||
exit(1);
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
@@ -519,7 +545,7 @@ void uwsgi_log_reopen() {
|
||||
char message[1024];
|
||||
if (!uwsgi.logfile) return;
|
||||
int ret = snprintf(message, 1024, "[%d] logsize: %llu, triggering log-reopen...\n", (int) uwsgi_now(), (unsigned long long) uwsgi.shared->logsize);
|
||||
if (ret > 0) {
|
||||
if (ret > 0 && ret < 1024) {
|
||||
if (write(uwsgi.original_log_fd, message, ret) != ret) {
|
||||
// very probably this will never be printed
|
||||
uwsgi_error("write()");
|
||||
@@ -535,7 +561,7 @@ void uwsgi_log_reopen() {
|
||||
return;
|
||||
}
|
||||
ret = snprintf(message, 1024, "[%d] %s reopened.\n", (int) uwsgi_now(), uwsgi.logfile);
|
||||
if (ret > 0) {
|
||||
if (ret > 0 && ret < 1024) {
|
||||
if (write(uwsgi.original_log_fd, message, ret) != ret) {
|
||||
// very probably this will never be printed
|
||||
uwsgi_error("write()");
|
||||
@@ -547,7 +573,7 @@ void uwsgi_log_reopen() {
|
||||
|
||||
void log_request(struct wsgi_request *wsgi_req) {
|
||||
|
||||
int log_it = uwsgi.shared->options[UWSGI_OPTION_LOGGING];
|
||||
int log_it = uwsgi.logging_options.enabled;
|
||||
|
||||
if (wsgi_req->do_not_log)
|
||||
return;
|
||||
@@ -557,25 +583,25 @@ void log_request(struct wsgi_request *wsgi_req) {
|
||||
}
|
||||
|
||||
/* conditional logging */
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOG_ZERO] && wsgi_req->response_size == 0) {
|
||||
if (uwsgi.logging_options.zero && wsgi_req->response_size == 0) {
|
||||
goto logit;
|
||||
}
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW] && (uint32_t) wsgi_req_time >= uwsgi.shared->options[UWSGI_OPTION_LOG_SLOW]) {
|
||||
if (uwsgi.logging_options.slow && (uint32_t) wsgi_req_time >= uwsgi.logging_options.slow) {
|
||||
goto logit;
|
||||
}
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOG_4xx] && (wsgi_req->status >= 400 && wsgi_req->status <= 499)) {
|
||||
if (uwsgi.logging_options._4xx && (wsgi_req->status >= 400 && wsgi_req->status <= 499)) {
|
||||
goto logit;
|
||||
}
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOG_5xx] && (wsgi_req->status >= 500 && wsgi_req->status <= 599)) {
|
||||
if (uwsgi.logging_options._5xx && (wsgi_req->status >= 500 && wsgi_req->status <= 599)) {
|
||||
goto logit;
|
||||
}
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOG_BIG] && (wsgi_req->response_size >= uwsgi.shared->options[UWSGI_OPTION_LOG_BIG])) {
|
||||
if (uwsgi.logging_options.big && (wsgi_req->response_size >= uwsgi.logging_options.big)) {
|
||||
goto logit;
|
||||
}
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOG_SENDFILE] && wsgi_req->via == UWSGI_VIA_SENDFILE) {
|
||||
if (uwsgi.logging_options.sendfile && wsgi_req->via == UWSGI_VIA_SENDFILE) {
|
||||
goto logit;
|
||||
}
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOG_IOERROR] && wsgi_req->read_errors > 0 && wsgi_req->write_errors > 0) {
|
||||
if (uwsgi.logging_options.ioerror && wsgi_req->read_errors > 0 && wsgi_req->write_errors > 0) {
|
||||
goto logit;
|
||||
}
|
||||
|
||||
@@ -660,7 +686,7 @@ void uwsgi_logit_simple(struct wsgi_request *wsgi_req) {
|
||||
logvecpos++;
|
||||
}
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] == 1) {
|
||||
if (uwsgi.logging_options.memory_report == 1) {
|
||||
rlen = snprintf(mempkt, 4096, "{address space usage: %lld bytes/%lluMB} {rss usage: %llu bytes/%lluMB} ", (unsigned long long) uwsgi.workers[uwsgi.mywid].vsz_size, (unsigned long long) uwsgi.workers[uwsgi.mywid].vsz_size / 1024 / 1024,
|
||||
(unsigned long long) uwsgi.workers[uwsgi.mywid].rss_size, (unsigned long long) uwsgi.workers[uwsgi.mywid].rss_size / 1024 / 1024);
|
||||
logvec[logvecpos].iov_base = mempkt;
|
||||
|
||||
+110
-73
@@ -168,53 +168,34 @@ void expire_rb_timeouts(struct uwsgi_rbtree *tree) {
|
||||
}
|
||||
}
|
||||
|
||||
int uwsgi_get_tcp_info(int fd) {
|
||||
static void get_tcp_info(struct uwsgi_socket *uwsgi_sock) {
|
||||
|
||||
#if defined(__linux__) || defined(__FreeBSD__)
|
||||
int fd = uwsgi_sock->fd;
|
||||
struct tcp_info ti;
|
||||
socklen_t tis = sizeof(struct tcp_info);
|
||||
|
||||
if (!getsockopt(fd, IPPROTO_TCP, TCP_INFO, &uwsgi.shared->ti, &tis)) {
|
||||
if (!getsockopt(fd, IPPROTO_TCP, TCP_INFO, &ti, &tis)) {
|
||||
|
||||
// checks for older kernels
|
||||
#if defined(__linux__)
|
||||
if (!uwsgi.shared->ti.tcpi_sacked) {
|
||||
if (!ti.tcpi_sacked) {
|
||||
#elif defined(__FreeBSD__)
|
||||
if (!uwsgi.shared->ti.__tcpi_sacked) {
|
||||
if (!ti.__tcpi_sacked) {
|
||||
#endif
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
uwsgi.shared->load = (uint64_t) uwsgi.shared->ti.tcpi_unacked;
|
||||
uwsgi.shared->max_load = (uint64_t) uwsgi.shared->ti.tcpi_sacked;
|
||||
uwsgi_sock->queue = (uint64_t) ti.tcpi_unacked;
|
||||
uwsgi_sock->max_queue = (uint64_t) ti.tcpi_sacked;
|
||||
#elif defined(__FreeBSD__)
|
||||
uwsgi.shared->load = (uint64_t) uwsgi.shared->ti.__tcpi_unacked;
|
||||
uwsgi.shared->max_load = (uint64_t) uwsgi.shared->ti.__tcpi_sacked;
|
||||
uwsgi_sock->queue = (uint64_t) ti.__tcpi_unacked;
|
||||
uwsgi_sock->max_queue = (uint64_t) ti.__tcpi_sacked;
|
||||
#endif
|
||||
|
||||
uwsgi.shared->options[UWSGI_OPTION_BACKLOG_STATUS] = uwsgi.shared->load;
|
||||
if (uwsgi.vassal_sos_backlog > 0 && uwsgi.has_emperor) {
|
||||
if (uwsgi.shared->load >= (uint64_t) uwsgi.vassal_sos_backlog) {
|
||||
// ask emperor for help
|
||||
char byte = 30;
|
||||
if (write(uwsgi.emperor_fd, &byte, 1) != 1) {
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("asking emperor for reinforcements (backlog: %llu)...\n", (unsigned long long) uwsgi.shared->load);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (uwsgi.shared->load >= uwsgi.shared->max_load) {
|
||||
uwsgi_log_verbose("*** uWSGI listen queue of socket %d full !!! (%llu/%llu) ***\n", fd, (unsigned long long) uwsgi.shared->load, (unsigned long long) uwsgi.shared->max_load);
|
||||
uwsgi.shared->options[UWSGI_OPTION_BACKLOG_ERRORS]++;
|
||||
}
|
||||
|
||||
return uwsgi.shared->load;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -227,25 +208,61 @@ int uwsgi_get_tcp_info(int fd) {
|
||||
|
||||
#ifdef SIOBKLGQ
|
||||
|
||||
int get_linux_unbit_SIOBKLGQ(int fd) {
|
||||
static void get_linux_unbit_SIOBKLGQ(struct uwsgi_socket *uwsgi_sock) {
|
||||
|
||||
int fd = uwsgi_sock->fd;
|
||||
int queue = 0;
|
||||
if (ioctl(fd, SIOBKLGQ, &queue) >= 0) {
|
||||
uwsgi.shared->load = queue;
|
||||
uwsgi.shared->options[UWSGI_OPTION_BACKLOG_STATUS] = queue;
|
||||
if (queue >= uwsgi.listen_queue) {
|
||||
uwsgi_log_verbose("*** uWSGI listen queue of socket %d full !!! (%d/%d) ***\n", fd, queue, uwsgi.listen_queue);
|
||||
uwsgi.shared->options[UWSGI_OPTION_BACKLOG_ERRORS]++;
|
||||
}
|
||||
return queue;
|
||||
uwsgi_sock->queue = (uint64_t) queue;
|
||||
uwsgi_sock->max_queue = (uint64_t) uwsgi.listen_queue;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static void master_check_listen_queue() {
|
||||
|
||||
uint64_t backlog = 0;
|
||||
struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
|
||||
while(uwsgi_sock) {
|
||||
if (uwsgi_sock->family == AF_INET) {
|
||||
get_tcp_info(uwsgi_sock);
|
||||
}
|
||||
#ifdef __linux__
|
||||
#ifdef SIOBKLGQ
|
||||
else if (uwsgi_sock->family == AF_UNIX) {
|
||||
get_linux_unbit_SIOBKLGQ(uwsgi_sock);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (uwsgi_sock->queue > backlog) {
|
||||
backlog = uwsgi_sock->queue;
|
||||
}
|
||||
if (uwsgi_sock->queue > 0 && uwsgi_sock->queue >= uwsgi_sock->max_queue) {
|
||||
uwsgi_log_verbose("*** uWSGI listen queue of socket \"%s\" (fd: %d) full !!! (%llu/%llu) ***\n", uwsgi_sock->name, uwsgi_sock->fd, (unsigned long long) uwsgi_sock->queue, (unsigned long long) uwsgi_sock->max_queue);
|
||||
}
|
||||
uwsgi_sock = uwsgi_sock->next;
|
||||
}
|
||||
|
||||
// TODO load should be something more advanced based on different values
|
||||
uwsgi.shared->load = backlog;
|
||||
|
||||
uwsgi.shared->backlog = backlog;
|
||||
|
||||
if (uwsgi.vassal_sos_backlog > 0 && uwsgi.has_emperor) {
|
||||
if (uwsgi.shared->backlog >= (uint64_t) uwsgi.vassal_sos_backlog) {
|
||||
// ask emperor for help
|
||||
char byte = 30;
|
||||
if (write(uwsgi.emperor_fd, &byte, 1) != 1) {
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
else {
|
||||
uwsgi_log_verbose("asking Emperor for reinforcements (backlog: %llu)...\n", (unsigned long long) uwsgi.shared->backlog);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int master_loop(char **argv, char **environ) {
|
||||
|
||||
@@ -358,6 +375,18 @@ int master_loop(char **argv, char **environ) {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
if (uwsgi.setns_socket) {
|
||||
uwsgi.setns_socket_fd = bind_to_unix(uwsgi.setns_socket, uwsgi.listen_queue, 0, 0);
|
||||
if (uwsgi.setns_socket_fd < 0) exit(1);
|
||||
if (chmod(uwsgi.setns_socket, S_IRUSR|S_IWUSR)) {
|
||||
uwsgi_error("[setns-socket] chmod()");
|
||||
exit(1);
|
||||
}
|
||||
event_queue_add_fd_read(uwsgi.master_queue, uwsgi.setns_socket_fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (uwsgi.zerg_server) {
|
||||
uwsgi.zerg_server_fd = bind_to_unix(uwsgi.zerg_server, uwsgi.listen_queue, 0, 0);
|
||||
event_queue_add_fd_read(uwsgi.master_queue, uwsgi.zerg_server_fd);
|
||||
@@ -471,6 +500,14 @@ int master_loop(char **argv, char **environ) {
|
||||
usl = usl->next;
|
||||
}
|
||||
uwsgi_check_touches(uwsgi.touch_signal);
|
||||
// daemon touches
|
||||
struct uwsgi_daemon *ud = uwsgi.daemons;
|
||||
while (ud) {
|
||||
if (ud->touch) {
|
||||
uwsgi_check_touches(ud->touch);
|
||||
}
|
||||
ud = ud->next;
|
||||
}
|
||||
|
||||
// fsmon
|
||||
uwsgi_fsmon_setup();
|
||||
@@ -578,10 +615,10 @@ int master_loop(char **argv, char **environ) {
|
||||
if (diedpid == 0) {
|
||||
|
||||
/* all processes ok, doing status scan after N seconds */
|
||||
check_interval = uwsgi.shared->options[UWSGI_OPTION_MASTER_INTERVAL];
|
||||
check_interval = uwsgi.master_interval;
|
||||
if (!check_interval) {
|
||||
check_interval = 1;
|
||||
uwsgi.shared->options[UWSGI_OPTION_MASTER_INTERVAL] = 1;
|
||||
uwsgi.master_interval = 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -680,43 +717,25 @@ int master_loop(char **argv, char **environ) {
|
||||
// check for idle
|
||||
uwsgi_master_check_idle();
|
||||
|
||||
check_interval = uwsgi.shared->options[UWSGI_OPTION_MASTER_INTERVAL];
|
||||
check_interval = uwsgi.master_interval;
|
||||
if (!check_interval) {
|
||||
check_interval = 1;
|
||||
uwsgi.shared->options[UWSGI_OPTION_MASTER_INTERVAL] = 1;
|
||||
uwsgi.master_interval = 1;
|
||||
}
|
||||
|
||||
|
||||
// get listen_queue status
|
||||
struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
|
||||
int tmp_queue = 0;
|
||||
while (uwsgi_sock) {
|
||||
if (uwsgi_sock->family == AF_INET) {
|
||||
uwsgi_sock->queue = uwsgi_get_tcp_info(uwsgi_sock->fd);
|
||||
}
|
||||
#ifdef __linux__
|
||||
#ifdef SIOBKLGQ
|
||||
else if (uwsgi_sock->family == AF_UNIX) {
|
||||
uwsgi_sock->queue = get_linux_unbit_SIOBKLGQ(uwsgi_sock->fd);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (uwsgi_sock->queue > tmp_queue) {
|
||||
tmp_queue = uwsgi_sock->queue;
|
||||
}
|
||||
uwsgi_sock = uwsgi_sock->next;
|
||||
}
|
||||
// fix queue size on multiple sockets
|
||||
uwsgi.shared->load = tmp_queue;
|
||||
// check listen_queue status
|
||||
master_check_listen_queue();
|
||||
|
||||
int someone_killed = 0;
|
||||
// check if some worker has to die (harakiri, evil checks...)
|
||||
uwsgi_master_check_workers_deadline();
|
||||
if (uwsgi_master_check_workers_deadline()) someone_killed++;
|
||||
if (uwsgi_master_check_gateways_deadline()) someone_killed++;
|
||||
if (uwsgi_master_check_mules_deadline()) someone_killed++;
|
||||
if (uwsgi_master_check_spoolers_deadline()) someone_killed++;
|
||||
if (uwsgi_master_check_crons_deadline()) someone_killed++;
|
||||
|
||||
uwsgi_master_check_gateways_deadline();
|
||||
uwsgi_master_check_mules_deadline();
|
||||
uwsgi_master_check_spoolers_deadline();
|
||||
uwsgi_master_check_crons_deadline();
|
||||
// this could trigger a complete exit...
|
||||
uwsgi_master_check_mountpoints();
|
||||
|
||||
#ifdef __linux__
|
||||
@@ -780,8 +799,26 @@ int master_loop(char **argv, char **environ) {
|
||||
uwsgi_route_signal(signum);
|
||||
uwsgi_log_verbose("[uwsgi-touch-signal] raising %u\n", signum);
|
||||
}
|
||||
|
||||
// daemon touches
|
||||
struct uwsgi_daemon *ud = uwsgi.daemons;
|
||||
while (ud) {
|
||||
if (ud->pid > 0 && ud->touch) {
|
||||
touched = uwsgi_check_touches(ud->touch);
|
||||
if (touched) {
|
||||
uwsgi_log_verbose("*** %s has been touched... reloading daemon \"%s\" (pid: %d) !!! ***\n", touched, ud->command, (int) ud->pid);
|
||||
if (kill(ud->pid, ud->stop_signal)) {
|
||||
uwsgi_error("[uwsgi-daemon/touch] kill()");
|
||||
}
|
||||
}
|
||||
}
|
||||
ud = ud->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// allows the KILL signal to be delivered;
|
||||
if (someone_killed > 0) sleep(1);
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
+30
-9
@@ -122,6 +122,8 @@ void uwsgi_master_check_idle() {
|
||||
}
|
||||
else {
|
||||
uwsgi.workers[i].pid = 0;
|
||||
uwsgi.workers[i].rss_size = 0;
|
||||
uwsgi.workers[i].vsz_size = 0;
|
||||
}
|
||||
}
|
||||
uwsgi_add_sockets_to_queue(uwsgi.master_queue, -1);
|
||||
@@ -131,19 +133,22 @@ void uwsgi_master_check_idle() {
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_master_check_workers_deadline() {
|
||||
int uwsgi_master_check_workers_deadline() {
|
||||
int i;
|
||||
int ret = 0;
|
||||
for (i = 1; i <= uwsgi.numproc; i++) {
|
||||
/* first check for harakiri */
|
||||
if (uwsgi.workers[i].harakiri > 0) {
|
||||
if (uwsgi.workers[i].harakiri < (time_t) uwsgi.current_time) {
|
||||
trigger_harakiri(i);
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
/* then user-defined harakiri */
|
||||
if (uwsgi.workers[i].user_harakiri > 0) {
|
||||
if (uwsgi.workers[i].user_harakiri < (time_t) uwsgi.current_time) {
|
||||
trigger_harakiri(i);
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
// then for evil memory checkers
|
||||
@@ -152,6 +157,7 @@ void uwsgi_master_check_workers_deadline() {
|
||||
uwsgi_log("*** EVIL RELOAD ON WORKER %d ADDRESS SPACE: %lld (pid: %d) ***\n", i, (long long) uwsgi.workers[i].vsz_size, uwsgi.workers[i].pid);
|
||||
kill(uwsgi.workers[i].pid, SIGKILL);
|
||||
uwsgi.workers[i].vsz_size = 0;
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
if (uwsgi.evil_reload_on_rss) {
|
||||
@@ -159,15 +165,17 @@ void uwsgi_master_check_workers_deadline() {
|
||||
uwsgi_log("*** EVIL RELOAD ON WORKER %d RSS: %lld (pid: %d) ***\n", i, (long long) uwsgi.workers[i].rss_size, uwsgi.workers[i].pid);
|
||||
kill(uwsgi.workers[i].pid, SIGKILL);
|
||||
uwsgi.workers[i].rss_size = 0;
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
// check if worker was running longer than allowed lifetime
|
||||
if (uwsgi.workers[i].pid > 0 && uwsgi.workers[i].cheaped == 0 && uwsgi.shared->options[UWSGI_OPTION_MAX_WORKER_LIFETIME] > 0) {
|
||||
if (uwsgi.workers[i].pid > 0 && uwsgi.workers[i].cheaped == 0 && uwsgi.max_worker_lifetime > 0) {
|
||||
uint64_t lifetime = uwsgi_now() - uwsgi.workers[i].last_spawn;
|
||||
if (lifetime > uwsgi.shared->options[UWSGI_OPTION_MAX_WORKER_LIFETIME] && uwsgi.workers[i].manage_next_request == 1) {
|
||||
if (lifetime > uwsgi.max_worker_lifetime && uwsgi.workers[i].manage_next_request == 1) {
|
||||
uwsgi_log("worker %d lifetime reached, it was running for %llu second(s)\n", i, (unsigned long long) lifetime);
|
||||
uwsgi.workers[i].manage_next_request = 0;
|
||||
kill(uwsgi.workers[i].pid, SIGWINCH);
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,29 +184,32 @@ void uwsgi_master_check_workers_deadline() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_master_check_gateways_deadline() {
|
||||
int uwsgi_master_check_gateways_deadline() {
|
||||
|
||||
int i;
|
||||
|
||||
int ret = 0;
|
||||
for (i = 0; i < ushared->gateways_cnt; i++) {
|
||||
if (ushared->gateways_harakiri[i] > 0) {
|
||||
if (ushared->gateways_harakiri[i] < (time_t) uwsgi.current_time) {
|
||||
if (ushared->gateways[i].pid > 0) {
|
||||
uwsgi_log("*** HARAKIRI ON GATEWAY %s %d (pid: %d) ***\n", ushared->gateways[i].name, ushared->gateways[i].num, ushared->gateways[i].pid);
|
||||
kill(ushared->gateways[i].pid, SIGKILL);
|
||||
ret = 1;
|
||||
}
|
||||
ushared->gateways_harakiri[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void uwsgi_master_check_mules_deadline() {
|
||||
int uwsgi_master_check_mules_deadline() {
|
||||
int i;
|
||||
int ret = 0;
|
||||
|
||||
for (i = 0; i < uwsgi.mules_cnt; i++) {
|
||||
if (uwsgi.mules[i].harakiri > 0) {
|
||||
@@ -206,6 +217,7 @@ void uwsgi_master_check_mules_deadline() {
|
||||
uwsgi_log("*** HARAKIRI ON MULE %d HANDLING SIGNAL %d (pid: %d) ***\n", i + 1, uwsgi.mules[i].signum, uwsgi.mules[i].pid);
|
||||
kill(uwsgi.mules[i].pid, SIGKILL);
|
||||
uwsgi.mules[i].harakiri = 0;
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
// user harakiri
|
||||
@@ -214,26 +226,32 @@ void uwsgi_master_check_mules_deadline() {
|
||||
uwsgi_log("*** HARAKIRI ON MULE %d (pid: %d) ***\n", i + 1, uwsgi.mules[i].pid);
|
||||
kill(uwsgi.mules[i].pid, SIGKILL);
|
||||
uwsgi.mules[i].user_harakiri = 0;
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void uwsgi_master_check_spoolers_deadline() {
|
||||
int uwsgi_master_check_spoolers_deadline() {
|
||||
int ret = 0;
|
||||
struct uwsgi_spooler *uspool = uwsgi.spoolers;
|
||||
while (uspool) {
|
||||
if (uspool->harakiri > 0 && uspool->harakiri < (time_t) uwsgi.current_time) {
|
||||
uwsgi_log("*** HARAKIRI ON THE SPOOLER (pid: %d) ***\n", uspool->pid);
|
||||
kill(uspool->pid, SIGKILL);
|
||||
uspool->harakiri = 0;
|
||||
ret = 1;
|
||||
}
|
||||
if (uspool->user_harakiri > 0 && uspool->user_harakiri < (time_t) uwsgi.current_time) {
|
||||
uwsgi_log("*** HARAKIRI ON THE SPOOLER (pid: %d) ***\n", uspool->pid);
|
||||
kill(uspool->pid, SIGKILL);
|
||||
uspool->user_harakiri = 0;
|
||||
ret = 1;
|
||||
}
|
||||
uspool = uspool->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -316,15 +334,18 @@ int uwsgi_master_check_cron_death(int diedpid) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void uwsgi_master_check_crons_deadline() {
|
||||
int uwsgi_master_check_crons_deadline() {
|
||||
int ret = 0;
|
||||
struct uwsgi_cron *uc = uwsgi.crons;
|
||||
while (uc) {
|
||||
if (uc->pid >= 0 && uc->harakiri > 0 && uc->harakiri < (time_t) uwsgi.current_time) {
|
||||
uwsgi_log("*** HARAKIRI ON CRON \"%s\" (pid: %d) ***\n", uc->command, uc->pid);
|
||||
kill(-uc->pid, SIGKILL);
|
||||
ret = 1;
|
||||
}
|
||||
uc = uc->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void uwsgi_master_check_mountpoints() {
|
||||
|
||||
@@ -51,6 +51,12 @@ int uwsgi_master_manage_events(int interesting_fd) {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
if (uwsgi.setns_socket && uwsgi.setns_socket_fd > -1 && interesting_fd == uwsgi.setns_socket_fd) {
|
||||
uwsgi_master_manage_setns(uwsgi.setns_socket_fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (uwsgi_fsmon_event(interesting_fd)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
+181
-13
@@ -101,7 +101,7 @@ int uwsgi_calc_cheaper(void) {
|
||||
|
||||
int i;
|
||||
static time_t last_check = 0;
|
||||
int check_interval = uwsgi.shared->options[UWSGI_OPTION_MASTER_INTERVAL];
|
||||
int check_interval = uwsgi.master_interval;
|
||||
|
||||
if (!last_check)
|
||||
last_check = uwsgi_now();
|
||||
@@ -186,6 +186,8 @@ int uwsgi_calc_cheaper(void) {
|
||||
uwsgi_log("worker %d should die...\n", oldest_worker);
|
||||
#endif
|
||||
uwsgi.workers[oldest_worker].cheaped = 1;
|
||||
uwsgi.workers[oldest_worker].rss_size = 0;
|
||||
uwsgi.workers[oldest_worker].vsz_size = 0;
|
||||
uwsgi.workers[oldest_worker].manage_next_request = 0;
|
||||
uwsgi_curse(oldest_worker, SIGWINCH);
|
||||
}
|
||||
@@ -330,7 +332,7 @@ int uwsgi_cheaper_algo_backlog(int can_spawn) {
|
||||
|
||||
int i;
|
||||
#ifdef __linux__
|
||||
int backlog = uwsgi.shared->options[UWSGI_OPTION_BACKLOG_STATUS];
|
||||
int backlog = uwsgi.shared->backlog;
|
||||
#else
|
||||
int backlog = 0;
|
||||
#endif
|
||||
@@ -383,14 +385,14 @@ void uwsgi_reload(char **argv) {
|
||||
// call master cleanup hooks
|
||||
uwsgi_master_cleanup_hooks();
|
||||
|
||||
// call atexit user exec
|
||||
uwsgi_exec_atexit();
|
||||
|
||||
if (uwsgi.exit_on_reload) {
|
||||
uwsgi_log("uWSGI: GAME OVER (insert coin)\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// call atexit user exec
|
||||
uwsgi_exec_atexit();
|
||||
|
||||
uwsgi_log("binary reloading uWSGI...\n");
|
||||
}
|
||||
else {
|
||||
@@ -584,6 +586,19 @@ void uwsgi_fixup_fds(int wid, int muleid, struct uwsgi_gateway *ug) {
|
||||
}
|
||||
|
||||
if (uwsgi.master_fifo_fd > -1) close(uwsgi.master_fifo_fd);
|
||||
|
||||
#ifdef __linux__
|
||||
for(i=0;i<uwsgi.setns_fds_count;i++) {
|
||||
close(uwsgi.setns_fds[i]);
|
||||
}
|
||||
#endif
|
||||
|
||||
// fd alarms
|
||||
struct uwsgi_alarm_fd *uafd = uwsgi.alarm_fds;
|
||||
while(uafd) {
|
||||
close(uafd->fd);
|
||||
uafd = uafd->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -713,9 +728,9 @@ struct uwsgi_stats *uwsgi_master_generate_stats() {
|
||||
goto end;
|
||||
|
||||
#ifdef __linux__
|
||||
if (uwsgi_stats_keylong_comma(us, "listen_queue", (unsigned long long) uwsgi.shared->options[UWSGI_OPTION_BACKLOG_STATUS]))
|
||||
if (uwsgi_stats_keylong_comma(us, "listen_queue", (unsigned long long) uwsgi.shared->backlog))
|
||||
goto end;
|
||||
if (uwsgi_stats_keylong_comma(us, "listen_queue_errors", (unsigned long long) uwsgi.shared->options[UWSGI_OPTION_BACKLOG_ERRORS]))
|
||||
if (uwsgi_stats_keylong_comma(us, "listen_queue_errors", (unsigned long long) uwsgi.shared->backlog_errors))
|
||||
goto end;
|
||||
#endif
|
||||
|
||||
@@ -957,6 +972,9 @@ struct uwsgi_stats *uwsgi_master_generate_stats() {
|
||||
if (uwsgi_stats_keylong_comma(us, "queue", (unsigned long long) uwsgi_sock->queue))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_keylong_comma(us, "max_queue", (unsigned long long) uwsgi_sock->max_queue))
|
||||
goto end;
|
||||
|
||||
if (uwsgi_stats_keylong_comma(us, "shared", (unsigned long long) uwsgi_sock->shared))
|
||||
goto end;
|
||||
|
||||
@@ -1080,7 +1098,7 @@ struct uwsgi_stats *uwsgi_master_generate_stats() {
|
||||
if (uwsgi_stats_keylong_comma(us, "exceptions", ua->exceptions))
|
||||
goto end;
|
||||
|
||||
if (ua->chdir) {
|
||||
if (*ua->chdir) {
|
||||
if (uwsgi_stats_keyval(us, "chdir", ua->chdir))
|
||||
goto end;
|
||||
}
|
||||
@@ -1453,7 +1471,7 @@ void uwsgi_register_cheaper_algo(char *name, int (*func) (int)) {
|
||||
|
||||
void trigger_harakiri(int i) {
|
||||
int j;
|
||||
uwsgi_log("*** HARAKIRI ON WORKER %d (pid: %d, try: %d) ***\n", i, uwsgi.workers[i].pid, uwsgi.workers[i].pending_harakiri + 1);
|
||||
uwsgi_log_verbose("*** HARAKIRI ON WORKER %d (pid: %d, try: %d) ***\n", i, uwsgi.workers[i].pid, uwsgi.workers[i].pending_harakiri + 1);
|
||||
if (uwsgi.harakiri_verbose) {
|
||||
#ifdef __linux__
|
||||
int proc_file;
|
||||
@@ -1501,15 +1519,12 @@ void trigger_harakiri(int i) {
|
||||
}
|
||||
}
|
||||
|
||||
kill(uwsgi.workers[i].pid, SIGUSR2);
|
||||
// allow SIGUSR2 to be delivered
|
||||
sleep(1);
|
||||
uwsgi_dump_worker(i, "HARAKIRI");
|
||||
kill(uwsgi.workers[i].pid, SIGKILL);
|
||||
if (!uwsgi.workers[i].pending_harakiri)
|
||||
uwsgi.workers[i].harakiri_count++;
|
||||
uwsgi.workers[i].pending_harakiri++;
|
||||
}
|
||||
// to avoid races
|
||||
|
||||
}
|
||||
|
||||
@@ -1649,6 +1664,8 @@ void uwsgi_go_cheap() {
|
||||
uwsgi.status.is_cheap = 1;
|
||||
for (i = 1; i <= uwsgi.numproc; i++) {
|
||||
uwsgi.workers[i].cheaped = 1;
|
||||
uwsgi.workers[i].rss_size = 0;
|
||||
uwsgi.workers[i].vsz_size = 0;
|
||||
if (uwsgi.workers[i].pid == 0)
|
||||
continue;
|
||||
uwsgi_log("killing worker %d (pid: %d)\n", i, (int) uwsgi.workers[i].pid);
|
||||
@@ -1661,3 +1678,154 @@ void uwsgi_go_cheap() {
|
||||
uwsgi_add_sockets_to_queue(uwsgi.master_queue, -1);
|
||||
uwsgi_log("cheap mode enabled: waiting for socket connection...\n");
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
void uwsgi_setns_preopen() {
|
||||
struct dirent *de;
|
||||
DIR *ns = opendir("/proc/self/ns");
|
||||
if (!ns) {
|
||||
uwsgi_error("uwsgi_setns_preopen()/opendir()");
|
||||
exit(1);
|
||||
}
|
||||
while ((de = readdir(ns)) != NULL) {
|
||||
if (strlen(de->d_name) > 0 && de->d_name[0] == '.') continue;
|
||||
struct uwsgi_string_list *usl = NULL;
|
||||
int found = 0;
|
||||
uwsgi_foreach(usl, uwsgi.setns_socket_skip) {
|
||||
if (!strcmp(de->d_name, usl->value)) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found) continue;
|
||||
char *filename = uwsgi_concat2("/proc/self/ns/", de->d_name);
|
||||
uwsgi.setns_fds[uwsgi.setns_fds_count] = open(filename, O_RDONLY);
|
||||
if (uwsgi.setns_fds[uwsgi.setns_fds_count] < 0) {
|
||||
uwsgi_error_open(filename);
|
||||
free(filename);
|
||||
exit(1);
|
||||
}
|
||||
free(filename);
|
||||
uwsgi.setns_fds_count++;
|
||||
}
|
||||
closedir(ns);
|
||||
}
|
||||
void uwsgi_master_manage_setns(int fd) {
|
||||
|
||||
struct sockaddr_un snsun;
|
||||
socklen_t snsun_len = sizeof(struct sockaddr_un);
|
||||
|
||||
int setns_client = accept(fd, (struct sockaddr *) &snsun, &snsun_len);
|
||||
if (setns_client < 0) {
|
||||
uwsgi_error("uwsgi_master_manage_setns()/accept()");
|
||||
return;
|
||||
}
|
||||
|
||||
int i;
|
||||
int tmp_fds[64];
|
||||
int *fds = tmp_fds;
|
||||
int num_fds = 0;
|
||||
|
||||
struct msghdr sn_msg;
|
||||
void *sn_msg_control;
|
||||
struct iovec sn_iov[2];
|
||||
struct cmsghdr *cmsg;
|
||||
DIR *ns = NULL;
|
||||
|
||||
if (uwsgi.setns_fds_count) {
|
||||
fds = uwsgi.setns_fds;
|
||||
num_fds = uwsgi.setns_fds_count;
|
||||
goto send;
|
||||
}
|
||||
|
||||
struct dirent *de;
|
||||
ns = opendir("/proc/self/ns");
|
||||
if (!ns) {
|
||||
close(setns_client);
|
||||
uwsgi_error("uwsgi_master_manage_setns()/opendir()");
|
||||
return;
|
||||
}
|
||||
while ((de = readdir(ns)) != NULL) {
|
||||
if (strlen(de->d_name) > 0 && de->d_name[0] == '.') continue;
|
||||
struct uwsgi_string_list *usl = NULL;
|
||||
int found = 0;
|
||||
uwsgi_foreach(usl, uwsgi.setns_socket_skip) {
|
||||
if (!strcmp(de->d_name, usl->value)) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found) continue;
|
||||
char *filename = uwsgi_concat2("/proc/self/ns/", de->d_name);
|
||||
fds[num_fds] = open(filename, O_RDONLY);
|
||||
if (fds[num_fds] < 0) {
|
||||
uwsgi_error_open(filename);
|
||||
free(filename);
|
||||
goto clear;
|
||||
}
|
||||
free(filename);
|
||||
num_fds++;
|
||||
}
|
||||
|
||||
send:
|
||||
|
||||
sn_msg_control = uwsgi_malloc(CMSG_SPACE(sizeof(int) * num_fds));
|
||||
|
||||
sn_iov[0].iov_base = "uwsgi-setns";
|
||||
sn_iov[0].iov_len = 11;
|
||||
sn_iov[1].iov_base = &num_fds;
|
||||
sn_iov[1].iov_len = sizeof(int);
|
||||
|
||||
sn_msg.msg_name = NULL;
|
||||
sn_msg.msg_namelen = 0;
|
||||
|
||||
sn_msg.msg_iov = sn_iov;
|
||||
sn_msg.msg_iovlen = 2;
|
||||
|
||||
sn_msg.msg_flags = 0;
|
||||
sn_msg.msg_control = sn_msg_control;
|
||||
sn_msg.msg_controllen = CMSG_SPACE(sizeof(int) * num_fds);
|
||||
|
||||
cmsg = CMSG_FIRSTHDR(&sn_msg);
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds);
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
|
||||
int *sn_fd_ptr = (int *) CMSG_DATA(cmsg);
|
||||
for(i=0;i<num_fds;i++) {
|
||||
sn_fd_ptr[i] = fds[i];
|
||||
}
|
||||
|
||||
if (sendmsg(setns_client, &sn_msg, 0) < 0) {
|
||||
uwsgi_error("uwsgi_master_manage_setns()/sendmsg()");
|
||||
}
|
||||
|
||||
free(sn_msg_control);
|
||||
|
||||
clear:
|
||||
close(setns_client);
|
||||
if (ns) {
|
||||
closedir(ns);
|
||||
for(i=0;i<num_fds;i++) {
|
||||
close(fds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
this is racey, but the worst thing would be printing garbage in the logs...
|
||||
*/
|
||||
void uwsgi_dump_worker(int wid, char *msg) {
|
||||
int i;
|
||||
uwsgi_log_verbose("%s !!! worker %d status !!!\n", msg, wid);
|
||||
for(i=0;i<uwsgi.cores;i++) {
|
||||
struct uwsgi_core *uc = &uwsgi.workers[wid].cores[i];
|
||||
struct wsgi_request *wsgi_req = &uc->req;
|
||||
if (uc->in_request) {
|
||||
uwsgi_log_verbose("%s [core %d] %.*s - %.*s %.*s since %llu\n", msg, i, wsgi_req->remote_addr_len, wsgi_req->remote_addr, wsgi_req->method_len, wsgi_req->method, wsgi_req->uri_len, wsgi_req->uri, (unsigned long long) (wsgi_req->start_of_request/(1000*1000)));
|
||||
}
|
||||
}
|
||||
uwsgi_log_verbose("%s !!! end of worker %d status !!!\n",msg, wid);
|
||||
}
|
||||
|
||||
+17
-9
@@ -465,7 +465,7 @@ static void *uwsgi_metrics_loop(void *arg) {
|
||||
if (uwsgi.metrics_dir && metric->map) {
|
||||
if (value != new_value) {
|
||||
int ret = snprintf(metric->map, uwsgi.page_size, "%lld\n", (long long) new_value);
|
||||
if (ret > 0) {
|
||||
if (ret > 0 && ret < uwsgi.page_size) {
|
||||
memset(metric->map+ret, 0, 4096-ret);
|
||||
}
|
||||
}
|
||||
@@ -694,11 +694,11 @@ int64_t uwsgi_metric_getn(char *name, size_t nlen, char *oid, size_t olen) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define uwsgi_metric_name(f, n) if (snprintf(buf, 4096, f, n) <= 1) { uwsgi_log("unable to register metric name %s\n", f); exit(1);}
|
||||
#define uwsgi_metric_name2(f, n, n2) if (snprintf(buf, 4096, f, n, n2) <= 1) { uwsgi_log("unable to register metric name %s\n", f); exit(1);}
|
||||
#define uwsgi_metric_name(f, n) ret = snprintf(buf, 4096, f, n); if (ret <= 1 || ret >= 4096) { uwsgi_log("unable to register metric name %s\n", f); exit(1);}
|
||||
#define uwsgi_metric_name2(f, n, n2) ret = snprintf(buf, 4096, f, n, n2); if (ret <= 1 || ret >= 4096) { uwsgi_log("unable to register metric name %s\n", f); exit(1);}
|
||||
|
||||
#define uwsgi_metric_oid(f, n) if (snprintf(buf2, 4096, f, n) <= 1) { uwsgi_log("unable to register metric oid %s\n", f); exit(1);}
|
||||
#define uwsgi_metric_oid2(f, n, n2) if (snprintf(buf2, 4096, f, n, n2) <= 1) { uwsgi_log("unable to register metric oid %s\n", f); exit(1);}
|
||||
#define uwsgi_metric_oid(f, n) ret = snprintf(buf2, 4096, f, n); if (ret <= 1 || ret >= 4096) { uwsgi_log("unable to register metric oid %s\n", f); exit(1);}
|
||||
#define uwsgi_metric_oid2(f, n, n2) ret = snprintf(buf2, 4096, f, n, n2); if (ret <= 1 || ret >= 4096) { uwsgi_log("unable to register metric oid %s\n", f); exit(1);}
|
||||
|
||||
void uwsgi_setup_metrics() {
|
||||
|
||||
@@ -732,6 +732,9 @@ void uwsgi_setup_metrics() {
|
||||
struct uwsgi_metric *total_tx = uwsgi_register_metric_do("core.total_tx", "5.100", UWSGI_METRIC_COUNTER, "sum", NULL, 0, NULL, 1);
|
||||
struct uwsgi_metric *total_rss = uwsgi_register_metric_do("core.total_rss", "5.101", UWSGI_METRIC_GAUGE, "sum", NULL, 0, NULL, 1);
|
||||
struct uwsgi_metric *total_vsz = uwsgi_register_metric_do("core.total_vsz", "5.102", UWSGI_METRIC_GAUGE, "sum", NULL, 0, NULL, 1);
|
||||
struct uwsgi_metric *total_avg_rt = uwsgi_register_metric_do("core.avg_response_time", "5.103", UWSGI_METRIC_GAUGE, "avg", NULL, 0, NULL, 1);
|
||||
|
||||
int ret;
|
||||
|
||||
// create the 'worker' namespace
|
||||
int i;
|
||||
@@ -750,19 +753,23 @@ void uwsgi_setup_metrics() {
|
||||
uwsgi_register_metric(buf, buf2, UWSGI_METRIC_COUNTER, "ptr", &uwsgi.workers[i].respawn_count, 0, NULL);
|
||||
|
||||
uwsgi_metric_name("worker.%d.avg_response_time", i) ; uwsgi_metric_oid("3.%d.8", i);
|
||||
uwsgi_register_metric(buf, buf2, UWSGI_METRIC_GAUGE, "ptr", &uwsgi.workers[i].avg_response_time, 0, NULL);
|
||||
struct uwsgi_metric *avg_rt = uwsgi_register_metric(buf, buf2, UWSGI_METRIC_GAUGE, "ptr", &uwsgi.workers[i].avg_response_time, 0, NULL);
|
||||
if (i > 0) uwsgi_metric_add_child(total_avg_rt, avg_rt);
|
||||
|
||||
uwsgi_metric_name("worker.%d.total_tx", i) ; uwsgi_metric_oid("3.%d.9", i);
|
||||
struct uwsgi_metric *tx = uwsgi_register_metric(buf, buf2, UWSGI_METRIC_COUNTER, "ptr", &uwsgi.workers[i].tx, 0, NULL);
|
||||
uwsgi_metric_add_child(total_tx, tx);
|
||||
if (i > 0) uwsgi_metric_add_child(total_tx, tx);
|
||||
|
||||
uwsgi_metric_name("worker.%d.rss_size", i) ; uwsgi_metric_oid("3.%d.11", i);
|
||||
struct uwsgi_metric *rss = uwsgi_register_metric(buf, buf2, UWSGI_METRIC_GAUGE, "ptr", &uwsgi.workers[i].rss_size, 0, NULL);
|
||||
uwsgi_metric_add_child(total_rss, rss);
|
||||
if (i > 0) uwsgi_metric_add_child(total_rss, rss);
|
||||
|
||||
uwsgi_metric_name("worker.%d.vsz_size", i) ; uwsgi_metric_oid("3.%d.12", i);
|
||||
struct uwsgi_metric *vsz = uwsgi_register_metric(buf, buf2, UWSGI_METRIC_GAUGE, "ptr", &uwsgi.workers[i].vsz_size, 0, NULL);
|
||||
uwsgi_metric_add_child(total_vsz, vsz);
|
||||
if (i > 0) uwsgi_metric_add_child(total_vsz, vsz);
|
||||
|
||||
// skip core metrics for core 0
|
||||
if (i == 0) continue;
|
||||
|
||||
int j;
|
||||
for(j=0;j<uwsgi.cores;j++) {
|
||||
@@ -794,6 +801,7 @@ void uwsgi_setup_metrics() {
|
||||
uwsgi_metric_append(total_tx);
|
||||
uwsgi_metric_append(total_rss);
|
||||
uwsgi_metric_append(total_vsz);
|
||||
uwsgi_metric_append(total_avg_rt);
|
||||
|
||||
// sockets
|
||||
struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
|
||||
|
||||
+11
-3
@@ -54,6 +54,9 @@ static struct uwsgi_mount_flag umflags[] = {
|
||||
#ifdef MS_REMOUNT
|
||||
{"remount", MS_REMOUNT},
|
||||
#endif
|
||||
#ifdef MS_NOSUID
|
||||
{"nosuid", MS_NOSUID},
|
||||
#endif
|
||||
#ifdef MS_SYNCHRONOUS
|
||||
{"sync", MS_SYNCHRONOUS},
|
||||
#endif
|
||||
@@ -102,7 +105,7 @@ uint64_t uwsgi_mount_flag(char *mflag) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uwsgi_mount(char *fs, char *what, char *where, char *flags) {
|
||||
int uwsgi_mount(char *fs, char *what, char *where, char *flags, char *data) {
|
||||
#if defined(__FreeBSD__) || defined(__GNU_kFreeBSD__)
|
||||
struct iovec iov[6];
|
||||
#endif
|
||||
@@ -121,7 +124,7 @@ int uwsgi_mount(char *fs, char *what, char *where, char *flags) {
|
||||
free(mflags);
|
||||
parsed:
|
||||
#ifdef __linux__
|
||||
return mount(what, where, fs, mountflags, NULL);
|
||||
return mount(what, where, fs, mountflags, data);
|
||||
#elif defined(__FreeBSD__) || defined(__GNU_kFreeBSD__)
|
||||
iov[0].iov_base = "fstype";
|
||||
iov[0].iov_len = 7;
|
||||
@@ -197,6 +200,7 @@ unmountable:
|
||||
}
|
||||
|
||||
int uwsgi_mount_hook(char *arg) {
|
||||
char *data = NULL;
|
||||
char *tmp_arg = uwsgi_str(arg);
|
||||
char *fs = tmp_arg;
|
||||
char *what = strchr(fs, ' ');
|
||||
@@ -208,8 +212,12 @@ int uwsgi_mount_hook(char *arg) {
|
||||
char *flags = strchr(where, ' ');
|
||||
if (flags) {
|
||||
*flags = 0; flags++;
|
||||
data = strchr(flags, ' ');
|
||||
if (data) {
|
||||
*data = 0; data++;
|
||||
}
|
||||
}
|
||||
if (uwsgi_mount(fs, what, where, flags)) {
|
||||
if (uwsgi_mount(fs, what, where, flags, data)) {
|
||||
uwsgi_error("uwsgi_mount()");
|
||||
free(tmp_arg);
|
||||
return -1;
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ int uwsgi_upload_progress_update(struct wsgi_request *wsgi_req, int fd, size_t r
|
||||
char buf[4096];
|
||||
|
||||
int ret = snprintf(buf, 4096, "{ \"state\" : \"uploading\", \"received\" : %llu, \"size\" : %llu }\r\n", (unsigned long long) (wsgi_req->post_cl - remains), (unsigned long long) wsgi_req->post_cl);
|
||||
if (ret < 0) {
|
||||
if (ret <= 0 || ret >= 4096) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
+27
-76
@@ -18,81 +18,6 @@ static size_t get_content_length(char *buf, uint16_t size) {
|
||||
}
|
||||
|
||||
|
||||
ssize_t send_udp_message(uint8_t modifier1, uint8_t modifier2, char *host, char *message, uint16_t message_size) {
|
||||
|
||||
int fd;
|
||||
struct sockaddr_in udp_addr;
|
||||
struct sockaddr_un un_addr;
|
||||
char *udp_port;
|
||||
ssize_t ret;
|
||||
|
||||
struct uwsgi_header *uh;
|
||||
|
||||
udp_port = strchr(host, ':');
|
||||
if (udp_port) {
|
||||
udp_port[0] = 0;
|
||||
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&udp_addr, 0, sizeof(struct sockaddr_in));
|
||||
udp_addr.sin_family = AF_INET;
|
||||
udp_addr.sin_port = htons(atoi(udp_port + 1));
|
||||
udp_addr.sin_addr.s_addr = inet_addr(host);
|
||||
}
|
||||
else {
|
||||
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&un_addr, 0, sizeof(struct sockaddr_un));
|
||||
un_addr.sun_family = AF_UNIX;
|
||||
// use 102 as the magic number
|
||||
strncat(un_addr.sun_path, host, 102);
|
||||
|
||||
}
|
||||
|
||||
if (message) {
|
||||
uh = (struct uwsgi_header *) message;
|
||||
}
|
||||
else {
|
||||
uh = (struct uwsgi_header *) uwsgi_malloc(4);
|
||||
}
|
||||
|
||||
uh->modifier1 = modifier1;
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uh->pktsize = uwsgi_swap16(message_size);
|
||||
#else
|
||||
uh->pktsize = message_size;
|
||||
#endif
|
||||
uh->modifier2 = modifier2;
|
||||
|
||||
if (udp_port) {
|
||||
ret = sendto(fd, (char *) uh, message_size + 4, 0, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
|
||||
udp_port[0] = ':';
|
||||
}
|
||||
else {
|
||||
ret = sendto(fd, (char *) uh, message_size + 4, 0, (struct sockaddr *) &un_addr, sizeof(un_addr));
|
||||
}
|
||||
if (ret < 0) {
|
||||
uwsgi_error("send_udp_message()/sendto()");
|
||||
}
|
||||
close(fd);
|
||||
|
||||
if ((char *) uh != message) {
|
||||
free(uh);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int uwsgi_read_response(int fd, struct uwsgi_header *uh, int timeout, char **buf) {
|
||||
|
||||
char *ptr = (char *) uh;
|
||||
@@ -412,6 +337,12 @@ static int uwsgi_proto_check_11(struct wsgi_request *wsgi_req, char *key, char *
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!uwsgi_proto_key("HTTP_ORIGIN", 11)) {
|
||||
wsgi_req->http_origin = buf;
|
||||
wsgi_req->http_origin_len = len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -563,7 +494,7 @@ static int uwsgi_proto_check_18(struct wsgi_request *wsgi_req, char *key, char *
|
||||
|
||||
|
||||
static int uwsgi_proto_check_20(struct wsgi_request *wsgi_req, char *key, char *buf, uint16_t len) {
|
||||
if (uwsgi.log_x_forwarded_for && !uwsgi_proto_key("HTTP_X_FORWARDED_FOR", 20)) {
|
||||
if (uwsgi.logging_options.log_x_forwarded_for && !uwsgi_proto_key("HTTP_X_FORWARDED_FOR", 20)) {
|
||||
wsgi_req->remote_addr = buf;
|
||||
wsgi_req->remote_addr_len = len;
|
||||
return 0;
|
||||
@@ -589,9 +520,28 @@ static int uwsgi_proto_check_22(struct wsgi_request *wsgi_req, char *key, char *
|
||||
wsgi_req->if_modified_since_len = len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!uwsgi_proto_key("HTTP_SEC_WEBSOCKET_KEY", 22)) {
|
||||
wsgi_req->http_sec_websocket_key = buf;
|
||||
wsgi_req->http_sec_websocket_key_len = len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_proto_check_27(struct wsgi_request *wsgi_req, char *key, char *buf, uint16_t len) {
|
||||
|
||||
if (!uwsgi_proto_key("HTTP_SEC_WEBSOCKET_PROTOCOL", 27)) {
|
||||
wsgi_req->http_sec_websocket_protocol = buf;
|
||||
wsgi_req->http_sec_websocket_protocol_len = len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_proto_hooks_setup() {
|
||||
int i = 0;
|
||||
for(i=0;i<UWSGI_PROTO_MAX_CHECK;i++) {
|
||||
@@ -609,6 +559,7 @@ void uwsgi_proto_hooks_setup() {
|
||||
uwsgi.proto_hooks[18] = uwsgi_proto_check_18;
|
||||
uwsgi.proto_hooks[20] = uwsgi_proto_check_20;
|
||||
uwsgi.proto_hooks[22] = uwsgi_proto_check_22;
|
||||
uwsgi.proto_hooks[27] = uwsgi_proto_check_27;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-4
@@ -476,8 +476,8 @@ int uwsgi_postbuffer_do_in_mem(struct wsgi_request *wsgi_req) {
|
||||
char *ptr = wsgi_req->post_buffering_buf;
|
||||
|
||||
while (remains > 0) {
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
inc_harakiri(uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
inc_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
ssize_t rlen = wsgi_req->socket->proto_read_body(wsgi_req, ptr, remains);
|
||||
@@ -550,8 +550,8 @@ int uwsgi_postbuffer_do_in_disk(struct wsgi_request *wsgi_req) {
|
||||
while (post_remains > 0) {
|
||||
|
||||
// during post buffering we need to constantly reset the harakiri
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
inc_harakiri(uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
inc_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
// we use the already available post buffering buffer to read chunks....
|
||||
|
||||
+2
-2
@@ -705,7 +705,7 @@ static int uwsgi_router_flush(struct uwsgi_route *ur, char *arg) {
|
||||
static int transform_fixcl(struct wsgi_request *wsgi_req, struct uwsgi_transformation *ut) {
|
||||
char buf[sizeof(UMAX64_STR)+1];
|
||||
int ret = snprintf(buf, sizeof(UMAX64_STR)+1, "%llu", (unsigned long long) ut->chunk->pos);
|
||||
if (ret <= 0 || ret > (int) (sizeof(UMAX64_STR)+1)) {
|
||||
if (ret <= 0 || ret >= (int) (sizeof(UMAX64_STR)+1)) {
|
||||
wsgi_req->write_errors++;
|
||||
return -1;
|
||||
}
|
||||
@@ -1656,7 +1656,7 @@ static char *uwsgi_route_var_uwsgi(struct wsgi_request *wsgi_req, char *key, uin
|
||||
}
|
||||
|
||||
else if (!uwsgi_strncmp(key, keylen, "lq", 2)) {
|
||||
ret = uwsgi_num2str(uwsgi.shared->load);
|
||||
ret = uwsgi_num2str(uwsgi.shared->backlog);
|
||||
*vallen = strlen(ret);
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -117,7 +117,7 @@ char *uwsgi_do_rpc(char *node, char *func, uint8_t argc, char *argv[], uint16_t
|
||||
return NULL;
|
||||
|
||||
// wait for connection;
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
@@ -156,14 +156,14 @@ char *uwsgi_do_rpc(char *node, char *func, uint8_t argc, char *argv[], uint16_t
|
||||
}
|
||||
|
||||
// ok the request is ready, let's send it in non blocking way
|
||||
if (uwsgi_write_true_nb(fd, buffer, buffer_size+4, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_write_true_nb(fd, buffer, buffer_size+4, uwsgi.socket_timeout)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// ok time to wait for the response in non blocking way
|
||||
size_t rlen = buffer_size+4;
|
||||
uint8_t modifier2 = 0;
|
||||
if (uwsgi_read_with_realloc(fd, &buffer, &rlen, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], NULL, &modifier2)) {
|
||||
if (uwsgi_read_with_realloc(fd, &buffer, &rlen, uwsgi.socket_timeout, NULL, &modifier2)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ char *uwsgi_do_rpc(char *node, char *func, uint8_t argc, char *argv[], uint16_t
|
||||
rlen = content_len;
|
||||
|
||||
// read the raw value from the socket
|
||||
if (uwsgi_read_whole_true_nb(fd, buffer, rlen, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_read_whole_true_nb(fd, buffer, rlen, uwsgi.socket_timeout)) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
+9
-9
@@ -45,21 +45,21 @@ int uwsgi_signal_handler(uint8_t sig) {
|
||||
uwsgi.workers[uwsgi.mywid].sig = 1;
|
||||
uwsgi.workers[uwsgi.mywid].signum = sig;
|
||||
uwsgi.workers[uwsgi.mywid].signals++;
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
}
|
||||
else if (uwsgi.muleid > 0) {
|
||||
uwsgi.mules[uwsgi.muleid - 1].sig = 1;
|
||||
uwsgi.mules[uwsgi.muleid - 1].signum = sig;
|
||||
uwsgi.mules[uwsgi.muleid - 1].signals++;
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MULE_HARAKIRI] > 0) {
|
||||
set_mule_harakiri(uwsgi.shared->options[UWSGI_OPTION_MULE_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.mules > 0) {
|
||||
set_mule_harakiri(uwsgi.harakiri_options.mules);
|
||||
}
|
||||
}
|
||||
else if (uwsgi.i_am_a_spooler && (getpid() == uwsgi.i_am_a_spooler->pid)) {
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI] > 0) {
|
||||
set_spooler_harakiri(uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.spoolers > 0) {
|
||||
set_spooler_harakiri(uwsgi.harakiri_options.spoolers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ int uwsgi_signal_handler(uint8_t sig) {
|
||||
}
|
||||
}
|
||||
else if (uwsgi.i_am_a_spooler && (getpid() == uwsgi.i_am_a_spooler->pid)) {
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI] > 0) {
|
||||
if (uwsgi.harakiri_options.spoolers > 0) {
|
||||
set_spooler_harakiri(0);
|
||||
}
|
||||
}
|
||||
@@ -269,7 +269,7 @@ int uwsgi_remote_signal_send(char *addr, uint8_t sig) {
|
||||
uh.pktsize = 0;
|
||||
uh.modifier2 = sig;
|
||||
|
||||
int fd = uwsgi_connect(addr, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0);
|
||||
int fd = uwsgi_connect(addr, uwsgi.socket_timeout, 0);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
@@ -279,7 +279,7 @@ int uwsgi_remote_signal_send(char *addr, uint8_t sig) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = uwsgi_read_response(fd, &uh, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], NULL);
|
||||
int ret = uwsgi_read_response(fd, &uh, uwsgi.socket_timeout, NULL);
|
||||
|
||||
close(fd);
|
||||
return ret;
|
||||
|
||||
+14
-1
@@ -750,7 +750,7 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
|
||||
if (!uwsgi.no_defer_accept) {
|
||||
|
||||
#ifdef __linux__
|
||||
if (setsockopt(serverfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], sizeof(int))) {
|
||||
if (setsockopt(serverfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &uwsgi.socket_timeout, sizeof(int))) {
|
||||
uwsgi_error("TCP_DEFER_ACCEPT setsockopt()");
|
||||
}
|
||||
// OSX has no SO_ACCEPTFILTER !!!
|
||||
@@ -1928,6 +1928,19 @@ found:
|
||||
return up;
|
||||
}
|
||||
|
||||
int uwsgi_socket_passcred(int fd) {
|
||||
#ifdef SO_PASSCRED
|
||||
int optval = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) < 0) {
|
||||
uwsgi_error("uwsgi_socket_passcred()/setsockopt()");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void uwsgi_protocols_register() {
|
||||
uwsgi_register_protocol("uwsgi", uwsgi_proto_uwsgi_setup);
|
||||
uwsgi_register_protocol("puwsgi", uwsgi_proto_puwsgi_setup);
|
||||
|
||||
+3
-3
@@ -552,11 +552,11 @@ void spooler_manage_task(struct uwsgi_spooler *uspool, char *dir, char *task) {
|
||||
for (i = 0; i < 256; i++) {
|
||||
if (uwsgi.p[i]->spooler) {
|
||||
time_t now = uwsgi_now();
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI] > 0) {
|
||||
set_spooler_harakiri(uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.spoolers > 0) {
|
||||
set_spooler_harakiri(uwsgi.harakiri_options.spoolers);
|
||||
}
|
||||
ret = uwsgi.p[i]->spooler(task, spool_buf, uh.pktsize, body, body_len);
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI] > 0) {
|
||||
if (uwsgi.harakiri_options.spoolers > 0) {
|
||||
set_spooler_harakiri(0);
|
||||
}
|
||||
if (ret == 0)
|
||||
|
||||
+31
-1
@@ -132,7 +132,6 @@ static int uwsgi_sni_cb(SSL *ssl, int *ad, void *arg) {
|
||||
free(sni_dir_key);
|
||||
free(sni_dir_client_ca);
|
||||
SSL_set_SSL_CTX(ssl, usl->custom_ptr);
|
||||
uwsgi_log("[uwsgi-sni for pid %d] added context for %s\n", (int) getpid(), servername);
|
||||
return SSL_TLSEXT_ERR_OK;
|
||||
}
|
||||
done:
|
||||
@@ -511,10 +510,41 @@ struct uwsgi_string_list *uwsgi_ssl_add_sni_item(char *name, char *crt, char *ke
|
||||
SSL_CTX *ctx = uwsgi_ssl_new_server_context(name, crt, key, ciphers, client_ca);
|
||||
if (!ctx) {
|
||||
uwsgi_log("[uwsgi-ssl] DANGER unable to initialize context for \"%s\"\n", name);
|
||||
free(name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct uwsgi_string_list *usl = uwsgi_string_new_list(&uwsgi.sni, name);
|
||||
usl->custom_ptr = ctx;
|
||||
// mark it as dynamic
|
||||
usl->custom = 1;
|
||||
uwsgi_log_verbose("[uwsgi-sni for pid %d] added SSL context for %s\n", (int) getpid(), name);
|
||||
return usl;
|
||||
}
|
||||
|
||||
void uwsgi_ssl_del_sni_item(char *name, uint16_t name_len) {
|
||||
struct uwsgi_string_list *usl = NULL, *last_sni = NULL, *sni_item = NULL;
|
||||
uwsgi_foreach(usl, uwsgi.sni) {
|
||||
if (!uwsgi_strncmp(usl->value, usl->len, name, name_len) && usl->custom) {
|
||||
sni_item = usl;
|
||||
break;
|
||||
}
|
||||
last_sni = usl;
|
||||
}
|
||||
|
||||
if (!sni_item) return;
|
||||
|
||||
if (last_sni) {
|
||||
last_sni->next = sni_item->next;
|
||||
}
|
||||
else {
|
||||
uwsgi.sni = sni_item->next;
|
||||
}
|
||||
|
||||
// we are free to destroy it as no more clients are using it
|
||||
SSL_CTX_free((SSL_CTX *) sni_item->custom_ptr);
|
||||
free(sni_item->value);
|
||||
free(sni_item);
|
||||
|
||||
uwsgi_log_verbose("[uwsgi-sni for pid %d] destroyed SSL context for %s\n",(int) getpid(), name);
|
||||
}
|
||||
|
||||
+2
-1
@@ -450,7 +450,8 @@ int uwsgi_real_file_serve(struct wsgi_request *wsgi_req, char *real_filename, si
|
||||
if (wsgi_req->if_modified_since_len) {
|
||||
time_t ims = parse_http_date(wsgi_req->if_modified_since, wsgi_req->if_modified_since_len);
|
||||
if (st->st_mtime <= ims) {
|
||||
uwsgi_response_prepare_headers(wsgi_req, "304 Not Modified", 16);
|
||||
if (uwsgi_response_prepare_headers(wsgi_req, "304 Not Modified", 16))
|
||||
return -1;
|
||||
return uwsgi_response_write_headers_do(wsgi_req);
|
||||
}
|
||||
}
|
||||
|
||||
+15
-15
@@ -111,7 +111,7 @@ int uwsgi_stats_keyval(struct uwsgi_stats *us, char *key, char *value) {
|
||||
size_t available = watermark - ptr;
|
||||
|
||||
int ret = snprintf(ptr, available, "\"%s\":\"%s\"", key, value);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
while (ret >= (int) available) {
|
||||
char *new_base = realloc(us->base, us->size + us->chunk);
|
||||
@@ -123,7 +123,7 @@ int uwsgi_stats_keyval(struct uwsgi_stats *us, char *key, char *value) {
|
||||
watermark = us->base + us->size;
|
||||
available = watermark - ptr;
|
||||
ret = snprintf(ptr, available, "\"%s\":\"%s\"", key, value);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ int uwsgi_stats_keyvalnum(struct uwsgi_stats *us, char *key, char *value, unsign
|
||||
size_t available = watermark - ptr;
|
||||
|
||||
int ret = snprintf(ptr, available, "\"%s\":\"%s%llu\"", key, value, num);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
while (ret >= (int) available) {
|
||||
char *new_base = realloc(us->base, us->size + us->chunk);
|
||||
@@ -161,7 +161,7 @@ int uwsgi_stats_keyvalnum(struct uwsgi_stats *us, char *key, char *value, unsign
|
||||
watermark = us->base + us->size;
|
||||
available = watermark - ptr;
|
||||
ret = snprintf(ptr, available, "\"%s\":\"%s%llu\"", key, value, num);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -188,7 +188,7 @@ int uwsgi_stats_keyvaln(struct uwsgi_stats *us, char *key, char *value, int vall
|
||||
size_t available = watermark - ptr;
|
||||
|
||||
int ret = snprintf(ptr, available, "\"%s\":\"%.*s\"", key, vallen, value);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
while (ret >= (int) available) {
|
||||
char *new_base = realloc(us->base, us->size + us->chunk);
|
||||
@@ -200,7 +200,7 @@ int uwsgi_stats_keyvaln(struct uwsgi_stats *us, char *key, char *value, int vall
|
||||
watermark = us->base + us->size;
|
||||
available = watermark - ptr;
|
||||
ret = snprintf(ptr, available, "\"%s\":\"%.*s\"", key, vallen, value);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ int uwsgi_stats_key(struct uwsgi_stats *us, char *key) {
|
||||
size_t available = watermark - ptr;
|
||||
|
||||
int ret = snprintf(ptr, available, "\"%s\":", key);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
while (ret >= (int) available) {
|
||||
char *new_base = realloc(us->base, us->size + us->chunk);
|
||||
@@ -239,7 +239,7 @@ int uwsgi_stats_key(struct uwsgi_stats *us, char *key) {
|
||||
watermark = us->base + us->size;
|
||||
available = watermark - ptr;
|
||||
ret = snprintf(ptr, available, "\"%s\":", key);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ int uwsgi_stats_str(struct uwsgi_stats *us, char *str) {
|
||||
size_t available = watermark - ptr;
|
||||
|
||||
int ret = snprintf(ptr, available, "\"%s\"", str);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
while (ret >= (int) available) {
|
||||
char *new_base = realloc(us->base, us->size + us->chunk);
|
||||
@@ -266,7 +266,7 @@ int uwsgi_stats_str(struct uwsgi_stats *us, char *str) {
|
||||
watermark = us->base + us->size;
|
||||
available = watermark - ptr;
|
||||
ret = snprintf(ptr, available, "\"%s\"", str);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -287,7 +287,7 @@ int uwsgi_stats_keylong(struct uwsgi_stats *us, char *key, unsigned long long nu
|
||||
size_t available = watermark - ptr;
|
||||
|
||||
int ret = snprintf(ptr, available, "\"%s\":%llu", key, num);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
while (ret >= (int) available) {
|
||||
char *new_base = realloc(us->base, us->size + us->chunk);
|
||||
@@ -299,7 +299,7 @@ int uwsgi_stats_keylong(struct uwsgi_stats *us, char *key, unsigned long long nu
|
||||
watermark = us->base + us->size;
|
||||
available = watermark - ptr;
|
||||
ret = snprintf(ptr, available, "\"%s\":%llu", key, num);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -325,7 +325,7 @@ int uwsgi_stats_keyslong(struct uwsgi_stats *us, char *key, long long num) {
|
||||
size_t available = watermark - ptr;
|
||||
|
||||
int ret = snprintf(ptr, available, "\"%s\":%lld", key, num);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
while (ret >= (int) available) {
|
||||
char *new_base = realloc(us->base, us->size + us->chunk);
|
||||
@@ -337,7 +337,7 @@ int uwsgi_stats_keyslong(struct uwsgi_stats *us, char *key, long long num) {
|
||||
watermark = us->base + us->size;
|
||||
available = watermark - ptr;
|
||||
ret = snprintf(ptr, available, "\"%s\":%lld", key, num);
|
||||
if (ret < 0)
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -379,7 +379,7 @@ void uwsgi_send_stats(int fd, struct uwsgi_stats *(*func) (void)) {
|
||||
size_t remains = us->pos;
|
||||
off_t pos = 0;
|
||||
while (remains > 0) {
|
||||
int ret = uwsgi_waitfd_write(client_fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi_waitfd_write(client_fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
goto end0;
|
||||
}
|
||||
|
||||
+163
-12
@@ -20,6 +20,51 @@
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
#ifdef UWSGI_SSL
|
||||
static void uwsgi_subscription_sni_check(struct uwsgi_subscribe_slot *current_slot, struct uwsgi_subscribe_req *usr) {
|
||||
if (usr->sni_key_len > 0 && usr->sni_crt_len > 0) {
|
||||
if (!current_slot->sni_enabled) {
|
||||
char *sni_key = uwsgi_concat2n(usr->sni_key, usr->sni_key_len, "", 0);
|
||||
char *sni_crt = uwsgi_concat2n(usr->sni_crt, usr->sni_crt_len, "", 0);
|
||||
char *sni_ca = NULL;
|
||||
if (usr->sni_ca_len > 0) {
|
||||
sni_ca = uwsgi_concat2n(usr->sni_ca, usr->sni_ca_len, "", 0);
|
||||
}
|
||||
char *servername = NULL;
|
||||
char *colon = memchr(current_slot->key, ':', current_slot->keylen);
|
||||
if (colon) {
|
||||
servername = uwsgi_concat2n(current_slot->key, colon-current_slot->key, "", 0);
|
||||
}
|
||||
else {
|
||||
servername = uwsgi_concat2n(current_slot->key, current_slot->keylen, "", 0);
|
||||
}
|
||||
if (uwsgi_ssl_add_sni_item(servername, sni_crt, sni_key, uwsgi.sni_dir_ciphers , sni_ca)) {
|
||||
current_slot->sni_enabled = 1;
|
||||
}
|
||||
if (sni_key) free(sni_key);
|
||||
if (sni_crt) free(sni_crt);
|
||||
if (sni_ca) free(sni_ca);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int uwsgi_subscription_credentials_check(struct uwsgi_subscribe_slot *slot, struct uwsgi_subscribe_req *usr) {
|
||||
struct uwsgi_string_list *usl = NULL;
|
||||
uwsgi_foreach(usl, uwsgi.subscriptions_credentials_check_dir) {
|
||||
char *filename = uwsgi_concat2n(usl->value, usl->len, slot->key, slot->keylen);
|
||||
struct stat st;
|
||||
int ret = stat(filename, &st);
|
||||
free(filename);
|
||||
if (ret != 0) continue;
|
||||
if (st.st_uid != usr->uid) continue;
|
||||
if (st.st_gid != usr->gid) continue;
|
||||
// accepted...
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct uwsgi_subscribe_slot *uwsgi_get_subscribe_slot(struct uwsgi_subscribe_slot **slot, char *key, uint16_t keylen) {
|
||||
|
||||
if (keylen > 0xff)
|
||||
@@ -305,6 +350,12 @@ int uwsgi_remove_subscribe_node(struct uwsgi_subscribe_slot **slot, struct uwsgi
|
||||
EVP_PKEY_free(node_slot->sign_public_key);
|
||||
EVP_MD_CTX_destroy(node_slot->sign_ctx);
|
||||
}
|
||||
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
|
||||
// if there is a SNI context active, destroy it
|
||||
if (node_slot->sni_enabled) {
|
||||
uwsgi_ssl_del_sni_item(node_slot->key, node_slot->keylen);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
free(node_slot);
|
||||
slot[hash_key] = NULL;
|
||||
@@ -358,6 +409,11 @@ struct uwsgi_subscribe_node *uwsgi_add_subscribe_node(struct uwsgi_subscribe_slo
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (uwsgi.subscriptions_credentials_check_dir && !uwsgi_subscription_credentials_check(current_slot, usr)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node = current_slot->nodes;
|
||||
while (node) {
|
||||
if (!uwsgi_strncmp(node->name, node->len, usr->address, usr->address_len)) {
|
||||
@@ -388,6 +444,8 @@ struct uwsgi_subscribe_node *uwsgi_add_subscribe_node(struct uwsgi_subscribe_slo
|
||||
uwsgi_log("[uwsgi-subscription for pid %d] invalid (sniffed ?) packet sent for slot: %.*s node: %.*s unix_check: %lu\n", (int) uwsgi.mypid, usr->keylen, usr->key, usr->address_len, usr->address, (unsigned long) usr->unix_check);
|
||||
return NULL;
|
||||
}
|
||||
// check here as we are sure the node will be added
|
||||
uwsgi_subscription_sni_check(current_slot, usr);
|
||||
#endif
|
||||
|
||||
node = uwsgi_malloc(sizeof(struct uwsgi_subscribe_node));
|
||||
@@ -396,7 +454,8 @@ struct uwsgi_subscribe_node *uwsgi_add_subscribe_node(struct uwsgi_subscribe_slo
|
||||
node->modifier2 = usr->modifier2;
|
||||
node->requests = 0;
|
||||
node->last_requests = 0;
|
||||
node->transferred = 0;
|
||||
node->tx = 0;
|
||||
node->rx = 0;
|
||||
node->reference = 0;
|
||||
node->death_mark = 0;
|
||||
node->failcnt = 0;
|
||||
@@ -407,6 +466,9 @@ struct uwsgi_subscribe_node *uwsgi_add_subscribe_node(struct uwsgi_subscribe_slo
|
||||
if (!node->weight)
|
||||
node->weight = 1;
|
||||
node->wrr = 0;
|
||||
node->pid = usr->pid;
|
||||
node->uid = usr->uid;
|
||||
node->gid = usr->gid;
|
||||
node->last_check = uwsgi_now();
|
||||
node->slot = current_slot;
|
||||
memcpy(node->name, usr->address, usr->address_len);
|
||||
@@ -464,16 +526,27 @@ struct uwsgi_subscribe_node *uwsgi_add_subscribe_node(struct uwsgi_subscribe_slo
|
||||
#endif
|
||||
current_slot->keylen = usr->keylen;
|
||||
memcpy(current_slot->key, usr->key, usr->keylen);
|
||||
if (uwsgi.subscriptions_credentials_check_dir) {
|
||||
if (!uwsgi_subscription_credentials_check(current_slot, usr)) {
|
||||
free(current_slot);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
current_slot->key[usr->keylen] = 0;
|
||||
current_slot->hits = 0;
|
||||
|
||||
#ifdef UWSGI_SSL
|
||||
current_slot->sni_enabled = 0;
|
||||
uwsgi_subscription_sni_check(current_slot, usr);
|
||||
#endif
|
||||
current_slot->nodes = uwsgi_malloc(sizeof(struct uwsgi_subscribe_node));
|
||||
current_slot->nodes->slot = current_slot;
|
||||
current_slot->nodes->len = usr->address_len;
|
||||
current_slot->nodes->reference = 0;
|
||||
current_slot->nodes->requests = 0;
|
||||
current_slot->nodes->last_requests = 0;
|
||||
current_slot->nodes->transferred = 0;
|
||||
current_slot->nodes->tx = 0;
|
||||
current_slot->nodes->rx = 0;
|
||||
current_slot->nodes->death_mark = 0;
|
||||
current_slot->nodes->failcnt = 0;
|
||||
current_slot->nodes->modifier1 = usr->modifier1;
|
||||
@@ -485,6 +558,9 @@ struct uwsgi_subscribe_node *uwsgi_add_subscribe_node(struct uwsgi_subscribe_slo
|
||||
if (!current_slot->nodes->weight)
|
||||
current_slot->nodes->weight = 1;
|
||||
current_slot->nodes->wrr = 0;
|
||||
current_slot->nodes->pid = usr->pid;
|
||||
current_slot->nodes->uid = usr->uid;
|
||||
current_slot->nodes->gid = usr->gid;
|
||||
memcpy(current_slot->nodes->name, usr->address, usr->address_len);
|
||||
current_slot->nodes->last_check = uwsgi_now();
|
||||
|
||||
@@ -516,8 +592,64 @@ struct uwsgi_subscribe_node *uwsgi_add_subscribe_node(struct uwsgi_subscribe_slo
|
||||
|
||||
}
|
||||
|
||||
static void send_subscription(char *host, char *message, uint16_t message_size) {
|
||||
|
||||
void uwsgi_send_subscription(char *udp_address, char *key, size_t keysize, uint8_t modifier1, uint8_t modifier2, uint8_t cmd, char *socket_name, char *sign) {
|
||||
int fd;
|
||||
struct sockaddr_in udp_addr;
|
||||
struct sockaddr_un un_addr;
|
||||
ssize_t ret;
|
||||
|
||||
char *udp_port = strchr(host, ':');
|
||||
if (udp_port) {
|
||||
udp_port[0] = 0;
|
||||
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
uwsgi_error("send_subscription()/socket()");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&udp_addr, 0, sizeof(struct sockaddr_in));
|
||||
udp_addr.sin_family = AF_INET;
|
||||
udp_addr.sin_port = htons(atoi(udp_port + 1));
|
||||
udp_addr.sin_addr.s_addr = inet_addr(host);
|
||||
}
|
||||
else {
|
||||
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
uwsgi_error("send_subscription()/socket()");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&un_addr, 0, sizeof(struct sockaddr_un));
|
||||
un_addr.sun_family = AF_UNIX;
|
||||
// use 102 as the magic number
|
||||
strncat(un_addr.sun_path, host, 102);
|
||||
|
||||
}
|
||||
|
||||
if (udp_port) {
|
||||
ret = sendto(fd, message, message_size, 0, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
|
||||
udp_port[0] = ':';
|
||||
}
|
||||
else {
|
||||
if (uwsgi.subscriptions_use_credentials) {
|
||||
// could be useless as internally the socket could add them automagically
|
||||
ret = uwsgi_pass_cred2(fd, message, message_size, (struct sockaddr *) &un_addr, sizeof(un_addr));
|
||||
}
|
||||
else {
|
||||
ret = sendto(fd, message, message_size, 0, (struct sockaddr *) &un_addr, sizeof(un_addr));
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
uwsgi_error("send_subscription()/sendto()");
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_send_subscription(char *udp_address, char *key, size_t keysize, uint8_t modifier1, uint8_t modifier2, uint8_t cmd, char *socket_name, char *sign, char *sni_key, char *sni_crt, char *sni_ca) {
|
||||
|
||||
if (socket_name == NULL && !uwsgi.sockets)
|
||||
return;
|
||||
@@ -560,8 +692,22 @@ void uwsgi_send_subscription(char *udp_address, char *key, size_t keysize, uint8
|
||||
}
|
||||
#endif
|
||||
|
||||
if (sni_key) {
|
||||
if (uwsgi_buffer_append_keyval(ub, "sni_key", 7, sni_key, strlen(sni_key))) goto end;
|
||||
}
|
||||
|
||||
send_udp_message(224, cmd, udp_address, ub->buf, ub->pos - 4);
|
||||
if (sni_crt) {
|
||||
if (uwsgi_buffer_append_keyval(ub, "sni_crt", 7, sni_crt, strlen(sni_crt))) goto end;
|
||||
}
|
||||
|
||||
if (sni_ca) {
|
||||
if (uwsgi_buffer_append_keyval(ub, "sni_ca", 6, sni_ca, strlen(sni_ca))) goto end;
|
||||
}
|
||||
|
||||
// add uwsgi header
|
||||
if (uwsgi_buffer_set_uh(ub, 224, cmd)) goto end;
|
||||
|
||||
send_subscription(udp_address, ub->buf, ub->pos);
|
||||
end:
|
||||
uwsgi_buffer_destroy(ub);
|
||||
}
|
||||
@@ -679,7 +825,7 @@ void uwsgi_subscribe(char *subscription, uint8_t cmd) {
|
||||
modifier1_len = strlen(modifier1);
|
||||
keysize = strlen(key);
|
||||
}
|
||||
uwsgi_send_subscription(udp_address, key, keysize, uwsgi_str_num(modifier1, modifier1_len), 0, cmd, socket_name, sign);
|
||||
uwsgi_send_subscription(udp_address, key, keysize, uwsgi_str_num(modifier1, modifier1_len), 0, cmd, socket_name, sign, NULL, NULL, NULL);
|
||||
modifier1 = NULL;
|
||||
modifier1_len = 0;
|
||||
}
|
||||
@@ -697,7 +843,7 @@ void uwsgi_subscribe(char *subscription, uint8_t cmd) {
|
||||
modifier1_len = strlen(modifier1);
|
||||
keysize = strlen(key);
|
||||
}
|
||||
uwsgi_send_subscription(udp_address, key, keysize, uwsgi_str_num(modifier1, modifier1_len), 0, cmd, socket_name, sign);
|
||||
uwsgi_send_subscription(udp_address, key, keysize, uwsgi_str_num(modifier1, modifier1_len), 0, cmd, socket_name, sign, NULL, NULL, NULL);
|
||||
modifier1 = NULL;
|
||||
modifier1_len = 0;
|
||||
lines[i] = '\n';
|
||||
@@ -709,9 +855,8 @@ void uwsgi_subscribe(char *subscription, uint8_t cmd) {
|
||||
}
|
||||
keysize++;
|
||||
}
|
||||
|
||||
free(lines);
|
||||
}
|
||||
free(lines);
|
||||
}
|
||||
else {
|
||||
modifier1 = strchr(subscription_key + 1, ',');
|
||||
@@ -727,7 +872,7 @@ void uwsgi_subscribe(char *subscription, uint8_t cmd) {
|
||||
modifier1_len = strlen(modifier1);
|
||||
}
|
||||
|
||||
uwsgi_send_subscription(udp_address, subscription_key + 1, strlen(subscription_key + 1), uwsgi_str_num(modifier1, modifier1_len), 0, cmd, socket_name, sign);
|
||||
uwsgi_send_subscription(udp_address, subscription_key + 1, strlen(subscription_key + 1), uwsgi_str_num(modifier1, modifier1_len), 0, cmd, socket_name, sign, NULL, NULL, NULL);
|
||||
if (modifier1)
|
||||
modifier1[-1] = ',';
|
||||
if (sign)
|
||||
@@ -752,6 +897,9 @@ void uwsgi_subscribe2(char *arg, uint8_t cmd) {
|
||||
char *s2_modifier1 = NULL;
|
||||
char *s2_modifier2 = NULL;
|
||||
char *s2_check = NULL;
|
||||
char *s2_sni_key = NULL;
|
||||
char *s2_sni_crt = NULL;
|
||||
char *s2_sni_ca = NULL;
|
||||
|
||||
if (uwsgi_kvlist_parse(arg, strlen(arg), ',', '=',
|
||||
"server", &s2_server,
|
||||
@@ -763,6 +911,9 @@ void uwsgi_subscribe2(char *arg, uint8_t cmd) {
|
||||
"modifier2", &s2_modifier2,
|
||||
"sign", &s2_sign,
|
||||
"check", &s2_check,
|
||||
"sni_key", &s2_sni_key,
|
||||
"sni_crt", &s2_sni_crt,
|
||||
"sni_ca", &s2_sni_ca,
|
||||
NULL)) {
|
||||
return;
|
||||
}
|
||||
@@ -798,7 +949,7 @@ void uwsgi_subscribe2(char *arg, uint8_t cmd) {
|
||||
modifier2 = atoi(s2_modifier2);
|
||||
}
|
||||
|
||||
uwsgi_send_subscription(s2_server, s2_key, strlen(s2_key), modifier1, modifier2, cmd, s2_addr, s2_sign);
|
||||
uwsgi_send_subscription(s2_server, s2_key, strlen(s2_key), modifier1, modifier2, cmd, s2_addr, s2_sign, s2_sni_key, s2_sni_crt, s2_sni_ca);
|
||||
end:
|
||||
if (s2_server) free(s2_server);
|
||||
if (s2_key) free(s2_key);
|
||||
@@ -813,7 +964,7 @@ end:
|
||||
|
||||
void uwsgi_subscribe_all(uint8_t cmd, int verbose) {
|
||||
|
||||
if (!uwsgi.subscriptions_blocked) return;
|
||||
if (uwsgi.subscriptions_blocked) return;
|
||||
// -- subscribe
|
||||
struct uwsgi_string_list *subscriptions = uwsgi.subscriptions;
|
||||
while (subscriptions) {
|
||||
|
||||
+272
-195
@@ -47,7 +47,7 @@ void inc_harakiri(int sec) {
|
||||
uwsgi.workers[uwsgi.mywid].harakiri += sec;
|
||||
}
|
||||
else {
|
||||
alarm(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] + sec);
|
||||
alarm(uwsgi.harakiri_options.workers + sec);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +206,8 @@ void uwsgi_set_cgroup() {
|
||||
if (!uwsgi.cgroup)
|
||||
return;
|
||||
|
||||
if (getuid()) return;
|
||||
if (getuid())
|
||||
return;
|
||||
|
||||
usl = uwsgi.cgroup;
|
||||
|
||||
@@ -278,41 +279,41 @@ void uwsgi_set_cgroup() {
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_CAP
|
||||
void uwsgi_apply_cap(cap_value_t *cap, int caps_count) {
|
||||
cap_value_t minimal_cap_values[] = { CAP_SYS_CHROOT, CAP_SETUID, CAP_SETGID, CAP_SETPCAP };
|
||||
void uwsgi_apply_cap(cap_value_t * cap, int caps_count) {
|
||||
cap_value_t minimal_cap_values[] = { CAP_SYS_CHROOT, CAP_SETUID, CAP_SETGID, CAP_SETPCAP };
|
||||
|
||||
cap_t caps = cap_init();
|
||||
cap_t caps = cap_init();
|
||||
|
||||
if (!caps) {
|
||||
uwsgi_error("cap_init()");
|
||||
exit(1);
|
||||
}
|
||||
cap_clear(caps);
|
||||
if (!caps) {
|
||||
uwsgi_error("cap_init()");
|
||||
exit(1);
|
||||
}
|
||||
cap_clear(caps);
|
||||
|
||||
cap_set_flag(caps, CAP_EFFECTIVE, 4, minimal_cap_values, CAP_SET);
|
||||
cap_set_flag(caps, CAP_EFFECTIVE, 4, minimal_cap_values, CAP_SET);
|
||||
|
||||
cap_set_flag(caps, CAP_PERMITTED, 4, minimal_cap_values, CAP_SET);
|
||||
cap_set_flag(caps, CAP_PERMITTED, caps_count, cap, CAP_SET);
|
||||
cap_set_flag(caps, CAP_PERMITTED, 4, minimal_cap_values, CAP_SET);
|
||||
cap_set_flag(caps, CAP_PERMITTED, caps_count, cap, CAP_SET);
|
||||
|
||||
cap_set_flag(caps, CAP_INHERITABLE, caps_count, cap, CAP_SET);
|
||||
cap_set_flag(caps, CAP_INHERITABLE, caps_count, cap, CAP_SET);
|
||||
|
||||
if (cap_set_proc(caps) < 0) {
|
||||
uwsgi_error("cap_set_proc()");
|
||||
exit(1);
|
||||
}
|
||||
cap_free(caps);
|
||||
if (cap_set_proc(caps) < 0) {
|
||||
uwsgi_error("cap_set_proc()");
|
||||
exit(1);
|
||||
}
|
||||
cap_free(caps);
|
||||
|
||||
#ifdef __linux__
|
||||
#ifdef SECBIT_KEEP_CAPS
|
||||
if (prctl(SECBIT_KEEP_CAPS, 1, 0, 0, 0) < 0) {
|
||||
uwsgi_error("prctl()");
|
||||
exit(1);
|
||||
}
|
||||
if (prctl(SECBIT_KEEP_CAPS, 1, 0, 0, 0) < 0) {
|
||||
uwsgi_error("prctl()");
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
|
||||
uwsgi_error("prctl()");
|
||||
exit(1);
|
||||
}
|
||||
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
|
||||
uwsgi_error("prctl()");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
@@ -517,7 +518,7 @@ void uwsgi_as_root() {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (in_jail) {
|
||||
if (in_jail || uwsgi.jailed) {
|
||||
uwsgi_hooks_run(uwsgi.hook_post_jail, "post-jail", 1);
|
||||
struct uwsgi_string_list *usl = NULL;
|
||||
uwsgi_foreach(usl, uwsgi.mount_post_jail) {
|
||||
@@ -587,7 +588,7 @@ void uwsgi_as_root() {
|
||||
exit(1);
|
||||
}
|
||||
#ifdef __linux__
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG]) {
|
||||
if (uwsgi.logging_options.memory_report) {
|
||||
uwsgi_log("*** Warning, on linux system you have to bind-mount the /proc fs in your chroot to get memory debug/report.\n");
|
||||
}
|
||||
#endif
|
||||
@@ -603,10 +604,10 @@ void uwsgi_as_root() {
|
||||
}
|
||||
*space = 0;
|
||||
#if defined(MS_REC) && defined(MS_PRIVATE)
|
||||
if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL)) {
|
||||
uwsgi_error("mount()");
|
||||
exit(1);
|
||||
}
|
||||
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
|
||||
uwsgi_error("mount()");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
if (chdir(arg)) {
|
||||
uwsgi_error("pivot_root()/chdir()");
|
||||
@@ -619,7 +620,7 @@ void uwsgi_as_root() {
|
||||
uwsgi_error("pivot_root()");
|
||||
exit(1);
|
||||
}
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG]) {
|
||||
if (uwsgi.logging_options.memory_report) {
|
||||
uwsgi_log("*** Warning, on linux system you have to bind-mount the /proc fs in your chroot to get memory debug/report.\n");
|
||||
}
|
||||
free(arg);
|
||||
@@ -632,25 +633,25 @@ void uwsgi_as_root() {
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) && !defined(OBSOLETE_LINUX_KERNEL)
|
||||
if (uwsgi.unshare2 && !uwsgi.reloads) {
|
||||
if (uwsgi.unshare2 && !uwsgi.reloads) {
|
||||
|
||||
if (unshare(uwsgi.unshare2)) {
|
||||
uwsgi_error("unshare()");
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("[linux-namespace] applied unshare() mask: %d\n", uwsgi.unshare2);
|
||||
}
|
||||
if (unshare(uwsgi.unshare2)) {
|
||||
uwsgi_error("unshare()");
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("[linux-namespace] applied unshare() mask: %d\n", uwsgi.unshare2);
|
||||
}
|
||||
#ifdef CLONE_NEWUSER
|
||||
if (uwsgi.unshare2 & CLONE_NEWUSER) {
|
||||
if (setuid(0)) {
|
||||
uwsgi_error("uwsgi_as_root()/setuid(0)");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (uwsgi.unshare2 & CLONE_NEWUSER) {
|
||||
if (setuid(0)) {
|
||||
uwsgi_error("uwsgi_as_root()/setuid(0)");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
in_jail = 1;
|
||||
}
|
||||
in_jail = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (uwsgi.refork_as_root) {
|
||||
@@ -1027,7 +1028,7 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
|
||||
uwsgi.workers[uwsgi.mywid].avg_response_time = (uwsgi.workers[uwsgi.mywid].avg_response_time + tmp_rt) / 2;
|
||||
|
||||
// get memory usage
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] == 1 || uwsgi.force_get_memusage) {
|
||||
if (uwsgi.logging_options.memory_report == 1 || uwsgi.force_get_memusage) {
|
||||
get_memusage(&rss, &vsz);
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = vsz;
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = rss;
|
||||
@@ -1077,7 +1078,7 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
|
||||
// after_request custom hooks
|
||||
struct uwsgi_string_list *usl = NULL;
|
||||
uwsgi_foreach(usl, uwsgi.after_request_hooks) {
|
||||
void (*func)(struct wsgi_request *) = (void (*)(struct wsgi_request *)) usl->custom_ptr;
|
||||
void (*func) (struct wsgi_request *) = (void (*)(struct wsgi_request *)) usl->custom_ptr;
|
||||
func(wsgi_req);
|
||||
}
|
||||
|
||||
@@ -1107,7 +1108,7 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
|
||||
}
|
||||
|
||||
// defunct process reaper
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_REAPER] == 1) {
|
||||
if (uwsgi.reaper == 1) {
|
||||
while (waitpid(WAIT_ANY, &waitpid_status, WNOHANG) > 0);
|
||||
}
|
||||
|
||||
@@ -1159,16 +1160,16 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
|
||||
// yes, this is pretty useless but we cannot ensure all of the plugin have the same behaviour
|
||||
uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].in_request = 0;
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MAX_REQUESTS] > 0 && uwsgi.workers[uwsgi.mywid].delta_requests >= uwsgi.shared->options[UWSGI_OPTION_MAX_REQUESTS]
|
||||
&& (end_of_request - (uwsgi.workers[uwsgi.mywid].last_spawn * 1000000) >= uwsgi.shared->options[UWSGI_OPTION_MIN_WORKER_LIFETIME] * 1000000)) {
|
||||
if (uwsgi.max_requests > 0 && uwsgi.workers[uwsgi.mywid].delta_requests >= uwsgi.max_requests
|
||||
&& (end_of_request - (uwsgi.workers[uwsgi.mywid].last_spawn * 1000000) >= uwsgi.min_worker_lifetime * 1000000)) {
|
||||
goodbye_cruel_world();
|
||||
}
|
||||
|
||||
if (uwsgi.reload_on_as && (rlim_t) vsz >= uwsgi.reload_on_as && (end_of_request - (uwsgi.workers[uwsgi.mywid].last_spawn * 1000000) >= uwsgi.shared->options[UWSGI_OPTION_MIN_WORKER_LIFETIME] * 1000000)) {
|
||||
if (uwsgi.reload_on_as && (rlim_t) vsz >= uwsgi.reload_on_as && (end_of_request - (uwsgi.workers[uwsgi.mywid].last_spawn * 1000000) >= uwsgi.min_worker_lifetime * 1000000)) {
|
||||
goodbye_cruel_world();
|
||||
}
|
||||
|
||||
if (uwsgi.reload_on_rss && (rlim_t) rss >= uwsgi.reload_on_rss && (end_of_request - (uwsgi.workers[uwsgi.mywid].last_spawn * 1000000) >= uwsgi.shared->options[UWSGI_OPTION_MIN_WORKER_LIFETIME] * 1000000)) {
|
||||
if (uwsgi.reload_on_rss && (rlim_t) rss >= uwsgi.reload_on_rss && (end_of_request - (uwsgi.workers[uwsgi.mywid].last_spawn * 1000000) >= uwsgi.min_worker_lifetime * 1000000)) {
|
||||
goodbye_cruel_world();
|
||||
}
|
||||
|
||||
@@ -1344,13 +1345,13 @@ int wsgi_req_async_recv(struct wsgi_request *wsgi_req) {
|
||||
if (event_queue_add_fd_read(uwsgi.async_queue, wsgi_req->fd) < 0)
|
||||
return -1;
|
||||
|
||||
async_add_timeout(wsgi_req, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
async_add_timeout(wsgi_req, uwsgi.socket_timeout);
|
||||
uwsgi.async_proto_fd_table[wsgi_req->fd] = wsgi_req;
|
||||
}
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1381,8 +1382,8 @@ int wsgi_req_recv(int queue, struct wsgi_request *wsgi_req) {
|
||||
}
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ROUTING
|
||||
@@ -1560,15 +1561,7 @@ void parse_sys_envs(char **envs) {
|
||||
char *earg, *eq_pos;
|
||||
|
||||
while (*uenvs) {
|
||||
if (!strncmp(*uenvs, "UWSGI_", 6) &&
|
||||
strncmp(*uenvs, "UWSGI_RELOADS=", 14) &&
|
||||
strncmp(*uenvs, "UWSGI_VASSALS_DIR=", 18) &&
|
||||
strncmp(*uenvs, "UWSGI_EMPEROR_FD=", 17) &&
|
||||
strncmp(*uenvs, "UWSGI_BROODLORD_NUM=", 20) &&
|
||||
strncmp(*uenvs, "UWSGI_EMPEROR_FD_CONFIG=", 24) &&
|
||||
strncmp(*uenvs, "UWSGI_EMPEROR_PROXY=", 20) &&
|
||||
strncmp(*uenvs, "UWSGI_JAIL_PID=", 15) &&
|
||||
strncmp(*uenvs, "UWSGI_ORIGINAL_PROC_NAME=", 25)) {
|
||||
if (!strncmp(*uenvs, "UWSGI_", 6) && strncmp(*uenvs, "UWSGI_RELOADS=", 14) && strncmp(*uenvs, "UWSGI_VASSALS_DIR=", 18) && strncmp(*uenvs, "UWSGI_EMPEROR_FD=", 17) && strncmp(*uenvs, "UWSGI_BROODLORD_NUM=", 20) && strncmp(*uenvs, "UWSGI_EMPEROR_FD_CONFIG=", 24) && strncmp(*uenvs, "UWSGI_EMPEROR_PROXY=", 20) && strncmp(*uenvs, "UWSGI_JAIL_PID=", 15) && strncmp(*uenvs, "UWSGI_ORIGINAL_PROC_NAME=", 25)) {
|
||||
earg = uwsgi_malloc(strlen(*uenvs + 6) + 1);
|
||||
env_to_arg(*uenvs + 6, earg);
|
||||
eq_pos = strchr(earg, '=');
|
||||
@@ -1871,7 +1864,7 @@ int uwsgi_num2str2n(int num, char *ptr, int size) {
|
||||
|
||||
int uwsgi_long2str2n(unsigned long long num, char *ptr, int size) {
|
||||
int ret = snprintf(ptr, size, "%llu", num);
|
||||
if (ret < 0)
|
||||
if (ret <= 0 || ret > size)
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
@@ -2779,6 +2772,7 @@ static struct uwsgi_unshare_id uwsgi_unshare_list[] = {
|
||||
{"ns", CLONE_NEWNS},
|
||||
{"fs", CLONE_NEWNS},
|
||||
{"mount", CLONE_NEWNS},
|
||||
{"mnt", CLONE_NEWNS},
|
||||
#endif
|
||||
#ifdef CLONE_SYSVSEM
|
||||
{"sysvsem", CLONE_SYSVSEM},
|
||||
@@ -2898,7 +2892,7 @@ static int uwsgi_get_cap_id(char *name) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int uwsgi_build_cap(char *what, cap_value_t **cap) {
|
||||
int uwsgi_build_cap(char *what, cap_value_t ** cap) {
|
||||
|
||||
int cap_id;
|
||||
char *caps = uwsgi_str(what);
|
||||
@@ -3192,128 +3186,139 @@ void http_url_decode(char *buf, uint16_t * len, char *dst) {
|
||||
|
||||
for (i = 0; i < *len; i++) {
|
||||
char c = buf[i];
|
||||
if (c == '#') break;
|
||||
switch(status) {
|
||||
case zero:
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
}
|
||||
if (c == '/') {
|
||||
status = slash;
|
||||
break;
|
||||
}
|
||||
*ptr++ = c;
|
||||
new_len++;
|
||||
if (c == '#')
|
||||
break;
|
||||
switch (status) {
|
||||
case zero:
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
case percent1:
|
||||
if (c == '%') {
|
||||
*ptr++ = '%';
|
||||
new_len++;
|
||||
status = zero;
|
||||
break;
|
||||
}
|
||||
value[0] = c;
|
||||
status = percent2;
|
||||
}
|
||||
if (c == '/') {
|
||||
status = slash;
|
||||
break;
|
||||
case percent2:
|
||||
value[1] = c;
|
||||
*ptr++ = hex2num(value);
|
||||
}
|
||||
*ptr++ = c;
|
||||
new_len++;
|
||||
break;
|
||||
case percent1:
|
||||
if (c == '%') {
|
||||
*ptr++ = '%';
|
||||
new_len++;
|
||||
status = zero;
|
||||
break;
|
||||
case slash:
|
||||
if (c == '.') {
|
||||
status = dot;
|
||||
break;
|
||||
}
|
||||
// we could be at the first round (in non slash)
|
||||
if (i > 0 || !no_slash) {
|
||||
*ptr++ = '/'; new_len++;
|
||||
}
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
}
|
||||
if (c == '/') {
|
||||
status = slash;
|
||||
break;
|
||||
}
|
||||
*ptr++ = c; new_len++;
|
||||
status = zero;
|
||||
}
|
||||
value[0] = c;
|
||||
status = percent2;
|
||||
break;
|
||||
case percent2:
|
||||
value[1] = c;
|
||||
*ptr++ = hex2num(value);
|
||||
new_len++;
|
||||
status = zero;
|
||||
break;
|
||||
case slash:
|
||||
if (c == '.') {
|
||||
status = dot;
|
||||
break;
|
||||
case dot:
|
||||
if (c == '.') {
|
||||
status = dotdot;
|
||||
break;
|
||||
}
|
||||
if (c == '/') {
|
||||
status = slash;
|
||||
break;
|
||||
}
|
||||
if (i > 1) {
|
||||
*ptr++ = '/'; new_len++;
|
||||
}
|
||||
*ptr++ = '.'; new_len++;
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
}
|
||||
*ptr++ = c; new_len++;
|
||||
status = zero;
|
||||
}
|
||||
// we could be at the first round (in non slash)
|
||||
if (i > 0 || !no_slash) {
|
||||
*ptr++ = '/';
|
||||
new_len++;
|
||||
}
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
case dotdot:
|
||||
// here we need to remove a segment
|
||||
if (c == '/') {
|
||||
current_new_len = new_len;
|
||||
while(current_new_len) {
|
||||
current_new_len--;
|
||||
ptr--;
|
||||
if (dst[current_new_len] == '/') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (c == '/') {
|
||||
status = slash;
|
||||
break;
|
||||
}
|
||||
*ptr++ = c;
|
||||
new_len++;
|
||||
status = zero;
|
||||
break;
|
||||
case dot:
|
||||
if (c == '.') {
|
||||
status = dotdot;
|
||||
break;
|
||||
}
|
||||
if (c == '/') {
|
||||
status = slash;
|
||||
break;
|
||||
}
|
||||
if (i > 1) {
|
||||
*ptr++ = '/';
|
||||
new_len++;
|
||||
}
|
||||
*ptr++ = '.';
|
||||
new_len++;
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
}
|
||||
*ptr++ = c;
|
||||
new_len++;
|
||||
status = zero;
|
||||
break;
|
||||
case dotdot:
|
||||
// here we need to remove a segment
|
||||
if (c == '/') {
|
||||
current_new_len = new_len;
|
||||
while (current_new_len) {
|
||||
current_new_len--;
|
||||
ptr--;
|
||||
if (dst[current_new_len] == '/') {
|
||||
break;
|
||||
}
|
||||
new_len = current_new_len;
|
||||
status = slash;
|
||||
break;
|
||||
}
|
||||
if (i > 2) {
|
||||
*ptr++ = '/'; new_len++;
|
||||
}
|
||||
*ptr++ = '.'; new_len++;
|
||||
*ptr++ = '.'; new_len++;
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
}
|
||||
*ptr++ = c; new_len++;
|
||||
status = zero;
|
||||
break;
|
||||
new_len = current_new_len;
|
||||
status = slash;
|
||||
break;
|
||||
}
|
||||
if (i > 2) {
|
||||
*ptr++ = '/';
|
||||
new_len++;
|
||||
}
|
||||
*ptr++ = '.';
|
||||
new_len++;
|
||||
*ptr++ = '.';
|
||||
new_len++;
|
||||
if (c == '%') {
|
||||
status = percent1;
|
||||
break;
|
||||
}
|
||||
*ptr++ = c;
|
||||
new_len++;
|
||||
status = zero;
|
||||
break;
|
||||
// over engineering
|
||||
default:
|
||||
*ptr++ = c;
|
||||
new_len++;
|
||||
break;
|
||||
default:
|
||||
*ptr++ = c;
|
||||
new_len++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch(status) {
|
||||
case slash:
|
||||
case dot:
|
||||
*ptr++ = '/'; new_len++;
|
||||
break;
|
||||
case dotdot:
|
||||
current_new_len = new_len;
|
||||
while(current_new_len) {
|
||||
if (dst[current_new_len-1] == '/') {
|
||||
break;
|
||||
}
|
||||
current_new_len--;
|
||||
switch (status) {
|
||||
case slash:
|
||||
case dot:
|
||||
*ptr++ = '/';
|
||||
new_len++;
|
||||
break;
|
||||
case dotdot:
|
||||
current_new_len = new_len;
|
||||
while (current_new_len) {
|
||||
if (dst[current_new_len - 1] == '/') {
|
||||
break;
|
||||
}
|
||||
new_len = current_new_len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
current_new_len--;
|
||||
}
|
||||
new_len = current_new_len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
*len = new_len;
|
||||
@@ -3573,7 +3578,7 @@ void uwsgi_set_cpu_affinity() {
|
||||
base_cpu = base_cpu % uwsgi.cpus;
|
||||
}
|
||||
ret = snprintf(buf, 4096, "mapping worker %d to CPUs:", uwsgi.mywid);
|
||||
if (ret < 25) {
|
||||
if (ret < 25 || ret >= 4096) {
|
||||
uwsgi_log("unable to initialize cpu affinity !!!\n");
|
||||
exit(1);
|
||||
}
|
||||
@@ -3591,7 +3596,7 @@ void uwsgi_set_cpu_affinity() {
|
||||
base_cpu = 0;
|
||||
CPU_SET(base_cpu, &cpuset);
|
||||
ret = snprintf(buf + pos, 4096 - pos, " %d", base_cpu);
|
||||
if (ret < 2) {
|
||||
if (ret < 2 || ret >= 4096) {
|
||||
uwsgi_log("unable to initialize cpu affinity !!!\n");
|
||||
exit(1);
|
||||
}
|
||||
@@ -3854,7 +3859,7 @@ int uwsgi_send_http_stats(int fd) {
|
||||
|
||||
char buf[4096];
|
||||
|
||||
int ret = uwsgi_waitfd(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi_waitfd(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0)
|
||||
return -1;
|
||||
|
||||
@@ -3937,19 +3942,22 @@ void uwsgi_uuid(char *buf) {
|
||||
uuid_generate(uuid_value);
|
||||
uuid_unparse(uuid_value, buf);
|
||||
#else
|
||||
int i,r[11];
|
||||
if (!uwsgi_file_exists("/dev/urandom")) goto fallback;
|
||||
int i, r[11];
|
||||
if (!uwsgi_file_exists("/dev/urandom"))
|
||||
goto fallback;
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd < 0) goto fallback;
|
||||
for(i=0;i<11;i++) {
|
||||
if (fd < 0)
|
||||
goto fallback;
|
||||
for (i = 0; i < 11; i++) {
|
||||
if (read(fd, &r[i], 4) != 4) {
|
||||
close(fd); goto fallback;
|
||||
close(fd);
|
||||
goto fallback;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
goto done;
|
||||
fallback:
|
||||
for(i=0;i<11;i++) {
|
||||
for (i = 0; i < 11; i++) {
|
||||
r[i] = rand();
|
||||
}
|
||||
done:
|
||||
@@ -4339,15 +4347,84 @@ void uwsgi_exit(int status) {
|
||||
}
|
||||
|
||||
int uwsgi_base128(struct uwsgi_buffer *ub, uint64_t l, int first) {
|
||||
if( l > 127 ) {
|
||||
if (uwsgi_base128(ub, l/128, 0)) return -1;
|
||||
if (l > 127) {
|
||||
if (uwsgi_base128(ub, l / 128, 0))
|
||||
return -1;
|
||||
}
|
||||
l %= 128;
|
||||
if(first) {
|
||||
if (uwsgi_buffer_u8(ub, (uint8_t) l)) return -1;
|
||||
if (first) {
|
||||
if (uwsgi_buffer_u8(ub, (uint8_t) l))
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
if (uwsgi_buffer_u8(ub, 0x80 | (uint8_t) l)) return -1;
|
||||
if (uwsgi_buffer_u8(ub, 0x80 | (uint8_t) l))
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
void uwsgi_setns(char *path) {
|
||||
int (*u_setns) (int, int) = (int (*)(int, int)) dlsym(RTLD_DEFAULT, "setns");
|
||||
if (!u_setns) {
|
||||
uwsgi_log("your system misses setns() syscall !!!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// cound be overwritten
|
||||
int count = 64;
|
||||
|
||||
uwsgi_log("joining namespaces from %s ...\n", path);
|
||||
for (;;) {
|
||||
int ns_fd = uwsgi_connect(path, 30, 0);
|
||||
if (ns_fd < 0) {
|
||||
uwsgi_error("uwsgi_setns()/uwsgi_connect()");
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
int *fds = uwsgi_attach_fd(ns_fd, &count, "uwsgi-setns", 11);
|
||||
if (fds && count > 0) {
|
||||
int i;
|
||||
for (i = 0; i < count; i++) {
|
||||
if (fds[i] > -1) {
|
||||
if (u_setns(fds[i], 0) < 0) {
|
||||
uwsgi_error("uwsgi_setns()/setns()");
|
||||
exit(1);
|
||||
}
|
||||
close(fds[i]);
|
||||
}
|
||||
}
|
||||
free(fds);
|
||||
break;
|
||||
}
|
||||
if (fds)
|
||||
free(fds);
|
||||
close(ns_fd);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
mode_t uwsgi_mode_t(char *value, int *error) {
|
||||
mode_t mode = 0;
|
||||
*error = 0;
|
||||
|
||||
if (strlen(value) < 3) {
|
||||
*error = 1;
|
||||
return mode;
|
||||
}
|
||||
|
||||
if (strlen(value) == 3) {
|
||||
mode = (mode << 3) + (value[0] - '0');
|
||||
mode = (mode << 3) + (value[1] - '0');
|
||||
mode = (mode << 3) + (value[2] - '0');
|
||||
}
|
||||
else {
|
||||
mode = (mode << 3) + (value[1] - '0');
|
||||
mode = (mode << 3) + (value[2] - '0');
|
||||
mode = (mode << 3) + (value[3] - '0');
|
||||
}
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
|
||||
+106
-63
@@ -76,13 +76,13 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"processes", required_argument, 'p', "spawn the specified number of workers/processes", uwsgi_opt_set_int, &uwsgi.numproc, 0},
|
||||
{"workers", required_argument, 'p', "spawn the specified number of workers/processes", uwsgi_opt_set_int, &uwsgi.numproc, 0},
|
||||
{"thunder-lock", no_argument, 0, "serialize accept() usage (if possible)", uwsgi_opt_true, &uwsgi.use_thunder_lock, 0},
|
||||
{"harakiri", required_argument, 't', "set harakiri timeout", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_HARAKIRI, 0},
|
||||
{"harakiri", required_argument, 't', "set harakiri timeout", uwsgi_opt_set_int, &uwsgi.harakiri_options.workers, 0},
|
||||
{"harakiri-verbose", no_argument, 0, "enable verbose mode for harakiri", uwsgi_opt_true, &uwsgi.harakiri_verbose, 0},
|
||||
{"harakiri-no-arh", no_argument, 0, "do not enable harakiri during after-request-hook", uwsgi_opt_true, &uwsgi.harakiri_no_arh, 0},
|
||||
{"no-harakiri-arh", no_argument, 0, "do not enable harakiri during after-request-hook", uwsgi_opt_true, &uwsgi.harakiri_no_arh, 0},
|
||||
{"no-harakiri-after-req-hook", no_argument, 0, "do not enable harakiri during after-request-hook", uwsgi_opt_true, &uwsgi.harakiri_no_arh, 0},
|
||||
{"backtrace-depth", required_argument, 0, "set backtrace depth", uwsgi_opt_set_int, &uwsgi.backtrace_depth, 0},
|
||||
{"mule-harakiri", required_argument, 0, "set harakiri timeout for mule tasks", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_MULE_HARAKIRI, 0},
|
||||
{"mule-harakiri", required_argument, 0, "set harakiri timeout for mule tasks", uwsgi_opt_set_int, &uwsgi.harakiri_options.mules, 0},
|
||||
#ifdef UWSGI_XML
|
||||
{"xmlconfig", required_argument, 'x', "load config from xml file", uwsgi_opt_load_xml, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"xml", required_argument, 'x', "load config from xml file", uwsgi_opt_load_xml, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
@@ -99,6 +99,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"set-ph", required_argument, 0, "set a placeholder", uwsgi_opt_set_placeholder, (void *) 1, UWSGI_OPT_IMMEDIATE},
|
||||
{"get", required_argument, 0, "print the specified option value and exit", uwsgi_opt_add_string_list, &uwsgi.get_list, UWSGI_OPT_NO_INITIAL},
|
||||
{"declare-option", required_argument, 0, "declare a new uWSGI custom option", uwsgi_opt_add_custom_option, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"declare-option2", required_argument, 0, "declare a new uWSGI custom option (non-immediate)", uwsgi_opt_add_custom_option, NULL, 0},
|
||||
|
||||
{"for", required_argument, 0, "(opt logic) for cycle", uwsgi_opt_logic, (void *) uwsgi_logic_opt_for, UWSGI_OPT_IMMEDIATE},
|
||||
{"for-glob", required_argument, 0, "(opt logic) for cycle (expand glob)", uwsgi_opt_logic, (void *) uwsgi_logic_opt_for_glob, UWSGI_OPT_IMMEDIATE},
|
||||
@@ -164,9 +165,9 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"max-vars", required_argument, 'v', "set the amount of internal iovec/vars structures", uwsgi_opt_max_vars, NULL, 0},
|
||||
{"max-apps", required_argument, 0, "set the maximum number of per-worker applications", uwsgi_opt_set_int, &uwsgi.max_apps, 0},
|
||||
{"buffer-size", required_argument, 'b', "set internal buffer size", uwsgi_opt_set_16bit, &uwsgi.buffer_size, 0},
|
||||
{"memory-report", no_argument, 'm', "enable memory report", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_MEMORY_DEBUG, 0},
|
||||
{"memory-report", no_argument, 'm', "enable memory report", uwsgi_opt_true, &uwsgi.logging_options.memory_report, 0},
|
||||
{"profiler", required_argument, 0, "enable the specified profiler", uwsgi_opt_set_str, &uwsgi.profiler, 0},
|
||||
{"cgi-mode", no_argument, 'c', "force CGI-mode for plugins supporting it", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_CGI_MODE, 0},
|
||||
{"cgi-mode", no_argument, 'c', "force CGI-mode for plugins supporting it", uwsgi_opt_true, &uwsgi.cgi_mode, 0},
|
||||
{"abstract-socket", no_argument, 'a', "force UNIX socket in abstract mode (Linux only)", uwsgi_opt_true, &uwsgi.abstract_socket, 0},
|
||||
{"chmod-socket", optional_argument, 'C', "chmod-socket", uwsgi_opt_chmod_socket, NULL, 0},
|
||||
{"chmod", optional_argument, 'C', "chmod-socket", uwsgi_opt_chmod_socket, NULL, 0},
|
||||
@@ -248,12 +249,12 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"print-sym", required_argument, 0, "print content of the specified binary symbol", uwsgi_print_sym, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"print-symbol", required_argument, 0, "print content of the specified binary symbol", uwsgi_print_sym, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
|
||||
{"reaper", no_argument, 'r', "call waitpid(-1,...) after each request to get rid of zombies", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_REAPER, 0},
|
||||
{"max-requests", required_argument, 'R', "reload workers after the specified amount of managed requests", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_MAX_REQUESTS, 0},
|
||||
{"min-worker-lifetime", required_argument, 0, "number of seconds worker must run before being reloaded (default is 60)", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_MIN_WORKER_LIFETIME, 0},
|
||||
{"max-worker-lifetime", required_argument, 0, "reload workers after the specified amount of seconds (default is disabled)", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_MAX_WORKER_LIFETIME, 0},
|
||||
{"reaper", no_argument, 'r', "call waitpid(-1,...) after each request to get rid of zombies", uwsgi_opt_true, &uwsgi.reaper, 0},
|
||||
{"max-requests", required_argument, 'R', "reload workers after the specified amount of managed requests", uwsgi_opt_set_64bit, &uwsgi.max_requests, 0},
|
||||
{"min-worker-lifetime", required_argument, 0, "number of seconds worker must run before being reloaded (default is 60)", uwsgi_opt_set_64bit, &uwsgi.min_worker_lifetime, 0},
|
||||
{"max-worker-lifetime", required_argument, 0, "reload workers after the specified amount of seconds (default is disabled)", uwsgi_opt_set_64bit, &uwsgi.max_worker_lifetime, 0},
|
||||
|
||||
{"socket-timeout", required_argument, 'z', "set internal sockets timeout", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_SOCKET_TIMEOUT, 0},
|
||||
{"socket-timeout", required_argument, 'z', "set internal sockets timeout", uwsgi_opt_set_int, &uwsgi.socket_timeout, 0},
|
||||
{"no-fd-passing", no_argument, 0, "disable file descriptor passing", uwsgi_opt_true, &uwsgi.no_fd_passing, 0},
|
||||
{"locks", required_argument, 0, "create the specified number of shared locks", uwsgi_opt_set_int, &uwsgi.locks, 0},
|
||||
{"lock-engine", required_argument, 0, "set the lock engine", uwsgi_opt_set_str, &uwsgi.lock_engine, 0},
|
||||
@@ -297,7 +298,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"spooler-processes", required_argument, 0, "set the number of processes for spoolers", uwsgi_opt_set_int, &uwsgi.spooler_numproc, UWSGI_OPT_IMMEDIATE},
|
||||
{"spooler-quiet", no_argument, 0, "do not be verbose with spooler tasks", uwsgi_opt_true, &uwsgi.spooler_quiet, 0},
|
||||
{"spooler-max-tasks", required_argument, 0, "set the maximum number of tasks to run before recycling a spooler", uwsgi_opt_set_int, &uwsgi.spooler_max_tasks, 0},
|
||||
{"spooler-harakiri", required_argument, 0, "set harakiri timeout for spooler tasks", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_SPOOLER_HARAKIRI, 0},
|
||||
{"spooler-harakiri", required_argument, 0, "set harakiri timeout for spooler tasks", uwsgi_opt_set_int, &uwsgi.harakiri_options.spoolers, 0},
|
||||
|
||||
{"mule", optional_argument, 0, "add a mule", uwsgi_opt_add_mule, NULL, UWSGI_OPT_MASTER},
|
||||
{"mules", required_argument, 0, "add the specified number of mules", uwsgi_opt_add_mules, NULL, UWSGI_OPT_MASTER},
|
||||
@@ -316,7 +317,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
|
||||
{"rpc-max", required_argument, 0, "maximum number of rpc slots (default: 64)", uwsgi_opt_set_64bit, &uwsgi.rpc_max, 0},
|
||||
|
||||
{"disable-logging", no_argument, 'L', "disable request logging", uwsgi_opt_dyn_false, (void *) UWSGI_OPTION_LOGGING, 0},
|
||||
{"disable-logging", no_argument, 'L', "disable request logging", uwsgi_opt_false, &uwsgi.logging_options.enabled, 0},
|
||||
|
||||
{"flock", required_argument, 0, "lock the specified file before starting, exit if locked", uwsgi_opt_flock, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"flock-wait", required_argument, 0, "lock the specified file before starting, wait if locked", uwsgi_opt_flock_wait, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
@@ -344,7 +345,13 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
#ifdef __linux__
|
||||
{"unshare", required_argument, 0, "unshare() part of the processes and put it in a new namespace", uwsgi_opt_set_unshare, &uwsgi.unshare, 0},
|
||||
{"unshare2", required_argument, 0, "unshare() part of the processes and put it in a new namespace after rootfs change", uwsgi_opt_set_unshare, &uwsgi.unshare2, 0},
|
||||
{"setns-socket", required_argument, 0, "expose a unix socket returning namespace fds from /proc/self/ns", uwsgi_opt_set_str, &uwsgi.setns_socket, UWSGI_OPT_MASTER},
|
||||
{"setns-socket-skip", required_argument, 0, "skip the specified entry when sending setns file descriptors", uwsgi_opt_add_string_list, &uwsgi.setns_socket_skip, 0},
|
||||
{"setns-skip", required_argument, 0, "skip the specified entry when sending setns file descriptors", uwsgi_opt_add_string_list, &uwsgi.setns_socket_skip, 0},
|
||||
{"setns", required_argument, 0, "join a namespace created by an external uWSGI instance", uwsgi_opt_set_str, &uwsgi.setns, 0},
|
||||
{"setns-preopen", no_argument, 0, "open /proc/self/ns as soon as possible and cache fds", uwsgi_opt_true, &uwsgi.setns_preopen, 0},
|
||||
#endif
|
||||
{"jailed", no_argument, 0, "mark the instance as jailed (force the execution of post_jail hooks)", uwsgi_opt_true, &uwsgi.jailed, 0},
|
||||
#if defined(__FreeBSD__) || defined(__GNU_kFreeBSD__)
|
||||
{"jail", required_argument, 0, "put the instance in a FreeBSD jail", uwsgi_opt_set_str, &uwsgi.jail, 0},
|
||||
{"jail-ip4", required_argument, 0, "add an ipv4 address to the FreeBSD jail", uwsgi_opt_add_string_list, &uwsgi.jail_ip4, 0},
|
||||
@@ -378,6 +385,10 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"hook-accepting-once", required_argument, 0, "run the specified hook after each worker enter the accepting phase (once per-instance)", uwsgi_opt_add_string_list, &uwsgi.hook_accepting_once, 0},
|
||||
{"hook-accepting1-once", required_argument, 0, "run the specified hook after the first worker enters the accepting phase (once per instance)", uwsgi_opt_add_string_list, &uwsgi.hook_accepting1_once, 0},
|
||||
|
||||
{"hook-emperor-stop", required_argument, 0, "run the specified hook when the Emperor send a stop message", uwsgi_opt_add_string_list, &uwsgi.hook_emperor_stop, 0},
|
||||
{"hook-emperor-reload", required_argument, 0, "run the specified hook when the Emperor send a reload message", uwsgi_opt_add_string_list, &uwsgi.hook_emperor_reload, 0},
|
||||
{"hook-emperor-lost", required_argument, 0, "run the specified hook when the Emperor connection is lost", uwsgi_opt_add_string_list, &uwsgi.hook_emperor_lost, 0},
|
||||
|
||||
{"hook-as-vassal", required_argument, 0, "run the specified hook before exec()ing the vassal", uwsgi_opt_add_string_list, &uwsgi.hook_as_vassal, 0},
|
||||
{"hook-as-emperor", required_argument, 0, "run the specified hook in the emperor after the vassal has been started", uwsgi_opt_add_string_list, &uwsgi.hook_as_emperor, 0},
|
||||
|
||||
@@ -573,6 +584,8 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"subscriptions-sign-check", required_argument, 0, "set digest algorithm and certificate directory for secured subscription system", uwsgi_opt_scd, NULL, UWSGI_OPT_MASTER},
|
||||
{"subscriptions-sign-check-tolerance", required_argument, 0, "set the maximum tolerance (in seconds) of clock skew for secured subscription system", uwsgi_opt_set_int, &uwsgi.subscriptions_sign_check_tolerance, UWSGI_OPT_MASTER},
|
||||
#endif
|
||||
{"subscriptions-credentials-check", required_argument, 0, "add a directory to search for subscriptions key credentials", uwsgi_opt_add_string_list, &uwsgi.subscriptions_credentials_check_dir, UWSGI_OPT_MASTER},
|
||||
{"subscriptions-use-credentials", no_argument, 0, "enable management of SCM_CREDENTIALS in subscriptions UNIX sockets", uwsgi_opt_true, &uwsgi.subscriptions_use_credentials, 0},
|
||||
{"subscription-algo", required_argument, 0, "set load balancing algorithm for the subscription system", uwsgi_opt_ssa, NULL, 0},
|
||||
{"subscription-dotsplit", no_argument, 0, "try to fallback to the next part (dot based) in subscription key", uwsgi_opt_true, &uwsgi.subscription_dotsplit, 0},
|
||||
{"subscribe-to", required_argument, 0, "subscribe to the specified subscription server", uwsgi_opt_add_string_list, &uwsgi.subscriptions, UWSGI_OPT_MASTER},
|
||||
@@ -598,7 +611,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"sni-regexp", required_argument, 0, "add an SNI-governed SSL context (the key is a regexp)", uwsgi_opt_sni, NULL, 0},
|
||||
#endif
|
||||
#endif
|
||||
{"check-interval", required_argument, 0, "set the interval (in seconds) of master checks", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_MASTER_INTERVAL, 0},
|
||||
{"check-interval", required_argument, 0, "set the interval (in seconds) of master checks", uwsgi_opt_set_int, &uwsgi.master_interval, UWSGI_OPT_MASTER},
|
||||
{"forkbomb-delay", required_argument, 0, "sleep for the specified number of seconds when a forkbomb is detected", uwsgi_opt_set_int, &uwsgi.forkbomb_delay, UWSGI_OPT_MASTER},
|
||||
{"binary-path", required_argument, 0, "force binary path", uwsgi_opt_set_str, &uwsgi.binary_path, 0},
|
||||
{"privileged-binary-patch", required_argument, 0, "patch the uwsgi binary with a new command (before privileges drop)", uwsgi_opt_set_str, &uwsgi.privileged_binary_patch, 0},
|
||||
@@ -665,15 +678,15 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"log-date", optional_argument, 0, "prefix logs with date or a strftime string", uwsgi_opt_log_date, NULL, 0},
|
||||
{"log-prefix", optional_argument, 0, "prefix logs with a string", uwsgi_opt_log_date, NULL, 0},
|
||||
|
||||
{"log-zero", no_argument, 0, "log responses without body", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_ZERO, 0},
|
||||
{"log-slow", required_argument, 0, "log requests slower than the specified number of milliseconds", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_LOG_SLOW, 0},
|
||||
{"log-4xx", no_argument, 0, "log requests with a 4xx response", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_4xx, 0},
|
||||
{"log-5xx", no_argument, 0, "log requests with a 5xx response", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_5xx, 0},
|
||||
{"log-big", required_argument, 0, "log requestes bigger than the specified size", uwsgi_opt_set_dyn, (void *) UWSGI_OPTION_LOG_BIG, 0},
|
||||
{"log-sendfile", required_argument, 0, "log sendfile requests", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_SENDFILE, 0},
|
||||
{"log-ioerror", required_argument, 0, "log requests with io errors", uwsgi_opt_dyn_true, (void *) UWSGI_OPTION_LOG_IOERROR, 0},
|
||||
{"log-zero", no_argument, 0, "log responses without body", uwsgi_opt_true, &uwsgi.logging_options.zero, 0},
|
||||
{"log-slow", required_argument, 0, "log requests slower than the specified number of milliseconds", uwsgi_opt_set_int, &uwsgi.logging_options.slow, 0},
|
||||
{"log-4xx", no_argument, 0, "log requests with a 4xx response", uwsgi_opt_true, &uwsgi.logging_options._4xx, 0},
|
||||
{"log-5xx", no_argument, 0, "log requests with a 5xx response", uwsgi_opt_true, &uwsgi.logging_options._5xx, 0},
|
||||
{"log-big", required_argument, 0, "log requestes bigger than the specified size", uwsgi_opt_set_64bit, &uwsgi.logging_options.big, 0},
|
||||
{"log-sendfile", required_argument, 0, "log sendfile requests", uwsgi_opt_true, &uwsgi.logging_options.sendfile, 0},
|
||||
{"log-ioerror", required_argument, 0, "log requests with io errors", uwsgi_opt_true, &uwsgi.logging_options.ioerror, 0},
|
||||
{"log-micros", no_argument, 0, "report response time in microseconds instead of milliseconds", uwsgi_opt_true, &uwsgi.log_micros, 0},
|
||||
{"log-x-forwarded-for", no_argument, 0, "use the ip from X-Forwarded-For header instead of REMOTE_ADDR", uwsgi_opt_true, &uwsgi.log_x_forwarded_for, 0},
|
||||
{"log-x-forwarded-for", no_argument, 0, "use the ip from X-Forwarded-For header instead of REMOTE_ADDR", uwsgi_opt_true, &uwsgi.logging_options.log_x_forwarded_for, 0},
|
||||
{"master-as-root", no_argument, 0, "leave master process running as root", uwsgi_opt_true, &uwsgi.master_as_root, 0},
|
||||
|
||||
{"drop-after-init", no_argument, 0, "run privileges drop after plugin initialization", uwsgi_opt_true, &uwsgi.drop_after_init, 0},
|
||||
@@ -688,7 +701,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"cheap", no_argument, 0, "set cheap mode (spawn workers only after the first request)", uwsgi_opt_true, &uwsgi.status.is_cheap, UWSGI_OPT_MASTER},
|
||||
{"cheaper", required_argument, 0, "set cheaper mode (adaptive process spawning)", uwsgi_opt_set_int, &uwsgi.cheaper_count, UWSGI_OPT_MASTER | UWSGI_OPT_CHEAPER},
|
||||
{"cheaper-initial", required_argument, 0, "set the initial number of processes to spawn in cheaper mode", uwsgi_opt_set_int, &uwsgi.cheaper_initial, UWSGI_OPT_MASTER | UWSGI_OPT_CHEAPER},
|
||||
{"cheaper-algo", required_argument, 0, "choose to algorithm used for adaptive process spawning)", uwsgi_opt_set_str, &uwsgi.requested_cheaper_algo, UWSGI_OPT_MASTER},
|
||||
{"cheaper-algo", required_argument, 0, "choose to algorithm used for adaptive process spawning", uwsgi_opt_set_str, &uwsgi.requested_cheaper_algo, UWSGI_OPT_MASTER},
|
||||
{"cheaper-step", required_argument, 0, "number of additional processes to spawn at each overload", uwsgi_opt_set_int, &uwsgi.cheaper_step, UWSGI_OPT_MASTER | UWSGI_OPT_CHEAPER},
|
||||
{"cheaper-overload", required_argument, 0, "increase workers after specified overload", uwsgi_opt_set_64bit, &uwsgi.cheaper_overload, UWSGI_OPT_MASTER | UWSGI_OPT_CHEAPER},
|
||||
{"cheaper-algo-list", no_argument, 0, "list enabled cheapers algorithms", uwsgi_opt_true, &uwsgi.cheaper_algo_list, 0},
|
||||
@@ -901,6 +914,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"legion-smart-attach-daemon2", required_argument, 0, "same as --smart-attach-daemon2 but daemon runs only on legion lord node", uwsgi_opt_add_daemon, NULL, UWSGI_OPT_MASTER},
|
||||
#endif
|
||||
{"daemons-honour-stdin", no_argument, 0, "do not change the stdin of external daemons to /dev/null", uwsgi_opt_true, &uwsgi.daemons_honour_stdin, UWSGI_OPT_MASTER},
|
||||
{"attach-daemon2", required_argument, 0, "attach-daemon keyval variant (supports smart modes too)", uwsgi_opt_add_daemon2, NULL, UWSGI_OPT_MASTER},
|
||||
{"plugins", required_argument, 0, "load uWSGI plugins", uwsgi_opt_load_plugin, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"plugin", required_argument, 0, "load uWSGI plugins", uwsgi_opt_load_plugin, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"need-plugins", required_argument, 0, "load uWSGI plugins (exit on error)", uwsgi_opt_load_plugin, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
@@ -1362,7 +1376,7 @@ void what_i_am_doing() {
|
||||
#else
|
||||
ctime_r((const time_t *) &wsgi_req->start_of_request_in_sec, ctime_storage);
|
||||
#endif
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now()) {
|
||||
if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now()) {
|
||||
uwsgi_log("HARAKIRI: --- uWSGI worker %d core %d (pid: %d) WAS managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, i, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime_storage);
|
||||
}
|
||||
else {
|
||||
@@ -1379,14 +1393,14 @@ void what_i_am_doing() {
|
||||
#else
|
||||
ctime_r((const time_t *) &wsgi_req->start_of_request_in_sec, ctime_storage);
|
||||
#endif
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now()) {
|
||||
if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now()) {
|
||||
uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime_storage);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("SIGUSR2: --- uWSGI worker %d (pid: %d) is managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime_storage);
|
||||
}
|
||||
}
|
||||
else if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now() && uwsgi.workers[uwsgi.mywid].sig) {
|
||||
else if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now() && uwsgi.workers[uwsgi.mywid].sig) {
|
||||
uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS handling signal %d ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, uwsgi.workers[uwsgi.mywid].signum);
|
||||
}
|
||||
}
|
||||
@@ -2036,6 +2050,12 @@ void uwsgi_setup(int argc, char *argv[], char *envp[]) {
|
||||
uwsgi.magic_table['V'] = UWSGI_VERSION;
|
||||
uwsgi.magic_table['k'] = uwsgi_num2str(uwsgi.cpus);
|
||||
uwsgi.magic_table['['] = "\033";
|
||||
uwsgi.magic_table['u'] = uwsgi_num2str((int)getuid());
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
uwsgi.magic_table['U'] = pw ? pw->pw_name : uwsgi.magic_table['u'];
|
||||
uwsgi.magic_table['g'] = uwsgi_num2str((int)getgid());
|
||||
struct group *gr = getgrgid(getgid());
|
||||
uwsgi.magic_table['G'] = gr ? gr->gr_name : uwsgi.magic_table['g'];
|
||||
|
||||
// you can embed a ini file in the uWSGi binary with default options
|
||||
#ifdef UWSGI_EMBED_CONFIG
|
||||
@@ -2268,6 +2288,16 @@ void uwsgi_setup(int argc, char *argv[], char *envp[]) {
|
||||
// initialize shared sockets
|
||||
uwsgi_setup_shared_sockets();
|
||||
|
||||
#ifdef __linux__
|
||||
if (uwsgi.setns_preopen) {
|
||||
uwsgi_setns_preopen();
|
||||
}
|
||||
// eventually join a linux namespace
|
||||
if (uwsgi.setns) {
|
||||
uwsgi_setns(uwsgi.setns);
|
||||
}
|
||||
#endif
|
||||
|
||||
// start the Emperor if needed
|
||||
if (uwsgi.early_emperor && uwsgi.emperor) {
|
||||
uwsgi_emperor_start();
|
||||
@@ -3193,7 +3223,7 @@ void uwsgi_worker_run() {
|
||||
}
|
||||
|
||||
// setup UNIX signals for the worker
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && !uwsgi.master_process) {
|
||||
if (uwsgi.harakiri_options.workers > 0 && !uwsgi.master_process) {
|
||||
signal(SIGALRM, (void *) &harakiri);
|
||||
}
|
||||
uwsgi_unix_signal(SIGHUP, gracefully_kill);
|
||||
@@ -3638,6 +3668,17 @@ void uwsgi_opt_true(char *opt, char *value, void *key) {
|
||||
}
|
||||
}
|
||||
|
||||
void uwsgi_opt_false(char *opt, char *value, void *key) {
|
||||
|
||||
int *ptr = (int *) key;
|
||||
*ptr = 0;
|
||||
if (value) {
|
||||
if (!strcasecmp("false", value) || !strcasecmp("off", value) || !strcasecmp("no", value) || !strcmp("0", value)) {
|
||||
*ptr = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void uwsgi_opt_set_immediate_gid(char *opt, char *value, void *none) {
|
||||
gid_t gid = atoi(value);
|
||||
if (gid == 0) {
|
||||
@@ -3714,6 +3755,42 @@ void uwsgi_opt_set_int(char *opt, char *value, void *key) {
|
||||
}
|
||||
}
|
||||
|
||||
void uwsgi_opt_uid(char *opt, char *value, void *key) {
|
||||
uid_t uid = atoi(value);
|
||||
if (!uid) {
|
||||
struct passwd *p = getpwnam(value);
|
||||
if (p) {
|
||||
uid = p->pw_uid;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("unable to find user %s\n", value);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (key) {
|
||||
uid_t *ptr = (uid_t *) key;
|
||||
*ptr = uid;
|
||||
}
|
||||
}
|
||||
|
||||
void uwsgi_opt_gid(char *opt, char *value, void *key) {
|
||||
gid_t gid = atoi(value);
|
||||
if (!gid) {
|
||||
struct group *g = getgrnam(value);
|
||||
if (g) {
|
||||
gid = g->gr_gid;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("unable to find group %s\n", value);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (key) {
|
||||
gid_t *ptr = (gid_t *) key;
|
||||
*ptr = gid;
|
||||
}
|
||||
}
|
||||
|
||||
void uwsgi_opt_set_rawint(char *opt, char *value, void *key) {
|
||||
int *ptr = (int *) key;
|
||||
if (value) {
|
||||
@@ -3755,27 +3832,6 @@ void uwsgi_opt_set_megabytes(char *opt, char *value, void *key) {
|
||||
*ptr = (uint64_t)strtoul(value, NULL, 10) * 1024 * 1024;
|
||||
}
|
||||
|
||||
void uwsgi_opt_set_dyn(char *opt, char *value, void *key) {
|
||||
|
||||
long *fake_ptr = (long *) key;
|
||||
uint8_t dyn_opt_id = (long) fake_ptr;
|
||||
uwsgi.shared->options[dyn_opt_id] = atoi(value);
|
||||
}
|
||||
|
||||
void uwsgi_opt_dyn_true(char *opt, char *value, void *key) {
|
||||
|
||||
long *fake_ptr = (long *) key;
|
||||
uint8_t dyn_opt_id = (long) fake_ptr;
|
||||
uwsgi.shared->options[dyn_opt_id] = 1;
|
||||
}
|
||||
|
||||
void uwsgi_opt_dyn_false(char *opt, char *value, void *key) {
|
||||
|
||||
long *fake_ptr = (long *) key;
|
||||
uint8_t dyn_opt_id = (long) fake_ptr;
|
||||
uwsgi.shared->options[dyn_opt_id] = 0;
|
||||
}
|
||||
|
||||
void uwsgi_opt_set_str(char *opt, char *value, void *key) {
|
||||
char **ptr = (char **) key;
|
||||
if (!value) {
|
||||
@@ -3992,27 +4048,14 @@ void uwsgi_opt_scd(char *opt, char *value, void *foobar) {
|
||||
#endif
|
||||
|
||||
void uwsgi_opt_set_umask(char *opt, char *value, void *mode) {
|
||||
|
||||
mode_t umask_mode = 0;
|
||||
|
||||
if (strlen(value) < 3) {
|
||||
int error = 0;
|
||||
mode_t mask = uwsgi_mode_t(value, &error);
|
||||
if (error) {
|
||||
uwsgi_log("invalid umask: %s\n", value);
|
||||
}
|
||||
umask_mode = 0;
|
||||
if (strlen(value) == 3) {
|
||||
umask_mode = (umask_mode << 3) + (value[0] - '0');
|
||||
umask_mode = (umask_mode << 3) + (value[1] - '0');
|
||||
umask_mode = (umask_mode << 3) + (value[2] - '0');
|
||||
}
|
||||
else {
|
||||
umask_mode = (umask_mode << 3) + (value[1] - '0');
|
||||
umask_mode = (umask_mode << 3) + (value[2] - '0');
|
||||
umask_mode = (umask_mode << 3) + (value[3] - '0');
|
||||
}
|
||||
umask(umask_mode);
|
||||
umask(mask);
|
||||
|
||||
uwsgi.do_not_change_umask = 1;
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_opt_exit(char *opt, char *value, void *none) {
|
||||
|
||||
+23
-2
@@ -367,18 +367,39 @@ ssize_t uwsgi_websockets_simple_send(struct wsgi_request *wsgi_req, struct uwsgi
|
||||
return len;
|
||||
}
|
||||
|
||||
int uwsgi_websocket_handshake(struct wsgi_request *wsgi_req, char *key, uint16_t key_len, char *origin, uint16_t origin_len) {
|
||||
int uwsgi_websocket_handshake(struct wsgi_request *wsgi_req, char *key, uint16_t key_len, char *origin, uint16_t origin_len, char *proto, uint16_t proto_len) {
|
||||
#ifdef UWSGI_SSL
|
||||
if (!key_len) {
|
||||
key = wsgi_req->http_sec_websocket_key;
|
||||
key_len = wsgi_req->http_sec_websocket_key_len;
|
||||
}
|
||||
if (key_len == 0) return -1;
|
||||
|
||||
char sha1[20];
|
||||
if (uwsgi_response_prepare_headers(wsgi_req, "101 Web Socket Protocol Handshake", 33)) return -1;
|
||||
if (uwsgi_response_add_header(wsgi_req, "Upgrade", 7, "WebSocket", 9)) return -1;
|
||||
if (uwsgi_response_add_header(wsgi_req, "Connection", 10, "Upgrade", 7)) return -1;
|
||||
if (origin_len > 0) {
|
||||
|
||||
// if origin was requested or proto_len is specified, send it back
|
||||
if (wsgi_req->http_origin_len > 0 || origin_len > 0) {
|
||||
if (!origin_len) {
|
||||
origin = wsgi_req->http_origin;
|
||||
origin_len = wsgi_req->http_origin_len;
|
||||
}
|
||||
if (uwsgi_response_add_header(wsgi_req, "Sec-WebSocket-Origin", 20, origin, origin_len)) return -1;
|
||||
}
|
||||
else {
|
||||
if (uwsgi_response_add_header(wsgi_req, "Sec-WebSocket-Origin", 20, "*", 1)) return -1;
|
||||
}
|
||||
|
||||
// if protocol was requested or proto_len is specified, send it back
|
||||
if (wsgi_req->http_sec_websocket_protocol_len > 0 || proto_len > 0) {
|
||||
if (!proto_len) {
|
||||
proto = wsgi_req->http_sec_websocket_protocol;
|
||||
proto_len = wsgi_req->http_sec_websocket_protocol_len;
|
||||
}
|
||||
if (uwsgi_response_add_header(wsgi_req, "Sec-WebSocket-Protocol", 22, proto, proto_len)) return -1;
|
||||
}
|
||||
// generate websockets sha1 and encode it to base64
|
||||
if (!uwsgi_sha1_2n(key, key_len, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36, sha1)) return -1;
|
||||
size_t b64_len = 0;
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ extern struct uwsgi_server uwsgi;
|
||||
int uwsgi_response_add_content_length(struct wsgi_request *wsgi_req, uint64_t cl) {
|
||||
char buf[sizeof(UMAX64_STR)+1];
|
||||
int ret = snprintf(buf, sizeof(UMAX64_STR)+1, "%llu", (unsigned long long) cl);
|
||||
if (ret <= 0 || ret > (int) (sizeof(UMAX64_STR)+1)) {
|
||||
if (ret <= 0 || ret >= (int) (sizeof(UMAX64_STR)+1)) {
|
||||
wsgi_req->write_errors++;
|
||||
return -1;
|
||||
}
|
||||
@@ -51,7 +51,7 @@ int uwsgi_response_add_content_range(struct wsgi_request *wsgi_req, uint64_t sta
|
||||
end = cl-1;
|
||||
}
|
||||
int ret = snprintf(buf, 6+(sizeof(UMAX64_STR)*3)+4, "bytes %llu-%llu/%llu", (unsigned long long) start, (unsigned long long) end, (unsigned long long) cl);
|
||||
if (ret <= 0 || ret > (int) (6+(sizeof(UMAX64_STR)*3)+4)) {
|
||||
if (ret <= 0 || ret >= (int) (6+(sizeof(UMAX64_STR)*3)+4)) {
|
||||
wsgi_req->write_errors++;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -42,16 +42,6 @@ def application(env, start_response):
|
||||
|
||||
yield '<h2>dynamic options</h2>'
|
||||
|
||||
yield '<b>logging</b>: ' + str(uwsgi.get_option(0)) + '<br/>'
|
||||
yield '<b>max_requests</b>: ' + str(uwsgi.getoption(1)) + '<br/>'
|
||||
yield '<b>socket_timeout</b>: ' + str(uwsgi.getoption(2)) + '<br/>'
|
||||
yield '<b>memory_debug</b>: ' + str(uwsgi.getoption(3)) + '<br/>'
|
||||
yield '<b>master_interval</b>: ' + str(uwsgi.getoption(4)) + '<br/>'
|
||||
yield '<b>harakiri</b>: ' + str(uwsgi.getoption(5)) + '<br/>'
|
||||
yield '<b>cgi_mode</b>: ' + str(uwsgi.get_option(6)) + '<br/>'
|
||||
yield '<b>threads</b>: ' + str(uwsgi.get_option(7)) + '<br/>'
|
||||
yield '<b>process_reaper</b>: ' + str(uwsgi.get_option(8)) + '<br/>'
|
||||
|
||||
yield '<table border="1">'
|
||||
yield '<th>worker id</th><th>pid</th><th>in request</th><th>requests</th><th>running time</th><th>address space</th><th>rss</th>'
|
||||
|
||||
|
||||
@@ -47,11 +47,6 @@ def serve_logo(e, sr):
|
||||
uwsgi.sendfile('logo_uWSGI.png')
|
||||
return ''
|
||||
|
||||
def serve_options(e, sr):
|
||||
sr('200 OK', [('Content-Type', 'text/html')])
|
||||
for opt in range(0,256):
|
||||
yield "<b>%d</b> = %d<br/>" % (opt, uwsgi.get_option(opt))
|
||||
|
||||
def serve_config(e, sr):
|
||||
sr('200 OK', [('Content-Type', 'text/html')])
|
||||
for opt in uwsgi.opt.keys():
|
||||
@@ -61,7 +56,6 @@ routes = {}
|
||||
routes['/xsendfile'] = xsendfile
|
||||
routes['/logo'] = serve_logo
|
||||
routes['/config'] = serve_config
|
||||
routes['/options'] = serve_options
|
||||
|
||||
@postfork
|
||||
def setprocname():
|
||||
@@ -119,11 +113,6 @@ def application(env, start_response):
|
||||
Configuration<br/>
|
||||
<iframe src="/config"></iframe><br/>
|
||||
|
||||
<br/>
|
||||
|
||||
Dynamic options<br/>
|
||||
<iframe src="/options"></iframe><br/>
|
||||
|
||||
<br/>
|
||||
Workers and applications<br/>
|
||||
<table border="1">
|
||||
|
||||
@@ -9,12 +9,6 @@ def serve_logo(e, sr):
|
||||
sr('200 OK', [('Content-Type', 'image/png')])
|
||||
return uwsgi.sendfile('logo_uWSGI.png')
|
||||
|
||||
def serve_options(e, sr):
|
||||
sr('200 OK', [('Content-Type', 'text/html')])
|
||||
for opt in range(0,256):
|
||||
body = "{opt} = {optvalue}<br/>".format(opt=opt, optvalue=uwsgi.get_option(opt))
|
||||
yield bytes(body.encode('ascii'))
|
||||
|
||||
def serve_config(e, sr):
|
||||
sr('200 OK', [('Content-Type', 'text/html')])
|
||||
for opt in uwsgi.opt.keys():
|
||||
@@ -25,7 +19,6 @@ routes = {}
|
||||
routes['/xsendfile'] = xsendfile
|
||||
routes['/logo'] = serve_logo
|
||||
routes['/config'] = serve_config
|
||||
routes['/options'] = serve_options
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
@@ -43,9 +36,6 @@ Configuration<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
Dynamic options<br/>
|
||||
<iframe src="/options"></iframe><br/>
|
||||
|
||||
""".format(version=uwsgi.version.decode('ascii'))
|
||||
|
||||
return bytes(body.encode('ascii'))
|
||||
|
||||
+4
-1
@@ -61,6 +61,7 @@ void linux_namespace_start(void *argv) {
|
||||
for (;;) {
|
||||
char stack[PTHREAD_STACK_MIN];
|
||||
int waitpid_status;
|
||||
char *pid_str = NULL;
|
||||
uwsgi_log("*** jailing uWSGI in %s ***\n", uwsgi.ns);
|
||||
int clone_flags = SIGCHLD | CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWIPC | CLONE_NEWNS;
|
||||
if (uwsgi.ns_net) {
|
||||
@@ -77,10 +78,12 @@ void linux_namespace_start(void *argv) {
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
pid_str = uwsgi_num2str((int) pid);
|
||||
// run the post-jail scripts
|
||||
if (setenv("UWSGI_JAIL_PID", uwsgi_num2str((int) pid), 1)) {
|
||||
if (setenv("UWSGI_JAIL_PID", pid_str, 1)) {
|
||||
uwsgi_error("setenv()");
|
||||
}
|
||||
free(pid_str);
|
||||
uwsgi_hooks_run(uwsgi.hook_post_jail, "post-jail", 1);
|
||||
struct uwsgi_string_list *usl = uwsgi.exec_post_jail;
|
||||
while(usl) {
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#include "../../uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
/* uwsgi ADMIN|10 */
|
||||
int uwsgi_request_admin(struct wsgi_request *wsgi_req) {
|
||||
|
||||
uint32_t opt_value = 0;
|
||||
|
||||
if (wsgi_req->uh->pktsize != 0 && wsgi_req->uh->pktsize != 4)
|
||||
return UWSGI_OK;
|
||||
|
||||
// write request
|
||||
if (wsgi_req->uh->pktsize == 4) {
|
||||
memcpy(&opt_value, wsgi_req->buffer, 4);
|
||||
uwsgi_log( "setting internal option %d to %d\n", wsgi_req->uh->modifier2, opt_value);
|
||||
uwsgi.shared->options[wsgi_req->uh->modifier2] = opt_value;
|
||||
|
||||
// ACK
|
||||
wsgi_req->uh->modifier1 = 255;
|
||||
wsgi_req->uh->pktsize = 0;
|
||||
wsgi_req->uh->modifier2 = 1;
|
||||
uwsgi_response_write_body_do(wsgi_req, (char *)wsgi_req->uh , 4);
|
||||
}
|
||||
// get request
|
||||
else {
|
||||
uwsgi_log( "internal option %d = %d\n", wsgi_req->uh->modifier2, uwsgi.shared->options[wsgi_req->uh->modifier2]);
|
||||
|
||||
wsgi_req->uh->modifier1 = 10;
|
||||
wsgi_req->uh->pktsize = 4;
|
||||
uwsgi_response_write_body_do(wsgi_req, (char *)wsgi_req->uh , 4);
|
||||
uwsgi_response_write_body_do(wsgi_req, (char *) &uwsgi.shared->options[wsgi_req->uh->modifier2], 4);
|
||||
}
|
||||
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
|
||||
struct uwsgi_plugin admin_plugin = {
|
||||
|
||||
.name = "admin",
|
||||
.modifier1 = 10,
|
||||
.request = uwsgi_request_admin,
|
||||
|
||||
};
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
|
||||
NAME='admin'
|
||||
CFLAGS = []
|
||||
LDFLAGS = []
|
||||
LIBS = []
|
||||
|
||||
GCC_LIST = ['admin_plugin']
|
||||
@@ -222,8 +222,8 @@ static void uwsgi_airbrake_loop(struct uwsgi_thread *ut) {
|
||||
// ARGH !!!
|
||||
if (!curl) return;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, uwsgi.socket_timeout);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, uwsgi.socket_timeout);
|
||||
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
||||
curl_easy_setopt(curl, CURLOPT_READDATA, ut);
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
|
||||
@@ -146,8 +146,8 @@ static void uwsgi_alarm_curl_loop(struct uwsgi_thread *ut) {
|
||||
// ARGH !!!
|
||||
if (!curl) return;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, uwsgi.socket_timeout);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, uwsgi.socket_timeout);
|
||||
curl_easy_setopt(curl, CURLOPT_READFUNCTION, uwsgi_alarm_curl_read_callback);
|
||||
curl_easy_setopt(curl, CURLOPT_READDATA, ut);
|
||||
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
||||
|
||||
@@ -281,7 +281,7 @@ static int carbon_push_stats(int retry_cycle, time_t now) {
|
||||
total_busyness += worker_busyness;
|
||||
u_carbon.was_busy[i-1] = 0;
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] == 1 || uwsgi.force_get_memusage) {
|
||||
if (uwsgi.logging_options.memory_report == 1 || uwsgi.force_get_memusage) {
|
||||
// only running workers are counted in total memory stats and if memory-report option is enabled
|
||||
total_rss += uwsgi.workers[i].rss_size;
|
||||
total_vsz += uwsgi.workers[i].vsz_size;
|
||||
@@ -294,7 +294,7 @@ static int carbon_push_stats(int retry_cycle, time_t now) {
|
||||
wok = carbon_write(fd, "%s%s.%s.worker%d.requests %llu %llu\n", u_carbon.root_node, u_carbon.hostname, u_carbon.id, i, (unsigned long long) uwsgi.workers[i].requests, (unsigned long long) now);
|
||||
if (!wok) goto clear;
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] == 1 || uwsgi.force_get_memusage) {
|
||||
if (uwsgi.logging_options.memory_report == 1 || uwsgi.force_get_memusage) {
|
||||
wok = carbon_write(fd, "%s%s.%s.worker%d.rss_size %llu %llu\n", u_carbon.root_node, u_carbon.hostname, u_carbon.id, i, (unsigned long long) uwsgi.workers[i].rss_size, (unsigned long long) now);
|
||||
if (!wok) goto clear;
|
||||
|
||||
@@ -327,7 +327,7 @@ static int carbon_push_stats(int retry_cycle, time_t now) {
|
||||
|
||||
}
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] == 1 || uwsgi.force_get_memusage) {
|
||||
if (uwsgi.logging_options.memory_report == 1 || uwsgi.force_get_memusage) {
|
||||
wok = carbon_write(fd, "%s%s.%s.rss_size %llu %llu\n", u_carbon.root_node, u_carbon.hostname, u_carbon.id, (unsigned long long) total_rss, (unsigned long long) now);
|
||||
if (!wok) goto clear;
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ int cheaper_backlog2_algo(int can_spawn) {
|
||||
|
||||
int i;
|
||||
#ifdef __linux__
|
||||
int backlog = uwsgi.shared->options[UWSGI_OPTION_BACKLOG_STATUS];
|
||||
int backlog = uwsgi.socket_timeout;
|
||||
#else
|
||||
int backlog = 0;
|
||||
#endif
|
||||
|
||||
@@ -13,11 +13,13 @@ struct uwsgi_cheaper_busyness_global {
|
||||
uint64_t busyness_max;
|
||||
uint64_t busyness_min;
|
||||
uint64_t *last_values;
|
||||
uint64_t *current_busyness;
|
||||
uint64_t total_avg_busyness;
|
||||
int *was_busy;
|
||||
uint64_t tcheck;
|
||||
uint64_t last_cheaped; // last time worker was cheaped due to low busyness
|
||||
uint64_t next_cheap; // timestamp, we can cheap worker after it
|
||||
uint64_t penalty; // penelty for respawning to fast, it will be added to multiplier
|
||||
uint64_t penalty; // penalty for respawning to fast, it will be added to multiplier
|
||||
uint64_t min_multi; //initial multiplier will be stored here
|
||||
uint64_t cheap_multi; // current multiplier value
|
||||
int last_action; // 1 - spawn workers ; 2 - cheap worker
|
||||
@@ -41,7 +43,7 @@ struct uwsgi_option uwsgi_cheaper_busyness_options[] = {
|
||||
uwsgi_opt_set_64bit, &uwsgi_cheaper_busyness_global.busyness_max, 0},
|
||||
|
||||
{"cheaper-busyness-min", required_argument, 0,
|
||||
"set the cheaper busyness low percent limit, belowe that value worker is considered idle (default 25)",
|
||||
"set the cheaper busyness low percent limit, below that value worker is considered idle (default 25)",
|
||||
uwsgi_opt_set_64bit, &uwsgi_cheaper_busyness_global.busyness_min, 0},
|
||||
|
||||
{"cheaper-busyness-multiplier", required_argument, 0,
|
||||
@@ -57,7 +59,7 @@ struct uwsgi_option uwsgi_cheaper_busyness_options[] = {
|
||||
|
||||
#ifdef __linux__
|
||||
{"cheaper-busyness-backlog-alert", required_argument, 0,
|
||||
"spawn emergency worker(s) if anytime listen queue is higher than this value (default 33)",
|
||||
"spawn emergency worker(s) if any time listen queue is higher than this value (default 33)",
|
||||
uwsgi_opt_set_int, &uwsgi_cheaper_busyness_global.backlog_alert, 0},
|
||||
{"cheaper-busyness-backlog-multiplier", required_argument, 0,
|
||||
"set cheaper multiplier used for emergency workers (default 3)",
|
||||
@@ -148,46 +150,6 @@ int cheaper_busyness_algo(int can_spawn) {
|
||||
// we use microseconds
|
||||
uint64_t t = uwsgi.cheaper_overload*1000000;
|
||||
|
||||
// this happens on the first run, the required memory is allocated
|
||||
if (!uwsgi_cheaper_busyness_global.last_values) {
|
||||
uwsgi_cheaper_busyness_global.last_values = uwsgi_calloc(sizeof(uint64_t) * uwsgi.numproc);
|
||||
}
|
||||
if (!uwsgi_cheaper_busyness_global.was_busy) {
|
||||
uwsgi_cheaper_busyness_global.was_busy = uwsgi_calloc(sizeof(int) * uwsgi.numproc);
|
||||
}
|
||||
|
||||
// set defaults
|
||||
if (!uwsgi_cheaper_busyness_global.busyness_max) uwsgi_cheaper_busyness_global.busyness_max = 50;
|
||||
if (!uwsgi_cheaper_busyness_global.busyness_min) uwsgi_cheaper_busyness_global.busyness_min = 25;
|
||||
if (!uwsgi_cheaper_busyness_global.cheap_multi) uwsgi_cheaper_busyness_global.cheap_multi = 10;
|
||||
if (!uwsgi_cheaper_busyness_global.penalty) uwsgi_cheaper_busyness_global.penalty = 2;
|
||||
|
||||
#ifdef __linux__
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_alert) uwsgi_cheaper_busyness_global.backlog_alert = 33;
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_multi) uwsgi_cheaper_busyness_global.backlog_multi = 3;
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_step) uwsgi_cheaper_busyness_global.backlog_step = 1;
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_nonzero_alert) uwsgi_cheaper_busyness_global.backlog_nonzero_alert = 60;
|
||||
#endif
|
||||
|
||||
if (!uwsgi_cheaper_busyness_global.min_multi) {
|
||||
// store initial multiplier so we don't loose its initial value
|
||||
uwsgi_cheaper_busyness_global.min_multi = uwsgi_cheaper_busyness_global.cheap_multi;
|
||||
// since this is first run we will print current values
|
||||
uwsgi_log("[busyness] settings: min=%llu%%, max=%llu%%, overload=%llu, multiplier=%llu, respawn penalty=%llu\n",
|
||||
uwsgi_cheaper_busyness_global.busyness_min, uwsgi_cheaper_busyness_global.busyness_max,
|
||||
uwsgi.cheaper_overload, uwsgi_cheaper_busyness_global.cheap_multi, uwsgi_cheaper_busyness_global.penalty);
|
||||
#ifdef __linux__
|
||||
uwsgi_log("[busyness] backlog alert is set to %d request(s), step is %d\n",
|
||||
uwsgi_cheaper_busyness_global.backlog_alert, uwsgi_cheaper_busyness_global.backlog_step);
|
||||
uwsgi_log("[busyness] backlog non-zero alert is set to %llu second(s)\n", uwsgi_cheaper_busyness_global.backlog_nonzero_alert);
|
||||
#endif
|
||||
}
|
||||
|
||||
// initialize with current time
|
||||
if (uwsgi_cheaper_busyness_global.tcheck == 0) uwsgi_cheaper_busyness_global.tcheck = uwsgi_micros();
|
||||
|
||||
if (uwsgi_cheaper_busyness_global.next_cheap == 0) set_next_cheap_time();
|
||||
|
||||
int active_workers = 0;
|
||||
uint64_t total_busyness = 0;
|
||||
uint64_t avg_busyness = 0;
|
||||
@@ -202,7 +164,7 @@ int cheaper_busyness_algo(int can_spawn) {
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
int backlog = uwsgi.shared->options[UWSGI_OPTION_BACKLOG_STATUS];
|
||||
int backlog = uwsgi.shared->backlog;
|
||||
#endif
|
||||
|
||||
uint64_t now = uwsgi_micros();
|
||||
@@ -228,11 +190,21 @@ int cheaper_busyness_algo(int can_spawn) {
|
||||
if (uwsgi_cheaper_busyness_global.verbose && active_workers > 1)
|
||||
uwsgi_log("[busyness] worker nr %d %llus average busyness is at %llu%%\n",
|
||||
i+1, uwsgi.cheaper_overload, percent);
|
||||
if (uwsgi.has_metrics) {
|
||||
// update metrics
|
||||
uwsgi_wlock(uwsgi.metrics_lock);
|
||||
uwsgi_cheaper_busyness_global.current_busyness[i] = percent;
|
||||
uwsgi_rwunlock(uwsgi.metrics_lock);
|
||||
}
|
||||
}
|
||||
uwsgi_cheaper_busyness_global.last_values[i] = uwsgi.workers[i+1].running_time;
|
||||
}
|
||||
|
||||
avg_busyness = (active_workers ? total_busyness / active_workers : 0);
|
||||
uwsgi_wlock(uwsgi.metrics_lock);
|
||||
uwsgi_cheaper_busyness_global.total_avg_busyness = avg_busyness;
|
||||
uwsgi_rwunlock(uwsgi.metrics_lock);
|
||||
|
||||
if (uwsgi_cheaper_busyness_global.verbose)
|
||||
uwsgi_log("[busyness] %ds average busyness of %d worker(s) is at %d%%\n",
|
||||
(int) uwsgi.cheaper_overload, (int) active_workers, (int) avg_busyness);
|
||||
@@ -261,7 +233,7 @@ int cheaper_busyness_algo(int can_spawn) {
|
||||
// worker was cheaped and then spawned back in less than current multiplier*cheaper_overload seconds
|
||||
// we will increase the multiplier so that next time worker will need to wait longer before being cheaped
|
||||
uwsgi_cheaper_busyness_global.cheap_multi += uwsgi_cheaper_busyness_global.penalty;
|
||||
uwsgi_log("[busyness] worker(s) respawned to fast, increasing chpeaper multiplier to %llu (+%llu)\n",
|
||||
uwsgi_log("[busyness] worker(s) respawned to fast, increasing cheaper multiplier to %llu (+%llu)\n",
|
||||
uwsgi_cheaper_busyness_global.cheap_multi, uwsgi_cheaper_busyness_global.penalty);
|
||||
} else {
|
||||
decrease_multi();
|
||||
@@ -333,7 +305,7 @@ int cheaper_busyness_algo(int can_spawn) {
|
||||
// time needed to cheap them, than a lot min<busy<max when we do not reset timer
|
||||
// and then another idle cycle than would trigger cheaping
|
||||
if (uwsgi_cheaper_busyness_global.verbose)
|
||||
uwsgi_log("[busyness] %llus average busyness is at %llu%%, %llu non-idle cycle(s), reseting cheaper timer\n",
|
||||
uwsgi_log("[busyness] %llus average busyness is at %llu%%, %llu non-idle cycle(s), resetting cheaper timer\n",
|
||||
uwsgi.cheaper_overload, avg_busyness, uwsgi_cheaper_busyness_global.tolerance_counter);
|
||||
set_next_cheap_time();
|
||||
} else {
|
||||
@@ -385,10 +357,73 @@ void uwsgi_cheaper_register_busyness(void) {
|
||||
uwsgi_register_cheaper_algo("busyness", cheaper_busyness_algo);
|
||||
}
|
||||
|
||||
static int uwsgi_cheaper_busyness_init(void) {
|
||||
if (!uwsgi.requested_cheaper_algo || strcmp(uwsgi.requested_cheaper_algo, "busyness")) return 0;
|
||||
// this happens on the first run, the required memory is allocated
|
||||
uwsgi_cheaper_busyness_global.last_values = uwsgi_calloc(sizeof(uint64_t) * uwsgi.numproc);
|
||||
uwsgi_cheaper_busyness_global.was_busy = uwsgi_calloc(sizeof(int) * uwsgi.numproc);
|
||||
|
||||
if (uwsgi.has_metrics) {
|
||||
// allocate metrics memory
|
||||
uwsgi_cheaper_busyness_global.current_busyness = uwsgi_calloc(sizeof(uint64_t) * uwsgi.numproc);
|
||||
}
|
||||
|
||||
// set defaults
|
||||
if (!uwsgi_cheaper_busyness_global.busyness_max) uwsgi_cheaper_busyness_global.busyness_max = 50;
|
||||
if (!uwsgi_cheaper_busyness_global.busyness_min) uwsgi_cheaper_busyness_global.busyness_min = 25;
|
||||
if (!uwsgi_cheaper_busyness_global.cheap_multi) uwsgi_cheaper_busyness_global.cheap_multi = 10;
|
||||
if (!uwsgi_cheaper_busyness_global.penalty) uwsgi_cheaper_busyness_global.penalty = 2;
|
||||
|
||||
#ifdef __linux__
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_alert) uwsgi_cheaper_busyness_global.backlog_alert = 33;
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_multi) uwsgi_cheaper_busyness_global.backlog_multi = 3;
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_step) uwsgi_cheaper_busyness_global.backlog_step = 1;
|
||||
if (!uwsgi_cheaper_busyness_global.backlog_nonzero_alert) uwsgi_cheaper_busyness_global.backlog_nonzero_alert = 60;
|
||||
#endif
|
||||
|
||||
// store initial multiplier so we don't loose that value
|
||||
uwsgi_cheaper_busyness_global.min_multi = uwsgi_cheaper_busyness_global.cheap_multi;
|
||||
// since this is first run we will print current values
|
||||
uwsgi_log("[busyness] settings: min=%llu%%, max=%llu%%, overload=%llu, multiplier=%llu, respawn penalty=%llu\n",
|
||||
uwsgi_cheaper_busyness_global.busyness_min, uwsgi_cheaper_busyness_global.busyness_max,
|
||||
uwsgi.cheaper_overload, uwsgi_cheaper_busyness_global.cheap_multi, uwsgi_cheaper_busyness_global.penalty);
|
||||
#ifdef __linux__
|
||||
uwsgi_log("[busyness] backlog alert is set to %d request(s), step is %d\n",
|
||||
uwsgi_cheaper_busyness_global.backlog_alert, uwsgi_cheaper_busyness_global.backlog_step);
|
||||
uwsgi_log("[busyness] backlog non-zero alert is set to %llu second(s)\n", uwsgi_cheaper_busyness_global.backlog_nonzero_alert);
|
||||
#endif
|
||||
|
||||
// register metrics if enabled
|
||||
if (uwsgi.has_metrics) {
|
||||
int i;
|
||||
char buf[4096];
|
||||
char buf2[4096];
|
||||
for (i = 0; i < uwsgi.numproc; i++) {
|
||||
if (snprintf(buf, 4096, "worker.%d.plugin.cheaper_busyness.busyness", i+1) <= 0) {
|
||||
uwsgi_log("[busyness] unable to register busyness metric for worker %d\n", i+1);
|
||||
exit(1);
|
||||
}
|
||||
if (snprintf(buf2, 4096, "3.%d.100.1", i+1) <= 0) {
|
||||
uwsgi_log("[busyness] unable to register busyness metric oid for worker %d\n", i+1);
|
||||
exit(1);
|
||||
}
|
||||
uwsgi_register_metric(buf, buf2, UWSGI_METRIC_GAUGE, "ptr", &uwsgi_cheaper_busyness_global.current_busyness[i], 0, NULL);
|
||||
}
|
||||
uwsgi_register_metric("plugin.cheaper_busyness.total_avg_busyness", "4.100.1", UWSGI_METRIC_GAUGE, "ptr", &uwsgi_cheaper_busyness_global.total_avg_busyness, 0, NULL);
|
||||
uwsgi_log("[busyness] metrics registered\n");
|
||||
}
|
||||
|
||||
// initialize timers
|
||||
uwsgi_cheaper_busyness_global.tcheck = uwsgi_micros();
|
||||
set_next_cheap_time();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct uwsgi_plugin cheaper_busyness_plugin = {
|
||||
|
||||
.name = "cheaper_busyness",
|
||||
.on_load = uwsgi_cheaper_register_busyness,
|
||||
.on_load = uwsgi_cheaper_register_busyness,
|
||||
.options = uwsgi_cheaper_busyness_options,
|
||||
|
||||
.init = uwsgi_cheaper_busyness_init
|
||||
};
|
||||
|
||||
@@ -272,6 +272,18 @@ void corerouter_manage_subscription(char *key, uint16_t keylen, char *val, uint1
|
||||
usr->sign = val;
|
||||
usr->sign_len = vallen;
|
||||
}
|
||||
else if (!uwsgi_strncmp("sni_key", 7, key, keylen)) {
|
||||
usr->sni_key = val;
|
||||
usr->sni_key_len = vallen;
|
||||
}
|
||||
else if (!uwsgi_strncmp("sni_crt", 7, key, keylen)) {
|
||||
usr->sni_crt = val;
|
||||
usr->sni_crt_len = vallen;
|
||||
}
|
||||
else if (!uwsgi_strncmp("sni_ca", 6, key, keylen)) {
|
||||
usr->sni_ca = val;
|
||||
usr->sni_ca_len = vallen;
|
||||
}
|
||||
}
|
||||
|
||||
void corerouter_close_peer(struct uwsgi_corerouter *ucr, struct corerouter_peer *peer) {
|
||||
@@ -919,10 +931,13 @@ int uwsgi_corerouter_init(struct uwsgi_corerouter *ucr) {
|
||||
uwsgi_log("starting %s in cheap mode\n", ucr->name);
|
||||
}
|
||||
for (i = 0; i < ucr->processes; i++) {
|
||||
if (register_gateway(ucr->name, uwsgi_corerouter_loop, ucr) == NULL) {
|
||||
struct uwsgi_gateway *ug = register_gateway(ucr->name, uwsgi_corerouter_loop, ucr);
|
||||
if (ug == NULL) {
|
||||
uwsgi_log("unable to register the %s gateway\n", ucr->name);
|
||||
exit(1);
|
||||
}
|
||||
ug->uid = ucr->uid;
|
||||
ug->gid = ucr->gid;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1021,6 +1036,9 @@ void corerouter_send_stats(struct uwsgi_corerouter *ucr) {
|
||||
if (uwsgi_stats_keyvaln_comma(us, "key", s_slot->key, s_slot->keylen)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "hash", (unsigned long long) s_slot->hash)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "hits", (unsigned long long) s_slot->hits)) goto end0;
|
||||
#ifdef UWSGI_SSL
|
||||
if (uwsgi_stats_keylong_comma(us, "sni_enabled", (unsigned long long) s_slot->sni_enabled)) goto end0;
|
||||
#endif
|
||||
|
||||
if (uwsgi_stats_key(us , "nodes")) goto end0;
|
||||
if (uwsgi_stats_list_open(us)) goto end0;
|
||||
@@ -1034,9 +1052,13 @@ void corerouter_send_stats(struct uwsgi_corerouter *ucr) {
|
||||
if (uwsgi_stats_keylong_comma(us, "modifier1", (unsigned long long) s_node->modifier1)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "modifier2", (unsigned long long) s_node->modifier2)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "last_check", (unsigned long long) s_node->last_check)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "pid", (unsigned long long) s_node->pid)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "uid", (unsigned long long) s_node->uid)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "gid", (unsigned long long) s_node->gid)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "requests", (unsigned long long) s_node->requests)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "last_requests", (unsigned long long) s_node->last_requests)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "tx", (unsigned long long) s_node->transferred)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "tx", (unsigned long long) s_node->tx)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "rx", (unsigned long long) s_node->rx)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "cores", (unsigned long long) s_node->cores)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "load", (unsigned long long) s_node->load)) goto end0;
|
||||
if (uwsgi_stats_keylong_comma(us, "weight", (unsigned long long) s_node->weight)) goto end0;
|
||||
@@ -1076,7 +1098,7 @@ void corerouter_send_stats(struct uwsgi_corerouter *ucr) {
|
||||
size_t remains = us->pos;
|
||||
off_t pos = 0;
|
||||
while(remains > 0) {
|
||||
int ret = uwsgi_waitfd_write(client_fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi_waitfd_write(client_fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
goto end0;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
uwsgi_cr_error(peer, f);\
|
||||
return -1;\
|
||||
}\
|
||||
if (peer != peer->session->main_peer && peer->un) peer->un->rx+=len;\
|
||||
peer->out_pos += len;
|
||||
|
||||
#define cr_write_buf(peer, ubuf, f) write(peer->fd, ubuf->buf + ubuf##_pos, ubuf->pos - ubuf##_pos);\
|
||||
@@ -29,6 +30,7 @@
|
||||
uwsgi_cr_error(peer, f);\
|
||||
return -1;\
|
||||
}\
|
||||
if (peer != peer->session->main_peer && peer->un) peer->un->rx+=len;\
|
||||
ubuf##_pos += len;
|
||||
|
||||
#define cr_write_complete(peer) peer->out_pos == peer->out->pos
|
||||
@@ -51,6 +53,7 @@
|
||||
uwsgi_cr_error(peer, f);\
|
||||
return -1;\
|
||||
}\
|
||||
if (peer != peer->session->main_peer && peer->un) peer->un->tx+=len;\
|
||||
peer->in->pos += len;\
|
||||
|
||||
#define cr_read_exact(peer, l, f) read(peer->fd, peer->in->buf + peer->in->pos, (l - peer->in->pos));\
|
||||
@@ -59,6 +62,7 @@
|
||||
uwsgi_cr_error(peer, f);\
|
||||
return -1;\
|
||||
}\
|
||||
if (peer != peer->session->main_peer && peer->un) peer->un->tx+=len;\
|
||||
peer->in->pos += len;\
|
||||
|
||||
#define cr_reset_hooks(peer) if(!peer->session->main_peer->disabled) {\
|
||||
@@ -258,6 +262,9 @@ struct uwsgi_corerouter {
|
||||
|
||||
uint64_t active_sessions;
|
||||
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
|
||||
};
|
||||
|
||||
// a session is started when a client connect to the router
|
||||
|
||||
@@ -4,7 +4,7 @@ common functions for various routers (fastrouter, http...)
|
||||
|
||||
*/
|
||||
|
||||
#include "../../uwsgi.h"
|
||||
#include <uwsgi.h>
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
@@ -79,6 +79,9 @@ void uwsgi_corerouter_setup_sockets(struct uwsgi_corerouter *ucr) {
|
||||
}
|
||||
else {
|
||||
ugs->fd = bind_to_unix_dgram(ugs->name);
|
||||
if (ugs->fd < 1 || (uwsgi.subscriptions_use_credentials && uwsgi_socket_passcred(ugs->fd))) {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
uwsgi_socket_nb(ugs->fd);
|
||||
}
|
||||
@@ -113,10 +116,17 @@ void uwsgi_corerouter_manage_subscription(struct uwsgi_corerouter *ucr, int id,
|
||||
int i;
|
||||
struct uwsgi_subscribe_req usr;
|
||||
char bbuf[4096];
|
||||
ssize_t len = -1;
|
||||
|
||||
ssize_t len = recv(ugs->fd, bbuf, 4096, 0);
|
||||
memset(&usr, 0, sizeof(struct uwsgi_subscribe_req));
|
||||
|
||||
if (uwsgi.subscriptions_use_credentials) {
|
||||
len = uwsgi_recv_cred2(ugs->fd, bbuf, 4096, &usr.pid, &usr.uid, &usr.gid);
|
||||
}
|
||||
else {
|
||||
len = recv(ugs->fd, bbuf, 4096, 0);
|
||||
}
|
||||
if (len > 0) {
|
||||
memset(&usr, 0, sizeof(struct uwsgi_subscribe_req));
|
||||
uwsgi_hooked_parse(bbuf + 4, len - 4, corerouter_manage_subscription, &usr);
|
||||
if (usr.sign_len > 0) {
|
||||
// calc the base size
|
||||
|
||||
@@ -12,8 +12,8 @@ static void uwsgi_opt_setup_coroae(char *opt, char *value, void *null) {
|
||||
|
||||
// set async mode
|
||||
uwsgi_opt_set_int(opt, value, &uwsgi.async);
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] < 30) {
|
||||
uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] = 30;
|
||||
if (uwsgi.socket_timeout < 30) {
|
||||
uwsgi.socket_timeout = 30;
|
||||
}
|
||||
// set loop engine
|
||||
uwsgi.loop = "coroae";
|
||||
@@ -164,7 +164,7 @@ XS(XS_coroae_accept_request) {
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
int ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.socket_timeout);
|
||||
wsgi_req->switches++;
|
||||
|
||||
if (ret <= 0) {
|
||||
@@ -244,8 +244,8 @@ edge:
|
||||
wsgi_req->start_of_request_in_sec = wsgi_req->start_of_request/1000000;
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ static void curl_cron_func(struct uwsgi_cron *uc, time_t now) {
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, uc->command);
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, uwsgi.socket_timeout);
|
||||
// use 1 minute as the cron resolution
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
|
||||
uwsgi_log("[uwsgi-curl-cron] requesting %s\n", uc->command);
|
||||
|
||||
@@ -25,7 +25,7 @@ extern "C" void uwsgi_imperial_monitor_mongodb(struct uwsgi_emperor_scanner *ues
|
||||
// the connection object (will be automatically destroyed at each cycle)
|
||||
mongo::DBClientConnection c;
|
||||
// set the socket timeout
|
||||
c.setSoTimeout(uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
c.setSoTimeout(uwsgi.socket_timeout);
|
||||
// connect
|
||||
c.connect(uems->address);
|
||||
|
||||
|
||||
@@ -264,7 +264,7 @@ static void uwsgi_gccgo_request_goroutine(void *arg) {
|
||||
int ret,status;
|
||||
|
||||
for(;;) {
|
||||
ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.socket_timeout);
|
||||
wsgi_req->switches++;
|
||||
|
||||
if (ret <= 0) {
|
||||
@@ -388,8 +388,8 @@ retry:
|
||||
wsgi_req->start_of_request_in_sec = wsgi_req->start_of_request/1000000;
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
// spawn the new goroutine
|
||||
|
||||
@@ -8,8 +8,8 @@ static void uwsgi_opt_setup_gevent(char *opt, char *value, void *null) {
|
||||
|
||||
// set async mode
|
||||
uwsgi_opt_set_int(opt, value, &uwsgi.async);
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] < 30) {
|
||||
uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] = 30;
|
||||
if (uwsgi.socket_timeout < 30) {
|
||||
uwsgi.socket_timeout = 30;
|
||||
}
|
||||
// set loop engine
|
||||
uwsgi.loop = "gevent";
|
||||
@@ -210,8 +210,8 @@ edge:
|
||||
wsgi_req->start_of_request_in_sec = wsgi_req->start_of_request/1000000;
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
// hack to easily pass wsgi_req pointer to the greenlet
|
||||
@@ -257,7 +257,7 @@ PyObject *py_uwsgi_gevent_request(PyObject * self, PyObject * args) {
|
||||
greenlet_switch = PyObject_GetAttrString(current_greenlet, "switch");
|
||||
|
||||
for(;;) {
|
||||
int ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.socket_timeout);
|
||||
wsgi_req->switches++;
|
||||
|
||||
if (ret <= 0) {
|
||||
@@ -349,7 +349,7 @@ static void gevent_loop() {
|
||||
uwsgi_log("!!! Running gevent without threads IS NOT recommended, enable them with --enable-threads !!!\n");
|
||||
}
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] < 30) {
|
||||
if (uwsgi.socket_timeout < 30) {
|
||||
uwsgi_log("!!! Running gevent with a socket-timeout lower than 30 seconds is not recommended, tune it with --socket-timeout !!!\n");
|
||||
}
|
||||
|
||||
|
||||
@@ -200,7 +200,7 @@ static void uwsgi_glusterfs_add_mountpoint(char *arg, size_t arg_len) {
|
||||
static void uwsgi_glusterfs_setup() {
|
||||
|
||||
if (!uglusterfs.timeout) {
|
||||
uglusterfs.timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
uglusterfs.timeout = uwsgi.socket_timeout;
|
||||
}
|
||||
|
||||
struct uwsgi_string_list *usl = uglusterfs.mountpoints;
|
||||
|
||||
@@ -90,7 +90,7 @@ ssize_t uwsgi_graylog2_logger(struct uwsgi_logger *ul, char *message, size_t len
|
||||
int rlen = snprintf(g2c.json_buf, MAX_GELF, "{ \"version\": \"1.0\", \"host\": \"%s\", \"short_message\": \"%.*s\", \"full_message\": \"%.*s\", \"timestamp\": %d, \"level\": 5, \"facility\": \"uWSGI-%s\" }",
|
||||
g2c.host, truncated, g2c.escaped_buf, (int)g2c.escaped_len, g2c.escaped_buf, (int) uwsgi_now(), UWSGI_VERSION);
|
||||
|
||||
if (rlen > 0) {
|
||||
if (rlen > 0 && rlen < MAX_GELF) {
|
||||
if (compressBound((uLong) rlen) <= MAX_GELF) {
|
||||
if (compress((Bytef *) ul->buf, &destLen, (Bytef *) g2c.json_buf, (uLong) rlen) == Z_OK) {
|
||||
return sendto(ul->fd, ul->buf, destLen, 0, (const struct sockaddr *) &ul->addr, ul->addr_len);
|
||||
|
||||
@@ -169,7 +169,7 @@ static struct uwsgi_gridfs_mountpoint *uwsgi_gridfs_add_mountpoint(char *arg, si
|
||||
ugm->timeout = atoi(ugm->timeout_str);
|
||||
}
|
||||
else {
|
||||
ugm->timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
ugm->timeout = uwsgi.socket_timeout;
|
||||
}
|
||||
|
||||
if (ugm->prefix) {
|
||||
|
||||
+7
-1
@@ -54,6 +54,8 @@ struct uwsgi_option http_options[] = {
|
||||
{"http-ss", required_argument, 0, "run the http router stats server", uwsgi_opt_set_str, &uhttp.cr.stats_server, 0},
|
||||
{"http-harakiri", required_argument, 0, "enable http router harakiri", uwsgi_opt_set_int, &uhttp.cr.harakiri, 0},
|
||||
{"http-stud-prefix", required_argument, 0, "expect a stud prefix (1byte family + 4/16 bytes address) on connections from the specified address", uwsgi_opt_add_addr_list, &uhttp.stud_prefix, 0},
|
||||
{"http-uid", required_argument, 0, "drop http routr privileges to the specified uid", uwsgi_opt_uid, &uhttp.cr.uid, 0 },
|
||||
{"http-gid", required_argument, 0, "drop http routr privileges to the specified gid", uwsgi_opt_gid, &uhttp.cr.gid, 0 },
|
||||
{0, 0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
@@ -616,6 +618,9 @@ ssize_t http_parse(struct corerouter_peer *main_peer) {
|
||||
|
||||
// is it http body ?
|
||||
if (hr->rnrn == 4) {
|
||||
// something bad happened in keepalive mode...
|
||||
if (!main_peer->session->peers) return -1;
|
||||
|
||||
if (hr->content_length == 0 && !hr->raw_body) {
|
||||
// ignore data...
|
||||
main_peer->in->pos = 0;
|
||||
@@ -869,7 +874,8 @@ int http_alloc_session(struct uwsgi_corerouter *ucr, struct uwsgi_gateway_socket
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
uwsgi_cr_set_hooks(cs->main_peer, cs->main_peer->last_hook_read, NULL);
|
||||
if (uwsgi_cr_set_hooks(cs->main_peer, cs->main_peer->last_hook_read, NULL))
|
||||
return -1;
|
||||
cs->close = hr_session_close;
|
||||
break;
|
||||
}
|
||||
|
||||
+2
-1
@@ -432,7 +432,8 @@ static uint16_t ldap_passwd_check(struct uwsgi_ldapauth_config *ulc, char *auth)
|
||||
LDAPMessage *msg, *entry;
|
||||
// use the minimal amount of memory
|
||||
char *filter = uwsgi_malloc( strlen(ulc->login_attr) + (colon-auth) + strlen(ulc->filter) + 7);
|
||||
if (snprintf(filter, 1024, "(&(%s=%.*s)%s)", ulc->login_attr, (int) (colon-auth), auth, ulc->filter) <= 0) {
|
||||
ret = snprintf(filter, 1024, "(&(%s=%.*s)%s)", ulc->login_attr, (int) (colon-auth), auth, ulc->filter);
|
||||
if (ret <= 0 || ret >= 1024) {
|
||||
free(filter);
|
||||
uwsgi_error("ldap_passwd_check()/sprintfn(filter)");
|
||||
goto close;
|
||||
|
||||
@@ -7,14 +7,16 @@ static int uwsgi_libtcc_hook(char *arg) {
|
||||
char *func_base = "uwsgi_libtcc_func";
|
||||
size_t func_len = strlen(func_base) + sizeof(UMAX64_STR);
|
||||
char *func_name = uwsgi_malloc(func_len);
|
||||
if (snprintf(func_name, func_len, "%s%llu", func_base, uwsgi_libtcc_counter) < (int) strlen(func_base)) {
|
||||
int ret = snprintf(func_name, func_len, "%s%llu", func_base, uwsgi_libtcc_counter);
|
||||
if (ret < (int) strlen(func_base) || ret >= (int) func_len) {
|
||||
free(func_name);
|
||||
return -1;
|
||||
}
|
||||
uwsgi_libtcc_counter++;
|
||||
size_t source_len = 64 + func_len + strlen(arg);
|
||||
char *source = uwsgi_malloc(source_len);
|
||||
if (snprintf(source, source_len, "void %s() { %s ;}", func_name, arg) < (int) ( strlen(func_base) + strlen(arg))) {
|
||||
ret = snprintf(source, source_len, "void %s() { %s ;}", func_name, arg);
|
||||
if (ret < (int) ( strlen(func_base) + strlen(arg)) || ret >= (int) (source_len)) {
|
||||
free(func_name);
|
||||
free(source);
|
||||
return -1;
|
||||
|
||||
+273
-6
@@ -5,8 +5,10 @@
|
||||
#include <lauxlib.h>
|
||||
|
||||
#if LUA_VERSION_NUM < 502
|
||||
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
|
||||
# define luaL_newuwsgilib(L,l) luaL_register(L, "uwsgi",l)
|
||||
# define lua_rawlen lua_objlen
|
||||
#else
|
||||
# define luaL_newuwsgilib(L,l) lua_newtable(L);luaL_setfuncs (L, l, 0);lua_pushvalue(L,-1);lua_setglobal(L,"uwsgi")
|
||||
#endif
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
@@ -16,6 +18,7 @@ struct uwsgi_lua {
|
||||
char *shell;
|
||||
char *filename;
|
||||
struct uwsgi_string_list *load;
|
||||
int gc_freq;
|
||||
} ulua;
|
||||
|
||||
struct uwsgi_plugin lua_plugin;
|
||||
@@ -40,6 +43,7 @@ static struct uwsgi_option uwsgi_lua_options[] = {
|
||||
{"lua-load", required_argument, 0, "load a lua file", uwsgi_opt_add_string_list, &ulua.load, 0},
|
||||
{"lua-shell", no_argument, 0, "run the lua interactive shell (debug.debug())", uwsgi_opt_luashell, NULL, 0},
|
||||
{"luashell", no_argument, 0, "run the lua interactive shell (debug.debug())", uwsgi_opt_luashell, NULL, 0},
|
||||
{"lua-gc-freq", no_argument, 0, "set the lua gc frequency (default: 0, runs after every request)", uwsgi_opt_set_int, &ulua.gc_freq, 0},
|
||||
|
||||
{0, 0, 0, 0},
|
||||
|
||||
@@ -265,6 +269,250 @@ error:
|
||||
|
||||
}
|
||||
|
||||
static int uwsgi_api_async_sleep(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto end;
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
int timeout = lua_tonumber(L, 1);
|
||||
|
||||
if (timeout >= 0) {
|
||||
async_add_timeout(wsgi_req, timeout);
|
||||
}
|
||||
end:
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_wait_fd_read(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto end;
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
int fd = lua_tonumber(L, 1);
|
||||
int timeout = 0;
|
||||
if (argc > 1) {
|
||||
timeout = lua_tonumber(L, 2);
|
||||
}
|
||||
|
||||
if (async_add_fd_read(wsgi_req, fd, timeout)) {
|
||||
lua_pushstring(L, "unable to call async_add_fd_read()");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
end:
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_wait_fd_write(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto end;
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
int fd = lua_tonumber(L, 1);
|
||||
int timeout = 0;
|
||||
if (argc > 1) {
|
||||
timeout = lua_tonumber(L, 2);
|
||||
}
|
||||
|
||||
if (async_add_fd_write(wsgi_req, fd, timeout)) {
|
||||
lua_pushstring(L, "unable to call async_add_fd_write()");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
end:
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_async_connect(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto end;
|
||||
|
||||
int fd = uwsgi_connect((char *)lua_tostring(L, 1), 0, 1);
|
||||
lua_pushnumber(L, fd);
|
||||
return 1;
|
||||
end:
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_is_connected(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto end;
|
||||
int fd = lua_tonumber(L, 1);
|
||||
if (uwsgi_is_connected(fd)) {
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
lua_pushboolean(L, 0);
|
||||
return 1;
|
||||
end:
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_close(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto end;
|
||||
int fd = lua_tonumber(L, 1);
|
||||
close(fd);
|
||||
end:
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int uwsgi_api_ready_fd(lua_State *L) {
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
int fd = uwsgi_ready_fd(wsgi_req);
|
||||
lua_pushnumber(L, fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_websocket_handshake(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
|
||||
const char *key = NULL, *origin = NULL, *proto = NULL;
|
||||
size_t key_len = 0, origin_len = 0, proto_len = 0;
|
||||
|
||||
if (argc > 0) {
|
||||
key = lua_tolstring(L, 1, &key_len);
|
||||
if (argc > 1) {
|
||||
origin = lua_tolstring(L, 2, &origin_len);
|
||||
if (argc > 2) {
|
||||
proto = lua_tolstring(L, 3, &proto_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
if (uwsgi_websocket_handshake(wsgi_req, (char *)key, key_len, (char *)origin, origin_len, (char *) proto, proto_len)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
|
||||
error:
|
||||
lua_pushstring(L, "unable to complete websocket handshake");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_api_websocket_send(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto error;
|
||||
|
||||
size_t message_len = 0;
|
||||
const char *message = lua_tolstring(L, 1, &message_len);
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
if (uwsgi_websocket_send(wsgi_req, (char *) message, message_len)) {
|
||||
goto error;
|
||||
}
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
error:
|
||||
lua_pushstring(L, "unable to send websocket message");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_api_websocket_send_binary(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc == 0) goto error;
|
||||
|
||||
size_t message_len = 0;
|
||||
const char *message = lua_tolstring(L, 1, &message_len);
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
if (uwsgi_websocket_send_binary(wsgi_req, (char *) message, message_len)) {
|
||||
goto error;
|
||||
}
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
error:
|
||||
lua_pushstring(L, "unable to send websocket binary message");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_api_websocket_send_from_sharedarea(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc < 2) goto error;
|
||||
|
||||
int id = lua_tonumber(L, 1);
|
||||
uint64_t pos = lua_tonumber(L, 2);
|
||||
uint64_t len = 0;
|
||||
if (argc > 2) {
|
||||
len = lua_tonumber(L, 3);
|
||||
}
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
if (uwsgi_websocket_send_from_sharedarea(wsgi_req, id, pos, len)) {
|
||||
goto error;
|
||||
}
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
error:
|
||||
lua_pushstring(L, "unable to send websocket message from sharedarea");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_api_websocket_send_binary_from_sharedarea(lua_State *L) {
|
||||
uint8_t argc = lua_gettop(L);
|
||||
if (argc < 2) goto error;
|
||||
|
||||
int id = lua_tonumber(L, 1);
|
||||
uint64_t pos = lua_tonumber(L, 2);
|
||||
uint64_t len = 0;
|
||||
if (argc > 2) {
|
||||
len = lua_tonumber(L, 3);
|
||||
}
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
if (uwsgi_websocket_send_binary_from_sharedarea(wsgi_req, id, pos, len)) {
|
||||
goto error;
|
||||
}
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
error:
|
||||
lua_pushstring(L, "unable to send websocket message from sharedarea");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_api_websocket_recv(lua_State *L) {
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
struct uwsgi_buffer *ub = uwsgi_websocket_recv(wsgi_req);
|
||||
if (!ub) {
|
||||
lua_pushstring(L, "unable to receive websocket message");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
lua_pushlstring(L, ub->buf, ub->pos);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_websocket_recv_nb(lua_State *L) {
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
struct uwsgi_buffer *ub = uwsgi_websocket_recv_nb(wsgi_req);
|
||||
if (!ub) {
|
||||
lua_pushstring(L, "unable to receive websocket message");
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
lua_pushlstring(L, ub->buf, ub->pos);
|
||||
uwsgi_buffer_destroy(ub);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int uwsgi_api_cache_get(lua_State *L) {
|
||||
|
||||
@@ -366,9 +614,25 @@ static const luaL_Reg uwsgi_api[] = {
|
||||
{"register_signal", uwsgi_api_register_signal},
|
||||
{"register_rpc", uwsgi_api_register_rpc},
|
||||
|
||||
{"websocket_handshake", uwsgi_api_websocket_handshake},
|
||||
{"websocket_recv", uwsgi_api_websocket_recv},
|
||||
{"websocket_recv_nb", uwsgi_api_websocket_recv_nb},
|
||||
{"websocket_send", uwsgi_api_websocket_send},
|
||||
{"websocket_send_from_sharedarea", uwsgi_api_websocket_send_from_sharedarea},
|
||||
{"websocket_send_binary", uwsgi_api_websocket_send_binary},
|
||||
{"websocket_send_binary_from_sharedarea", uwsgi_api_websocket_send_binary_from_sharedarea},
|
||||
|
||||
{"lock", uwsgi_api_lock},
|
||||
{"unlock", uwsgi_api_unlock},
|
||||
|
||||
{"async_sleep", uwsgi_api_async_sleep},
|
||||
{"async_connect", uwsgi_api_async_connect},
|
||||
{"is_connected", uwsgi_api_is_connected},
|
||||
{"close", uwsgi_api_close},
|
||||
{"wait_fd_read", uwsgi_api_wait_fd_read},
|
||||
{"wait_fd_write", uwsgi_api_wait_fd_write},
|
||||
{"ready_fd", uwsgi_api_ready_fd},
|
||||
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -416,7 +680,7 @@ static void uwsgi_lua_app() {
|
||||
for(i=0;i<uwsgi.cores;i++) {
|
||||
ulua.L[i] = luaL_newstate();
|
||||
luaL_openlibs(ulua.L[i]);
|
||||
luaL_newlib(ulua.L[i], uwsgi_api);
|
||||
luaL_newuwsgilib(ulua.L[i], uwsgi_api);
|
||||
|
||||
lua_pushstring(ulua.L[i], UWSGI_VERSION);
|
||||
lua_setfield(ulua.L[i], -2, "version");
|
||||
@@ -526,7 +790,7 @@ static int uwsgi_lua_request(struct wsgi_request *wsgi_req) {
|
||||
uwsgi_log("%s\n", lua_tostring(L, -1));
|
||||
lua_pop(L, 1);
|
||||
lua_pushvalue(L, -1);
|
||||
goto clear;
|
||||
goto clear2;
|
||||
}
|
||||
|
||||
//uwsgi_log("%d %s %s %s\n",i,lua_typename(L, lua_type(L, -3)), lua_typename(L, lua_type(L, -2)) , lua_typename(L, lua_type(L, -1)));
|
||||
@@ -534,7 +798,8 @@ static int uwsgi_lua_request(struct wsgi_request *wsgi_req) {
|
||||
// send status
|
||||
if (lua_type(L, -3) == LUA_TSTRING || lua_type(L, -3) == LUA_TNUMBER) {
|
||||
http = lua_tolstring(L, -3, &slen);
|
||||
uwsgi_response_prepare_headers(wsgi_req, (char *) http, slen);
|
||||
if (uwsgi_response_prepare_headers(wsgi_req, (char *) http, slen))
|
||||
goto clear2;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("[uwsgi-lua] invalid response status !!!\n");
|
||||
@@ -565,11 +830,13 @@ static int uwsgi_lua_request(struct wsgi_request *wsgi_req) {
|
||||
return UWSGI_AGAIN;
|
||||
}
|
||||
}
|
||||
|
||||
clear:
|
||||
lua_pop(L, 4);
|
||||
clear2:
|
||||
// set frequency
|
||||
lua_gc(L, LUA_GCCOLLECT, 0);
|
||||
if (!ulua.gc_freq || uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].requests % ulua.gc_freq == 0) {
|
||||
lua_gc(L, LUA_GCCOLLECT, 0);
|
||||
}
|
||||
|
||||
return UWSGI_OK;
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ done:
|
||||
ums = (struct uwsgi_mongodb_state *) ul->data;
|
||||
|
||||
if (ums->fd == -1) {
|
||||
ums->fd = uwsgi_connect(ums->address, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0);
|
||||
ums->fd = uwsgi_connect(ums->address, uwsgi.socket_timeout, 0);
|
||||
}
|
||||
|
||||
if (ums->fd == -1) return -1;
|
||||
|
||||
@@ -26,7 +26,7 @@ int nagios() {
|
||||
}
|
||||
|
||||
|
||||
int fd = uwsgi_connect(uwsgi.sockets->name, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0);
|
||||
int fd = uwsgi_connect(uwsgi.sockets->name, uwsgi.socket_timeout, 0);
|
||||
if (fd < 0) {
|
||||
fprintf(stdout, "UWSGI CRITICAL: could not connect() to workers %s\n", strerror(errno));
|
||||
if (errno == EPERM || errno == EACCES) {
|
||||
@@ -45,7 +45,7 @@ int nagios() {
|
||||
}
|
||||
|
||||
|
||||
int ret = uwsgi_read_response(fd, &uh, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], &buf);
|
||||
int ret = uwsgi_read_response(fd, &uh, uwsgi.socket_timeout, &buf);
|
||||
|
||||
if (ret == -2) {
|
||||
fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n");
|
||||
|
||||
@@ -92,11 +92,13 @@ static int sapi_uwsgi_send_headers(sapi_headers_struct *sapi_headers)
|
||||
int hrc = SG(sapi_headers).http_response_code;
|
||||
if (!hrc) hrc = 200;
|
||||
uwsgi_num2str2n(hrc, status, 4);
|
||||
uwsgi_response_prepare_headers(wsgi_req, status, 3);
|
||||
if (uwsgi_response_prepare_headers(wsgi_req, status, 3))
|
||||
return SAPI_HEADER_SEND_FAILED;
|
||||
}
|
||||
else {
|
||||
char *sl = SG(sapi_headers).http_status_line;
|
||||
uwsgi_response_prepare_headers(wsgi_req, sl + 9 , strlen(sl + 9));
|
||||
if (uwsgi_response_prepare_headers(wsgi_req, sl + 9 , strlen(sl + 9)))
|
||||
return SAPI_HEADER_SEND_FAILED;
|
||||
}
|
||||
|
||||
h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
|
||||
|
||||
@@ -63,6 +63,8 @@ struct uwsgi_perl {
|
||||
|
||||
char *shell;
|
||||
int shell_oneshot;
|
||||
|
||||
CV *spooler;
|
||||
};
|
||||
|
||||
void init_perl_embedded_module(void);
|
||||
|
||||
@@ -889,6 +889,59 @@ static void uwsgi_perl_hijack(void) {
|
||||
|
||||
}
|
||||
|
||||
static void uwsgi_perl_add_item(char *key, uint16_t keylen, char *val, uint16_t vallen, void *data) {
|
||||
|
||||
HV *spool_dict = (HV*) data;
|
||||
|
||||
(void)hv_store(spool_dict, key, keylen, newSVpv(val, vallen), 0);
|
||||
}
|
||||
|
||||
|
||||
static int uwsgi_perl_spooler(char *filename, char *buf, uint16_t len, char *body, size_t body_len) {
|
||||
|
||||
int ret = -1;
|
||||
|
||||
if (!uperl.spooler) return 0;
|
||||
|
||||
dSP;
|
||||
ENTER;
|
||||
SAVETMPS;
|
||||
PUSHMARK(SP);
|
||||
|
||||
HV *spool_dict = newHV();
|
||||
|
||||
if (uwsgi_hooked_parse(buf, len, uwsgi_perl_add_item, (void *) spool_dict)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
(void) hv_store(spool_dict, "spooler_task_name", 18, newSVpv(filename, 0), 0);
|
||||
|
||||
if (body && body_len > 0) {
|
||||
(void) hv_store(spool_dict, "body", 4, newSVpv(body, body_len), 0);
|
||||
}
|
||||
|
||||
XPUSHs( sv_2mortal((SV*)newRV_noinc((SV*)spool_dict)) );
|
||||
PUTBACK;
|
||||
|
||||
call_sv( SvRV((SV*)uperl.spooler), G_SCALAR|G_EVAL);
|
||||
|
||||
SPAGAIN;
|
||||
if(SvTRUE(ERRSV)) {
|
||||
uwsgi_log("[uwsgi-spooler-perl error] %s", SvPV_nolen(ERRSV));
|
||||
ret = -1;
|
||||
}
|
||||
else {
|
||||
ret = POPi;
|
||||
}
|
||||
|
||||
PUTBACK;
|
||||
FREETMPS;
|
||||
LEAVE;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct uwsgi_plugin psgi_plugin = {
|
||||
|
||||
@@ -916,4 +969,6 @@ struct uwsgi_plugin psgi_plugin = {
|
||||
.atexit = uwsgi_perl_atexit,
|
||||
|
||||
.magic = uwsgi_perl_magic,
|
||||
|
||||
.spooler = uwsgi_perl_spooler,
|
||||
};
|
||||
|
||||
@@ -44,6 +44,8 @@ XS(XS_wait_fd_read) {
|
||||
croak("unable to add fd %d to the event queue", fd);
|
||||
}
|
||||
|
||||
wsgi_req->async_force_again = 1;
|
||||
|
||||
XSRETURN_UNDEF;
|
||||
}
|
||||
|
||||
@@ -243,6 +245,13 @@ XS(XS_register_signal) {
|
||||
|
||||
}
|
||||
|
||||
XS(XS_spooler) {
|
||||
dXSARGS;
|
||||
psgi_check_args(1);
|
||||
uperl.spooler = (CV *) newRV_inc(ST(0));
|
||||
XSRETURN_YES;
|
||||
}
|
||||
|
||||
XS(XS_register_rpc) {
|
||||
dXSARGS;
|
||||
|
||||
@@ -319,6 +328,14 @@ XS(XS_async_connect) {
|
||||
XSRETURN(1);
|
||||
}
|
||||
|
||||
XS(XS_ready_fd) {
|
||||
dXSARGS;
|
||||
psgi_check_args(0);
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
ST(0) = newSViv(uwsgi_ready_fd(wsgi_req));
|
||||
XSRETURN(1);
|
||||
}
|
||||
|
||||
XS(XS_call) {
|
||||
|
||||
dXSARGS;
|
||||
@@ -463,16 +480,24 @@ XS(XS_websocket_handshake) {
|
||||
char *origin = NULL;
|
||||
STRLEN origin_len = 0;
|
||||
|
||||
psgi_check_args(1);
|
||||
|
||||
key = SvPV(ST(0), key_len);
|
||||
char *proto = NULL;
|
||||
STRLEN proto_len = 0;
|
||||
|
||||
if (items > 1) {
|
||||
origin = SvPV(ST(0), origin_len);
|
||||
psgi_check_args(0);
|
||||
|
||||
if (items > 0) {
|
||||
key = SvPV(ST(0), key_len);
|
||||
if (items > 1) {
|
||||
origin = SvPV(ST(1), origin_len);
|
||||
if (items > 2) {
|
||||
proto = SvPV(ST(2), proto_len);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len)) {
|
||||
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len, proto, proto_len)) {
|
||||
croak("unable to complete websocket handshake");
|
||||
}
|
||||
|
||||
@@ -886,6 +911,7 @@ void init_perl_embedded_module() {
|
||||
psgi_xs(wait_fd_read);
|
||||
psgi_xs(wait_fd_write);
|
||||
psgi_xs(async_sleep);
|
||||
psgi_xs(ready_fd);
|
||||
psgi_xs(log);
|
||||
psgi_xs(async_connect);
|
||||
psgi_xs(suspend);
|
||||
@@ -929,4 +955,6 @@ void init_perl_embedded_module() {
|
||||
psgi_xs(sharedarea_readfast);
|
||||
psgi_xs(sharedarea_write);
|
||||
psgi_xs(sharedarea_wait);
|
||||
|
||||
psgi_xs(spooler);
|
||||
}
|
||||
|
||||
+1
-1
@@ -292,7 +292,7 @@ static int uwsgi_pty_client() {
|
||||
uwsgi.restore_tc = 1;
|
||||
}
|
||||
|
||||
upty.server_fd = uwsgi_connect(remote, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0);
|
||||
upty.server_fd = uwsgi_connect(remote, uwsgi.socket_timeout, 0);
|
||||
if (upty.server_fd < 0) {
|
||||
uwsgi_error("uwsgi_pty_client()/connect()");
|
||||
exit(1);
|
||||
|
||||
@@ -224,7 +224,7 @@ int async_add_fd_write(struct wsgi_request *, int, int);
|
||||
int async_add_fd_read(struct wsgi_request *, int, int);
|
||||
int uwsgi_connect(char *, int, int);
|
||||
|
||||
int uwsgi_websocket_handshake(struct wsgi_request *, char *, uint16_t, char *, uint16_t);
|
||||
int uwsgi_websocket_handshake(struct wsgi_request *, char *, uint16_t, char *, uint16_t, char *, uint16_t);
|
||||
int uwsgi_websocket_send(struct wsgi_request *, char *, size_t);
|
||||
struct uwsgi_buffer *uwsgi_websocket_recv(struct wsgi_request *);
|
||||
struct uwsgi_buffer *uwsgi_websocket_recv_nb(struct wsgi_request *);
|
||||
@@ -839,9 +839,12 @@ uwsgi.websocket_recv_nb = uwsgi_pypy_websocket_recv_nb
|
||||
"""
|
||||
uwsgi.websocket_handshake(key, origin)
|
||||
"""
|
||||
def uwsgi_pypy_websocket_handshake(key, origin=''):
|
||||
def uwsgi_pypy_websocket_handshake(key='', origin='', proto=''):
|
||||
wsgi_req = uwsgi_pypy_current_wsgi_req();
|
||||
if lib.uwsgi_websocket_handshake(wsgi_req, ffi.new('char[]', key), len(key), ffi.new('char[]',origin), len(origin)) < 0:
|
||||
c_key = ffi.new('char[]', key)
|
||||
c_origin = ffi.new('char[]', origin)
|
||||
c_proto = ffi.new('char[]', proto)
|
||||
if lib.uwsgi_websocket_handshake(wsgi_req, c_key, len(key), c_origin, len(origin), c_proto, len(proto)) < 0:
|
||||
raise IOError("unable to complete websocket handshake")
|
||||
uwsgi.websocket_handshake = uwsgi_pypy_websocket_handshake
|
||||
|
||||
|
||||
@@ -1585,7 +1585,11 @@ void uwsgi_python_add_item(char *key, uint16_t keylen, char *val, uint16_t valle
|
||||
|
||||
PyObject *pydict = (PyObject *) data;
|
||||
|
||||
PyDict_SetItem(pydict, PyString_FromStringAndSize(key, keylen), PyString_FromStringAndSize(val, vallen));
|
||||
PyObject *o_key = PyString_FromStringAndSize(key, keylen);
|
||||
PyObject *zero = PyString_FromStringAndSize(val, vallen);
|
||||
PyDict_SetItem(pydict, o_key, zero);
|
||||
Py_DECREF(o_key);
|
||||
Py_DECREF(zero);
|
||||
}
|
||||
|
||||
int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, size_t body_len) {
|
||||
@@ -1594,9 +1598,6 @@ int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, si
|
||||
|
||||
UWSGI_GET_GIL;
|
||||
|
||||
PyObject *spool_dict = PyDict_New();
|
||||
PyObject *spool_func, *pyargs, *ret;
|
||||
|
||||
if (!random_seed_reset) {
|
||||
uwsgi_python_reset_random_seed();
|
||||
random_seed_reset = 1;
|
||||
@@ -1608,14 +1609,19 @@ int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, si
|
||||
return 0;
|
||||
}
|
||||
|
||||
spool_func = PyDict_GetItemString(up.embedded_dict, "spooler");
|
||||
PyObject *spool_func = PyDict_GetItemString(up.embedded_dict, "spooler");
|
||||
if (!spool_func) {
|
||||
// ignore
|
||||
UWSGI_RELEASE_GIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject *spool_dict = PyDict_New();
|
||||
PyObject *pyargs, *ret;
|
||||
|
||||
|
||||
if (uwsgi_hooked_parse(buf, len, uwsgi_python_add_item, spool_dict)) {
|
||||
Py_DECREF(spool_dict);
|
||||
// malformed packet, destroy it
|
||||
UWSGI_RELEASE_GIL;
|
||||
return -2;
|
||||
@@ -1623,7 +1629,9 @@ int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, si
|
||||
|
||||
pyargs = PyTuple_New(1);
|
||||
|
||||
PyDict_SetItemString(spool_dict, "spooler_task_name", PyString_FromString(filename));
|
||||
PyObject *zero = PyString_FromString(filename);
|
||||
PyDict_SetItemString(spool_dict, "spooler_task_name", zero);
|
||||
Py_DECREF(zero);
|
||||
|
||||
if (body && body_len > 0) {
|
||||
PyDict_SetItemString(spool_dict, "body", PyString_FromStringAndSize(body, body_len));
|
||||
@@ -1634,12 +1642,18 @@ int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, si
|
||||
|
||||
if (ret) {
|
||||
if (!PyInt_Check(ret)) {
|
||||
Py_DECREF(ret);
|
||||
Py_DECREF(spool_dict);
|
||||
Py_DECREF(pyargs);
|
||||
// error, retry
|
||||
UWSGI_RELEASE_GIL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int retval = (int) PyInt_AsLong(ret);
|
||||
Py_DECREF(ret);
|
||||
Py_DECREF(spool_dict);
|
||||
Py_DECREF(pyargs);
|
||||
UWSGI_RELEASE_GIL;
|
||||
return retval;
|
||||
|
||||
@@ -1648,6 +1662,9 @@ int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, si
|
||||
if (PyErr_Occurred())
|
||||
PyErr_Print();
|
||||
|
||||
Py_DECREF(spool_dict);
|
||||
Py_DECREF(pyargs);
|
||||
|
||||
// error, retry
|
||||
UWSGI_RELEASE_GIL;
|
||||
return -1;
|
||||
@@ -1778,21 +1795,27 @@ static void uwsgi_python_harakiri(int wid) {
|
||||
if (up.tracebacker) {
|
||||
|
||||
char buf[8192];
|
||||
char *address = uwsgi_concat2(up.tracebacker, uwsgi_num2str(wid));
|
||||
char *wid_str = uwsgi_num2str(wid);
|
||||
char *address = uwsgi_concat2(up.tracebacker, wid_str);
|
||||
|
||||
int fd = uwsgi_connect(address, -1, 0);
|
||||
while (fd >= 0) {
|
||||
int ret = uwsgi_waitfd(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
if (ret <= 0) {
|
||||
break;
|
||||
}
|
||||
ssize_t len = read(fd, buf, 8192);
|
||||
if (len <= 0) {
|
||||
break;
|
||||
}
|
||||
uwsgi_log("%.*s", (int) len, buf);
|
||||
}
|
||||
if (fd < 1)
|
||||
goto exit;
|
||||
|
||||
int ret = uwsgi_waitfd(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
ssize_t len = read(fd, buf, 8192);
|
||||
if (len <= 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
uwsgi_log("%.*s", (int) len, buf);
|
||||
|
||||
cleanup:
|
||||
close(fd);
|
||||
exit:
|
||||
free(wid_str);
|
||||
free(address);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,12 @@ void *uwsgi_python_tracebacker_thread(void *foobar) {
|
||||
int current_defer_accept = uwsgi.no_defer_accept;
|
||||
uwsgi.no_defer_accept = 1;
|
||||
int fd = bind_to_unix(sock_path, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket);
|
||||
if (fd < 0) {
|
||||
uwsgi.no_defer_accept = current_defer_accept;
|
||||
free(str_wid);
|
||||
free(sock_path);
|
||||
return NULL;
|
||||
}
|
||||
uwsgi.no_defer_accept = current_defer_accept;
|
||||
|
||||
PyObject *traceback_module = PyImport_ImportModule("traceback");
|
||||
|
||||
+24
-135
@@ -174,7 +174,18 @@ char *uwsgi_encode_pydict(PyObject * pydict, uint16_t * size) {
|
||||
|
||||
static PyObject *py_uwsgi_listen_queue(PyObject * self, PyObject * args) {
|
||||
|
||||
return PyInt_FromLong(uwsgi.shared->options[UWSGI_OPTION_BACKLOG_STATUS]);
|
||||
int id = 0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|i:listen_queue", &id)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct uwsgi_socket *uwsgi_sock = uwsgi_get_socket_by_num(id);
|
||||
if (!uwsgi_sock) {
|
||||
return PyErr_Format(PyExc_ValueError, "unable to find socket %d", id);
|
||||
}
|
||||
|
||||
return PyInt_FromLong(uwsgi_sock->queue);
|
||||
}
|
||||
|
||||
static PyObject *py_uwsgi_close(PyObject * self, PyObject * args) {
|
||||
@@ -187,7 +198,6 @@ static PyObject *py_uwsgi_close(PyObject * self, PyObject * args) {
|
||||
|
||||
close(fd);
|
||||
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
||||
@@ -604,59 +614,6 @@ PyObject *py_uwsgi_set_logvar(PyObject * self, PyObject * args) {
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_recv_block(PyObject * self, PyObject * args) {
|
||||
|
||||
char buf[4096];
|
||||
char *bufptr;
|
||||
ssize_t rlen = 0, len;
|
||||
int fd, size, remains, ret, timeout = -1;
|
||||
|
||||
|
||||
if (!PyArg_ParseTuple(args, "ii|i:recv_block", &fd, &size, &timeout)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fd < 0)
|
||||
goto clear;
|
||||
|
||||
UWSGI_RELEASE_GIL
|
||||
// security check
|
||||
if (size > 4096)
|
||||
size = 4096;
|
||||
|
||||
remains = size;
|
||||
|
||||
bufptr = buf;
|
||||
while (remains > 0) {
|
||||
uwsgi_log("%d %d %d\n", remains, size, timeout);
|
||||
ret = uwsgi_waitfd(fd, timeout);
|
||||
if (ret > 0) {
|
||||
len = read(fd, bufptr, UMIN(remains, size));
|
||||
if (len > 0) {
|
||||
bufptr += len;
|
||||
rlen += len;
|
||||
remains -= len;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi_log("error waiting for block data\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
UWSGI_GET_GIL if (rlen == size) {
|
||||
return PyString_FromStringAndSize(buf, rlen);
|
||||
}
|
||||
|
||||
clear:
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_recv(PyObject * self, PyObject * args) {
|
||||
|
||||
int fd, max_size = 4096;
|
||||
@@ -685,28 +642,19 @@ PyObject *py_uwsgi_recv(PyObject * self, PyObject * args) {
|
||||
|
||||
PyObject *py_uwsgi_is_connected(PyObject * self, PyObject * args) {
|
||||
|
||||
int fd, soopt;
|
||||
socklen_t solen = sizeof(int);
|
||||
int fd = -1;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i:is_connected", &fd)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
uwsgi_error("getsockopt()");
|
||||
goto clear;
|
||||
if (uwsgi_is_connected(fd)) {
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
/* is something bad ? */
|
||||
if (soopt)
|
||||
goto clear;
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
|
||||
clear:
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
Py_INCREF(Py_False);
|
||||
return Py_False;
|
||||
}
|
||||
|
||||
|
||||
@@ -1028,14 +976,17 @@ PyObject *py_uwsgi_websocket_handshake(PyObject * self, PyObject * args) {
|
||||
char *origin = NULL;
|
||||
Py_ssize_t origin_len = 0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s#|s#:websocket_handshake", &key, &key_len, &origin, &origin_len)) {
|
||||
char *proto = NULL;
|
||||
Py_ssize_t proto_len = 0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|s#s#s#:websocket_handshake", &key, &key_len, &origin, &origin_len, &proto, &proto_len)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wsgi_request *wsgi_req = py_current_wsgi_req();
|
||||
|
||||
UWSGI_RELEASE_GIL
|
||||
int ret = uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len);
|
||||
int ret = uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len, proto, proto_len);
|
||||
UWSGI_GET_GIL
|
||||
|
||||
if (ret) {
|
||||
@@ -1991,46 +1942,6 @@ PyObject *py_uwsgi_spooler_pid(PyObject * self, PyObject * args) {
|
||||
}
|
||||
|
||||
|
||||
PyObject *py_uwsgi_get_option(PyObject * self, PyObject * args) {
|
||||
int opt_id;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i:get_option", &opt_id)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyInt_FromLong(uwsgi.shared->options[(uint8_t) opt_id]);
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_set_option(PyObject * self, PyObject * args) {
|
||||
int opt_id;
|
||||
int value;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "ii:set_option", &opt_id, &value)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uwsgi.shared->options[(uint8_t) opt_id] = (uint32_t) value;
|
||||
return PyInt_FromLong(value);
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_has_hook(PyObject * self, PyObject * args) {
|
||||
int modifier1;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i:has_hook", &modifier1)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
if (uwsgi.shared->hooks[modifier1] != unconfigured_hook) {
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
*/
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_connect(PyObject * self, PyObject * args) {
|
||||
|
||||
char *socket_name = NULL;
|
||||
@@ -2225,7 +2136,7 @@ PyObject *py_uwsgi_workers(PyObject * self, PyObject * args) {
|
||||
PyDict_SetItemString(apps_dict, "exceptions", zero);
|
||||
Py_DECREF(zero);
|
||||
|
||||
if (ua->chdir) {
|
||||
if (*ua->chdir) {
|
||||
zero = PyString_FromString(ua->chdir);
|
||||
}
|
||||
else {
|
||||
@@ -2281,21 +2192,6 @@ PyObject *py_uwsgi_stop(PyObject * self, PyObject * args) {
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
|
||||
/* blocking hint */
|
||||
PyObject *py_uwsgi_set_blocking(PyObject * self, PyObject * args) {
|
||||
|
||||
if (uwsgi.master_process) {
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
|
||||
PyObject *py_uwsgi_request_id(PyObject * self, PyObject * args) {
|
||||
return PyLong_FromUnsignedLongLong(uwsgi.workers[uwsgi.mywid].requests);
|
||||
}
|
||||
@@ -2497,11 +2393,6 @@ static PyMethodDef uwsgi_advanced_methods[] = {
|
||||
{"workers", py_uwsgi_workers, METH_VARARGS, ""},
|
||||
{"masterpid", py_uwsgi_masterpid, METH_VARARGS, ""},
|
||||
{"total_requests", py_uwsgi_total_requests, METH_VARARGS, ""},
|
||||
{"getoption", py_uwsgi_get_option, METH_VARARGS, ""},
|
||||
{"get_option", py_uwsgi_get_option, METH_VARARGS, ""},
|
||||
{"setoption", py_uwsgi_set_option, METH_VARARGS, ""},
|
||||
{"set_option", py_uwsgi_set_option, METH_VARARGS, ""},
|
||||
{"sorry_i_need_to_block", py_uwsgi_set_blocking, METH_VARARGS, ""},
|
||||
{"request_id", py_uwsgi_request_id, METH_VARARGS, ""},
|
||||
{"worker_id", py_uwsgi_worker_id, METH_VARARGS, ""},
|
||||
{"mule_id", py_uwsgi_mule_id, METH_VARARGS, ""},
|
||||
@@ -2542,7 +2433,6 @@ static PyMethodDef uwsgi_advanced_methods[] = {
|
||||
{"offload", py_uwsgi_offload, METH_VARARGS, ""},
|
||||
{"set_warning_message", py_uwsgi_warning, METH_VARARGS, ""},
|
||||
{"mem", py_uwsgi_mem, METH_VARARGS, ""},
|
||||
{"has_hook", py_uwsgi_has_hook, METH_VARARGS, ""},
|
||||
{"logsize", py_uwsgi_logsize, METH_VARARGS, ""},
|
||||
#ifdef UWSGI_SSL
|
||||
{"i_am_the_lord", py_uwsgi_i_am_the_lord, METH_VARARGS, ""},
|
||||
@@ -2562,7 +2452,6 @@ static PyMethodDef uwsgi_advanced_methods[] = {
|
||||
{"is_connected", py_uwsgi_is_connected, METH_VARARGS, ""},
|
||||
{"send", py_uwsgi_send, METH_VARARGS, ""},
|
||||
{"recv", py_uwsgi_recv, METH_VARARGS, ""},
|
||||
{"recv_block", py_uwsgi_recv_block, METH_VARARGS, ""},
|
||||
{"close", py_uwsgi_close, METH_VARARGS, ""},
|
||||
{"i_am_the_spooler", py_uwsgi_i_am_the_spooler, METH_VARARGS, ""},
|
||||
|
||||
|
||||
+18
-17
@@ -1048,25 +1048,26 @@ static VALUE uwsgi_ruby_websocket_handshake(int argc, VALUE *argv, VALUE *class)
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
if (argc < 1) {
|
||||
rb_raise(rb_eRuntimeError, "you neeto specify a valid websocket key");
|
||||
return Qnil;
|
||||
char *key = NULL, *origin = NULL, *proto = NULL;
|
||||
size_t key_len = 0, origin_len = 0, proto_len = 0;
|
||||
|
||||
if (argc > 0) {
|
||||
Check_Type(argv[0], T_STRING);
|
||||
key = RSTRING_PTR(argv[0]);
|
||||
key_len = RSTRING_LEN(argv[0]);
|
||||
if (argc > 1) {
|
||||
Check_Type(argv[1], T_STRING);
|
||||
origin = RSTRING_PTR(argv[1]);
|
||||
origin_len = RSTRING_LEN(argv[1]);
|
||||
if (argc > 2) {
|
||||
Check_Type(argv[2], T_STRING);
|
||||
proto = RSTRING_PTR(argv[2]);
|
||||
proto_len = RSTRING_LEN(argv[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Check_Type(argv[0], T_STRING);
|
||||
char *key = RSTRING_PTR(argv[0]);
|
||||
size_t key_len = RSTRING_LEN(argv[0]);
|
||||
|
||||
char *origin = NULL;
|
||||
size_t origin_len = 0;
|
||||
|
||||
if (argc > 1) {
|
||||
Check_Type(argv[1], T_STRING);
|
||||
origin = RSTRING_PTR(argv[1]);
|
||||
origin_len = RSTRING_LEN(argv[1]);
|
||||
}
|
||||
|
||||
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len)) {
|
||||
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len, proto, proto_len)) {
|
||||
rb_raise(rb_eRuntimeError, "unable to complete websocket handshake");
|
||||
}
|
||||
return Qnil;
|
||||
|
||||
@@ -158,7 +158,7 @@ static void uwsgi_rados_add_mountpoint(char *arg, size_t arg_len) {
|
||||
// this happens before fork() if not in lazy/lazy-apps mode
|
||||
static void uwsgi_rados_setup() {
|
||||
if (!urados.timeout) {
|
||||
urados.timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
urados.timeout = uwsgi.socket_timeout;
|
||||
}
|
||||
|
||||
struct uwsgi_string_list *usl = urados.mountpoints;
|
||||
|
||||
@@ -32,7 +32,7 @@ static char *uwsgi_redis_logger_build_command(char *src) {
|
||||
while(*ptr++) {
|
||||
if (*ptr == ' ') {
|
||||
pos = snprintf(dst, len, "$%d\r\n%.*s\r\n", (int) (ptr-base), (int) (ptr-base), base);
|
||||
if (pos > len || pos < 0) {
|
||||
if (pos >= len || pos < 0) {
|
||||
// i do not know what to do, better to exit...
|
||||
exit(1);
|
||||
}
|
||||
@@ -118,7 +118,7 @@ done:
|
||||
|
||||
uredislog = (struct uwsgi_redislog_state *) ul->data;
|
||||
if (uredislog->fd == -1) {
|
||||
uredislog->fd = uwsgi_connect(uredislog->address, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0);
|
||||
uredislog->fd = uwsgi_connect(uredislog->address, uwsgi.socket_timeout, 0);
|
||||
}
|
||||
|
||||
if (uredislog->fd == -1) return -1;
|
||||
|
||||
@@ -68,7 +68,7 @@ static int uwsgi_routing_func_http(struct wsgi_request *wsgi_req, struct uwsgi_r
|
||||
}
|
||||
}
|
||||
|
||||
if (uwsgi_proxy_nb(wsgi_req, ub_addr->buf, ub, remains, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_proxy_nb(wsgi_req, ub_addr->buf, ub, remains, uwsgi.socket_timeout)) {
|
||||
uwsgi_log("error routing request to http server %s\n", ub_addr->buf);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ static size_t memcached_firstline_parse(char *buf, size_t len) {
|
||||
// store an item in memcached
|
||||
static void memcached_store(char *addr, struct uwsgi_buffer *key, struct uwsgi_buffer *value, char *expires) {
|
||||
|
||||
int timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
int timeout = uwsgi.socket_timeout;
|
||||
|
||||
int fd = uwsgi_connect(addr, 0, 1);
|
||||
if (fd < 0) return;
|
||||
@@ -167,7 +167,7 @@ static int uwsgi_routing_func_memcached(struct wsgi_request *wsgi_req, struct uw
|
||||
}
|
||||
|
||||
// wait for connection;
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
uwsgi_buffer_destroy(ub_key) ;
|
||||
uwsgi_buffer_destroy(ub_addr);
|
||||
@@ -177,7 +177,7 @@ static int uwsgi_routing_func_memcached(struct wsgi_request *wsgi_req, struct uw
|
||||
|
||||
// build the request and send it
|
||||
char *cmd = uwsgi_concat3n("get ", 4, ub_key->buf, ub_key->pos, "\r\n", 2);
|
||||
if (uwsgi_write_true_nb(fd, cmd, 6+ub_key->pos, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_write_true_nb(fd, cmd, 6+ub_key->pos, uwsgi.socket_timeout)) {
|
||||
uwsgi_buffer_destroy(ub_key);
|
||||
uwsgi_buffer_destroy(ub_addr);
|
||||
free(cmd);
|
||||
@@ -204,7 +204,7 @@ static int uwsgi_routing_func_memcached(struct wsgi_request *wsgi_req, struct uw
|
||||
close(fd);
|
||||
goto end;
|
||||
wait:
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.socket_timeout);
|
||||
// when we have a chunk try to read the first line
|
||||
if (ret > 0) {
|
||||
len = read(fd, buf + pos, MEMCACHED_BUFSIZE - pos);
|
||||
@@ -270,7 +270,7 @@ read:
|
||||
}
|
||||
goto error;
|
||||
wait2:
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret > 0) {
|
||||
len = read(fd, buf, UMIN(MEMCACHED_BUFSIZE, response_size));
|
||||
if (len > 0) goto write;
|
||||
|
||||
@@ -104,7 +104,7 @@ static uint16_t uwsgi_radius_auth(struct uwsgi_radius_conf *urc, char *auth, siz
|
||||
}
|
||||
|
||||
// now wait for the response
|
||||
int ret = uwsgi.wait_read_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_read_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) goto end;
|
||||
|
||||
ssize_t rlen = read(fd, access_request, access_request_len);
|
||||
|
||||
@@ -50,7 +50,7 @@ static size_t redis_firstline_parse(char *buf, size_t len) {
|
||||
// store an item in redis
|
||||
static void redis_store(char *addr, struct uwsgi_buffer *key, struct uwsgi_buffer *value, char *expires) {
|
||||
|
||||
int timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
int timeout = uwsgi.socket_timeout;
|
||||
|
||||
int fd = uwsgi_connect(addr, 0, 1);
|
||||
if (fd < 0) return;
|
||||
@@ -166,7 +166,7 @@ static int uwsgi_routing_func_redis(struct wsgi_request *wsgi_req, struct uwsgi_
|
||||
}
|
||||
|
||||
// wait for connection;
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
int ret = uwsgi.wait_write_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret <= 0) {
|
||||
uwsgi_buffer_destroy(ub_key) ;
|
||||
uwsgi_buffer_destroy(ub_addr);
|
||||
@@ -176,7 +176,7 @@ static int uwsgi_routing_func_redis(struct wsgi_request *wsgi_req, struct uwsgi_
|
||||
|
||||
// build the request and send it
|
||||
char *cmd = uwsgi_concat3n("get ", 4, ub_key->buf, ub_key->pos, "\r\n", 2);
|
||||
if (uwsgi_write_true_nb(fd, cmd, 6+ub_key->pos, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_write_true_nb(fd, cmd, 6+ub_key->pos, uwsgi.socket_timeout)) {
|
||||
uwsgi_buffer_destroy(ub_key);
|
||||
uwsgi_buffer_destroy(ub_addr);
|
||||
free(cmd);
|
||||
@@ -203,7 +203,7 @@ static int uwsgi_routing_func_redis(struct wsgi_request *wsgi_req, struct uwsgi_
|
||||
close(fd);
|
||||
goto end;
|
||||
wait:
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.socket_timeout);
|
||||
// when we have a chunk try to read the first line
|
||||
if (ret > 0) {
|
||||
len = read(fd, buf + pos, REDIS_BUFSIZE - pos);
|
||||
@@ -268,7 +268,7 @@ read:
|
||||
}
|
||||
goto error;
|
||||
wait2:
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
ret = uwsgi.wait_read_hook(fd, uwsgi.socket_timeout);
|
||||
if (ret > 0) {
|
||||
len = read(fd, buf, UMIN(REDIS_BUFSIZE, response_size));
|
||||
if (len > 0) goto write;
|
||||
|
||||
@@ -72,7 +72,7 @@ static int uwsgi_routing_func_uwsgi_remote(struct wsgi_request *wsgi_req, struct
|
||||
}
|
||||
}
|
||||
|
||||
if (uwsgi_proxy_nb(wsgi_req, addr, ub, remains, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
if (uwsgi_proxy_nb(wsgi_req, addr, ub, remains, uwsgi.socket_timeout)) {
|
||||
uwsgi_log("error routing request to uwsgi server %s\n", addr);
|
||||
}
|
||||
end:
|
||||
|
||||
@@ -150,7 +150,7 @@ static void rrdtool_push(struct uwsgi_stats_pusher_instance *uspi, time_t now, c
|
||||
uwsgi_rlock(uwsgi.metrics_lock);
|
||||
int ret = snprintf(buf, 1024, "N:%lld", (long long) (*um->value));
|
||||
uwsgi_rwunlock(uwsgi.metrics_lock);
|
||||
if (ret < 3) {
|
||||
if (ret < 3 || ret >= 1024) {
|
||||
uwsgi_log("unable to update rrdtool metric for %s\n", um->name);
|
||||
goto next;
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ ssize_t uwsgi_rsyslog_logger(struct uwsgi_logger *ul, char *message, size_t len)
|
||||
if (pos > 0 && !u_rsyslog.split_msg) return pos;
|
||||
msg_len = ( ((int)len)-pos > u_rsyslog.msg_size ? u_rsyslog.msg_size : ((int)len)-pos);
|
||||
rlen = snprintf(ul->buf, u_rsyslog.packet_size, "<%d>%.*s %s: %.*s", ul->count, 15, ctime_storage+4, (char *) ul->data, msg_len, &message[pos]);
|
||||
if (rlen > 0 && rlen <= u_rsyslog.packet_size) {
|
||||
if (rlen > 0 && rlen < u_rsyslog.packet_size) {
|
||||
ret = sendto(ul->fd, ul->buf, rlen, 0, (const struct sockaddr *) &ul->addr, ul->addr_len);
|
||||
if (ret <= 0) return ret;
|
||||
pos += msg_len;
|
||||
|
||||
@@ -139,7 +139,7 @@ static struct uwsgi_option sslrouter_options[] = {
|
||||
{"sslrouter-use-socket", optional_argument, 0, "forward request to the specified uwsgi socket", uwsgi_opt_corerouter_use_socket, &usr, 0},
|
||||
{"sslrouter-to", required_argument, 0, "forward requests to the specified uwsgi server (you can specify it multiple times for load balancing)", uwsgi_opt_add_string_list, &usr.cr.static_nodes, 0},
|
||||
{"sslrouter-gracetime", required_argument, 0, "retry connections to dead static nodes after the specified amount of seconds", uwsgi_opt_set_int, &usr.cr.static_node_gracetime, 0},
|
||||
{"sslrouter-events", required_argument, 0, "set the maximum number of concusrent events", uwsgi_opt_set_int, &usr.cr.nevents, 0},
|
||||
{"sslrouter-events", required_argument, 0, "set the maximum number of concurrent events", uwsgi_opt_set_int, &usr.cr.nevents, 0},
|
||||
{"sslrouter-max-retries", required_argument, 0, "set the maximum number of retries/fallbacks to other nodes", uwsgi_opt_set_int, &usr.cr.max_retries, 0},
|
||||
{"sslrouter-quiet", required_argument, 0, "do not report failed connections to instances", uwsgi_opt_true, &usr.cr.quiet, 0},
|
||||
{"sslrouter-cheap", no_argument, 0, "run the sslrouter in cheap mode", uwsgi_opt_true, &usr.cr.cheap, 0},
|
||||
@@ -362,7 +362,8 @@ static int sslrouter_alloc_session(struct uwsgi_corerouter *ucr, struct uwsgi_ga
|
||||
SSL_set_fd(sr->ssl, cs->main_peer->fd);
|
||||
SSL_set_accept_state(sr->ssl);
|
||||
|
||||
uwsgi_cr_set_hooks(cs->main_peer, sr_read, NULL);
|
||||
if (uwsgi_cr_set_hooks(cs->main_peer, sr_read, NULL))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ extern "C" void stats_pusher_mongodb(struct uwsgi_stats_pusher_instance *uspi, t
|
||||
// the connection object (will be automatically destroyed at each cycle)
|
||||
mongo::DBClientConnection c;
|
||||
// set the socket timeout
|
||||
c.setSoTimeout(uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
c.setSoTimeout(uwsgi.socket_timeout);
|
||||
// connect
|
||||
c.connect(spmc->address);
|
||||
c.insert(spmc->collection, b);
|
||||
|
||||
@@ -20,8 +20,8 @@ static void uwsgi_opt_setup_tornado(char *opt, char *value, void *null) {
|
||||
|
||||
// set async mode
|
||||
uwsgi_opt_set_int(opt, value, &uwsgi.async);
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] < 30) {
|
||||
uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] = 30;
|
||||
if (uwsgi.socket_timeout < 30) {
|
||||
uwsgi.socket_timeout = 30;
|
||||
}
|
||||
// set loop engine
|
||||
uwsgi.loop = "tornado";
|
||||
@@ -234,8 +234,8 @@ PyObject *py_uwsgi_tornado_accept(PyObject *self, PyObject *args) {
|
||||
wsgi_req->start_of_request_in_sec = wsgi_req->start_of_request/1000000;
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0) {
|
||||
set_harakiri(uwsgi.shared->options[UWSGI_OPTION_HARAKIRI]);
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
uwsgi.async_proto_fd_table[wsgi_req->fd] = wsgi_req;
|
||||
@@ -322,7 +322,7 @@ static void tornado_loop() {
|
||||
uwsgi_log("!!! Running tornado without threads IS NOT recommended, enable them with --enable-threads !!!\n");
|
||||
}
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] < 30) {
|
||||
if (uwsgi.socket_timeout < 30) {
|
||||
uwsgi_log("!!! Running tornado with a socket-timeout lower than 30 seconds is not recommended, tune it with --socket-timeout !!!\n");
|
||||
}
|
||||
|
||||
|
||||
+62
-13
@@ -3,7 +3,7 @@
|
||||
extern struct uwsgi_tuntap utt;
|
||||
|
||||
// create a new peer
|
||||
struct uwsgi_tuntap_peer *uwsgi_tuntap_peer_create(struct uwsgi_tuntap_router *uttr, int fd) {
|
||||
struct uwsgi_tuntap_peer *uwsgi_tuntap_peer_create(struct uwsgi_tuntap_router *uttr, int fd, int is_router) {
|
||||
|
||||
struct uwsgi_tuntap_peer *uttp = uwsgi_calloc(sizeof(struct uwsgi_tuntap_peer));
|
||||
uttp->fd = fd;
|
||||
@@ -21,6 +21,23 @@ struct uwsgi_tuntap_peer *uwsgi_tuntap_peer_create(struct uwsgi_tuntap_router *u
|
||||
uttr->peers_tail = uttp;
|
||||
}
|
||||
|
||||
if (!is_router) {
|
||||
if (utt.use_credentials) {
|
||||
uwsgi_log_verbose("[uwsgi-tuntap] waiting for privileges drop...\n");
|
||||
for(;;) {
|
||||
if (getuid() > 0) break;
|
||||
sleep(1);
|
||||
}
|
||||
uwsgi_log_verbose("[uwsgi-tuntap] privileges dropped\n");
|
||||
if (uwsgi_pass_cred(fd, "uwsgi-tuntap", 12)) {
|
||||
// better to exit
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
uwsgi_tuntap_peer_send_rules(fd, uttp);
|
||||
}
|
||||
|
||||
return uttp;
|
||||
}
|
||||
|
||||
@@ -47,6 +64,7 @@ void uwsgi_tuntap_peer_destroy(struct uwsgi_tuntap_router *uttr, struct uwsgi_tu
|
||||
|
||||
free(uttp->buf);
|
||||
free(uttp->write_buf);
|
||||
if (uttp->rules) free(uttp->rules);
|
||||
close(uttp->fd);
|
||||
free(uttp);
|
||||
}
|
||||
@@ -159,8 +177,26 @@ retry:
|
||||
}
|
||||
}
|
||||
|
||||
int uwsgi_tuntap_register_addr(struct uwsgi_tuntap_router *uttr, struct uwsgi_tuntap_peer *uttp) {
|
||||
|
||||
struct uwsgi_tuntap_peer *tmp_uttp = uwsgi_tuntap_peer_get_by_addr(uttr, uttp->addr);
|
||||
char ip[INET_ADDRSTRLEN + 1];
|
||||
memset(ip, 0, INET_ADDRSTRLEN + 1);
|
||||
if (!inet_ntop(AF_INET, &uttp->addr, ip, INET_ADDRSTRLEN)) {
|
||||
uwsgi_error("uwsgi_tuntap_register_addr()/inet_ntop()");
|
||||
return -1;
|
||||
}
|
||||
if (uttp != tmp_uttp) {
|
||||
uwsgi_log("[tuntap-router] detected ip collision for %s\n", ip);
|
||||
uwsgi_tuntap_peer_destroy(uttr, tmp_uttp);
|
||||
}
|
||||
uwsgi_log("[tuntap-router] registered new peer %s (fd: %d)\n", ip, uttp->fd);
|
||||
memcpy(uttp->ip, ip, INET_ADDRSTRLEN + 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// receive a packet from the client
|
||||
int uwsgi_tuntap_peer_dequeue(struct uwsgi_tuntap_router *uttr, struct uwsgi_tuntap_peer *uttp) {
|
||||
int uwsgi_tuntap_peer_dequeue(struct uwsgi_tuntap_router *uttr, struct uwsgi_tuntap_peer *uttp, int is_router) {
|
||||
// get body
|
||||
if (uttp->header_pos >= 4) {
|
||||
ssize_t rlen = read(uttp->fd, uttp->buf + uttp->buf_pos, uttp->buf_pktsize - uttp->buf_pos);
|
||||
@@ -180,30 +216,43 @@ int uwsgi_tuntap_peer_dequeue(struct uwsgi_tuntap_router *uttr, struct uwsgi_tun
|
||||
uttp->header_pos = 0;
|
||||
uttp->buf_pos = 0;
|
||||
|
||||
if (!is_router) goto enqueue;
|
||||
|
||||
// a rule block
|
||||
if (uttp->header[3] == 1) {
|
||||
if (uttp->rules) free(uttp->rules);
|
||||
uttp->rules = uwsgi_malloc(uttp->buf_pktsize);
|
||||
memcpy(uttp->rules, uttp->buf, uttp->buf_pktsize);
|
||||
uttp->rules_cnt = uttp->buf_pktsize / sizeof(struct uwsgi_tuntap_peer_rule);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (uwsgi_tuntap_firewall_check(utt.fw_out, uttp->buf, uttp->buf_pktsize)) return 0;
|
||||
|
||||
// if there is no associated address store the source
|
||||
if (!uttp->addr) {
|
||||
uint32_t *src_ip = (uint32_t *) & uttp->buf[12];
|
||||
// close on invalid first packet
|
||||
if (uttp->buf_pktsize < 20) return -1;
|
||||
uint32_t *src_ip = (uint32_t *) (&uttp->buf[12]);
|
||||
uttp->addr = *src_ip;
|
||||
// drop invalid ip addresses
|
||||
if (!uttp->addr)
|
||||
return -1;
|
||||
|
||||
struct uwsgi_tuntap_peer *tmp_uttp = uwsgi_tuntap_peer_get_by_addr(uttr, uttp->addr);
|
||||
char ip[INET_ADDRSTRLEN + 1];
|
||||
memset(ip, 0, INET_ADDRSTRLEN + 1);
|
||||
if (!inet_ntop(AF_INET, &uttp->addr, ip, INET_ADDRSTRLEN)) {
|
||||
uwsgi_error("inet_ntop()");
|
||||
if (uwsgi_tuntap_register_addr(uttr, uttp)) {
|
||||
return -1;
|
||||
}
|
||||
if (uttp != tmp_uttp) {
|
||||
uwsgi_log("[tuntap-router] detected ip collision for %s\n", ip);
|
||||
uwsgi_tuntap_peer_destroy(uttr, tmp_uttp);
|
||||
}
|
||||
uwsgi_log("[tuntap-router] registered new peer %s (fd: %d)\n", ip, uttp->fd);
|
||||
|
||||
}
|
||||
|
||||
if (uwsgi_tuntap_peer_rules_check(uttr, uttp, uttp->buf, uttp->buf_pktsize, 1)) return 0;
|
||||
|
||||
// check four routing rule
|
||||
if (uttr->gateway_fd > -1) {
|
||||
if (uwsgi_tuntap_route_check(uttr->gateway_fd, uttp->buf, uttp->buf_pktsize)) return 0;
|
||||
}
|
||||
enqueue:
|
||||
|
||||
memcpy(uttr->write_buf, uttp->buf, uttp->buf_pktsize);
|
||||
uttr->write_pktsize = uttp->buf_pktsize;
|
||||
uwsgi_tuntap_enqueue(uttr);
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user