Statistics
| Branch: | Revision:

root / qga / guest-agent-commands.c @ e3d4d252

History | View | Annotate | Download (13 kB)

1
/*
2
 * QEMU Guest Agent commands
3
 *
4
 * Copyright IBM Corp. 2011
5
 *
6
 * Authors:
7
 *  Michael Roth      <mdroth@linux.vnet.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10
 * See the COPYING file in the top-level directory.
11
 */
12

    
13
#include <glib.h>
14
#include <mntent.h>
15
#include <sys/types.h>
16
#include <sys/ioctl.h>
17
#include <linux/fs.h>
18
#include "qga/guest-agent-core.h"
19
#include "qga-qmp-commands.h"
20
#include "qerror.h"
21
#include "qemu-queue.h"
22

    
23
static GAState *ga_state;
24

    
25
static void disable_logging(void)
26
{
27
    ga_disable_logging(ga_state);
28
}
29

    
30
static void enable_logging(void)
31
{
32
    ga_enable_logging(ga_state);
33
}
34

    
35
/* Note: in some situations, like with the fsfreeze, logging may be
36
 * temporarilly disabled. if it is necessary that a command be able
37
 * to log for accounting purposes, check ga_logging_enabled() beforehand,
38
 * and use the QERR_QGA_LOGGING_DISABLED to generate an error
39
 */
40
static void slog(const char *fmt, ...)
41
{
42
    va_list ap;
43

    
44
    va_start(ap, fmt);
45
    g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
46
    va_end(ap);
47
}
48

    
49
int64_t qmp_guest_sync(int64_t id, Error **errp)
50
{
51
    return id;
52
}
53

    
54
void qmp_guest_ping(Error **err)
55
{
56
    slog("guest-ping called");
57
}
58

    
59
struct GuestAgentInfo *qmp_guest_info(Error **err)
60
{
61
    GuestAgentInfo *info = qemu_mallocz(sizeof(GuestAgentInfo));
62

    
63
    info->version = g_strdup(QGA_VERSION);
64

    
65
    return info;
66
}
67

    
68
void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
69
{
70
    int ret;
71
    const char *shutdown_flag;
72

    
73
    slog("guest-shutdown called, mode: %s", mode);
74
    if (!has_mode || strcmp(mode, "powerdown") == 0) {
75
        shutdown_flag = "-P";
76
    } else if (strcmp(mode, "halt") == 0) {
77
        shutdown_flag = "-H";
78
    } else if (strcmp(mode, "reboot") == 0) {
79
        shutdown_flag = "-r";
80
    } else {
81
        error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
82
                  "halt|powerdown|reboot");
83
        return;
84
    }
85

    
86
    ret = fork();
87
    if (ret == 0) {
88
        /* child, start the shutdown */
89
        setsid();
90
        fclose(stdin);
91
        fclose(stdout);
92
        fclose(stderr);
93

    
94
        ret = execl("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
95
                    "hypervisor initiated shutdown", (char*)NULL);
96
        if (ret) {
97
            slog("guest-shutdown failed: %s", strerror(errno));
98
        }
99
        exit(!!ret);
100
    } else if (ret < 0) {
101
        error_set(err, QERR_UNDEFINED_ERROR);
102
    }
