From d79eeefe978eb8426c94ac98d043a409869aa75b Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 23 Dec 2014 15:39:19 +0100 Subject: [PATCH 1/8] Write pidfile and pidfile2 only after successful start --- core/utils.c | 7 +++++++ core/uwsgi.c | 15 +++++++-------- uwsgi.h | 1 + 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/core/utils.c b/core/utils.c index d6c749b3..5ca81bfc 100755 --- a/core/utils.c +++ b/core/utils.c @@ -3587,6 +3587,13 @@ void uwsgi_write_pidfile(char *pidfile_name) { } } +void uwsgi_write_pidfile_explicit(char *pidfile_name, pid_t pid) { + uwsgi_log("writing pidfile to %s\n", pidfile_name); + if (uwsgi_write_intfile(pidfile_name, (int) pid)) { + uwsgi_log("could not write pidfile.\n"); + } +} + char *uwsgi_expand_path(char *dir, int dir_len, char *ptr) { char src[PATH_MAX + 1]; memcpy(src, dir, dir_len); diff --git a/core/uwsgi.c b/core/uwsgi.c index 3ec74882..99bde3e3 100755 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -2420,10 +2420,6 @@ configure: uwsgi_log("*** running under screen session %s ***\n", uwsgi.screen_session); } - if (uwsgi.pidfile && !uwsgi.is_a_reload) { - uwsgi_write_pidfile(uwsgi.pidfile); - } - uwsgi_log_initial("detected binary path: %s\n", uwsgi.binary_path); if (uwsgi.is_a_reload) { @@ -2523,6 +2519,10 @@ configure: #if defined(__linux__) && !defined(__ia64__) } #endif + + if (uwsgi.pidfile && !uwsgi.is_a_reload) { + uwsgi_write_pidfile_explicit(uwsgi.pidfile, masterpid); + } } @@ -2595,10 +2595,6 @@ int uwsgi_start(void *v_argv) { } } - if (uwsgi.pidfile2 && !uwsgi.is_a_reload) { - uwsgi_write_pidfile(uwsgi.pidfile2); - } - if (!uwsgi.master_process && !uwsgi.command_mode) { uwsgi_log_initial("*** WARNING: you are running uWSGI without its master process manager ***\n"); } @@ -3258,6 +3254,9 @@ next2: } } + if (uwsgi.pidfile2 && !uwsgi.is_a_reload) { + uwsgi_write_pidfile_explicit(uwsgi.pidfile2, masterpid); + } // END OF INITIALIZATION return 0; diff --git a/uwsgi.h b/uwsgi.h index 96278587..cce402cd 100755 --- a/uwsgi.h +++ b/uwsgi.h @@ -3958,6 +3958,7 @@ void uwsgi_setup_post_buffering(void); struct uwsgi_lock_item *uwsgi_lock_ipcsem_init(char *); void uwsgi_write_pidfile(char *); +void uwsgi_write_pidfile_explicit(char *, pid_t); int uwsgi_write_intfile(char *, int); void uwsgi_protected_close(int); From 7a057f144ad1bcb8622ddc628c085f04b41cc356 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 23 Dec 2014 17:04:16 +0100 Subject: [PATCH 2/8] Fix inconsistent use of tabs and spaces in indentation --- contrib/runuwsgi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/runuwsgi.py b/contrib/runuwsgi.py index 88429605..f6164b0a 100644 --- a/contrib/runuwsgi.py +++ b/contrib/runuwsgi.py @@ -40,8 +40,8 @@ class Command(BaseCommand): elif self.socket_addr: os.environ['UWSGI_SOCKET'] = self.socket_addr - # map admin static files - os.environ['UWSGI_STATIC_MAP'] = '%s=%s' % (settings.ADMIN_MEDIA_PREFIX, os.path.join(django.__path__[0], 'contrib', 'admin', 'media')) + # map admin static files + os.environ['UWSGI_STATIC_MAP'] = '%s=%s' % (settings.ADMIN_MEDIA_PREFIX, os.path.join(django.__path__[0], 'contrib', 'admin', 'media')) # remove sockets/pidfile at exit os.environ['UWSGI_VACUUM'] = '1' # retrieve/set the PythonHome From 40fa1579e6d305f7a0e827806a92d26dfc34d3b9 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 23 Dec 2014 17:06:17 +0100 Subject: [PATCH 3/8] Fix inconsistent use of tabs and spaces in indentation --- contrib/twuwsgi.py | 66 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/contrib/twuwsgi.py b/contrib/twuwsgi.py index a20400c0..ceb27817 100644 --- a/contrib/twuwsgi.py +++ b/contrib/twuwsgi.py @@ -12,7 +12,7 @@ class uWSGIClientResource(resource.LeafResource): resource.LeafResource.__init__(self) self.host = host self.port = port - self.app = app + self.app = app def renderHTTP(self, request): return uWSGI(request, self.app, self.host, self.port) @@ -33,52 +33,52 @@ class uWSGIClientProtocol(basic.LineReceiver): self.deferred = deferred self.stream = stream.ProducerStream() self.response = http.Response(stream=self.stream) - self.status_parsed = None + self.status_parsed = None def build_uwsgi_var(self,key,value): - return struct.pack(' Date: Tue, 23 Dec 2014 17:17:28 +0100 Subject: [PATCH 4/8] Python 3 compatibility --- contrib/twuwsgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/twuwsgi.py b/contrib/twuwsgi.py index ceb27817..e606c98f 100644 --- a/contrib/twuwsgi.py +++ b/contrib/twuwsgi.py @@ -39,7 +39,7 @@ class uWSGIClientProtocol(basic.LineReceiver): return struct.pack(' Date: Tue, 23 Dec 2014 17:34:02 +0100 Subject: [PATCH 5/8] Python 3 compatibility --- contrib/uwsgi-cache-monitor.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/contrib/uwsgi-cache-monitor.py b/contrib/uwsgi-cache-monitor.py index d76ad7d0..34b71fd8 100644 --- a/contrib/uwsgi-cache-monitor.py +++ b/contrib/uwsgi-cache-monitor.py @@ -85,15 +85,15 @@ class Cache: def show_dump(self): d = self.dump() - print - print "Recorded %d samples (%d second(s) sleep between samples)" % \ - (d['samples'], d['sample_sleep']) - print "Cache empty %d times, full %d times, %.2f items on average" % \ - (d['cache_empty'], d['cache_full'], d['cache_items'] / d['samples']) - print "Block size average size: %d bytes" % \ - (d['block_sizes'] / d['cache_items'] * 8) - print "Data in cache average: %d bytes" % \ - (d['block_sizes'] / d['samples'] * 8) + print() + print("Recorded %d samples (%d second(s) sleep between samples)" % \ + (d['samples'], d['sample_sleep'])) + print("Cache empty %d times, full %d times, %.2f items on average" % \ + (d['cache_empty'], d['cache_full'], d['cache_items'] / d['samples'])) + print("Block size average size: %d bytes" % \ + (d['block_sizes'] / d['cache_items'] * 8)) + print("Data in cache average: %d bytes" % \ + (d['block_sizes'] / d['samples'] * 8)) def main(options): cache = Cache(options.cache_store, options.cache_slots, options.block_size, From 789f32fcb619d4182efd98f90bd159d12232b037 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 23 Dec 2014 17:38:01 +0100 Subject: [PATCH 6/8] Python 3 compatibility --- contrib/uwsgi-cache-monitor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/uwsgi-cache-monitor.py b/contrib/uwsgi-cache-monitor.py index 34b71fd8..58402ca9 100644 --- a/contrib/uwsgi-cache-monitor.py +++ b/contrib/uwsgi-cache-monitor.py @@ -98,7 +98,7 @@ class Cache: def main(options): cache = Cache(options.cache_store, options.cache_slots, options.block_size, options.sleep_time) - print "Recording..." + print("Recording...") while True: try: data = cache.read() From 54ff63e2803bd5ec4dce65b549f1e2fba2b18732 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 23 Dec 2014 19:13:51 +0100 Subject: [PATCH 7/8] Implement safe-pidfile and safe-pidfile2 --- core/uwsgi.c | 40 ++++++++++++++++++++++++++++++++++++---- uwsgi.h | 3 +++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/core/uwsgi.c b/core/uwsgi.c index 99bde3e3..d346a5ca 100755 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -354,6 +354,8 @@ static struct uwsgi_option uwsgi_base_options[] = { {"pidfile", required_argument, 0, "create pidfile (before privileges drop)", uwsgi_opt_set_str, &uwsgi.pidfile, 0}, {"pidfile2", required_argument, 0, "create pidfile (after privileges drop)", uwsgi_opt_set_str, &uwsgi.pidfile2, 0}, + {"safe-pidfile", required_argument, 0, "create safe pidfile (before privileges drop)", uwsgi_opt_set_str, &uwsgi.safe_pidfile, 0}, + {"safe-pidfile2", required_argument, 0, "create safe pidfile (after privileges drop)", uwsgi_opt_set_str, &uwsgi.safe_pidfile2, 0}, {"chroot", required_argument, 0, "chroot() to the specified directory", uwsgi_opt_set_str, &uwsgi.chroot, 0}, #ifdef __linux__ {"pivot-root", required_argument, 0, "pivot_root() to the specified directories (new_root and put_old must be separated with a space)", uwsgi_opt_set_str, &uwsgi.pivot_root, 0}, @@ -1604,6 +1606,22 @@ static void vacuum(void) { uwsgi_log("VACUUM: pidfile2 removed.\n"); } } + if (uwsgi.safe_pidfile && !uwsgi.uid) { + if (unlink(uwsgi.safe_pidfile)) { + uwsgi_error("unlink()"); + } + else { + uwsgi_log("VACUUM: safe pidfile removed.\n"); + } + } + if (uwsgi.safe_pidfile2) { + if (unlink(uwsgi.safe_pidfile2)) { + uwsgi_error("unlink()"); + } + else { + uwsgi_log("VACUUM: safe pidfile2 removed.\n"); + } + } if (uwsgi.chdir) { if (chdir(uwsgi.chdir)) { uwsgi_error("chdir()"); @@ -2420,6 +2438,10 @@ configure: uwsgi_log("*** running under screen session %s ***\n", uwsgi.screen_session); } + if (uwsgi.pidfile && !uwsgi.is_a_reload) { + uwsgi_write_pidfile(uwsgi.pidfile); + } + uwsgi_log_initial("detected binary path: %s\n", uwsgi.binary_path); if (uwsgi.is_a_reload) { @@ -2520,8 +2542,8 @@ configure: } #endif - if (uwsgi.pidfile && !uwsgi.is_a_reload) { - uwsgi_write_pidfile_explicit(uwsgi.pidfile, masterpid); + if (uwsgi.safe_pidfile && !uwsgi.is_a_reload) { + uwsgi_write_pidfile_explicit(uwsgi.safe_pidfile, masterpid); } } @@ -2595,6 +2617,10 @@ int uwsgi_start(void *v_argv) { } } + if (uwsgi.pidfile2 && !uwsgi.is_a_reload) { + uwsgi_write_pidfile(uwsgi.pidfile2); + } + if (!uwsgi.master_process && !uwsgi.command_mode) { uwsgi_log_initial("*** WARNING: you are running uWSGI without its master process manager ***\n"); } @@ -3254,8 +3280,8 @@ next2: } } - if (uwsgi.pidfile2 && !uwsgi.is_a_reload) { - uwsgi_write_pidfile_explicit(uwsgi.pidfile2, masterpid); + if (uwsgi.safe_pidfile2 && !uwsgi.is_a_reload) { + uwsgi_write_pidfile_explicit(uwsgi.safe_pidfile2, masterpid); } // END OF INITIALIZATION @@ -4892,6 +4918,12 @@ void uwsgi_update_pidfiles() { if (uwsgi.pidfile2) { uwsgi_write_pidfile(uwsgi.pidfile2); } + if (uwsgi.safe_pidfile) { + uwsgi_write_pidfile(uwsgi.safe_pidfile); + } + if (uwsgi.safe_pidfile2) { + uwsgi_write_pidfile(uwsgi.safe_pidfile2); + } } void uwsgi_opt_binary_append_data(char *opt, char *value, void *none) { diff --git a/uwsgi.h b/uwsgi.h index cce402cd..3da28b77 100755 --- a/uwsgi.h +++ b/uwsgi.h @@ -2408,6 +2408,9 @@ struct uwsgi_server { char *pidfile; char *pidfile2; + char *safe_pidfile; + char *safe_pidfile2; + char *flock2; char *flock_wait2; From 7eebcf3246a8e0eb4ebd93175740dc2bf15f83a6 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 23 Dec 2014 19:32:33 +0100 Subject: [PATCH 8/8] Move safe_pidfile and safe_pidfile2 at the end of uwsgi_server struct --- uwsgi.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/uwsgi.h b/uwsgi.h index 3da28b77..899b2d94 100755 --- a/uwsgi.h +++ b/uwsgi.h @@ -2408,9 +2408,6 @@ struct uwsgi_server { char *pidfile; char *pidfile2; - char *safe_pidfile; - char *safe_pidfile2; - char *flock2; char *flock_wait2; @@ -2789,6 +2786,8 @@ struct uwsgi_server { char *zeus; uint64_t buffer_size; int emperor_tyrant_initgroups; + char *safe_pidfile; + char *safe_pidfile2; }; struct uwsgi_rpc {