2 * QMP interface for background jobs
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include "qemu/osdep.h"
28 #include "qapi/qapi-commands-job.h"
29 #include "qapi/error.h"
30 #include "trace/trace-root.h"
33 * Get a job using its ID. Called with job_mutex held.
35 static Job
*find_job_locked(const char *id
, Error
**errp
)
39 job
= job_get_locked(id
);
41 error_setg(errp
, "Job not found");
48 void qmp_job_cancel(const char *id
, Error
**errp
)
53 job
= find_job_locked(id
, errp
);
59 trace_qmp_job_cancel(job
);
60 job_user_cancel_locked(job
, true, errp
);
63 void qmp_job_pause(const char *id
, Error
**errp
)
68 job
= find_job_locked(id
, errp
);
74 trace_qmp_job_pause(job
);
75 job_user_pause_locked(job
, errp
);
78 void qmp_job_resume(const char *id
, Error
**errp
)
83 job
= find_job_locked(id
, errp
);
89 trace_qmp_job_resume(job
);
90 job_user_resume_locked(job
, errp
);
93 void qmp_job_complete(const char *id
, Error
**errp
)
98 job
= find_job_locked(id
, errp
);
104 trace_qmp_job_complete(job
);
105 job_complete_locked(job
, errp
);
108 void qmp_job_finalize(const char *id
, Error
**errp
)
113 job
= find_job_locked(id
, errp
);
119 trace_qmp_job_finalize(job
);
121 job_finalize_locked(job
, errp
);
123 job_unref_locked(job
);
126 void qmp_job_dismiss(const char *id
, Error
**errp
)
131 job
= find_job_locked(id
, errp
);
137 trace_qmp_job_dismiss(job
);
138 job_dismiss_locked(&job
, errp
);
141 /* Called with job_mutex held. */
142 static JobInfo
*job_query_single_locked(Job
*job
, Error
**errp
)
145 uint64_t progress_current
;
146 uint64_t progress_total
;
148 assert(!job_is_internal(job
));
149 progress_get_snapshot(&job
->progress
, &progress_current
,
152 info
= g_new(JobInfo
, 1);
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
,
160 g_strdup(error_get_pretty(job
->err
)) : NULL
,
166 JobInfoList
*qmp_query_jobs(Error
**errp
)
168 JobInfoList
*head
= NULL
, **tail
= &head
;
173 for (job
= job_next_locked(NULL
); job
; job
= job_next_locked(job
)) {
176 if (job_is_internal(job
)) {
179 value
= job_query_single_locked(job
, errp
);
181 qapi_free_JobInfoList(head
);
184 QAPI_LIST_APPEND(tail
, value
);