~ubuntu-branches/ubuntu/maverick/libvirt/maverick

« back to all changes in this revision

Viewing changes to qemud/qemud.c

Tags: upstream-0.6.4
Import upstream version 0.6.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
#ifdef WITH_UML
76
76
#include "uml_driver.h"
77
77
#endif
 
78
#ifdef WITH_ONE
 
79
#include "opennebula/one_driver.h"
 
80
#endif
78
81
#ifdef WITH_NETWORK
79
82
#include "network_driver.h"
80
83
#endif
371
374
    virMutexUnlock(&server->lock);
372
375
}
373
376
 
374
 
int qemudSetCloseExec(int fd) {
375
 
    int flags;
376
 
    if ((flags = fcntl(fd, F_GETFD)) < 0)
377
 
        goto error;
378
 
    flags |= FD_CLOEXEC;
379
 
    if ((fcntl(fd, F_SETFD, flags)) < 0)
380
 
        goto error;
381
 
    return 0;
382
 
 error:
383
 
    VIR_ERROR0(_("Failed to set close-on-exec file descriptor flag"));
384
 
    return -1;
385
 
}
386
 
 
387
 
 
388
 
int qemudSetNonBlock(int fd) {
389
 
    int flags;
390
 
    if ((flags = fcntl(fd, F_GETFL)) < 0)
391
 
        goto error;
392
 
    flags |= O_NONBLOCK;
393
 
    if ((fcntl(fd, F_SETFL, flags)) < 0)
394
 
        goto error;
395
 
    return 0;
396
 
 error:
397
 
    VIR_ERROR0(_("Failed to set non-blocking file descriptor flag"));
398
 
    return -1;
399
 
}
400
377
 
401
378
static int qemudGoDaemon(void) {
402
379
    int pid = fork();
525
502
        goto cleanup;
526
503
    }
527
504
 
528
 
    if (qemudSetCloseExec(sock->fd) < 0 ||
529
 
        qemudSetNonBlock(sock->fd) < 0)
 
505
    if (virSetCloseExec(sock->fd) < 0 ||
 
506
        virSetNonBlock(sock->fd) < 0)
530
507
        goto cleanup;
531
508
 
532
509
    memset(&addr, 0, sizeof(addr));
687
664
        else
688
665
            sock->port = -1;
689
666
 
690
 
        if (qemudSetCloseExec(sock->fd) < 0 ||
691
 
            qemudSetNonBlock(sock->fd) < 0)
 
667
        if (virSetCloseExec(sock->fd) < 0 ||
 
668
            virSetNonBlock(sock->fd) < 0)
692
669
            goto cleanup;
693
670
 
