summaryrefslogtreecommitdiff
path: root/src/driver.c
blob: 35f81566058f90cd0597e7d9f25f9477bcacfe76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * driver.c: Helpers for loading drivers
 *
 * Copyright (C) 2006-2011 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */


#include <config.h>

#include <unistd.h>

#include "driver.h"
#include "viralloc.h"
#include "virfile.h"
#include "virlog.h"
#include "virmodule.h"
#include "virobject.h"
#include "virstring.h"
#include "virthread.h"
#include "virutil.h"
#include "viridentity.h"
#include "datatypes.h"
#include "configmake.h"
#include "virtypedparam.h"

VIR_LOG_INIT("driver");

#define VIR_FROM_THIS VIR_FROM_NONE

/* XXX re-implement this for other OS, or use libtools helper lib ? */
#define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"



int
virDriverLoadModule(const char *name,
                    const char *regfunc,
                    bool required)
{
    g_autofree char *modfile = NULL;

    VIR_DEBUG("Module load %s", name);

    if (!(modfile = virFileFindResourceFull(name,
                                            "libvirt_driver_",
                                            VIR_FILE_MODULE_EXT,
                                            abs_top_builddir "/src",
                                            DEFAULT_DRIVER_DIR,
                                            "LIBVIRT_DRIVER_DIR")))
        return -1;

    return virModuleLoad(modfile, regfunc, required);
}


/* XXX unload modules, but we can't until we can unregister libvirt drivers */

/**
 * virDriverShouldAutostart:
 * @dir: driver's run state directory (usually /var/run/libvirt/$driver)
 * @autostart: whether driver should initiate autostart
 *
 * Automatic starting of libvirt's objects (e.g. domains, networks, storage
 * pools, etc.) doesn't play nice with using '--timeout' on daemon's command
 * line because the objects are attempted to autostart on every start of
 * corresponding driver/daemon. To resolve this problem, a file is created in
 * driver's private directory (which doesn't survive host's reboot) and thus
 * autostart is attempted only once.
 */
int
virDriverShouldAutostart(const char *dir,
                         bool *autostart)
{
    g_autofree char *path = NULL;

    *autostart = false;

    path = g_strdup_printf("%s/autostarted", dir);

    if (virFileExists(path)) {
        VIR_DEBUG("Autostart file %s exists, skipping autostart", path);
        return 0;
    }

    VIR_DEBUG("Autostart file %s does not exist, do autostart", path);
    *autostart = true;

    if (virFileTouch(path, 0600) < 0)
        return -1;

    return 0;
}


virThreadLocal connectInterface;
virThreadLocal connectNetwork;
virThreadLocal connectNWFilter;
virThreadLocal connectNodeDev;
virThreadLocal connectSecret;
virThreadLocal connectStorage;

static int
virConnectCacheOnceInit(void)
{
    if (virThreadLocalInit(&connectInterface, NULL) < 0 ||
        virThreadLocalInit(&connectNetwork, NULL) < 0 ||
        virThreadLocalInit(&connectNWFilter, NULL) < 0 ||
        virThreadLocalInit(&connectNodeDev, NULL) < 0 ||
        virThreadLocalInit(&connectSecret, NULL) < 0 ||
        virThreadLocalInit(&connectStorage, NULL) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to initialize thread local variable"));
        return -1;
    }

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virConnectCache);

static virConnectPtr
virGetConnectGeneric(virThreadLocal *threadPtr, const char *name)
{
    virConnectPtr conn;
    virErrorPtr orig_err;

    if (virConnectCacheInitialize() < 0)
        return NULL;

    conn = virThreadLocalGet(threadPtr);

    if (conn) {
        VIR_DEBUG("Return cached %s connection %p", name, conn);
        virObjectRef(conn);
    } else {
        g_autofree char *uri = NULL;
        const char *uriPath = geteuid() == 0 ? "/system" : "/session";

        uri = g_strdup_printf("%s://%s", name, uriPath);

        conn = virConnectOpen(uri);
        VIR_DEBUG("Opened new %s connection %p", name, conn);
        if (!conn)
            return NULL;

        if (conn->driver->connectSetIdentity != NULL) {
            g_autoptr(virIdentity) ident = NULL;

            VIR_DEBUG("Attempting to delegate current identity");
            ident = virIdentityGetCurrent();
            if (ident) {
                g_autoptr(virTypedParamList) tmp = virIdentityGetParameters(ident);
                virTypedParameterPtr par;
                size_t npar;

                if (virTypedParamListFetch(tmp, &par, &npar) < 0)
                    goto error;

                if (virConnectSetIdentity(conn, par, npar, 0) < 0)
                    goto error;
            }
        }
    }
    return conn;

 error:
    virErrorPreserveLast(&orig_err);
    virConnectClose(conn);
    virErrorRestore(&orig_err);
    return NULL;
}


virConnectPtr virGetConnectInterface(void)
{
    return virGetConnectGeneric(&connectInterface, "interface");
}

virConnectPtr virGetConnectNetwork(void)
{
    return virGetConnectGeneric(&connectNetwork, "network");
}

virConnectPtr virGetConnectNWFilter(void)
{
    return virGetConnectGeneric(&connectNWFilter, "nwfilter");
}

virConnectPtr virGetConnectNodeDev(void)
{
    return virGetConnectGeneric(&connectNodeDev, "nodedev");
}

