chiark / gitweb /
cgls: add --machine/-M
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 30 Apr 2013 04:35:38 +0000 (00:35 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 1 May 2013 14:15:25 +0000 (10:15 -0400)
cg_get_machine_path is modified to include the escaped machine name
+ ".nspawn" if the machine argument is nonnull.

man/systemd-cgls.xml
src/analyze/systemd-analyze.c
src/cgls/cgls.c
src/login/sd-login.c
src/nspawn/nspawn.c
src/shared/cgroup-util.c
src/shared/cgroup-util.h
src/test/test-cgroup-util.c

index b280b87..6249d05 100644 (file)
@@ -49,7 +49,9 @@
 
         <refsynopsisdiv>
                 <cmdsynopsis>
-                        <command>systemd-cgls <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="opt" rep="repeat">CGROUP</arg></command>
+                        <command>systemd-cgls</command>
+                        <arg choice="opt" rep="repeat">OPTIONS</arg>
+                        <arg choice="opt" rep="repeat">CGROUP</arg>
                 </cmdsynopsis>
         </refsynopsisdiv>
 
                                 threads in output.</para></listitem>
                         </varlistentry>
 
+                        <varlistentry>
+                                <term><option>-m <replaceable>MACHINE</replaceable></option></term>
+                                <term><option>--machine=<replaceable>MACHINE</replaceable></option></term>
+
+                                <listitem><para>Limit cgroups shown to
+                                the part corresponding to the
+                                container <replaceable>MACHINE</replaceable>.
+                                </para></listitem>
+                        </varlistentry>
+
                 </variablelist>
 
         </refsect1>
                         <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>systemd-cgtop</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+                        <citerefentry><refentrytitle>systemd-nspawn</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>1</manvolnum></citerefentry>
                 </para>
         </refsect1>
index 7f0b69c..bb86ec7 100644 (file)
@@ -780,7 +780,6 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, unsigned
 }
 
 static int list_dependencies(DBusConnection *bus) {
-        _cleanup_free_ char *unit = NULL;
         _cleanup_strv_free_ char **units = NULL;
         char ts[FORMAT_TIMESPAN_MAX];
         struct unit_times *times;
index 8ddd733..ef3e567 100644 (file)
@@ -39,6 +39,7 @@ static bool arg_no_pager = false;
 static bool arg_kernel_threads = false;
 static bool arg_all = false;
 static int arg_full = -1;
+static char* arg_machine = NULL;
 
 static void help(void) {
 
@@ -49,7 +50,8 @@ static void help(void) {
                "     --no-pager       Do not pipe output into a pager\n"
                "  -a --all            Show all groups, including empty\n"
                "  --full              Do not ellipsize output\n"
-               "  -k                  Include kernel threads in output\n",
+               "  -k                  Include kernel threads in output\n"
+               "  -M --machine        Show container\n",
                program_invocation_short_name);
 }
 
@@ -67,6 +69,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "no-pager",  no_argument,       NULL, ARG_NO_PAGER },
                 { "all",       no_argument,       NULL, 'a'          },
                 { "full",      no_argument,       NULL, ARG_FULL     },
+                { "machine",   required_argument, NULL, 'M'          },
                 { NULL,        0,                 NULL, 0            }
         };
 
@@ -75,7 +78,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "hka", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "hkaM:", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -104,6 +107,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_kernel_threads = true;
                         break;
 
