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

/src/sacctmgr/job_functions.c

https://github.com/cfenoy/slurm
C | 249 lines | 186 code | 24 blank | 39 comment | 60 complexity | e545ec41d80cfa675c1070b07a2edd84 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. /*****************************************************************************\
  2. * job_functions.c - functions dealing with jobs in the accounting system.
  3. *****************************************************************************
  4. * Copyright (C) 2008-2010 Lawrence Livermore National Security.
  5. * Copyright (C) 2002-2008 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. static int _set_cond(int *start, int argc, char *argv[],
  41. slurmdb_job_modify_cond_t *job_cond)
  42. {
  43. char *next_str;
  44. int i;
  45. int set = 0;
  46. int end = 0;
  47. int command_len = 0;
  48. if (!job_cond) {
  49. error("No job_cond given");
  50. return -1;
  51. }
  52. job_cond->job_id = NO_VAL;
  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. /* option = (int)argv[i][end-1]; */
  61. end++;
  62. }
  63. }
  64. if (!strncasecmp (argv[i], "Set", MAX(command_len, 3))) {
  65. i--;
  66. break;
  67. } else if (!end && !strncasecmp(argv[i], "where",
  68. MAX(command_len, 5))) {
  69. continue;
  70. } else if (!strncasecmp (argv[i], "Cluster",
  71. MAX(command_len, 1))) {
  72. job_cond->cluster = xstrdup(argv[i]+end);
  73. } else if (!strncasecmp (argv[i], "JobID",
  74. MAX(command_len, 1))) {
  75. job_cond->job_id = (uint32_t) strtol(argv[i]+end,
  76. &next_str, 10);
  77. if ((job_cond->job_id == 0) ||
  78. ((next_str[0] != '\0') && (next_str[0] != ' '))) {
  79. fprintf(stderr, "Invalid job id %s specified\n",
  80. argv[i]+end);
  81. exit_code = 1;
  82. } else
  83. set = 1;
  84. } else {
  85. exit_code = 1;
  86. fprintf(stderr, " Unknown condition: %s\n"
  87. " Use keyword 'set' to modify value\n",
  88. argv[i]);
  89. }
  90. }
  91. if (!job_cond->cluster)
  92. job_cond->cluster = slurm_get_cluster_name();
  93. (*start) = i;
  94. return set;
  95. }
  96. static int _set_rec(int *start, int argc, char *argv[],
  97. slurmdb_job_rec_t *job)
  98. {
  99. int i;
  100. int set = 0;
  101. int end = 0;
  102. int command_len = 0;
  103. for (i=(*start); i<argc; i++) {
  104. end = parse_option_end(argv[i]);
  105. if(!end)
  106. command_len=strlen(argv[i]);
  107. else {
  108. command_len=end-1;
  109. if (argv[i][end] == '=') {
  110. end++;
  111. }
  112. }
  113. if (!strncasecmp (argv[i], "Where", MAX(command_len, 5))) {
  114. i--;
  115. break;
  116. } else if(!end && !strncasecmp(argv[i], "set",
  117. MAX(command_len, 3))) {
  118. continue;
  119. } else if(!end) {
  120. exit_code=1;
  121. fprintf(stderr,
  122. " Bad format on %s: End your option with "
  123. "an '=' sign\n", argv[i]);
  124. } else if ((!strncasecmp(argv[i], "DerivedExitCode",
  125. MAX(command_len, 12))) ||
  126. (!strncasecmp(argv[i], "DerivedEC",
  127. MAX(command_len, 9)))) {
  128. if (get_uint(argv[i]+end, &job->derived_ec,
  129. "DerivedExitCode") == SLURM_SUCCESS) {
  130. set = 1;
  131. }
  132. } else if ((!strncasecmp(argv[i], "Comment",
  133. MAX(command_len, 7))) ||
  134. (!strncasecmp(argv[i], "DerivedExitString",
  135. MAX(command_len, 12))) ||
  136. (!strncasecmp(argv[i], "DerivedES",
  137. MAX(command_len, 9)))) {
  138. if(job->derived_es)
  139. xfree(job->derived_es);
  140. job->derived_es = strip_quotes(argv[i]+end, NULL, 1);
  141. set = 1;
  142. } else {
  143. printf(" Unknown option: %s\n"
  144. " Use keyword 'where' to modify condition\n",
  145. argv[i]);
  146. }
  147. }
  148. (*start) = i;
  149. return set;
  150. }
  151. extern int sacctmgr_modify_job(int argc, char *argv[])
  152. {
  153. int rc = SLURM_SUCCESS;
  154. slurmdb_job_modify_cond_t *job_cond = xmalloc(sizeof(
  155. slurmdb_job_modify_cond_t));
  156. slurmdb_job_rec_t *job = slurmdb_create_job_rec();
  157. int i=0;
  158. int cond_set = 0, rec_set = 0, set = 0;
  159. List ret_list = NULL;
  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. i++;
  164. cond_set += _set_cond(&i, argc, argv, job_cond);
  165. } else if (!strncasecmp(argv[i], "Set", MAX(command_len, 3))) {
  166. i++;
  167. rec_set += _set_rec(&i, argc, argv, job);
  168. } else {
  169. cond_set += _set_cond(&i, argc, argv, job_cond);
  170. }
  171. }
  172. if (exit_code) {
  173. slurmdb_destroy_job_modify_cond(job_cond);
  174. slurmdb_destroy_job_rec(job);
  175. return SLURM_ERROR;
  176. } else if (!rec_set) {
  177. exit_code=1;
  178. fprintf(stderr, " You didn't give me anything to set\n");
  179. slurmdb_destroy_job_modify_cond(job_cond);
  180. slurmdb_destroy_job_rec(job);
  181. return SLURM_ERROR;
  182. } else if (!cond_set) {
  183. if (!commit_check("You didn't set any conditions with 'WHERE'."
  184. "\nAre you sure you want to continue?")) {
  185. printf("Aborted\n");
  186. slurmdb_destroy_job_modify_cond(job_cond);
  187. slurmdb_destroy_job_rec(job);
  188. return SLURM_SUCCESS;
  189. }
  190. }
  191. notice_thread_init();
  192. ret_list = acct_storage_g_modify_job(db_conn, my_uid, job_cond, job);
  193. if (ret_list && list_count(ret_list)) {
  194. char *object = NULL;
  195. ListIterator itr = list_iterator_create(ret_list);
  196. printf(" Modified jobs...\n");
  197. while((object = list_next(itr))) {
  198. printf(" %s\n", object);
  199. }
  200. list_iterator_destroy(itr);
  201. set = 1;
  202. } else if (ret_list) {
  203. printf(" Nothing modified\n");
  204. rc = SLURM_ERROR;
  205. } else {
  206. exit_code=1;
  207. fprintf(stderr, " Error with request: %s\n",
  208. slurm_strerror(errno));
  209. rc = SLURM_ERROR;
  210. }
  211. if (ret_list)
  212. list_destroy(ret_list);
  213. notice_thread_fini();
  214. if (set) {
  215. if (commit_check("Would you like to commit changes?"))
  216. acct_storage_g_commit(db_conn, 1);
  217. else {
  218. printf(" Changes Discarded\n");
  219. acct_storage_g_commit(db_conn, 0);
  220. }
  221. }
  222. slurmdb_destroy_job_modify_cond(job_cond);
  223. slurmdb_destroy_job_rec(job);
  224. return rc;
  225. }