virConnectPtr virGetConnectSecret(void)
{
    return virGetConnectGeneric(&connectSecret, "secret");
}

virConnectPtr virGetConnectStorage(void)
{
    return virGetConnectGeneric(&connectStorage, "storage");
}


int
virSetConnectInterface(virConnectPtr conn)
{
    if (virConnectCacheInitialize() < 0)
        return -1;

    VIR_DEBUG("Override interface connection with %p", conn);
    return virThreadLocalSet(&connectInterface, conn);
}


int
virSetConnectNetwork(virConnectPtr conn)
{
    if (virConnectCacheInitialize() < 0)
        return -1;

    VIR_DEBUG("Override network connection with %p", conn);
    return virThreadLocalSet(&connectNetwork, conn);
}


int
virSetConnectNWFilter(virConnectPtr conn)
{
    if (virConnectCacheInitialize() < 0)
        return -1;

    VIR_DEBUG("Override nwfilter connection with %p", conn);
    return virThreadLocalSet(&connectNWFilter, conn);
}


int
virSetConnectNodeDev(virConnectPtr conn)
{
    if (virConnectCacheInitialize() < 0)
        return -1;

    VIR_DEBUG("Override nodedev connection with %p", conn);
    return virThreadLocalSet(&connectNodeDev, conn);
}


int
virSetConnectSecret(virConnectPtr conn)
{
    if (virConnectCacheInitialize() < 0)
        return -1;

    VIR_DEBUG("Override secret connection with %p", conn);
    return virThreadLocalSet(&connectSecret, conn);
}


int
virSetConnectStorage(virConnectPtr conn)
{
    if (virConnectCacheInitialize() < 0)
        return -1;

    VIR_DEBUG("Override storage connection with %p", conn);
    return virThreadLocalSet(&connectStorage, conn);
}

bool
virConnectValidateURIPath(const char *uriPath,
                          const char *entityName,
                          bool privileged)
{
    if (privileged) {
        /* TODO: qemu and vbox drivers allow '/session'
         * connections as root. This is not ideal, but changing
         * these drivers to refuse privileged '/session'
         * connections, like everyone else is already doing, can
         * break existing applications. Until we decide what to do,
         * for now we can handle them as exception in this validate
         * function.
         */
        bool compatSessionRoot = (STREQ(entityName, "qemu") ||
                                  STREQ(entityName, "vbox")) &&
                                  STREQ(uriPath, "/session");

        if (STRNEQ(uriPath, "/system") && !compatSessionRoot) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected %1$s URI path '%2$s', try %3$s:///system"),
                           entityName, uriPath, entityName);
            return false;
        }
    } else {
        if (STRNEQ(uriPath, "/session")) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected %1$s URI path '%2$s', try %3$s:///session"),
                           entityName, uriPath, entityName);
            return false;
        }
    }

    return true;
}


/**
 * virDriverFeatureIsGlobal:
 * @feat: a VIR_DRV_FEATURE
 * @supported: If a feature is globally handled
 *
 * Certain driver feature flags are really not for individual drivers to decide
 * whether they implement them or not, but are rather global based on e.g.
 * whether the RPC protocol supports it.
 *
 * This function returns 'true' and fills @supported if a feature is a global
 * feature and the individual driver implementations don't decide whether
 * they support it or not.
 */
bool
virDriverFeatureIsGlobal(virDrvFeature feat,
                         int *supported)

{
    switch (feat) {
    /* This is a special case where the generated remote driver dispatcher
     * function intercepts this specific flag and returns '1'. Thus any local
     * implementation must return 0, so that the return value properly reflects
     * whether we are going through the remote driver */
    case VIR_DRV_FEATURE_REMOTE:
    /* keepalive is handled at RPC level, driver implementations must always
     * return 0, to signal that direct/embedded use doesn't use keepalive */
    case VIR_DRV_FEATURE_PROGRAM_KEEPALIVE:
    /* Support for close callbacks and remote event filtering are both features
     * of the RPC protocol and thus normal drivers must not signal support
     * for them. */
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
        *supported = 0;
        return true;

    /* Limitation of string support in typed parameters was an RPC limitation.
     * At this point everything supports them and thus also drivers need to
     * always advertise this feature */
    case VIR_DRV_FEATURE_TYPED_PARAM_STRING:
    /* Feature flag exposes that the accidental switching of order of arguments
     * in the public API trampoline virNetworkUpdate is known. Updated clients
     * thus use the correct ordering with an updated server. All drivers must
     * signal support for this feature. */
    case VIR_DRV_FEATURE_NETWORK_UPDATE_HAS_CORRECT_ORDER:
    /* The remote driver intercepts and always reports the feature since it was
     * introduced. This means that all driver implementations should advertise
     * it too as it works natively without RPC. Always enabling this will also
     * prevent regressions when a driver is used in embedded mode */
    case VIR_DRV_FEATURE_FD_PASSING:
        *supported = 1;
        return true;

    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_MIGRATION_V3:
    case VIR_DRV_FEATURE_MIGRATION_P2P:
    case VIR_DRV_FEATURE_MIGRATE_CHANGE_PROTECTION:
    case VIR_DRV_FEATURE_XML_MIGRATABLE:
    case VIR_DRV_FEATURE_MIGRATION_OFFLINE:
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
    case VIR_DRV_FEATURE_MIGRATION_V1:
    default:
        return false;
    }
}