qemu
/
job-qmp.c
188 строк · 4.3 Кб
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
26#include "qemu/osdep.h"
27#include "qemu/job.h"
28#include "qapi/qapi-commands-job.h"
29#include "qapi/error.h"
30#include "trace/trace-root.h"
31
32/*
33* Get a job using its ID. Called with job_mutex held.
34*/
35static Job *find_job_locked(const char *id, Error **errp)
36{
37Job *job;
38
39job = job_get_locked(id);
40if (!job) {
41error_setg(errp, "Job not found");
42return NULL;
43}
44
45return job;
46}
47
48void qmp_job_cancel(const char *id, Error **errp)
49{
50Job *job;
51
52JOB_LOCK_GUARD();
53job = find_job_locked(id, errp);
54
55if (!job) {
56return;
57}
58
59trace_qmp_job_cancel(job);
60job_user_cancel_locked(job, true, errp);
61}
62
63void qmp_job_pause(const char *id, Error **errp)
64{
65Job *job;
66
67JOB_LOCK_GUARD();
68job = find_job_locked(id, errp);
69
70if (!job) {
71return;
72}
73
74trace_qmp_job_pause(job);
75job_user_pause_locked(job, errp);
76}
77
78void qmp_job_resume(const char *id, Error **errp)
79{
80Job *job;
81
82JOB_LOCK_GUARD();
83job = find_job_locked(id, errp);
84
85if (!job) {
86return;
87}
88
89trace_qmp_job_resume(job);
90job_user_resume_locked(job, errp);
91}
92
93void qmp_job_complete(const char *id, Error **errp)
94{
95Job *job;
96
97JOB_LOCK_GUARD();
98job = find_job_locked(id, errp);
99
100if (!job) {
101return;
102}
103
104trace_qmp_job_complete(job);
105job_complete_locked(job, errp);
106}
107
108void qmp_job_finalize(const char *id, Error **errp)
109{
110Job *job;
111
112JOB_LOCK_GUARD();
113job = find_job_locked(id, errp);
114
115if (!job) {
116return;
117}
118
119trace_qmp_job_finalize(job);
120job_ref_locked(job);
121job_finalize_locked(job, errp);
122
123job_unref_locked(job);
124}
125
126void qmp_job_dismiss(const char *id, Error **errp)
127{
128Job *job;
129
130JOB_LOCK_GUARD();
131job = find_job_locked(id, errp);
132
133if (!job) {
134return;
135}
136
137trace_qmp_job_dismiss(job);
138job_dismiss_locked(&job, errp);
139}
140
141/* Called with job_mutex held. */
142static JobInfo *job_query_single_locked(Job *job, Error **errp)
143{
144JobInfo *info;
145uint64_t progress_current;
146uint64_t progress_total;
147
148assert(!job_is_internal(job));
149progress_get_snapshot(&job->progress, &progress_current,
150&progress_total);
151
152info = g_new(JobInfo, 1);
153*info = (JobInfo) {
154.id = g_strdup(job->id),
155.type = job_type(job),
156.status = job->status,
157.current_progress = progress_current,
158.total_progress = progress_total,
159.error = job->err ?
160g_strdup(error_get_pretty(job->err)) : NULL,
161};
162
163return info;
164}
165
166JobInfoList *qmp_query_jobs(Error **errp)
167{
168JobInfoList *head = NULL, **tail = &head;
169Job *job;
170
171JOB_LOCK_GUARD();
172
173for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
174JobInfo *value;
175
176if (job_is_internal(job)) {
177continue;
178}
179value = job_query_single_locked(job, errp);
180if (!value) {
181qapi_free_JobInfoList(head);
182return NULL;
183}
184QAPI_LIST_APPEND(tail, value);
185}
186
187return head;
188}
189