]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
table: add TABLE_IFINDEX type
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 24 May 2019 09:11:13 +0000 (18:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 29 May 2019 05:21:19 +0000 (14:21 +0900)
src/shared/format-table.c
src/shared/format-table.h

index 09357694ddb26e8bf845cb5fa59254b303cebc51..4f562be81a1819bd504237f6d20be8c6dde5ee06 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <ctype.h>
+#include <net/if.h>
 
 #include "alloc-util.h"
 #include "fd-util.h"
@@ -80,6 +81,7 @@ typedef struct TableData {
                 uint32_t uint32;
                 uint64_t uint64;
                 int percent;        /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
+                int ifindex;
                 /* … add more here as we start supporting more cell data types … */
         };
 } TableData;
@@ -241,6 +243,7 @@ static size_t table_data_size(TableDataType type, const void *data) {
         case TABLE_INT:
         case TABLE_UINT:
         case TABLE_PERCENT:
+        case TABLE_IFINDEX:
                 return sizeof(int);
 
         default:
@@ -693,6 +696,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         uint32_t uint32;
                         uint64_t uint64;
                         int percent;
+                        int ifindex;
                         bool b;
                 } buffer;
 
@@ -757,6 +761,11 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         data = &buffer.percent;
                         break;
 
+                case TABLE_IFINDEX:
+                        buffer.ifindex = va_arg(ap, int);
+                        data = &buffer.ifindex;
+                        break;
+
                 case _TABLE_DATA_TYPE_MAX:
                         /* Used as end marker */
                         va_end(ap);
@@ -898,6 +907,9 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                 case TABLE_PERCENT:
                         return CMP(a->percent, b->percent);
 
+                case TABLE_IFINDEX:
+                        return CMP(a->ifindex, b->ifindex);
+
                 default:
                         ;
                 }
@@ -1094,6 +1106,23 @@ static const char *table_data_format(TableData *d) {
                 break;
         }
 
+        case TABLE_IFINDEX: {
+                _cleanup_free_ char *p;
+                char name[IF_NAMESIZE + 1] = {};
+
+                if (if_indextoname(d->ifindex, name)) {
+                        p = strdup(name);
+                        if (!p)
+                                return NULL;
+                } else {
+                        if (asprintf(&p, "%i" , d->ifindex) < 0)
+                                return NULL;
+                }
+
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
         default:
                 assert_not_reached("Unexpected type?");
         }
@@ -1618,6 +1647,9 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
         case TABLE_PERCENT:
                 return json_variant_new_integer(ret, d->percent);
 
+        case TABLE_IFINDEX:
+                return json_variant_new_integer(ret, d->ifindex);
+
         default:
                 return -EINVAL;
         }
index c5cf5708d4b15f1bc5d7c9c7c4896c6a4193463e..ec2bbba29272e15def977bedb04d99ad72a20f6b 100644 (file)
@@ -22,6 +22,7 @@ typedef enum TableDataType {
         TABLE_UINT32,
         TABLE_UINT64,
         TABLE_PERCENT,
+        TABLE_IFINDEX,
         _TABLE_DATA_TYPE_MAX,
         _TABLE_DATA_TYPE_INVALID = -1,
 } TableDataType;