694
671
        if (listen (sock->fd, 30) < 0) {
841
818
    virDriverLoadModule("qemu");
842
819
    virDriverLoadModule("lxc");
843
820
    virDriverLoadModule("uml");
 
821
    virDriverLoadModule("one");
844
822
#else
845
823
#ifdef WITH_NETWORK
846
824
    networkRegister();
861
839
#ifdef WITH_UML
862
840
    umlRegister();
863
841
#endif
 
842
#ifdef WITH_ONE
 
843
    oneRegister();
 
844
#endif
864
845
#endif
865
846
 
866
847
    virEventRegisterImpl(virEventAddHandleImpl,
1273
1254
    setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (void *)&no_slow_start,
1274
1255
                sizeof no_slow_start);
1275
1256
 
1276
 
    if (qemudSetCloseExec(fd) < 0 ||
1277
 
        qemudSetNonBlock(fd) < 0) {
 
1257
    if (virSetCloseExec(fd) < 0 ||
 
1258
        virSetNonBlock(fd) < 0) {
1278
1259
        close(fd);
1279
1260
        return -1;
1280
1261
    }
1397
1378
 * jobs have finished, then clean it up elsehwere
1398
1379
 */
1399
1380
void qemudDispatchClientFailure(struct qemud_client *client) {
1400
 
    virEventRemoveHandleImpl(client->watch);
 
1381
    if (client->watch != -1) {
 
1382
        virEventRemoveHandleImpl(client->watch);
 
1383
        client->watch = -1;
 
1384
    }
1401
1385
 
1402
1386
    /* Deregister event delivery callback */
1403
1387
    if(client->conn) {
1406
1390
    }
1407
1391
 
1408
1392
#if HAVE_SASL
1409
 
    if (client->saslconn) sasl_dispose(&client->saslconn);
 
1393
    if (client->saslconn) {
 
1394
        sasl_dispose(&client->saslconn);
 
1395
        client->saslconn = NULL;
 
1396
    }
1410
1397
    free(client->saslUsername);
 
1398
    client->saslUsername = NULL;
1411
1399
#endif
1412
 
    if (client->tlssession) gnutls_deinit (client->tlssession);
1413
 
    close(client->fd);
1414
 
    client->fd = -1;
 
1400
    if (client->tlssession) {
 
1401
        gnutls_deinit (client->tlssession);
 
1402
        client->tlssession = NULL;
 
1403
    }
 
1404
    if (client->fd != -1) {
 
1405
        close(client->fd);
 
1406
        client->fd = -1;
 
1407
    }
1415
1408
}
1416
1409
 
1417
1410
 
2529
2522
    char *unix_sock_ro_perms = NULL;
2530
2523
    char *unix_sock_rw_perms = NULL;
2531
2524
    char *unix_sock_group = NULL;
 
2525
    char *buf = NULL;
2532
2526
 
2533
2527
#if HAVE_POLKIT
2534
2528
    /* Change the default back to no auth for non-root */
2574
2568
        if (getuid() != 0) {
2575
2569
            VIR_WARN0(_("Cannot set group when not running as root"));
2576
2570
        } else {
2577
 
            char buf[1024];
 
2571
            int ret;
2578
2572
            struct group grpdata, *grp;
2579
 
            if (getgrnam_r(unix_sock_group, &grpdata, buf, sizeof(buf), &grp) != 0 || !grp) {
 
2573
            size_t maxbuf = sysconf(_SC_GETGR_R_SIZE_MAX);
 
2574
 
 
2575
            if (maxbuf == -1)
 
2576
                maxbuf = 1024;
 
2577
 
 
2578
            if (VIR_ALLOC_N(buf, maxbuf) < 0) {
 
2579
                VIR_ERROR("%s", _("Failed to allocate memory for buffer"));
 
2580
                goto free_and_fail;
 
2581
            }
 
2582
 
 
2583
            while ((ret = getgrnam_r(unix_sock_group, &grpdata,
 
2584
                                     buf, maxbuf,
 
2585
                                     &grp)) == ERANGE) {
 
2586
                    maxbuf *= 2;
 
2587
                    if (maxbuf > 65536 || VIR_REALLOC_N(buf, maxbuf) < 0) {
 
2588
                        VIR_ERROR("%s", _("Failed to reallocate enough memory for buffer"));
 
2589
                        goto free_and_fail;
 
2590
                    }
 
2591
            }
 
2592
 
 
2593
            if (ret != 0 || !grp) {
2580
2594
                VIR_ERROR(_("Failed to lookup group '%s'"), unix_sock_group);
2581
2595
                goto free_and_fail;
2582
2596
            }
2583
2597
            unix_sock_gid = grp->gr_gid;
 
2598
            VIR_FREE (buf);
2584
2599
        }
2585
2600
        free (unix_sock_group);
2586
2601
        unix_sock_group = NULL;
2643
2658
    free (unix_sock_ro_perms);
2644
2659
    free (unix_sock_rw_perms);
2645
2660
    free (unix_sock_group);
 
2661
    VIR_FREE (buf);
2646
2662
 
2647
2663
    /* Don't bother trying to free listen_addr, tcp_port, tls_port, key_file,
2648
2664
       cert_file, ca_file, or crl_file, since they are initialized to
2676
2692
 
2677
2693
    if (__init_daemon_priv (PU_RESETGROUPS | PU_CLEARLIMITSET,
2678
2694
        SYSTEM_UID, SYSTEM_UID, PRIV_XVM_CONTROL, NULL)) {
2679
 
        fprintf (stderr, "additional privileges are required\n");
 
2695
        VIR_ERROR0(_("additional privileges are required\n"));
2680
2696
        return -1;
2681
2697
    }
2682
2698
 
2683
2699
    if (priv_set (PRIV_OFF, PRIV_ALLSETS, PRIV_FILE_LINK_ANY, PRIV_PROC_INFO,
2684
2700
        PRIV_PROC_SESSION, PRIV_PROC_EXEC, PRIV_PROC_FORK, NULL)) {
2685
 
        fprintf (stderr, "failed to set reduced privileges\n");
 
2701
        VIR_ERROR0(_("failed to set reduced privileges\n"));
2686
2702
        return -1;
2687
2703
    }
2688
2704
 
2849
2865
        goto error1;
2850
2866
 
2851
2867
    if (pipe(sigpipe) < 0 ||
2852
 
        qemudSetNonBlock(sigpipe[0]) < 0 ||
2853
 
        qemudSetNonBlock(sigpipe[1]) < 0 ||
2854
 
        qemudSetCloseExec(sigpipe[0]) < 0 ||
2855
 
        qemudSetCloseExec(sigpipe[1]) < 0) {
 
2868
        virSetNonBlock(sigpipe[0]) < 0 ||
 
2869
        virSetNonBlock(sigpipe[1]) < 0 ||
 
2870
        virSetCloseExec(sigpipe[0]) < 0 ||
 
2871
        virSetCloseExec(sigpipe[1]) < 0) {
2856
2872
        char ebuf[1024];
2857
2873
        VIR_ERROR(_("Failed to create pipe: %s"),
2858
2874
                  virStrerror(errno, ebuf, sizeof ebuf));