chiark / gitweb /
logind: minor cleanup and use IN_SET() in manager_handle_action()
[elogind.git] / src / login / sysfs-show.c
index 172c75d..cb5bb5e 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
 
 #include <errno.h>
 #include <string.h>
+
+#if 0 /// elogind needs the systems udev header
+#include "libudev.h"
+#else
 #include <libudev.h>
+#endif // 0
 
-#include "util.h"
-#include "sysfs-show.h"
+#include "alloc-util.h"
+#include "locale-util.h"
 #include "path-util.h"
+#include "string-util.h"
+#include "sysfs-show.h"
+#include "terminal-util.h"
+#include "udev-util.h"
+#include "util.h"
 
 static int show_sysfs_one(
                 struct udev *udev,
@@ -41,10 +49,11 @@ static int show_sysfs_one(
         assert(prefix);
 
         while (*item) {
+                _cleanup_udev_device_unref_ struct udev_device *d = NULL;
                 struct udev_list_entry *next, *lookahead;
-                struct udev_device *d;
                 const char *sn, *name, *sysfs, *subsystem, *sysname;
-                char *l, *k;
+                _cleanup_free_ char *k = NULL, *l = NULL;
+                bool is_master;
 
                 sysfs = udev_list_entry_get_name(*item);
                 if (!path_startswith(sysfs, sub))
@@ -60,13 +69,14 @@ static int show_sysfs_one(
                 if (isempty(sn))
                         sn = "seat0";
 
-                /* fixme, also check for tag 'seat' here */
+                /* Explicitly also check for tag 'seat' here */
                 if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
-                        udev_device_unref(d);
                         *item = udev_list_entry_get_next(*item);
                         continue;
                 }
 
+                is_master = udev_device_has_tag(d, "master-of-seat");
+
                 name = udev_device_get_sysattr_value(d, "name");
                 if (!name)
                         name = udev_device_get_sysattr_value(d, "id");
@@ -82,21 +92,17 @@ static int show_sysfs_one(
 
                         if (path_startswith(lookahead_sysfs, sub) &&
                             !path_startswith(lookahead_sysfs, sysfs)) {
-                                struct udev_device *lookahead_d;
+                                _cleanup_udev_device_unref_ struct udev_device *lookahead_d = NULL;
 
                                 lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
                                 if (lookahead_d) {
                                         const char *lookahead_sn;
-                                        bool found;
 
                                         lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
                                         if (isempty(lookahead_sn))
                                                 lookahead_sn = "seat0";
 
-                                        found = streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat");
-                                        udev_device_unref(lookahead_d);
-
-                                        if (found)
+                                        if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
                                                 break;
                                 }
                         }
@@ -105,43 +111,44 @@ static int show_sysfs_one(
                 }
 
                 k = ellipsize(sysfs, n_columns, 20);
-                printf("%s%s %s\n", prefix, draw_special_char(lookahead ? DRAW_BOX_VERT_AND_RIGHT : DRAW_BOX_UP_AND_RIGHT),
-                                    k ? k : sysfs);
-                free(k);
+                if (!k)
+                        return -ENOMEM;
+
+                printf("%s%s%s\n", prefix, special_glyph(lookahead ? TREE_BRANCH : TREE_RIGHT), k);
 
                 if (asprintf(&l,
-                             "(%s:%s)%s%s%s",
+                             "%s%s:%s%s%s%s",
+                             is_master ? "[MASTER] " : "",
                              subsystem, sysname,
-                             name ? " \"" : "", name ? name : "", name ? "\"" : "") < 0) {
-                        udev_device_unref(d);
+                             name ? " \"" : "", strempty(name), name ? "\"" : "") < 0)
                         return -ENOMEM;
-                }
 
-                k = ellipsize(l, n_columns, 70);
-                printf("%s%s %s\n", prefix, lookahead ? draw_special_char(DRAW_BOX_VERT) : " ",
-                                    k ? k : l);
                 free(k);
-                free(l);
+                k = ellipsize(l, n_columns, 70);
+                if (!k)
+                        return -ENOMEM;
+
+                printf("%s%s%s\n", prefix, lookahead ? special_glyph(TREE_VERTICAL) : "  ", k);
 
                 *item = next;
                 if (*item) {
-                        char *p;
+                        _cleanup_free_ char *p = NULL;
 
-                        p = strjoin(prefix, lookahead ? draw_special_char(DRAW_BOX_VERT) : " ", " ", NULL);
-                        show_sysfs_one(udev, seat, item, sysfs, p ? p : prefix, n_columns - 2);
-                        free(p);
-                }
+                        p = strappend(prefix, lookahead ? special_glyph(TREE_VERTICAL) : "  ");
+                        if (!p)
+                                return -ENOMEM;
 
-                udev_device_unref(d);
+                        show_sysfs_one(udev, seat, item, sysfs, p, n_columns - 2);
+                }
         }
 
         return 0;
 }
 
 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
-        struct udev *udev;
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
         struct udev_list_entry *first = NULL;
-        struct udev_enumerate *e;
         int r;
 
         if (n_columns <= 0)
@@ -158,33 +165,29 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
                 return -ENOMEM;
 
         e = udev_enumerate_new(udev);
-        if (!e) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        if (!e)
+                return -ENOMEM;
 
         if (!streq(seat, "seat0"))
                 r = udev_enumerate_add_match_tag(e, seat);
         else
                 r = udev_enumerate_add_match_tag(e, "seat");
+        if (r < 0)
+                return r;
 
+        r = udev_enumerate_add_match_is_initialized(e);
         if (r < 0)
-                goto finish;
+                return r;
 
         r = udev_enumerate_scan_devices(e);
         if (r < 0)
-                goto finish;
+                return r;
 
         first = udev_enumerate_get_list_entry(e);
         if (first)
                 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
-
-finish:
-        if (e)
-                udev_enumerate_unref(e);
-
-        if (udev)
-                udev_unref(udev);
+        else
+                printf("%s%s%s\n", prefix, special_glyph(TREE_RIGHT), "(none)");
 
         return r;
 }