]>
git.proxmox.com Git - mirror_lxc.git/blob - src/liblxc/monitor.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/inotify.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
40 #define UNIX_PATH_MAX 108
43 int lxc_monitor(const char *name
, int output_fd
)
45 char path
[MAXPATHLEN
];
46 int err
= -1, nfd
, wfd
, state
;
50 lxc_log_syserror("failed to initialize inotify");
54 snprintf(path
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
56 wfd
= inotify_add_watch(nfd
, path
, IN_DELETE_SELF
|IN_CLOSE_WRITE
);
58 lxc_log_syserror("failed to add a watch on %s", path
);
63 struct inotify_event evt
;
65 if (read(nfd
, &evt
, sizeof(evt
)) < 0) {
66 lxc_log_syserror("failed to read inotify event");
70 if (evt
.mask
& IN_CLOSE_WRITE
) {
72 state
= lxc_getstate(name
);
74 lxc_log_error("failed to get the state for %s",
79 if (write(output_fd
, &state
, sizeof(state
)) < 0) {
80 lxc_log_syserror("failed to send state to %d",
87 if (evt
.mask
& IN_DELETE_SELF
) {
93 lxc_log_error("unknown evt for inotity (%d)", evt
.mask
);
98 inotify_rm_watch(nfd
, wfd
);
103 void lxc_monitor_send_state(const char *name
, lxc_state_t state
)
106 struct sockaddr_un addr
;
108 fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
110 lxc_log_syserror("failed to create notification socket");
112 memset(&addr
, 0, sizeof(addr
));
113 addr
.sun_family
= AF_UNIX
;
114 snprintf(addr
.sun_path
, UNIX_PATH_MAX
, LXCPATH
"/%s/notification", name
);
116 sendto(fd
, &state
, sizeof(state
), 0,
117 (const struct sockaddr
*)&addr
, sizeof(addr
));
122 void lxc_monitor_cleanup(const char *name
)
124 char path
[UNIX_PATH_MAX
];
125 snprintf(path
, UNIX_PATH_MAX
, LXCPATH
"/%s/notification", name
);
129 int lxc_monitor_open(const char *name
)
132 struct sockaddr_un addr
;
134 fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
136 lxc_log_syserror("failed to create notification socket");
140 memset(&addr
, 0, sizeof(addr
));
142 addr
.sun_family
= AF_UNIX
;
143 snprintf(addr
.sun_path
, UNIX_PATH_MAX
, LXCPATH
"/%s/notification", name
);
144 unlink(addr
.sun_path
);
146 if (bind(fd
, (const struct sockaddr
*)&addr
, sizeof(addr
))) {
147 lxc_log_syserror("failed to bind to '%s'", addr
.sun_path
);
154 int lxc_monitor_read(int fd
, lxc_state_t
*state
)
158 ret
= recv(fd
, state
, sizeof(*state
), 0);
160 lxc_log_syserror("failed to received state");
167 int lxc_monitor_close(int fd
)