]>
Commit | Line | Data |
---|---|---|
5e97c3fc | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
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 | 36 | lxc_log_define(lxc_monitor_ui, lxc_monitor); |
3ab87b66 | 37 | |
4237c6ca MN |
38 | static const struct option my_longopts[] = { |
39 | LXC_COMMON_OPTIONS | |
40 | }; | |
41 | ||
42 | static struct lxc_arguments my_args = { | |
43 | .progname = "lxc-monitor", | |
44 | .help = "\ | |
45 | --name=NAME\n\ | |
46 | \n\ | |
47 | lxc-monitor monitors the state of the NAME container\n\ | |
48 | \n\ | |
49 | Options :\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 | |
57 | int 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 | |
4237c6ca MN |
68 | if (lxc_log_init(my_args.log_file, my_args.log_priority, |
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 | ||
90 | fd = lxc_monitor_open(); | |
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 | |
80f41298 | 100 | if (regexec(&preg, msg.name, 0, NULL, 0)) |
101 | continue; | |
102 | ||
eae6543d | 103 | switch (msg.type) { |
104 | case lxc_msg_state: | |
105 | printf("'%s' changed state to [%s]\n", | |
80f41298 | 106 | msg.name, lxc_state2str(msg.value)); |
eae6543d | 107 | break; |
108 | default: | |
80f41298 | 109 | /* ignore garbage */ |
eae6543d | 110 | break; |
5e97c3fc | 111 | } |
5e97c3fc | 112 | } |
eae6543d | 113 | |
80f41298 | 114 | regfree(&preg); |
115 | ||
5e97c3fc | 116 | return 0; |
117 | } | |
118 |