/* to start all exercisers */ int htxd_activate_all_ecg_devices(htxd_ecg_manager *this_ecg_manager) { htxd_ecg_info *p_current_ecg_info; int number_of_exercisers_to_run; struct htxshm_HE *p_HE; int i; HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_activate_all_ecg_devices"); p_current_ecg_info = this_ecg_manager->current_loading_ecg_info; /* number_of_exercisers_to_run = p_current_ecg_info->ecg_number_of_exercisers_to_run; */ number_of_exercisers_to_run = p_current_ecg_info->ecg_shm_exerciser_entries; p_HE = (struct htxshm_HE *)(p_current_ecg_info->ecg_shm_addr.hdr_addr + 1); /* skipping shm header and point to first HE */ for(i = 0; i < number_of_exercisers_to_run; i++) { htxd_load_exerciser(p_HE + i); } sleep(10); /* let all exercisers to start */ (p_current_ecg_info->ecg_shm_addr.hdr_addr)->started = 1; p_current_ecg_info->ecg_status = ECG_ACTIVE; this_ecg_manager->loaded_device_count += number_of_exercisers_to_run; HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_activate_all_ecg_devices"); return 0; }
/* select mdt command main function */ int htxd_option_method_select_mdt(char **result, htxd_command *p_command) { htxd *htxd_instance; htxd_ecg_info * p_ecg_info_list; htxd_ecg_info * p_ecg_info_to_select; char command_ecg_name[MAX_ECG_NAME_LENGTH]; htxd_ecg_manager *ecg_manager; char trace_str[512], cmd[128]; char temp_str[512]; HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_option_method_select_mdt"); /* htxd instance will be created only first time */ htxd_instance = htxd_get_instance(); *result = malloc(1024); ecg_manager = htxd_get_ecg_manager(); strcpy(command_ecg_name, p_command->ecg_name); if(htxd_instance->is_mdt_created == 0) { htxd_execute_shell_profile(); htxd_instance->is_mdt_created = 1; } if(htxd_instance->run_state == HTXD_DAEMON_STATE_SELECTED_MDT) { if(strcmp(command_ecg_name, ecg_manager->selected_ecg_name) == 0) { sprintf(*result, "Failed to select specified ecg/mdt(%s), same ecg/mdt is already selected", command_ecg_name); } else { sprintf(*result, "Failed to select specified ecg/mdt(%s), another ecg/mdt(%s) is already selected", command_ecg_name, ecg_manager->selected_ecg_name); } return 1; } if(htxd_instance->run_state == HTXD_DAEMON_STATE_RUNNING_MDT) { if(strcmp(command_ecg_name, ecg_manager->running_ecg_name) == 0) { sprintf(*result, "Failed to select specified ecg/mdt(%s), same ecg/mdt is being run", command_ecg_name); } else { sprintf(*result, "Failed to select specified ecg/mdt(%s), another ecg/mdt(%s) is being run", command_ecg_name, ecg_manager->running_ecg_name); } return 1; } sprintf(trace_str, "ECG name from command = <%s>", command_ecg_name); HTXD_TRACE(LOG_OFF, trace_str); if(command_ecg_name[0] == '\0') { sprintf(command_ecg_name, "%s/mdt/%s", global_htx_home_dir, DEFAULT_ECG_NAME); } if(htxd_is_file_exist(command_ecg_name) == FALSE) { sprintf(*result, "specified mdt(%s) is invalid", command_ecg_name); HTXD_TRACE(LOG_OFF, "htxd_is_file_exist() could not find the ECG file"); return 1; } if(ecg_manager == NULL) { HTXD_TRACE(LOG_OFF, "ecg manager is creating"); ecg_manager = create_ecg_manager(); if(ecg_manager == NULL) { HTXD_TRACE(LOG_OFF, "e_ecg_manager() failed to create ecg manager"); return 1; } htxd_instance->p_ecg_manager = ecg_manager; htxd_send_message ("HTX Daemon wakeup for responding", 0, HTX_SYS_INFO, HTX_SYS_MSG); } p_ecg_info_list = htxd_get_ecg_info_list(htxd_instance->p_ecg_manager); while(p_ecg_info_list != NULL) { if( strcmp(command_ecg_name, p_ecg_info_list->ecg_name) == 0) { sprintf(*result, "specified ECG (%s) is already running", command_ecg_name); HTXD_TRACE(LOG_OFF, "specified ECG is already running"); return 1; } p_ecg_info_list = p_ecg_info_list->ecg_info_next; } htxd_set_daemon_state(HTXD_DAEMON_STATE_SELECTING_MDT); /* copy run mdt to current mdt */ sprintf(temp_str, "cp %s %s/mdt/mdt ; sync", command_ecg_name, global_htx_home_dir); system(temp_str); /* update for camss mdt */ if( strstr(command_ecg_name, "camss") != NULL) { sprintf(trace_str, "updating camss mdt <%s>", command_ecg_name); HTXD_TRACE(LOG_OFF, trace_str); sprintf(cmd, "%s/etc/scripts/%s %s", global_htx_home_dir, CAMSS_MDT_UPDATE_SCRIPT, command_ecg_name); system(cmd); } /* execute run setup script for this MDT */ htxd_execute_run_setup_script(command_ecg_name); /* allocating required IPCs and initialize the value at shared memory*/ HTXD_TRACE(LOG_OFF, "run initializing ecg_info"); htxd_init_ecg_info(ecg_manager, command_ecg_name); /* start hxsmsg process if it is not already started */ if(htxd_get_htx_msg_pid() == 0) { sprintf(temp_str, "%s/%s", global_htx_home_dir, HTXD_AUTOSTART_FILE); if(htxd_is_file_exist(temp_str) == FALSE) { /* incase of bootme do not truncate error file */ htxd_truncate_error_file(); } htxd_truncate_message_file(); htxd_load_hxsmsg(htxd_instance->p_profile); HTXD_TRACE(LOG_ON, "run started htxsmsg process"); } strcpy(ecg_manager->selected_ecg_name, command_ecg_name); /* to DEBUG */ /* htxd_display_ecg_info_list(); htxd_display_exer_table(); */ p_ecg_info_to_select = ecg_manager->current_loading_ecg_info; if(p_ecg_info_to_select->ecg_exerciser_entries == 0){ sprintf(*result, "No device is present in ECG <%s>, could not start any device", command_ecg_name); } else if(p_ecg_info_to_select->ecg_shm_exerciser_entries == 0){ sprintf(*result, "No device is started under ECG <%s>, since all the devices are already loaded under another ECG(s)", command_ecg_name); } else if (p_ecg_info_to_select->ecg_exerciser_entries != p_ecg_info_to_select->ecg_shm_exerciser_entries) { sprintf(*result, "ECG (%s) is slected\nWARNING: few devices are unable to start since they may be already loaded under another ECG(s)", command_ecg_name); } else { sprintf(*result, "ECG (%s) is selected", command_ecg_name); sprintf(trace_str, "date +\"ECG (%s) was selected on %%x at %%X %%Z\" >>%s/%s", command_ecg_name, global_htxd_log_dir, HTXD_START_STOP_LOG); system(trace_str); } htxd_send_message (*result, 0, HTX_SYS_INFO, HTX_SYS_MSG); HTXD_TRACE(LOG_ON, *result); htxd_set_daemon_state(HTXD_DAEMON_STATE_SELECTED_MDT); HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_option_method_select_mdt"); return 0; }
/* run command main function */ int htxd_option_method_run_mdt(char **result, htxd_command *p_command) { htxd *htxd_instance; htxd_ecg_info * p_ecg_info_list; htxd_ecg_info * p_ecg_info_to_run; char command_ecg_name[MAX_ECG_NAME_LENGTH]; htxd_ecg_manager *ecg_manager; char trace_str[512], cmd[128]; int return_code; char temp_str[512]; HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_option_method_run_mdt"); /* htxd instance will be created only first time */ htxd_instance = htxd_get_instance(); strcpy(command_ecg_name, p_command->ecg_name); sprintf(trace_str, "ECG name from command = <%s>", command_ecg_name); HTXD_TRACE(LOG_OFF, trace_str); htxd_ecg_shutdown_flag = FALSE; ecg_manager = htxd_get_ecg_manager(); if(command_ecg_name[0] == '\0') { sprintf(command_ecg_name, "%s/mdt/%s", global_htx_home_dir, DEFAULT_ECG_NAME); } if(htxd_instance->is_mdt_created == 0) { htxd_execute_shell_profile(); htxd_instance->is_mdt_created = 1; } *result = malloc(1024); if(htxd_is_file_exist(command_ecg_name) == FALSE) { sprintf(*result, "specified mdt(%s) is invalid", command_ecg_name); HTXD_TRACE(LOG_OFF, "htxd_is_file_exist() could not find the ECG file"); return 1; } if(ecg_manager == NULL) { HTXD_TRACE(LOG_OFF, "ecg manager is creating"); ecg_manager = create_ecg_manager(); if(ecg_manager == NULL) { HTXD_TRACE(LOG_OFF, "e_ecg_manager() failed to create ecg manager"); return 1; } htxd_instance->p_ecg_manager = ecg_manager; } /* start hxsmsg process if it is not already started */ if(htxd_get_htx_msg_pid() == 0) { sprintf(temp_str, "%s/%s", global_htx_home_dir, HTXD_AUTOSTART_FILE); if(htxd_is_file_exist(temp_str) == FALSE) { /* incase of bootme do not truncate error file */ htxd_truncate_error_file(); } htxd_truncate_message_file(); htxd_load_hxsmsg(htxd_instance->p_profile); HTXD_TRACE(LOG_ON, "run started htxsmsg process"); htxd_send_message ("HTX Daemon wakeup for responding", 0, HTX_SYS_INFO, HTX_SYS_MSG); } if(htxd_is_daemon_selected() != TRUE) { p_ecg_info_list = htxd_get_ecg_info_list(htxd_instance->p_ecg_manager); while(p_ecg_info_list != NULL) { if( strcmp(command_ecg_name, p_ecg_info_list->ecg_name) == 0) { sprintf(*result, "specified ECG (%s) is already running", command_ecg_name); HTXD_TRACE(LOG_OFF, "specified ECG is already running"); return 1; } p_ecg_info_list = p_ecg_info_list->ecg_info_next; } sprintf(trace_str, "start activating MDT <%s>", command_ecg_name); htxd_send_message (trace_str, 0, HTX_SYS_INFO, HTX_SYS_MSG); HTXD_TRACE(LOG_ON, trace_str); /* copy run mdt to current mdt */ sprintf(temp_str, "cp %s %s/mdt/mdt ; sync", command_ecg_name, global_htx_home_dir); system(temp_str); /* update for camss mdt */ if( strstr(command_ecg_name, "camss") != NULL) { sprintf(trace_str, "updating camss mdt <%s>", command_ecg_name); HTXD_TRACE(LOG_OFF, trace_str); sprintf(cmd, "%s/etc/scripts/%s %s", global_htx_home_dir, CAMSS_MDT_UPDATE_SCRIPT, command_ecg_name); system(cmd); } /* execute run setup script for this MDT */ htxd_execute_run_setup_script(command_ecg_name); /* allocating required IPCs and initialize the value at shared memory*/ HTXD_TRACE(LOG_OFF, "run initializing ecg_info"); htxd_init_ecg_info(ecg_manager, command_ecg_name); } else { if(strcmp(command_ecg_name, ecg_manager->selected_ecg_name) != 0) { sprintf(*result, "Failed to run specified ecg/mdt (%s), another ecg/mdt (%s) is already selected", command_ecg_name, ecg_manager->selected_ecg_name); return 1; } ecg_manager->selected_ecg_name[0] = '\0'; } htxd_set_daemon_state(HTXD_DAEMON_STATE_STARTING_MDT); ecg_manager->current_loading_ecg_info->ecg_run_start_time = (int) time((time_t *) 0); /* initializing syscfg */ if(htxd_is_init_syscfg() != TRUE) { return_code = init_syscfg(); if (return_code != 0) { sprintf(*result, "Internal error: failed to initialize syscfg with error code <%d>", return_code); HTXD_TRACE(LOG_ON, *result); return return_code; } htxd_set_init_syscfg_flag(TRUE); } /* start all devices in the ecg */ HTXD_TRACE(LOG_OFF, "run activating all devices under the ECG"); htxd_activate_all_ecg_devices(ecg_manager); /* start hxstats process if it is not already started */ if(htxd_get_htx_stats_pid() == 0) { htxd_load_hxstats(); HTXD_TRACE(LOG_ON, "run started htxstats process"); } #ifdef __HTXD_DR__ /* start DR child process */ if(htxd_get_dr_child_pid() == 0) { htxd_start_DR_child(); HTXD_TRACE(LOG_ON, "run started DR child process"); } #endif #ifdef __HTX_LINUX__ /* start hotplug monitor */ if( htxd_is_hotplug_monitor_initialized() != TRUE && htxd_get_equaliser_offline_cpu_flag() != 1) { htxd_start_hotplug_monitor(&(htxd_instance->p_hotplug_monitor_thread)); HTXD_TRACE(LOG_ON, "run started hotplug monitor"); } #endif /* start equaliser process */ if( htxd_check_for_equaliser_start(ecg_manager) == TRUE) { htxd_start_equaliser(); HTXD_TRACE(LOG_ON, "run started equaliser process"); } /* start hang monitor thread */ if(htxd_is_hang_monitor_initialized() != TRUE) { htxd_start_hang_monitor(&(htxd_instance->p_hang_monitor_thread)); HTXD_TRACE(LOG_ON, "run started hang monitor thread"); } /* start stop_watch monitor thread */ if(htxd_is_stop_watch_monitor_initialized() != TRUE) { htxd_start_stop_watch_monitor(&(htxd_instance->stop_watch_monitor_thread)); HTXD_TRACE(LOG_ON, "run started stop watch monitor thread"); } htxd_set_test_running_state(HTXD_TEST_ACTIVATED); strcpy(ecg_manager->running_ecg_name, command_ecg_name); /* to DEBUG */ /* htxd_display_ecg_info_list(); htxd_display_exer_table(); */ p_ecg_info_to_run = ecg_manager->current_loading_ecg_info; if(p_ecg_info_to_run->ecg_exerciser_entries == 0){ sprintf(*result, "No device is present in ECG <%s>, could not start any device", command_ecg_name); } else if(p_ecg_info_to_run->ecg_shm_exerciser_entries == 0){ sprintf(*result, "No device is started under ECG <%s>, since all the devices are already loaded under another ECG(s)", command_ecg_name); } else if (p_ecg_info_to_run->ecg_exerciser_entries != p_ecg_info_to_run->ecg_shm_exerciser_entries) { sprintf(*result, "ECG (%s) Activated\nWARNING: few devices are unable to start since they may be already loaded under another ECG(s)", command_ecg_name); } else { sprintf(*result, "ECG (%s) Activated.", command_ecg_name); sprintf(trace_str, "date +\"ECG (%s) was activated on %%x at %%X %%Z\" >>%s/%s", command_ecg_name, global_htxd_log_dir, HTXD_START_STOP_LOG); system(trace_str); } /* have to set shm key to system header shared memory so that htx stats is able to connect */ htxd_set_system_header_info_shm_with_current_shm_key(p_ecg_info_to_run->ecg_shm_key); htxd_send_message (*result, 0, HTX_SYS_INFO, HTX_SYS_MSG); htxd_set_daemon_state(HTXD_DAEMON_STATE_RUNNING_MDT); HTXD_TRACE(LOG_ON, *result); HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_option_method_run_mdt"); return 0; }
/* load exerciser : start exerciser process */ int htxd_load_exerciser(struct htxshm_HE *p_HE) { int exerciser_pid; int temp_int; char exerciser_path[512]; char exerciser_name[64]; char device_name[64]; char run_mode[64]; char rule_path[64]; int emc_mode; char trace_str[256]; HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_load_exerciser"); sprintf(trace_str, "loading exerciser <%s>", p_HE->sdev_id); HTXD_TRACE(LOG_OFF, trace_str); exerciser_pid = htxd_create_child_process(); switch(exerciser_pid) { case 0: setsid(); temp_int = p_HE->priority; nice(temp_int); sleep(5); /* let daemon update shared memory */ strcpy(exerciser_name, p_HE->HE_name); if (strcmp(p_HE->HE_name, "hxemem64") == 0) { putenv("CORE_NOSHM=true"); } if (strcmp(p_HE->HE_name, "hxepowermixer") == 0) { putenv("MEMORY_AFFINITY=MCM"); } strcpy(exerciser_path, global_htx_home_dir); strcat(exerciser_path, "/bin/"); strcat(exerciser_path, exerciser_name); strcpy(device_name, "/dev/"); strcat(device_name, p_HE->sdev_id); emc_mode = htxd_get_emc_mode(); if(emc_mode == 1) { strcpy(run_mode, "EMC"); } else { strcpy(run_mode, "REG"); } if(emc_mode == 1) { if(p_HE->emc_rules[0] != '/') { sprintf(rule_path, "%s/rules/emc/%s", global_htx_home_dir, p_HE->emc_rules); } else { strcpy(rule_path, p_HE->emc_rules); } } else { if(p_HE->reg_rules[0] != '/') { sprintf(rule_path, "%s/rules/reg/%s", global_htx_home_dir, p_HE->reg_rules); } else { strcpy(rule_path, p_HE->reg_rules); } } /* system("export EXTSHM=OFF"); */ unsetenv("EXTSHM"); if ( (execl(exerciser_path, exerciser_name, device_name, run_mode, rule_path, (char *) 0) ) == -1) { sprintf(trace_str, "execl() failed exerciser_path <%s> exerciser_name <%s> errno = <%d>\n", exerciser_path, exerciser_name, errno); htxd_send_message (trace_str, 0, HTX_SYS_SOFT_ERROR, HTX_SYS_MSG); HTXD_TRACE(LOG_ON, trace_str); exit(-1); } case -1: sprintf(trace_str, "exerciser <%s> fork failed with error <%d>", p_HE->sdev_id, errno); HTXD_TRACE(LOG_ON, trace_str); return -1; default: p_HE->PID = exerciser_pid; htxd_update_exer_pid_in_exer_list(htxd_get_exer_table(), p_HE->sdev_id, exerciser_pid); sprintf(trace_str, "exerciser <%s> forked with PID <%d>", p_HE->sdev_id, p_HE->PID); HTXD_TRACE(LOG_OFF, trace_str); break; } htxd_reset_FD_close_on_exec_flag(); HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_load_exerciser"); return 0; }
/* daemon live here, receives command, process it, send back result */ int htxd_start_daemon(htxd *htxd_instance) { int result = 0; int socket_fd; struct sockaddr_in local_address; struct sockaddr_in client_address; socklen_t address_length; int client_socket_fd; htxd_thread command_thread; HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_start_daemon"); init_option_list(); htxd_launch_autostart(); if(htxd_is_profile_initialized(htxd_instance) != TRUE) { HTXD_TRACE(LOG_ON, "initialize HTX profile details"); htxd_init_profile(&(htxd_instance->p_profile)); /* htxd_display_profile(htxd_instance->p_profile); */ /* To DEBUG */ } socket_fd = htxd_create_socket(); result = htxd_set_socket_option(socket_fd); local_address.sin_family = AF_INET; local_address.sin_port = htons (htxd_instance->port_number); local_address.sin_addr.s_addr = INADDR_ANY; memset (&(local_address.sin_zero), '\0', 8); result = htxd_bind_socket(socket_fd, &local_address, htxd_instance->port_number); result = htxd_listen_socket(socket_fd); HTXD_TRACE(LOG_ON, "starting daemon main loop"); do /* this loop make the daemon live */ { if(htxd_shutdown_flag == TRUE) { break; } client_socket_fd = htxd_accept_connection(socket_fd, &client_address, &address_length); if(client_socket_fd == -1) { if(htxd_shutdown_flag == TRUE) { break; } HTXD_TRACE(LOG_ON, "htxd_accept_connection returned -1, ignoring and continue..."); continue; } HTXD_TRACE(LOG_OFF, "found a command for receiving"); memset(&command_thread, 0, sizeof(htxd_thread)); command_thread.thread_function = htxd_command_thraead_handler; command_thread.thread_data = malloc(sizeof(int)); (*((int *)command_thread.thread_data)) = client_socket_fd; command_thread.thread_stack_size = 10000000; htxd_create_command_thread(&command_thread); } while(htxd_shutdown_flag == FALSE); htxd_shutdown_all_mdt(); HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_start_daemon"); return result; }
/* allocating IPC resources required for the new ECG */ int htxd_init_ecg_info(htxd_ecg_manager *this_ecg_manager, char *new_ecg_name) { int number_of_devices; int max_number_of_devices; char error_msg[1024]; htxd_ecg_info *p_current_ecg_info; int next_key_offset; char temp_string[256]; HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_init_ecg_info"); sprintf(temp_string, "start processing ECG <%s>", new_ecg_name); HTXD_TRACE(LOG_OFF, temp_string); /* htxd_send_message (temp_string, 0, HTX_SYS_INFO, HTX_SYS_MSG); */ htxd_allocate_ecg_info(this_ecg_manager); number_of_devices = htxd_get_number_of_device(new_ecg_name, error_msg); max_number_of_devices = number_of_devices + EXTRA_DEVICE_ENTRIES; p_current_ecg_info = this_ecg_manager->current_loading_ecg_info; p_current_ecg_info->ecg_info_next = NULL; p_current_ecg_info->ecg_exerciser_entries = number_of_devices; p_current_ecg_info->ecg_max_exerciser_entries = max_number_of_devices; p_current_ecg_info->ecg_shm_exerciser_entries = 0; strcpy( p_current_ecg_info->ecg_name, new_ecg_name); p_current_ecg_info->ecg_status = ECG_UNLOADED; next_key_offset = htxd_get_next_key_offset(this_ecg_manager); p_current_ecg_info->ecg_shm_key = ECG_SHMKEY_START + next_key_offset; p_current_ecg_info->ecg_sem_key = ECG_SEMKEY_START + next_key_offset; HTXD_TRACE(LOG_OFF, "allocationg semaphore for ecg_info"); /* allocating semaphore */ if( (p_current_ecg_info->ecg_sem_id = htxd_init_sem(p_current_ecg_info->ecg_sem_key, max_number_of_devices) ) == -1 ) { return -1; } HTXD_TRACE(LOG_OFF, "allocationg shared memory for ecg_info"); /* allocating shared memory */ if( ( p_current_ecg_info->ecg_shm_addr.hdr_addr = htxd_init_shm(p_current_ecg_info->ecg_shm_key, max_number_of_devices,&(p_current_ecg_info->ecg_shm_id) ) ) == NULL ) { return -1; } /* setting shared memory with header values */ HTXD_TRACE(LOG_OFF, "setting shm header values"); htxd_set_shm_header_values(this_ecg_manager); /* setting shared memory with HE values */ HTXD_TRACE(LOG_OFF, "setting shm HE values"); htxd_set_shm_exercisers_values(this_ecg_manager, new_ecg_name); /* initialize equaliser info */ HTXD_TRACE(LOG_OFF, "initialize equaliser_info"); htxd_init_equaliser_info(p_current_ecg_info); /* initialize start-halted mode */ htxd_init_start_halted_exerciser_mode(p_current_ecg_info); p_current_ecg_info->ecg_status = ECG_INACTIVE; p_current_ecg_info->ecg_shm_addr.hdr_addr->num_entries = p_current_ecg_info->ecg_shm_exerciser_entries; (this_ecg_manager->ecg_list_length)++; HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_init_ecg_info"); return 0; }
/* daemon live here, receives command, process it, send back result */ int htxd_start_daemon(htxd *htxd_instance) { int result = 0; int socket_fd; struct sockaddr_in local_address; struct sockaddr_in client_address; socklen_t address_length; int new_fd; char * command_buffer = NULL; char * command_result = NULL; int command_return_code = 0; char trace_string[256]; HTXD_FUNCTION_TRACE(FUN_ENTRY, "htxd_start_daemon"); init_option_list(); htxd_autostart(htxd_instance); /* try autostart if find the autostart flag file */ socket_fd = htxd_create_socket(); result = htxd_set_socket_option(socket_fd); local_address.sin_family = AF_INET; local_address.sin_port = htons (htxd_instance->port_number); local_address.sin_addr.s_addr = INADDR_ANY; memset (&(local_address.sin_zero), '\0', 8); result = htxd_bind_socket(socket_fd, &local_address, htxd_instance->port_number); result = htxd_listen_socket(socket_fd); HTXD_TRACE(LOG_ON, "starting daemon main loop"); do /* this loop make the daemon live */ { do /* this loop listens for incomming messages */ { HTXD_TRACE(LOG_OFF, "daemon wating for command"); result = htxd_select(socket_fd); if(htxd_shutdown_flag == TRUE) { break; } }while( (result == -1) && (errno == EINTR) ); if(htxd_shutdown_flag == TRUE) { break; } new_fd = htxd_accept_connection(socket_fd, &client_address, &address_length); if(new_fd == -1) { if(htxd_shutdown_flag == TRUE) { break; } HTXD_TRACE(LOG_OFF, "select time out"); continue; } HTXD_TRACE(LOG_OFF, "found a command for receiving"); if(htxd_is_profile_initialized(htxd_instance) != TRUE) { HTXD_TRACE(LOG_ON, "initialize HTX profile details"); htxd_init_profile(&(htxd_instance->p_profile)); /* htxd_display_profile(htxd_instance->p_profile); */ /* To DEBUG */ register_signal_handlers(); } /* receive the incomming command */ HTXD_TRACE(LOG_OFF, "daemon receiving command"); command_buffer = htxd_receive_command(new_fd); if(command_buffer == NULL) { return -1; } HTXD_TRACE(LOG_OFF, "command received start<<"); HTXD_TRACE(LOG_OFF, command_buffer); HTXD_TRACE(LOG_OFF, ">> command received end"); htxd_update_command_object(command_buffer); if(command_buffer != NULL) { free(command_buffer); command_buffer = NULL; } /* process the received command */ HTXD_TRACE(LOG_OFF, "daemon start processing command"); command_return_code = htxd_process_command(&command_result); /* handling if command did not generate result buffer */ if(command_result == NULL) { command_result = malloc(HTX_ERR_MESSAGE_LENGTH); if(command_result == NULL) { sprintf(trace_string, "Error : malloc(%d) failed with errno = <%d> while allocating error message", HTX_ERR_MESSAGE_LENGTH, errno); HTXD_TRACE(LOG_ON, trace_string); exit(1); } strcpy(command_result, "No result is generated by the command"); } HTXD_TRACE(LOG_OFF, "command result start<<"); HTXD_TRACE(LOG_OFF, command_result); HTXD_TRACE(LOG_OFF, ">> command result end"); /* send back command result to client */ HTXD_TRACE(LOG_OFF, "daemon sending the result to client"); result = htxd_send_response(new_fd, command_result, command_return_code); if(result == -1) { return result; } if(command_result != 0) { free(command_result); } close(new_fd); } while(htxd_shutdown_flag == FALSE); if(htxd_get_ecg_list_length(htxd_instance->p_ecg_manager) > 0) { /* shutdown all running ecgs and return */ // htxd_shutdown_all_running_ecgs(); } htxd_shutdown_all_mdt(); HTXD_FUNCTION_TRACE(FUN_EXIT, "htxd_start_daemon"); return result; }