~serge-hallyn/ubuntu/natty/lxc/lxc-fix-3bugs

« back to all changes in this revision

Viewing changes to src/lxc/monitor.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter, Stéphane Graber, Guido Trotter
  • Date: 2010-01-10 10:40:21 UTC
  • mfrom: (1.1.2 upstream) (3.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100110104021-z8rj5zw5mlvra08l
Tags: 0.6.4-1
[ Stéphane Graber ]
* Upgrade standards-version to 3.8.3
* Drop the copy of etc/* from rules as "etc" is no longer in the tarball

[ Guido Trotter ]
* New Upstream Version
* Update libcap2-dev dependency to libcap-dev
* Install upstream-built man pages via debian/lxc.manpages
* Drop unneeded docbook-utils build dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <sys/types.h>
30
30
#include <sys/stat.h>
31
31
#include <sys/param.h>
32
 
#include <sys/inotify.h>
33
32
#include <sys/socket.h>
34
33
#include <sys/un.h>
35
34
#include <netinet/in.h>
37
36
 
38
37
#include "error.h"
39
38
#include "af_unix.h"
40
 
#include <lxc/lxc.h>
 
39
 
41
40
#include <lxc/log.h>
 
41
#include <lxc/state.h>
 
42
#include <lxc/monitor.h>
42
43
 
43
44
lxc_log_define(lxc_monitor, lxc);
44
45
 
46
47
#define UNIX_PATH_MAX 108
47
48
#endif
48
49
 
49
 
int lxc_monitor(const char *name, int output_fd)
50
 
{
51
 
        char path[MAXPATHLEN];
52
 
        int err = -1, nfd, wfd, state;
53
 
 
54
 
        nfd = inotify_init();
55
 
        if (nfd < 0) {
56
 
                SYSERROR("failed to initialize inotify");
57
 
                return -1;
58
 
        }
59
 
 
60
 
        snprintf(path, MAXPATHLEN, LXCPATH "/%s/state", name);
61
 
 
62
 
        wfd = inotify_add_watch(nfd, path, IN_DELETE_SELF|IN_CLOSE_WRITE);
63
 
        if (wfd < 0) {
64
 
                SYSERROR("failed to add a watch on %s", path);
65
 
                goto out;
66
 
        }
67
 
 
68
 
        for(;;) {
69
 
                struct inotify_event evt;
70
 
 
71
 
                if (read(nfd, &evt, sizeof(evt)) < 0) {
72
 
                        SYSERROR("failed to read inotify event");
73
 
                        goto out;
74
 
                }
75
 
 
76
 
                if (evt.mask & IN_CLOSE_WRITE) {
77
 
 
78
 
                        state = lxc_getstate(name);
79
 
                        if (state < 0) {
80
 
                                ERROR("failed to get the state for %s",
81
 
                                              name);
82
 
                                goto out;
83
 
                        }
84
 
 
85
 
                        if (write(output_fd, &state, sizeof(state)) < 0) {
86
 
                                SYSERROR("failed to send state to %d",
87
 
                                                 output_fd);
88
 
                                goto out;
89
 
                        }
90
 
                        continue;
91
 
                }
92
 
 
93
 
                if (evt.mask & IN_DELETE_SELF) {
94
 
                        close(output_fd);
95
 
                        err = 0;
96
 
                        goto out;
97
 
                }
98
 
 
99
 
                ERROR("unknown evt for inotity (%d)", evt.mask);
100
 
                goto out;
101
 
        }
102
 
 
103
 
out:
104
 
        inotify_rm_watch(nfd, wfd);
105
 
        close(nfd);
106
 
        return err;
107
 
}
108
 
 
109
50
static void lxc_monitor_send(struct lxc_msg *msg)
110
51
{
111
52
        int fd;
162
103
        socklen_t len = sizeof(from);
163
104
        int ret;
164
105
 
165
 
        ret = recvfrom(fd, msg, sizeof(*msg), 0, 
 
106
        ret = recvfrom(fd, msg, sizeof(*msg), 0,
166
107
                       (struct sockaddr *)&from, &len);
167
108
        if (ret < 0) {
168
109
                SYSERROR("failed to receive state");