]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc-info: Rework based on mailinglist thread
authorStéphane Graber <stgraber@ubuntu.com>
Fri, 15 Nov 2013 20:14:00 +0000 (15:14 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Fri, 15 Nov 2013 21:06:49 +0000 (16:06 -0500)
So this implements the changes we discussed yesterday:
 - Only one container may be queried at the time
 - -n is now required once again
 - -H + a single filter only returns the value
 - -t/--is-state is now removed

Note that -S is considered as more than a single filter, so -H in that
case only affects the formatting of the values.

For the same reason, I haven't yet implemented the -H + multiple filters
case which we said should return a simple "key: value" output as it
wasn't trivial to re-arrange the stats code to print a different format
(for the other options, it's just a two lines change in the print
functions).

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
doc/ja/lxc-info.sgml.in
doc/lxc-info.sgml.in
src/lxc/lxc-netstat.in
src/lxc/lxc-ps.in
src/lxc/lxc_info.c

index 4cb81972a1ec85b6f143506fc597758d1987bb46..394c367d8874c4c1a5d82cd740f0321e3bfcb664 100644 (file)
@@ -58,7 +58,6 @@ by KATOH Yasufumi <karma at jazz.email.ne.jp>
       <arg choice="opt">-s</arg>
       <arg choice="opt">-p</arg>
       <arg choice="opt">-i</arg>
-      <arg choice="opt">-t <replaceable>state</replaceable></arg>
       <arg choice="opt">-S</arg>
       <arg choice="opt">-H</arg>
     </cmdsynopsis>
@@ -212,20 +211,6 @@ by KATOH Yasufumi <karma at jazz.email.ne.jp>
           </para>
         </listitem>
       </varlistentry>
-
-      <varlistentry>
-        <term>
-          <option><optional>-t <replaceable>state</replaceable></optional></option>
-        </term>
-        <listitem>
-          <para>
-            <!--
-            Check whether the container is in the provided state.
-            -->
-            コンテナが指定した状態かどうかをチェックする.
-          </para>
-        </listitem>
-      </varlistentry>
     </variablelist>
   </refsect1>
 
@@ -258,18 +243,6 @@ by KATOH Yasufumi <karma at jazz.email.ne.jp>
         </listitem>
       </varlistentry>
 
-      <varlistentry>
-        <term>lxc-info -n foo -t RUNNING</term>
-        <listitem>
-          <para>
-            <!--
-            exits 0 if foo is RUNNING, 1 otherwise.
-            -->
-            もしコンテナ foo が RUNNING であれば 0 を返します.それ以外は 1 を返します.
-          </para>
-        </listitem>
-      </varlistentry>
-
       <varlistentry>
         <term>lxc-info -n foo -c lxc.network.0.veth.pair</term>
         <listitem>
index 791d7809cd0e000fc912bda5f2a887398034db6d..5f99994bb353a68adeb59647652955db28f69a26 100644 (file)
@@ -52,7 +52,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
       <arg choice="opt">-s</arg>
       <arg choice="opt">-p</arg>
       <arg choice="opt">-i</arg>
-      <arg choice="opt">-t <replaceable>state</replaceable></arg>
       <arg choice="opt">-S</arg>
       <arg choice="opt">-H</arg>
     </cmdsynopsis>
@@ -169,17 +168,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
           </para>
         </listitem>
       </varlistentry>
-
-      <varlistentry>
-        <term>
-          <option><optional>-t <replaceable>state</replaceable></optional></option>
-        </term>
-        <listitem>
-          <para>
-            Check whether the container is in the provided state.
-          </para>
-        </listitem>
-      </varlistentry>
     </variablelist>
   </refsect1>
 
@@ -206,15 +194,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
         </listitem>
       </varlistentry>
 
-      <varlistentry>
-        <term>lxc-info -n foo -t RUNNING</term>
-        <listitem>
-          <para>
-            exits 0 if foo is RUNNING, 1 otherwise.
-          </para>
-        </listitem>
-      </varlistentry>
-
       <varlistentry>
         <term>lxc-info -n foo -c lxc.network.0.veth.pair</term>
         <listitem>
index 2add5300361097e3635f4daeb62ff46c9cb6442c..0aecb0c0f0c16b1fe598c92a5a95a4f5de059893 100644 (file)
@@ -117,7 +117,7 @@ if [ -z "$exec" ]; then
     exec @BINDIR@/lxc-unshare -s MOUNT -- $0 -n $name -P "$lxc_path" --exec -- "$@"
 fi
 
-if lxc-info -n $name -P "$lxc_path" --state-is 'STOPPED'; then
+if lxc-wait -n $name -P "$lxc_path" -s 'STOPPED' -t 0; then
     echo "$(basename $0): container '$name' is not running" >&2
     exit 1
 fi
index 75d5610bfa75af9a811e3906e9ba7f8767c1319a..ae57e2140f74fdeaf93ffadf7c2d25f9873f8126 100644 (file)
@@ -116,7 +116,7 @@ for container in ${containers}; do
         container_field_width=${#container}
     fi
 
-    if ! lxc-info -P $lxc_path -t STOPPED -n $container; then
+    if ! lxc-wait -P $lxc_path -s STOPPED -n $container -t 0; then
         initpid=`lxc-info -P $lxc_path -p -n $container | awk -F: '{ print $2 }' | awk '{ print $1 }'`
         cgroup=`head -n 1 /proc/$initpid/cgroup | awk -F: '{ print $3}'`
         if [ -f "$parent_cgroup/$cgroup/tasks" ]; then
index 6c35bbbb5e596a04a0a3b38bf98a2fe70b6ad73d..11555aa3e21834de1a1f312d707f7d03e327b515 100644 (file)
@@ -25,7 +25,6 @@
 #include <stdbool.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <regex.h>
 #include <limits.h>
 #include <libgen.h>
 #include <sys/types.h>
@@ -43,9 +42,9 @@ static bool state;
 static bool pid;
 static bool stats;
 static bool humanize = true;
-static char *test_state = NULL;
 static char **key = NULL;
 static int keys = 0;
+static int filter_count = 0;
 
 static int my_parser(struct lxc_arguments* args, int c, char* arg)
 {
@@ -55,12 +54,11 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
                key[keys] = arg;
                keys++;
                break;
-       case 'i': ips = true; break;
-       case 's': state = true; break;
-       case 'p': pid = true; break;
-       case 'S': stats = true; break;
+       case 'i': ips = true; filter_count += 1; break;
+       case 's': state = true; filter_count += 1; break;
+       case 'p': pid = true; filter_count += 1; break;
+       case 'S': stats = true; filter_count += 5; break;
        case 'H': humanize = false; break;
-       case 't': test_state = arg; break;
        }
        return 0;
 }
@@ -72,7 +70,6 @@ static const struct option my_longopts[] = {
        {"pid", no_argument, 0, 'p'},
        {"stats", no_argument, 0, 'S'},
        {"no-humanize", no_argument, 0, 'H'},
-       {"state-is", required_argument, 0, 't'},
        LXC_COMMON_OPTIONS,
 };
 
@@ -90,10 +87,8 @@ Options :\n\
   -p, --pid             shows the process id of the init container\n\
   -S, --stats           shows usage stats\n\
   -H, --no-humanize     shows stats as raw numbers, not humanized\n\
-  -s, --state           shows the state of the container\n\
-  -t, --state-is=STATE  test if current state is STATE\n\
-                        returns success if it matches, false otherwise\n",
-       .name     = ".*",
+  -s, --state           shows the state of the container\n",
+       .name     = NULL,
        .options  = my_longopts,
        .parser   = my_parser,
        .checker  = NULL,
@@ -249,14 +244,40 @@ static void print_stats(struct lxc_container *c)
        }
 }
 
