configuration.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "configuration.h"
  19. char * hadoop_conf_dir;
  20. struct configuration config={.size=0, .confdetails=NULL};
  21. //clean up method for freeing configuration
  22. void free_configurations() {
  23. int i = 0;
  24. for (i = 0; i < config.size; i++) {
  25. if (config.confdetails[i]->key != NULL) {
  26. free((void *)config.confdetails[i]->key);
  27. }
  28. if (config.confdetails[i]->value != NULL) {
  29. free((void *)config.confdetails[i]->value);
  30. }
  31. free(config.confdetails[i]);
  32. }
  33. if (config.size > 0) {
  34. free(config.confdetails);
  35. }
  36. config.size = 0;
  37. }
  38. //function used to load the configurations present in the secure config
  39. void get_configs() {
  40. FILE *conf_file;
  41. char *line;
  42. char *equaltok;
  43. char *temp_equaltok;
  44. size_t linesize = 1000;
  45. int size_read = 0;
  46. int str_len = 0;
  47. char *file_name = NULL;
  48. #ifndef HADOOP_CONF_DIR
  49. str_len = strlen(CONF_FILE_PATTERN) + strlen(hadoop_conf_dir);
  50. file_name = (char *) malloc(sizeof(char) * (str_len + 1));
  51. #else
  52. str_len = strlen(CONF_FILE_PATTERN) + strlen(HADOOP_CONF_DIR);
  53. file_name = (char *) malloc(sizeof(char) * (str_len + 1));
  54. #endif
  55. if (file_name == NULL) {
  56. fprintf(LOGFILE, "Malloc failed :Out of memory \n");
  57. return;
  58. }
  59. memset(file_name,'\0',str_len +1);
  60. #ifndef HADOOP_CONF_DIR
  61. snprintf(file_name,str_len, CONF_FILE_PATTERN, hadoop_conf_dir);
  62. #else
  63. snprintf(file_name, str_len, CONF_FILE_PATTERN, HADOOP_CONF_DIR);
  64. #endif
  65. #ifdef DEBUG
  66. fprintf(LOGFILE, "get_configs :Conf file name is : %s \n", file_name);
  67. #endif
  68. //allocate space for ten configuration items.
  69. config.confdetails = (struct confentry **) malloc(sizeof(struct confentry *)
  70. * MAX_SIZE);
  71. config.size = 0;
  72. conf_file = fopen(file_name, "r");
  73. if (conf_file == NULL) {
  74. fprintf(LOGFILE, "Invalid conf file provided : %s \n", file_name);
  75. free(file_name);
  76. return;
  77. }
  78. while(!feof(conf_file)) {
  79. line = (char *) malloc(linesize);
  80. if(line == NULL) {
  81. fprintf(LOGFILE, "malloc failed while reading configuration file.\n");
  82. goto cleanup;
  83. }
  84. size_read = getline(&line,&linesize,conf_file);
  85. //feof returns true only after we read past EOF.
  86. //so a file with no new line, at last can reach this place
  87. //if size_read returns negative check for eof condition
  88. if (size_read == -1) {
  89. if(!feof(conf_file)){
  90. fprintf(LOGFILE, "getline returned error.\n");
  91. goto cleanup;
  92. }else {
  93. break;
  94. }
  95. }
  96. //trim the ending new line
  97. line[strlen(line)-1] = '\0';
  98. //comment line
  99. if(line[0] == '#') {
  100. free(line);
  101. continue;
  102. }
  103. //tokenize first to get key and list of values.
  104. //if no equals is found ignore this line, can be an empty line also
  105. equaltok = strtok_r(line, "=", &temp_equaltok);
  106. if(equaltok == NULL) {
  107. free(line);
  108. continue;
  109. }
  110. config.confdetails[config.size] = (struct confentry *) malloc(
  111. sizeof(struct confentry));
  112. if(config.confdetails[config.size] == NULL) {
  113. fprintf(LOGFILE,
  114. "Failed allocating memory for single configuration item\n");
  115. goto cleanup;
  116. }
  117. #ifdef DEBUG
  118. fprintf(LOGFILE, "get_configs : Adding conf key : %s \n", equaltok);
  119. #endif
  120. memset(config.confdetails[config.size], 0, sizeof(struct confentry));
  121. config.confdetails[config.size]->key = (char *) malloc(
  122. sizeof(char) * (strlen(equaltok)+1));
  123. strcpy((char *)config.confdetails[config.size]->key, equaltok);
  124. equaltok = strtok_r(NULL, "=", &temp_equaltok);
  125. if (equaltok == NULL) {
  126. fprintf(LOGFILE, "configuration tokenization failed \n");
  127. goto cleanup;
  128. }
  129. //means value is commented so don't store the key
  130. if(equaltok[0] == '#') {
  131. free(line);
  132. free((void *)config.confdetails[config.size]->key);
  133. free(config.confdetails[config.size]);
  134. continue;
  135. }
  136. #ifdef DEBUG
  137. fprintf(LOGFILE, "get_configs : Adding conf value : %s \n", equaltok);
  138. #endif
  139. config.confdetails[config.size]->value = (char *) malloc(
  140. sizeof(char) * (strlen(equaltok)+1));
  141. strcpy((char *)config.confdetails[config.size]->value, equaltok);
  142. if((config.size + 1) % MAX_SIZE == 0) {
  143. config.confdetails = (struct confentry **) realloc(config.confdetails,
  144. sizeof(struct confentry **) * (MAX_SIZE + config.size));
  145. if (config.confdetails == NULL) {
  146. fprintf(LOGFILE,
  147. "Failed re-allocating memory for configuration items\n");
  148. goto cleanup;
  149. }
  150. }
  151. if(config.confdetails[config.size] )
  152. config.size++;
  153. free(line);
  154. }
  155. //close the file
  156. fclose(conf_file);
  157. //clean up allocated file name
  158. free(file_name);
  159. return;
  160. //free spaces alloced.
  161. cleanup:
  162. if (line != NULL) {
  163. free(line);
  164. }
  165. fclose(conf_file);
  166. free(file_name);
  167. free_configurations();
  168. return;
  169. }
  170. /*
  171. * function used to get a configuration value.
  172. * The function for the first time populates the configuration details into
  173. * array, next time onwards used the populated array.
  174. *
  175. */
  176. const char * get_value(const char* key) {
  177. int count;
  178. if (config.size == 0) {
  179. get_configs();
  180. }
  181. if (config.size == 0) {
  182. fprintf(LOGFILE, "Invalid configuration provided\n");
  183. return NULL;
  184. }
  185. for (count = 0; count < config.size; count++) {
  186. if (strcmp(config.confdetails[count]->key, key) == 0) {
  187. return strdup(config.confdetails[count]->value);
  188. }
  189. }
  190. return NULL;
  191. }
  192. /**
  193. * Function to return an array of values for a key.
  194. * Value delimiter is assumed to be a comma.
  195. */
  196. const char ** get_values(const char * key) {
  197. const char ** toPass = NULL;
  198. const char *value = get_value(key);
  199. char *tempTok = NULL;
  200. char *tempstr = NULL;
  201. int size = 0;
  202. int len;
  203. //first allocate any array of 10
  204. if(value != NULL) {
  205. toPass = (const char **) malloc(sizeof(char *) * MAX_SIZE);
  206. tempTok = strtok_r((char *)value, ",", &tempstr);
  207. if (tempTok != NULL) {
  208. while (1) {
  209. toPass[size++] = tempTok;
  210. tempTok = strtok_r(NULL, ",", &tempstr);
  211. if(tempTok == NULL){
  212. break;
  213. }
  214. if((size % MAX_SIZE) == 0) {
  215. toPass = (const char **) realloc(toPass,(sizeof(char *) *
  216. (MAX_SIZE * ((size/MAX_SIZE) +1))));
  217. }
  218. }
  219. } else {
  220. toPass[size] = (char *)value;
  221. }
  222. }
  223. if(size > 0) {
  224. toPass[size] = NULL;
  225. }
  226. return toPass;
  227. }