]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxc_monitor.c
ubuntu: Don't break when the locale is C.*
[mirror_lxc.git] / src / lxc / lxc_monitor.c
CommitLineData
5e97c3fc 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
5e97c3fc 8 *
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.
13 *
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.
18 *
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
22 */
23#include <stdio.h>
8e9d7710 24#include <stdlib.h>
80f41298 25#include <string.h>
5e97c3fc 26#include <libgen.h>
27#include <unistd.h>
80f41298 28#include <regex.h>
5e97c3fc 29#include <sys/types.h>
30
b113348e 31#include <lxc/lxc.h>
00b3c2e2
CLG
32#include <lxc/log.h>
33#include <lxc/monitor.h>
4237c6ca 34#include "arguments.h"
5e97c3fc 35
77075659 36lxc_log_define(lxc_monitor_ui, lxc_monitor);
3ab87b66 37
4237c6ca
MN
38static const struct option my_longopts[] = {
39 LXC_COMMON_OPTIONS
40};
41
42static struct lxc_arguments my_args = {
43 .progname = "lxc-monitor",
44 .help = "\
45--name=NAME\n\
46\n\
47lxc-monitor monitors the state of the NAME container\n\
48\n\
49Options :\n\
50 -n, --name=NAME NAME for name of the container\n\
51 NAME may be a regular expression",
52 .options = my_longopts,
53 .parser = NULL,
54 .checker = NULL,
55};
5e97c3fc 56
57int main(int argc, char *argv[])
58{
80f41298 59 char *regexp;
eae6543d 60 struct lxc_msg msg;
80f41298 61 regex_t preg;
75b1e198 62 int fd;
9ba8130c 63 int len, rc;
5e97c3fc 64
75b1e198
MN
65 if (lxc_arguments_parse(&my_args, argc, argv))
66 return -1;
5e97c3fc 67
5e1e7aaf 68 if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
4237c6ca 69 my_args.progname, my_args.quiet))
75b1e198 70 return -1;
51cab631 71
9ba8130c
SH
72 len = strlen(my_args.name) + 3;
73 regexp = malloc(len + 3);
302aaa2b
MS
74 if (!regexp) {
75 ERROR("failed to allocate memory");
76 return -1;
77 }
9ba8130c
SH
78 rc = snprintf(regexp, len, "^%s$", my_args.name);
79 if (rc < 0 || rc >= len) {
80 ERROR("Name too long");
81 free(regexp);
82 return -1;
83 }
80f41298 84
85 if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) {
4237c6ca 86 ERROR("failed to compile the regex '%s'", my_args.name);
75b1e198 87 return -1;
80f41298 88 }
89
9123e471 90 fd = lxc_monitor_open(my_args.lxcpath);
3ab87b66 91 if (fd < 0)
eae6543d 92 return -1;
5e97c3fc 93
65b6a86d
GK
94 setlinebuf(stdout);
95
5e97c3fc 96 for (;;) {
3ab87b66 97 if (lxc_monitor_read(fd, &msg) < 0)
eae6543d 98 return -1;
5e97c3fc 99
b4e4ca49 100 msg.name[sizeof(msg.name)-1] = '\0';
80f41298 101 if (regexec(&preg, msg.name, 0, NULL, 0))
102 continue;
103
eae6543d 104 switch (msg.type) {
105 case lxc_msg_state:
f79d43bb 106 printf("'%s' changed state to [%s]\n",
80f41298 107 msg.name, lxc_state2str(msg.value));
eae6543d 108 break;
109 default:
80f41298 110 /* ignore garbage */
eae6543d 111 break;
5e97c3fc 112 }
5e97c3fc 113 }
eae6543d 114
80f41298 115 regfree(&preg);
116
5e97c3fc 117 return 0;
118}
119