+                case 'M':
+                        arg_machine = optarg;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -119,6 +126,7 @@ static int parse_argv(int argc, char *argv[]) {
 int main(int argc, char *argv[]) {
         int r = 0, retval = EXIT_FAILURE;
         int output_flags;
+        char _cleanup_free_ *root = NULL;
 
         log_parse_environment();
         log_open();
@@ -144,13 +152,20 @@ int main(int argc, char *argv[]) {
                 (arg_full > 0) * OUTPUT_FULL_WIDTH;
 
         if (optind < argc) {
-                unsigned i;
+                int i;
 
-                for (i = (unsigned) optind; i < (unsigned) argc; i++) {
+                for (i = optind; i < argc; i++) {
                         int q;
                         printf("%s:\n", argv[i]);
 
-                        q = show_cgroup_by_path(argv[i], NULL, 0,
+                        if (arg_machine)
+                                root = strjoin("machine/", arg_machine, "/", argv[i], NULL);
+                        else
+                                root = strdup(argv[i]);
+                        if (!root)
+                                return log_oom();
+
+                        q = show_cgroup_by_path(root, NULL, 0,
                                                 arg_kernel_threads, output_flags);
                         if (q < 0)
                                 r = q;
@@ -165,16 +180,18 @@ int main(int argc, char *argv[]) {
                         goto finish;
                 }
 
-                if (path_startswith(p, "/sys/fs/cgroup")) {
+                if (path_startswith(p, "/sys/fs/cgroup") && !arg_machine) {
                         printf("Working Directory %s:\n", p);
                         r = show_cgroup_by_path(p, NULL, 0,
                                                 arg_kernel_threads, output_flags);
                 } else {
-                        _cleanup_free_ char *root = NULL;
-
-                        r = cg_get_root_path(&root);
+                        if (arg_machine)
+                                r = cg_get_machine_path(arg_machine, &root);
+                        else
+                                r = cg_get_root_path(&root);
                         if (r < 0) {
-                                log_error("Failed to get root path: %s", strerror(-r));
+                                log_error("Failed to get %s path: %s",
+                                          arg_machine ? "machine" : "root", strerror(-r));
                                 goto finish;
                         }
 
@@ -183,10 +200,11 @@ int main(int argc, char *argv[]) {
                 }
         }
 
-        if (r < 0)
-                log_error("Failed to list cgroup tree: %s", strerror(-r));
-
-        retval = EXIT_SUCCESS;
+        if (r < 0) {
+                log_error("Failed to list cgroup tree %s: %s", root, strerror(-r));
+                retval = EXIT_FAILURE;
+        } else
+                retval = EXIT_SUCCESS;
 
 finish:
         pager_close();
index bc8cd8a..66c4487 100644 (file)
@@ -598,7 +598,7 @@ int sd_get_machine_names(char ***machines) {
         char *n;
         int c = 0, r;
 
-        r = cg_get_machine_path(&md);
+        r = cg_get_machine_path(NULL, &md);
         if (r < 0)
                 return r;
 
@@ -681,7 +681,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
                 _cleanup_free_ char *md = NULL, *p = NULL;
                 int r;
 
-                r = cg_get_machine_path(&md);
+                r = cg_get_machine_path(NULL, &md);
                 if (r < 0)
                         return r;
 
index d772b47..bbb3334 100644 (file)
@@ -1217,7 +1217,7 @@ finish:
 int main(int argc, char *argv[]) {
         pid_t pid = 0;
         int r = EXIT_FAILURE, k;
-        _cleanup_free_ char *machine_root = NULL, *name = NULL, *escaped = NULL, *newcg = NULL;
+        _cleanup_free_ char *newcg = NULL;
         _cleanup_close_ int master = -1;
         int n_fd_passed;
         const char *console = NULL;
@@ -1301,30 +1301,12 @@ int main(int argc, char *argv[]) {
         fdset_close_others(fds);
         log_open();
 
-        k = cg_get_machine_path(&machine_root);
+        k = cg_get_machine_path(arg_machine, &newcg);
         if (k < 0) {
                 log_error("Failed to determine machine cgroup path: %s", strerror(-k));
                 goto finish;
         }
 
-        name = strappend(arg_machine, ".nspawn");
-        if (!name) {
-                log_oom();
-                goto finish;
-        }
-
-        escaped = cg_escape(name);
-        if (!escaped) {
-                log_oom();
-                goto finish;
-        }
-
-        newcg = strjoin(machine_root, "/", escaped, NULL);
-        if (!newcg) {
-                log_oom();
-                goto finish;
-        }
-
         k = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, newcg, true);
         if (k <= 0 && k != -ENOENT) {
                 log_error("Container already running.");
index 1366f58..8c2ef45 100644 (file)
@@ -1146,17 +1146,22 @@ int cg_get_user_path(char **path) {
         return 0;
 }
 
-int cg_get_machine_path(char **path) {
-        _cleanup_free_ char *root = NULL;
+int cg_get_machine_path(const char *machine, char **path) {
+        _cleanup_free_ char *root = NULL, *escaped = NULL;
         char *p;
 
         assert(path);
 
-        if (cg_get_root_path(&root) < 0 || streq(root, "/"))
-                p = strdup("/machine");
-        else
-                p = strappend(root, "/machine");
+        if (machine) {
+                const char *name = strappenda(machine, ".nspawn");
+
+                escaped = cg_escape(name);
+                if (!escaped)
+                        return -ENOMEM;
+        }
 
+        p = strjoin(cg_get_root_path(&root) >= 0 && !streq(root, "/") ? root : "",
+                    "/machine", machine ? "/" : "", machine ? escaped : "", NULL);
         if (!p)
                 return -ENOMEM;
 
index 7bd02c1..92caa0c 100644 (file)
@@ -87,7 +87,7 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_
 int cg_get_root_path(char **path);
 int cg_get_system_path(char **path);
 int cg_get_user_path(char **path);
-int cg_get_machine_path(char **path);
+int cg_get_machine_path(const char *machine, char **path);
 
 int cg_path_get_session(const char *path, char **session);
 int cg_path_get_owner_uid(const char *path, uid_t *uid);
index 6726f8f..cc1a6fd 100644 (file)
@@ -85,7 +85,7 @@ static void test_get_paths(void) {
         assert_se(cg_get_user_path(&c) >= 0);
         log_info("User = %s", c);
 
-        assert_se(cg_get_machine_path(&d) >= 0);
+        assert_se(cg_get_machine_path("harley", &d) >= 0);
         log_info("Machine = %s", d);
 }