]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/lsirq.c
treewide: fix newlines when using fputs
[thirdparty/util-linux.git] / sys-utils / lsirq.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * lsirq - utility to display kernel interrupt information.
5 *
6 * Copyright (C) 2019 zhenwei pi <pizhenwei@bytedance.com>
7 * Copyright (C) 2020 Karel Zak <kzak@redhat.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 */
14 #include <ctype.h>
15 #include <errno.h>
16 #include <getopt.h>
17 #include <limits.h>
18 #include <locale.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include <libsmartcols.h>
26
27 #include "closestream.h"
28 #include "optutils.h"
29 #include "strutils.h"
30 #include "xalloc.h"
31
32 #include "irq-common.h"
33
34 static int print_irq_data(struct irq_output *out, int softirq)
35 {
36 struct libscols_table *table;
37
38 table = get_scols_table(out, NULL, NULL, softirq, 0, NULL);
39 if (!table)
40 return -1;
41
42 scols_print_table(table);
43 scols_unref_table(table);
44 return 0;
45 }
46
47 static void __attribute__((__noreturn__)) usage(void)
48 {
49 fputs(USAGE_HEADER, stdout);
50 printf(_(" %s [options]\n"), program_invocation_short_name);
51 fputs(USAGE_SEPARATOR, stdout);
52
53 fputsln(_("Utility to display kernel interrupt information."), stdout);
54
55 fputs(USAGE_OPTIONS, stdout);
56 fputs(_(" -J, --json use JSON output format\n"), stdout);
57 fputs(_(" -P, --pairs use key=\"value\" output format\n"), stdout);
58 fputs(_(" -n, --noheadings don't print headings\n"), stdout);
59 fputs(_(" -o, --output <list> define which output columns to use\n"), stdout);
60 fputs(_(" -s, --sort <column> specify sort column\n"), stdout);
61 fputs(_(" -S, --softirq show softirqs instead of interrupts\n"), stdout);
62 fputs(USAGE_SEPARATOR, stdout);
63 fprintf(stdout, USAGE_HELP_OPTIONS(22));
64
65 fputs(USAGE_COLUMNS, stdout);
66 irq_print_columns(stdout, 1);
67
68 fprintf(stdout, USAGE_MAN_TAIL("lsirq(1)"));
69 exit(EXIT_SUCCESS);
70 }
71
72 int main(int argc, char **argv)
73 {
74 struct irq_output out = {
75 .ncolumns = 0
76 };
77 static const struct option longopts[] = {
78 {"sort", required_argument, NULL, 's'},
79 {"noheadings", no_argument, NULL, 'n'},
80 {"output", required_argument, NULL, 'o'},
81 {"softirq", no_argument, NULL, 'S'},
82 {"json", no_argument, NULL, 'J'},
83 {"pairs", no_argument, NULL, 'P'},
84 {"help", no_argument, NULL, 'h'},
85 {"version", no_argument, NULL, 'V'},
86 {NULL, 0, NULL, 0}
87 };
88 int c;
89 const char *outarg = NULL;
90 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
91 {'J', 'P'},
92 {0}
93 };
94 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
95 int softirq = 0;
96
97 setlocale(LC_ALL, "");
98
99 while ((c = getopt_long(argc, argv, "no:s:ShJPV", longopts, NULL)) != -1) {
100 err_exclusive_options(c, longopts, excl, excl_st);
101
102 switch (c) {
103 case 'J':
104 out.json = 1;
105 break;
106 case 'P':
107 out.pairs = 1;
108 break;
109 case 'n':
110 out.no_headings = 1;
111 break;
112 case 'o':
113 outarg = optarg;
114 break;
115 case 's':
116 set_sort_func_by_name(&out, optarg);
117 break;
118 case 'S':
119 softirq = 1;
120 break;
121 case 'V':
122 print_version(EXIT_SUCCESS);
123 case 'h':
124 usage();
125 default:
126 errtryhelp(EXIT_FAILURE);
127 }
128 }
129
130 /* default */
131 if (!out.ncolumns) {
132 out.columns[out.ncolumns++] = COL_IRQ;
133 out.columns[out.ncolumns++] = COL_TOTAL;
134 out.columns[out.ncolumns++] = COL_NAME;
135 }
136
137 /* add -o [+]<list> to putput */
138 if (outarg && string_add_to_idarray(outarg, out.columns,
139 ARRAY_SIZE(out.columns),
140 &out.ncolumns,
141 irq_column_name_to_id) < 0)
142 exit(EXIT_FAILURE);
143
144 return print_irq_data(&out, softirq) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
145 }