OSDN Git Service

Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qmiga/qemu.git] / job-qmp.c
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-common.h"
28 #include "qemu/job.h"
29 #include "qapi/qapi-commands-job.h"
30 #include "qapi/error.h"
31 #include "trace-root.h"
32
33 /* Get a job using its ID and acquire its AioContext */
34 static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
35 {
36     Job *job;
37
38     *aio_context = NULL;
39
40     job = job_get(id);
41     if (!job) {
42         error_setg(errp, "Job not found");
43         return NULL;
44     }
45
46     *aio_context = job->aio_context;
47     aio_context_acquire(*aio_context);
48
49     return job;
50 }
51
52 void qmp_job_cancel(const char *id, Error **errp)
53 {
54     AioContext *aio_context;
55     Job *job = find_job(id, &aio_context, errp);
56
57     if (!job) {
58         return;
59     }
60
61     trace_qmp_job_cancel(job);
62     job_user_cancel(job, true, errp);
63     aio_context_release(aio_context);
64 }
65
66 void qmp_job_pause(const char *id, Error **errp)
67 {
68     AioContext *aio_context;
69     Job *job = find_job(id, &aio_context, errp);
70
71     if (!job) {
72         return;
73     }
74
75     trace_qmp_job_pause(job);
76     job_user_pause(job, errp);
77     aio_context_release(aio_context);
78 }
79
80 void qmp_job_resume(const char *id, Error **errp)
81 {
82     AioContext *aio_context;
83     Job *job = find_job(id, &aio_context, errp);
84
85     if (!job) {
86         return;
87     }
88
89     trace_qmp_job_resume(job);
90     job_user_resume(job, errp);
91     aio_context_release(aio_context);
92 }
93
94 void qmp_job_complete(const char *id, Error **errp)
95 {
96     AioContext *aio_context;
97     Job *job = find_job(id, &aio_context, errp);
98
99     if (!job) {
100         return;
101     }
102
103     trace_qmp_job_complete(job);
104     job_complete(job, errp);
105     aio_context_release(aio_context);
106 }
107
108 void qmp_job_finalize(const char *id, Error **errp)
109 {
110     AioContext *aio_context;
111     Job *job = find_job(id, &aio_context, errp);
112
113     if (!job) {
114         return;
115     }
116
117     trace_qmp_job_finalize(job);
118     job_finalize(job, errp);
119     aio_context_release(aio_context);
120 }
121
122 void qmp_job_dismiss(const char *id, Error **errp)
123 {
124     AioContext *aio_context;
125     Job *job = find_job(id, &aio_context, errp);
126
127     if (!job) {
128         return;
129     }
130
131     trace_qmp_job_dismiss(job);
132     job_dismiss(&job, errp);
133     aio_context_release(aio_context);
134 }
135
136 static JobInfo *job_query_single(Job *job, Error **errp)
137 {
138     JobInfo *info;
139     const char *errmsg = NULL;
140
141     assert(!job_is_internal(job));
142
143     if (job->ret < 0) {
144         errmsg = strerror(-job->ret);
145     }
146
147     info = g_new(JobInfo, 1);
148     *info = (JobInfo) {
149         .id                 = g_strdup(job->id),
150         .type               = job_type(job),
151         .status             = job->status,
152         .current_progress   = job->progress_current,
153         .total_progress     = job->progress_total,
154         .has_error          = !!errmsg,
155         .error              = g_strdup(errmsg),
156     };
157
158     return info;
159 }
160
161 JobInfoList *qmp_query_jobs(Error **errp)
162 {
163     JobInfoList *head = NULL, **p_next = &head;
164     Job *job;
165
166     for (job = job_next(NULL); job; job = job_next(job)) {
167         JobInfoList *elem;
168         AioContext *aio_context;
169
170         if (job_is_internal(job)) {
171             continue;
172         }
173         elem = g_new0(JobInfoList, 1);
174         aio_context = job->aio_context;
175         aio_context_acquire(aio_context);
176         elem->value = job_query_single(job, errp);
177         aio_context_release(aio_context);
178         if (!elem->value) {
179             g_free(elem);
180             qapi_free_JobInfoList(head);
181             return NULL;
182         }
183         *p_next = elem;
184         p_next = &elem->next;
185     }
186
187     return head;
188 }