You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

job-qmp.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-root.h"
  30. /* Get a job using its ID and acquire its AioContext */
  31. static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
  32. {
  33. Job *job;
  34. *aio_context = NULL;
  35. job = job_get(id);
  36. if (!job) {
  37. error_setg(errp, "Job not found");
  38. return NULL;
  39. }
  40. *aio_context = job->aio_context;
  41. aio_context_acquire(*aio_context);
  42. return job;
  43. }
  44. void qmp_job_cancel(const char *id, Error **errp)
  45. {
  46. AioContext *aio_context;
  47. Job *job = find_job(id, &aio_context, errp);
  48. if (!job) {
  49. return;
  50. }
  51. trace_qmp_job_cancel(job);
  52. job_user_cancel(job, true, errp);
  53. aio_context_release(aio_context);
  54. }
  55. void qmp_job_pause(const char *id, Error **errp)
  56. {
  57. AioContext *aio_context;
  58. Job *job = find_job(id, &aio_context, errp);
  59. if (!job) {
  60. return;
  61. }
  62. trace_qmp_job_pause(job);
  63. job_user_pause(job, errp);
  64. aio_context_release(aio_context);
  65. }
  66. void qmp_job_resume(const char *id, Error **errp)
  67. {
  68. AioContext *aio_context;
  69. Job *job = find_job(id, &aio_context, errp);
  70. if (!job) {
  71. return;
  72. }
  73. trace_qmp_job_resume(job);
  74. job_user_resume(job, errp);
  75. aio_context_release(aio_context);
  76. }
  77. void qmp_job_complete(const char *id, Error **errp)
  78. {
  79. AioContext *aio_context;
  80. Job *job = find_job(id, &aio_context, errp);
  81. if (!job) {
  82. return;
  83. }
  84. trace_qmp_job_complete(job);
  85. job_complete(job, errp);
  86. aio_context_release(aio_context);
  87. }
  88. void qmp_job_finalize(const char *id, Error **errp)
  89. {
  90. AioContext *aio_context;
  91. Job *job = find_job(id, &aio_context, errp);
  92. if (!job) {
  93. return;
  94. }
  95. trace_qmp_job_finalize(job);
  96. job_finalize(job, errp);
  97. aio_context_release(aio_context);
  98. }
  99. void qmp_job_dismiss(const char *id, Error **errp)
  100. {
  101. AioContext *aio_context;
  102. Job *job = find_job(id, &aio_context, errp);
  103. if (!job) {
  104. return;
  105. }
  106. trace_qmp_job_dismiss(job);
  107. job_dismiss(&job, errp);
  108. aio_context_release(aio_context);
  109. }
  110. static JobInfo *job_query_single(Job *job, Error **errp)
  111. {
  112. JobInfo *info;
  113. assert(!job_is_internal(job));
  114. info = g_new(JobInfo, 1);
  115. *info = (JobInfo) {
  116. .id = g_strdup(job->id),
  117. .type = job_type(job),
  118. .status = job->status,
  119. .current_progress = job->progress_current,
  120. .total_progress = job->progress_total,
  121. .has_error = !!job->err,
  122. .error = job->err ? \
  123. g_strdup(error_get_pretty(job->err)) : NULL,
  124. };
  125. return info;
  126. }
  127. JobInfoList *qmp_query_jobs(Error **errp)
  128. {
  129. JobInfoList *head = NULL, **p_next = &head;
  130. Job *job;
  131. for (job = job_next(NULL); job; job = job_next(job)) {
  132. JobInfoList *elem;
  133. AioContext *aio_context;
  134. if (job_is_internal(job)) {
  135. continue;
  136. }
  137. elem = g_new0(JobInfoList, 1);
  138. aio_context = job->aio_context;
  139. aio_context_acquire(aio_context);
  140. elem->value = job_query_single(job, errp);
  141. aio_context_release(aio_context);
  142. if (!elem->value) {
  143. g_free(elem);
  144. qapi_free_JobInfoList(head);
  145. return NULL;
  146. }
  147. *p_next = elem;
  148. p_next = &elem->next;
  149. }
  150. return head;
  151. }