int Initialize_log(Chk_log_type *log, log_type_type log_type) { int i, j; log->log_type = log_type; log->num_entries = 0; log->max_entries = REG_INITIAL_CHK_LOG_SIZE; log->num_unsent = 0; log->emit_in_progress = REG_FALSE; log->file_content = NULL; log->send_all = REG_TRUE; if(log->log_type == PARAM){ for(i=0; i<REG_MAX_NUM_STR_PARAMS; i++){ log->param_send_all[i] = REG_TRUE; } Initialize_log_impl(log); /* We delete any existing parameter log file rather than append to it */ Delete_log_file(log); } Set_log_primary_key(log); if( Open_log_file(log) != REG_SUCCESS ){ fprintf(stderr, "STEER: Initialize_log: failed to create log file, " "log will not be saved to file\n"); } log->entry = (Chk_log_entry_type *)malloc(Chk_log.max_entries *sizeof(Chk_log_entry_type)); log->pSteer_cmds = (char *)malloc(REG_MAX_MSG_SIZE*sizeof(char)); log->steer_cmds_bytes = REG_MAX_MSG_SIZE; if(!(log->entry) || !(log->pSteer_cmds) ){ fprintf(stderr, "STEER: Initialize_log: failed to allocate memory " "for checkpoint logging\n"); return REG_FAILURE; } log->pSteer_cmds[0] = '\0'; /* pSteer_cmds_slot points to next free space in the buffer */ log->pSteer_cmds_slot = log->pSteer_cmds; for(i=0; i<log->max_entries; i++){ log->entry[i].sent_to_steerer = REG_TRUE; log->entry[i].num_param = 0; for(j=0; j<REG_MAX_NUM_STR_PARAMS; j++){ log->entry[i].param[j].handle = REG_PARAM_HANDLE_NOTSET; } } return REG_SUCCESS; }
int RMTD_server_init ( int argc, /* Number of command arguments */ char **argv, /* UNIX command arguments */ char **envp, /* UNIX command environment */ void (*set_up_user_functions) (int (**user_func) (int,char *,char **)) ) { char *dir = NULL; int where; int max_nfds, n_clients; int ret; MISC_malloc_retry (1); MISC_log_reg_callback (Write_log); /* set log function */ Set_up_user_functions = set_up_user_functions; /* initialize Prog_name, the executable file name */ where = strlen (argv[0]) - 1; while (where >= 0 && argv[0][where] != '/') where--; strncpy (Prog_name, argv[0] + where + 1, NAME_SIZE); Prog_name [NAME_SIZE - 1] = '\0'; /* The default values and options */ Conf_name[0] = '\0'; Log_name[0] = '\0'; Run_in_background = 1; max_nfds = 0; if (Get_options (argc, argv, Prog_name, &max_nfds) == FAILURE) exit (1); if ((dir = getenv ("HOME")) == NULL || strlen (dir) > NAME_SIZE - 32) { /* reserve 32 chars for internal names */ MISC_log ("Environmental variable HOME is not defined\n"); exit (1); } if (Conf_name[0] == '\0') sprintf (Conf_name, "%s/.%s.conf", dir, Prog_name); if (Log_name[0] == '\0') { char buf[64]; unsigned int rmtport_ip; rmtport_ip = PNUM_get_local_ip_from_rmtport (); if (rmtport_ip == INADDR_NONE) sprintf (Log_name, "%s/%s.log", dir, Prog_name); else sprintf (Log_name, "%s/.rssd/%s/%s.log", dir, NET_string_IP (rmtport_ip, 1, buf), Prog_name); } /* Open the log message file */ if (Run_in_background == 1) { if (Open_log_file () == FAILURE) exit (1); } /* gets/sets max number of fds */ if ((ret = MISC_rsrc_nofile (max_nfds)) < max_nfds) { MISC_log ("MISC_rsrc_nofile failed (ret %d < req %d)", ret, max_nfds); exit (1); } max_nfds = ret; /* read configuration file */ if ((n_clients = GCLD_initialize_host_table (Conf_name)) <= 0) exit (1); MSGD_set_max_n_local_senders (max_nfds - n_clients - N_child - 64); /* Retrieve the user defined functions */ if (Get_user_functions () == FAILURE) exit (1); MISC_log ("Max number of fds set to %d", max_nfds); return (0); }
int Emit_log(Chk_log_type *log, int handle) { int size = 0; int index = 0; int return_status = REG_SUCCESS; if(log->log_type == PARAM){ index = Param_index_from_handle(&Params_table, handle); if(index == REG_PARAM_HANDLE_NOTSET)return REG_FAILURE; } if(log->emit_in_progress == REG_TRUE){ if(Emit_log_entries(log, log->file_content, handle) == REG_UNFINISHED){ return return_status; } log->emit_in_progress = REG_FALSE; free(log->file_content); log->file_content = NULL; /* Now send the entries that we have stored in memory - need to send all current log entries to the steerer */ if(Log_to_xml(log, handle, &(log->file_content), &size, REG_FALSE) != REG_SUCCESS){ return REG_FAILURE; } } else if((log->log_type == CHKPT && log->send_all == REG_TRUE)|| (log->log_type == PARAM && log->param_send_all[index] == REG_TRUE)){ #ifdef REG_DEBUG_FULL fprintf(stderr, "STEER: Emit_log: sending all saved log entries...\n"); #endif /* Then we have to send any entries we've saved to file too... */ Close_log_file(log); /* Read the log file and get back contents in buffer pointed to by pbuf. We must free() this once we're done. */ if(log->log_type == CHKPT){ return_status = Read_file(log->filename, &(log->file_content), &size, REG_FALSE); } else if(log->log_type == PARAM){ return_status = Read_file(log->filename, &(log->file_content), &size, REG_TRUE); } if(return_status != REG_SUCCESS){ if(!log->file_content)free(log->file_content); log->file_content = NULL; return REG_FAILURE; } if (size > 0) Emit_log_entries(log, log->file_content, handle); /* Re-open log-file for future buffering */ if( Open_log_file(log) != REG_SUCCESS){ #ifdef REG_DEBUG fprintf(stderr, "STEER: Emit_log: Open_log_file failed\n"); #endif } if(log->emit_in_progress == REG_TRUE){ /* Don't set log->send_all if going to continue to Log_to_xml as will affect what log entries are returned */ log->send_all = REG_FALSE; return REG_SUCCESS; } free(log->file_content); log->file_content = NULL; /* End of sending buffered entries */ /* Now send the entries that we have stored in memory - need to send all current log entries to the steerer */ if(Log_to_xml(log, handle, &(log->file_content), &size, REG_FALSE) != REG_SUCCESS){ return REG_FAILURE; } if(log->log_type == PARAM){ log->param_send_all[index] = REG_FALSE; } else{ log->send_all = REG_FALSE; } } else{ #ifdef REG_DEBUG_FULL fprintf(stderr, "STEER: Emit_log: sending unsent log entries...\n"); #endif /* Fifth argument specifies that we only want those entries that haven't already been sent to the steerer */ if(Log_to_xml(log, handle, &(log->file_content), &size, REG_TRUE) != REG_SUCCESS){ return REG_FAILURE; } } #ifdef REG_DEBUG_FULL fprintf(stderr, "STEER: Emit_log: calling Emit_log_entries...\n"); #endif /* Pull the entries out of the buffer returned by Log_to_xml and send them to the steerer */ if(size > 0){ if( (return_status = Emit_log_entries(log, log->file_content, handle)) == REG_UNFINISHED){ return REG_SUCCESS; } } free(log->file_content); log->file_content = NULL; #ifdef REG_DEBUG_FULL fprintf(stderr, "STEER: Emit_log: sending logged steering commands...\n"); #endif /* Send log of steering commands */ if(strlen(log->pSteer_cmds) > 0){ Emit_log_entries(log, log->pSteer_cmds, handle); log->pSteer_cmds[0]='\0'; log->pSteer_cmds_slot = log->pSteer_cmds; } if(return_status == REG_SUCCESS){ /* Zero counter since we've just told steerer all about any log entries it hadn't got */ log->num_unsent = 0; } return return_status; }
int Set_log_primary_key(Chk_log_type *log) { int size; int return_status = REG_SUCCESS; char *pbuf = NULL; char *ptr = NULL; char *old_ptr = NULL; Close_log_file(log); /* Read the log file and get back contents in buffer pointed to by pbuf. We must free() this once we're done. */ if(Read_file(log->filename, &pbuf, &size, REG_TRUE) != REG_SUCCESS){ log->primary_key_value = 0; if(pbuf)free(pbuf); return REG_SUCCESS; } if(size == 0){ /* Log file existed but was empty */ log->primary_key_value = 0; if(pbuf)free(pbuf); return REG_SUCCESS; } ptr = pbuf; /* Check whether we've got xml or columns... */ if(!strstr(pbuf, "<Key>")){ /* We have columns of data, first column holds key values... */ while( (ptr = strstr((ptr+1), "\n")) ){ if(*(ptr+1) == '\0')break; old_ptr = ptr; } #ifdef REG_DEBUG fprintf(stderr, "STEER: Set_log_primary_key: last chunk = >>%s<<\n", old_ptr); #endif if(2 != sscanf(old_ptr, "%d %s", &(log->primary_key_value), Steer_lib_config.scratch_buffer)){ log->primary_key_value = 0; return_status = REG_FAILURE; } else{ log->primary_key_value++; } } else{ /* We have xml...*/ while( (ptr = strstr((ptr+1), "<Key>")) ){ old_ptr = ptr; } #ifdef REG_DEBUG fprintf(stderr, "STEER: Set_log_primary_key: last chunk = >>%s<<\n", old_ptr); #endif if( 1 != sscanf(old_ptr, "<Key>%d</Key>", &(log->primary_key_value))){ log->primary_key_value = 0; return_status = REG_FAILURE; } else{ log->primary_key_value++; } } free(pbuf); Open_log_file(log); return return_status; }