103
}
104

    
105
typedef struct GuestFileHandle {
106
    uint64_t id;
107
    FILE *fh;
108
    QTAILQ_ENTRY(GuestFileHandle) next;
109
} GuestFileHandle;
110

    
111
static struct {
112
    QTAILQ_HEAD(, GuestFileHandle) filehandles;
113
} guest_file_state;
114

    
115
static void guest_file_handle_add(FILE *fh)
116
{
117
    GuestFileHandle *gfh;
118

    
119
    gfh = qemu_mallocz(sizeof(GuestFileHandle));
120
    gfh->id = fileno(fh);
121
    gfh->fh = fh;
122
    QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
123
}
124

    
125
static GuestFileHandle *guest_file_handle_find(int64_t id)
126
{
127
    GuestFileHandle *gfh;
128

    
129
    QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
130
    {
131
        if (gfh->id == id) {
132
            return gfh;
133
        }
134
    }
135

    
136
    return NULL;
137
}
138

    
139
int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
140
{
141
    FILE *fh;
142
    int fd;
143
    int64_t ret = -1;
144

    
145
    if (!has_mode) {
146
        mode = "r";
147
    }
148
    slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
149
    fh = fopen(path, mode);
150
    if (!fh) {
151
        error_set(err, QERR_OPEN_FILE_FAILED, path);
152
        return -1;
153
    }
154

    
155
    /* set fd non-blocking to avoid common use cases (like reading from a
156
     * named pipe) from hanging the agent
157
     */
158
    fd = fileno(fh);
159
    ret = fcntl(fd, F_GETFL);
160
    ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
161
    if (ret == -1) {
162
        error_set(err, QERR_QGA_COMMAND_FAILED, "fcntl() failed");
163
        fclose(fh);
164
        return -1;
165
    }
166

    
167
    guest_file_handle_add(fh);
168
    slog("guest-file-open, handle: %d", fd);
169
    return fd;
170
}
171

    
172
void qmp_guest_file_close(int64_t handle, Error **err)
173
{
174
    GuestFileHandle *gfh = guest_file_handle_find(handle);
175
    int ret;
176

    
177
    slog("guest-file-close called, handle: %ld", handle);
178
    if (!gfh) {
179
        error_set(err, QERR_FD_NOT_FOUND, "handle");
180
        return;
181
    }
182

    
183
    ret = fclose(gfh->fh);
184
    if (ret == -1) {
185
        error_set(err, QERR_QGA_COMMAND_FAILED, "fclose() failed");
186
        return;
187
    }
188

    
189
    QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
190
    qemu_free(gfh);
191
}
192

    
193
struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
194
                                          int64_t count, Error **err)
195
{
196
    GuestFileHandle *gfh = guest_file_handle_find(handle);
197
    GuestFileRead *read_data = NULL;
198
    guchar *buf;
199
    FILE *fh;
200
    size_t read_count;
201

    
202
    if (!gfh) {
203
        error_set(err, QERR_FD_NOT_FOUND, "handle");
204
        return NULL;
205
    }
206

    
207
    if (!has_count) {
208
        count = QGA_READ_COUNT_DEFAULT;
209
    } else if (count < 0) {
210
        error_set(err, QERR_INVALID_PARAMETER, "count");
211
        return NULL;
212
    }
213

    
214
    fh = gfh->fh;
215
    buf = qemu_mallocz(count+1);
216
    read_count = fread(buf, 1, count, fh);
217
    if (ferror(fh)) {
218
        slog("guest-file-read failed, handle: %ld", handle);
219
        error_set(err, QERR_QGA_COMMAND_FAILED, "fread() failed");
220
    } else {
221
        buf[read_count] = 0;
222
        read_data = qemu_mallocz(sizeof(GuestFileRead));
223
        read_data->count = read_count;
224
        read_data->eof = feof(fh);
225
        if (read_count) {
226
            read_data->buf_b64 = g_base64_encode(buf, read_count);
227
        }
228
    }
229
    qemu_free(buf);
230
    clearerr(fh);
231

    
232
    return read_data;
233
}
234

    
235
GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
236
                                     bool has_count, int64_t count, Error **err)
237
{
238
    GuestFileWrite *write_data = NULL;
239
    guchar *buf;
240
    gsize buf_len;
241
    int write_count;
242
    GuestFileHandle *gfh = guest_file_handle_find(handle);
243
    FILE *fh;
244

    
245
    if (!gfh) {
246
        error_set(err, QERR_FD_NOT_FOUND, "handle");
247
        return NULL;
248
    }
249

    
250
    fh = gfh->fh;
251
    buf = g_base64_decode(buf_b64, &buf_len);
252

    
253
    if (!has_count) {
254
        count = buf_len;
255
    } else if (count < 0 || count > buf_len) {
256
        qemu_free(buf);
257
        error_set(err, QERR_INVALID_PARAMETER, "count");
258
        return NULL;
259
    }
260

    
261
    write_count = fwrite(buf, 1, count, fh);
262
    if (ferror(fh)) {
263
        slog("guest-file-write failed, handle: %ld", handle);
264
        error_set(err, QERR_QGA_COMMAND_FAILED, "fwrite() error");
265
    } else {
266
        write_data = qemu_mallocz(sizeof(GuestFileWrite));
267
        write_data->count = write_count;
268
        write_data->eof = feof(fh);
269
    }
270
    qemu_free(buf);
271
    clearerr(fh);
272

    
273
    return write_data;
274
}
275

    
276
struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
277
                                          int64_t whence, Error **err)
