~serge-hallyn/ubuntu/natty/lxc/lxc-fix-3bugs

« back to all changes in this revision

Viewing changes to src/lxc/lxc_monitor.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2009-07-25 12:24:30 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090725122430-dxv1wb7ds07fc0sk
Tags: 0.6.3-1
* New Upstream Version
* Remove duplicate build-dependency on autotools-dev
* Build depend on linux-libc-dev
* Disable checking of netlink headers from configure
  (currently fails under sid)
* Upgrade standards-version to 3.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
22
 */
23
23
#include <stdio.h>
 
24
#include <stdlib.h>
24
25
#include <string.h>
25
26
#include <libgen.h>
26
27
#include <unistd.h>
28
29
#include <sys/types.h>
29
30
 
30
31
#include <lxc/lxc.h>
31
 
 
32
 
void usage(char *cmd)
33
 
{
34
 
        fprintf(stderr, "%s <command>\n", basename(cmd));
35
 
        fprintf(stderr, "\t -n <name>   : name of the container or regular expression\n");
36
 
        _exit(1);
37
 
}
 
32
#include "arguments.h"
 
33
 
 
34
lxc_log_define(monitor, lxc);
 
35
 
 
36
static const struct option my_longopts[] = {
 
37
        LXC_COMMON_OPTIONS
 
38
};
 
39
 
 
40
static struct lxc_arguments my_args = {
 
41
        .progname = "lxc-monitor",
 
42
        .help     = "\
 
43
--name=NAME\n\
 
44
\n\
 
45
lxc-monitor monitors the state of the NAME container\n\
 
46
\n\
 
47
Options :\n\
 
48
  -n, --name=NAME   NAME for name of the container\n\
 
49
                    NAME may be a regular expression",
 
50
        .options  = my_longopts,
 
51
        .parser   = NULL,
 
52
        .checker  = NULL,
 
53
};
38
54
 
39
55
int main(int argc, char *argv[])
40
56
{
41
 
        char *name = NULL;
42
57
        char *regexp;
43
58
        struct lxc_msg msg;
44
59
        regex_t preg;
45
 
        int fd, opt;
46
 
 
47
 
        while ((opt = getopt(argc, argv, "n:")) != -1) {
48
 
                switch (opt) {
49
 
                case 'n':
50
 
                        name = optarg;
51
 
                        break;
52
 
                }
53
 
        }
54
 
 
55
 
        if (!name)
56
 
                usage(argv[0]);
57
 
 
58
 
        regexp = malloc(strlen(name) + 3);
59
 
        sprintf(regexp, "^%s$", name);
 
60
        int fd;
 
61
 
 
62
        if (lxc_arguments_parse(&my_args, argc, argv))
 
63
                return -1;
 
64
 
 
65
        if (lxc_log_init(my_args.log_file, my_args.log_priority,
 
66
                         my_args.progname, my_args.quiet))
 
67
                return -1;
 
68
 
 
69
        regexp = malloc(strlen(my_args.name) + 3);
 
70
        sprintf(regexp, "^%s$", my_args.name);
60
71
 
61
72
        if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) {
62
 
                fprintf(stderr, "failed to compile the regex '%s'\n",
63
 
                        name);
64
 
                return 1;
 
73
                ERROR("failed to compile the regex '%s'", my_args.name);
 
74
                return -1;
65
75
        }
66
76
 
67
77
        fd = lxc_monitor_open();
68
 
        if (fd < 0) {
69
 
                fprintf(stderr, "failed to open monitor for '%s'\n", name);
 
78
        if (fd < 0)
70
79
                return -1;
71
 
        }
72
80
 
73
81
        for (;;) {
74
 
                if (lxc_monitor_read(fd, &msg) < 0) {
75
 
                        fprintf(stderr, 
76
 
                                "failed to read monitor's message for '%s'\n", 
77
 
                                name);
 
82
                if (lxc_monitor_read(fd, &msg) < 0)
78
83
                        return -1;
79
 
                }
80
84
 
81
85
                if (regexec(&preg, msg.name, 0, NULL, 0))
82
86
                        continue;