PageRenderTime 71ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sacctmgr/wckey_functions.c

https://github.com/ralphbean/slurm
C | 318 lines | 245 code | 33 blank | 40 comment | 63 complexity | 399aed7c53664978adc88859f649c4b0 MD5 | raw file
  1. /*****************************************************************************\
  2. * wckey_functions.c - functions dealing with wckeys in the accounting system.
  3. *****************************************************************************
  4. * Copyright (C) 2008 Lawrence Livermore National Security.
  5. * Copyright (C) 2002-2007 The Regents of the University of California.
  6. * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  7. * Written by Danny Auble <da@llnl.gov>
  8. * CODE-OCEC-09-009. All rights reserved.
  9. *
  10. * This file is part of SLURM, a resource management program.
  11. * For details, see <http://www.schedmd.com/slurmdocs/>.
  12. * Please also read the included file: DISCLAIMER.
  13. *
  14. * SLURM is free software; you can redistribute it and/or modify it under
  15. * the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. * In addition, as a special exception, the copyright holders give permission
  20. * to link the code of portions of this program with the OpenSSL library under
  21. * certain conditions as described in each individual source file, and
  22. * distribute linked combinations including the two. You must obey the GNU
  23. * General Public License in all respects for all of the code used other than
  24. * OpenSSL. If you modify file(s) with this exception, you may extend this
  25. * exception to your version of the file(s), but you are not obligated to do
  26. * so. If you do not wish to do so, delete this exception statement from your
  27. * version. If you delete this exception statement from all source files in
  28. * the program, then also delete it here.
  29. *
  30. * SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
  31. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  32. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  33. * details.
  34. *
  35. * You should have received a copy of the GNU General Public License along
  36. * with SLURM; if not, write to the Free Software Foundation, Inc.,
  37. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  38. \*****************************************************************************/
  39. #include "src/sacctmgr/sacctmgr.h"
  40. #include "src/common/uid.h"
  41. static int _set_cond(int *start, int argc, char *argv[],
  42. slurmdb_wckey_cond_t *wckey_cond,
  43. List format_list)
  44. {
  45. int i;
  46. int set = 0;
  47. int end = 0;
  48. int command_len = 0;
  49. int option = 0;
  50. if(!wckey_cond) {
  51. error("No wckey_cond given");
  52. return -1;
  53. }
  54. for (i=(*start); i<argc; i++) {
  55. end = parse_option_end(argv[i]);
  56. if(!end)
  57. command_len=strlen(argv[i]);
  58. else {
  59. command_len=end-1;
  60. if(argv[i][end] == '=') {
  61. option = (int)argv[i][end-1];
  62. end++;
  63. }
  64. }
  65. if(!end && !strncasecmp(argv[i], "where",
  66. MAX(command_len, 5))) {
  67. continue;
  68. } else if(!end && !strncasecmp(argv[i], "withdeleted",
  69. MAX(command_len, 5))) {
  70. wckey_cond->with_deleted = 1;
  71. set = 1;
  72. } else if(!end
  73. || !strncasecmp (argv[i], "WCKeys",
  74. MAX(command_len, 3))
  75. || !strncasecmp (argv[i], "Names",
  76. MAX(command_len, 3))) {
  77. if(!wckey_cond->name_list)
  78. wckey_cond->name_list =
  79. list_create(slurm_destroy_char);
  80. if(slurm_addto_char_list(wckey_cond->name_list,
  81. argv[i]+end))
  82. set = 1;
  83. } else if (!strncasecmp (argv[i], "Ids",
  84. MAX(command_len, 1))) {
  85. ListIterator itr = NULL;
  86. char *temp = NULL;
  87. uint32_t id = 0;
  88. if(!wckey_cond->id_list)
  89. wckey_cond->id_list =
  90. list_create(slurm_destroy_char);
  91. if(slurm_addto_char_list(wckey_cond->id_list,
  92. argv[i]+end))
  93. set = 1;
  94. /* check to make sure user gave ints here */
  95. itr = list_iterator_create(wckey_cond->id_list);
  96. while ((temp = list_next(itr))) {
  97. if (get_uint(temp, &id, "WCKeyID")
  98. != SLURM_SUCCESS) {
  99. exit_code = 1;
  100. list_delete_item(itr);
  101. }
  102. }
  103. list_iterator_destroy(itr);
  104. } else if (!strncasecmp (argv[i], "Clusters",
  105. MAX(command_len, 3))) {
  106. if(!wckey_cond->cluster_list)
  107. wckey_cond->cluster_list =
  108. list_create(slurm_destroy_char);
  109. if(slurm_addto_char_list(wckey_cond->cluster_list,
  110. argv[i]+end))
  111. set = 1;
  112. } else if (!strncasecmp (argv[i], "End", MAX(command_len, 1))) {
  113. wckey_cond->usage_end = parse_time(argv[i]+end, 1);
  114. set = 1;
  115. } else if (!strncasecmp (argv[i], "Format",
  116. MAX(command_len, 1))) {
  117. if(format_list)
  118. slurm_addto_char_list(format_list, argv[i]+end);
  119. } else if (!strncasecmp (argv[i], "Start",
  120. MAX(command_len, 1))) {
  121. wckey_cond->usage_start = parse_time(argv[i]+end, 1);
  122. set = 1;
  123. } else if (!strncasecmp (argv[i], "Users",
  124. MAX(command_len, 1))) {
  125. if(!wckey_cond->user_list)
  126. wckey_cond->user_list =
  127. list_create(slurm_destroy_char);
  128. if(slurm_addto_char_list(wckey_cond->user_list,
  129. argv[i]+end))
  130. set = 1;
  131. } else {
  132. exit_code=1;
  133. fprintf(stderr, " Unknown condition: %s\n", argv[i]);
  134. }
  135. }
  136. (*start) = i;
  137. return set;
  138. }
  139. extern int sacctmgr_list_wckey(int argc, char *argv[])
  140. {
  141. int rc = SLURM_SUCCESS;
  142. slurmdb_wckey_cond_t *wckey_cond =
  143. xmalloc(sizeof(slurmdb_wckey_cond_t));
  144. List wckey_list = NULL;
  145. int i=0;
  146. ListIterator itr = NULL;
  147. ListIterator itr2 = NULL;
  148. slurmdb_wckey_rec_t *wckey = NULL;
  149. char *object;
  150. print_field_t *field = NULL;
  151. int field_count = 0;
  152. List format_list = list_create(slurm_destroy_char);
  153. List print_fields_list; /* types are of print_field_t */
  154. enum {
  155. PRINT_CLUSTER,
  156. PRINT_ID,
  157. PRINT_NAME,
  158. PRINT_USER
  159. };
  160. for (i=0; i<argc; i++) {
  161. int command_len = strlen(argv[i]);
  162. if (!strncasecmp (argv[i], "Where", MAX(command_len, 5))
  163. || !strncasecmp (argv[i], "Set", MAX(command_len, 3)))
  164. i++;
  165. _set_cond(&i, argc, argv, wckey_cond, format_list);
  166. }
  167. if(exit_code) {
  168. slurmdb_destroy_wckey_cond(wckey_cond);
  169. list_destroy(format_list);
  170. return SLURM_ERROR;
  171. }
  172. if(!list_count(format_list)) {
  173. slurm_addto_char_list(format_list,
  174. "Name,Cluster,User");
  175. }
  176. print_fields_list = list_create(destroy_print_field);
  177. itr = list_iterator_create(format_list);
  178. while((object = list_next(itr))) {
  179. char *tmp_char = NULL;
  180. int command_len = 0;
  181. int newlen = 0;
  182. if((tmp_char = strstr(object, "\%"))) {
  183. newlen = atoi(tmp_char+1);
  184. tmp_char[0] = '\0';
  185. }
  186. command_len = strlen(object);
  187. field = xmalloc(sizeof(print_field_t));
  188. if(!strncasecmp("WCKeys", object, MAX(command_len, 1))
  189. || !strncasecmp("Names", object, MAX(command_len, 1))) {
  190. field->type = PRINT_NAME;
  191. field->name = xstrdup("WCKey");
  192. field->len = 10;
  193. field->print_routine = print_fields_str;
  194. } else if(!strncasecmp("Clusters", object,
  195. MAX(command_len, 2))) {
  196. field->type = PRINT_CLUSTER;
  197. field->name = xstrdup("Cluster");
  198. field->len = 10;
  199. field->print_routine = print_fields_str;
  200. } else if(!strncasecmp("IDs", object, MAX(command_len, 1))) {
  201. field->type = PRINT_ID;
  202. field->name = xstrdup("ID");
  203. field->len = 6;
  204. field->print_routine = print_fields_uint;
  205. } else if(!strncasecmp("Users", object, MAX(command_len, 1))) {
  206. field->type = PRINT_USER;
  207. field->name = xstrdup("User");
  208. field->len = 10;
  209. field->print_routine = print_fields_str;
  210. } else {
  211. exit_code=1;
  212. fprintf(stderr, "Unknown field '%s'\n", object);
  213. xfree(field);
  214. continue;
  215. }
  216. if(newlen)
  217. field->len = newlen;
  218. list_append(print_fields_list, field);
  219. }
  220. list_iterator_destroy(itr);
  221. list_destroy(format_list);
  222. if(exit_code) {
  223. slurmdb_destroy_wckey_cond(wckey_cond);
  224. list_destroy(print_fields_list);
  225. return SLURM_ERROR;
  226. }
  227. wckey_list = acct_storage_g_get_wckeys(db_conn, my_uid, wckey_cond);
  228. slurmdb_destroy_wckey_cond(wckey_cond);
  229. if(!wckey_list) {
  230. exit_code=1;
  231. fprintf(stderr, " Error with request: %s\n",
  232. slurm_strerror(errno));
  233. list_destroy(print_fields_list);
  234. return SLURM_ERROR;
  235. }
  236. itr = list_iterator_create(wckey_list);
  237. itr2 = list_iterator_create(print_fields_list);
  238. print_fields_header(print_fields_list);
  239. field_count = list_count(print_fields_list);
  240. while((wckey = list_next(itr))) {
  241. int curr_inx = 1;
  242. while((field = list_next(itr2))) {
  243. switch(field->type) {
  244. /* All the association stuff */
  245. case PRINT_CLUSTER:
  246. field->print_routine(
  247. field,
  248. wckey->cluster,
  249. (curr_inx == field_count));
  250. break;
  251. case PRINT_ID:
  252. field->print_routine(
  253. field,
  254. wckey->id,
  255. (curr_inx == field_count));
  256. break;
  257. case PRINT_NAME:
  258. field->print_routine(
  259. field,
  260. wckey->name,
  261. (curr_inx == field_count));
  262. break;
  263. case PRINT_USER:
  264. field->print_routine(
  265. field,
  266. wckey->user,
  267. (curr_inx == field_count));
  268. break;
  269. default:
  270. field->print_routine(
  271. field, NULL,
  272. (curr_inx == field_count));
  273. break;
  274. }
  275. curr_inx++;
  276. }
  277. list_iterator_reset(itr2);
  278. printf("\n");
  279. }
  280. list_iterator_destroy(itr2);
  281. list_iterator_destroy(itr);
  282. list_destroy(wckey_list);
  283. list_destroy(print_fields_list);
  284. return rc;
  285. }