1 #include "uwsgi.h" 2 3 extern struct uwsgi_server uwsgi; 4 5 uwsgi_setup_systemd()6void uwsgi_setup_systemd() { 7 struct uwsgi_socket *uwsgi_sock = NULL; 8 int i; 9 10 char *listen_pid = getenv("LISTEN_PID"); 11 if (listen_pid) { 12 if (atoi(listen_pid) == (int) getpid()) { 13 char *listen_fds = getenv("LISTEN_FDS"); 14 if (listen_fds) { 15 int systemd_fds = atoi(listen_fds); 16 if (systemd_fds > 0) { 17 uwsgi_log("- SystemD socket activation detected -\n"); 18 for (i = 3; i < 3 + systemd_fds; i++) { 19 uwsgi_sock = uwsgi_new_socket(NULL); 20 uwsgi_add_socket_from_fd(uwsgi_sock, i); 21 } 22 uwsgi.skip_zero = 1; 23 } 24 unsetenv("LISTEN_PID"); 25 unsetenv("LISTEN_FDS"); 26 } 27 } 28 } 29 30 } 31 uwsgi_setup_upstart()32void uwsgi_setup_upstart() { 33 34 struct uwsgi_socket *uwsgi_sock = NULL; 35 36 char *upstart_events = getenv("UPSTART_EVENTS"); 37 if (upstart_events && !strcmp(upstart_events, "socket")) { 38 char *upstart_fds = getenv("UPSTART_FDS"); 39 if (upstart_fds) { 40 uwsgi_log("- Upstart socket bridge detected (job: %s) -\n", getenv("UPSTART_JOB")); 41 uwsgi_sock = uwsgi_new_socket(NULL); 42 uwsgi_add_socket_from_fd(uwsgi_sock, atoi(upstart_fds)); 43 uwsgi.skip_zero = 1; 44 } 45 unsetenv("UPSTART_EVENTS"); 46 unsetenv("UPSTART_FDS"); 47 } 48 49 } 50 51 uwsgi_setup_zerg()52void uwsgi_setup_zerg() { 53 54 struct uwsgi_socket *uwsgi_sock = NULL; 55 int i; 56 57 struct uwsgi_string_list *zn = uwsgi.zerg_node; 58 while (zn) { 59 if (uwsgi_zerg_attach(zn->value)) { 60 if (!uwsgi.zerg_fallback) { 61 exit(1); 62 } 63 } 64 zn = zn->next; 65 } 66 67 68 69 if (uwsgi.zerg) { 70 #ifdef UWSGI_DEBUG 71 uwsgi_log("attaching zerg sockets...\n"); 72 #endif 73 int zerg_fd; 74 i = 0; 75 for (;;) { 76 zerg_fd = uwsgi.zerg[i]; 77 if (zerg_fd == -1) { 78 break; 79 } 80 uwsgi_sock = uwsgi_new_socket(NULL); 81 uwsgi_add_socket_from_fd(uwsgi_sock, zerg_fd); 82 i++; 83 } 84 85 uwsgi_log("zerg sockets attached\n"); 86 } 87 } 88 uwsgi_setup_inherited_sockets()89void uwsgi_setup_inherited_sockets() { 90 91 int j; 92 union uwsgi_sockaddr usa; 93 union uwsgi_sockaddr_ptr gsa; 94 95 struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; 96 while (uwsgi_sock) { 97 //a bit overengineering 98 if (uwsgi_sock->name[0] != 0 && !uwsgi_sock->bound) { 99 for (j = 3; j < (int) uwsgi.max_fd; j++) { 100 uwsgi_add_socket_from_fd(uwsgi_sock, j); 101 } 102 } 103 uwsgi_sock = uwsgi_sock->next; 104 } 105 106 //now close all the unbound fd 107 for (j = 3; j < (int) uwsgi.max_fd; j++) { 108 int useless = 1; 109 110 if (uwsgi_fd_is_safe(j)) continue; 111 112 if (uwsgi.has_emperor) { 113 if (j == uwsgi.emperor_fd) 114 continue; 115 if (j == uwsgi.emperor_fd_config) 116 continue; 117 } 118 119 if (uwsgi.shared->worker_log_pipe[0] > -1) { 120 if (j == uwsgi.shared->worker_log_pipe[0]) 121 continue; 122 } 123 124 if (uwsgi.shared->worker_log_pipe[1] > -1) { 125 if (j == uwsgi.shared->worker_log_pipe[1]) 126 continue; 127 } 128 129 if (uwsgi.shared->worker_req_log_pipe[0] > -1) { 130 if (j == uwsgi.shared->worker_req_log_pipe[0]) 131 continue; 132 } 133 134 if (uwsgi.shared->worker_req_log_pipe[1] > -1) { 135 if (j == uwsgi.shared->worker_req_log_pipe[1]) 136 continue; 137 } 138 139 if (uwsgi.original_log_fd > -1) { 140 if (j == uwsgi.original_log_fd) 141 continue; 142 } 143 144 struct uwsgi_gateway_socket *ugs = uwsgi.gateway_sockets; 145 int found = 0; 146 while (ugs) { 147 if (ugs->fd == j) { 148 found = 1; 149 break; 150 } 151 ugs = ugs->next; 152 } 153 if (found) 154 continue; 155 156 157 int y; 158 found = 0; 159 for (y = 0; y < ushared->gateways_cnt; y++) { 160 if (ushared->gateways[y].internal_subscription_pipe[0] == j) { 161 found = 1; 162 break; 163 } 164 if (ushared->gateways[y].internal_subscription_pipe[1] == j) { 165 found = 1; 166 break; 167 } 168 } 169 170 if (found) 171 continue; 172 173 174 socklen_t socket_type_len = sizeof(struct sockaddr_un); 175 gsa.sa = (struct sockaddr *) &usa; 176 if (!getsockname(j, gsa.sa, &socket_type_len)) { 177 uwsgi_sock = uwsgi.sockets; 178 while (uwsgi_sock) { 179 if (uwsgi_sock->fd == j && uwsgi_sock->bound) { 180 useless = 0; 181 break; 182 } 183 uwsgi_sock = uwsgi_sock->next; 184 } 185 186 if (useless) { 187 uwsgi_sock = uwsgi.shared_sockets; 188 while (uwsgi_sock) { 189 if (uwsgi_sock->fd == j && uwsgi_sock->bound) { 190 useless = 0; 191 break; 192 } 193 uwsgi_sock = uwsgi_sock->next; 194 } 195 } 196 } 197 198 if (useless) { 199 close(j); 200 } 201 } 202 203 } 204