File: setup_utils.c

package info (click to toggle)
uwsgi 2.0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: ansic: 87,027; python: 7,001; cpp: 1,131; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (203 lines) | stat: -rw-r--r-- 4,108 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "uwsgi.h"

extern struct uwsgi_server uwsgi;


void uwsgi_setup_systemd() {
	struct uwsgi_socket *uwsgi_sock = NULL;
	int i;

	char *listen_pid = getenv("LISTEN_PID");
	if (listen_pid) {
		if (atoi(listen_pid) == (int) getpid()) {
			char *listen_fds = getenv("LISTEN_FDS");
			if (listen_fds) {
				int systemd_fds = atoi(listen_fds);
				if (systemd_fds > 0) {
					uwsgi_log("- SystemD socket activation detected -\n");
					for (i = 3; i < 3 + systemd_fds; i++) {
						uwsgi_sock = uwsgi_new_socket(NULL);
						uwsgi_add_socket_from_fd(uwsgi_sock, i);
					}
					uwsgi.skip_zero = 1;
				}
				unsetenv("LISTEN_PID");
				unsetenv("LISTEN_FDS");
			}
		}
	}

}

void uwsgi_setup_upstart() {

	struct uwsgi_socket *uwsgi_sock = NULL;

	char *upstart_events = getenv("UPSTART_EVENTS");
	if (upstart_events && !strcmp(upstart_events, "socket")) {
		char *upstart_fds = getenv("UPSTART_FDS");
		if (upstart_fds) {
			uwsgi_log("- Upstart socket bridge detected (job: %s) -\n", getenv("UPSTART_JOB"));
			uwsgi_sock = uwsgi_new_socket(NULL);
			uwsgi_add_socket_from_fd(uwsgi_sock, atoi(upstart_fds));
			uwsgi.skip_zero = 1;
		}
		unsetenv("UPSTART_EVENTS");
		unsetenv("UPSTART_FDS");
	}

}


void uwsgi_setup_zerg() {

	struct uwsgi_socket *uwsgi_sock = NULL;
	int i;

	struct uwsgi_string_list *zn = uwsgi.zerg_node;
	while (zn) {
		if (uwsgi_zerg_attach(zn->value)) {
			if (!uwsgi.zerg_fallback) {
				exit(1);
			}
		}
		zn = zn->next;
	}



	if (uwsgi.zerg) {
#ifdef UWSGI_DEBUG
		uwsgi_log("attaching zerg sockets...\n");
#endif
		int zerg_fd;
		i = 0;
		for (;;) {
			zerg_fd = uwsgi.zerg[i];
			if (zerg_fd == -1) {
				break;
			}
			uwsgi_sock = uwsgi_new_socket(NULL);
			uwsgi_add_socket_from_fd(uwsgi_sock, zerg_fd);
			i++;
		}

		uwsgi_log("zerg sockets attached\n");
	}
}

void uwsgi_setup_inherited_sockets() {

	int j;
	union uwsgi_sockaddr usa;
	union uwsgi_sockaddr_ptr gsa;

	struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
	while (uwsgi_sock) {
		//a bit overengineering
		if (uwsgi_sock->name[0] != 0 && !uwsgi_sock->bound) {
			for (j = 3; j < (int) uwsgi.max_fd; j++) {
				uwsgi_add_socket_from_fd(uwsgi_sock, j);
			}
		}
		uwsgi_sock = uwsgi_sock->next;
	}

	//now close all the unbound fd
	for (j = 3; j < (int) uwsgi.max_fd; j++) {
		int useless = 1;

		if (uwsgi_fd_is_safe(j)) continue;

		if (uwsgi.has_emperor) {
			if (j == uwsgi.emperor_fd)
				continue;
			if (j == uwsgi.emperor_fd_config)
				continue;
		}

		if (uwsgi.shared->worker_log_pipe[0] > -1) {
			if (j == uwsgi.shared->worker_log_pipe[0])
				continue;
		}

		if (uwsgi.shared->worker_log_pipe[1] > -1) {
			if (j == uwsgi.shared->worker_log_pipe[1])
				continue;
		}

		if (uwsgi.shared->worker_req_log_pipe[0] > -1) {
			if (j == uwsgi.shared->worker_req_log_pipe[0])
				continue;
		}

		if (uwsgi.shared->worker_req_log_pipe[1] > -1) {
			if (j == uwsgi.shared->worker_req_log_pipe[1])
				continue;
		}

		if (uwsgi.original_log_fd > -1) {
			if (j == uwsgi.original_log_fd)
				continue;
		}

		struct uwsgi_gateway_socket *ugs = uwsgi.gateway_sockets;
		int found = 0;
		while (ugs) {
			if (ugs->fd == j) {
				found = 1;
				break;
			}
			ugs = ugs->next;
		}
		if (found)
			continue;


		int y;
		found = 0;
		for (y = 0; y < ushared->gateways_cnt; y++) {
			if (ushared->gateways[y].internal_subscription_pipe[0] == j) {
				found = 1;
				break;
			}
			if (ushared->gateways[y].internal_subscription_pipe[1] == j) {
				found = 1;
				break;
			}
		}

		if (found)
			continue;


		socklen_t socket_type_len = sizeof(struct sockaddr_un);
		gsa.sa = (struct sockaddr *) &usa;
		if (!getsockname(j, gsa.sa, &socket_type_len)) {
			uwsgi_sock = uwsgi.sockets;
			while (uwsgi_sock) {
				if (uwsgi_sock->fd == j && uwsgi_sock->bound) {
					useless = 0;
					break;
				}
				uwsgi_sock = uwsgi_sock->next;
			}

			if (useless) {
				uwsgi_sock = uwsgi.shared_sockets;
				while (uwsgi_sock) {
					if (uwsgi_sock->fd == j && uwsgi_sock->bound) {
						useless = 0;
						break;
					}
					uwsgi_sock = uwsgi_sock->next;
				}
			}
		}

		if (useless) {
			close(j);
		}
	}

}