]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/monitor.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Dwight Engen <dwight.engen@oracle.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <netinet/in.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
40 #include <sys/types.h>
52 lxc_log_define(lxc_monitor
, lxc
);
54 /* routines used by monitor publishers (containers) */
55 int lxc_monitor_fifo_name(const char *lxcpath
, char *fifo_path
, size_t fifo_path_sz
,
61 rundir
= get_rundir();
66 ret
= snprintf(fifo_path
, fifo_path_sz
, "%s/lxc/%s", rundir
, lxcpath
);
67 if (ret
< 0 || (size_t)ret
>= fifo_path_sz
) {
68 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo.", rundir
, lxcpath
);
72 ret
= mkdir_p(fifo_path
, 0755);
74 ERROR("Unable to create monitor fifo directory %s.", fifo_path
);
79 ret
= snprintf(fifo_path
, fifo_path_sz
, "%s/lxc/%s/monitor-fifo", rundir
, lxcpath
);
80 if (ret
< 0 || (size_t)ret
>= fifo_path_sz
) {
81 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo.", rundir
, lxcpath
);
89 static void lxc_monitor_fifo_send(struct lxc_msg
*msg
, const char *lxcpath
)
92 char fifo_path
[PATH_MAX
];
94 BUILD_BUG_ON(sizeof(*msg
) > PIPE_BUF
); /* write not guaranteed atomic */
96 ret
= lxc_monitor_fifo_name(lxcpath
, fifo_path
, sizeof(fifo_path
), 0);
100 /* Open the fifo nonblock in case the monitor is dead, we don't want the
101 * open to wait for a reader since it may never come.
103 fd
= open(fifo_path
, O_WRONLY
| O_NONBLOCK
);
105 /* It is normal for this open() to fail with ENXIO when there is
106 * no monitor running, so we don't log it.
111 WARN("Failed to open fifo to send message: %s.", strerror(errno
));
115 if (fcntl(fd
, F_SETFL
, O_WRONLY
) < 0) {
120 ret
= write(fd
, msg
, sizeof(*msg
));
121 if (ret
!= sizeof(*msg
)) {
123 SYSERROR("Failed to write to monitor fifo \"%s\".", fifo_path
);
130 void lxc_monitor_send_state(const char *name
, lxc_state_t state
,
133 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
134 strncpy(msg
.name
, name
, sizeof(msg
.name
));
135 msg
.name
[sizeof(msg
.name
) - 1] = 0;
137 lxc_monitor_fifo_send(&msg
, lxcpath
);
140 void lxc_monitor_send_exit_code(const char *name
, int exit_code
,
143 struct lxc_msg msg
= {.type
= lxc_msg_exit_code
, .value
= exit_code
};
144 strncpy(msg
.name
, name
, sizeof(msg
.name
));
145 msg
.name
[sizeof(msg
.name
) - 1] = 0;
147 lxc_monitor_fifo_send(&msg
, lxcpath
);
150 /* routines used by monitor subscribers (lxc-monitor) */
151 int lxc_monitor_close(int fd
)
156 int lxc_monitor_sock_name(const char *lxcpath
, struct sockaddr_un
*addr
) {
163 /* addr.sun_path is only 108 bytes, so we hash the full name and
164 * then append as much of the name as we can fit.
166 sockname
= &addr
->sun_path
[1];
167 memset(addr
, 0, sizeof(*addr
));
168 addr
->sun_family
= AF_UNIX
;
170 len
= strlen(lxcpath
) + 18;
172 ret
= snprintf(path
, len
, "lxc/%s/monitor-sock", lxcpath
);
173 if (ret
< 0 || (size_t)ret
>= len
) {
174 ERROR("Failed to create path for monitor.");
178 len
= sizeof(addr
->sun_path
) - 1;
179 hash
= fnv_64a_buf(path
, ret
, FNV1A_64_INIT
);
180 ret
= snprintf(sockname
, len
, "lxc/%016" PRIx64
"/%s", hash
, lxcpath
);
184 sockname
[sizeof(addr
->sun_path
)-3] = '\0';
185 INFO("Using monitor socket name \"%s\".", sockname
);
190 int lxc_monitor_open(const char *lxcpath
)
192 struct sockaddr_un addr
;
196 int ret
= 0, backoff_ms
[] = {10, 50, 100};
198 if (lxc_monitor_sock_name(lxcpath
, &addr
) < 0)
201 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
203 ERROR("Failed to create socket: %s.", strerror(errno
));
207 len
= strlen(&addr
.sun_path
[1]) + 1;
208 if (len
>= sizeof(addr
.sun_path
) - 1) {
210 errno
= ENAMETOOLONG
;
214 for (retry
= 0; retry
< sizeof(backoff_ms
) / sizeof(backoff_ms
[0]); retry
++) {
215 ret
= connect(fd
, (struct sockaddr
*)&addr
, offsetof(struct sockaddr_un
, sun_path
) + len
);
216 if (ret
== 0 || errno
!= ECONNREFUSED
)
218 ERROR("Failed to connect to monitor socket. Retrying in %d ms.", backoff_ms
[retry
]);
219 usleep(backoff_ms
[retry
] * 1000);
223 ERROR("Failed to connect to monitor socket: %s.", strerror(errno
));
234 int lxc_monitor_read_fdset(struct pollfd
*fds
, nfds_t nfds
, struct lxc_msg
*msg
,
240 ret
= poll(fds
, nfds
, timeout
* 1000);
244 return -2; // timed out
246 /* Only read from the first ready fd, the others will remain ready for
247 * when this routine is called again.
249 for (i
= 0; i
< nfds
; i
++) {
250 if (fds
[i
].revents
!= 0) {
252 ret
= recv(fds
[i
].fd
, msg
, sizeof(*msg
), 0);
254 SYSERROR("Failed to receive message. Did monitord die?: %s.", strerror(errno
));
261 SYSERROR("No ready fd found.");
266 int lxc_monitor_read_timeout(int fd
, struct lxc_msg
*msg
, int timeout
)
271 fds
.events
= POLLIN
| POLLPRI
;
274 return lxc_monitor_read_fdset(&fds
, 1, msg
, timeout
);
277 int lxc_monitor_read(int fd
, struct lxc_msg
*msg
)
279 return lxc_monitor_read_timeout(fd
, msg
, -1);
282 #define LXC_MONITORD_PATH LIBEXECDIR "/lxc/lxc-monitord"
284 /* Used to spawn a monitord either on startup of a daemon container, or when
285 * lxc-monitor starts.
288 int lxc_monitord_spawn(const char *lxcpath
)
292 char pipefd_str
[__INT_LEN
];
295 char *const args
[] = {
302 /* double fork to avoid zombies when monitord exits */
305 SYSERROR("Failed to fork().");
310 DEBUG("Going to wait for pid %d.", pid1
);
311 if (waitpid(pid1
, NULL
, 0) != pid1
)
313 DEBUG("Finished waiting on pid %d.", pid1
);
317 if (pipe(pipefd
) < 0) {
318 SYSERROR("Failed to create pipe.");
324 SYSERROR("Failed to fork().");
329 DEBUG("Trying to sync with child process.");
331 /* Wait for daemon to create socket. */
334 /* Sync with child, we're ignoring the return from read
335 * because regardless if it works or not, either way we've
336 * synced with the child process. the if-empty-statement
337 * construct is to quiet the warn-unused-result warning.
339 if (read(pipefd
[0], &c
, 1))
344 DEBUG("Sucessfully synced with child process.");
349 SYSERROR("Failed to setsid().");
353 lxc_check_inherited(NULL
, true, pipefd
[1]);
354 if (null_stdfds() < 0) {
355 SYSERROR("Failed to dup2() standard file descriptors to /dev/null.");
361 ret
= snprintf(pipefd_str
, __INT_LEN
, "%d", pipefd
[1]);
362 if (ret
< 0 || ret
>= __INT_LEN
) {
363 ERROR("Failed to create pid argument to pass to monitord.");
367 DEBUG("Using pipe file descriptor %d for monitord.", pipefd
[1]);
369 execvp(args
[0], args
);
370 ERROR("Failed to exec lxc-monitord.");