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

« back to all changes in this revision

Viewing changes to src/lxc_controller.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2009-02-11 01:01:42 UTC
  • mto: (3.4.1 sid) (1.2.1 upstream) (0.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090211010142-wk9mgtbw8bmp3zcb
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
#include "util.h"
46
46
#include "cgroup.h"
47
47
 
 
48
#define VIR_FROM_THIS VIR_FROM_LXC
 
49
 
48
50
struct cgroup_device_policy {
49
51
    char type;
50
52
    int major;
109
111
    rc = virCgroupAddTask(cgroup, getpid());
110
112
out:
111
113
    if (rc != 0) {
112
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
113
 
                 _("Failed to set lxc resources: %s\n"), strerror(-rc));
 
114
        virReportSystemError(NULL, -rc, "%s",
 
115
                             _("Failed to set lxc resources"));
114
116
        virCgroupRemove(cgroup);
115
117
    }
116
118
 
122
124
static char*lxcMonitorPath(virDomainDefPtr def)
123
125
{
124
126
    char *sockpath;
125
 
    if (asprintf(&sockpath, "%s/%s.sock",
126
 
                 LXC_STATE_DIR, def->name) < 0) {
127
 
        lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
128
 
        return NULL;
129
 
    }
 
127
 
 
128
    if (virAsprintf(&sockpath, "%s/%s.sock",
 
129
                    LXC_STATE_DIR, def->name) < 0)
 
130
        virReportOOMError(NULL);
130
131
    return sockpath;
131
132
}
132
133
 
136
137
    struct sockaddr_un addr;
137
138
 
138
139
    if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
139
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
140
 
                 _("failed to create server socket %s: %s"),
141
 
                 sockpath, strerror(errno));
 
140
        virReportSystemError(NULL, errno,
 
141
                             _("failed to create server socket '%s'"),
 
142
                             sockpath);
142
143
        goto error;
143
144
    }
144
145
 
148
149
    strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
149
150
 
150
151
    if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
151
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
152
 
                 _("failed to bind server socket %s: %s"),
153
 
                 sockpath, strerror(errno));
 
152
        virReportSystemError(NULL, errno,
 
153
                             _("failed to bind server socket '%s'"),
 
154
                             sockpath);
154
155
        goto error;
155
156
    }
156
157
    if (listen(fd, 30 /* backlog */ ) < 0) {
157
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
158
 
                 _("failed to listen server socket %s: %s"),
159
 
                 sockpath, strerror(errno));
 
158
        virReportSystemError(NULL, errno,
 
159
                             _("failed to listen server socket %s"),
 
160
                             sockpath);
160
161
        goto error;
161
162
    }
162
163
 
188
189
            goto cleanup;
189
190
        }
190
191
 
191
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
192
 
                 _("read of fd %d failed: %s"), readFd, strerror(errno));
 
192
        virReportSystemError(NULL, errno,
 
193
                             _("read of fd %d failed"),
 
194
                             readFd);
193
195
        goto cleanup;
194
196
    }
195
197
 
196
198
    if (1 != (safewrite(writeFd, buf, 1))) {
197
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
198
 
                 _("write to fd %d failed: %s"), writeFd, strerror(errno));
 
199
        virReportSystemError(NULL, errno,
 
200
                             _("write to fd %d failed"),
 
201
                             writeFd);
199
202
        goto cleanup;
200
203
    }
201
204
 
245
248
    /* create the epoll fild descriptor */
246
249
    epollFd = epoll_create(2);
247
250
    if (0 > epollFd) {
248
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
249
 
                 _("epoll_create(2) failed: %s"), strerror(errno));
 
251
        virReportSystemError(NULL, errno, "%s",
 
252
                             _("epoll_create(2) failed"));
250
253
        goto cleanup;
251
254
    }
252
255
 
255
258
    epollEvent.events = EPOLLIN|EPOLLET;    /* edge triggered */
256
259
    epollEvent.data.fd = appPty;
257
260
    if (0 > epoll_ctl(epollFd, EPOLL_CTL_ADD, appPty, &epollEvent)) {
258
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
259
 
                 _("epoll_ctl(appPty) failed: %s"), strerror(errno));
 
261
        virReportSystemError(NULL, errno, "%s",
 
262
                             _("epoll_ctl(appPty) failed"));
260
263
        goto cleanup;
261
264
    }
262
265
    epollEvent.data.fd = contPty;
263
266
    if (0 > epoll_ctl(epollFd, EPOLL_CTL_ADD, contPty, &epollEvent)) {
264
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
265
 
                 _("epoll_ctl(contPty) failed: %s"), strerror(errno));
 
267
        virReportSystemError(NULL, errno, "%s",
 
268
                             _("epoll_ctl(contPty) failed"));
266
269
        goto cleanup;
267
270
    }
268
271
 
269
272
    epollEvent.events = EPOLLIN;
270
273
    epollEvent.data.fd = monitor;
271
274
    if (0 > epoll_ctl(epollFd, EPOLL_CTL_ADD, monitor, &epollEvent)) {
272
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
273
 
                 _("epoll_ctl(contPty) failed: %s"), strerror(errno));
 
275
        virReportSystemError(NULL, errno, "%s",
 
276
                             _("epoll_ctl(contPty) failed"));
274
277
        goto cleanup;
275
278
    }
276
279
 
