From 0441e88b82ad7f9b6f9eff3cbca55f7ea72f404e Mon Sep 17 00:00:00 2001 From: Unbit Date: Fri, 23 Aug 2013 14:28:01 +0200 Subject: [PATCH] track unix sockets inode to reduce accidental removal on vacuum --- core/socket.c | 4 ++++ core/utils.c | 2 ++ core/uwsgi.c | 9 +++++++++ uwsgi.h | 4 ++++ 4 files changed, 19 insertions(+) diff --git a/core/socket.c b/core/socket.c index dee17d2e..a1a3f12d 100644 --- a/core/socket.c +++ b/core/socket.c @@ -1714,6 +1714,10 @@ void uwsgi_bind_sockets() { uwsgi_chown(uwsgi_sock->name, uwsgi.chown_socket); } uwsgi_log("uwsgi socket %d bound to UNIX address %s fd %d\n", uwsgi_get_socket_num(uwsgi_sock), uwsgi_sock->name, uwsgi_sock->fd); + struct stat st; + if (uwsgi_sock->name[0] != '@' && !stat(uwsgi_sock->name, &st)) { + uwsgi_sock->inode = st.st_ino; + } } else { #ifdef AF_INET6 diff --git a/core/utils.c b/core/utils.c index 081f770d..cebe2e4b 100644 --- a/core/utils.c +++ b/core/utils.c @@ -191,6 +191,8 @@ void daemonize(char *logfile) { // get current working directory char *uwsgi_get_cwd() { + if (uwsgi.force_cwd) return uwsgi.force_cwd; + // set this to static to avoid useless reallocations in stats mode static size_t newsize = 256; diff --git a/core/uwsgi.c b/core/uwsgi.c index c7899291..72be38e9 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -526,6 +526,7 @@ static struct uwsgi_option uwsgi_base_options[] = { {"log-micros", no_argument, 0, "report response time in microseconds instead of milliseconds", uwsgi_opt_true, &uwsgi.log_micros, 0}, {"log-x-forwarded-for", no_argument, 0, "use the ip from X-Forwarded-For header instead of REMOTE_ADDR", uwsgi_opt_true, &uwsgi.log_x_forwarded_for, 0}, {"master-as-root", no_argument, 0, "leave master process running as root", uwsgi_opt_true, &uwsgi.master_as_root, 0}, + {"force-cwd", required_argument, 0, "force the initial working directory to the specified value", uwsgi_opt_set_str, &uwsgi.force_cwd, 0}, {"chdir", required_argument, 0, "chdir to specified directory before apps loading", uwsgi_opt_set_str, &uwsgi.chdir, 0}, {"chdir2", required_argument, 0, "chdir to specified directory after apps loading", uwsgi_opt_set_str, &uwsgi.chdir2, 0}, {"lazy", no_argument, 0, "set lazy mode (load apps in workers instead of master)", uwsgi_opt_true, &uwsgi.lazy, 0}, @@ -1392,6 +1393,13 @@ static void vacuum(void) { } while (uwsgi_sock) { if (uwsgi_sock->family == AF_UNIX && uwsgi_sock->name[0] != '@') { + struct stat st; + if (!stat(uwsgi_sock->name, &st)) { + if (st.st_ino != uwsgi_sock->inode) { + uwsgi_log("VACUUM WARNING: unix socket %s changed inode. Skip removal\n", uwsgi_sock->name); + goto next; + } + } if (unlink(uwsgi_sock->name)) { uwsgi_error("unlink()"); } @@ -1399,6 +1407,7 @@ static void vacuum(void) { uwsgi_log("VACUUM: unix socket %s removed.\n", uwsgi_sock->name); } } +next: uwsgi_sock = uwsgi_sock->next; } } diff --git a/uwsgi.h b/uwsgi.h index ff02f0a5..ed684559 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -929,6 +929,9 @@ struct uwsgi_socket { int lazy; int shared; int from_shared; + + // used for avoiding vacuum mess + ino_t inode; }; struct uwsgi_server; @@ -2203,6 +2206,7 @@ struct uwsgi_server { int skip_zero; int skip_atexit; + char *force_cwd; char *chdir; char *chdir2;