/*+++++++++++++++++++++++++ get_afd_config_value() ++++++++++++++++++++++*/ static void get_afd_config_value(void) { char *buffer, config_file[MAX_PATH_LENGTH]; (void)snprintf(config_file, MAX_PATH_LENGTH, "%s%s%s", p_work_dir, ETC_DIR, AFD_CONFIG_FILE); if ((eaccess(config_file, F_OK) == 0) && (read_file_no_cr(config_file, &buffer, YES, __FILE__, __LINE__) != INCORRECT)) { char value[MAX_INT_LENGTH]; if (get_definition(buffer, ARCHIVE_WATCH_PRIORITY_DEF, value, MAX_INT_LENGTH) != NULL) { if (setpriority(PRIO_PROCESS, 0, atoi(value)) == -1) { system_log(WARN_SIGN, __FILE__, __LINE__, "Failed to set priority to %d : %s", atoi(value), strerror(errno)); } } free(buffer); } return; }
/*############################ check_changes() ##########################*/ void check_changes(FILE *p_data) { static int old_amg_status = PROC_INIT_VALUE, old_archive_watch_status = PROC_INIT_VALUE, old_fd_status = PROC_INIT_VALUE, old_max_connections; static unsigned int old_sys_log_ec; static time_t next_stat_time, old_st_mtime; static char old_receive_log_history[MAX_LOG_HISTORY], old_sys_log_history[MAX_LOG_HISTORY], old_trans_log_history[MAX_LOG_HISTORY]; register int i; time_t now; if (check_fsa(YES, AFDD) == YES) { int loop_counter = 0, status; retry_check: if (old_error_history != NULL) { FREE_RT_ARRAY(old_error_history); old_error_history = NULL; } if (check_fsa(YES, AFDD) == YES) { loop_counter++; if (loop_counter < 10) { system_log(DEBUG_SIGN, __FILE__, __LINE__, _("Hmm, FSA has changed again!")); my_usleep(500000L); goto retry_check; } } status = 0; while (p_afd_status->amg_jobs & WRITTING_JID_STRUCT) { (void)my_usleep(100000L); status++; if ((status > 1) && ((status % 100) == 0)) { system_log(INFO_SIGN, __FILE__, __LINE__, _("AFDD: Timeout arrived for waiting for AMG to finish writting to JID structure.")); } } RT_ARRAY(old_error_history, no_of_hosts, ERROR_HISTORY_LENGTH, unsigned char); for (i = 0; i < no_of_hosts; i++) { if (fsa[i].real_hostname[0][0] == GROUP_IDENTIFIER) { (void)memset(old_error_history[i], 0, ERROR_HISTORY_LENGTH); } else { (void)memcpy(old_error_history[i], fsa[i].error_history, ERROR_HISTORY_LENGTH); } } host_config_counter = (int)*(unsigned char *)((char *)fsa - AFD_WORD_OFFSET + SIZEOF_INT); show_host_list(p_data); show_job_list(p_data); } else { if (host_config_counter != (int)*(unsigned char *)((char *)fsa - AFD_WORD_OFFSET + SIZEOF_INT)) { FREE_RT_ARRAY(old_error_history); RT_ARRAY(old_error_history, no_of_hosts, ERROR_HISTORY_LENGTH, unsigned char); for (i = 0; i < no_of_hosts; i++) { if (fsa[i].real_hostname[0][0] == GROUP_IDENTIFIER) { (void)memset(old_error_history[i], 0, ERROR_HISTORY_LENGTH); } else { (void)memcpy(old_error_history[i], fsa[i].error_history, ERROR_HISTORY_LENGTH); } } host_config_counter = (int)*(unsigned char *)((char *)fsa - AFD_WORD_OFFSET + SIZEOF_INT); show_host_list(p_data); } } if (check_fra(YES) == YES) { show_dir_list(p_data); } /* * It costs too much system performance to constantly stat() * the AFD_CONFIG file to see if the modification time has * changed. For this reason lets only stat() this file at a * reasonable interval of say STAT_INTERVAL seconds. */ now = time(NULL); if (next_stat_time < now) { struct stat stat_buf; next_stat_time = now + STAT_INTERVAL; if (stat(afd_config_file, &stat_buf) == 0) { if (stat_buf.st_mtime != old_st_mtime) { char *buffer; old_st_mtime = stat_buf.st_mtime; if ((eaccess(afd_config_file, F_OK) == 0) && (read_file_no_cr(afd_config_file, &buffer, YES, __FILE__, __LINE__) != INCORRECT)) { int max_connections = 0; char value[MAX_INT_LENGTH]; if (get_definition(buffer, MAX_CONNECTIONS_DEF, value, MAX_INT_LENGTH) != NULL) { max_connections = atoi(value); } if ((max_connections < 1) || (max_connections > MAX_CONFIGURABLE_CONNECTIONS)) { max_connections = MAX_DEFAULT_CONNECTIONS; } if (max_connections != old_max_connections) { old_max_connections = max_connections; (void)fprintf(p_data, "MC %d\r\n", old_max_connections); } free(buffer); } } } else { if (errno != ENOENT) { system_log(DEBUG_SIGN, __FILE__, __LINE__, _("Failed to stat() `%s' : %s"), afd_config_file, strerror(errno)); } } } if (old_sys_log_ec != p_afd_status->sys_log_ec) { char buf[LOG_FIFO_SIZE + 1]; old_sys_log_ec = p_afd_status->sys_log_ec; for (i = 0; i < LOG_FIFO_SIZE; i++) { buf[i] = p_afd_status->sys_log_fifo[i] + ' '; } buf[i] = '\0'; (void)fprintf(p_data, "SR %u %s\r\n", old_sys_log_ec, buf); } if (memcmp(old_receive_log_history, p_afd_status->receive_log_history, MAX_LOG_HISTORY) != 0) { char buf[MAX_LOG_HISTORY + 1]; (void)memcpy(old_receive_log_history, p_afd_status->receive_log_history, MAX_LOG_HISTORY); for (i = 0; i < MAX_LOG_HISTORY; i++) { buf[i] = p_afd_status->receive_log_history[i] + ' '; } buf[i] = '\0'; (void)fprintf(p_data, "RH %s\r\n", buf); } if (memcmp(old_sys_log_history, p_afd_status->sys_log_history, MAX_LOG_HISTORY) != 0) { char buf[MAX_LOG_HISTORY + 1]; (void)memcpy(old_sys_log_history, p_afd_status->sys_log_history, MAX_LOG_HISTORY); for (i = 0; i < MAX_LOG_HISTORY; i++) { buf[i] = p_afd_status->sys_log_history[i] + ' '; } buf[i] = '\0'; (void)fprintf(p_data, "SH %s\r\n", buf); } if (memcmp(old_trans_log_history, p_afd_status->trans_log_history, MAX_LOG_HISTORY) != 0) { char buf[MAX_LOG_HISTORY + 1]; (void)memcpy(old_trans_log_history, p_afd_status->trans_log_history, MAX_LOG_HISTORY); for (i = 0; i < MAX_LOG_HISTORY; i++) { buf[i] = p_afd_status->trans_log_history[i] + ' '; } buf[i] = '\0'; (void)fprintf(p_data, "TH %s\r\n", buf); } for (i = 0; i < no_of_hosts; i++) { if (fsa[i].real_hostname[0][0] != GROUP_IDENTIFIER) { if (memcmp(old_error_history[i], fsa[i].error_history, ERROR_HISTORY_LENGTH) != 0) { int k; (void)memcpy(old_error_history[i], fsa[i].error_history, ERROR_HISTORY_LENGTH); (void)fprintf(p_data, "EL %d %d", i, old_error_history[i][0]); for (k = 1; k < ERROR_HISTORY_LENGTH; k++) { (void)fprintf(p_data, " %d", old_error_history[i][k]); } (void)fprintf(p_data, "\r\n"); } } } /* * Check if status of any of the main process (AMG, FD and * archive_watch) have changed. */ if (old_amg_status != p_afd_status->amg) { old_amg_status = p_afd_status->amg; (void)fprintf(p_data, "AM %d\r\n", old_amg_status); } if (old_fd_status != p_afd_status->fd) { old_fd_status = p_afd_status->fd; (void)fprintf(p_data, "FD %d\r\n", old_fd_status); } if (old_archive_watch_status != p_afd_status->archive_watch) { old_archive_watch_status = p_afd_status->archive_watch; (void)fprintf(p_data, "AW %d\r\n", old_archive_watch_status); } (void)fflush(p_data); return; }
/*++++++++++++++++++++++++++ get_event_reason() +++++++++++++++++++++++++*/ static void get_event_reason(char *reason_str, char *host_alias) { int fd, i, max_event_log_files, type; char log_file[MAX_PATH_LENGTH], *p_log_file, *ptr, *src; struct stat stat_buf; /* Always reset reason_str, so we do not display incoreect data. */ reason_str[0] = '\0'; /* Get the maximum event log file number. */ (void)sprintf(log_file, "%s%s%s", p_work_dir, ETC_DIR, AFD_CONFIG_FILE); if ((eaccess(log_file, F_OK) == 0) && (read_file_no_cr(log_file, &src, __FILE__, __LINE__) != INCORRECT)) { char value[MAX_INT_LENGTH + 1]; if (get_definition(src, MAX_EVENT_LOG_FILES_DEF, value, MAX_INT_LENGTH) != NULL) { max_event_log_files = atoi(value); } else { max_event_log_files = MAX_EVENT_LOG_FILES; } free(src); } else { max_event_log_files = MAX_EVENT_LOG_FILES; } /* Prepare log file name. */ p_log_file = log_file; p_log_file += sprintf(log_file, "%s%s/%s", p_work_dir, LOG_DIR, EVENT_LOG_NAME); for (i = 0; i < max_event_log_files; i++) { (void)sprintf(p_log_file, "%d", i); if (stat(log_file, &stat_buf) < 0) { if (errno == ENOENT) { /* For some reason the file is not there. So lets */ /* assume we have found nothing. */; } else { (void)xrec(WARN_DIALOG, "Failed to stat() %s : %s (%s %d)", log_file, strerror(errno), __FILE__, __LINE__); } return; } if (stat_buf.st_size == 0) { return; } if ((fd = open(log_file, O_RDONLY)) == -1) { (void)xrec(FATAL_DIALOG, "Failed to open() %s : %s (%s %d)", log_file, strerror(errno), __FILE__, __LINE__); return; } #ifdef HAVE_MMAP if ((src = mmap(0, stat_buf.st_size, PROT_READ, (MAP_FILE | MAP_SHARED), fd, 0)) == (caddr_t) -1) { (void)xrec(FATAL_DIALOG, "Failed to mmap() %s : %s (%s %d)", log_file, strerror(errno), __FILE__, __LINE__); (void)close(fd); return; } #else if ((src = malloc(stat_buf.st_size)) == NULL) { (void)xrec(FATAL_DIALOG, "malloc() error : %s (%s %d)", strerror(errno), __FILE__, __LINE__); (void)close(fd); return; } if (read(fd, src, stat_buf.st_size) != stat_buf.st_size) { (void)xrec(FATAL_DIALOG, "Failed to read() from %s : %s (%s %d)", log_file, strerror(errno), __FILE__, __LINE__); free(src); (void)close(fd); return; } #endif if (close(fd) == -1) { system_log(DEBUG_SIGN, __FILE__, __LINE__, "close() error : %s", strerror(errno)); } ptr = src + stat_buf.st_size - 2; /* * Lets first search for an EA_OFFLINE or EA_ACKNOWLEDGE event * for this host. */ while (ptr > src) { while ((ptr > src) && (*ptr != '\n')) { ptr--; } if ((ptr > src) && (*ptr == '\n') && (*(ptr + 1) != ' ')) { ptr++; HEX_CHAR_TO_INT((*(ptr + LOG_DATE_LENGTH + 1))); if (type == EC_HOST) { HEX_CHAR_TO_INT((*(ptr + LOG_DATE_LENGTH + 3))); if (type == ET_MAN) { int k; unsigned int event_action_no; char str_number[MAX_INT_LENGTH + 1], *tmp_ptr; tmp_ptr = ptr; ptr += LOG_DATE_LENGTH + 5; k = 0; do { str_number[k] = *(ptr + k); k++; } while ((*(ptr + k) != SEPARATOR_CHAR) && (*(ptr + k) != '\n') && (k < MAX_INT_LENGTH)); str_number[k] = '\0'; event_action_no = (unsigned int)strtoul(str_number, NULL, 16); if ((event_action_no == EA_ACKNOWLEDGE) || (event_action_no == EA_OFFLINE)) { ptr += k; if (*ptr == SEPARATOR_CHAR) { ptr++; k = 0; while ((*(ptr + k) != SEPARATOR_CHAR) && (*(ptr + k) != '\n') && (*(ptr + k) == *(host_alias + k))) { k++; } if (((*(ptr + k) == SEPARATOR_CHAR) || (*(ptr + k) == '\n')) && (*(host_alias + k) == '\0')) { ptr += k; if (*ptr == SEPARATOR_CHAR) { ptr++; k = 0; /* Store user. */ while ((*(ptr + k) != SEPARATOR_CHAR) && (*(ptr + k) != '\n')) { *(reason_str + k) = *(ptr + k); k++; } if (*(ptr + k) == SEPARATOR_CHAR) { int j; *(reason_str + k) = '\n'; k += 1; j = k; while (*(ptr + k) != '\n') { if (*(ptr + k) == '%') { char hex_char[3]; hex_char[0] = *(ptr + k + 1); hex_char[1] = *(ptr + k + 2); hex_char[2] = '\0'; *(reason_str + j) = (char)strtol(hex_char, NULL, 16); k += 3; j += 1; } else { *(reason_str + j) = *(ptr + k); j++; k++; } } *(reason_str + j) = '\0'; } else { *(reason_str + k) = '\0'; } /* Free all memory we have allocated. */ #ifdef HAVE_MMAP if (munmap(src, stat_buf.st_size) < 0) { (void)xrec(ERROR_DIALOG, "munmap() error : %s (%s %d)", strerror(errno), __FILE__, __LINE__); } #else free(src); #endif return; } else { ptr = tmp_ptr - 2; } } else { ptr = tmp_ptr - 2; } } else { ptr = tmp_ptr - 2; } } else { ptr = tmp_ptr - 2; } } else { ptr -= 2; } } else { ptr -= 2; } } else { ptr--; } } /* Free all memory we have allocated. */ #ifdef HAVE_MMAP if (munmap(src, stat_buf.st_size) < 0) { (void)xrec(ERROR_DIALOG, "munmap() error : %s (%s %d)", strerror(errno), __FILE__, __LINE__); } #else free(src); #endif } return; }
/*######################### check_fake_user() ###########################*/ void check_fake_user(int *argc, char *argv[], char *config_file, char *fake_user) { register int i; char wanted_user[MAX_FULL_USER_ID_LENGTH]; wanted_user[0] = 1; for (i = 1; i < *argc; i++) { if (CHECK_STRCMP(argv[i], "-u") == 0) { if (((i + 1) < *argc) && (argv[i + 1][0] != '-')) { /* Check if the buffer is long enough! */ if (MAX_FULL_USER_ID_LENGTH < strlen(argv[i + 1])) { (void)fprintf(stderr, _("Buffer for storing fake user to short. (%s %d)\n"), __FILE__, __LINE__); fake_user[0] = '\0'; return; } (void)strcpy(wanted_user, argv[i + 1]); if ((i + 2) < *argc) { register int j; for (j = i; j < *argc; j++) { argv[j] = argv[j + 2]; } argv[j] = NULL; } else { argv[i] = NULL; } *argc -= 2; i = *argc; } else { /* No fake user supplied, so lets take it from */ /* config file. */ wanted_user[0] = '\0'; if ((i + 1) < *argc) { register int j; for (j = i; j < *argc; j++) { argv[j] = argv[j + 1]; } argv[j] = NULL; } else { argv[i] = NULL; } *argc -= 1; i = *argc; } break; } } if (wanted_user[0] != 1) { struct passwd *pwd; if ((pwd = getpwuid(getuid())) != NULL) { char *buffer = NULL, full_config_name[MAX_PATH_LENGTH]; (void)snprintf(full_config_name, MAX_PATH_LENGTH, "%s%s%s", p_work_dir, ETC_DIR, config_file); if ((eaccess(full_config_name, F_OK) == 0) && (read_file_no_cr(full_config_name, &buffer, YES, __FILE__, __LINE__) != INCORRECT)) { char fake_user_list[MAX_PATH_LENGTH]; if (get_definition(buffer, FAKE_USER_DEF, fake_user_list, MAX_PATH_LENGTH) != NULL) { size_t length; length = strlen(pwd->pw_name); if (length > 0) { int change_char; char real_user[MAX_FULL_USER_ID_LENGTH + 2], *ptr, *tmp_ptr; if ((length + 1) < MAX_FULL_USER_ID_LENGTH) { (void)memcpy(real_user, pwd->pw_name, length); } else { (void)memcpy(real_user, pwd->pw_name, MAX_FULL_USER_ID_LENGTH - 1); length = MAX_FULL_USER_ID_LENGTH - 1; } real_user[length] = '-'; real_user[length + 1] = '>'; real_user[length + 2] = '\0'; ptr = fake_user_list; while ((ptr = lposi(ptr, real_user, length + 2)) != NULL) { ptr--; tmp_ptr = ptr; while ((*tmp_ptr != ',') && (*tmp_ptr != '\0')) { tmp_ptr++; } if (*tmp_ptr == ',') { change_char = YES; *tmp_ptr = '\0'; } else { change_char = NO; } if ((wanted_user[0] == '\0') || (CHECK_STRCMP(ptr, wanted_user) == 0)) { i = 0; while ((*(ptr + i) != ' ') && (*(ptr + i) != '\0') && (*(ptr + i) != '\t') && (i < MAX_FULL_USER_ID_LENGTH)) { fake_user[i] = *(ptr + i); i++; } fake_user[i] = '\0'; free(buffer); return; } if (change_char == YES) { *tmp_ptr = ','; } ptr = tmp_ptr; } } } } free(buffer); (void)fprintf(stderr, "%s (%s %d)\n", PERMISSION_DENIED_STR, __FILE__, __LINE__); exit(INCORRECT); } } fake_user[0] = '\0'; return; }