Compare commits

..
27 Commits
Author SHA1 Message Date
roberto@natty32 6827b3bf15 cron-like interface added to signal framework 2011-03-09 10:33:44 +01:00
roberto@natty32 2055a1472f great fix suggested by Damjan Georgievski for linux namespaces 2011-03-08 14:17:30 +01:00
roberto@natty32 f4f9018068 fixed multithreading + max-requests 2011-03-08 11:45:37 +01:00
roberto@sirius 3e82b5cd33 improved configuration system 2011-03-07 16:06:59 +01:00
roberto@sirius 07036bce20 fix %v usage in emperor 2011-03-07 11:52:44 +01:00
roberto@sirius a4a7a75f29 uWSGI 0.9.7-rc1 2011-03-07 09:42:53 +01:00
roberto@natty32 02d6006b2f added %v in emperor 2011-03-06 19:21:09 +01:00
roberto@natty32 e8e5f11c66 use sigaction() instead of signal() 2011-03-05 17:20:10 +01:00
roberto@natty32 e53a6fa4c0 fixed http fd passing on POST 2011-03-04 14:23:36 +01:00
roberto@natty32 c478b0a0c8 added uwsgi.erlang_register_process 2011-03-04 12:07:07 +01:00
roberto@natty32 5f207e6978 fix getsockname on unbound sockets 2011-03-04 09:01:14 +01:00
roberto@natty32 a2b74d0afd fixed linux namespace on recent kernel 2011-03-04 08:20:20 +01:00
roberto@maverick64 b867b11de3 fixed python2.x threads 2011-03-03 17:17:27 +01:00
roberto@maverick64 806765d1d3 fixed python3.2 threads 2011-03-03 17:15:01 +01:00
roberto@sirius 62d2b97e17 export build date 2011-03-03 14:06:29 +01:00
roberto@sirius c04e339034 report clustering without a socket 2011-03-03 11:07:58 +01:00
roberto@sirius 927b495646 nuclear blast all the processes on initialization error 2011-03-03 10:38:19 +01:00
roberto@mrspurr 57b5392590 fixed gateways reloading 2011-03-03 09:52:57 +01:00
roberto@mrspurr 463030108f fixed vacuum 2011-03-03 09:17:03 +01:00
roberto@netbsd64 46dd754264 NetBSD fixes 2011-03-02 19:54:13 +01:00
roberto@silente 12d9641b66 OpenBSD, endianess and clustering fixes 2010-12-16 07:21:51 +01:00
roberto@maverick64 6cb1203d26 fixed post buffering in python 2011-03-02 17:22:12 +01:00
roberto@maverick64 23d0ec333e encode python string/binary to erlang binary 2011-03-02 11:17:12 +01:00
roberto@maverick64 da579fd308 fix refcnt in erlang->python translation 2011-03-02 08:54:01 +01:00
roberto@longshot 14152bc999 removed broken log rotation 2011-03-01 20:55:04 +01:00
roberto@sirius 6c32e5f2e2 fixed no_argument options in ini and yaml parsing 2011-03-01 16:38:45 +01:00
roberto@freebsd64 62f74f2b60 uWSGI 0.9.7-beta1 2011-03-01 10:47:32 +01:00
29 changed files with 751 additions and 311 deletions
+2
View File
@@ -9,3 +9,5 @@ b8f67c6cd0d62f864db9b2b265b4e260ad7d873c 0.9.6-rc2
35918dd4b3771158b2b57410613ef029e08bbd60 0.9.6
5f25d1f97f9dde3e391a668c1c610a52a5db8244 0.9.6.1
ef31fc61ac48018c464f28ebfdf865b96bf83f18 0.9.6.2
9e100c1f085034396cec586f0c76ad3f9800d1a5 0.9.7-beta1
ff4111eb8f55b958ec91c70163e10f3372a9b94d 0.9.7-rc1
+22
View File
@@ -1,8 +1,30 @@
*** march 2011 ***
* 0.9.7-rc1 [20110307]
- export %v in emperor mode
- fixed signal race condition in server reload
- fixed http router post handling bug
- added uwsgi.erlang_register_process in pyerl plugin
- fixed unix socket inherit
- fixed linux namespace support in recent kernel/gcc
- ported threading to python 3.2
- add build date to uWSGI startup info
- do not allow clustering without defining a socket
- kill all the gateways/daemons on startup errors
- fixed vacuum after chdir()
- fixed gateways reload
- re-added support for OpenBSD and NetBSD
- fixed post-buffering in python
- use erlang binary for python string
- fixed memory leak in pyerl plugin
- removed broken log rotation (post-poned)
- fixed no_argument options in ini and yaml parsing
* 0.9.7-beta1 [20110301]
- fully modular re-design
- advanced build system with presets
- threading support
- emperor mode for massive auto-hosting
- caching subsystem
-1
View File
@@ -15,7 +15,6 @@ async = true
evdis = false
ldap = false
pcre = auto
stackless = false
debug = false
unbit = false
xml_implementation = libxml2
+3
View File
@@ -121,6 +121,9 @@ void emperor_add(char *name, time_t born) {
return;
}
else {
unsetenv("UWSGI_RELOADS");
uef = uwsgi_num2str(n_ui->pipe[1]);
if (setenv("UWSGI_EMPEROR_FD", uef, 1)) {
uwsgi_error("setenv()");
+10
View File
@@ -55,6 +55,11 @@ struct uwsgi_gateway *register_gateway(char *name, void (*loop)(void)) {
if (!uwsgi.master_process) {
if (gw_pid > 0) {
#ifdef __linux__
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0,0,0)) {
uwsgi_error("prctl()");
}
#endif
loop();
// never here !!! (i hope)
exit(1);
@@ -101,6 +106,11 @@ void gateway_respawn(int id) {
if (gw_pid == 0) {
if (uwsgi.master_as_root) uwsgi_as_root();
#ifdef __linux__
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0,0,0)) {
uwsgi_error("prctl()");
}
#endif
ug->loop();
// never here !!! (i hope)
exit(1);
+1 -21
View File
@@ -84,7 +84,6 @@ void uwsgi_ini_config(char *file, char *magic_table[]) {
int lines = 1;
struct option *lopt, *aopt;
char *section_asked = "uwsgi";
char *colon;
@@ -126,26 +125,7 @@ void uwsgi_ini_config(char *file, char *magic_table[]) {
ini_rstrip(key);
val = ini_lstrip(val);
ini_rstrip(val);
lopt = uwsgi.long_options;
while ((aopt = lopt)) {
if (!aopt->name)
break;
if (!strcmp(key, aopt->name)) {
if (aopt->flag) {
*aopt->flag = aopt->val;
add_exported_option(0, (char *)key);
}
else {
if (aopt->has_arg == optional_argument) {
if (!strcmp("true", val)) {
val = NULL;
}
}
manage_opt(aopt->val, val);
}
}
lopt++;
}
add_exported_option((char *)key, val, 0);
}
}
}
+25 -3
View File
@@ -134,14 +134,36 @@ void uwsgi_rwunlock(void *lock) { uwsgi_unlock(lock); }
#define UWSGI_LOCK_SIZE 8
#define UWSGI_RWLOCK_SIZE 8
void uwsgi_lock_init(void *lock) {}
static int lock_counter = 0;
void uwsgi_lock_init(void *lock) {
char filename[17];
if (snprintf(filename, 17, ".uwsgiflock%d", lock_counter) < 0) {
uwsgi_log("unable to create lock %d\n", lock_counter);
}
int fd = open(filename, O_CREAT|O_RDWR|O_TRUNC);
if (fd < 0) {
uwsgi_error("open()");
exit(1);
}
memcpy(lock, &fd, sizeof(int));
}
void uwsgi_lock(void *lock) {
//if (flock((int) *lock, LOCK_EX)) { uwsgi_error("flock()"); }
int fd;
memcpy(&fd, lock, sizeof(int));
if (flock(fd, LOCK_EX)) { uwsgi_error("flock()"); }
}
void uwsgi_unlock(void *lock) {
//if (flock((int) *lock, LOCK_UN)) { uwsgi_error("flock()"); }
int fd;
memcpy(&fd, lock, sizeof(int));
if (flock(fd, LOCK_UN)) { uwsgi_error("flock()"); }
}
void uwsgi_rwlock_init(void *lock) { uwsgi_lock_init(lock) ;}
+1
View File
@@ -1,3 +1,4 @@
#define _NO_UWSGI_RB
#include "uwsgi.h"
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
+104 -32
View File
@@ -125,7 +125,7 @@ void master_loop(char **argv, char **environ) {
char log_buf[4096];
uint64_t current_time = time(NULL);
time_t current_time = time(NULL);
struct timeval last_respawn;
@@ -172,21 +172,19 @@ void master_loop(char **argv, char **environ) {
struct uwsgi_rb_timer *min_timeout;
struct rb_root *rb_timers = uwsgi_init_rb_timer();
struct tm *uwsgi_cron_delta;
// release the GIL
//UWSGI_RELEASE_GIL
/* route signals to workers... */
signal(SIGHUP, (void *) &grace_them_all);
signal(SIGTERM, (void *) &reap_them_all);
signal(SIGINT, (void *) &kill_them_all);
signal(SIGQUIT, (void *) &kill_them_all);
/* used only to avoid human-errors */
uwsgi_unix_signal(SIGHUP, grace_them_all);
uwsgi_unix_signal(SIGTERM, reap_them_all);
uwsgi_unix_signal(SIGINT, kill_them_all);
uwsgi_unix_signal(SIGQUIT, kill_them_all);
uwsgi_unix_signal(SIGUSR1, stats);
signal(SIGUSR1, (void *) &stats);
uwsgi.master_queue = event_queue_init();
/* route signals to workers... */
#ifdef UWSGI_DEBUG
uwsgi_log("adding %d to signal poll\n", uwsgi.shared->worker_signal_pipe[0]);
#endif
@@ -245,6 +243,10 @@ void master_loop(char **argv, char **environ) {
uh->modifier1 = 99;
uh->pktsize = cluster_opt_size - 4;
uh->modifier2 = 1;
#ifdef __BIG_ENDIAN__
uh->pktsize = uwsgi_swap16(uh->pktsize);
#endif
cptrbuf = cluster_opt_buf+4;
@@ -361,6 +363,11 @@ void master_loop(char **argv, char **environ) {
}
#endif
// call a series of waitpid to ensure all processes (gateways and daemons) are dead
for(i=0;i<(uwsgi.gateways_cnt+ushared->daemons_cnt);i++) {
diedpid = waitpid(WAIT_ANY, &waitpid_status, WNOHANG);
}
uwsgi_log( "binary reloading uWSGI...\n");
if (chdir(uwsgi.cwd)) {
uwsgi_error("chdir()");
@@ -407,12 +414,6 @@ void master_loop(char **argv, char **environ) {
master_has_children = 1;
}
#endif
#ifdef UWSGI_PROXY
if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) {
master_has_children = 1;
}
// TODO if gateways > 0 master_has_children == 1
#endif
if (!master_has_children) {
diedpid = 0;
@@ -423,7 +424,7 @@ void master_loop(char **argv, char **environ) {
uwsgi_error("waitpid()");
/* here is better to reload all the uWSGI stack */
uwsgi_log( "something horrible happened...\n");
reap_them_all();
reap_them_all(0);
exit(1);
}
}
@@ -497,6 +498,75 @@ void master_loop(char **argv, char **environ) {
}
}
// check uwsgi-cron table
if (ushared->cron_cnt) {
current_time = time(NULL);
uwsgi_cron_delta = localtime( &current_time );
if (uwsgi_cron_delta) {
// fix month
uwsgi_cron_delta->tm_mon++;
uwsgi_lock(uwsgi.cron_table_lock);
for(i=0;i<ushared->cron_cnt;i++) {
struct uwsgi_cron *ucron = &ushared->cron[i];
int uc_minute, uc_hour, uc_day, uc_month, uc_week;
uc_minute = ucron->minute;
uc_hour = ucron->hour;
uc_day = ucron->day;
uc_month = ucron->month;
uc_week = ucron->week;
if (ucron->minute == -1) uc_minute = uwsgi_cron_delta->tm_min;
if (ucron->hour == -1) uc_hour = uwsgi_cron_delta->tm_hour;
if (ucron->month == -1) uc_month = uwsgi_cron_delta->tm_mon;
// mday and wday are ORed
if (ucron->day == -1 && ucron->week == -1) {
if (ucron->day == -1) uc_day = uwsgi_cron_delta->tm_mday;
if (ucron->week == -1) uc_week = uwsgi_cron_delta->tm_wday;
}
else if (ucron->day == -1) {
ucron->day = uwsgi_cron_delta->tm_mday;
}
else if (ucron->week == -1) {
ucron->week = uwsgi_cron_delta->tm_wday;
}
else {
if (ucron->day == uwsgi_cron_delta->tm_mday) {
ucron->week = uwsgi_cron_delta->tm_wday;
}
else if (ucron->week == uwsgi_cron_delta->tm_wday) {
ucron->day = uwsgi_cron_delta->tm_mday;
}
}
if (uwsgi_cron_delta->tm_min == uc_minute &&
uwsgi_cron_delta->tm_hour == uc_hour &&
uwsgi_cron_delta->tm_mon == uc_month &&
uwsgi_cron_delta->tm_mday == uc_day &&
uwsgi_cron_delta->tm_wday == uc_week) {
// date match, signal it ?
if (current_time - ucron->last_job > 60) {
uwsgi_route_signal(ucron->sig);
ucron->last_job = current_time;
}
}
}
uwsgi_unlock(uwsgi.cron_table_lock);
}
else {
uwsgi_error("localtime()");
}
}
if (rlen > 0) {
if (uwsgi.log_master) {
@@ -521,18 +591,18 @@ void master_loop(char **argv, char **environ) {
if (byte == 0) {
close(uwsgi.emperor_fd);
uwsgi.has_emperor = 0;
kill_them_all();
kill_them_all(0);
}
// reload me
else if (byte == 1) {
grace_them_all();
grace_them_all(0);
}
}
else {
uwsgi_log("lost connection with my emperor !!!\n");
close(uwsgi.emperor_fd);
uwsgi.has_emperor = 0;
kill_them_all();
kill_them_all(0);
}
}
}
@@ -593,12 +663,19 @@ void master_loop(char **argv, char **environ) {
switch(uwsgi.wsgi_requests[0]->uh.modifier1) {
case 95:
memset(&nucn, 0, sizeof(struct uwsgi_cluster_node));
#ifdef __BIG_ENDIAN__
uwsgi.wsgi_requests[0]->uh.pktsize = uwsgi_swap16(uwsgi.wsgi_requests[0]->uh.pktsize);
#endif
uwsgi_hooked_parse(uwsgi.wsgi_requests[0]->buffer, uwsgi.wsgi_requests[0]->uh.pktsize, manage_cluster_announce, &nucn);
if (nucn.name[0] != 0) {
uwsgi_cluster_add_node(&nucn, CLUSTER_NODE_DYNAMIC);
}
break;
case 96:
#ifdef __BIG_ENDIAN__
uwsgi.wsgi_requests[0]->uh.pktsize = uwsgi_swap16(uwsgi.wsgi_requests[0]->uh.pktsize);
#endif
uwsgi_log_verbose("%.*s\n", uwsgi.wsgi_requests[0]->uh.pktsize, uwsgi.wsgi_requests[0]->buffer);
break;
case 98:
@@ -614,6 +691,9 @@ void master_loop(char **argv, char **environ) {
}
break;
case 73:
#ifdef __BIG_ENDIAN__
uwsgi.wsgi_requests[0]->uh.pktsize = uwsgi_swap16(uwsgi.wsgi_requests[0]->uh.pktsize);
#endif
uwsgi_log_verbose("[uWSGI cluster %s] new node available: %.*s\n", uwsgi.cluster, uwsgi.wsgi_requests[0]->uh.pktsize, uwsgi.wsgi_requests[0]->buffer);
break;
}
@@ -679,6 +759,7 @@ void master_loop(char **argv, char **environ) {
// checking logsize
if (uwsgi.logfile) {
uwsgi.shared->logsize = lseek(2, 0, SEEK_CUR);
/*
if (uwsgi.shared->logsize > 8192) {
//uwsgi_log("logsize: %d\n", uwsgi.shared->logsize);
char *new_logfile = uwsgi_malloc(strlen(uwsgi.logfile) + 14 + 1);
@@ -688,6 +769,7 @@ void master_loop(char **argv, char **environ) {
}
free(new_logfile);
}
*/
}
@@ -698,16 +780,6 @@ void master_loop(char **argv, char **environ) {
// recalculate requests counter on race conditions risky configurations
// a bit of inaccuracy is better than locking;)
if (uwsgi.cores > 1) {
for(i=1;i<uwsgi.numproc+1;i++) {
tmp_counter = 0;
for(j=0;j<uwsgi.cores;j++) {
tmp_counter += uwsgi.core[j]->requests;
}
uwsgi.workers[i].requests = tmp_counter;
}
}
if (uwsgi.numproc > 1) {
tmp_counter = 0;
for(i=1;i<uwsgi.numproc+1;i++) {
@@ -716,12 +788,12 @@ void master_loop(char **argv, char **environ) {
uwsgi.workers[0].requests = tmp_counter;
}
// remove expired cache items
// remove expired cache items TODO use rb_tree timeouts
if (uwsgi.cache_max_items > 0) {
for(i=0;i< (int)uwsgi.cache_max_items;i++) {
uwsgi_wlock(uwsgi.cache_lock);
if (uwsgi.cache_items[i].expires) {
if (uwsgi.cache_items[i].expires < current_time) {
if (uwsgi.cache_items[i].expires < (uint64_t) current_time) {
uwsgi_cache_del(uwsgi.cache_items[i].key, uwsgi.cache_items[i].keysize);
}
}
+20 -1
View File
@@ -247,6 +247,7 @@ void erlang_loop() {
int fd;
int eversion;
int i;
ei_x_buff x, xr;
@@ -291,13 +292,31 @@ void erlang_loop() {
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 {
dump_eterm(&x);
int uep = -1;
for(i=0;i<uerl.uep_cnt;i++) {
if (!strcmp(uerl.uep[i].name, em.toname)) {
uep = i;
break;
}
}
if (uep > -1) {
if (uerl.uep[uep].plugin) {
uerl.uep[uep].plugin( uerl.uep[uep].func, &x );
}
}
else {
uwsgi_log("!!! unregistered erlang process requested, dumping it !!!\n");
dump_eterm(&x);
}
}
+13 -1
View File
@@ -1,9 +1,19 @@
#include <ei.h>
#define MAX_UWSGI_ERLANG_PROCESSES 64
#define LONG_ARGS_ERLANG 17012
#define LONG_ARGS_ERLANG_COOKIE 17013
struct uwsgi_erlang_process {
char name[0xff];
void (*plugin)(void *, ei_x_buff *);
void *func;
};
struct uwsgi_erlang {
ei_cnode cnode;
@@ -13,6 +23,8 @@ struct uwsgi_erlang {
int fd;
void *lock;
struct uwsgi_erlang_process uep[MAX_UWSGI_ERLANG_PROCESSES];
int uep_cnt;
};
+8 -3
View File
@@ -209,7 +209,7 @@ uint16_t http_add_uwsgi_header(struct http_session *h_session, struct iovec *iov
for(i=0;i<hhlen;i++) {
if (!status) {
hh[i] = toupper(hh[i]);
hh[i] = toupper((int)hh[i]);
if (hh[i] == '-') hh[i] = '_';
if (hh[i] == ':') {
status = 1;
@@ -548,9 +548,10 @@ void http_loop() {
continue;
}
uhttp_session->timeout = reset_timeout(uhttp_session);
switch(uhttp_session->status) {
uhttp_session->timeout = reset_timeout(uhttp_session);
case HTTP_STATUS_RECV:
len = recv(uhttp_session->fd, uhttp_session->buffer + uhttp_session->h_pos, UMAX16-uhttp_session->h_pos, 0);
@@ -681,6 +682,10 @@ void http_loop() {
break;
}
#ifdef __BIG_ENDIAN__
uhttp_session->uh.pktsize = uwsgi_swap16(uhttp_session->uh.pktsize);
#endif
uhttp_session->iov[0].iov_base = &uhttp_session->uh;
uhttp_session->iov[0].iov_len = 4;
@@ -691,7 +696,7 @@ void http_loop() {
}
// fd passing: PERFORMANCE EXTREME BOOST !!!
if (uhttp_session->pass_fd) {
if (uhttp_session->pass_fd && !uhttp_session->remains) {
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = uhttp_session->iov;
+44 -1
View File
@@ -72,6 +72,7 @@ PyObject *erl_to_py(ei_x_buff* x) {
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);
@@ -84,6 +85,7 @@ PyObject *erl_to_py(ei_x_buff* x) {
pobj = PyUnicode_FromString(atom);
#endif
free(atom);
Py_INCREF(pobj);
return pobj;
case ERL_SMALL_TUPLE_EXT:
case ERL_LARGE_TUPLE_EXT:
@@ -94,6 +96,7 @@ PyObject *erl_to_py(ei_x_buff* x) {
PyTuple_SetItem(pobj, i, zero);
Py_DECREF(zero);
}
Py_INCREF(pobj);
return pobj;
case ERL_LIST_EXT:
case ERL_NIL_EXT:
@@ -108,12 +111,14 @@ PyObject *erl_to_py(ei_x_buff* 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);
@@ -121,6 +126,7 @@ PyObject *erl_to_py(ei_x_buff* x) {
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);
@@ -292,6 +298,42 @@ recv:
return Py_None;
}
void pyerl_call_registered(void *func, ei_x_buff *x) {
PyObject *pyargs = PyTuple_New(1);
PyObject *ret;
PyTuple_SetItem(pyargs, 0, erl_to_py(x));
ret = python_call((PyObject *) func, pyargs, 0);
}
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 (uerl.uep_cnt >= MAX_UWSGI_ERLANG_PROCESSES)
return PyErr_Format(PyExc_ValueError, "You can define max %d erlang registered processes", MAX_UWSGI_ERLANG_PROCESSES);
if (strlen(name) > 0xff-1)
return PyErr_Format(PyExc_ValueError, "Invalid erlang process name");
strcpy(uerl.uep[uerl.uep_cnt].name, name);
uerl.uep[uerl.uep_cnt].plugin = pyerl_call_registered;
uerl.uep[uerl.uep_cnt].func = callable;
uerl.uep_cnt++;
Py_INCREF(Py_None);
return Py_None;
}
PyObject *pyerl_recv(PyObject * self, PyObject * args) {
ei_x_buff x;
@@ -401,6 +443,7 @@ static PyMethodDef uwsgi_pyerl_methods[] = {
{"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},
};
@@ -426,7 +469,7 @@ int py_to_erl(PyObject *pobj, ei_x_buff *x) {
ei_x_encode_empty_list(x);
}
else if (PyString_Check(pobj)) {
ei_x_encode_string(x, PyString_AsString(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));
+10 -1
View File
@@ -4,13 +4,22 @@ extern struct uwsgi_server uwsgi;
extern struct uwsgi_python up;
void gil_real_get() {
#ifndef PYTHREE
PyEval_AcquireLock();
PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_gil_key));
#else
PyEval_RestoreThread((PyThreadState *) pthread_getspecific(up.upt_gil_key));
#endif
}
void gil_real_release() {
#ifndef PYTHREE
pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Swap(NULL));
PyEval_ReleaseLock();
PyEval_ReleaseLock();
#else
pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get());
PyEval_SaveThread();
#endif
}
void gil_fake_get() {}
+9
View File
@@ -13,6 +13,9 @@ struct option uwsgi_python_options[] = {
{"callable", required_argument, 0, LONG_ARGS_CALLABLE},
{"test", required_argument, 0, 'j'},
{"home", required_argument, 0, 'H'},
{"virtualenv", required_argument, 0, 'H'},
{"venv", required_argument, 0, 'H'},
{"pyhome", required_argument, 0, 'H'},
{"pythonpath", required_argument, 0, LONG_ARGS_PYTHONPATH},
{"python-path", required_argument, 0, LONG_ARGS_PYTHONPATH},
{"pymodule-alias", required_argument, 0, LONG_ARGS_PYMODULE_ALIAS},
@@ -795,6 +798,9 @@ int uwsgi_python_xml(char *node, char *content) {
uwsgi.wsgi_req->module_len = strlen(uwsgi.wsgi_req->module);
return init_uwsgi_app(LOADER_DYN, uwsgi.wsgi_req, uwsgi.wsgi_req, interpreter);
}
else {
return init_uwsgi_app(LOADER_UWSGI, content, uwsgi.wsgi_req, interpreter);
}
return 1;
}
else if (!strcmp("pyhome", node)) {
@@ -829,6 +835,9 @@ int uwsgi_python_signal_handler(uint8_t sig, void *handler) {
if (!args)
return -1;
if (!handler) return -1;
PyTuple_SetItem(args, 0, PyInt_FromLong(sig));
ret = python_call(handler, args, 0);
+20
View File
@@ -181,6 +181,25 @@ PyObject *py_uwsgi_close(PyObject * self, PyObject * args) {
}
PyObject *py_uwsgi_add_cron(PyObject * self, PyObject * args) {
uint8_t uwsgi_signal;
int minute, hour, day, month, week;
if (!PyArg_ParseTuple(args, "Biiiii:add_cron", &uwsgi_signal, &minute, &hour, &day, &month, &week)) {
return NULL;
}
if (uwsgi_signal_add_cron(uwsgi_signal, minute, hour, day, month, week)) {
return PyErr_Format(PyExc_ValueError, "unable to add cron");
}
Py_INCREF(Py_True);
return Py_True;
}
PyObject *py_uwsgi_add_timer(PyObject * self, PyObject * args) {
uint8_t uwsgi_signal;
@@ -2343,6 +2362,7 @@ static PyMethodDef uwsgi_advanced_methods[] = {
{"add_file_monitor", py_uwsgi_add_file_monitor, METH_VARARGS, ""},
{"add_timer", py_uwsgi_add_timer, METH_VARARGS, ""},
{"add_rb_timer", py_uwsgi_add_rb_timer, METH_VARARGS, ""},
{"add_cron", py_uwsgi_add_cron, METH_VARARGS, ""},
{"register_rpc", py_uwsgi_register_rpc, METH_VARARGS, ""},
{"rpc", py_uwsgi_rpc, METH_VARARGS, ""},
+17 -6
View File
@@ -428,15 +428,26 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) {
if (uwsgi.post_buffering > 0) {
UWSGI_RELEASE_GIL
// read to disk
if (wsgi_req->post_cl <= (size_t) uwsgi.post_buffering) {
if (!uwsgi_read_whole_body(wsgi_req, wsgi_req->post_buffering_buf, uwsgi.post_buffering_bufsize)) {
goto clear;
// read to disk if post_cl > post_buffering
if (!up.pep3333_input) {
if (wsgi_req->post_cl >= (size_t) uwsgi.post_buffering) {
if (!uwsgi_read_whole_body(wsgi_req, wsgi_req->post_buffering_buf, uwsgi.post_buffering_bufsize)) {
goto clear;
}
}
}
else {
if (!uwsgi_read_whole_body_in_mem(wsgi_req, wsgi_req->post_buffering_buf)) {
goto clear;
// read to disk if post_cl > post_buffering
if (wsgi_req->post_cl >= (size_t) uwsgi.post_buffering) {
if (!uwsgi_read_whole_body(wsgi_req, wsgi_req->post_buffering_buf, uwsgi.post_buffering_bufsize)) {
goto clear;
}
}
// on tiny post use memory
else {
if (!uwsgi_read_whole_body_in_mem(wsgi_req, wsgi_req->post_buffering_buf)) {
goto clear;
}
}
}
UWSGI_GET_GIL
+3
View File
@@ -893,6 +893,9 @@ int uwsgi_string_sendto(int fd, uint8_t modifier1, uint8_t modifier2, struct soc
uh->modifier1 = modifier1;
uh->pktsize = len;
#ifdef __BIG_ENDIAN__
uh->pktsize = uwsgi_swap16(uh->pktsize);
#endif
uh->modifier2 = modifier2;
memcpy(upkt+4, message, len);
+25
View File
@@ -91,6 +91,31 @@ int uwsgi_add_timer(uint8_t sig, int secs) {
}
int uwsgi_signal_add_cron(uint8_t sig, int minute, int hour, int day, int month, int week) {
uwsgi_lock(uwsgi.cron_table_lock);
if (ushared->cron_cnt < MAX_CRONS) {
ushared->cron[ushared->cron_cnt].sig = sig;
ushared->cron[ushared->cron_cnt].minute = minute;
ushared->cron[ushared->cron_cnt].hour = hour;
ushared->cron[ushared->cron_cnt].day = day;
ushared->cron[ushared->cron_cnt].month = month;
ushared->cron[ushared->cron_cnt].week = week;
ushared->cron_cnt++;
}
else {
uwsgi_log("you can register max %d cron !!!\n", MAX_CRONS);
uwsgi_unlock(uwsgi.cron_table_lock);
return -1;
}
uwsgi_unlock(uwsgi.cron_table_lock);
return 0;
}
int uwsgi_signal_add_rb_timer(uint8_t sig, int secs, int iterations) {
uwsgi_lock(uwsgi.rb_timer_table_lock);
+14 -14
View File
@@ -10,20 +10,20 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
// leave 1 byte for abstract namespace (108 linux -> 104 bsd/mac)
if (strlen(socket_name) > 102) {
uwsgi_log( "invalid socket name\n");
exit(1);
uwsgi_nuclear_blast();
}
uws_addr = malloc(sizeof(struct sockaddr_un));
if (uws_addr == NULL) {
uwsgi_error("malloc()");
exit(1);
uwsgi_nuclear_blast();
}
memset(uws_addr, 0, sizeof(struct sockaddr_un));
serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (serverfd < 0) {
uwsgi_error("socket()");
exit(1);
uwsgi_nuclear_blast();
}
if (abstract_socket == 0) {
if (unlink(socket_name) != 0 && errno != ENOENT) {
@@ -44,13 +44,13 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
if (bind(serverfd, (struct sockaddr *) uws_addr, strlen(socket_name) + abstract_socket + ((void *) uws_addr->sun_path - (void *) uws_addr)) != 0) {
#endif
uwsgi_error("bind()");
exit(1);
uwsgi_nuclear_blast();
}
if (listen(serverfd, listen_queue) != 0) {
uwsgi_error("listen()");
exit(1);
uwsgi_nuclear_blast();
}
// chmod unix socket for lazy users
@@ -104,7 +104,7 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
if (serverfd < 0) {
uwsgi_error("socket()");
exit(1);
uwsgi_nuclear_blast();
}
uwsgi_log( "binding on %d SCTP interfaces on port: %d\n", num_ip, ntohs(uws_addr[0].sin_port));
@@ -112,7 +112,7 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
if (sctp_bindx(serverfd, (struct sockaddr *) uws_addr, num_ip, SCTP_BINDX_ADD_ADDR) != 0) {
uwsgi_error("sctp_bindx()");
exit(1);
uwsgi_nuclear_blast();
}
sctp_im.sinit_max_instreams = 0xFFFF;
@@ -124,7 +124,7 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
if (listen(serverfd, listen_queue) != 0) {
uwsgi_error("listen()");
exit(1);
uwsgi_nuclear_blast();
}
return serverfd;
@@ -328,7 +328,7 @@ char *generate_socket_name(char *socket_name) {
struct ifaddrs *ifap = NULL, *ifa, *ifaf;
if (getifaddrs(&ifap)) {
uwsgi_error("getifaddrs()");
exit(1);
uwsgi_nuclear_blast();
}
// here socket_name will be truncated
@@ -359,7 +359,7 @@ char *generate_socket_name(char *socket_name) {
}
uwsgi_log("unable to find avalid socket address\n");
exit(1);
uwsgi_nuclear_blast();
}
return socket_name ;
}
@@ -381,7 +381,7 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
serverfd = socket(AF_INET, SOCK_STREAM, 0);
if (serverfd < 0) {
uwsgi_error("socket()");
exit(1);
uwsgi_nuclear_blast();
}
if (socket_name[0] == 0) {
@@ -394,7 +394,7 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse, sizeof(int)) < 0) {
uwsgi_error("setsockopt()");
exit(1);
uwsgi_nuclear_blast();
}
if (!uwsgi.no_defer_accept) {
@@ -421,12 +421,12 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
uwsgi_log("probably another instance of uWSGI is running on the same address.\n");
}
uwsgi_error("bind()");
exit(1);
uwsgi_nuclear_blast();
}
if (listen(serverfd, listen_queue) != 0) {
uwsgi_error("listen()");
exit(1);
uwsgi_nuclear_blast();
}
+3
View File
@@ -0,0 +1,3 @@
def application(env, start_response):
while 1:
pass
+35 -62
View File
@@ -394,10 +394,6 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
uwsgi.workers[0].requests++;
uwsgi.workers[uwsgi.mywid].requests++;
if (uwsgi.cores > 1) {
uwsgi.core[wsgi_req->async_id]->requests++;
}
// after_request hook
if (uwsgi.p[wsgi_req->uh.modifier1]->after_request) uwsgi.p[wsgi_req->uh.modifier1]->after_request(wsgi_req);
@@ -504,7 +500,7 @@ polling:
if (read(uwsgi.sockets_poll[uwsgi.sockets_cnt].fd, &uwsgi_signal, 1) <= 0) {
if (uwsgi.no_orphans) {
uwsgi_log_verbose("uWSGI worker %d screams: UAAAAAAH my master died, i will follow him...\n", uwsgi.mywid);
end_me();
end_me(0);
}
}
else {
@@ -614,13 +610,11 @@ void env_to_arg(char *src, char *dst) {
void parse_sys_envs(char **envs) {
struct option *lopt, *aopt;
char **uenvs = envs;
char *earg, *eq_pos;
while(*uenvs) {
if (!strncmp(*uenvs, "UWSGI_", 6)) {
if (!strncmp(*uenvs, "UWSGI_", 6) && strncmp(*uenvs, "UWSGI_RELOADS=",14)) {
earg = uwsgi_malloc(strlen(*uenvs+6)+1);
env_to_arg(*uenvs+6, earg);
eq_pos = strchr(earg, '=');
@@ -629,27 +623,7 @@ void parse_sys_envs(char **envs) {
}
eq_pos[0] = 0;
lopt = uwsgi.long_options;
while ((aopt = lopt)) {
if (!aopt->name)
break;
if (!strcmp(earg, aopt->name)) {
if (aopt->flag) {
*aopt->flag = aopt->val;
}
else {
if (eq_pos[1] != 0) {
manage_opt(aopt->val, eq_pos+1);
}
else {
manage_opt(aopt->val, NULL);
}
}
}
lopt++;
}
add_exported_option(earg, eq_pos+1, 0);
}
uenvs++;
}
@@ -1173,37 +1147,7 @@ end:
return 0;
}
void add_exported_option(int i, char *value) {
char *key = NULL;
struct option *lopt, *aopt;
if (i == 0) {
key = value;
value = NULL;
}
else {
lopt = uwsgi.long_options;
while ((aopt = lopt)) {
if (!aopt->name)
break;
if (aopt->val == 0 && *aopt->flag == i) {
key = (char *) aopt->name;
break;
}
if (aopt->val == i) {
key = (char *) aopt->name;
break;
}
lopt++;
}
}
//uwsgi_log("%s = %s\n", key, value);
if (!key) return;
void add_exported_option(char *key, char *value, int configured) {
if (!uwsgi.exported_opts) {
uwsgi.exported_opts = uwsgi_malloc(sizeof(struct uwsgi_opt*));
@@ -1216,10 +1160,10 @@ void add_exported_option(int i, char *value) {
}
}
uwsgi.exported_opts[uwsgi.exported_opts_cnt] = uwsgi_malloc(sizeof(struct uwsgi_opt));
uwsgi.exported_opts[uwsgi.exported_opts_cnt]->key = key;
uwsgi.exported_opts[uwsgi.exported_opts_cnt]->value = value;
uwsgi.exported_opts[uwsgi.exported_opts_cnt]->configured = configured;
uwsgi.exported_opts_cnt++;
}
@@ -1489,6 +1433,7 @@ void init_magic_table(char *magic_table[]) {
}
magic_table['%'] = "%";
magic_table['('] = "%(";
}
char *uwsgi_get_last_char(char *what, char c) {
@@ -1634,8 +1579,36 @@ int is_a_number(char *what) {
int i;
for(i=0;i<(int)strlen(what);i++) {
if (!isdigit(what[i])) return 0;
if (!isdigit((int)what[i])) return 0;
}
return 1;
}
void uwsgi_unix_signal(int signum, void (*func)(int)) {
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = func;
sigemptyset(&sa.sa_mask);
if (sigaction(signum, &sa, NULL) < 0) {
uwsgi_error("sigaction()");
}
}
char *uwsgi_get_exported_opt(char *key) {
int i;
for (i = 0; i < uwsgi.exported_opts_cnt; i++) {
if (!strcmp(uwsgi.exported_opts[i]->key, key)) {
return uwsgi.exported_opts[i]->value;
}
}
return NULL;
}
+260 -89
View File
@@ -160,6 +160,7 @@ static struct option long_base_options[] = {
{"plugins", required_argument, 0, LONG_ARGS_PLUGINS},
{"remap-modifier", required_argument, 0, LONG_ARGS_REMAP_MODIFIER},
{"dump-options", no_argument, &uwsgi.dump_options, 1},
{"show-config", no_argument, &uwsgi.show_config, 1},
{"print", required_argument, 0, LONG_ARGS_PRINT},
{"version", no_argument, 0, LONG_ARGS_VERSION},
{0, 0, 0, 0}
@@ -188,22 +189,22 @@ void warn_pipe()
}
}
void gracefully_kill()
void gracefully_kill(int signum)
{
uwsgi_log("Gracefully killing worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid);
if (UWSGI_IS_IN_REQUEST) {
uwsgi.workers[uwsgi.mywid].manage_next_request = 0;
} else {
reload_me();
reload_me(0);
}
}
void reload_me()
void reload_me(int signum)
{
exit(UWSGI_RELOAD_CODE);
}
void end_me()
void end_me(int signum)
{
exit(UWSGI_END_CODE);
}
@@ -214,7 +215,7 @@ void goodbye_cruel_world()
exit(0);
}
void kill_them_all()
void kill_them_all(int signum)
{
int i;
uwsgi.to_hell = 1;
@@ -224,16 +225,18 @@ void kill_them_all()
kill(uwsgi.workers[i].pid, SIGINT);
}
for (i = 0; i <= uwsgi.gateways_cnt; i++) {
kill(uwsgi.gateways[i].pid, SIGKILL);
for (i = 0; i < uwsgi.shared->daemons_cnt; i++) {
if (uwsgi.shared->daemons[i].pid > 0)
kill(uwsgi.shared->daemons[i].pid, SIGKILL);
}
for (i = 0; i <= uwsgi.shared->daemons_cnt; i++) {
kill(uwsgi.shared->daemons[i].pid, SIGKILL);
for (i = 0; i < uwsgi.gateways_cnt; i++) {
if (uwsgi.gateways[i].pid > 0)
kill(uwsgi.gateways[i].pid, SIGKILL);
}
}
void grace_them_all()
void grace_them_all(int signum)
{
int i;
uwsgi.to_heaven = 1;
@@ -243,9 +246,16 @@ void grace_them_all()
}
for (i = 0; i < uwsgi.shared->daemons_cnt; i++) {
if (uwsgi.shared->daemons[i].pid > 0)
kill(uwsgi.shared->daemons[i].pid, SIGKILL);
}
for (i = 0; i < uwsgi.gateways_cnt; i++) {
if (uwsgi.gateways[i].pid > 0)
kill(uwsgi.gateways[i].pid, SIGKILL);
}
uwsgi_log("...gracefully killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
if (uwsgi.workers[i].pid > 0)
@@ -254,15 +264,37 @@ void grace_them_all()
}
void reap_them_all()
void uwsgi_nuclear_blast() {
if (!uwsgi.workers) {
reap_them_all(0);
}
else if (uwsgi.master_process) {
if (getpid() == uwsgi.workers[0].pid) {
reap_them_all(0);
}
}
exit(1);
}
void reap_them_all(int signum)
{
int i;
uwsgi.to_heaven = 1;
for (i = 0; i < uwsgi.shared->daemons_cnt; i++) {
if (uwsgi.shared->daemons[i].pid > 0)
kill(uwsgi.shared->daemons[i].pid, SIGKILL);
}
for (i = 0; i < uwsgi.gateways_cnt; i++) {
if (uwsgi.gateways[i].pid > 0)
kill(uwsgi.gateways[i].pid, SIGKILL);
}
if (!uwsgi.workers) return;
uwsgi_log("...brutally killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
if (uwsgi.workers[i].pid > 0)
@@ -281,7 +313,7 @@ void harakiri()
exit(0);
}
void stats()
void stats(int signum)
{
//fix this for better logging(this cause races)
struct uwsgi_app *ua = NULL;
@@ -366,6 +398,18 @@ static void vacuum(void)
if (chdir(uwsgi.cwd)) {
uwsgi_error("chdir()");
}
if (uwsgi.pidfile && !uwsgi.uid) {
if (unlink(uwsgi.pidfile)) {
uwsgi_error("unlink()");
} else {
uwsgi_log("VACUUM: pidfile removed.\n");
}
}
if (uwsgi.chdir) {
if (chdir(uwsgi.chdir)) {
uwsgi_error("chdir()");
}
}
for (i = 0; i < uwsgi.sockets_cnt; i++) {
if (uwsgi.sockets[i].family == AF_UNIX) {
if (unlink(uwsgi.sockets[i].name)) {
@@ -375,13 +419,6 @@ static void vacuum(void)
}
}
}
if (uwsgi.pidfile && !uwsgi.uid) {
if (unlink(uwsgi.pidfile)) {
uwsgi_error("unlink()");
} else {
uwsgi_log("VACUUM: pidfile removed.\n");
}
}
}
}
}
@@ -389,7 +426,7 @@ static void vacuum(void)
int main(int argc, char *argv[], char *envp[])
{
int i;
int i, j;
int rlen;
FILE *pidfile;
@@ -548,21 +585,14 @@ int main(int argc, char *argv[], char *envp[])
while ((i = getopt_long(argc, argv, short_options, uwsgi.long_options, &uwsgi.option_index)) != -1) {
if (i == 0) {
add_exported_option(0, (char *)uwsgi.long_options[uwsgi.option_index].name);
add_exported_option((char *)uwsgi.long_options[uwsgi.option_index].name, "1", 0);
}
else {
add_exported_option((char *)uwsgi.long_options[uwsgi.option_index].name, optarg, 1);
manage_opt(i, optarg);
}
}
if (uwsgi.dump_options) {
struct option *lopt = uwsgi.long_options;
while(lopt->name) {
uwsgi_log("--%s\n", lopt->name);
lopt++;
}
}
if (optind < argc) {
char *lazy = argv[optind];
@@ -611,6 +641,15 @@ int main(int argc, char *argv[], char *envp[])
}
}
if (gethostname(uwsgi.hostname, 255)) {
uwsgi_error("gethostname()");
}
uwsgi.hostname_len = strlen(uwsgi.hostname);
magic_table['v'] = uwsgi.cwd;
magic_table['h'] = uwsgi.hostname;
#ifdef UWSGI_XML
if (uwsgi.xml_config != NULL) {
magic_table['o'] = uwsgi.xml_config;
@@ -625,6 +664,7 @@ int main(int argc, char *argv[], char *envp[])
if (uwsgi_get_last_char(uwsgi.xml_config, '.')) magic_table['e'] = uwsgi_get_last_char(uwsgi.xml_config, '.')+1;
if (uwsgi_get_last_char(magic_table['s'], '.')) magic_table['n'] = uwsgi_concat2n(magic_table['s'], uwsgi_get_last_char(magic_table['s'], '.')-magic_table['s'], "", 0) ;
uwsgi_xml_config(uwsgi.wsgi_req, 0, magic_table);
uwsgi.xml_config = magic_table['p'];
}
#endif
#ifdef UWSGI_INI
@@ -669,12 +709,121 @@ int main(int argc, char *argv[], char *envp[])
//parse environ
parse_sys_envs(environ);
if (gethostname(uwsgi.hostname, 255)) {
uwsgi_error("gethostname()");
// second pass
for (i = 0; i < uwsgi.exported_opts_cnt; i++) {
int has_percent = 0;
char *magic_key = NULL;
char *magic_val = NULL;
if (uwsgi.exported_opts[i]->value && !uwsgi.exported_opts[i]->configured) {
for(j=0;j<(int)strlen(uwsgi.exported_opts[i]->value);j++) {
if (uwsgi.exported_opts[i]->value[j] == '%') {
has_percent = 1;
}
else if (uwsgi.exported_opts[i]->value[j] == '(' && has_percent == 1) {
has_percent = 2;
magic_key = uwsgi.exported_opts[i]->value + j + 1;
}
else if (has_percent > 1) {
if (uwsgi.exported_opts[i]->value[j] == ')') {
if (has_percent <= 2) {
magic_key = NULL;
has_percent = 0;
continue;
}
#ifdef UWSGI_DEBUG
uwsgi_log("need to interpret the %.*s tag\n", has_percent-2, magic_key);
#endif
char *tmp_magic_key = uwsgi_concat2n(magic_key, has_percent-2, "", 0);
magic_val = uwsgi_get_exported_opt(tmp_magic_key);
free(tmp_magic_key);
if (!magic_val) {
magic_key = NULL;
has_percent = 0;
continue;
}
uwsgi.exported_opts[i]->value = uwsgi_concat4n(
uwsgi.exported_opts[i]->value, (magic_key-2) - uwsgi.exported_opts[i]->value,
magic_val, strlen(magic_val),
magic_key + (has_percent-1),
strlen(magic_key + (has_percent-1)), "", 0);
#ifdef UWSGI_DEBUG
uwsgi_log("computed new value = %s\n", uwsgi.exported_opts[i]->value);
#endif
magic_key = NULL;
has_percent = 0;
}
else {
has_percent++;
}
}
else {
has_percent = 0;
}
}
}
}
uwsgi.hostname_len = strlen(uwsgi.hostname);
// ok, the options dictionary is available, lets manage it
struct option *lopt = uwsgi.long_options;
struct option *aopt;
char *val;
for(i=0;i<uwsgi.exported_opts_cnt;i++) {
if (uwsgi.exported_opts[i]->configured) continue;
lopt = uwsgi.long_options;;
while ((aopt = lopt)) {
if (!aopt->name) break;
if (!strcmp(aopt->name, uwsgi.exported_opts[i]->key)) {
val = uwsgi.exported_opts[i]->value;
if (aopt->flag) *aopt->flag = aopt->val;
else if (val) {
if (aopt->has_arg == optional_argument) {
if (!strcasecmp("true", val)) {
val = NULL;
}
}
if (aopt->has_arg == no_argument) {
if (!strcasecmp("false", val) || val[0] == '0') {
lopt++;
continue;
}
}
manage_opt(aopt->val, val);
}
}
lopt++;
}
}
/* uWSGI IS CONFIGURED !!! */
if (uwsgi.dump_options) {
struct option *lopt = uwsgi.long_options;
while(lopt->name) {
uwsgi_log("--%s\n", lopt->name);
lopt++;
}
}
if (uwsgi.show_config) {
fprintf(stdout, "\n;uWSGI instance configuration\n[uwsgi]\n");
for(i=0;i<uwsgi.exported_opts_cnt;i++) {
if (uwsgi.exported_opts[i]->value) {
fprintf(stdout,"%s = %s\n", uwsgi.exported_opts[i]->key, uwsgi.exported_opts[i]->value);
}
else {
fprintf(stdout,"%s = true\n", uwsgi.exported_opts[i]->key);
}
}
fprintf(stdout, ";end of configuration\n\n");
}
#ifdef UWSGI_UDP
@@ -736,7 +885,7 @@ options_parsed:
uwsgi_log("***\n*** You are running a DEBUG version of uWSGI, plese disable DEBUG in uwsgiconfig.py and recompile it ***\n***\n");
#endif
uwsgi_log("compiled with version: %s\n", __VERSION__);
uwsgi_log("compiled with version: %s on %s\n", __VERSION__, UWSGI_BUILD_DATE);
#ifdef __BIG_ENDIAN__
uwsgi_log("*** big endian arch detected ***\n");
@@ -771,19 +920,27 @@ options_parsed:
#define CLONE_NEWIPC 0x08000000
#endif
if (uwsgi.ns) {
void *linux_clone_stack = alloca(uwsgi.page_size);
for(;;) {
pid_t pid = clone(uwsgi_start, linux_clone_stack+uwsgi.page_size, SIGCHLD|CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS, (void *)argv);
char stack[PTHREAD_STACK_MIN];
int waitpid_status;
uwsgi_log("*** jailing uWSGI in %s ***\n", uwsgi.ns);
pid_t pid = clone(uwsgi_start, stack+PTHREAD_STACK_MIN, SIGCHLD|CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS, (void *)argv);
if (pid == -1) {
uwsgi_error("clone()");
exit(1);
}
uwsgi_log("waiting for jailed master dead...\n");
pid = waitpid(pid, NULL, 0);
uwsgi_log("waiting for jailed master (pid: %d) death...\n", (int) pid);
pid = waitpid(pid, &waitpid_status, 0);
if (pid < 0) {
uwsgi_error("waitpid()");
exit(1);
}
// in Linux this is reliable
if (WIFEXITED(waitpid_status) && WEXITSTATUS(waitpid_status) == 1) {
exit(1);
}
uwsgi_log("pid %d ended. Respawning...\n", (int) pid);
}
}
@@ -820,7 +977,7 @@ int uwsgi_start(void *v_argv) {
#ifdef __linux__
if (uwsgi.ns) {
if (getpid() != 1) {
uwsgi_log("your kernel does not support linux pid namespace\n");
exit(1);
@@ -840,25 +997,9 @@ int uwsgi_start(void *v_argv) {
int unmounted = 1;
char *delim0, *delim1;
while(unmounted) {
unmounted = 0;
procmounts = fopen("/proc/self/mounts", "r");
while(fgets(line,1024,procmounts) != NULL) {
delim0 = strchr(line, ' ');
delim0++;
delim1 = strchr(delim0, ' ');
*delim1 = 0;
if (!umount(delim0)) {
unmounted++;
}
}
fclose(procmounts);
}
uwsgi_log("unmounting /proc\n");
if (umount("/proc")) {
uwsgi_error("umount()");
if (chdir(uwsgi.ns)) {
uwsgi_error("chdir()");
exit(1);
}
char *ns_tmp_mountpoint = uwsgi_concat2(uwsgi.ns, "/.uwsgi_ns_tmp_mountpoint");
@@ -873,25 +1014,49 @@ int uwsgi_start(void *v_argv) {
if (chdir(ns_tmp_mountpoint)) {
uwsgi_error("chdir()");
}
if (pivot_root(".", ns_tmp_mountpoint2)) {
uwsgi_error("pivot_root()");
exit(1);
}
if (umount("/.uwsgi_ns_tmp_mountpoint")) {
uwsgi_error("umount tmp()");
if (chdir("/")) {
uwsgi_error("chdir()");
exit(1);
}
rmdir("/.uwsgi_ns_tmp_mountpoint/.uwsgi_ns_tmp_mountpoint");
rmdir("/.uwsgi_ns_tmp_mountpoint");
free(ns_tmp_mountpoint2);
free(ns_tmp_mountpoint);
uwsgi_log("remounting /proc\n");
if (mount("proc","/proc", "proc", 0, NULL)) {
uwsgi_error("mount()");
}
while(unmounted) {
unmounted = 0;
procmounts = fopen("/proc/self/mounts", "r");
while(fgets(line,1024,procmounts) != NULL) {
delim0 = strchr(line, ' ');
delim0++;
delim1 = strchr(delim0, ' ');
*delim1 = 0;
if (!strcmp(delim0,"/") || !strcmp(delim0,"/proc")) continue;
if (!umount(delim0)) {
unmounted++;
}
}
fclose(procmounts);
}
if (rmdir("/.uwsgi_ns_tmp_mountpoint/.uwsgi_ns_tmp_mountpoint")) {
uwsgi_error("rmdir()");
}
if (rmdir("/.uwsgi_ns_tmp_mountpoint")) {
uwsgi_error("rmdir()");
}
free(ns_tmp_mountpoint2);
free(ns_tmp_mountpoint);
}
#endif
@@ -973,6 +1138,11 @@ int uwsgi_start(void *v_argv) {
// never here
exit(1);
}
// do not go on if no socket or gateway is defined
if (!uwsgi.sockets_cnt && !uwsgi.gateways_cnt) {
exit(0);
}
close(emperor_pipe[0]);
uwsgi.has_emperor = 1;
uwsgi.emperor_fd = emperor_pipe[1];
@@ -1081,6 +1251,10 @@ int uwsgi_start(void *v_argv) {
// daemons table lock
uwsgi.daemon_table_lock = uwsgi_mmap_shared_lock();
uwsgi_lock_init(uwsgi.daemon_table_lock);
// cron table lock
uwsgi.cron_table_lock = uwsgi_mmap_shared_lock();
uwsgi_lock_init(uwsgi.cron_table_lock);
}
if (uwsgi.spool_dir) {
@@ -1212,6 +1386,10 @@ int uwsgi_start(void *v_argv) {
socket_type_len = sizeof(struct sockaddr_un);
gsa.sa = &usa.sa;
if (!getsockname(j, gsa.sa, &socket_type_len)) {
if (socket_type_len <= 2) {
// unbound socket
continue;
}
if (gsa.sa->sa_family == AF_UNIX) {
if (!strcmp(usa.sa_un.sun_path, uwsgi.sockets[i].name)) {
uwsgi.sockets[i].fd = j;
@@ -1715,12 +1893,12 @@ uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && !uwsgi.master_process) {
signal(SIGALRM, (void *) &harakiri);
}
signal(SIGHUP, (void *) &gracefully_kill);
signal(SIGINT, (void *) &end_me);
signal(SIGTERM, (void *) &reload_me);
uwsgi_unix_signal(SIGHUP, gracefully_kill);
uwsgi_unix_signal(SIGINT, end_me);
uwsgi_unix_signal(SIGTERM, reload_me);
signal(SIGUSR1, (void *) &stats);
uwsgi_unix_signal(SIGUSR1, stats);
signal(SIGUSR2, (void *) &what_i_am_doing);
@@ -1822,7 +2000,7 @@ uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100
end:
if (uwsgi.workers[uwsgi.mywid].manage_next_request == 0) {
reload_me();
reload_me(0);
} else {
goodbye_cruel_world();
}
@@ -1954,10 +2132,12 @@ end:
return 1;
case LONG_ARGS_CLUSTER:
uwsgi.cluster = optarg;
uwsgi.master_process = 1;
return 1;
case LONG_ARGS_CLUSTER_NODES:
uwsgi.cluster = optarg;
uwsgi.cluster_nodes = 1;
uwsgi.master_process = 1;
return 1;
#endif
case LONG_ARGS_CHROOT:
@@ -2151,7 +2331,11 @@ end:
uwsgi.numproc = rl.rlim_max;
}
else {
#ifdef _SC_NPROCESSORS_ONLN
uwsgi.numproc = (sysconf(_SC_NPROCESSORS_ONLN))*2;
#else
uwsgi.numproc = 1;
#endif
}
}
else {
@@ -2338,14 +2522,12 @@ end:
int j;
if (manage_base_opt(i, optarg)) {
add_exported_option( i, optarg );
return;
}
for (j = 0; j < 0xFF; j++) {
if (uwsgi.p[j]->manage_opt) {
if (uwsgi.p[j]->manage_opt(i, optarg)) {
add_exported_option( i, optarg );
return;
}
}
@@ -2354,7 +2536,6 @@ end:
for (j = 0; j < uwsgi.gp_cnt; j++) {
if (uwsgi.gp[j]->manage_opt) {
if (uwsgi.gp[j]->manage_opt(i, optarg)) {
add_exported_option( i, optarg );
return;
}
}
@@ -2606,27 +2787,12 @@ void build_options() {
void manage_string_opt(char *key, uint16_t keylen, char *val, uint16_t vallen, void *data) {
struct option *lopt, *aopt;
// never free this value
char *key2 = uwsgi_concat2n(key, keylen, "", 0);
char *val2 = uwsgi_concat2n(val, vallen, "", 0);
uwsgi_log("%s = %s\n", key2, val2);
lopt = uwsgi.long_options;
while ((aopt = lopt)) {
if (!aopt->name) break;
if (!strcmp(key2, aopt->name)) {
if (aopt->flag) {
*aopt->flag = aopt->val;
add_exported_option(0, key2);
}
else {
manage_opt(aopt->val, val2);
}
}
lopt++;
}
add_exported_option(key2, val2, 0);
}
#ifdef UWSGI_UDP
@@ -2641,6 +2807,11 @@ int uwsgi_cluster_add_me() {
uint16_t ustrlen;
char numproc[6];
if (!uwsgi.sockets[0].name) {
uwsgi_log("you need to specify at least a socket to start a uWSGI cluster\n");
exit(1);
}
snprintf(numproc, 6, "%d", uwsgi.numproc);
size_t len = 2 + strlen(key1) + 2 + strlen(uwsgi.hostname) + 2 + strlen(key2) + 2 + strlen(uwsgi.sockets[0].name) + 2 + strlen(key3) + 2 + strlen(numproc) + 2 + strlen(key4) + 2 + 1;
+39 -9
View File
@@ -20,6 +20,7 @@
#define MAX_GATEWAYS 64
#define MAX_DAEMONS 8
#define MAX_SUBSCRIPTIONS 8
#define MAX_CRONS 64
#ifndef UWSGI_LOAD_EMBEDDED_PLUGINS
#define UWSGI_LOAD_EMBEDDED_PLUGINS
@@ -273,6 +274,7 @@ struct uwsgi_cache_item {
struct uwsgi_opt {
char *key;
char *value;
int configured;
};
#define MAX_CLUSTER_NODES 100
@@ -721,6 +723,7 @@ struct uwsgi_server {
struct uwsgi_opt **exported_opts;
int exported_opts_cnt;
int dump_options;
int show_config;
//base for all the requests(even on async mode)
struct wsgi_request **wsgi_requests;
struct wsgi_request *wsgi_req;
@@ -976,6 +979,7 @@ struct uwsgi_server {
void *fmon_table_lock;
void *timer_table_lock;
void *rb_timer_table_lock;
void *cron_table_lock;
void *rpc_table_lock;
void *spooler_lock;
@@ -1061,6 +1065,18 @@ struct uwsgi_snmp_server_value {
};
#endif
struct uwsgi_cron {
int minute;
int hour;
int day;
int month;
int week;
time_t last_job;
uint8_t sig;
};
struct uwsgi_shared {
//vga 80 x25 specific !
@@ -1116,6 +1132,9 @@ struct uwsgi_shared {
#ifdef __linux__
struct tcp_info ti;
#endif
struct uwsgi_cron cron[MAX_CRONS];
int cron_cnt;
};
struct uwsgi_core {
@@ -1125,7 +1144,7 @@ struct uwsgi_core {
time_t harakiri;
uint64_t requests;
//uint64_t requests;
uint64_t failed_requests;
//multiple ts per - core are needed only with multiple_interpreter + threads
@@ -1163,12 +1182,12 @@ char *uwsgi_get_cwd(void);
void warn_pipe(void);
void what_i_am_doing(void);
void goodbye_cruel_world(void);
void gracefully_kill(void);
void reap_them_all(void);
void kill_them_all(void);
void grace_them_all(void);
void reload_me(void);
void end_me(void);
void gracefully_kill(int);
void reap_them_all(int);
void kill_them_all(int);
void grace_them_all(int);
void reload_me(int);
void end_me(int);
int bind_to_unix(char *, int, int, int);
int bind_to_tcp(char *, int, char *);
int bind_to_udp(char *, int, int);
@@ -1188,7 +1207,7 @@ void log_request(struct wsgi_request *);
void get_memusage(void);
void harakiri(void);
void stats(void);
void stats(int);
#ifdef UWSGI_XML
void uwsgi_xml_config(struct wsgi_request *, int, char *[]);
@@ -1367,7 +1386,7 @@ ssize_t uwsgi_sendfile(struct wsgi_request *);
void uwsgi_register_loop(char *, void *);
void *uwsgi_get_loop(char *);
void add_exported_option(int, char *);
void add_exported_option(char *, char *, int);
ssize_t uwsgi_send_empty_pkt(int , char *, uint8_t , uint8_t);
@@ -1566,6 +1585,7 @@ int uwsgi_dict_set(struct uwsgi_dict *, char *, uint16_t, char *, uint64_t);
struct uwsgi_subscriber_name *uwsgi_get_subscriber(struct uwsgi_dict *, char *, uint16_t);
#ifndef _NO_UWSGI_RB
#include "lib/rbtree.h"
struct uwsgi_rb_timer {
@@ -1579,3 +1599,13 @@ struct uwsgi_rb_timer {
struct rb_root *uwsgi_init_rb_timer(void);
struct uwsgi_rb_timer *uwsgi_add_rb_timer(struct rb_root *, time_t, void *);
struct uwsgi_rb_timer *uwsgi_min_rb_timer(struct rb_root *);
#endif
void uwsgi_nuclear_blast();
void uwsgi_unix_signal(int, void (*)(int));
char *uwsgi_get_exported_opt(char *);
int uwsgi_signal_add_cron(uint8_t, int, int, int, int, int);
+4 -1
View File
@@ -2,6 +2,7 @@
import os
import re
import time
uwsgi_os = os.uname()[0]
uwsgi_os_k = re.split('[-+]', os.uname()[2])[0]
uwsgi_os_v = os.uname()[3]
@@ -208,6 +209,8 @@ class uConf(object):
sys.exit(1)
def get_gcll(self):
self.cflags.append('-DUWSGI_BUILD_DATE="\\"%s\\""' % time.strftime("%d %B %Y %H:%M:%S"))
kvm_list = ['FreeBSD', 'OpenBSD', 'NetBSD', 'DragonFly']
if uwsgi_os == 'SunOS':
@@ -256,7 +259,7 @@ class uConf(object):
if int(sun_major) >= 5:
if int(sun_minor) >= 10:
event_mode = 'port'
elif uwsgi_os in ('Darwin', 'FreeBSD', 'OpenBSD'):
elif uwsgi_os in ('Darwin', 'FreeBSD', 'OpenBSD', 'NetBSD'):
event_mode = 'kqueue'
if event_mode == 'epoll':
+9
View File
@@ -0,0 +1,9 @@
[uwsgi]
trac = trac.web.main
id = myapp
socket = /tmp/%(id).sock
env = TRAC_ENV=/opt/projects/%(id)
module = %(trac):dispatch_request
processes = 8
master = true
+31
View File
@@ -0,0 +1,31 @@
<uwsgi>
<id>app001</id>
<print>starting dir is %v</print>
<chdir>%v/../</chdir>
<localhost>127.0.0.1</localhost>
<socket>%(localhost):3031</socket>
<socket>/tmp/%(id).sock</socket>
<cpus>4</cpus>
<werkzeug>werkzeug.testapp:test_app</werkzeug>
<master/>
<vhost/>
<vhost-host/>
<processes>%(cpus)</processes>
<app domain="sirius001:9090">
<script>werkzeug.testapp:test_app</script>
</app>
<app domain="sirius002:9090">
<script>uwsgicc</script>
</app>
</uwsgi>
+18 -45
View File
@@ -18,12 +18,11 @@ void uwsgi_xml_config(struct wsgi_request *wsgi_req, int app_tag, char *magic_ta
xmlChar *xml_uwsgi_mountpoint = NULL;
xmlChar *xml_uwsgi_domain = NULL;
xmlChar *node_mode;
struct option *lopt, *aopt;
char *colon;
char *xml_id;
int i;
char *xml_id;
char *xml_content;
int xml_size = 0;
@@ -105,52 +104,26 @@ void uwsgi_xml_config(struct wsgi_request *wsgi_req, int app_tag, char *magic_ta
continue;
}
#endif
lopt = uwsgi.long_options;
while ((aopt = lopt)) {
if (!aopt->name)
break;
if (!strcmp((char *) node->name, aopt->name)) {
if (!node->children && aopt->has_arg) {
uwsgi_log( "[uWSGI] %s option need a value. skip.\n", aopt->name);
exit(1);
}
if (node->children) {
if (!node->children->content && aopt->has_arg) {
uwsgi_log( "[uWSGI] %s option need a value. skip.\n", aopt->name);
exit(1);
}
}
node_mode = xmlGetProp(node, (const xmlChar *) "mode");
if (uwsgi.mode && node_mode) {
if (strcmp(uwsgi.mode, (char *) node_mode)) {
continue;
}
}
node_mode = xmlGetProp(node, (const xmlChar *) "mode");
if (uwsgi.mode && node_mode) {
if (strcmp(uwsgi.mode, (char *) node_mode)) {
goto next;
}
}
xml_id = (char *) xmlGetProp(node, (const xmlChar *) "id");
if (colon && xml_id) {
if (strcmp(colon, xml_id)) {
continue;
}
}
xml_id = (char *) xmlGetProp(node, (const xmlChar *) "id");
if (colon && xml_id) {
if (strcmp(colon, xml_id)) {
goto next;
}
}
if (aopt->flag) {
*aopt->flag = aopt->val;
add_exported_option(0, (char *) node->name);
}
else {
if (node->children) {
manage_opt(aopt->val, (char *) node->children->content);
}
else {
manage_opt(aopt->val, NULL);
}
}
}
next:
lopt++;
if (node->children) {
add_exported_option((char *) node->name, (char *) node->children->content, 0);
}
else {
add_exported_option((char *) node->name, "1", 0);
}
}
}
+1 -21
View File
@@ -101,7 +101,6 @@ void uwsgi_yaml_config(char *file, char *magic_table[]) {
int lines = 1;
struct option *lopt, *aopt;
char *section_asked = "uwsgi";
char *colon;
@@ -171,26 +170,7 @@ void uwsgi_yaml_config(char *file, char *magic_table[]) {
//uwsgi_log("YAML: %s = %s\n", key, val);
lopt = uwsgi.long_options;
while ((aopt = lopt)) {
if (!aopt->name)
break;
if (!strcmp(key, aopt->name)) {
if (aopt->flag) {
*aopt->flag = aopt->val;
add_exported_option(0, (char *)key);
}
else {
if (aopt->has_arg == optional_argument) {
if (!strcmp("true", val)) {
val = NULL;
}
}
manage_opt(aopt->val, val);
}
}
lopt++;
}
add_exported_option((char *)key, val, 0);
}
next:
len -= (yaml_line - yaml);