job-qmp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * QMP interface for background jobs
  3. *
  4. * Copyright (c) 2011 IBM Corp.
  5. * Copyright (c) 2012, 2018 Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/job.h"
  27. #include "qapi/qapi-commands-job.h"
  28. #include "qapi/error.h"
  29. #include "trace/trace-root.h"
  30. /*
  31. * Get a job using its ID. Called with job_mutex held.
  32. */
  33. static Job *find_job_locked(const char *id, Error **errp)
  34. {
  35. Job *job;
  36. job = job_get_locked(id);
  37. if (!job) {
  38. error_setg(errp, "Job not found");
  39. return NULL;
  40. }
  41. return job;
  42. }
  43. void qmp_job_cancel(const char *id, Error **errp)
  44. {
  45. Job *job;
  46. JOB_LOCK_GUARD();
  47. job = find_job_locked(id, errp);
  48. if (!job) {
  49. return;
  50. }
  51. trace_qmp_job_cancel(job);
  52. job_user_cancel_locked(job, true, errp);
  53. }
  54. void qmp_job_pause(const char *id, Error **errp)
  55. {
  56. Job *job;
  57. JOB_LOCK_GUARD();
  58. job = find_job_locked(id, errp);
  59. if (!job) {
  60. return;
  61. }
  62. trace_qmp_job_pause(job);
  63. job_user_pause_locked(job, errp);
  64. }
  65. void qmp_job_resume(const char *id, Error **errp)
  66. {
  67. Job *job;
  68. JOB_LOCK_GUARD();
  69. job = find_job_locked(id, errp);
  70. if (!job) {
  71. return;
  72. }
  73. trace_qmp_job_resume(job);
  74. job_user_resume_locked(job, errp);
  75. }
  76. void qmp_job_complete(const char *id, Error **errp)
  77. {
  78. Job *job;
  79. JOB_LOCK_GUARD();
  80. job = find_job_locked(id, errp);
  81. if (!job) {
  82. return;
  83. }
  84. trace_qmp_job_complete(job);
  85. job_complete_locked(job, errp);
  86. }
  87. void qmp_job_finalize(const char *id, Error **errp)
  88. {
  89. Job *job;
  90. JOB_LOCK_GUARD();
  91. job = find_job_locked(id, errp);
  92. if (!job) {
  93. return;
  94. }
  95. trace_qmp_job_finalize(job);
  96. job_ref_locked(job);
  97. job_finalize_locked(job, errp);
  98. job_unref_locked(job);
  99. }
  100. void qmp_job_dismiss(const char *id, Error **errp)
  101. {
  102. Job *job;
  103. JOB_LOCK_GUARD();
  104. job = find_job_locked(id, errp);
  105. if (!job) {
  106. return;
  107. }
  108. trace_qmp_job_dismiss(job);
  109. job_dismiss_locked(&job, errp);
  110. }
  111. /* Called with job_mutex held. */
  112. static JobInfo *job_query_single_locked(Job *job, Error **errp)
  113. {
  114. JobInfo *info;
  115. uint64_t progress_current;
  116. uint64_t progress_total;
  117. assert(!job_is_internal(job));
  118. progress_get_snapshot(&job->progress, &progress_current,
  119. &progress_total);
  120. info = g_new(JobInfo, 1);
  121. *info = (JobInfo) {
  122. .id = g_strdup(job->id),
  123. .type = job_type(job),
  124. .status = job->status,
  125. .current_progress = progress_current,
  126. .total_progress = progress_total,
  127. .error = job->err ?
  128. g_strdup(error_get_pretty(job->err)) : NULL,
  129. };
  130. return info;
  131. }
  132. JobInfoList *qmp_query_jobs(Error **errp)
  133. {
  134. JobInfoList *head = NULL, **tail = &head;
  135. Job *job;
  136. JOB_LOCK_GUARD();
  137. for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
  138. JobInfo *value;
  139. if (job_is_internal(job)) {
  140. continue;
  141. }
  142. value = job_query_single_locked(job, errp);
  143. if (!value) {
  144. qapi_free_JobInfoList(head);
  145. return NULL;
  146. }
  147. QAPI_LIST_APPEND(tail, value);
  148. }
  149. return head;
  150. }