278
{
279
    GuestFileHandle *gfh = guest_file_handle_find(handle);
280
    GuestFileSeek *seek_data = NULL;
281
    FILE *fh;
282
    int ret;
283

    
284
    if (!gfh) {
285
        error_set(err, QERR_FD_NOT_FOUND, "handle");
286
        return NULL;
287
    }
288

    
289
    fh = gfh->fh;
290
    ret = fseek(fh, offset, whence);
291
    if (ret == -1) {
292
        error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
293
    } else {
294
        seek_data = qemu_mallocz(sizeof(GuestFileRead));
295
        seek_data->position = ftell(fh);
296
        seek_data->eof = feof(fh);
297
    }
298
    clearerr(fh);
299

    
300
    return seek_data;
301
}
302

    
303
void qmp_guest_file_flush(int64_t handle, Error **err)
304
{
305
    GuestFileHandle *gfh = guest_file_handle_find(handle);
306
    FILE *fh;
307
    int ret;
308

    
309
    if (!gfh) {
310
        error_set(err, QERR_FD_NOT_FOUND, "handle");
311
        return;
312
    }
313

    
314
    fh = gfh->fh;
315
    ret = fflush(fh);
316
    if (ret == EOF) {
317
        error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
318
    }
319
}
320

    
321
static void guest_file_init(void)
322
{
323
    QTAILQ_INIT(&guest_file_state.filehandles);
324
}
325

    
326
typedef struct GuestFsfreezeMount {
327
    char *dirname;
328
    char *devtype;
329
    QTAILQ_ENTRY(GuestFsfreezeMount) next;
330
} GuestFsfreezeMount;
331

    
332
struct {
333
    GuestFsfreezeStatus status;
334
    QTAILQ_HEAD(, GuestFsfreezeMount) mount_list;
335
} guest_fsfreeze_state;
336

    
337
/*
338
 * Walk the mount table and build a list of local file systems
339
 */
340
static int guest_fsfreeze_build_mount_list(void)
341
{
342
    struct mntent *ment;
343
    GuestFsfreezeMount *mount, *temp;
344
    char const *mtab = MOUNTED;
345
    FILE *fp;
346

    
347
    QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
348
        QTAILQ_REMOVE(&guest_fsfreeze_state.mount_list, mount, next);
349
        qemu_free(mount->dirname);
350
        qemu_free(mount->devtype);
351
        qemu_free(mount);
352
    }
353

    
354
    fp = setmntent(mtab, "r");
355
    if (!fp) {
356
        g_warning("fsfreeze: unable to read mtab");
357
        return -1;
358
    }
359

    
360
    while ((ment = getmntent(fp))) {
361
        /*
362
         * An entry which device name doesn't start with a '/' is
363
         * either a dummy file system or a network file system.
364
         * Add special handling for smbfs and cifs as is done by
365
         * coreutils as well.
366
         */
367
        if ((ment->mnt_fsname[0] != '/') ||
368
            (strcmp(ment->mnt_type, "smbfs") == 0) ||
369
            (strcmp(ment->mnt_type, "cifs") == 0)) {
370
            continue;
371
        }
372

    
373
        mount = qemu_mallocz(sizeof(GuestFsfreezeMount));
374
        mount->dirname = qemu_strdup(ment->mnt_dir);
375
        mount->devtype = qemu_strdup(ment->mnt_type);
376

    
377
        QTAILQ_INSERT_TAIL(&guest_fsfreeze_state.mount_list, mount, next);
378
    }
379

    
380
    endmntent(fp);
381

    
382
    return 0;
383
}
384

    
385
/*
386
 * Return status of freeze/thaw
387
 */
388
GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
389
{
390
    return guest_fsfreeze_state.status;
391
}
392

    
393
/*
394
 * Walk list of mounted file systems in the guest, and freeze the ones which
395
 * are real local file systems.
396
 */