277
280
    epollEvent.events = EPOLLHUP;
278
281
    epollEvent.data.fd = client;
279
282
    if (0 > epoll_ctl(epollFd, EPOLL_CTL_ADD, client, &epollEvent)) {
280
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
281
 
                 _("epoll_ctl(contPty) failed: %s"), strerror(errno));
 
283
        virReportSystemError(NULL, errno, "%s",
 
284
                             _("epoll_ctl(contPty) failed"));
282
285
        goto cleanup;
283
286
    }
284
287
 
297
300
                epollEvent.events = EPOLLHUP;
298
301
                epollEvent.data.fd = client;
299
302
                if (0 > epoll_ctl(epollFd, EPOLL_CTL_ADD, client, &epollEvent)) {
300
 
                    lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
301
 
                             _("epoll_ctl(contPty) failed: %s"), strerror(errno));
 
303
                    virReportSystemError(NULL, errno, "%s",
 
304
                                         _("epoll_ctl(contPty) failed"));
302
305
                    goto cleanup;
303
306
                }
304
307
            } else if (client != -1 && epollEvent.data.fd == client) {
305
308
                if (0 > epoll_ctl(epollFd, EPOLL_CTL_DEL, client, &epollEvent)) {
306
 
                    lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
307
 
                             _("epoll_ctl(contPty) failed: %s"), strerror(errno));
 
309
                    virReportSystemError(NULL, errno, "%s",
 
310
                                         _("epoll_ctl(contPty) failed"));
308
311
                    goto cleanup;
309
312
                }
310
313
                close(client);
341
344
            }
342
345
 
343
346
            /* error */
344
 
            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
345
 
                     _("epoll_wait() failed: %s"), strerror(errno));
 
347
            virReportSystemError(NULL, errno, "%s",
 
348
                                 _("epoll_wait() failed"));
346
349
            goto cleanup;
347
350
 
348
351
        }
439
442
    pid_t container = -1;
440
443
 
441
444
    if (socketpair(PF_UNIX, SOCK_STREAM, 0, control) < 0) {
442
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
443
 
                 _("sockpair failed: %s"), strerror(errno));
 
445
        virReportSystemError(NULL, errno, "%s",
 
446
                             _("sockpair failed"));
444
447
        goto cleanup;
445
448
    }
446
449
 
447
450
    if (virFileOpenTty(&containerPty,
448
451
                       &containerPtyPath,
449
452
                       0) < 0) {
450
 
        lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
451
 
                 _("failed to allocate tty: %s"), strerror(errno));
 
453
        virReportSystemError(NULL, errno, "%s",
 
454
                             _("failed to allocate tty"));
452
455
        goto cleanup;
453
456
    }
454
457
 
529
532
 
530
533
        case 'n':
531
534
            if ((name = strdup(optarg)) == NULL) {
532
 
                fprintf(stderr, "%s", strerror(errno));
 
535
                virReportOOMError(NULL);
533
536
                goto cleanup;
534
537
            }
535
538
            break;
536
539
 
537
540
        case 'v':
538
541
            if (VIR_REALLOC_N(veths, nveths+1) < 0) {
539
 
                fprintf(stderr, "cannot allocate veths %s", strerror(errno));
 
542
                virReportOOMError(NULL);
540
543
                goto cleanup;
541
544
            }
542
545
            if ((veths[nveths++] = strdup(optarg)) == NULL) {
543
 
                fprintf(stderr, "cannot allocate veth name %s", strerror(errno));
 
546
                virReportOOMError(NULL);
544
547
                goto cleanup;
545
548
            }
546
549
            break;
615
618
 
616
619
        if (pid > 0) {
617
620
            if ((rc = virFileWritePid(LXC_STATE_DIR, name, pid)) != 0) {
618
 
                fprintf(stderr, _("Unable to write pid file: %s\n"),
619
 
                        strerror(rc));
 
621
                virReportSystemError(NULL, rc,
 
622
                                     _("Unable to write pid file '%s/%s.pid'"),
 
623
                                     LXC_STATE_DIR, name);
620
624
                _exit(1);
621
625
            }
622
626
 
628
632
 
629
633
        /* Don't hold onto any cwd we inherit from libvirtd either */
630
634
        if (chdir("/") < 0) {
631
 
            fprintf(stderr, _("Unable to change to root dir: %s\n"),
632
 
                    strerror(errno));
 
635
            virReportSystemError(NULL, errno, "%s",
 
636
                                 _("Unable to change to root dir"));
633
637
            goto cleanup;
634
638
        }
635
639
 
636
640
        if (setsid() < 0) {
637
 
            fprintf(stderr, _("Unable to become session leader: %s\n"),
638
 
                    strerror(errno));
 
641
            virReportSystemError(NULL, errno, "%s",
 
642
                                 _("Unable to become session leader"));
639
643
            goto cleanup;
640
644
        }
641
645
    }
642
646
 
643
647
    /* Accept initial client which is the libvirtd daemon */
644
648
    if ((client = accept(monitor, NULL, 0)) < 0) {
645
 
        fprintf(stderr, _("Failed connection from LXC driver: %s\n"),
646
 
                strerror(errno));
 
649
        virReportSystemError(NULL, errno, "%s",
 
650
                             _("Failed connection from LXC driver"));
647
651
        goto cleanup;
648
652
    }
649
653