+void print_info_msg_int(const char *key, int value)
+{
+       if (humanize)
+               printf("%-15s %d\n", key, value);
+       else {
+               if (filter_count == 1)
+                       printf("%d\n", value);
+               else
+                       printf("%-15s %d\n", key, value);
+       }
+}
+
+void print_info_msg_str(const char *key, const char *value)
+{
+       if (humanize)
+               printf("%-15s %s\n", key, value);
+       else {
+               if (filter_count == 1)
+                       printf("%s\n", value);
+               else
+                       printf("%-15s %s\n", key, value);
+       }
+}
+
 static int print_info(const char *name, const char *lxcpath)
 {
        int i;
        struct lxc_container *c;
 
        c = lxc_container_new(name, lxcpath);
-       if (!c)
+       if (!c) {
+               fprintf(stderr, "Failure to retrieve information on %s\n", c->name);
                return -1;
+       }
 
        if (!c->may_control(c)) {
                fprintf(stderr, "Insufficent privileges to control %s\n", c->name);
@@ -264,16 +285,19 @@ static int print_info(const char *name, const char *lxcpath)
                return -1;
        }
 
-       if (!state && !pid && !ips && !stats && keys <= 0)
-               state = pid = ips = stats = true;
-
-       printf("%-15s %s\n", "Name:", c->name);
+       if (!c->is_running(c) && !c->is_defined(c)) {
+               fprintf(stderr, "%s doesn't exist\n", c->name);
+               lxc_container_put(c);
+               return -1;
+       }
 
-       if (state || test_state) {
-               if (test_state)
-                       return strcmp(c->state(c), test_state) != 0;
+       if (!state && !pid && !ips && !stats && keys <= 0) {
+               state = pid = ips = stats = true;
+               print_info_msg_str("Name:", c->name);
+       }
 
-               printf("%-15s %s\n", "State:", c->state(c));
+       if (state) {
+               print_info_msg_str("State:", c->state(c));
        }
 
        if (pid) {
@@ -281,7 +305,7 @@ static int print_info(const char *name, const char *lxcpath)
 
                initpid = c->init_pid(c);
                if (initpid >= 0)
-                       printf("%-15s %d\n", "Pid:", initpid);
+                       print_info_msg_int("PID:", initpid);
        }
 
        if (ips) {
@@ -291,7 +315,7 @@ static int print_info(const char *name, const char *lxcpath)
                        i = 0;
                        while (addresses[i]) {
                                address = addresses[i];
-                               printf("%-15s %s\n", "IP:", address);
+                               print_info_msg_str("IP:", address);
                                i++;
                        }
                }
@@ -325,63 +349,20 @@ static int print_info(const char *name, const char *lxcpath)
 
 int main(int argc, char *argv[])
 {
-       int rc, i, len, ret = EXIT_FAILURE;
-       char *regexp;
-       regex_t preg;
-       int ct_cnt;
-       char **ct_name;
-       bool printed;
+       int ret = EXIT_FAILURE;
 
        if (lxc_arguments_parse(&my_args, argc, argv))
-               goto err1;
+               return ret;
 
        if (!my_args.log_file)
                my_args.log_file = "none";
 
        if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
                         my_args.progname, my_args.quiet, my_args.lxcpath[0]))
-               goto err1;
+               return ret;
 
-       len = strlen(my_args.name) + 3;
-       regexp = malloc(len + 3);
-       if (!regexp) {
-               fprintf(stderr, "failed to allocate memory");
-               goto err1;
-       }
-       rc = snprintf(regexp, len, "^%s$", my_args.name);
-       if (rc < 0 || rc >= len) {
-               fprintf(stderr, "Name too long");
-               goto err2;
-       }
-
-       if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) {
-               fprintf(stderr, "failed to compile the regex '%s'", my_args.name);
-               goto err2;
-       }
+       if (print_info(my_args.name, my_args.lxcpath[0]) == 0)
+               ret = EXIT_SUCCESS;
 
-       printed = false;
-       ct_cnt = list_all_containers(my_args.lxcpath[0], &ct_name, NULL);
-       if (ct_cnt < 0)
-               goto err3;
-
-       for (i = 0; i < ct_cnt; i++) {
-               if (regexec(&preg, ct_name[i], 0, NULL, 0) == 0)
-               {
-                       if (printed)
-                               printf("\n");
-                       print_info(ct_name[i], my_args.lxcpath[0]);
-                       printed = true;
-               }
-               free(ct_name[i]);
-       }
-       if (ct_name)
-               free(ct_name);
-       ret = EXIT_SUCCESS;
-
-err3:
-       regfree(&preg);
-err2:
-       free(regexp);
-err1:
        return ret;
 }