397
int64_t qmp_guest_fsfreeze_freeze(Error **err)
398
{
399
    int ret = 0, i = 0;
400
    struct GuestFsfreezeMount *mount, *temp;
401
    int fd;
402
    char err_msg[512];
403

    
404
    slog("guest-fsfreeze called");
405

    
406
    if (guest_fsfreeze_state.status == GUEST_FSFREEZE_STATUS_FROZEN) {
407
        return 0;
408
    }
409

    
410
    ret = guest_fsfreeze_build_mount_list();
411
    if (ret < 0) {
412
        return ret;
413
    }
414

    
415
    /* cannot risk guest agent blocking itself on a write in this state */
416
    disable_logging();
417

    
418
    QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
419
        fd = qemu_open(mount->dirname, O_RDONLY);
420
        if (fd == -1) {
421
            sprintf(err_msg, "failed to open %s, %s", mount->dirname, strerror(errno));
422
            error_set(err, QERR_QGA_COMMAND_FAILED, err_msg);
423
            goto error;
424
        }
425

    
426
        /* we try to cull filesytems we know won't work in advance, but other
427
         * filesytems may not implement fsfreeze for less obvious reasons.
428
         * these will report EOPNOTSUPP, so we simply ignore them. when
429
         * thawing, these filesystems will return an EINVAL instead, due to
430
         * not being in a frozen state. Other filesystem-specific
431
         * errors may result in EINVAL, however, so the user should check the
432
         * number * of filesystems returned here against those returned by the
433
         * thaw operation to determine whether everything completed
434
         * successfully
435
         */
436
        ret = ioctl(fd, FIFREEZE);
437
        if (ret < 0 && errno != EOPNOTSUPP) {
438
            sprintf(err_msg, "failed to freeze %s, %s", mount->dirname, strerror(errno));
439
            error_set(err, QERR_QGA_COMMAND_FAILED, err_msg);
440
            close(fd);
441
            goto error;
442
        }
443
        close(fd);
444

    
445
        i++;
446
    }
447

    
448
    guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_FROZEN;
449
    return i;
450

    
451
error:
452
    if (i > 0) {
453
        qmp_guest_fsfreeze_thaw(NULL);
454
    }
455
    return 0;
456
}
457

    
458
/*
459
 * Walk list of frozen file systems in the guest, and thaw them.
460
 */
461
int64_t qmp_guest_fsfreeze_thaw(Error **err)
462
{
463
    int ret;
464
    GuestFsfreezeMount *mount, *temp;
465
    int fd, i = 0;
466
    bool has_error = false;
467

    
468
    QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
469
        fd = qemu_open(mount->dirname, O_RDONLY);
470
        if (fd == -1) {
471
            has_error = true;
472
            continue;
473
        }
474
        ret = ioctl(fd, FITHAW);
475
        if (ret < 0 && errno != EOPNOTSUPP && errno != EINVAL) {
476
            has_error = true;
477
            close(fd);
478
            continue;
479
        }
480
        close(fd);
481
        i++;
482
    }
483

    
484
    if (has_error) {
485
        guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_ERROR;
486
    } else {
487
        guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
488
    }
489
    enable_logging();
490
    return i;
491
}
492

    
493
static void guest_fsfreeze_init(void)
494
{
495
    guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
496
    QTAILQ_INIT(&guest_fsfreeze_state.mount_list);
497
}
498

    
499
static void guest_fsfreeze_cleanup(void)
500
{
501
    int64_t ret;
502
    Error *err = NULL;
503

    
504
    if (guest_fsfreeze_state.status == GUEST_FSFREEZE_STATUS_FROZEN) {
505
        ret = qmp_guest_fsfreeze_thaw(&err);
506
        if (ret < 0 || err) {
507
            slog("failed to clean up frozen filesystems");
508
        }
509
    }
510
}
511

    
512
/* register init/cleanup routines for stateful command groups */
513
void ga_command_state_init(GAState *s, GACommandState *cs)
514
{
515
    ga_state = s;
516
    ga_command_state_add(cs, guest_fsfreeze_init, guest_fsfreeze_cleanup);
517
    ga_command_state_add(cs, guest_file_init, NULL);
518
}