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,
22 * but heavily modified for Bacula
28 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
35 int allow_severity = LOG_NOTICE;
36 int deny_severity = LOG_WARNING;
37 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
40 /* Become Threaded Network Server */
42 bnet_thread_server(int port, int max_clients, workq_t *client_wq,
43 void handle_client_request(void *bsock))
45 int newsockfd, sockfd, stat;
47 struct sockaddr_in cli_addr; /* client's address */
48 struct sockaddr_in serv_addr; /* our address */
50 fd_set ready, sockset;
54 struct request_info request;
60 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
63 Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
71 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
72 Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
76 * Bind our local address so that the client can send to us.
78 bzero((char *) &serv_addr, sizeof(serv_addr));
79 serv_addr.sin_family = AF_INET;
80 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
81 serv_addr.sin_port = htons(port);
83 int tmax = 30 * (60 / 5); /* wait 30 minutes max */
84 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
86 tlog = 2*60; /* Complain every 2 minutes */
87 Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
91 Emsg2(M_ABORT, 0, "Cannot bind port %d: %s.\n", port, strerror(errno));
94 listen(sockfd, 5); /* tell system we are ready */
97 FD_SET(sockfd, &sockset);
99 /* Start work queue thread */
100 if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
101 Emsg1(M_ABORT, 0, "Could not init client queue: ERR=%s\n", strerror(stat));
106 * Wait for a connection from a client process.
109 if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
110 if (errno == EINTR || errno == EAGAIN) {
115 Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
118 clilen = sizeof(cli_addr);
119 newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
122 P(mutex); /* hosts_access is not thread safe */
123 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
125 if (!hosts_access(&request)) {
127 Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
128 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
136 * Receive notification when connection dies.
138 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
139 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
142 /* see who client is. i.e. who connected to us. */
143 caller = inet_ntoa(cli_addr.sin_addr);
144 if (caller == NULL) {
145 caller = "unknown client";
148 /* Queue client to be served */
149 if ((stat = workq_add(client_wq,
150 (void *)init_bsock(newsockfd, "client", caller, port))) != 0) {
151 Emsg1(M_ABORT, 0, "Could not add job to client queue: ERR=%s\n", strerror(stat));
159 * Bind an address so that we may accept connections
166 struct sockaddr_in serv_addr; /* our address */
173 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
176 Emsg1(M_ERROR, 0, "Cannot open stream socket: %s\n", strerror(errno));
184 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
185 Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
189 * Bind our local address so that the client can send to us.
191 bzero((char *) &serv_addr, sizeof(serv_addr));
192 serv_addr.sin_family = AF_INET;
193 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
194 serv_addr.sin_port = htons(port);
196 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
199 Emsg2(M_WARNING, 0, "Cannot bind port %d: %s: retrying ...\n", port, strerror(errno));
203 listen(sockfd, 1); /* tell system we are ready */
204 return init_bsock(sockfd, "Server socket", "client", port);
208 * Accept a single connection
211 bnet_accept(BSOCK *bsock, char *who)
213 fd_set ready, sockset;
214 int newsockfd, stat, len;
216 struct sockaddr_in cli_addr; /* client's address */
221 struct request_info request;
225 * Wait for a connection from the client process.
228 FD_SET(bsock->fd, &sockset);
232 * Wait for a connection from a client process.
235 if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
236 if (errno == EINTR || errno == EAGAIN) {
240 Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
244 clilen = sizeof(cli_addr);
245 newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
251 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
253 if (!hosts_access(&request)) {
255 Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
256 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
264 * Receive notification when connection dies.
266 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
267 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
270 /* see who client is. I.e. who connected to us.
271 * return it in the input message buffer.
273 if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
274 strcpy(bsock->msg, caller);
278 bsock->msglen = strlen(bsock->msg);
281 Emsg2(M_FATAL, 0, "Socket accept error for %s. ERR=%s\n", who,
285 if (caller == NULL) {
288 len = strlen(caller) + strlen(who) + 3;
289 buf = (char *) malloc(len);
293 bs = init_bsock(newsockfd, "client", buf, bsock->port);
295 return bs; /* return new BSOCK */
304 * The following code will soon be deleted, don't attempt
307 #ifdef dont_have_threads
310 * This routine is called by the main process to
311 * track its children. On CYGWIN, the child processes
312 * don't always exit when the other end of the socket
313 * hangs up. Thus they remain hung on a read(). After
314 * 30 seconds, we send them a SIGTERM signal, which
315 * causes them to wake up to the reality of the situation.
317 * Also, on certain Unix systems such as SunOS, we must
318 * explicitly do the wait, otherwise, the child that dies
319 * remains a zombie potentially remaining bound to the
325 static void reap_children(int childpid)
327 static int pids[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
328 static int times[10];
333 for (i=0; i<10; i++) {
334 if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
337 if (pids[i] && ((now - times[i]) > 30))
338 kill(pids[i], SIGTERM);
341 for (i=0; i<10; i++) {
342 if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
345 if (childpid && (pids[i] == 0)) {
353 #endif /* HAVE_SUN_OS */
355 /* Become network server */
357 bnet_server(int port, void handle_client_request(BSOCK *bsock))
359 int newsockfd, sockfd, clilen, childpid, stat;
360 struct sockaddr_in cli_addr; /* client's address */
361 struct sockaddr_in serv_addr; /* our address */
363 fd_set ready, sockset;
367 struct request_info request;
370 /* **** FIXME **** handle BSD too */
371 signal(SIGCHLD, SIG_IGN);
376 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
379 Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
387 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
388 Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
392 * Receive notification when connection dies.
394 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
395 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
399 * Bind our local address so that the client can send to us.
401 bzero((char *) &serv_addr, sizeof(serv_addr));
402 serv_addr.sin_family = AF_INET;
403 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
404 serv_addr.sin_port = htons(port);
406 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
408 tlog = 2*60; /* Complain every 2 minutes */
409 Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
413 listen(sockfd, 5); /* tell system we are ready */
416 FD_SET(sockfd, &sockset);
420 * Wait for a connection from a client process.
423 if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
424 if (errno == EINTR || errno == EAGAIN) {
429 Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
432 clilen = sizeof(cli_addr);
433 newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
436 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
438 if (!hosts_access(&request)) {
439 Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
440 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
447 * Receive notification when connection dies.
449 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
450 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
454 /* see who client is. i.e. who connected to us. */
455 caller = inet_ntoa(cli_addr.sin_addr);
456 if (caller == NULL) {
462 handle_client_request(init_bsock(newsockfd, "client", caller, port));
464 /* fork to provide the response */
465 for (tlog=0; (childpid = fork()) < 0; tlog -= 5*60 ) {
468 Emsg1(M_FATAL, 0, "Fork error: %s\n", strerror(errno));
472 if (childpid == 0) { /* child process */
473 close(sockfd); /* close original socket */
474 handle_client_request(init_bsock(newsockfd, "client", caller, port)); /* process the request */
475 Dmsg0(99, "back from handle request\n");
481 close(newsockfd); /* parent process */
483 reap_children(childpid);
484 #endif /* HAVE_SUN_OS */
488 #endif /* no threads -- not supported any more sorry! */