2 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * Originally written by Kern Sibbald for inclusion in apcupsd.
25 #include <netinet/in.h>
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
32 int allow_severity = LOG_NOTICE;
33 int deny_severity = LOG_WARNING;
34 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
37 /* Become Threaded Network Server */
39 bnet_thread_server(int port, int max_clients, workq_t *client_wq,
40 void handle_client_request(void *bsock))
42 int newsockfd, sockfd, stat;
44 struct sockaddr_in cli_addr; /* client's address */
45 struct sockaddr_in serv_addr; /* our address */
47 fd_set ready, sockset;
51 struct request_info request;
57 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
60 Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
68 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
69 Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
73 * Bind our local address so that the client can send to us.
75 bzero((char *) &serv_addr, sizeof(serv_addr));
76 serv_addr.sin_family = AF_INET;
77 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
78 serv_addr.sin_port = htons(port);
80 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
82 tlog = 2*60; /* Complain every 2 minutes */
83 Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
87 listen(sockfd, 5); /* tell system we are ready */
90 FD_SET(sockfd, &sockset);
92 /* Start work queue thread */
93 if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
94 Emsg1(M_ABORT, 0, "Could not init client queue: ERR=%s\n", strerror(stat));
99 * Wait for a connection from a client process.
102 if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
103 if (errno == EINTR || errno == EAGAIN) {
108 Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
111 clilen = sizeof(cli_addr);
112 newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
115 P(mutex); /* hosts_access is not thread safe */
116 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
118 if (!hosts_access(&request)) {
120 Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
121 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
129 * Receive notification when connection dies.
131 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
132 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
135 /* see who client is. i.e. who connected to us. */
136 caller = inet_ntoa(cli_addr.sin_addr);
137 if (caller == NULL) {
138 caller = "unknown client";
141 /* Queue client to be served */
142 if ((stat = workq_add(client_wq,
143 (void *)init_bsock(newsockfd, "client", caller, port))) != 0) {
144 Emsg1(M_ABORT, 0, "Could not add job to client queue: ERR=%s\n", strerror(stat));
151 * Bind an address so that we may accept connections
158 struct sockaddr_in serv_addr; /* our address */
165 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
168 Emsg1(M_ERROR, 0, "Cannot open stream socket: %s\n", strerror(errno));
176 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
177 Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
181 * Bind our local address so that the client can send to us.
183 bzero((char *) &serv_addr, sizeof(serv_addr));
184 serv_addr.sin_family = AF_INET;
185 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
186 serv_addr.sin_port = htons(port);
188 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
191 Emsg2(M_WARNING, 0, "Cannot bind port %d: %s: retrying ...\n", port, strerror(errno));
195 listen(sockfd, 1); /* tell system we are ready */
196 return init_bsock(sockfd, "Server socket", "client", port);
200 * Accept a single connection
203 bnet_accept(BSOCK *bsock, char *who)
205 fd_set ready, sockset;
206 int newsockfd, stat, len;
208 struct sockaddr_in cli_addr; /* client's address */
213 struct request_info request;
217 * Wait for a connection from the client process.
220 FD_SET(bsock->fd, &sockset);
224 * Wait for a connection from a client process.
227 if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
228 if (errno == EINTR || errno == EAGAIN) {
232 Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
236 clilen = sizeof(cli_addr);
237 newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
243 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
245 if (!hosts_access(&request)) {
247 Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
248 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
256 * Receive notification when connection dies.
258 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
259 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
262 /* see who client is. I.e. who connected to us.
263 * return it in the input message buffer.
265 if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
266 strcpy(bsock->msg, caller);
270 bsock->msglen = strlen(bsock->msg);
273 Emsg2(M_FATAL, 0, "Socket accept error for %s. ERR=%s\n", who,
277 if (caller == NULL) {
280 len = strlen(caller) + strlen(who) + 3;
281 buf = (char *) malloc(len);
285 bs = init_bsock(newsockfd, "client", buf, bsock->port);
287 return bs; /* return new BSOCK */
295 * The following code will soon be deleted, don't attempt
298 #ifdef dont_have_threads_junk
301 * This routine is called by the main process to
302 * track its children. On CYGWIN, the child processes
303 * don't always exit when the other end of the socket
304 * hangs up. Thus they remain hung on a read(). After
305 * 30 seconds, we send them a SIGTERM signal, which
306 * causes them to wake up to the reality of the situation.
308 * Also, on certain Unix systems such as SunOS, we must
309 * explicitly do the wait, otherwise, the child that dies
310 * remains a zombie potentially remaining bound to the
316 static void reap_children(int childpid)
318 static int pids[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
319 static int times[10];
324 for (i=0; i<10; i++) {
325 if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
328 if (pids[i] && ((now - times[i]) > 30))
329 kill(pids[i], SIGTERM);
332 for (i=0; i<10; i++) {
333 if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
336 if (childpid && (pids[i] == 0)) {
344 #endif /* HAVE_SUN_OS */
346 /* Become network server */
348 bnet_server(int port, void handle_client_request(BSOCK *bsock))
350 int newsockfd, sockfd, clilen, childpid, stat;
351 struct sockaddr_in cli_addr; /* client's address */
352 struct sockaddr_in serv_addr; /* our address */
354 fd_set ready, sockset;
358 struct request_info request;
361 /* **** FIXME **** handle BSD too */
362 signal(SIGCHLD, SIG_IGN);
367 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
370 Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
378 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
379 Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
383 * Receive notification when connection dies.
385 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
386 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
390 * Bind our local address so that the client can send to us.
392 bzero((char *) &serv_addr, sizeof(serv_addr));
393 serv_addr.sin_family = AF_INET;
394 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
395 serv_addr.sin_port = htons(port);
397 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
399 tlog = 2*60; /* Complain every 2 minutes */
400 Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
404 listen(sockfd, 5); /* tell system we are ready */
407 FD_SET(sockfd, &sockset);
411 * Wait for a connection from a client process.
414 if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
415 if (errno == EINTR || errno == EAGAIN) {
420 Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
423 clilen = sizeof(cli_addr);
424 newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
427 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
429 if (!hosts_access(&request)) {
430 Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
431 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
438 * Receive notification when connection dies.
440 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
441 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
445 /* see who client is. i.e. who connected to us. */
446 caller = inet_ntoa(cli_addr.sin_addr);
447 if (caller == NULL) {
453 handle_client_request(init_bsock(newsockfd, "client", caller, port));
455 /* fork to provide the response */
456 for (tlog=0; (childpid = fork()) < 0; tlog -= 5*60 ) {
459 Emsg1(M_FATAL, 0, "Fork error: %s\n", strerror(errno));
463 if (childpid == 0) { /* child process */
464 close(sockfd); /* close original socket */
465 handle_client_request(init_bsock(newsockfd, "client", caller, port)); /* process the request */
466 Dmsg0(99, "back from handle request\n");
472 close(newsockfd); /* parent process */
474 reap_children(childpid);
475 #endif /* HAVE_SUN_OS */
479 #endif /* no threads -- not supported any more sorry! */