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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
|
#include "common.h"
#include "c-icap.h"
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include "net_io.h"
#include "proc_mutex.h"
#include "debug.h"
#include "log.h"
#include "request.h"
#include "ci_threads.h"
#include "proc_threads_queues.h"
#include "cfg_param.h"
#include "commands.h"
extern int KEEPALIVE_TIMEOUT;
extern int MAX_KEEPALIVE_REQUESTS;
extern int MAX_SECS_TO_LINGER;
extern int START_CHILDS;
extern int MAX_CHILDS;
extern int START_SERVERS;
extern int MIN_FREE_SERVERS;
extern int MAX_FREE_SERVERS;
extern int MAX_REQUESTS_BEFORE_REALLOCATE_MEM;
extern int MAX_REQUESTS_PER_CHILD;
extern struct ci_server_conf C_ICAP_CONF;
typedef struct server_decl {
int srv_id;
ci_thread_t srv_pthread;
struct connections_queue *con_queue;
ci_request_t *current_req;
int served_requests;
int served_requests_no_reallocation;
int running;
} server_decl_t;
#undef SINGLE_ACCEPT
ci_thread_mutex_t threads_list_mtx;
server_decl_t **threads_list = NULL;
ci_thread_t listener_thread_id = -1;
int listener_running = 0;
ci_thread_cond_t free_server_cond;
ci_thread_mutex_t counters_mtx;
struct childs_queue *childs_queue;
struct childs_queue *old_childs_queue = NULL;
child_shared_data_t *child_data;
struct connections_queue *con_queue;
const int CHILD_SHUTDOWN_TIMEOUT = 10;
int CHILD_HALT = 0;
ci_proc_mutex_t accept_mutex;
ci_socket LISTEN_SOCKET = -1;
int c_icap_going_to_term = 0;
int c_icap_reconfigure = 0;
#define hard_close_connection(connection) ci_hard_close(connection->fd)
#define close_connection(connection) ci_linger_close(connection->fd,MAX_SECS_TO_LINGER)
#define check_for_keepalive_data(fd) ci_wait_for_data(fd,KEEPALIVE_TIMEOUT,wait_for_read)
void init_commands();
int init_server(char *address, int port, int *family);
int start_child(int fd);
void system_shutdown();
static void term_handler_child(int sig)
{
ci_debug_printf(5, "A termination signal received (%d).\n", sig);
if (!child_data->father_said)
child_data->to_be_killed = IMMEDIATELY;
else
child_data->to_be_killed = child_data->father_said;
}
static void sigpipe_handler(int sig)
{
ci_debug_printf(5, "SIGPIPE signal received.\n");
}
static void empty(int sig)
{
ci_debug_printf(10, "An empty signal handler (%d).\n", sig);
}
static void sigint_handler_main(int sig)
{
if (sig == SIGTERM) {
ci_debug_printf(5, "SIGTERM signal received for main server.\n");
}
else if (sig == SIGINT) {
ci_debug_printf(5, "SIGINT signal received for main server.\n");
}
else {
ci_debug_printf(5, "Signal %d received. Exiting ....\n", sig);
}
c_icap_going_to_term = 1;
}
static void sigchld_handler_main(int sig)
{
}
static void sighup_handler_main()
{
c_icap_reconfigure = 1;
}
void child_signals()
{
signal(SIGPIPE, sigpipe_handler);
signal(SIGINT, SIG_IGN);
signal(SIGTERM, term_handler_child);
signal(SIGHUP, empty);
}
void main_signals()
{
signal(SIGPIPE, sigpipe_handler);
signal(SIGTERM, sigint_handler_main);
signal(SIGINT, sigint_handler_main);
signal(SIGCHLD, sigchld_handler_main);
signal(SIGHUP, sighup_handler_main);
}
void thread_signals(int islistener)
{
sigset_t sig_mask;
sigemptyset(&sig_mask);
sigaddset(&sig_mask, SIGINT);
if (!islistener)
sigaddset(&sig_mask, SIGHUP);
if (pthread_sigmask(SIG_BLOCK, &sig_mask, NULL))
ci_debug_printf(5, "O an error....\n");
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
}
static void exit_normaly()
{
dettach_childs_queue(childs_queue);
system_shutdown();
}
static void release_thread_i(int i)
{
if (threads_list[i]->current_req) {
ci_request_destroy(threads_list[i]->current_req);
}
free(threads_list[i]);
threads_list[i] = NULL;
}
static void cancel_all_threads()
{
int i = 0;
int wait_listener_time = 10000;
int wait_for_workers = CHILD_SHUTDOWN_TIMEOUT>5?CHILD_SHUTDOWN_TIMEOUT:5;
int servers_running;
while (wait_listener_time > 0 && listener_running != 0) {
#ifdef SINGLE_ACCEPT
pthread_cancel(listener_thread_id);
#else
pthread_kill(listener_thread_id, SIGHUP);
#endif
ci_thread_cond_signal(&free_server_cond);
ci_usleep(1000);
wait_listener_time -= 10;
}
if (listener_running == 0) {
ci_debug_printf(5,
"Going to wait for the listener thread (pid: %d) to exit!\n",
threads_list[0]->srv_id);
ci_thread_join(listener_thread_id);
ci_debug_printf(5, "OK, cancelling the listener thread (pid: %d)!\n",
threads_list[0]->srv_id);
}
else {
}
ci_thread_cond_broadcast(&(con_queue->queue_cond));
ci_usleep(1000);
servers_running = START_SERVERS;
while (servers_running && wait_for_workers >= 0) {
if (child_data->to_be_killed == IMMEDIATELY) {
CHILD_HALT = 1;
}
for (i=0; i<START_SERVERS; i++) {
if (threads_list[i] != NULL) {
if (!threads_list[i]->running) {
ci_debug_printf(5, "Cancel server %d, thread_id %lu (%d)\n",
threads_list[i]->srv_id, threads_list[i]->srv_pthread,
i);
ci_thread_join(threads_list[i]->srv_pthread);
release_thread_i(i);
servers_running --;
}
else if (child_data->to_be_killed == IMMEDIATELY){
if (wait_for_workers <= 2) {
ci_debug_printf(5, "Thread %ld still running near the timeout. Try to kill it\n", threads_list[i]->srv_pthread);
pthread_kill( threads_list[i]->srv_pthread, SIGTERM);
}
}
}
}
ci_usleep(999999);
if (child_data->to_be_killed == IMMEDIATELY)
wait_for_workers --;
}
if (servers_running) {
ci_debug_printf(5, "Not all the servers canceled. Anyway exiting....\n");
}
else {
ci_debug_printf(5, "All servers canceled\n");
free(threads_list);
}
}
static void kill_all_childs()
{
int i = 0, status, pid;
ci_debug_printf(5, "Going to term children....\n");
for (i = 0; i < childs_queue->size; i++) {
if ((pid = childs_queue->childs[i].pid) == 0)
continue;
childs_queue->childs[i].father_said = IMMEDIATELY;
kill(pid, SIGTERM);
}
for (i = 0; i < childs_queue->size; i++) {
if (childs_queue->childs[i].pid == 0)
continue;
pid = wait(&status);
ci_debug_printf(5, "Child %d died with status %d\n", pid, status);
}
ci_proc_mutex_destroy(&accept_mutex);
destroy_childs_queue(childs_queue);
}
static void check_for_exited_childs()
{
int status, pid, ret;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
ci_debug_printf(5, "Child %d died ...\n", pid);
if (!WIFEXITED(status)) {
ci_debug_printf(1, "Child %d did not exit normally.", pid);
if (WIFSIGNALED(status))
ci_debug_printf(1, "signaled with signal:%d\n",
WTERMSIG(status));
}
ret = remove_child(childs_queue, pid);
if (ret == 0 && old_childs_queue) {
ci_debug_printf(5,
"Child %d will be removed from the old list ...\n",
pid);
remove_child(old_childs_queue, pid);
if (childs_queue_is_empty(old_childs_queue)) {
ret = destroy_childs_queue(old_childs_queue);
old_childs_queue = NULL;
}
}
}
if (pid < 0)
ci_debug_printf(1, "Fatal error waiting for a child to exit .....\n");
}
int system_reconfigure();
static int server_reconfigure()
{
int i;
if (old_childs_queue) {
ci_debug_printf(1,
"A reconfigure pending. Ignoring reconfigure request.....\n");
return 1;
}
if (!system_reconfigure())
return 0;
init_commands();
for (i = 0; i < childs_queue->size; i++) {
if (childs_queue->childs[i].pid != 0) {
childs_queue->childs[i].father_said = GRACEFULLY;
kill(childs_queue->childs[i].pid, SIGTERM);
}
}
old_childs_queue = childs_queue;
childs_queue = malloc(sizeof(struct childs_queue));
if (!create_childs_queue(childs_queue, 2 * MAX_CHILDS)) {
ci_debug_printf(1,
"Cannot init shared memory. Fatal error, exiting!\n");
return 0;
}
if (START_CHILDS > MAX_CHILDS)
START_CHILDS = MAX_CHILDS;
for (i = 0; i < START_CHILDS; i++) {
start_child(LISTEN_SOCKET);
}
return 1;
}
int ci_named_pipe_create(char *name)
{
int status, pipe;
errno = 0;
status = mkfifo(name, S_IRWXU | S_IRGRP | S_IROTH);
if (status < 0 && errno != EEXIST)
return -1;
pipe = open(name, O_RDONLY | O_NONBLOCK);
return pipe;
}
int ci_named_pipe_open(char *name)
{
int pipe;
pipe = open(name, O_RDONLY | O_NONBLOCK);
return pipe;
}
void ci_named_pipe_close(int pipe_fd)
{
close(pipe_fd);
}
int wait_for_commands(int pipe_fd, char *command_buffer, int secs)
{
fd_set fds;
struct timeval tv;
int ret = 0;
if (secs >= 0) {
tv.tv_sec = secs;
tv.tv_usec = 0;
}
FD_ZERO(&fds);
FD_SET(pipe_fd, &fds);
errno = 0;
if ((ret =
select(pipe_fd + 1, &fds, NULL, NULL,
(secs >= 0 ? &tv : NULL))) > 0) {
if (FD_ISSET(pipe_fd, &fds)) {
ret =
ci_read_nonblock(pipe_fd, command_buffer,
COMMANDS_BUFFER_SIZE - 1);
if (ret >=0) {
command_buffer[ret] = '\0';
return ret;
}
}
}
if (ret < 0 && errno != EINTR) {
ci_debug_printf(1,
"Unxpected error waiting for events in control socket!\n");
return 0;
}
return -1;
}
void handle_monitor_process_commands(char *cmd_line)
{
ci_command_t *command;
int i, bytes;
if ((command = find_command(cmd_line)) != NULL) {
if (command->type & MONITOR_PROC_CMD)
execute_command(command, cmd_line, MONITOR_PROC_CMD);
if (command->type & CHILDS_PROC_CMD) {
for (i = 0; i < childs_queue->size; i++) {
bytes = write(childs_queue->childs[i].pipe, cmd_line,
strlen(cmd_line));
}
}
if (command->type & MONITOR_PROC_POST_CMD)
execute_command(command, cmd_line, MONITOR_PROC_POST_CMD);
}
}
void handle_child_process_commands(char *cmd_line)
{
ci_command_t *command;
if ((command = find_command(cmd_line)) != NULL) {
if (command->type & CHILDS_PROC_CMD)
execute_command(command, cmd_line, CHILDS_PROC_CMD);
}
}
server_decl_t *newthread(struct connections_queue *con_queue)
{
server_decl_t *serv;
serv = (server_decl_t *) malloc(sizeof(server_decl_t));
serv->srv_id = 0;
serv->con_queue = con_queue;
serv->served_requests = 0;
serv->served_requests_no_reallocation = 0;
serv->current_req = NULL;
serv->running = 1;
return serv;
}
int thread_main(server_decl_t * srv)
{
ci_connection_t con;
char clientname[CI_MAXHOSTNAMELEN + 1];
int ret, request_status = 0;
int keepalive_reqs;
thread_signals(0);
srv->srv_id = getpid();
for (;;) {
if (child_data->to_be_killed == IMMEDIATELY) {
srv->running = 0;
return 1;
}
if ((ret = get_from_queue(con_queue, &con)) == 0) {
if (child_data->to_be_killed) {
srv->running = 0;
return 1;
}
ret = wait_for_queue(con_queue);
continue;
}
if (ret < 0) {
ci_debug_printf(1,
"Fatal Error!!! Error getting a connection from connections queue!!!\n");
break;
}
ci_thread_mutex_lock(&counters_mtx);
(child_data->freeservers)--;
(child_data->usedservers)++;
ci_thread_mutex_unlock(&counters_mtx);
ci_netio_init(con.fd);
ret = 1;
if (srv->current_req == NULL)
srv->current_req = newrequest(&con);
else
ret = recycle_request(srv->current_req, &con);
if (srv->current_req == NULL || ret == 0) {
ci_sockaddr_t_to_host(&(con.claddr), clientname,
CI_MAXHOSTNAMELEN);
ci_debug_printf(1, "Request from %s denied...\n", clientname);
hard_close_connection((&con));
goto end_of_main_loop_thread;
}
keepalive_reqs = 0;
do {
if (MAX_KEEPALIVE_REQUESTS > 0
&& keepalive_reqs >= MAX_KEEPALIVE_REQUESTS)
srv->current_req->keepalive = 0;
if (child_data->to_be_killed)
srv->current_req->keepalive = 0;
if ((request_status = process_request(srv->current_req)) < 0) {
ci_debug_printf(5,
"Process request timeout or interrupted....\n");
ci_request_reset(srv->current_req);
break;
}
srv->served_requests++;
srv->served_requests_no_reallocation++;
keepalive_reqs++;
ci_thread_mutex_lock(&counters_mtx);
(child_data->requests)++;
ci_thread_mutex_unlock(&counters_mtx);
log_access(srv->current_req, request_status);
if (child_data->to_be_killed == IMMEDIATELY)
break;
if (child_data->to_be_killed == GRACEFULLY &&
srv->current_req->keepalive == 0)
break;
ci_debug_printf(8, "Keep-alive:%d\n",
srv->current_req->keepalive);
if (srv->current_req->keepalive
&& check_for_keepalive_data(srv->current_req->connection->
fd) > 0) {
ci_request_reset(srv->current_req);
ci_debug_printf(8,
"Server %d going to serve new request from client (keep-alive) \n",
srv->srv_id);
}
else
break;
} while (1);
if (srv->current_req) {
if (request_status < 0 || child_data->to_be_killed) {
hard_close_connection(srv->current_req->connection);
}
else {
close_connection(srv->current_req->connection);
}
}
if (srv->served_requests_no_reallocation >
MAX_REQUESTS_BEFORE_REALLOCATE_MEM) {
ci_debug_printf(5,
"Max requests reached, reallocate memory and buffers .....\n");
ci_request_destroy(srv->current_req);
srv->current_req = NULL;
srv->served_requests_no_reallocation = 0;
}
end_of_main_loop_thread:
ci_thread_mutex_lock(&counters_mtx);
(child_data->freeservers)++;
(child_data->usedservers)--;
ci_thread_mutex_unlock(&counters_mtx);
ci_thread_cond_signal(&free_server_cond);
}
srv->running = 0;
return 0;
}
void listener_thread(int *fd)
{
ci_connection_t conn;
socklen_t claddrlen = sizeof(struct sockaddr_in);
int haschild = 1, jobs_in_queue = 0;
int pid, sockfd;
sockfd = *fd;
thread_signals(1);
pid = getpid();
for (;;) {
if (child_data->to_be_killed) {
ci_debug_printf(5, "Listener of pid:%d exiting!\n", pid);
goto LISTENER_FAILS_UNLOCKED;
}
if (!ci_proc_mutex_lock(&accept_mutex)) {
if (errno == EINTR) {
ci_debug_printf(5,
"proc_mutex_lock interrupted (EINTR received, pid=%d)!\n",
pid);
continue;
}
else {
ci_debug_printf(1,
"Unknown errno %d in proc_mutex_lock of pid %d. Exiting!\n",
errno, pid);
goto LISTENER_FAILS_UNLOCKED;
}
}
child_data->idle = 0;
ci_debug_printf(7, "Child %d getting requests now ...\n", pid);
do {
#ifndef SINGLE_ACCEPT
fd_set fds;
int ret;
do {
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
errno = 0;
ret = select(sockfd + 1, &fds, NULL, NULL, NULL);
if (ret < 0) {
if (errno != EINTR) {
ci_debug_printf(1,
"Error in select %d! Exiting server!\n",
errno);
goto LISTENER_FAILS;
}
if (child_data->to_be_killed) {
ci_debug_printf(5,
"Listener server signalled to exit!\n");
goto LISTENER_FAILS;
}
}
} while (errno == EINTR);
#endif
do {
errno = 0;
claddrlen = sizeof(conn.claddr.sockaddr);
if (((conn.fd =
accept(sockfd,
(struct sockaddr *) &(conn.claddr.sockaddr),
&claddrlen)) == -1)) {
if (errno != EINTR) {
ci_debug_printf(1,
"Error accept %d!\nExiting server!\n",
errno);
goto LISTENER_FAILS;
}
if (errno == EINTR && child_data->to_be_killed) {
ci_debug_printf(5,
"Listener server signalled to exit!\n");
goto LISTENER_FAILS;
}
}
} while (errno == EINTR && !child_data->to_be_killed);
claddrlen = sizeof(conn.srvaddr.sockaddr);
getsockname(conn.fd,
(struct sockaddr *) &(conn.srvaddr.sockaddr),
&claddrlen);
ci_fill_sockaddr(&conn.claddr);
ci_fill_sockaddr(&conn.srvaddr);
icap_socket_opts(sockfd, MAX_SECS_TO_LINGER);
if ((jobs_in_queue = put_to_queue(con_queue, &conn)) == 0) {
ci_debug_printf(1,
"ERROR!!!!!! NO AVAILABLE SERVERS! THIS IS A BUG!!!!!!!!\n");
ci_debug_printf(1,
"Jobs in Queue: %d, Free servers: %d, Used Servers: %d, Requests: %d\n",
jobs_in_queue, child_data->freeservers,
child_data->usedservers,
child_data->requests);
goto LISTENER_FAILS;
}
(child_data->connections)++;
if (child_data->to_be_killed) {
ci_debug_printf(5, "Listener server must exit!\n");
goto LISTENER_FAILS;
}
ci_thread_mutex_lock(&counters_mtx);
haschild =
((child_data->freeservers - jobs_in_queue) > 0 ? 1 : 0);
ci_thread_mutex_unlock(&counters_mtx);
} while (haschild);
ci_debug_printf(7, "Child %d STOPS getting requests now ...\n", pid);
child_data->idle = 1;
while (!ci_proc_mutex_unlock(&accept_mutex)) {
if (errno != EINTR) {
ci_debug_printf(1,
"Error:%d while trying to unlock proc_mutex, exiting listener of server:%d\n",
errno, pid);
goto LISTENER_FAILS_UNLOCKED;
}
ci_debug_printf(5,
"Mutex lock interrupted while trying to unlock proc_mutex, pid: %d\n",
pid);
}
ci_thread_mutex_lock(&counters_mtx);
if ((child_data->freeservers - connections_pending(con_queue)) <= 0) {
ci_debug_printf(7,
"Child %d waiting for a thread to accept more connections ...\n",
pid);
ci_thread_cond_wait(&free_server_cond, &counters_mtx);
}
ci_thread_mutex_unlock(&counters_mtx);
}
LISTENER_FAILS_UNLOCKED:
listener_running = 0;
return;
LISTENER_FAILS:
listener_running = 0;
errno = 0;
while (!ci_proc_mutex_unlock(&accept_mutex)) {
if (errno != EINTR) {
ci_debug_printf(1,
"Error:%d while trying to unlock proc_mutex of server:%d\n",
errno, pid);
break;
}
ci_debug_printf(7,
"Mutex lock interrupted while trying to unlock proc_mutex before terminating\n");
}
return;
}
void child_main(int sockfd, int pipefd)
{
ci_thread_t thread;
int i, ret;
signal(SIGTERM, SIG_IGN);
ci_thread_mutex_init(&threads_list_mtx);
ci_thread_mutex_init(&counters_mtx);
ci_thread_cond_init(&free_server_cond);
ci_stat_attach_mem(child_data->stats, child_data->stats_size, NULL);
threads_list =
(server_decl_t **) malloc((START_SERVERS + 1) *
sizeof(server_decl_t *));
con_queue = init_queue(START_SERVERS);
execure_start_child_commands ();
for (i = 0; i < START_SERVERS; i++) {
if ((threads_list[i] = newthread(con_queue)) == NULL) {
exit(-1);
}
ret =
ci_thread_create(&thread, (void *(*)(void *)) thread_main,
(void *) threads_list[i]);
threads_list[i]->srv_pthread = thread;
}
threads_list[START_SERVERS] = NULL;
ret = ci_thread_create(&thread, (void *(*)(void *)) listener_thread,
(void *) &sockfd);
listener_thread_id = thread;
listener_running = 1;
srand(((unsigned int)time(NULL)) + (unsigned int)getpid());
child_signals();
for (;;) {
char buf[512];
int bytes;
if ((ret = ci_wait_for_data(pipefd, 5, wait_for_read)) > 0) {
bytes = ci_read_nonblock(pipefd, buf, 511);
if (bytes == 0) {
ci_debug_printf(1,
"Parent closed the pipe connection! Going to term immediately!\n");
child_data->to_be_killed = IMMEDIATELY;
break;
}
buf[bytes] = '\0';
handle_child_process_commands(buf);
}
else if (ret < 0) {
ci_debug_printf(1,
"An error occured while waiting for commands from parent. Terminating!\n");
child_data->to_be_killed = IMMEDIATELY;
}
if (!listener_running && !child_data->to_be_killed) {
ci_debug_printf(1,
"Ohh!! something happened to listener thread! Terminating\n");
child_data->to_be_killed = GRACEFULLY;
}
if (child_data->to_be_killed)
break;
}
ci_debug_printf(5, "Child :%d going down :%s\n", getpid(),
child_data->to_be_killed == IMMEDIATELY?
"IMMEDIATELY" : "GRACEFULLY");
cancel_all_threads();
exit_normaly();
}
#define MULTICHILD
int start_child(int fd)
{
int pid;
int pfd[2];
if (pipe(pfd) < 0) {
ci_debug_printf(1,
"Error creating pipe for communication with child\n");
return -1;
}
if (fcntl(pfd[0], F_SETFL, O_NONBLOCK) < 0
|| fcntl(pfd[1], F_SETFL, O_NONBLOCK) < 0) {
ci_debug_printf(1, "Error making the child pipe non-blocking\n");
close(pfd[0]);
close(pfd[1]);
}
if ((pid = fork()) == 0) {
attach_childs_queue(childs_queue);
child_data =
register_child(childs_queue, getpid(), START_SERVERS, pfd[1]);
close(pfd[1]);
child_main(fd, pfd[0]);
exit(0);
}
else {
close(pfd[0]);
return pid;
}
}
void stop_command(char *name, int type, char **argv)
{
c_icap_going_to_term = 1;
}
void reconfigure_command(char *name, int type, char **argv)
{
if (type == MONITOR_PROC_CMD)
c_icap_reconfigure = 1;
}
void dump_statistics_command(char *name, int type, char **argv)
{
if (type == MONITOR_PROC_CMD)
dump_queue_statistics(childs_queue);
}
void test_command(char *name, int type, char **argv)
{
int i = 0;
ci_debug_printf(1, "Test command for %s. Arguments:",
(type ==
MONITOR_PROC_CMD ? "monitor process" : "child process"));
while (argv[i] != NULL) {
ci_debug_printf(1, "%s,", argv[i]);
i++;
}
ci_debug_printf(1, "\n");
}
int init_server(char *address, int port, int *family)
{
if (LISTEN_SOCKET != -1)
close(LISTEN_SOCKET);
LISTEN_SOCKET = icap_init_server(address, port, family, MAX_SECS_TO_LINGER);
if (LISTEN_SOCKET == CI_SOCKET_ERROR)
return 0;
return 1;
}
void init_commands()
{
register_command("stop", MONITOR_PROC_CMD, stop_command);
register_command("reconfigure", MONITOR_PROC_CMD, reconfigure_command);
register_command("dump_statistics", MONITOR_PROC_CMD, dump_statistics_command);
register_command("test", MONITOR_PROC_CMD | CHILDS_PROC_CMD, test_command);
}
int start_server()
{
int child_indx, pid, i, ctl_socket;
int childs, freeservers, used, maxrequests, ret;
char command_buffer[COMMANDS_BUFFER_SIZE];
int user_informed = 0;
ctl_socket = ci_named_pipe_create(C_ICAP_CONF.COMMANDS_SOCKET);
if (ctl_socket < 0) {
ci_debug_printf(1,
"Error opening control socket %s. Fatal error, exiting!\n",
C_ICAP_CONF.COMMANDS_SOCKET);
exit(0);
}
ci_proc_mutex_init(&accept_mutex);
childs_queue = malloc(sizeof(struct childs_queue));
if (!create_childs_queue(childs_queue, 2 * MAX_CHILDS)) {
ci_debug_printf(1,
"Can't init shared memory. Fatal error, exiting!\n");
exit(0);
}
init_commands();
pid = 1;
#ifdef MULTICHILD
if (START_CHILDS > MAX_CHILDS)
START_CHILDS = MAX_CHILDS;
for (i = 0; i < START_CHILDS; i++) {
if (pid)
pid = start_child(LISTEN_SOCKET);
}
if (pid != 0) {
main_signals();
while (1) {
if ((ret = wait_for_commands(ctl_socket, command_buffer, 1)) > 0) {
ci_debug_printf(5, "I received the command: %s\n",
command_buffer);
handle_monitor_process_commands(command_buffer);
}
if (ret == 0) {
ci_named_pipe_close(ctl_socket);
ctl_socket = ci_named_pipe_open(C_ICAP_CONF.COMMANDS_SOCKET);
if (ctl_socket < 0) {
ci_debug_printf(1,
"Error opening control socket. We are unstable and going down!");
c_icap_going_to_term = 1;
}
}
if (c_icap_going_to_term)
break;
childs_queue_stats(childs_queue, &childs, &freeservers, &used,
&maxrequests);
ci_debug_printf(10,
"Server stats: \n\t Children: %d\n\t Free servers: %d\n"
"\tUsed servers:%d\n\tRequests served:%d\n",
childs, freeservers, used, maxrequests);
if (MAX_REQUESTS_PER_CHILD > 0 && (child_indx =
find_a_child_nrequests
(childs_queue,
MAX_REQUESTS_PER_CHILD)) >=
0) {
ci_debug_printf(8,
"Max requests reached for child :%d of pid :%d\n",
child_indx,
childs_queue->childs[child_indx].pid);
pid = start_child(LISTEN_SOCKET);
childs_queue->childs[child_indx].father_said = GRACEFULLY;
kill(childs_queue->childs[child_indx].pid, SIGTERM);
}
else if ((freeservers <= MIN_FREE_SERVERS && childs < MAX_CHILDS)
|| childs < START_CHILDS) {
ci_debug_printf(8,
"Free Servers: %d, children: %d. Going to start a child .....\n",
freeservers, childs);
pid = start_child(LISTEN_SOCKET);
}
else if (freeservers >= MAX_FREE_SERVERS &&
childs > START_CHILDS &&
(freeservers - START_SERVERS) > MIN_FREE_SERVERS) {
if ((child_indx = find_an_idle_child(childs_queue)) >= 0) {
childs_queue->childs[child_indx].father_said =
GRACEFULLY;
ci_debug_printf(8,
"Free Servers: %d, children: %d. Going to stop child %d(index: %d)\n",
freeservers, childs,
childs_queue->childs[child_indx].pid,
child_indx);
kill(childs_queue->childs[child_indx].pid, SIGTERM);
user_informed = 0;
}
}
else if (childs == MAX_CHILDS && freeservers < MIN_FREE_SERVERS) {
if(! user_informed) {
ci_debug_printf(1,
"ATTENTION!!!! Not enough available servers (children %d, free servers %d, used servers %d)!!!!! "
"Maybe you should increase the MaxServers and the "
"ThreadsPerChild values in c-icap.conf file!!!!!!!!!",childs , freeservers, used);
user_informed = 1;
}
}
if (c_icap_going_to_term)
break;
check_for_exited_childs();
if (c_icap_reconfigure) {
c_icap_reconfigure = 0;
if (!server_reconfigure()) {
ci_debug_printf(1, "Error while reconfiguring, exiting!\n");
break;
}
}
}
ci_debug_printf(1,
"Possibly a term signal received. Monitor process going to term all children\n");
kill_all_childs();
system_shutdown();
ci_debug_printf(1, "Exiting....\n");
return 1;
}
#else
child_data = (child_shared_data_t *) malloc(sizeof(child_shared_data_t));
child_data->pid = 0;
child_data->freeservers = START_SERVERS;
child_data->usedservers = 0;
child_data->requests = 0;
child_data->connections = 0;
child_data->to_be_killed = 0;
child_data->father_said = 0;
child_data->idle = 1;
child_main(LISTEN_SOCKET, 0);
#endif
return 1;
}
|