xref: /qemu/job.c (revision e7c1d78b)
133e9e9bdSKevin Wolf /*
233e9e9bdSKevin Wolf  * Background jobs (long-running operations)
333e9e9bdSKevin Wolf  *
433e9e9bdSKevin Wolf  * Copyright (c) 2011 IBM Corp.
533e9e9bdSKevin Wolf  * Copyright (c) 2012, 2018 Red Hat, Inc.
633e9e9bdSKevin Wolf  *
733e9e9bdSKevin Wolf  * Permission is hereby granted, free of charge, to any person obtaining a copy
833e9e9bdSKevin Wolf  * of this software and associated documentation files (the "Software"), to deal
933e9e9bdSKevin Wolf  * in the Software without restriction, including without limitation the rights
1033e9e9bdSKevin Wolf  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1133e9e9bdSKevin Wolf  * copies of the Software, and to permit persons to whom the Software is
1233e9e9bdSKevin Wolf  * furnished to do so, subject to the following conditions:
1333e9e9bdSKevin Wolf  *
1433e9e9bdSKevin Wolf  * The above copyright notice and this permission notice shall be included in
1533e9e9bdSKevin Wolf  * all copies or substantial portions of the Software.
1633e9e9bdSKevin Wolf  *
1733e9e9bdSKevin Wolf  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1833e9e9bdSKevin Wolf  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1933e9e9bdSKevin Wolf  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2033e9e9bdSKevin Wolf  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2133e9e9bdSKevin Wolf  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2233e9e9bdSKevin Wolf  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2333e9e9bdSKevin Wolf  * THE SOFTWARE.
2433e9e9bdSKevin Wolf  */
2533e9e9bdSKevin Wolf 
2633e9e9bdSKevin Wolf #include "qemu/osdep.h"
2733e9e9bdSKevin Wolf #include "qemu-common.h"
2833e9e9bdSKevin Wolf #include "qapi/error.h"
2933e9e9bdSKevin Wolf #include "qemu/job.h"
3033e9e9bdSKevin Wolf #include "qemu/id.h"
3133e9e9bdSKevin Wolf 
32*e7c1d78bSKevin Wolf static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
33*e7c1d78bSKevin Wolf 
34252291eaSKevin Wolf JobType job_type(const Job *job)
35252291eaSKevin Wolf {
36252291eaSKevin Wolf     return job->driver->job_type;
37252291eaSKevin Wolf }
38252291eaSKevin Wolf 
39252291eaSKevin Wolf const char *job_type_str(const Job *job)
40252291eaSKevin Wolf {
41252291eaSKevin Wolf     return JobType_str(job_type(job));
42252291eaSKevin Wolf }
43252291eaSKevin Wolf 
44*e7c1d78bSKevin Wolf Job *job_next(Job *job)
45*e7c1d78bSKevin Wolf {
46*e7c1d78bSKevin Wolf     if (!job) {
47*e7c1d78bSKevin Wolf         return QLIST_FIRST(&jobs);
48*e7c1d78bSKevin Wolf     }
49*e7c1d78bSKevin Wolf     return QLIST_NEXT(job, job_list);
50*e7c1d78bSKevin Wolf }
51*e7c1d78bSKevin Wolf 
52*e7c1d78bSKevin Wolf Job *job_get(const char *id)
53*e7c1d78bSKevin Wolf {
54*e7c1d78bSKevin Wolf     Job *job;
55*e7c1d78bSKevin Wolf 
56*e7c1d78bSKevin Wolf     QLIST_FOREACH(job, &jobs, job_list) {
57*e7c1d78bSKevin Wolf         if (job->id && !strcmp(id, job->id)) {
58*e7c1d78bSKevin Wolf             return job;
59*e7c1d78bSKevin Wolf         }
60*e7c1d78bSKevin Wolf     }
61*e7c1d78bSKevin Wolf 
62*e7c1d78bSKevin Wolf     return NULL;
63*e7c1d78bSKevin Wolf }
64*e7c1d78bSKevin Wolf 
6533e9e9bdSKevin Wolf void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
6633e9e9bdSKevin Wolf {
6733e9e9bdSKevin Wolf     Job *job;
6833e9e9bdSKevin Wolf 
6933e9e9bdSKevin Wolf     if (job_id) {
7033e9e9bdSKevin Wolf         if (!id_wellformed(job_id)) {
7133e9e9bdSKevin Wolf             error_setg(errp, "Invalid job ID '%s'", job_id);
7233e9e9bdSKevin Wolf             return NULL;
7333e9e9bdSKevin Wolf         }
74*e7c1d78bSKevin Wolf         if (job_get(job_id)) {
75*e7c1d78bSKevin Wolf             error_setg(errp, "Job ID '%s' already in use", job_id);
76*e7c1d78bSKevin Wolf             return NULL;
77*e7c1d78bSKevin Wolf         }
7833e9e9bdSKevin Wolf     }
7933e9e9bdSKevin Wolf 
8033e9e9bdSKevin Wolf     job = g_malloc0(driver->instance_size);
8133e9e9bdSKevin Wolf     job->driver        = driver;
8233e9e9bdSKevin Wolf     job->id            = g_strdup(job_id);
8333e9e9bdSKevin Wolf 
84*e7c1d78bSKevin Wolf     QLIST_INSERT_HEAD(&jobs, job, job_list);
85*e7c1d78bSKevin Wolf 
8633e9e9bdSKevin Wolf     return job;
8733e9e9bdSKevin Wolf }
88fd61a701SKevin Wolf 
89fd61a701SKevin Wolf void job_delete(Job *job)
90fd61a701SKevin Wolf {
91*e7c1d78bSKevin Wolf     QLIST_REMOVE(job, job_list);
92*e7c1d78bSKevin Wolf 
93fd61a701SKevin Wolf     g_free(job->id);
94fd61a701SKevin Wolf     g_free(job);
95fd61a701SKevin Wolf }
96