xref: /qemu/job.c (revision fd61a701)
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 
32252291eaSKevin Wolf JobType job_type(const Job *job)
33252291eaSKevin Wolf {
34252291eaSKevin Wolf     return job->driver->job_type;
35252291eaSKevin Wolf }
36252291eaSKevin Wolf 
37252291eaSKevin Wolf const char *job_type_str(const Job *job)
38252291eaSKevin Wolf {
39252291eaSKevin Wolf     return JobType_str(job_type(job));
40252291eaSKevin Wolf }
41252291eaSKevin Wolf 
4233e9e9bdSKevin Wolf void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
4333e9e9bdSKevin Wolf {
4433e9e9bdSKevin Wolf     Job *job;
4533e9e9bdSKevin Wolf 
4633e9e9bdSKevin Wolf     if (job_id) {
4733e9e9bdSKevin Wolf         if (!id_wellformed(job_id)) {
4833e9e9bdSKevin Wolf             error_setg(errp, "Invalid job ID '%s'", job_id);
4933e9e9bdSKevin Wolf             return NULL;
5033e9e9bdSKevin Wolf         }
5133e9e9bdSKevin Wolf     }
5233e9e9bdSKevin Wolf 
5333e9e9bdSKevin Wolf     job = g_malloc0(driver->instance_size);
5433e9e9bdSKevin Wolf     job->driver        = driver;
5533e9e9bdSKevin Wolf     job->id            = g_strdup(job_id);
5633e9e9bdSKevin Wolf 
5733e9e9bdSKevin Wolf     return job;
5833e9e9bdSKevin Wolf }
59*fd61a701SKevin Wolf 
60*fd61a701SKevin Wolf void job_delete(Job *job)
61*fd61a701SKevin Wolf {
62*fd61a701SKevin Wolf     g_free(job->id);
63*fd61a701SKevin Wolf     g_free(job);
64*fd61a701SKevin Wolf }
65