mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-10-04 16:08:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3930960159 | ||
|
|
be12175c8a | ||
|
|
02a1847539 | ||
|
|
0235535658 | ||
|
|
3af75ca64c | ||
|
|
efbe28502d | ||
|
|
47847ee471 | ||
|
|
688ffe32a9 | ||
|
|
c6bbe01f3e | ||
|
|
002af6128c | ||
|
|
5b5c92a9d7 | ||
|
|
7d31d73fc5 | ||
|
|
c5c2c83c65 |
@@ -1,2 +1,3 @@
|
||||
d3850b52334a91d005b0fe45e1e38ba0997a1fb8 no_server mode
|
||||
2d8a617299153ed2efa6ecee7920e9d36c29e0f5 0.9.5beta1
|
||||
04722a2d0121080d14a64df097c278860db5ce8a 0.9.5rc1
|
||||
|
||||
@@ -14,7 +14,7 @@ int async_queue_init(int serverfd) {
|
||||
epfd = epoll_create(256);
|
||||
|
||||
if (epfd < 0) {
|
||||
perror("epoll_create()");
|
||||
uwsgi_error("epoll_create()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ int async_queue_init(int serverfd) {
|
||||
ee.data.fd = serverfd;
|
||||
|
||||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, serverfd, &ee)) {
|
||||
perror("epoll_ctl()");
|
||||
uwsgi_error("epoll_ctl()");
|
||||
close(epfd);
|
||||
return -1;
|
||||
}
|
||||
@@ -45,7 +45,7 @@ int async_wait(int queuefd, void *events, int nevents, int block, int timeout) {
|
||||
//fprintf(stderr,"waiting with timeout %d nevents %d\n", timeout, nevents);
|
||||
ret = epoll_wait(queuefd, (struct epoll_event *) events, nevents, timeout);
|
||||
if (ret < 0) {
|
||||
perror("epoll_wait()");
|
||||
uwsgi_error("epoll_wait()");
|
||||
}
|
||||
return ret ;
|
||||
}
|
||||
@@ -58,7 +58,7 @@ int async_add(int queuefd, int fd, int etype) {
|
||||
ee.data.fd = fd;
|
||||
|
||||
if (epoll_ctl(queuefd, EPOLL_CTL_ADD, fd, &ee)) {
|
||||
perror("epoll_ctl()");
|
||||
uwsgi_error("epoll_ctl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ int async_mod(int queuefd, int fd, int etype) {
|
||||
ee.data.fd = fd;
|
||||
|
||||
if (epoll_ctl(queuefd, EPOLL_CTL_MOD, fd, &ee)) {
|
||||
perror("epoll_ctl()");
|
||||
uwsgi_error("epoll_ctl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ int async_del(int queuefd, int fd, int etype) {
|
||||
ee.data.fd = fd;
|
||||
|
||||
if (epoll_ctl(queuefd, EPOLL_CTL_DEL, fd, &ee)) {
|
||||
perror("epoll_ctl()");
|
||||
uwsgi_error("epoll_ctl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -101,21 +101,25 @@ int async_queue_init(int serverfd) {
|
||||
int dpfd ;
|
||||
struct pollfd dpev;
|
||||
|
||||
|
||||
dpfd = open("/dev/poll", O_RDWR);
|
||||
|
||||
if (dpfd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
|
||||
dpev.fd = serverfd;
|
||||
dpev.events = POLLIN ;
|
||||
dpev.revents = 0;
|
||||
|
||||
if (write(dpfd, &dpev, sizeof(struct pollfd)) < 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return dpfd;
|
||||
}
|
||||
|
||||
@@ -138,7 +142,7 @@ int async_wait(int queuefd, void *events, int nevents, int block, int timeout) {
|
||||
//fprintf(stderr,"waiting with timeout %d nevents %d\n", timeout, nevents);
|
||||
ret = ioctl(queuefd, DP_POLL, &dv);
|
||||
if (ret < 0) {
|
||||
perror("ioctl()");
|
||||
uwsgi_error("ioctl()");
|
||||
}
|
||||
return ret ;
|
||||
}
|
||||
@@ -148,9 +152,10 @@ int async_add(int queuefd, int fd, int etype) {
|
||||
|
||||
pl.fd = fd ;
|
||||
pl.events = etype ;
|
||||
pl.revents = 0 ;
|
||||
|
||||
if (write(queuefd, &pl, sizeof(struct pollfd))) {
|
||||
perror("write()");
|
||||
if (write(queuefd, &pl, sizeof(struct pollfd)) < 0) {
|
||||
uwsgi_error("write()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -163,8 +168,8 @@ int async_mod(int queuefd, int fd, int etype) {
|
||||
}
|
||||
|
||||
int async_del(int queuefd, int fd, int etype) {
|
||||
// to remove an fd from /dev/poll you have to simply close it
|
||||
return 0;
|
||||
// use POLLREMOVE to remove an fd
|
||||
return async_add(queuefd, fd, POLLREMOVE);
|
||||
}
|
||||
|
||||
#else
|
||||
@@ -175,13 +180,13 @@ int async_queue_init(int serverfd) {
|
||||
kfd = kqueue();
|
||||
|
||||
if (kfd < 0) {
|
||||
perror("kqueue()");
|
||||
uwsgi_error("kqueue()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
EV_SET(&kev, serverfd, EVFILT_READ, EV_ADD, 0, 0, 0);
|
||||
if (kevent(kfd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
perror("kevent()");
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -211,7 +216,7 @@ int async_wait(int queuefd, void *events, int nevents, int block, int timeout) {
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
perror("kevent()");
|
||||
uwsgi_error("kevent()");
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -223,7 +228,7 @@ int async_add(int queuefd, int fd, int etype) {
|
||||
|
||||
EV_SET(&kev, fd, etype, EV_ADD, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
perror("kevent()");
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@@ -234,13 +239,13 @@ int async_mod(int queuefd, int fd, int etype) {
|
||||
|
||||
EV_SET(&kev, fd, ASYNC_OUT, EV_DISABLE, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
perror("kevent()");
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
EV_SET(&kev, fd, etype, EV_ADD, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
perror("kevent()");
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@@ -251,7 +256,7 @@ int async_del(int queuefd, int fd, int etype) {
|
||||
|
||||
EV_SET(&kev, fd, etype, EV_DELETE, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
perror("kevent()");
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -260,11 +265,11 @@ int async_del(int queuefd, int fd, int etype) {
|
||||
|
||||
#endif
|
||||
|
||||
struct wsgi_request *next_wsgi_req(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
inline struct wsgi_request *next_wsgi_req(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
uint8_t *ptr = (uint8_t *) wsgi_req ;
|
||||
|
||||
ptr += sizeof(struct wsgi_request)+(uwsgi->buffer_size-1) ;
|
||||
ptr += sizeof(struct wsgi_request) ;
|
||||
|
||||
return (struct wsgi_request *) ptr ;
|
||||
}
|
||||
@@ -340,7 +345,7 @@ struct wsgi_request *find_wsgi_req_by_id(struct uwsgi_server *uwsgi, int async_i
|
||||
|
||||
uint8_t *ptr = (uint8_t *) uwsgi->wsgi_requests ;
|
||||
|
||||
ptr += (sizeof(struct wsgi_request)+(uwsgi->buffer_size-1)) * async_id ;
|
||||
ptr += sizeof(struct wsgi_request) * async_id ;
|
||||
|
||||
return (struct wsgi_request *) ptr ;
|
||||
}
|
||||
@@ -389,7 +394,7 @@ void async_write_all(struct uwsgi_server *uwsgi, char *data, size_t len) {
|
||||
if (wsgi_req->async_status == UWSGI_PAUSED) {
|
||||
rlen = write(wsgi_req->poll.fd, data, len);
|
||||
if (rlen < 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
else {
|
||||
wsgi_req->response_size += rlen ;
|
||||
|
||||
+8
-1
@@ -17,7 +17,14 @@ sub new {
|
||||
sub run {
|
||||
my ($self, $app) = @_;
|
||||
|
||||
my $server = IO::Socket::INET->new(LocalPort => $self->{port}, LocalAddr => $self->{host}, Listen => 100, ReuseAddr => 1);
|
||||
my $server ;
|
||||
|
||||
if (exists($ENV{'UWSGI_FD'})) {
|
||||
$server = IO::Socket::UNIX->new_from_fd($ENV{'UWSGI_FD'}, '+<');
|
||||
}
|
||||
else {
|
||||
$server = IO::Socket::INET->new(LocalPort => $self->{port}, LocalAddr => $self->{host}, Listen => 100, ReuseAddr => 1);
|
||||
}
|
||||
|
||||
while ( my $client = $server->accept ) {
|
||||
|
||||
|
||||
+18
-118
@@ -4,41 +4,22 @@
|
||||
$stdout.sync = true
|
||||
options = {}
|
||||
|
||||
if Process.uid == 0
|
||||
puts "Never run uRack as root !!!"
|
||||
exit
|
||||
end
|
||||
|
||||
$stderr.puts "[#{Time.new}] starting uRack"
|
||||
|
||||
def human_round(float)
|
||||
(float * (10 ** 2)).round / (10 ** 2).to_f
|
||||
end
|
||||
|
||||
unless ENV.has_key?('UNBIT_RACK_PATH')
|
||||
if File.exists?("#{ARGV.last}/config/environment.rb")
|
||||
RAILS_ROOT = String.new(ARGV.last)
|
||||
else
|
||||
RAILS_ROOT = String.new(Dir.getwd)
|
||||
end
|
||||
options[:sockname] = '/tmp/uwsgi.sock'
|
||||
options[:sock_chmod] = 0
|
||||
options[:stderr_logfile] = nil
|
||||
else
|
||||
$domain = ARGV.last.gsub('[','').gsub(']','')
|
||||
$unbit_log_base = "[uRack/Unbit on #{$domain}]"
|
||||
RAILS_ROOT = String.new(ENV['UNBIT_RACK_PATH'])
|
||||
options[:unbit] = true
|
||||
$limit_as = human_round(Process.getrlimit(Process::RLIMIT_AS)[1].to_f/1024/1024)
|
||||
puts "#{$unbit_log_base} process address space limit is #{$limit_as} MB"
|
||||
$uidsec_size = syscall(357,0,0)
|
||||
puts "#{$unbit_log_base} need #{$uidsec_size} bytes to store uidsec_struct..."
|
||||
$uidsec = '1' * $uidsec_size
|
||||
puts "#{$unbit_log_base} uidsec_struct allocated."
|
||||
end
|
||||
$domain = ARGV.last.gsub('[','').gsub(']','')
|
||||
$unbit_log_base = "[uRack/Unbit on #{$domain}]"
|
||||
|
||||
$limit_as = human_round(Process.getrlimit(Process::RLIMIT_AS)[1].to_f/1024/1024)
|
||||
puts "#{$unbit_log_base} process address space limit is #{$limit_as} MB"
|
||||
|
||||
$uidsec_size = syscall(357,0,0)
|
||||
puts "#{$unbit_log_base} need #{$uidsec_size} bytes to store uidsec_struct..."
|
||||
$uidsec = '1' * $uidsec_size
|
||||
puts "#{$unbit_log_base} uidsec_struct allocated."
|
||||
|
||||
|
||||
options[:environment] = (ENV['RAILS_ENV'] || "development").dup
|
||||
options[:processes] = 1
|
||||
options[:serve_file] = nil
|
||||
options[:max_input_size] = 8
|
||||
@@ -46,50 +27,9 @@ options[:unbit_debug] = false
|
||||
options[:master] = true
|
||||
options[:gc_freq] = nil
|
||||
|
||||
require 'optparse'
|
||||
|
||||
ARGV.clone.options do |opts|
|
||||
opts.on('-s', '--socket=socket', String, 'Unix socket path.', 'Default: /tmp/uwsgi.sock') { |v|
|
||||
options[:sockname] = v
|
||||
}
|
||||
opts.on('-C','--chmod','chmod to 666 the unix socket.') {
|
||||
options[:sock_chmod] = true
|
||||
}
|
||||
opts.on('-F','--serve-file','serve static file.') {
|
||||
options[:serve_file] = true
|
||||
}
|
||||
opts.on('-p', '--processes=processes', Integer, 'Number of processes to spawn.', 'Default: 1') {|v|
|
||||
options[:processes] = v
|
||||
}
|
||||
opts.on('-g', '--gc-freq=requests', Integer, 'Number of requests between GC.', 'Default: 1') {|v|
|
||||
options[:gc_freq] = v
|
||||
}
|
||||
opts.on('-d', '--daemon=logfile', 'Put processes in background.', 'Default: all processes stay in foreground') {|v|
|
||||
options[:stderr_logfile] = v
|
||||
}
|
||||
opts.on('-i', '--max-input-size=size', 'Max POST data size (in Kbyte). Bigger data goes into a temporary file.', 'Default: 8') {|v|
|
||||
options[:max_input_size] = v
|
||||
}
|
||||
opts.on('-D', '--unbit-debug', 'Enable debug-level logging.', 'Default: disabled') {|v|
|
||||
options[:unbit_debug] = true
|
||||
}
|
||||
opts.on('-M', '--master-process', 'Enable the master process manager.', 'Default: disabled') {|v|
|
||||
options[:master] = true
|
||||
}
|
||||
opts.on("-e", '--environment=name', String, 'Specifies the environment to run this server under (test/development/production).','Default: development') { |v|
|
||||
options[:environment] = v
|
||||
}
|
||||
|
||||
opts.separator ""
|
||||
|
||||
opts.parse!
|
||||
end
|
||||
|
||||
puts "[#{Time.new}] uRack: loading app [#{RAILS_ROOT}]..."
|
||||
starttime = Time.now
|
||||
require RAILS_ROOT + "/config/environment"
|
||||
|
||||
options[:app_name] = RAILS_ROOT
|
||||
require "config/environment"
|
||||
|
||||
require 'socket'
|
||||
require 'rubygems'
|
||||
@@ -109,39 +49,10 @@ module Rack
|
||||
end
|
||||
def self.run(app, options={})
|
||||
|
||||
# parse the socket options
|
||||
# note: on unbit we use the stdin as communication socket
|
||||
|
||||
server = nil
|
||||
master_pid = Process.pid
|
||||
options[:processes] ||= 1
|
||||
|
||||
unless options[:unbit]
|
||||
begin
|
||||
::File.delete(options[:sockname])
|
||||
rescue
|
||||
end
|
||||
|
||||
server = UNIXServer.new(options[:sockname])
|
||||
if options[:sock_chmod]
|
||||
::File.chmod(0666, options[:sockname])
|
||||
end
|
||||
|
||||
if options[:stderr_logfile]
|
||||
cwd = Dir.getwd
|
||||
Process.daemon
|
||||
Dir.chdir(cwd)
|
||||
$stdout.reopen(options[:stderr_logfile],'a')
|
||||
$stderr.reopen(options[:stderr_logfile],'a')
|
||||
# log files need to be unbuffered !
|
||||
$stdout.sync = true
|
||||
$stderr.sync = true
|
||||
# pid is changed after the .daemon call
|
||||
master_pid = Process.pid
|
||||
end
|
||||
else
|
||||
server = UNIXServer.for_fd($stdin.fileno)
|
||||
end
|
||||
server = UNIXServer.for_fd(0)
|
||||
|
||||
$workers = Array.new
|
||||
|
||||
@@ -334,17 +245,8 @@ module Rack
|
||||
client.close
|
||||
requests = requests+1
|
||||
# logging
|
||||
begin
|
||||
# the syscall 356 is only available on unbit kernels
|
||||
# other systems need to use the proc file way
|
||||
# stat = ::File.open('/proc/self/stat', 'r')
|
||||
# procline = stat.readline
|
||||
# statd = procline.split /\s+/
|
||||
# stat.close
|
||||
$stderr.puts "[#{Time.new}] uRack: [#{options[:app_name]}] req: #{requests} ip: #{env['REMOTE_ADDR']} pid: #{Process.pid} as: #{human_round(syscall(356).to_f/1024/1024)} MB => #{env['REQUEST_METHOD']} #{env['REQUEST_URI']} in #{$speed} secs [#{status}]#{' TIMED OUT !!!' if timed_out}"
|
||||
rescue
|
||||
$stderr.puts "[#{Time.new}] uRack: [#{options[:app_name]}] req: #{requests} unable to get /proc/self/stat or syscall 356"
|
||||
end
|
||||
$stderr.puts "[#{Time.new}] uRack: [#{options[:app_name]}] req: #{requests} ip: #{env['REMOTE_ADDR']} pid: #{Process.pid} as: #{human_round(syscall(356).to_f/1024/1024)} MB => #{env['REQUEST_METHOD']} #{env['REQUEST_URI']} in #{$speed} secs [#{status}]#{' TIMED OUT !!!' if timed_out}"
|
||||
|
||||
if syscall(357, $uidsec, 0) == $uidsec_size
|
||||
if $uidsec[120..123].unpack('i')[0] > 0
|
||||
$stderr.puts "[#{Time.new}] uRack: found a memory allocation error for request #{requests} (pid: #{Process.pid}). Better to kill myself..."
|
||||
@@ -352,6 +254,7 @@ module Rack
|
||||
end
|
||||
end
|
||||
$uidsec = '1' * $uidsec_size
|
||||
|
||||
if options[:unbit_debug]
|
||||
current_as = human_round( (syscall(356).to_f/1024/1024) - $last_as )
|
||||
$stderr.puts "#{$unbit_log_base} resource status after request #{requests}: AS for this request: #{current_as} MB | OBJ for this request: #{ObjectSpace.each_object {} - $last_obj} | OBJ total: #{ObjectSpace.each_object {}}"
|
||||
@@ -388,16 +291,13 @@ app = Rack::Builder.new {
|
||||
end
|
||||
}.to_app
|
||||
|
||||
if options[:unbit]
|
||||
$after_spawn_used_as = human_round(syscall(356).to_f/1024/1024)
|
||||
$stderr.puts "#{$unbit_log_base} now you have #{$limit_as-$after_spawn_used_as} MB of address space available (used #{$after_spawn_used_as}MB after app startup)"
|
||||
end
|
||||
|
||||
secs = Time.now-starttime
|
||||
puts "[#{Time.new}] uRack: ready to serve requests after #{secs.to_i} secs (pid: #{Process.pid})"
|
||||
|
||||
if options[:unbit_debug]
|
||||
$last_as = $after_spawn_used_as ;
|
||||
$last_obj = ObjectSpace.each_object {}
|
||||
end
|
||||
$last_as = $after_spawn_used_as ;
|
||||
$last_obj = ObjectSpace.each_object {}
|
||||
|
||||
server.run(app, options)
|
||||
|
||||
+6
-1
@@ -1,13 +1,18 @@
|
||||
require 'socket'
|
||||
require 'rack/content_length'
|
||||
require 'rack/rewindable_input'
|
||||
require 'stringio'
|
||||
|
||||
module Rack
|
||||
module Handler
|
||||
class Uwsgi
|
||||
|
||||
def self.run(app, options={})
|
||||
server = TCPServer.new(options[:Host], options[:Port])
|
||||
if ENV['UWSGI_FD']
|
||||
server = UNIXServer.for_fd(ENV['UWSGI_FD'].to_i)
|
||||
else
|
||||
server = TCPServer.new(options[:Host], options[:Port])
|
||||
end
|
||||
while client = server.accept
|
||||
serve client, app
|
||||
end
|
||||
|
||||
@@ -47,7 +47,7 @@ PyObject *py_erlang_recv_message(PyObject * self, PyObject * args) {
|
||||
if (timeout > 0) {
|
||||
eret = poll(&erpoll, 1, timeout * 1000);
|
||||
if (eret < 0) {
|
||||
perror("poll()");
|
||||
uwsgi_error("poll()");
|
||||
goto clear;
|
||||
}
|
||||
else if (eret == 0) {
|
||||
@@ -331,7 +331,7 @@ int init_erlang(char *nodename, char *cookie) {
|
||||
}
|
||||
cookiefile = malloc(strlen(cookiehome) + 1 + strlen(".erlang.cookie") + 1);
|
||||
if (!cookiefile) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
}
|
||||
cookiefile[0] = 0;
|
||||
strcat(cookiefile, cookiehome);
|
||||
@@ -339,7 +339,7 @@ int init_erlang(char *nodename, char *cookie) {
|
||||
|
||||
cookiefd = open(cookiefile, O_RDONLY);
|
||||
if (cookiefd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
free(cookiefile);
|
||||
return -1;
|
||||
}
|
||||
@@ -358,7 +358,7 @@ int init_erlang(char *nodename, char *cookie) {
|
||||
|
||||
node = malloc((ip - nodename) + 1);
|
||||
if (node == NULL) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
return -1;
|
||||
}
|
||||
memset(node, 0, (ip - nodename) + 1);
|
||||
@@ -373,7 +373,7 @@ int init_erlang(char *nodename, char *cookie) {
|
||||
|
||||
efd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (efd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -384,26 +384,26 @@ int init_erlang(char *nodename, char *cookie) {
|
||||
|
||||
rlen = 1;
|
||||
if (setsockopt(efd, SOL_SOCKET, SO_REUSEADDR, &rlen, sizeof(rlen))) {
|
||||
perror("setsockopt()");
|
||||
uwsgi_error("setsockopt()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bind(efd, (struct sockaddr *) &e_addr, sizeof(struct sockaddr_in)) < 0) {
|
||||
perror("bind()");
|
||||
uwsgi_error("bind()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rlen = sizeof(struct sockaddr_in);
|
||||
if (getsockname(efd, (struct sockaddr *) &e_addr, (socklen_t *) & rlen)) {
|
||||
perror("getsockname()");
|
||||
uwsgi_error("getsockname()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (listen(efd, uwsgi.listen_queue)) {
|
||||
perror("listen()");
|
||||
uwsgi_error("listen()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
@@ -623,7 +623,7 @@ void erlang_loop(struct wsgi_request *wsgi_req) {
|
||||
|
||||
UWSGI_SET_ERLANGING;
|
||||
for (;;) {
|
||||
if (erl_receive_msg(wsgi_req->poll.fd, (unsigned char *) &wsgi_req->buffer, uwsgi.buffer_size, &em) == ERL_MSG) {
|
||||
if (erl_receive_msg(wsgi_req->poll.fd, (unsigned char *) wsgi_req->buffer, uwsgi.buffer_size, &em) == ERL_MSG) {
|
||||
if (em.type == ERL_TICK)
|
||||
continue;
|
||||
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || defined(__OpenBSD__)
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
||||
#include <kvm.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/user.h>
|
||||
#elif defined(__sun__)
|
||||
/* Terrible Hack !!! */
|
||||
#ifndef _LP64
|
||||
#undef _FILE_OFFSET_BITS
|
||||
#endif
|
||||
#include <procfs.h>
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#endif
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
@@ -96,6 +102,19 @@ void get_memusage() {
|
||||
fclose(procfile);
|
||||
}
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = uwsgi.workers[uwsgi.mywid].rss_size * uwsgi.page_size;
|
||||
#elif defined (__sun__)
|
||||
psinfo_t info;
|
||||
int procfd ;
|
||||
|
||||
procfd = open("/proc/self/psinfo", O_RDONLY);
|
||||
if (procfd >= 0) {
|
||||
if ( read(procfd, (char *) &info, sizeof(info)) > 0) {
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = (uint64_t) info.pr_rssize * 1024 ;
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = (uint64_t) info.pr_size * 1024 ;
|
||||
}
|
||||
close(procfd);
|
||||
}
|
||||
|
||||
#elif defined( __APPLE__)
|
||||
/* darwin documentation says that the value are in pages, but they are bytes !!! */
|
||||
struct task_basic_info t_info;
|
||||
@@ -105,13 +124,14 @@ void get_memusage() {
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = t_info.resident_size;
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = t_info.virtual_size;
|
||||
}
|
||||
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || defined(__OpenBSD__)
|
||||
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
||||
kvm_t *kv;
|
||||
int cnt;
|
||||
|
||||
kv = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
|
||||
if (kv) {
|
||||
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
|
||||
struct kinfo_proc *kproc;
|
||||
kproc = kvm_getprocs(kv, KERN_PROC_PID, uwsgi.mypid, &cnt);
|
||||
if (kproc && cnt > 0) {
|
||||
|
||||
@@ -25,18 +25,18 @@ void nagios(struct uwsgi_server *uwsgi) {
|
||||
uwsgi->wsgi_req->uh.pktsize = 0;
|
||||
uwsgi->wsgi_req->uh.modifier2 = 0;
|
||||
if (write(nagios_poll.fd, uwsgi->wsgi_req, 4) != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
fprintf(stdout, "UWSGI CRITICAL: could not send ping packet to workers\n");
|
||||
exit(2);
|
||||
}
|
||||
nagios_poll.events = POLLIN;
|
||||
if (!uwsgi_parse_response(&nagios_poll, uwsgi->shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) uwsgi->wsgi_req, &uwsgi->wsgi_req->buffer)) {
|
||||
if (!uwsgi_parse_response(&nagios_poll, uwsgi->shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) uwsgi->wsgi_req, uwsgi->wsgi_req->buffer)) {
|
||||
fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n");
|
||||
exit(2);
|
||||
}
|
||||
else {
|
||||
if (uwsgi->wsgi_req->uh.pktsize > 0) {
|
||||
fprintf(stdout, "UWSGI WARNING: %.*s\n", uwsgi->wsgi_req->uh.pktsize, &uwsgi->wsgi_req->buffer);
|
||||
fprintf(stdout, "UWSGI WARNING: %.*s\n", uwsgi->wsgi_req->uh.pktsize, uwsgi->wsgi_req->buffer);
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
|
||||
+18
-17
@@ -23,7 +23,7 @@ ssize_t send_udp_message(uint8_t modifier1, char *host, char *message, uint16_t
|
||||
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ ssize_t send_udp_message(uint8_t modifier1, char *host, char *message, uint16_t
|
||||
|
||||
ret = sendto(fd, udpbuff, message_size+4, 0, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
|
||||
if (ret < 0) {
|
||||
perror("sendto()");
|
||||
uwsgi_error("sendto()");
|
||||
}
|
||||
close(fd);
|
||||
|
||||
@@ -75,7 +75,7 @@ int uwsgi_enqueue_message(char *host, int port, uint8_t modifier1, uint8_t modif
|
||||
|
||||
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_poll.fd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ int uwsgi_enqueue_message(char *host, int port, uint8_t modifier1, uint8_t modif
|
||||
uwsgi_poll.events = POLLIN;
|
||||
|
||||
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in), timeout)) {
|
||||
perror("connect()");
|
||||
uwsgi_error("connect()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
@@ -98,14 +98,14 @@ int uwsgi_enqueue_message(char *host, int port, uint8_t modifier1, uint8_t modif
|
||||
|
||||
cnt = write(uwsgi_poll.fd, &uh, 4);
|
||||
if (cnt != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cnt = write(uwsgi_poll.fd, message, size);
|
||||
if (cnt != size) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
@@ -136,7 +136,7 @@ PyObject *uwsgi_send_message(const char *host, int port, uint8_t modifier1, uint
|
||||
|
||||
uwsgi_mpoll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_mpoll.fd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
@@ -149,7 +149,7 @@ PyObject *uwsgi_send_message(const char *host, int port, uint8_t modifier1, uint
|
||||
UWSGI_SET_BLOCKING;
|
||||
|
||||
if (timed_connect(&uwsgi_mpoll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in), timeout)) {
|
||||
perror("connect()");
|
||||
uwsgi_error("connect()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
@@ -161,7 +161,7 @@ PyObject *uwsgi_send_message(const char *host, int port, uint8_t modifier1, uint
|
||||
|
||||
cnt = write(uwsgi_mpoll.fd, &uh, 4);
|
||||
if (cnt != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
@@ -169,7 +169,7 @@ PyObject *uwsgi_send_message(const char *host, int port, uint8_t modifier1, uint
|
||||
|
||||
cnt = write(uwsgi_mpoll.fd, message, size);
|
||||
if (cnt != size) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
@@ -208,7 +208,7 @@ int uwsgi_parse_response(struct pollfd *upoll, int timeout, struct uwsgi_header
|
||||
/* first 4 byte header */
|
||||
rlen = poll(upoll, 1, timeout * 1000);
|
||||
if (rlen < 0) {
|
||||
perror("poll()");
|
||||
uwsgi_error("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
@@ -222,7 +222,7 @@ int uwsgi_parse_response(struct pollfd *upoll, int timeout, struct uwsgi_header
|
||||
while (i < 4) {
|
||||
rlen = poll(upoll, 1, timeout * 1000);
|
||||
if (rlen < 0) {
|
||||
perror("poll()");
|
||||
uwsgi_error("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
@@ -265,7 +265,7 @@ int uwsgi_parse_response(struct pollfd *upoll, int timeout, struct uwsgi_header
|
||||
while (i < uh->pktsize) {
|
||||
rlen = poll(upoll, 1, timeout * 1000);
|
||||
if (rlen < 0) {
|
||||
perror("poll()");
|
||||
uwsgi_error("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
@@ -292,7 +292,7 @@ int uwsgi_parse_response(struct pollfd *upoll, int timeout, struct uwsgi_header
|
||||
|
||||
int uwsgi_parse_vars(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
char *buffer = &wsgi_req->buffer;
|
||||
char *buffer = wsgi_req->buffer;
|
||||
|
||||
char *ptrbuf, *bufferend;
|
||||
|
||||
@@ -392,6 +392,7 @@ int uwsgi_parse_vars(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req)
|
||||
// var value
|
||||
wsgi_req->hvec[wsgi_req->var_cnt].iov_base = ptrbuf;
|
||||
wsgi_req->hvec[wsgi_req->var_cnt].iov_len = strsize;
|
||||
//fprintf(stderr,"%.*s = %.*s\n", wsgi_req->hvec[wsgi_req->var_cnt-1].iov_len, wsgi_req->hvec[wsgi_req->var_cnt-1].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len, wsgi_req->hvec[wsgi_req->var_cnt].iov_base);
|
||||
if (wsgi_req->var_cnt < uwsgi->vec_size - (4 + 1)) {
|
||||
wsgi_req->var_cnt++;
|
||||
}
|
||||
@@ -436,7 +437,7 @@ int uwsgi_ping_node(int node, struct wsgi_request *wsgi_req) {
|
||||
|
||||
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_poll.fd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -449,12 +450,12 @@ int uwsgi_ping_node(int node, struct wsgi_request *wsgi_req) {
|
||||
wsgi_req->uh.pktsize = 0;
|
||||
wsgi_req->uh.modifier2 = 0;
|
||||
if (write(uwsgi_poll.fd, wsgi_req, 4) != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_poll.events = POLLIN;
|
||||
if (!uwsgi_parse_response(&uwsgi_poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, &wsgi_req->buffer)) {
|
||||
if (!uwsgi_parse_response(&uwsgi_poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, wsgi_req->buffer)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -146,7 +146,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
// allocate memory for connections
|
||||
upcs = malloc(sizeof(struct uwsgi_proxy_connection) * max_connections);
|
||||
if (!upcs) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
memset(upcs, 0, sizeof(struct uwsgi_proxy_connection) * max_connections);
|
||||
@@ -169,7 +169,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
#endif
|
||||
|
||||
if (!eevents) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
|
||||
nevents = async_wait(efd, eevents, max_events, -1, 0);
|
||||
if (nevents < 0) {
|
||||
perror("epoll_wait()");
|
||||
uwsgi_error("epoll_wait()");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -195,7 +195,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
// new connection, accept it
|
||||
ev.ASYNC_FD = accept(proxyfd, (struct sockaddr *) &upc_addr, &upc_len);
|
||||
if (ev.ASYNC_FD < 0) {
|
||||
perror("accept()");
|
||||
uwsgi_error("accept()");
|
||||
continue;
|
||||
}
|
||||
upcs[ev.ASYNC_FD].node = -1;
|
||||
@@ -204,7 +204,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
|
||||
upcs[ev.ASYNC_FD].dest_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (upcs[ev.ASYNC_FD].dest_fd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
@@ -212,7 +212,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
|
||||
// set nonblocking
|
||||
if (ioctl(upcs[ev.ASYNC_FD].dest_fd, FIONBIO, &nonblocking)) {
|
||||
perror("ioctl()");
|
||||
uwsgi_error("ioctl()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
@@ -250,7 +250,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
|
||||
// re-set blocking
|
||||
if (ioctl(upcs[upcs[ev.ASYNC_FD].dest_fd].dest_fd, FIONBIO, &blocking)) {
|
||||
perror("ioctl()");
|
||||
uwsgi_error("ioctl()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
@@ -271,7 +271,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
}
|
||||
else {
|
||||
// connection failed, retry with the next node ?
|
||||
perror("connect()");
|
||||
uwsgi_error("connect()");
|
||||
// close only when all node are tried
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
@@ -296,7 +296,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
|
||||
rlen = read(eevents[i].ASYNC_FD, buffer, 4096);
|
||||
if (rlen < 0) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
uwsgi_proxy_close(upcs, eevents[i].ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
@@ -307,7 +307,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
else {
|
||||
wlen = write(upcs[eevents[i].ASYNC_FD].dest_fd, buffer, rlen);
|
||||
if (wlen != rlen) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
uwsgi_proxy_close(upcs, eevents[i].ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
@@ -333,7 +333,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
|
||||
#ifdef UWSGI_PROXY_USE_KQUEUE
|
||||
if (getsockopt(eevents[i].ASYNC_FD, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
perror("getsockopt()");
|
||||
uwsgi_error("getsockopt()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
@@ -366,7 +366,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
}
|
||||
// re-set blocking
|
||||
if (ioctl(ev.ASYNC_FD, FIONBIO, &blocking)) {
|
||||
perror("ioctl()");
|
||||
uwsgi_error("ioctl()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
@@ -378,7 +378,7 @@ void uwsgi_proxy(int proxyfd) {
|
||||
else {
|
||||
if (upcs[eevents[i].ASYNC_FD].status == UWSGI_PROXY_CONNECTING) {
|
||||
if (getsockopt(eevents[i].ASYNC_FD, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
perror("getsockopt()");
|
||||
uwsgi_error("getsockopt()");
|
||||
}
|
||||
/* is something bad ? */
|
||||
if (soopt) {
|
||||
|
||||
@@ -8,11 +8,10 @@ int manage_python_response(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi
|
||||
ssize_t sf_len = 0 ;
|
||||
#endif
|
||||
|
||||
//fprintf(stderr,"managing request for %d %p\n", wsgi_req->async_id, wsgi_req);
|
||||
// return or yield ?
|
||||
if (PyString_Check((PyObject *)wsgi_req->async_result)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(wsgi_req->async_result), PyString_Size(wsgi_req->async_result))) < 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
goto clear;
|
||||
}
|
||||
wsgi_req->response_size += wsize;
|
||||
@@ -22,8 +21,7 @@ int manage_python_response(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi
|
||||
#ifdef PYTHREE
|
||||
if (PyBytes_Check((PyObject *)wsgi_req->async_result)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyBytes_AsString(wsgi_req->async_result), PyBytes_Size(wsgi_req->async_result))) < 0) {
|
||||
perror("write()");
|
||||
Py_DECREF(wsgi_req->async_result);
|
||||
uwsgi_error("write()");
|
||||
goto clear;
|
||||
}
|
||||
wsgi_req->response_size += wsize;
|
||||
@@ -47,13 +45,13 @@ int manage_python_response(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// ok its a yield
|
||||
if (!wsgi_req->async_placeholder) {
|
||||
wsgi_req->async_placeholder = PyObject_GetIter(wsgi_req->async_result);
|
||||
if (!wsgi_req->async_placeholder) {
|
||||
goto clear2;
|
||||
}
|
||||
Py_DECREF((PyObject *)wsgi_req->async_result);
|
||||
#ifdef UWSGI_ASYNC
|
||||
if (uwsgi->async > 1) {
|
||||
return UWSGI_AGAIN;
|
||||
@@ -63,6 +61,7 @@ int manage_python_response(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi
|
||||
|
||||
|
||||
|
||||
|
||||
pychunk = PyIter_Next(wsgi_req->async_placeholder) ;
|
||||
|
||||
if (!pychunk) {
|
||||
@@ -74,7 +73,7 @@ int manage_python_response(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi
|
||||
|
||||
if (PyString_Check(pychunk)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(pychunk), PyString_Size(pychunk))) < 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
Py_DECREF(pychunk);
|
||||
goto clear;
|
||||
}
|
||||
@@ -84,7 +83,7 @@ int manage_python_response(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi
|
||||
#ifdef PYTHREE
|
||||
if (PyBytes_Check(pychunk)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyBytes_AsString(pychunk), PyBytes_Size(pychunk))) < 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
Py_DECREF(pychunk);
|
||||
goto clear;
|
||||
}
|
||||
|
||||
+8
-8
@@ -35,7 +35,7 @@ ssize_t uwsgi_sendfile(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
if (!wsgi_req->sendfile_fd_size) {
|
||||
|
||||
if (fstat(fd, &stat_buf)) {
|
||||
perror("fstat()");
|
||||
uwsgi_error("fstat()");
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
@@ -61,7 +61,7 @@ ssize_t uwsgi_sendfile(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
}
|
||||
|
||||
if (sf_ret) {
|
||||
perror("sendfile()");
|
||||
uwsgi_error("sendfile()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ ssize_t uwsgi_sendfile(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
}
|
||||
|
||||
if (sf_ret) {
|
||||
perror("sendfile()");
|
||||
uwsgi_error("sendfile()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ ssize_t uwsgi_sendfile(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
}
|
||||
|
||||
if (sf_ret < 0) {
|
||||
perror("sendfile()");
|
||||
uwsgi_error("sendfile()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -119,12 +119,12 @@ ssize_t uwsgi_sendfile(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
if (uwsgi->async > 1) {
|
||||
jlen = read(fd, nosf_buf, wsgi_req->sendfile_fd_chunk);
|
||||
if (jlen <= 0) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
return 0;
|
||||
}
|
||||
jlen = write(sockfd, nosf_buf, jlen);
|
||||
if (jlen <= 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
return 0;
|
||||
}
|
||||
return jlen ;
|
||||
@@ -133,13 +133,13 @@ ssize_t uwsgi_sendfile(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
while (i < wsgi_req->sendfile_fd_size) {
|
||||
jlen = read(fd, nosf_buf, wsgi_req->sendfile_fd_chunk);
|
||||
if (jlen <= 0) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
break;
|
||||
}
|
||||
i += jlen;
|
||||
jlen = write(sockfd, nosf_buf, jlen);
|
||||
if (jlen <= 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
break;
|
||||
}
|
||||
rlen += jlen ;
|
||||
|
||||
@@ -2,3 +2,7 @@ def application(env, start_response):
|
||||
#print env
|
||||
start_response('200 Ok', [('Content-type', 'text/plain')])
|
||||
yield "hello world"
|
||||
yield "hello world2"
|
||||
|
||||
for i in xrange(1,1000):
|
||||
yield str(i)
|
||||
|
||||
@@ -206,7 +206,7 @@ void manage_snmp(int fd, uint8_t * buffer, int size, struct sockaddr_in *client_
|
||||
|
||||
if (size > 0) {
|
||||
if (sendto(fd, buffer, size, 0, (struct sockaddr *) client_addr, sizeof(struct sockaddr_in)) < 0) {
|
||||
perror("sendto()");
|
||||
uwsgi_error("sendto()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,19 +17,19 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
|
||||
|
||||
uws_addr = malloc(sizeof(struct sockaddr_un));
|
||||
if (uws_addr == NULL) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset(uws_addr, 0, sizeof(struct sockaddr_un));
|
||||
serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (serverfd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
exit(1);
|
||||
}
|
||||
if (abstract_socket == 0) {
|
||||
if (unlink(socket_name) != 0 && errno != ENOENT) {
|
||||
perror("unlink()");
|
||||
uwsgi_error("unlink()");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,12 +41,12 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
|
||||
strcpy(uws_addr->sun_path + abstract_socket, socket_name);
|
||||
|
||||
if (bind(serverfd, (struct sockaddr *) uws_addr, strlen(socket_name) + abstract_socket + ((void *) uws_addr->sun_path - (void *) uws_addr)) != 0) {
|
||||
perror("bind()");
|
||||
uwsgi_error("bind()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (listen(serverfd, listen_queue) != 0) {
|
||||
perror("listen()");
|
||||
uwsgi_error("listen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
|
||||
if (chmod_socket == 1 && abstract_socket == 0) {
|
||||
fprintf(stderr, "chmod() socket to 666 for lazy and brave users\n");
|
||||
if (chmod(socket_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) != 0) {
|
||||
perror("chmod()");
|
||||
uwsgi_error("chmod()");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ int bind_to_sctp(char *socket_name, int listen_queue, char *sctp_port) {
|
||||
|
||||
serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
|
||||
if (serverfd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ int bind_to_sctp(char *socket_name, int listen_queue, char *sctp_port) {
|
||||
|
||||
|
||||
if (sctp_bindx(serverfd, (struct sockaddr *) uws_addr, num_ip, SCTP_BINDX_ADD_ADDR) != 0) {
|
||||
perror("sctp_bindx()");
|
||||
uwsgi_error("sctp_bindx()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -107,11 +107,11 @@ int bind_to_sctp(char *socket_name, int listen_queue, char *sctp_port) {
|
||||
sctp_im.sinit_num_ostreams = 0xFFFF;
|
||||
|
||||
if (setsockopt(serverfd, IPPROTO_SCTP, SCTP_INITMSG, &sctp_im, sizeof(sctp_im))) {
|
||||
perror("setsockopt()");
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
|
||||
if (listen(serverfd, listen_queue) != 0) {
|
||||
perror("listen()");
|
||||
uwsgi_error("listen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ int bind_to_udp(char *socket_name) {
|
||||
|
||||
serverfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (serverfd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ int bind_to_udp(char *socket_name) {
|
||||
fprintf(stderr, "binding on UDP port: %d\n", ntohs(uws_addr.sin_port));
|
||||
|
||||
if (bind(serverfd, (struct sockaddr *) &uws_addr, sizeof(uws_addr)) != 0) {
|
||||
perror("bind()");
|
||||
uwsgi_error("bind()");
|
||||
close(serverfd);
|
||||
return -1;
|
||||
}
|
||||
@@ -180,7 +180,7 @@ int bind_to_udp(char *socket_name) {
|
||||
if (uwsgi.multicast_group) {
|
||||
fprintf(stderr, "joining uWSGI multicast group: %s:%d\n", uwsgi.multicast_group, ntohs(uws_addr.sin_port));
|
||||
if (setsockopt(serverfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc))) {
|
||||
perror("setsockopt()");
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -209,14 +209,14 @@ int connect_to_tcp(char *socket_name, int port, int timeout) {
|
||||
|
||||
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_poll.fd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_poll.events = POLLIN;
|
||||
|
||||
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in), timeout)) {
|
||||
perror("connect()");
|
||||
uwsgi_error("connect()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
@@ -247,12 +247,12 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
|
||||
|
||||
serverfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (serverfd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse, sizeof(int)) < 0) {
|
||||
perror("setsockopt()");
|
||||
uwsgi_error("setsockopt()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -260,14 +260,14 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
|
||||
|
||||
#ifdef __linux__
|
||||
if (setsockopt(serverfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], sizeof(int))) {
|
||||
perror("setsockopt()");
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
#elif defined(__apple__) || defined(__freebsd__)
|
||||
struct accept_filter_arg afa;
|
||||
strcpy(afa.af_name, "dataready");
|
||||
afa.af_arg[0] = 0;
|
||||
if (setsockopt(serverfd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(struct accept_filter_arg))) {
|
||||
perror("setsockopt()");
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -277,12 +277,12 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
|
||||
fprintf(stderr, "binding on TCP port: %d\n", ntohs(uws_addr.sin_port));
|
||||
|
||||
if (bind(serverfd, (struct sockaddr *) &uws_addr, sizeof(uws_addr)) != 0) {
|
||||
perror("bind()");
|
||||
uwsgi_error("bind()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (listen(serverfd, listen_queue) != 0) {
|
||||
perror("listen()");
|
||||
uwsgi_error("listen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -300,12 +300,12 @@ int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_s
|
||||
|
||||
arg = fcntl(fdpoll->fd, F_GETFL, NULL);
|
||||
if (arg < 0) {
|
||||
perror("fcntl()");
|
||||
uwsgi_error("fcntl()");
|
||||
return -1;
|
||||
}
|
||||
arg |= O_NONBLOCK;
|
||||
if (fcntl(fdpoll->fd, F_SETFL, arg) < 0) {
|
||||
perror("fcntl()");
|
||||
uwsgi_error("fcntl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -321,13 +321,13 @@ int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_s
|
||||
cnt = poll(fdpoll, 1, timeout * 1000);
|
||||
/* check for errors */
|
||||
if (cnt < 0 && errno != EINTR) {
|
||||
perror("poll()");
|
||||
uwsgi_error("poll()");
|
||||
return -1;
|
||||
}
|
||||
/* something hapened on the socket ... */
|
||||
else if (cnt > 0) {
|
||||
if (getsockopt(fdpoll->fd, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
perror("getsockopt()");
|
||||
uwsgi_error("getsockopt()");
|
||||
return -1;
|
||||
}
|
||||
/* is something bad ? */
|
||||
@@ -348,7 +348,7 @@ int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_s
|
||||
/* re-set blocking socket */
|
||||
arg &= (~O_NONBLOCK);
|
||||
if (fcntl(fdpoll->fd, F_SETFL, arg) < 0) {
|
||||
perror("fcntl()");
|
||||
uwsgi_error("fcntl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ int spool_request(struct uwsgi_server *uwsgi, char *filename, int rn, char *buff
|
||||
struct uwsgi_header uh;
|
||||
|
||||
if (gethostname(hostname, 256)) {
|
||||
perror("gethostname()");
|
||||
uwsgi_error("gethostname()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -27,16 +27,16 @@ int spool_request(struct uwsgi_server *uwsgi, char *filename, int rn, char *buff
|
||||
|
||||
fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
|
||||
if (fd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __sun__
|
||||
if (lockf(fd, F_LOCK, 0)) {
|
||||
perror("lockf()");
|
||||
uwsgi_error("lockf()");
|
||||
#else
|
||||
if (flock(fd, LOCK_EX)) {
|
||||
perror("flock()");
|
||||
uwsgi_error("flock()");
|
||||
#endif
|
||||
close(fd);
|
||||
return 0;
|
||||
@@ -66,7 +66,7 @@ int spool_request(struct uwsgi_server *uwsgi, char *filename, int rn, char *buff
|
||||
|
||||
clear:
|
||||
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
unlink(filename);
|
||||
close(fd);
|
||||
return 0;
|
||||
@@ -109,7 +109,7 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
}
|
||||
|
||||
if (chdir(uwsgi->spool_dir)) {
|
||||
perror("chdir()");
|
||||
uwsgi_error("chdir()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -143,23 +143,23 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
|
||||
spool_fd = open(dp->d_name, O_RDONLY);
|
||||
if (spool_fd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef __sun__
|
||||
if (lockf(spool_fd, F_LOCK, 0)) {
|
||||
perror("lockf()");
|
||||
uwsgi_error("lockf()");
|
||||
#else
|
||||
if (flock(spool_fd, LOCK_EX)) {
|
||||
perror("flock()");
|
||||
uwsgi_error("flock()");
|
||||
#endif
|
||||
close(spool_fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (read(spool_fd, &uh, 4) != 4) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
close(spool_fd);
|
||||
continue;
|
||||
}
|
||||
@@ -173,7 +173,7 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
while (datasize < uh.pktsize) {
|
||||
rlen = read(spool_fd, &uwstrlen, 2);
|
||||
if (rlen != 2) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
goto next_spool;
|
||||
}
|
||||
datasize += rlen;
|
||||
@@ -182,12 +182,12 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
if (uwstrlen > 0) {
|
||||
key = malloc(uwstrlen + 1);
|
||||
if (!key) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
goto retry_later;
|
||||
}
|
||||
rlen = read(spool_fd, key, uwstrlen);
|
||||
if (rlen != uwstrlen) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
free(key);
|
||||
goto next_spool;
|
||||
}
|
||||
@@ -197,7 +197,7 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
|
||||
rlen = read(spool_fd, &uwstrlen, 2);
|
||||
if (rlen != 2) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
free(key);
|
||||
goto next_spool;
|
||||
}
|
||||
@@ -207,13 +207,13 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
val = malloc(uwstrlen + 1);
|
||||
if (!val) {
|
||||
free(key);
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
goto retry_later;
|
||||
}
|
||||
|
||||
rlen = read(spool_fd, val, uwstrlen);
|
||||
if (rlen != uwstrlen) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
free(key);
|
||||
goto next_spool;
|
||||
}
|
||||
@@ -258,7 +258,7 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
next_spool:
|
||||
|
||||
if (unlink(dp->d_name)) {
|
||||
perror("unlink");
|
||||
uwsgi_error("unlink");
|
||||
fprintf(stderr, "something horrible happened to the spooler. Better to kill it.\n");
|
||||
exit(1);
|
||||
}
|
||||
@@ -271,7 +271,7 @@ void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
closedir(sdir);
|
||||
}
|
||||
else {
|
||||
perror("opendir()");
|
||||
uwsgi_error("opendir()");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -289,13 +289,13 @@ int uwsgi_request_spooler(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_
|
||||
wsgi_req->uh.modifier2 = 0;
|
||||
i = write(wsgi_req->poll.fd, wsgi_req, 4);
|
||||
if (i != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "managing spool request...\n");
|
||||
i = spool_request(uwsgi, spool_filename, uwsgi->workers[0].requests + 1, &wsgi_req->buffer, wsgi_req->uh.pktsize);
|
||||
i = spool_request(uwsgi, spool_filename, uwsgi->workers[0].requests + 1, wsgi_req->buffer, wsgi_req->uh.pktsize);
|
||||
wsgi_req->uh.modifier1 = 255;
|
||||
wsgi_req->uh.pktsize = 0;
|
||||
if (i > 0) {
|
||||
@@ -304,7 +304,7 @@ int uwsgi_request_spooler(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_
|
||||
fprintf(stderr, "disconnected client, remove spool file.\n");
|
||||
/* client disconnect, remove spool file */
|
||||
if (unlink(spool_filename)) {
|
||||
perror("unlink()");
|
||||
uwsgi_error("unlink()");
|
||||
fprintf(stderr, "something horrible happened !!! check your spooler ASAP !!!\n");
|
||||
goodbye_cruel_world();
|
||||
}
|
||||
@@ -316,7 +316,7 @@ int uwsgi_request_spooler(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_
|
||||
wsgi_req->uh.modifier2 = 0;
|
||||
i = write(wsgi_req->poll.fd, wsgi_req, 4);
|
||||
if (i != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -62,13 +62,13 @@ void stackless_init(struct uwsgi_server *uwsgi) {
|
||||
|
||||
uwsgi->stackless_table = malloc( sizeof(struct stackless_req*) * uwsgi->async);
|
||||
if (!uwsgi->stackless_table) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
uwsgi->stackless_table[i] = malloc(sizeof(struct stackless_req));
|
||||
if (!uwsgi->stackless_table[i]) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
memset(uwsgi->stackless_table[i], 0, sizeof(struct stackless_req));
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import uwsgi
|
||||
import psycopg2
|
||||
|
||||
|
||||
def ugreen_wait_callback(conn, timeout=-1):
|
||||
"""A wait callback useful to allow uWSGI/uGreen to work with Psycopg."""
|
||||
while True:
|
||||
state = conn.poll()
|
||||
if state == psycopg2.extensions.POLL_OK:
|
||||
break
|
||||
elif state == psycopg2.extensions.POLL_READ:
|
||||
uwsgi.green_wait_fdread(conn.fileno())
|
||||
elif state == psycopg2.extensions.POLL_WRITE:
|
||||
uwsgi.green_wait_fdwrite(conn.fileno())
|
||||
else:
|
||||
raise Exception("Unexpected result from poll: %r", state)
|
||||
|
||||
|
||||
|
||||
# set the wait callback
|
||||
psycopg2.extensions.set_wait_callback(ugreen_wait_callback)
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
start_response('200 Ok', [('Content-type', 'text/html')])
|
||||
|
||||
# connect
|
||||
conn = psycopg2.connect("dbname=prova user=postgres")
|
||||
# get cursor
|
||||
curs = conn.cursor()
|
||||
|
||||
yield "<table>"
|
||||
|
||||
# run query
|
||||
curs.execute("SELECT * FROM tests")
|
||||
|
||||
while True:
|
||||
row = curs.fetchone()
|
||||
if not row: break
|
||||
yield "<tr><td>%s</td></tr>" % str(row)
|
||||
|
||||
yield "</table>"
|
||||
|
||||
conn.close()
|
||||
@@ -0,0 +1,50 @@
|
||||
import uwsgi
|
||||
import psycopg2
|
||||
|
||||
def async_wait(conn):
|
||||
# conn can be a connection or a cursor
|
||||
if not hasattr(conn, 'poll'):
|
||||
conn = conn.connection
|
||||
|
||||
# interesting part: suspend until ready
|
||||
while True:
|
||||
state = conn.poll()
|
||||
if state == psycopg2.extensions.POLL_OK:
|
||||
break
|
||||
elif state == psycopg2.extensions.POLL_READ:
|
||||
uwsgi.green_wait_fdread(conn.fileno())
|
||||
elif state == psycopg2.extensions.POLL_WRITE:
|
||||
uwsgi.green_wait_fdwrite(conn.fileno())
|
||||
else:
|
||||
raise Exception("Unexpected result from poll: %r", state)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
start_response('200 Ok', [('Content-type', 'text/html')])
|
||||
|
||||
conn = psycopg2.connect("dbname=prova user=postgres", async=True)
|
||||
|
||||
# suspend until connection
|
||||
async_wait(conn)
|
||||
|
||||
curs = conn.cursor()
|
||||
|
||||
yield "<table>"
|
||||
|
||||
curs.execute("SELECT * FROM tests")
|
||||
|
||||
# suspend until result
|
||||
async_wait(curs)
|
||||
|
||||
while True:
|
||||
row = curs.fetchone()
|
||||
if not row: break
|
||||
yield "<tr><td>%s</td></tr>" % str(row)
|
||||
|
||||
yield "</table>"
|
||||
|
||||
conn.close()
|
||||
@@ -18,7 +18,7 @@ void u_green_write_all(struct uwsgi_server *uwsgi, char *data, size_t len) {
|
||||
if (wsgi_req->async_status == UWSGI_PAUSED) {
|
||||
rlen = write(wsgi_req->poll.fd, data, len);
|
||||
if (rlen < 0) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
// mark core as plagued
|
||||
wsgi_req->async_plagued = 1 ;
|
||||
}
|
||||
@@ -262,6 +262,7 @@ static void u_green_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsg
|
||||
}
|
||||
wsgi_req->async_status = UWSGI_OK;
|
||||
|
||||
|
||||
u_green_schedule_to_main(uwsgi, async_id);
|
||||
|
||||
if (wsgi_req_recv(wsgi_req)) {
|
||||
@@ -300,7 +301,7 @@ void u_green_init(struct uwsgi_server *uwsgi) {
|
||||
|
||||
uwsgi->ugreen_contexts = malloc( sizeof(ucontext_t*) * uwsgi->async);
|
||||
if (!uwsgi->ugreen_contexts) {
|
||||
perror("malloc()\n");
|
||||
uwsgi_error("malloc()\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -308,24 +309,26 @@ void u_green_init(struct uwsgi_server *uwsgi) {
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
uwsgi->ugreen_contexts[i] = malloc( sizeof(ucontext_t) );
|
||||
if (!uwsgi->ugreen_contexts[i]) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
getcontext(uwsgi->ugreen_contexts[i]);
|
||||
uwsgi->ugreen_contexts[i]->uc_stack.ss_sp = mmap(NULL, u_stack_size + (uwsgi->page_size*2) , PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0) + uwsgi->page_size;
|
||||
|
||||
if (!uwsgi->ugreen_contexts[i]->uc_stack.ss_sp) {
|
||||
perror("mmap()");
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
// set guard pages for stack
|
||||
if (mprotect(uwsgi->ugreen_contexts[i]->uc_stack.ss_sp - uwsgi->page_size, uwsgi->page_size, PROT_NONE)) {
|
||||
perror("mprotect()");
|
||||
uwsgi_error("mprotect()");
|
||||
exit(1);
|
||||
}
|
||||
if (mprotect(uwsgi->ugreen_contexts[i]->uc_stack.ss_sp + u_stack_size, uwsgi->page_size, PROT_NONE)) {
|
||||
perror("mprotect()");
|
||||
uwsgi_error("mprotect()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uwsgi->ugreen_contexts[i]->uc_stack.ss_size = u_stack_size ;
|
||||
uwsgi->ugreen_contexts[i]->uc_link = NULL;
|
||||
makecontext(uwsgi->ugreen_contexts[i], (void (*) (void)) &u_green_request, 3, uwsgi, wsgi_req, i);
|
||||
@@ -397,25 +400,23 @@ void u_green_loop(struct uwsgi_server *uwsgi) {
|
||||
|
||||
while(uwsgi->workers[uwsgi->mywid].manage_next_request) {
|
||||
|
||||
|
||||
uwsgi->async_running = u_green_blocking(uwsgi) ;
|
||||
timeout = u_green_get_timeout(uwsgi);
|
||||
uwsgi->async_nevents = async_wait(uwsgi->async_queue, uwsgi->async_events, uwsgi->async, uwsgi->async_running, timeout);
|
||||
|
||||
u_green_expire_timeouts(uwsgi);
|
||||
|
||||
|
||||
if (uwsgi->async_nevents < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
u_green_expire_timeouts(uwsgi);
|
||||
|
||||
if (uwsgi->async_nevents > 0) {
|
||||
wsgi_req = find_first_accepting_wsgi_req(uwsgi);
|
||||
if (!wsgi_req) goto cycle;
|
||||
}
|
||||
|
||||
for(i=0; i<uwsgi->async_nevents;i++) {
|
||||
|
||||
if (uwsgi->async_events[i].ASYNC_FD == uwsgi->serverfd) {
|
||||
wsgi_req = find_first_accepting_wsgi_req(uwsgi);
|
||||
if (!wsgi_req) goto cycle;
|
||||
u_green_schedule_to_req(uwsgi, wsgi_req);
|
||||
}
|
||||
else {
|
||||
@@ -431,6 +432,7 @@ void u_green_loop(struct uwsgi_server *uwsgi) {
|
||||
}
|
||||
|
||||
cycle:
|
||||
|
||||
wsgi_req = find_wsgi_req_by_id(uwsgi, current) ;
|
||||
if (wsgi_req->async_status != UWSGI_ACCEPTING && wsgi_req->async_status != UWSGI_PAUSED && wsgi_req->async_waiting_fd == -1 && !wsgi_req->async_timeout) {
|
||||
u_green_schedule_to_req(uwsgi, wsgi_req);
|
||||
|
||||
@@ -38,7 +38,7 @@ void daemonize(char *logfile) {
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("fork()");
|
||||
uwsgi_error("fork()");
|
||||
exit(1);
|
||||
}
|
||||
if (pid != 0) {
|
||||
@@ -46,7 +46,7 @@ void daemonize(char *logfile) {
|
||||
}
|
||||
|
||||
if (setsid() < 0) {
|
||||
perror("setsid()");
|
||||
uwsgi_error("setsid()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ void daemonize(char *logfile) {
|
||||
/* refork... */
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("fork()");
|
||||
uwsgi_error("fork()");
|
||||
exit(1);
|
||||
}
|
||||
if (pid != 0) {
|
||||
@@ -65,14 +65,14 @@ void daemonize(char *logfile) {
|
||||
|
||||
|
||||
/*if (chdir("/") != 0) {
|
||||
perror("chdir()");
|
||||
uwsgi_error("chdir()");
|
||||
exit(1);
|
||||
} */
|
||||
|
||||
|
||||
fdin = open("/dev/null", O_RDWR);
|
||||
if (fdin < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ void daemonize(char *logfile) {
|
||||
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket()");
|
||||
uwsgi_error("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ void daemonize(char *logfile) {
|
||||
udp_addr.sin_addr.s_addr = inet_addr(logfile);
|
||||
|
||||
if (connect(fd, (const struct sockaddr *) &udp_addr, sizeof(struct sockaddr_in)) < 0) {
|
||||
perror("connect()");
|
||||
uwsgi_error("connect()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -107,7 +107,7 @@ void daemonize(char *logfile) {
|
||||
#endif
|
||||
fd = open(logfile, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP);
|
||||
if (fd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
exit(1);
|
||||
}
|
||||
#ifdef UWSGI_UDP
|
||||
@@ -116,20 +116,20 @@ void daemonize(char *logfile) {
|
||||
|
||||
/* stdin */
|
||||
if (dup2(fdin, 0) < 0) {
|
||||
perror("dup2()");
|
||||
uwsgi_error("dup2()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
/* stdout */
|
||||
if (dup2(fd, 1) < 0) {
|
||||
perror("dup2()");
|
||||
uwsgi_error("dup2()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* stderr */
|
||||
if (dup2(fd, 2) < 0) {
|
||||
perror("dup2()");
|
||||
uwsgi_error("dup2()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ char *uwsgi_get_cwd() {
|
||||
|
||||
cwd = malloc(newsize);
|
||||
if (cwd == NULL) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -158,11 +158,11 @@ char *uwsgi_get_cwd() {
|
||||
free(cwd);
|
||||
cwd = malloc(newsize);
|
||||
if (cwd == NULL) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
if (getcwd(cwd, newsize) == NULL) {
|
||||
perror("getcwd()");
|
||||
uwsgi_error("getcwd()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -194,7 +194,7 @@ void uwsgi_as_root() {
|
||||
if (uwsgi.chroot) {
|
||||
fprintf(stderr, "chroot() to %s\n", uwsgi.chroot);
|
||||
if (chroot(uwsgi.chroot)) {
|
||||
perror("chroot()");
|
||||
uwsgi_error("chroot()");
|
||||
exit(1);
|
||||
}
|
||||
#ifdef __linux__
|
||||
@@ -206,14 +206,14 @@ void uwsgi_as_root() {
|
||||
if (uwsgi.gid) {
|
||||
fprintf(stderr, "setgid() to %d\n", uwsgi.gid);
|
||||
if (setgid(uwsgi.gid)) {
|
||||
perror("setgid()");
|
||||
uwsgi_error("setgid()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (uwsgi.uid) {
|
||||
fprintf(stderr, "setuid() to %d\n", uwsgi.uid);
|
||||
if (setuid(uwsgi.uid)) {
|
||||
perror("setuid()");
|
||||
uwsgi_error("setuid()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -292,6 +292,7 @@ void wsgi_req_setup(struct wsgi_request *wsgi_req, int async_id) {
|
||||
wsgi_req->async_waiting_fd = -1;
|
||||
#endif
|
||||
wsgi_req->hvec = &uwsgi.async_hvec[wsgi_req->async_id];
|
||||
wsgi_req->buffer = uwsgi.async_buf[wsgi_req->async_id];
|
||||
|
||||
}
|
||||
|
||||
@@ -301,7 +302,7 @@ int wsgi_req_recv(struct wsgi_request *wsgi_req) {
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) gettimeofday(&wsgi_req->start_of_request, NULL);
|
||||
|
||||
if (!uwsgi_parse_response(&wsgi_req->poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, &wsgi_req->buffer)) {
|
||||
if (!uwsgi_parse_response(&wsgi_req->poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, wsgi_req->buffer)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -320,7 +321,7 @@ int wsgi_req_accept(int fd, struct wsgi_request *wsgi_req) {
|
||||
wsgi_req->poll.fd = accept(fd, (struct sockaddr *) &wsgi_req->c_addr, (socklen_t *) &wsgi_req->c_len);
|
||||
|
||||
if (wsgi_req->poll.fd < 0) {
|
||||
perror("accept()");
|
||||
uwsgi_error("accept()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -387,7 +388,7 @@ void parse_sys_envs(char **envs, struct option *long_options) {
|
||||
if (!strncmp(*uenvs, "UWSGI_", 6)) {
|
||||
earg = malloc(strlen(*uenvs+6)+1);
|
||||
if (!earg) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
env_to_arg(*uenvs+6, earg);
|
||||
|
||||
@@ -254,7 +254,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
/* generic shared area */
|
||||
uwsgi.shared = (struct uwsgi_shared *) mmap(NULL, sizeof(struct uwsgi_shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
if (!uwsgi.shared) {
|
||||
perror("mmap()");
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
memset(uwsgi.shared, 0, sizeof(struct uwsgi_shared));
|
||||
@@ -390,13 +390,13 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
if (rlen > 0) {
|
||||
env_reload_buf[rlen] = 0;
|
||||
if (setenv("UWSGI_RELOADS", env_reload_buf, 1)) {
|
||||
perror("setenv()");
|
||||
uwsgi_error("setenv()");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (setenv("UWSGI_RELOADS", "0", 1)) {
|
||||
perror("setenv()");
|
||||
uwsgi_error("setenv()");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -446,7 +446,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
if (!strncmp(*uenvs, "UWSGI_", 6)) {
|
||||
char *earg = malloc(strlen(*uenvs+6)+1);
|
||||
if (!earg) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
env_to_arg(*uenvs+6, earg);
|
||||
@@ -484,7 +484,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
cwd = uwsgi_get_cwd();
|
||||
uwsgi.binary_path = malloc(strlen(argv[0]) + 1);
|
||||
if (uwsgi.binary_path == NULL) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
strcpy(uwsgi.binary_path, argv[0]);
|
||||
@@ -521,7 +521,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
if (uwsgi.rl.rlim_max > 0) {
|
||||
fprintf(stderr, "limiting address space of processes...\n");
|
||||
if (setrlimit(RLIMIT_AS, &uwsgi.rl)) {
|
||||
perror("setrlimit()");
|
||||
uwsgi_error("setrlimit()");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -572,36 +572,52 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
}
|
||||
|
||||
// allocate more wsgi_req for async mode
|
||||
uwsgi.wsgi_requests = malloc((sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) ) * uwsgi.async);
|
||||
uwsgi.wsgi_requests = malloc(sizeof(struct wsgi_request) * uwsgi.async);
|
||||
if (uwsgi.wsgi_requests == NULL) {
|
||||
fprintf(stderr, "unable to allocate memory for requests.\n");
|
||||
exit(1);
|
||||
}
|
||||
memset(uwsgi.wsgi_requests, 0, (sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) ) * uwsgi.async);
|
||||
memset(uwsgi.wsgi_requests, 0, sizeof(struct wsgi_request) * uwsgi.async);
|
||||
|
||||
uwsgi.async_buf = malloc( sizeof(char *) * uwsgi.async);
|
||||
if (!uwsgi.async_buf) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(i=0;i<uwsgi.async;i++) {
|
||||
uwsgi.async_buf[i] = malloc(uwsgi.buffer_size);
|
||||
if (!uwsgi.async_buf[i]) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// by default set wsgi_req to the first slot
|
||||
uwsgi.wsgi_req = uwsgi.wsgi_requests ;
|
||||
|
||||
fprintf(stderr, "allocated %llu bytes (%llu KB) for %d request's buffer.\n", (uint64_t) (sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) ) * uwsgi.async,
|
||||
(uint64_t)( (sizeof(struct wsgi_request) + (uwsgi.buffer_size-1) * uwsgi.async ) / 1024),
|
||||
fprintf(stderr, "allocated %llu bytes (%llu KB) for %d request's buffer.\n", (uint64_t) (sizeof(struct wsgi_request) * uwsgi.async),
|
||||
(uint64_t)( (sizeof(struct wsgi_request) * uwsgi.async ) / 1024),
|
||||
uwsgi.async);
|
||||
|
||||
if (uwsgi.synclog) {
|
||||
fprintf(stderr, "allocating a memory page for synced logging.\n");
|
||||
uwsgi.sync_page = malloc(uwsgi.page_size);
|
||||
if (!uwsgi.sync_page) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (uwsgi.pyhome != NULL) {
|
||||
fprintf(stderr, "Setting PythonHome to %s...\n", uwsgi.pyhome);
|
||||
#ifdef PYTHREE
|
||||
wchar_t *wpyhome;
|
||||
wpyhome = malloc((sizeof(wchar_t) * strlen(uwsgi.pyhome)) + 2);
|
||||
if (!wpyhome) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
mbstowcs(wpyhome, uwsgi.pyhome, strlen(uwsgi.pyhome));
|
||||
@@ -626,6 +642,8 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
|
||||
Py_Initialize();
|
||||
|
||||
|
||||
|
||||
#ifdef PYTHREE
|
||||
mbstowcs(pname, "uwsgi", 6);
|
||||
pyargv[0] = pname;
|
||||
@@ -637,13 +655,19 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
#ifdef PYTHREE
|
||||
wchar_t *wcargv = malloc( sizeof( wchar_t ) * strlen(uwsgi.pyargv));
|
||||
if (!wcargv) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
wchar_t *wa;
|
||||
#endif
|
||||
char *ap;
|
||||
#ifdef __sun__
|
||||
// FIX THIS !!!
|
||||
ap = strtok(uwsgi.pyargv, " ");
|
||||
while ((ap = strtok(NULL, " ")) != NULL) {
|
||||
#else
|
||||
while ((ap = strsep(&uwsgi.pyargv, " \t")) != NULL) {
|
||||
#endif
|
||||
if (*ap != '\0') {
|
||||
#ifdef PYTHREE
|
||||
wa = (wchar_t *) ( (ap-uwsgi.pyargv) * sizeof(wchar_t) );
|
||||
@@ -677,7 +701,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
#ifndef __OpenBSD__
|
||||
uwsgi.sharedareamutex = mmap(NULL, sizeof(pthread_mutexattr_t) + sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
if (!uwsgi.sharedareamutex) {
|
||||
perror("mmap()");
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
@@ -708,7 +732,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
|
||||
}
|
||||
else {
|
||||
perror("mmap()");
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -804,7 +828,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
uwsgi.async_events = malloc( sizeof(struct kevent) * uwsgi.async ) ;
|
||||
#endif
|
||||
if (!uwsgi.async_events) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -831,7 +855,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
/* shared area for workers */
|
||||
uwsgi.workers = (struct uwsgi_worker *) mmap(NULL, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
||||
if (!uwsgi.workers) {
|
||||
perror("mmap()");
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
memset(uwsgi.workers, 0, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1);
|
||||
@@ -844,7 +868,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
fprintf(stderr, "writing pidfile to %s\n", uwsgi.pidfile);
|
||||
pidfile = fopen(uwsgi.pidfile, "w");
|
||||
if (!pidfile) {
|
||||
perror("fopen");
|
||||
uwsgi_error("fopen");
|
||||
exit(1);
|
||||
}
|
||||
if (fprintf(pidfile, "%d\n", masterpid) < 0) {
|
||||
@@ -1015,7 +1039,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
break;
|
||||
}
|
||||
else if (pid < 1) {
|
||||
perror("fork()");
|
||||
uwsgi_error("fork()");
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
@@ -1118,7 +1142,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
fprintf(stderr, "binary reloading uWSGI...\n");
|
||||
if (cwd) {
|
||||
if (chdir(cwd)) {
|
||||
perror("chdir()");
|
||||
uwsgi_error("chdir()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -1132,7 +1156,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
}
|
||||
if (uwsgi.serverfd != 3) {
|
||||
if (dup2(uwsgi.serverfd, 3) < 0) {
|
||||
perror("dup2()");
|
||||
uwsgi_error("dup2()");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -1140,7 +1164,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
argv[0] = uwsgi.binary_path;
|
||||
//strcpy (argv[0], uwsgi.binary_path);
|
||||
execve(uwsgi.binary_path, argv, environ);
|
||||
perror("execve()");
|
||||
uwsgi_error("execve()");
|
||||
// never here
|
||||
exit(1);
|
||||
}
|
||||
@@ -1167,7 +1191,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
else {
|
||||
diedpid = waitpid(WAIT_ANY, &waitpid_status, WNOHANG);
|
||||
if (diedpid == -1) {
|
||||
perror("waitpid()");
|
||||
uwsgi_error("waitpid()");
|
||||
/* here is better to reload all the uWSGI stack */
|
||||
fprintf(stderr, "something horrible happened...\n");
|
||||
reap_them_all();
|
||||
@@ -1193,21 +1217,21 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
if (uwsgi.udp_socket && uwsgi_poll.fd >= 0) {
|
||||
rlen = poll(&uwsgi_poll, 1, check_interval.tv_sec * 1000);
|
||||
if (rlen < 0) {
|
||||
perror("poll()");
|
||||
uwsgi_error("poll()");
|
||||
}
|
||||
else if (rlen > 0) {
|
||||
udp_len = sizeof(udp_client);
|
||||
rlen = recvfrom(uwsgi_poll.fd, &uwsgi.wsgi_req->buffer, uwsgi.buffer_size, 0, (struct sockaddr *) &udp_client, &udp_len);
|
||||
if (rlen < 0) {
|
||||
perror("recvfrom()");
|
||||
uwsgi_error("recvfrom()");
|
||||
}
|
||||
else if (rlen > 0) {
|
||||
memset(udp_client_addr, 0, 16);
|
||||
if (inet_ntop(AF_INET, &udp_client.sin_addr.s_addr, udp_client_addr, 16)) {
|
||||
if (uwsgi.wsgi_req->buffer == UWSGI_MODIFIER_MULTICAST_ANNOUNCE) {
|
||||
if (uwsgi.wsgi_req->buffer[0] == UWSGI_MODIFIER_MULTICAST_ANNOUNCE) {
|
||||
}
|
||||
#ifdef UWSGI_SNMP
|
||||
else if (uwsgi.wsgi_req->buffer == 0x30 && uwsgi.snmp) {
|
||||
else if (uwsgi.wsgi_req->buffer[0] == 0x30 && uwsgi.snmp) {
|
||||
manage_snmp(uwsgi_poll.fd, (uint8_t *) &uwsgi.wsgi_req->buffer, rlen, &udp_client);
|
||||
}
|
||||
#endif
|
||||
@@ -1215,7 +1239,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
if (udp_callable && udp_callable_args) {
|
||||
PyTuple_SetItem(udp_callable_args, 0, PyString_FromString(udp_client_addr));
|
||||
PyTuple_SetItem(udp_callable_args, 1, PyInt_FromLong(ntohs(udp_client.sin_port)));
|
||||
PyTuple_SetItem(udp_callable_args, 2, PyString_FromStringAndSize(&uwsgi.wsgi_req->buffer, rlen));
|
||||
PyTuple_SetItem(udp_callable_args, 2, PyString_FromStringAndSize(uwsgi.wsgi_req->buffer, rlen));
|
||||
PyObject *udp_response = python_call(udp_callable, udp_callable_args);
|
||||
if (udp_response) {
|
||||
Py_DECREF(udp_response);
|
||||
@@ -1225,12 +1249,12 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
}
|
||||
else {
|
||||
// a simple udp logger
|
||||
fprintf(stderr, "[udp:%s:%d] %.*s", udp_client_addr, ntohs(udp_client.sin_port), rlen, &uwsgi.wsgi_req->buffer);
|
||||
fprintf(stderr, "[udp:%s:%d] %.*s", udp_client_addr, ntohs(udp_client.sin_port), rlen, uwsgi.wsgi_req->buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
perror("inet_ntop()");
|
||||
uwsgi_error("inet_ntop()");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1352,7 +1376,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
break;
|
||||
}
|
||||
else if (pid < 1) {
|
||||
perror("fork()");
|
||||
uwsgi_error("fork()");
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Respawned uWSGI worker (new pid: %d)\n", pid);
|
||||
@@ -1485,7 +1509,7 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
goto cycle;
|
||||
}
|
||||
|
||||
wsgi_req_setup(uwsgi.wsgi_req, ( (uint8_t *) uwsgi.wsgi_req - (uint8_t *) uwsgi.wsgi_requests)/(sizeof(struct wsgi_request)+(uwsgi.buffer_size-1))) ;
|
||||
wsgi_req_setup(uwsgi.wsgi_req, ( (uint8_t *)uwsgi.wsgi_req - (uint8_t *)uwsgi.wsgi_requests)/sizeof(struct wsgi_request) );
|
||||
|
||||
if (wsgi_req_accept(uwsgi.serverfd, uwsgi.wsgi_req)) {
|
||||
continue;
|
||||
@@ -1750,7 +1774,7 @@ int init_uwsgi_app(PyObject * force_wsgi_dict, PyObject * my_callable) {
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_environ = malloc(sizeof(PyObject*)*uwsgi.async);
|
||||
if (!wi->wsgi_environ) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
@@ -1838,7 +1862,7 @@ int init_uwsgi_app(PyObject * force_wsgi_dict, PyObject * my_callable) {
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_args = malloc(sizeof(PyObject*));
|
||||
if (!wi->wsgi_args) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
@@ -1865,7 +1889,7 @@ int init_uwsgi_app(PyObject * force_wsgi_dict, PyObject * my_callable) {
|
||||
#ifdef UWSGI_ASYNC
|
||||
wi->wsgi_args = malloc(sizeof(PyObject*)*uwsgi.async);
|
||||
if (!wi->wsgi_args) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
if (uwsgi.single_interpreter == 0) {
|
||||
Py_EndInterpreter(wi->interpreter);
|
||||
PyThreadState_Swap(uwsgi.main_thread) ;
|
||||
@@ -2002,7 +2026,7 @@ void uwsgi_wsgi_file_config() {
|
||||
|
||||
wsgifile = fopen(uwsgi.wsgi_file, "r");
|
||||
if (!wsgifile) {
|
||||
perror("fopen()");
|
||||
uwsgi_error("fopen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -2074,7 +2098,7 @@ void uwsgi_wsgi_config(char *filename) {
|
||||
if (filename) {
|
||||
uwsgifile = fopen(filename, "r");
|
||||
if (!uwsgifile) {
|
||||
perror("fopen()");
|
||||
uwsgi_error("fopen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -2397,7 +2421,7 @@ pid_t proxy_start(has_master) {
|
||||
else {
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("fork()");
|
||||
uwsgi_error("fork()");
|
||||
exit(1);
|
||||
}
|
||||
else if (pid > 0) {
|
||||
@@ -2420,7 +2444,7 @@ pid_t spooler_start(int serverfd, PyObject * uwsgi_module_dict) {
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("fork()");
|
||||
uwsgi_error("fork()");
|
||||
exit(1);
|
||||
}
|
||||
else if (pid == 0) {
|
||||
@@ -2547,15 +2571,15 @@ void manage_opt(int i, char *optarg) {
|
||||
case 'Q':
|
||||
uwsgi.spool_dir = malloc(PATH_MAX);
|
||||
if (!uwsgi.spool_dir) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
if (access(optarg, R_OK | W_OK | X_OK)) {
|
||||
perror("[spooler directory] access()");
|
||||
uwsgi_error("[spooler directory] access()");
|
||||
exit(1);
|
||||
}
|
||||
if (!realpath(optarg, uwsgi.spool_dir)) {
|
||||
perror("realpath()");
|
||||
uwsgi_error("realpath()");
|
||||
exit(1);
|
||||
}
|
||||
uwsgi.master_process = 1;
|
||||
@@ -2573,7 +2597,7 @@ void manage_opt(int i, char *optarg) {
|
||||
case 'C':
|
||||
tmp_dir_fd = open(optarg, O_DIRECTORY);
|
||||
if (tmp_dir_fd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
exit(1);
|
||||
}
|
||||
tmp_filename = malloc(8192);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#define UWSGI_VERSION "0.9.5-dev"
|
||||
|
||||
#define uwsgi_error(x) fprintf(stderr, "%s: %s [%s line %d]\n", x, strerror(errno), __FILE__, __LINE__);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
@@ -58,8 +60,9 @@
|
||||
#undef _POSIX_C_SOURCE
|
||||
#endif
|
||||
#ifdef __sun__
|
||||
#undef _FILE_OFFSET_BITS
|
||||
#define WAIT_ANY (-1)
|
||||
#include <sys/filio.h>
|
||||
#define PRIO_MAX 20
|
||||
#endif
|
||||
|
||||
#define MAX_PYARGV 10
|
||||
@@ -160,6 +163,10 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t);
|
||||
#ifdef __linux__
|
||||
#include <endian.h>
|
||||
#elif __sun__
|
||||
#include <sys/byteorder.h>
|
||||
#ifdef _BIG_ENDIAN
|
||||
#define __BIG_ENDIAN__ 1
|
||||
#endif
|
||||
#elif __apple__
|
||||
#include <libkern/OSByteOrder.h>
|
||||
#else
|
||||
@@ -337,8 +344,7 @@ struct wsgi_request {
|
||||
PyTaskletObject* tasklet;
|
||||
#endif
|
||||
|
||||
// buffer MUST BE THE LAST VAR !!!
|
||||
char buffer;
|
||||
char *buffer;
|
||||
};
|
||||
|
||||
struct uwsgi_server {
|
||||
@@ -369,6 +375,7 @@ struct uwsgi_server {
|
||||
#endif
|
||||
|
||||
struct iovec *async_hvec;
|
||||
char **async_buf;
|
||||
|
||||
struct rlimit rl;
|
||||
|
||||
@@ -717,7 +724,7 @@ struct wsgi_request *find_first_available_wsgi_req(struct uwsgi_server *);
|
||||
struct wsgi_request *find_wsgi_req_by_fd(struct uwsgi_server *, int, int);
|
||||
struct wsgi_request *find_wsgi_req_by_id(struct uwsgi_server *, int);
|
||||
|
||||
struct wsgi_request *next_wsgi_req(struct uwsgi_server *, struct wsgi_request *);
|
||||
inline struct wsgi_request *next_wsgi_req(struct uwsgi_server *, struct wsgi_request *);
|
||||
|
||||
int async_add(int, int , int) ;
|
||||
int async_mod(int, int , int) ;
|
||||
|
||||
+6
-6
@@ -14,13 +14,13 @@ int uwsgi_request_ping(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req
|
||||
wsgi_req->uh.pktsize = len;
|
||||
}
|
||||
if (write(wsgi_req->poll.fd, wsgi_req, 4) != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
|
||||
if (len > 0) {
|
||||
if (write(wsgi_req->poll.fd, uwsgi->shared->warning_message, len)
|
||||
!= len) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ int uwsgi_request_admin(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_re
|
||||
wsgi_req->uh.modifier2 = 1;
|
||||
i = write(wsgi_req->poll.fd, wsgi_req, 4);
|
||||
if (i != 4) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
|
||||
return UWSGI_OK;
|
||||
@@ -76,7 +76,7 @@ int uwsgi_request_marshal(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_
|
||||
PyObject *umm = PyDict_GetItemString(uwsgi->embedded_dict,
|
||||
"message_manager_marshal");
|
||||
if (umm) {
|
||||
PyObject *ummo = PyMarshal_ReadObjectFromString(&wsgi_req->buffer,
|
||||
PyObject *ummo = PyMarshal_ReadObjectFromString(wsgi_req->buffer,
|
||||
wsgi_req->uh.pktsize);
|
||||
if (ummo) {
|
||||
if (!PyTuple_SetItem(uwsgi->embedded_args, 0, ummo)) {
|
||||
@@ -96,11 +96,11 @@ int uwsgi_request_marshal(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_
|
||||
PyString_Size(marshalled);
|
||||
if (write(wsgi_req->poll.fd, wsgi_req, 4) == 4) {
|
||||
if (write(wsgi_req->poll.fd, PyString_AsString(marshalled), wsgi_req->uh.pktsize) != wsgi_req->uh.pktsize) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
}
|
||||
else {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
+8
-8
@@ -9,12 +9,12 @@ extern struct uwsgi_server uwsgi;
|
||||
#ifdef __APPLE__
|
||||
#define UWSGI_LOCK OSSpinLockLock((OSSpinLock *) uwsgi.sharedareamutex);
|
||||
#define UWSGI_UNLOCK OSSpinLockUnlock((OSSpinLock *) uwsgi.sharedareamutex);
|
||||
#elif defined(__linux__)
|
||||
#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__)
|
||||
#define UWSGI_LOCK pthread_mutex_lock((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
#define UWSGI_UNLOCK pthread_mutex_unlock((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
#else
|
||||
#define UWSGI_LOCK if (flock(uwsgi.serverfd, LOCK_EX)) { perror("flock()"); }
|
||||
#define UWSGI_UNLOCK if (flock(uwsgi.serverfd, LOCK_UN)) { perror("flock()"); }
|
||||
#define UWSGI_LOCK if (flock(uwsgi.serverfd, LOCK_EX)) { uwsgi_error("flock()"); }
|
||||
#define UWSGI_UNLOCK if (flock(uwsgi.serverfd, LOCK_UN)) { uwsgi_error("flock()"); }
|
||||
#endif
|
||||
|
||||
#define UWSGI_LOGBASE "[- uWSGI -"
|
||||
@@ -430,7 +430,7 @@ PyObject *py_uwsgi_send_multi_message(PyObject * self, PyObject * args) {
|
||||
clen = PyTuple_Size(arg_cluster);
|
||||
multipoll = malloc(clen * sizeof(struct pollfd));
|
||||
if (!multipoll) {
|
||||
perror("malloc");
|
||||
uwsgi_error("malloc");
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
@@ -438,7 +438,7 @@ PyObject *py_uwsgi_send_multi_message(PyObject * self, PyObject * args) {
|
||||
|
||||
buffer = malloc(uwsgi.buffer_size * clen);
|
||||
if (!buffer) {
|
||||
perror("malloc");
|
||||
uwsgi_error("malloc");
|
||||
free(multipoll);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
@@ -493,7 +493,7 @@ PyObject *py_uwsgi_send_multi_message(PyObject * self, PyObject * args) {
|
||||
while (managed < clen) {
|
||||
pret = poll(multipoll, clen, PyInt_AsLong(arg_timeout) * 1000);
|
||||
if (pret < 0) {
|
||||
perror("poll()");
|
||||
uwsgi_error("poll()");
|
||||
goto megamulticlear;
|
||||
}
|
||||
else if (pret == 0) {
|
||||
@@ -797,7 +797,7 @@ PyObject *py_uwsgi_workers(PyObject * self, PyObject * args) {
|
||||
PyObject *py_uwsgi_reload(PyObject * self, PyObject * args) {
|
||||
|
||||
if (kill(uwsgi.workers[0].pid, SIGHUP)) {
|
||||
perror("kill()");
|
||||
uwsgi_error("kill()");
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
@@ -906,7 +906,7 @@ void init_uwsgi_module_spooler(PyObject * current_uwsgi_module) {
|
||||
|
||||
spool_buffer = malloc(uwsgi.buffer_size);
|
||||
if (!spool_buffer) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -144,10 +144,11 @@ def parse_vars():
|
||||
if str(PYLIB_PATH) != '':
|
||||
ldflags.insert(0,'-L' + PYLIB_PATH)
|
||||
|
||||
kvm_list = ['SunOS', 'FreeBSD', 'OpenBSD', 'NetBSD', 'DragonFly']
|
||||
kvm_list = ['FreeBSD', 'OpenBSD', 'NetBSD', 'DragonFly']
|
||||
|
||||
if uwsgi_os == 'SunOS':
|
||||
ldflags.append('-lsendfile')
|
||||
ldflags.remove('-rdynamic')
|
||||
|
||||
if uwsgi_os in kvm_list:
|
||||
ldflags.append('-lkvm')
|
||||
|
||||
+1
-1
@@ -151,7 +151,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) {
|
||||
|
||||
wsgi_req->headers_size = writev(wsgi_req->poll.fd, wsgi_req->hvec, j + 1);
|
||||
if (wsgi_req->headers_size < 0) {
|
||||
perror("writev()");
|
||||
uwsgi_error("writev()");
|
||||
}
|
||||
|
||||
Py_INCREF(uwsgi.wsgi_writeout);
|
||||
|
||||
@@ -249,24 +249,24 @@ void uwsgi_xml_config(struct wsgi_request *wsgi_req, struct option *long_options
|
||||
|
||||
xmlfd = open(uwsgi.xml_config, O_RDONLY);
|
||||
if (xmlfd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (fstat(xmlfd, &stat_buf)) {
|
||||
perror("fstat()");
|
||||
uwsgi_error("fstat()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
xmlbuf = malloc(stat_buf.st_size);
|
||||
if (!xmlbuf) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
rlen = read(xmlfd, xmlbuf, stat_buf.st_size);
|
||||
if (rlen != stat_buf.st_size) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
exit(1);
|
||||
}
|
||||
close(xmlfd);
|
||||
|
||||
Reference in New Issue
Block a user