void uwsgi_reload(char **argv) { int i; int waitpid_status; // call a series of waitpid to ensure all processes (gateways, mules and daemons) are dead for (i = 0; i < (ushared->gateways_cnt + uwsgi.daemons_cnt + uwsgi.mules_cnt); i++) { waitpid(WAIT_ANY, &waitpid_status, WNOHANG); } // call master cleanup hooks uwsgi_master_cleanup_hooks(); // call atexit user exec uwsgi_exec_atexit(); if (uwsgi.exit_on_reload) { uwsgi_log("uWSGI: GAME OVER (insert coin)\n"); exit(0); } uwsgi_log("binary reloading uWSGI...\n"); uwsgi_log("chdir() to %s\n", uwsgi.cwd); if (chdir(uwsgi.cwd)) { uwsgi_error("chdir()"); } /* check fd table (a module can obviosly open some fd on initialization...) */ uwsgi_log("closing all non-uwsgi socket fds > 2 (max_fd = %d)...\n", (int) uwsgi.max_fd); for (i = 3; i < (int) uwsgi.max_fd; i++) { if (uwsgi_fd_is_safe(i)) continue; int found = 0; struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; while (uwsgi_sock) { if (i == uwsgi_sock->fd) { uwsgi_log("found fd %d mapped to socket %d (%s)\n", i, uwsgi_get_socket_num(uwsgi_sock), uwsgi_sock->name); found = 1; break; } uwsgi_sock = uwsgi_sock->next; } uwsgi_sock = uwsgi.shared_sockets; while (uwsgi_sock) { if (i == uwsgi_sock->fd) { uwsgi_log("found fd %d mapped to shared socket %d (%s)\n", i, uwsgi_get_shared_socket_num(uwsgi_sock), uwsgi_sock->name); found = 1; break; } uwsgi_sock = uwsgi_sock->next; } if (found) continue; if (uwsgi.has_emperor) { if (i == uwsgi.emperor_fd) { continue; } if (i == uwsgi.emperor_fd_config) { continue; } } if (uwsgi.alarm_thread) { if (i == uwsgi.alarm_thread->queue) continue; if (i == uwsgi.alarm_thread->pipe[0]) continue; if (i == uwsgi.alarm_thread->pipe[1]) continue; } if (uwsgi.log_master) { if (uwsgi.original_log_fd > -1) { if (i == uwsgi.original_log_fd) { continue; } } if (uwsgi.shared->worker_log_pipe[0] > -1) { if (i == uwsgi.shared->worker_log_pipe[0]) { continue; } } if (uwsgi.shared->worker_log_pipe[1] > -1) { if (i == uwsgi.shared->worker_log_pipe[1]) { continue; } } } #ifdef __APPLE__ fcntl(i, F_SETFD, FD_CLOEXEC); #else close(i); #endif } #ifndef UWSGI_IPCSEM_ATEXIT // free ipc semaphores if in use if (uwsgi.lock_engine && !strcmp(uwsgi.lock_engine, "ipcsem")) { uwsgi_ipcsem_clear(); } #endif uwsgi_log("running %s\n", uwsgi.binary_path); uwsgi_flush_logs(); argv[0] = uwsgi.binary_path; //strcpy (argv[0], uwsgi.binary_path); if (uwsgi.log_master) { if (uwsgi.original_log_fd > -1) { dup2(uwsgi.original_log_fd, 1); dup2(1, 2); } if (uwsgi.shared->worker_log_pipe[0] > -1) { close(uwsgi.shared->worker_log_pipe[0]); } if (uwsgi.shared->worker_log_pipe[1] > -1) { close(uwsgi.shared->worker_log_pipe[1]); } } execvp(uwsgi.binary_path, argv); uwsgi_error("execvp()"); // never here exit(1); }
void uwsgi_setup_shared_sockets() { int i; struct uwsgi_socket *shared_sock = uwsgi.shared_sockets; while (shared_sock) { if (!uwsgi.is_a_reload) { char *tcp_port = strrchr(shared_sock->name, ':'); int current_defer_accept = uwsgi.no_defer_accept; if (shared_sock->no_defer) { uwsgi.no_defer_accept = 1; } if (tcp_port == NULL) { shared_sock->fd = bind_to_unix(shared_sock->name, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket); shared_sock->family = AF_UNIX; uwsgi_log("uwsgi shared socket %d bound to UNIX address %s fd %d\n", uwsgi_get_shared_socket_num(shared_sock), shared_sock->name, shared_sock->fd); } else { #ifdef UWSGI_IPV6 if (shared_sock->name[0] == '[' && tcp_port[-1] == ']') { shared_sock->fd = bind_to_tcp6(shared_sock->name, uwsgi.listen_queue, tcp_port); shared_sock->family = AF_INET6; // fix socket name shared_sock->name = uwsgi_getsockname(shared_sock->fd); uwsgi_log("uwsgi shared socket %d bound to TCP6 address %s fd %d\n", uwsgi_get_shared_socket_num(shared_sock), shared_sock->name, shared_sock->fd); } else { #endif shared_sock->fd = bind_to_tcp(shared_sock->name, uwsgi.listen_queue, tcp_port); shared_sock->family = AF_INET; // fix socket name shared_sock->name = uwsgi_getsockname(shared_sock->fd); uwsgi_log("uwsgi shared socket %d bound to TCP address %s fd %d\n", uwsgi_get_shared_socket_num(shared_sock), shared_sock->name, shared_sock->fd); #ifdef UWSGI_IPV6 } #endif } if (shared_sock->fd < 0) { uwsgi_log("unable to create shared socket on: %s\n", shared_sock->name); exit(1); } if (shared_sock->no_defer) { uwsgi.no_defer_accept = current_defer_accept; } } else { for (i = 3; i < (int) uwsgi.max_fd; i++) { char *sock = uwsgi_getsockname(i); if (sock) { if (!strcmp(sock, shared_sock->name)) { if (strchr(sock, ':')) { uwsgi_log("uwsgi shared socket %d inherited TCP address %s fd %d\n", uwsgi_get_shared_socket_num(shared_sock), sock, i); shared_sock->family = AF_INET; } else { uwsgi_log("uwsgi shared socket %d inherited UNIX address %s fd %d\n", uwsgi_get_shared_socket_num(shared_sock), sock, i); shared_sock->family = AF_UNIX; } shared_sock->fd = i; } else { free(sock); } } } } shared_sock->bound = 1; shared_sock = shared_sock->next; } struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; while (uwsgi_sock) { if (uwsgi_sock->shared) { shared_sock = uwsgi_get_shared_socket_by_num(uwsgi_sock->from_shared); if (!shared_sock) { uwsgi_log("unable to find shared socket %d\n", uwsgi_sock->from_shared); exit(1); } uwsgi_sock->fd = shared_sock->fd; uwsgi_sock->family = shared_sock->family; uwsgi_sock->name = shared_sock->name; uwsgi_log("uwsgi socket %d mapped to shared socket %d (%s) fd %d\n", uwsgi_get_socket_num(uwsgi_sock), uwsgi_get_shared_socket_num(shared_sock), shared_sock->name, uwsgi_sock->fd); } uwsgi_sock = uwsgi_sock->next; } }