PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sacctmgr/wckey_functions.c

https://github.com/cfenoy/slurm
C | 316 lines | 243 code | 33 blank | 40 comment | 63 complexity | 712187a66dca6c8d52248274c52eb047 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  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. if(!wckey_cond) {
  50. error("No wckey_cond given");
  51. return -1;
  52. }
  53. for (i=(*start); i<argc; i++) {
  54. end = parse_option_end(argv[i]);
  55. if(!end)
  56. command_len=strlen(argv[i]);
  57. else {
  58. command_len=end-1;
  59. if (argv[i][end] == '=') {
  60. end++;
  61. }
  62. }
  63. if(!end && !strncasecmp(argv[i], "where",
  64. MAX(command_len, 5))) {
  65. continue;
  66. } else if(!end && !strncasecmp(argv[i], "withdeleted",
  67. MAX(command_len, 5))) {
  68. wckey_cond->with_deleted = 1;
  69. set = 1;
  70. } else if(!end
  71. || !strncasecmp (argv[i], "WCKeys",
  72. MAX(command_len, 3))
  73. || !strncasecmp (argv[i], "Names",
  74. MAX(command_len, 3))) {
  75. if(!wckey_cond->name_list)
  76. wckey_cond->name_list =
  77. list_create(slurm_destroy_char);
  78. if(slurm_addto_char_list(wckey_cond->name_list,
  79. argv[i]+end))
  80. set = 1;
  81. } else if (!strncasecmp (argv[i], "Ids",
  82. MAX(command_len, 1))) {
  83. ListIterator itr = NULL;
  84. char *temp = NULL;
  85. uint32_t id = 0;
  86. if(!wckey_cond->id_list)
  87. wckey_cond->id_list =
  88. list_create(slurm_destroy_char);
  89. if(slurm_addto_char_list(wckey_cond->id_list,
  90. argv[i]+end))
  91. set = 1;
  92. /* check to make sure user gave ints here */
  93. itr = list_iterator_create(wckey_cond->id_list);
  94. while ((temp = list_next(itr))) {
  95. if (get_uint(temp, &id, "WCKeyID")
  96. != SLURM_SUCCESS) {
  97. exit_code = 1;
  98. list_delete_item(itr);
  99. }
  100. }
  101. list_iterator_destroy(itr);
  102. } else if (!strncasecmp (argv[i], "Clusters",
  103. MAX(command_len, 3))) {
  104. if(!wckey_cond->cluster_list)
  105. wckey_cond->cluster_list =
  106. list_create(slurm_destroy_char);
  107. if(slurm_addto_char_list(wckey_cond->cluster_list,
  108. argv[i]+end))
  109. set = 1;
  110. } else if (!strncasecmp (argv[i], "End", MAX(command_len, 1))) {
  111. wckey_cond->usage_end = parse_time(argv[i]+end, 1);
  112. set = 1;
  113. } else if (!strncasecmp (argv[i], "Format",
  114. MAX(command_len, 1))) {
  115. if(format_list)
  116. slurm_addto_char_list(format_list, argv[i]+end);
  117. } else if (!strncasecmp (argv[i], "Start",
  118. MAX(command_len, 1))) {
  119. wckey_cond->usage_start = parse_time(argv[i]+end, 1);
  120. set = 1;
  121. } else if (!strncasecmp (argv[i], "Users",
  122. MAX(command_len, 1))) {
  123. if(!wckey_cond->user_list)
  124. wckey_cond->user_list =
  125. list_create(slurm_destroy_char);
  126. if(slurm_addto_char_list(wckey_cond->user_list,
  127. argv[i]+end))
  128. set = 1;
  129. } else {
  130. exit_code=1;
  131. fprintf(stderr, " Unknown condition: %s\n", argv[i]);
  132. }
  133. }
  134. (*start) = i;
  135. return set;
  136. }
  137. extern int sacctmgr_list_wckey(int argc, char *argv[])
  138. {
  139. int rc = SLURM_SUCCESS;
  140. slurmdb_wckey_cond_t *wckey_cond =
  141. xmalloc(sizeof(slurmdb_wckey_cond_t));
  142. List wckey_list = NULL;
  143. int i=0;
  144. ListIterator itr = NULL;
  145. ListIterator itr2 = NULL;
  146. slurmdb_wckey_rec_t *wckey = NULL;
  147. char *object;
  148. print_field_t *field = NULL;
  149. int field_count = 0;
  150. List format_list = list_create(slurm_destroy_char);
  151. List print_fields_list; /* types are of print_field_t */
  152. enum {
  153. PRINT_CLUSTER,
  154. PRINT_ID,
  155. PRINT_NAME,
  156. PRINT_USER
  157. };
  158. for (i=0; i<argc; i++) {
  159. int command_len = strlen(argv[i]);
  160. if (!strncasecmp (argv[i], "Where", MAX(command_len, 5))
  161. || !strncasecmp (argv[i], "Set", MAX(command_len, 3)))
  162. i++;
  163. _set_cond(&i, argc, argv, wckey_cond, format_list);
  164. }
  165. if(exit_code) {
  166. slurmdb_destroy_wckey_cond(wckey_cond);
  167. list_destroy(format_list);
  168. return SLURM_ERROR;
  169. }
  170. if(!list_count(format_list)) {
  171. slurm_addto_char_list(format_list,
  172. "Name,Cluster,User");
  173. }
  174. print_fields_list = list_create(destroy_print_field);
  175. itr = list_iterator_create(format_list);
  176. while((object = list_next(itr))) {
  177. char *tmp_char = NULL;
  178. int command_len = 0;
  179. int newlen = 0;
  180. if((tmp_char = strstr(object, "\%"))) {
  181. newlen = atoi(tmp_char+1);
  182. tmp_char[0] = '\0';
  183. }
  184. command_len = strlen(object);
  185. field = xmalloc(sizeof(print_field_t));
  186. if(!strncasecmp("WCKeys", object, MAX(command_len, 1))
  187. || !strncasecmp("Names", object, MAX(command_len, 1))) {
  188. field->type = PRINT_NAME;
  189. field->name = xstrdup("WCKey");
  190. field->len = 10;
  191. field->print_routine = print_fields_str;
  192. } else if(!strncasecmp("Clusters", object,
  193. MAX(command_len, 2))) {
  194. field->type = PRINT_CLUSTER;
  195. field->name = xstrdup("Cluster");
  196. field->len = 10;
  197. field->print_routine = print_fields_str;
  198. } else if(!strncasecmp("IDs", object, MAX(command_len, 1))) {
  199. field->type = PRINT_ID;
  200. field->name = xstrdup("ID");
  201. field->len = 6;
  202. field->print_routine = print_fields_uint;
  203. } else if(!strncasecmp("Users", object, MAX(command_len, 1))) {
  204. field->type = PRINT_USER;
  205. field->name = xstrdup("User");
  206. field->len = 10;
  207. field->print_routine = print_fields_str;
  208. } else {
  209. exit_code=1;
  210. fprintf(stderr, "Unknown field '%s'\n", object);
  211. xfree(field);
  212. continue;
  213. }
  214. if(newlen)
  215. field->len = newlen;
  216. list_append(print_fields_list, field);
  217. }
  218. list_iterator_destroy(itr);
  219. list_destroy(format_list);
  220. if(exit_code) {
  221. slurmdb_destroy_wckey_cond(wckey_cond);
  222. list_destroy(print_fields_list);
  223. return SLURM_ERROR;
  224. }
  225. wckey_list = acct_storage_g_get_wckeys(db_conn, my_uid, wckey_cond);
  226. slurmdb_destroy_wckey_cond(wckey_cond);
  227. if(!wckey_list) {
  228. exit_code=1;
  229. fprintf(stderr, " Error with request: %s\n",
  230. slurm_strerror(errno));
  231. list_destroy(print_fields_list);
  232. return SLURM_ERROR;
  233. }
  234. itr = list_iterator_create(wckey_list);
  235. itr2 = list_iterator_create(print_fields_list);
  236. print_fields_header(print_fields_list);
  237. field_count = list_count(print_fields_list);
  238. while((wckey = list_next(itr))) {
  239. int curr_inx = 1;
  240. while((field = list_next(itr2))) {
  241. switch(field->type) {
  242. /* All the association stuff */
  243. case PRINT_CLUSTER:
  244. field->print_routine(
  245. field,
  246. wckey->cluster,
  247. (curr_inx == field_count));
  248. break;
  249. case PRINT_ID:
  250. field->print_routine(
  251. field,
  252. wckey->id,
  253. (curr_inx == field_count));
  254. break;
  255. case PRINT_NAME:
  256. field->print_routine(
  257. field,
  258. wckey->name,
  259. (curr_inx == field_count));
  260. break;
  261. case PRINT_USER:
  262. field->print_routine(
  263. field,
  264. wckey->user,
  265. (curr_inx == field_count));
  266. break;
  267. default:
  268. field->print_routine(
  269. field, NULL,
  270. (curr_inx == field_count));
  271. break;
  272. }
  273. curr_inx++;
  274. }
  275. list_iterator_reset(itr2);
  276. printf("\n");
  277. }
  278. list_iterator_destroy(itr2);
  279. list_iterator_destroy(itr);
  280. list_destroy(wckey_list);
  281. list_destroy(print_fields_list);
  282. return rc;
  283. }