]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgls: add --unit to show units
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 1 Feb 2017 19:52:46 +0000 (14:52 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 2 Feb 2017 03:30:03 +0000 (22:30 -0500)
$ systemd-cgls -u systemd-journald.service machine.slice

I opted for a "global" switch, instead of modifying the behaviour of just one
argument. It seem to be a more useful setting, since usually one will want to
query one or more units, and not mix unit names with paths.

Closes #5156.

man/systemd-cgls.xml
src/cgls/cgls.c

index e8f0368f48dfbe42cb4e63825024fb2b85b1cbbd..91d0c0b1bb63ce4527725f93a45892bfe88e38a8 100644 (file)
       <arg choice="opt" rep="repeat">OPTIONS</arg>
       <arg choice="opt" rep="repeat">CGROUP</arg>
     </cmdsynopsis>
+    <cmdsynopsis>
+      <command>systemd-cgls</command>
+      <arg choice="opt" rep="repeat">OPTIONS</arg>
+      <arg choice="plain"><option>--unit</option></arg>
+      <arg choice="opt" rep="repeat">UNIT</arg>
+    </cmdsynopsis>
   </refsynopsisdiv>
 
   <refsect1>
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>-u</option></term>
+        <term><option>--unit</option></term>
+
+        <listitem><para>Show cgroup subtrees for the specified units.</para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><option>-k</option></term>
 
index b8af4680f979863c12cc141651f8fb944fbbdfc8..83e47170d8d981297cd74ef771bfaac7e46efd5c 100644 (file)
@@ -40,6 +40,7 @@
 static bool arg_no_pager = false;
 static bool arg_kernel_threads = false;
 static bool arg_all = false;
+static bool arg_unit = false;
 static int arg_full = -1;
 static char* arg_machine = NULL;
 
@@ -50,6 +51,7 @@ static void help(void) {
                "     --version        Show package version\n"
                "     --no-pager       Do not pipe output into a pager\n"
                "  -a --all            Show all groups, including empty\n"
+               "  -u --unit           Show the subtrees of specifified units\n"
                "  -l --full           Do not ellipsize output\n"
                "  -k                  Include kernel threads in output\n"
                "  -M --machine=       Show container\n"
@@ -70,6 +72,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "all",       no_argument,       NULL, 'a'          },
                 { "full",      no_argument,       NULL, 'l'          },
                 { "machine",   required_argument, NULL, 'M'          },
+                { "unit",      no_argument,       NULL, 'u'          },
                 {}
         };
 
@@ -78,7 +81,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "hkalM:", options, NULL)) >= 0)
+        while ((c = getopt_long(argc, argv, "hkalM:u", options, NULL)) >= 0)
 
                 switch (c) {
 
@@ -97,6 +100,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_all = true;
                         break;
 
+                case 'u':
+                        arg_unit = true;
+                        break;
+
                 case 'l':
                         arg_full = true;
                         break;
@@ -116,6 +123,11 @@ static int parse_argv(int argc, char *argv[]) {
                         assert_not_reached("Unhandled option");
                 }
 
+        if (arg_machine && arg_unit) {
+                log_error("Cannot combine --unit with --machine.");
+                return -EINVAL;
+        }
+
         return 1;
 }
 
@@ -150,17 +162,42 @@ int main(int argc, char *argv[]) {
                 arg_kernel_threads * OUTPUT_KERNEL_THREADS;
 
         if (optind < argc) {
+                _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
                 _cleanup_free_ char *root = NULL;
                 int i;
 
-                r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root);
-                if (r < 0)
-                        goto finish;
-
                 for (i = optind; i < argc; i++) {
                         int q;
 
-                        if (path_startswith(argv[i], "/sys/fs/cgroup")) {
+                        if (arg_unit) {
+                                /* Command line arguments are unit names */
+                                _cleanup_free_ char *cgroup = NULL;
+
+                                if (!bus) {
+                                        /* Connect to the bus only if necessary */
+                                        r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, false, &bus);
+                                        if (r < 0) {
+                                                log_error_errno(r, "Failed to create bus connection: %m");
+                                                goto finish;
+                                        }
+                                }
+
+                                q = show_cgroup_get_unit_path_and_warn(bus, argv[i], &cgroup);
+                                if (q < 0)
+                                        goto failed;
+
+                                if (isempty(cgroup)) {
+                                        log_warning("Unit %s not found.", argv[i]);
+                                        q = -ENOENT;
+                                        goto failed;
+                                }
+
+                                printf("Unit %s (%s):\n", argv[i], cgroup);
+                                fflush(stdout);
+
+                                q = show_cgroup_by_path(cgroup, NULL, 0, output_flags);
+
+                        } else if (path_startswith(argv[i], "/sys/fs/cgroup")) {
 
                                 printf("Directory %s:\n", argv[i]);
                                 fflush(stdout);
@@ -170,10 +207,17 @@ int main(int argc, char *argv[]) {
                                 _cleanup_free_ char *c = NULL, *p = NULL, *j = NULL;
                                 const char *controller, *path;
 
+                                if (!root) {
+                                        /* Query root only if needed, treat error as fatal */
+                                        r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root);
+                                        if (r < 0)
+                                                goto finish;
+                                }
+
                                 r = cg_split_spec(argv[i], &c, &p);
                                 if (r < 0) {
                                         log_error_errno(r, "Failed to split argument %s: %m", argv[i]);
-                                        goto finish;
+                                        goto failed;
                                 }
 
                                 controller = c ?: SYSTEMD_CGROUP_CONTROLLER;
@@ -194,7 +238,8 @@ int main(int argc, char *argv[]) {
                                 q = show_cgroup(controller, path, NULL, 0, output_flags);
                         }
 
-                        if (q < 0)
+                failed:
+                        if (q < 0 && r >= 0)
                                 r = q;
                 }