File: blkio-info.c

package info (click to toggle)
libblkio 1.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,016 kB
  • sloc: ansic: 3,443; sh: 432; makefile: 35; python: 16
file content (287 lines) | stat: -rw-r--r-- 7,853 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: MIT
/*
 * blkio-info - show information about a block device
 *
 * This program prints information reported by libblkio about a block device.
 * It does this by assigning and querying the given blkio properties:
 *
 *   blkio-info [--output=text|json] <driver> <property>[=<value>] ...
 *
 * A blkio instance is created with the given driver and each property listed
 * on the command-line is either assigned or queried from left to right.
 *
 * Property assignments are initially made with the blkio instance in the
 * created state. Once the first property query is reached the blkio instance
 * is put into the connected state and all following property accesses occur in
 * the connected state.
 *
 * Output is formatted in "text" mode by default where just the property values
 * are printed in command-line argument order. In "json" mode a JSON object is
 * printed with a member for each queried property on the command-line. The
 * appropriate JSON data type is used for each property.
 *
 * Show the size of the block device, in bytes:
 *
 *   $ blkio-info io_uring path=/dev/nvme0n1 capacity
 *   107374182400
 *
 * Query the "max-queues" and "request-alignment" properties with JSON output:
 *
 *   $ blkio-info --output=json io_uring path=/dev/sdb max-queues request-alignment
 *   {
 *       "max-queues": 2147483647,
 *       "request-alignment": 512
 *   }
 */
#include <blkio.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Needed for type-aware JSON output */
struct property_query_visitor
{
    void (*visit_bool)(const char *property, bool value);
    void (*visit_int)(const char *property, int value);
    void (*visit_uint64)(const char *property, uint64_t value);
    void (*visit_str)(const char *property, const char *value);
};

static void text_output_bool(const char *property, bool value)
{
    printf("%s\n", value ? "true" : "false");
}

static void text_output_int(const char *property, int value)
{
    printf("%d\n", value);
}

static void text_output_uint64(const char *property, uint64_t value)
{
    printf("%" PRId64 "\n", value);
}

static void text_output_str(const char *property, const char *value)
{
    printf("%s\n", value);
}

static const struct property_query_visitor text_output_visitor = {
    .visit_bool = text_output_bool,
    .visit_int = text_output_int,
    .visit_uint64 = text_output_uint64,
    .visit_str = text_output_str,
};

static void json_output_bool(const char *property, bool value)
{
    printf("    \"%s\": %s\n", property, value ? "true" : "false");
}

static void json_output_int(const char *property, int value)
{
    printf("    \"%s\": %d\n", property, value);
}

static void json_output_uint64(const char *property, uint64_t value)
{
    printf("    \"%s\": %" PRId64 "\n", property, value);
}

static void json_output_str(const char *property, const char *value)
{
    printf("    \"%s\": \"%s\"\n", property, value);
}

static const struct property_query_visitor json_output_visitor = {
    .visit_bool = json_output_bool,
    .visit_int = json_output_int,
    .visit_uint64 = json_output_uint64,
    .visit_str = json_output_str,
};

static void assign_property(struct blkio *b,
                            const char *property,
                            const char *value)
{
    int ret;

    ret = blkio_set_str(b, property, value);
    if (ret < 0) {
        fprintf(stderr, "Failed to assign \"%s\" to \"%s\": %s: %s\n",
                value, property, strerror(-ret), blkio_get_error_msg());
        exit(EXIT_FAILURE);
    }
}

static void query_property(struct blkio *b, const char *property,
                           const struct property_query_visitor *visitor)
{
    bool bool_value;
    int int_value;
    uint64_t uint64_value;
    char *str_value;
    int ret;

    /* There is no property type information, so brute force it... */

    ret = blkio_get_bool(b, property, &bool_value);
    if (ret == 0) {
        visitor->visit_bool(property, bool_value);
        return;
    }
    if (ret != -ENOTTY) {
        goto err;
    }

    ret = blkio_get_int(b, property, &int_value);
    if (ret == 0) {
        visitor->visit_int(property, int_value);
        return;
    }
    if (ret != -ENOTTY) {
        goto err;
    }

    ret = blkio_get_uint64(b, property, &uint64_value);
    if (ret == 0) {
        visitor->visit_uint64(property, uint64_value);
        return;
    }
    if (ret != -ENOTTY) {
        goto err;
    }

    ret = blkio_get_str(b, property, &str_value);
    if (ret == 0) {
        visitor->visit_str(property, str_value);
        free(str_value);
        return;
    }

err:
    fprintf(stderr, "Failed to query \"%s\" property: %s: %s\n",
            property, strerror(-ret), blkio_get_error_msg());
    exit(EXIT_FAILURE);
}

static void usage(const char *progname)
{
    printf("Usage: %s [--output=text|json] <driver> <property>[=<value>] ...\n", progname);
    printf("Show information about a libblkio block device. This is done by\n");
    printf("assigning and querying the given blkio properties.\n\n");
    printf("  --output=text       print text output\n");
    printf("  --output=json       print JSON output\n");
    exit(EXIT_FAILURE);
}

enum {
    OPT_OUTPUT = 256,
};

static const struct option options[] = {
    {
        .name = "help",
        .has_arg = no_argument,
        .flag = NULL,
        .val = 'h',
    },
    {
        .name = "output",
        .has_arg = required_argument,
        .flag = NULL,
        .val = OPT_OUTPUT,
    },
    {}
};

int main(int argc, char **argv)
{
    const struct property_query_visitor *visitor = &text_output_visitor;
    const char *driver;
    struct blkio *b;
    bool connected = false;
    int opt;
    int ret;

    while ((opt = getopt_long(argc, argv, "h", options, NULL)) != -1) {
        switch (opt) {
        case 'h':
            usage(argv[0]);
            break;
        case OPT_OUTPUT:
            if (strcmp(optarg, "text") == 0) {
                visitor = &text_output_visitor;
            } else if (strcmp(optarg, "json") == 0) {
                visitor = &json_output_visitor;
            } else {
                fprintf(stderr, "Invalid output format \"%s\"\n\n", optarg);
                usage(argv[0]);
            }
            break;
        default:
            usage(argv[0]);
            break;
        }
    }

    if (optind == argc) {
        fprintf(stderr, "Missing driver name.\n\n");
        usage(argv[0]);
    }
    driver = argv[optind++];

    if (optind == argc) {
        fprintf(stderr, "No properties specified!\n\n");
        usage(argv[0]);
    }

    ret = blkio_create(driver, &b);
    if (ret < 0) {
        fprintf(stderr, "blkio_create: %s: %s\n", strerror(-ret),
                blkio_get_error_msg());
        return EXIT_FAILURE;
    }

    if (visitor == &json_output_visitor) {
        printf("{\n");
    }

    for (; optind < argc; optind++) {
        char *property = argv[optind];
        char *value = strchr(property, '=');

        if (value) {
            /* The C standard allows argv[] string modification */
            *value = '\0';
            value++;

            assign_property(b, property, value);
        } else {
            if (!connected) {
                if (blkio_connect(b) != 0) {
                    fprintf(stderr, "Unable to connect: %s\n",
                            blkio_get_error_msg());
                    blkio_destroy(&b);
                    return EXIT_FAILURE;
                }
                connected = true;
            }

            query_property(b, property, visitor);
        }
    }

    if (visitor == &json_output_visitor) {
        printf("}\n");
    }

    blkio_destroy(&b);
    return EXIT_SUCCESS;
}