]>
Commit | Line | Data |
---|---|---|
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 | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
5e97c3fc | 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, | |
8d06bd13 | 55 | .lxcpath_additional = -1, |
4237c6ca | 56 | }; |
5e97c3fc | 57 | |
58 | int main(int argc, char *argv[]) | |
59 | { | |
80f41298 | 60 | char *regexp; |
eae6543d | 61 | struct lxc_msg msg; |
80f41298 | 62 | regex_t preg; |
8d06bd13 DE |
63 | fd_set rfds, rfds_save; |
64 | int len, rc, i, nfds = -1; | |
5e97c3fc | 65 | |
75b1e198 MN |
66 | if (lxc_arguments_parse(&my_args, argc, argv)) |
67 | return -1; | |
5e97c3fc | 68 | |
5e1e7aaf | 69 | if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority, |
8d06bd13 | 70 | my_args.progname, my_args.quiet, my_args.lxcpath[0])) |
75b1e198 | 71 | return -1; |
51cab631 | 72 | |
9ba8130c SH |
73 | len = strlen(my_args.name) + 3; |
74 | regexp = malloc(len + 3); | |
302aaa2b MS |
75 | if (!regexp) { |
76 | ERROR("failed to allocate memory"); | |
77 | return -1; | |
78 | } | |
9ba8130c SH |
79 | rc = snprintf(regexp, len, "^%s$", my_args.name); |
80 | if (rc < 0 || rc >= len) { | |
81 | ERROR("Name too long"); | |
82 | free(regexp); | |
83 | return -1; | |
84 | } | |
80f41298 | 85 | |
86 | if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) { | |
4237c6ca | 87 | ERROR("failed to compile the regex '%s'", my_args.name); |
75b1e198 | 88 | return -1; |
80f41298 | 89 | } |
90 | ||
8d06bd13 DE |
91 | if (my_args.lxcpath_cnt > FD_SETSIZE) { |
92 | ERROR("too many paths requested, only the first %d will be monitored", FD_SETSIZE); | |
93 | my_args.lxcpath_cnt = FD_SETSIZE; | |
94 | } | |
e51d4895 | 95 | |
8d06bd13 DE |
96 | FD_ZERO(&rfds); |
97 | for (i = 0; i < my_args.lxcpath_cnt; i++) { | |
98 | int fd; | |
99 | ||
100 | lxc_monitord_spawn(my_args.lxcpath[i]); | |
101 | ||
102 | fd = lxc_monitor_open(my_args.lxcpath[i]); | |
103 | if (fd < 0) | |
104 | return -1; | |
105 | FD_SET(fd, &rfds); | |
106 | if (fd > nfds) | |
107 | nfds = fd; | |
108 | } | |
109 | memcpy(&rfds_save, &rfds, sizeof(rfds_save)); | |
110 | nfds++; | |
5e97c3fc | 111 | |
65b6a86d GK |
112 | setlinebuf(stdout); |
113 | ||
5e97c3fc | 114 | for (;;) { |
8d06bd13 DE |
115 | memcpy(&rfds, &rfds_save, sizeof(rfds)); |
116 | ||
117 | if (lxc_monitor_read_fdset(&rfds, nfds, &msg, -1) < 0) | |
eae6543d | 118 | return -1; |
5e97c3fc | 119 | |
b4e4ca49 | 120 | msg.name[sizeof(msg.name)-1] = '\0'; |
80f41298 | 121 | if (regexec(&preg, msg.name, 0, NULL, 0)) |
122 | continue; | |
123 | ||
eae6543d | 124 | switch (msg.type) { |
125 | case lxc_msg_state: | |
f79d43bb | 126 | printf("'%s' changed state to [%s]\n", |
80f41298 | 127 | msg.name, lxc_state2str(msg.value)); |
eae6543d | 128 | break; |
129 | default: | |
80f41298 | 130 | /* ignore garbage */ |
eae6543d | 131 | break; |
5e97c3fc | 132 | } |
5e97c3fc | 133 | } |
eae6543d | 134 | |
80f41298 | 135 | regfree(&preg); |
136 | ||
5e97c3fc | 137 | return 0; |
138 | } |