]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lsmem.c
misc: consolidate version printing and close_stdout()
[thirdparty/util-linux.git] / sys-utils / lsmem.c
CommitLineData
cad2d1ac
HC
1/*
2 * lsmem - Show memory configuration
3 *
4 * Copyright IBM Corp. 2016
a5c4535b 5 * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
cad2d1ac
HC
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
cad2d1ac
HC
21#include <c.h>
22#include <nls.h>
23#include <path.h>
24#include <strutils.h>
25#include <closestream.h>
26#include <xalloc.h>
27#include <getopt.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <dirent.h>
cad2d1ac
HC
31#include <fcntl.h>
32#include <inttypes.h>
33#include <assert.h>
34#include <optutils.h>
35#include <libsmartcols.h>
36
37#define _PATH_SYS_MEMORY "/sys/devices/system/memory"
cad2d1ac
HC
38
39#define MEMORY_STATE_ONLINE 0
40#define MEMORY_STATE_OFFLINE 1
41#define MEMORY_STATE_GOING_OFFLINE 2
42#define MEMORY_STATE_UNKNOWN 3
43
60a7e9e9
GS
44enum zone_id {
45 ZONE_DMA = 0,
46 ZONE_DMA32,
47 ZONE_NORMAL,
48 ZONE_HIGHMEM,
49 ZONE_MOVABLE,
50 ZONE_DEVICE,
51 ZONE_NONE,
52 ZONE_UNKNOWN,
53 MAX_NR_ZONES,
54};
55
cad2d1ac
HC
56struct memory_block {
57 uint64_t index;
58 uint64_t count;
59 int state;
60 int node;
60a7e9e9
GS
61 int nr_zones;
62 int zones[MAX_NR_ZONES];
cad2d1ac
HC
63 unsigned int removable:1;
64};
65
0cf0710a 66struct lsmem {
e4319a10 67 struct path_cxt *sysmem; /* _PATH_SYS_MEMORY directory handler */
cad2d1ac
HC
68 struct dirent **dirs;
69 int ndirs;
70 struct memory_block *blocks;
71 int nblocks;
cad2d1ac
HC
72 uint64_t block_size;
73 uint64_t mem_online;
74 uint64_t mem_offline;
cad2d1ac 75
0a1ed6df
KZ
76 struct libscols_table *table;
77 unsigned int have_nodes : 1,
78 raw : 1,
79 export : 1,
80 json : 1,
81 noheadings : 1,
82 summary : 1,
83 list_all : 1,
84 bytes : 1,
c5332fbb 85 want_summary : 1,
60a7e9e9 86 want_table : 1,
d12e945d
KZ
87 split_by_node : 1,
88 split_by_state : 1,
89 split_by_removable : 1,
90 split_by_zones : 1,
60a7e9e9 91 have_zones : 1;
cad2d1ac
HC
92};
93
d12e945d 94
cad2d1ac
HC
95enum {
96 COL_RANGE,
97 COL_SIZE,
98 COL_STATE,
99 COL_REMOVABLE,
100 COL_BLOCK,
101 COL_NODE,
60a7e9e9
GS
102 COL_ZONES,
103};
104
105static char *zone_names[] = {
106 [ZONE_DMA] = "DMA",
107 [ZONE_DMA32] = "DMA32",
108 [ZONE_NORMAL] = "Normal",
109 [ZONE_HIGHMEM] = "Highmem",
110 [ZONE_MOVABLE] = "Movable",
111 [ZONE_DEVICE] = "Device",
112 [ZONE_NONE] = "None", /* block contains more than one zone, can't be offlined */
113 [ZONE_UNKNOWN] = "Unknown",
cad2d1ac
HC
114};
115
0a1ed6df
KZ
116/* column names */
117struct coldesc {
118 const char *name; /* header */
119 double whint; /* width hint (N < 1 is in percent of termwidth) */
120 int flags; /* SCOLS_FL_* */
121 const char *help;
cad2d1ac
HC
122};
123
0a1ed6df
KZ
124/* columns descriptions */
125static struct coldesc coldescs[] = {
4775cc69
KZ
126 [COL_RANGE] = { "RANGE", 0, 0, N_("start and end address of the memory range")},
127 [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the memory range")},
4c0e1eaf 128 [COL_STATE] = { "STATE", 0, SCOLS_FL_RIGHT, N_("online status of the memory range")},
0a1ed6df 129 [COL_REMOVABLE] = { "REMOVABLE", 0, SCOLS_FL_RIGHT, N_("memory is removable")},
4775cc69
KZ
130 [COL_BLOCK] = { "BLOCK", 0, SCOLS_FL_RIGHT, N_("memory block number or blocks range")},
131 [COL_NODE] = { "NODE", 0, SCOLS_FL_RIGHT, N_("numa node of memory")},
60a7e9e9 132 [COL_ZONES] = { "ZONES", 0, SCOLS_FL_RIGHT, N_("valid zones for the memory range")},
cad2d1ac
HC
133};
134
0a1ed6df
KZ
135/* columns[] array specifies all currently wanted output column. The columns
136 * are defined by coldescs[] array and you can specify (on command line) each
137 * column twice. That's enough, dynamically allocated array of the columns is
138 * unnecessary overkill and over-engineering in this case */
139static int columns[ARRAY_SIZE(coldescs) * 2];
140static size_t ncolumns;
141
142static inline size_t err_columns_index(size_t arysz, size_t idx)
143{
144 if (idx >= arysz)
145 errx(EXIT_FAILURE, _("too many columns specified, "
146 "the limit is %zu columns"),
147 arysz - 1);
148 return idx;
149}
150
60a7e9e9
GS
151/*
152 * name must be null-terminated
153 */
154static int zone_name_to_id(const char *name)
155{
156 size_t i;
157
158 for (i = 0; i < ARRAY_SIZE(zone_names); i++) {
159 if (!strcasecmp(name, zone_names[i]))
160 return i;
161 }
162 return ZONE_UNKNOWN;
163}
164
0a1ed6df
KZ
165#define add_column(ary, n, id) \
166 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
167
cad2d1ac
HC
168static int column_name_to_id(const char *name, size_t namesz)
169{
170 size_t i;
171
172 for (i = 0; i < ARRAY_SIZE(coldescs); i++) {
173 const char *cn = coldescs[i].name;
174
175 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
176 return i;
177 }
178 warnx(_("unknown column: %s"), name);
179 return -1;
180}
181
0a1ed6df 182static inline int get_column_id(int num)
cad2d1ac 183{
0a1ed6df
KZ
184 assert(num >= 0);
185 assert((size_t) num < ncolumns);
186 assert(columns[num] < (int) ARRAY_SIZE(coldescs));
187
188 return columns[num];
cad2d1ac
HC
189}
190
0a1ed6df 191static inline struct coldesc *get_column_desc(int num)
cad2d1ac 192{
0a1ed6df 193 return &coldescs[ get_column_id(num) ];
cad2d1ac
HC
194}
195
d12e945d 196static inline void reset_split_policy(struct lsmem *l, int enable)
cad2d1ac 197{
d12e945d
KZ
198 l->split_by_state = enable;
199 l->split_by_node = enable;
200 l->split_by_removable = enable;
201 l->split_by_zones = enable;
cad2d1ac
HC
202}
203
96cbe362
KZ
204static void set_split_policy(struct lsmem *l, int cols[], size_t ncols)
205{
206 size_t i;
207
208 reset_split_policy(l, 0);
209
210 for (i = 0; i < ncols; i++) {
211 switch (cols[i]) {
212 case COL_STATE:
213 l->split_by_state = 1;
214 break;
215 case COL_NODE:
216 l->split_by_node = 1;
217 break;
218 case COL_REMOVABLE:
219 l->split_by_removable = 1;
220 break;
221 case COL_ZONES:
222 l->split_by_zones = 1;
223 break;
224 default:
225 break;
226 }
227 }
228}
229
0cf0710a 230static void add_scols_line(struct lsmem *lsmem, struct memory_block *blk)
cad2d1ac 231{
0a1ed6df
KZ
232 size_t i;
233 struct libscols_line *line;
cad2d1ac 234
0cf0710a 235 line = scols_table_new_line(lsmem->table, NULL);
0a1ed6df
KZ
236 if (!line)
237 err_oom();
cad2d1ac 238
0a1ed6df
KZ
239 for (i = 0; i < ncolumns; i++) {
240 char *str = NULL;
cad2d1ac 241
0a1ed6df
KZ
242 switch (get_column_id(i)) {
243 case COL_RANGE:
244 {
0cf0710a
KZ
245 uint64_t start = blk->index * lsmem->block_size;
246 uint64_t size = blk->count * lsmem->block_size;
0a1ed6df
KZ
247 xasprintf(&str, "0x%016"PRIx64"-0x%016"PRIx64, start, start + size - 1);
248 break;
249 }
250 case COL_SIZE:
0cf0710a
KZ
251 if (lsmem->bytes)
252 xasprintf(&str, "%"PRId64, (uint64_t) blk->count * lsmem->block_size);
0a1ed6df
KZ
253 else
254 str = size_to_human_string(SIZE_SUFFIX_1LETTER,
0cf0710a 255 (uint64_t) blk->count * lsmem->block_size);
0a1ed6df
KZ
256 break;
257 case COL_STATE:
258 str = xstrdup(
259 blk->state == MEMORY_STATE_ONLINE ? _("online") :
260 blk->state == MEMORY_STATE_OFFLINE ? _("offline") :
261 blk->state == MEMORY_STATE_GOING_OFFLINE ? _("on->off") :
262 "?");
263 break;
264 case COL_REMOVABLE:
265 if (blk->state == MEMORY_STATE_ONLINE)
266 str = xstrdup(blk->removable ? _("yes") : _("no"));
267 break;
268 case COL_BLOCK:
269 if (blk->count == 1)
270 xasprintf(&str, "%"PRId64, blk->index);
271 else
272 xasprintf(&str, "%"PRId64"-%"PRId64,
273 blk->index, blk->index + blk->count - 1);
274 break;
275 case COL_NODE:
0cf0710a 276 if (lsmem->have_nodes)
0a1ed6df
KZ
277 xasprintf(&str, "%d", blk->node);
278 break;
60a7e9e9
GS
279 case COL_ZONES:
280 if (lsmem->have_zones) {
281 char valid_zones[BUFSIZ];
282 int j, zone_id;
283
284 valid_zones[0] = '\0';
285 for (j = 0; j < blk->nr_zones; j++) {
286 zone_id = blk->zones[j];
287 if (strlen(valid_zones) +
288 strlen(zone_names[zone_id]) > BUFSIZ - 2)
289 break;
290 strcat(valid_zones, zone_names[zone_id]);
291 if (j + 1 < blk->nr_zones)
292 strcat(valid_zones, "/");
293 }
294 str = xstrdup(valid_zones);
92368ce1 295 }
60a7e9e9 296 break;
cad2d1ac 297 }
0a1ed6df
KZ
298
299 if (str && scols_line_refer_data(line, i, str) != 0)
300 err_oom();
cad2d1ac 301 }
cad2d1ac
HC
302}
303
0cf0710a 304static void fill_scols_table(struct lsmem *lsmem)
0a1ed6df
KZ
305{
306 int i;
307
0cf0710a
KZ
308 for (i = 0; i < lsmem->nblocks; i++)
309 add_scols_line(lsmem, &lsmem->blocks[i]);
0a1ed6df
KZ
310}
311
0cf0710a 312static void print_summary(struct lsmem *lsmem)
cad2d1ac 313{
c5332fbb
KZ
314 if (lsmem->bytes) {
315 printf("%-23s %15"PRId64"\n",_("Memory block size:"), lsmem->block_size);
316 printf("%-23s %15"PRId64"\n",_("Total online memory:"), lsmem->mem_online);
317 printf("%-23s %15"PRId64"\n",_("Total offline memory:"), lsmem->mem_offline);
318 } else {
8e7a0c16
KZ
319 char *p;
320
321 if ((p = size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->block_size)))
322 printf("%-23s %5s\n",_("Memory block size:"), p);
323 free(p);
324
325 if ((p = size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->mem_online)))
326 printf("%-23s %5s\n",_("Total online memory:"), p);
327 free(p);
328
329 if ((p = size_to_human_string(SIZE_SUFFIX_1LETTER, lsmem->mem_offline)))
330 printf("%-23s %5s\n",_("Total offline memory:"), p);
331 free(p);
c5332fbb 332 }
cad2d1ac
HC
333}
334
e4319a10 335static int memory_block_get_node(struct lsmem *lsmem, char *name)
cad2d1ac
HC
336{
337 struct dirent *de;
cad2d1ac
HC
338 DIR *dir;
339 int node;
340
e4319a10
KZ
341 dir = ul_path_opendir(lsmem->sysmem, name);
342 if (!dir)
343 err(EXIT_FAILURE, _("Failed to open %s"), name);
81435af3 344
cad2d1ac
HC
345 node = -1;
346 while ((de = readdir(dir)) != NULL) {
347 if (strncmp("node", de->d_name, 4))
348 continue;
349 if (!isdigit_string(de->d_name + 4))
350 continue;
351 node = strtol(de->d_name + 4, NULL, 10);
51a90159 352 break;
cad2d1ac
HC
353 }
354 closedir(dir);
355 return node;
356}
357
0cf0710a 358static void memory_block_read_attrs(struct lsmem *lsmem, char *name,
cad2d1ac
HC
359 struct memory_block *blk)
360{
e4319a10
KZ
361 char *line = NULL;
362 int i, x = 0;
363
364 memset(blk, 0, sizeof(*blk));
cad2d1ac
HC
365
366 blk->count = 1;
cad2d1ac 367 blk->state = MEMORY_STATE_UNKNOWN;
e4319a10 368 blk->index = strtoumax(name + 6, NULL, 10); /* get <num> of "memory<num>" */
9c41d270 369
e4319a10
KZ
370 if (ul_path_readf_s32(lsmem->sysmem, &x, "%s/removable", name) == 0)
371 blk->removable = x == 1;
372
373 if (ul_path_readf_string(lsmem->sysmem, &line, "%s/state", name) > 0) {
374 if (strcmp(line, "offline") == 0)
375 blk->state = MEMORY_STATE_OFFLINE;
376 else if (strcmp(line, "online") == 0)
377 blk->state = MEMORY_STATE_ONLINE;
378 else if (strcmp(line, "going-offline") == 0)
379 blk->state = MEMORY_STATE_GOING_OFFLINE;
380 free(line);
381 }
9c41d270 382
0cf0710a 383 if (lsmem->have_nodes)
e4319a10 384 blk->node = memory_block_get_node(lsmem, name);
60a7e9e9
GS
385
386 blk->nr_zones = 0;
e4319a10
KZ
387 if (lsmem->have_zones &&
388 ul_path_readf_string(lsmem->sysmem, &line, "%s/valid_zones", name) > 0) {
389
390 char *token = strtok(line, " ");
391
392 for (i = 0; token && i < MAX_NR_ZONES; i++) {
60a7e9e9
GS
393 blk->zones[i] = zone_name_to_id(token);
394 blk->nr_zones++;
395 token = strtok(NULL, " ");
396 }
e4319a10
KZ
397
398 free(line);
60a7e9e9 399 }
cad2d1ac
HC
400}
401
0cf0710a 402static int is_mergeable(struct lsmem *lsmem, struct memory_block *blk)
cad2d1ac
HC
403{
404 struct memory_block *curr;
60a7e9e9 405 int i;
cad2d1ac 406
0cf0710a 407 if (!lsmem->nblocks)
cad2d1ac 408 return 0;
0cf0710a
KZ
409 curr = &lsmem->blocks[lsmem->nblocks - 1];
410 if (lsmem->list_all)
cad2d1ac
HC
411 return 0;
412 if (curr->index + curr->count != blk->index)
413 return 0;
d12e945d 414 if (lsmem->split_by_state && curr->state != blk->state)
cad2d1ac 415 return 0;
d12e945d 416 if (lsmem->split_by_removable && curr->removable != blk->removable)
cad2d1ac 417 return 0;
d12e945d 418 if (lsmem->split_by_node && lsmem->have_nodes) {
cad2d1ac
HC
419 if (curr->node != blk->node)
420 return 0;
421 }
d12e945d 422 if (lsmem->split_by_zones && lsmem->have_zones) {
60a7e9e9
GS
423 if (curr->nr_zones != blk->nr_zones)
424 return 0;
425 for (i = 0; i < curr->nr_zones; i++) {
426 if (curr->zones[i] == ZONE_UNKNOWN ||
427 curr->zones[i] != blk->zones[i])
428 return 0;
429 }
430 }
cad2d1ac
HC
431 return 1;
432}
433
0cf0710a 434static void read_info(struct lsmem *lsmem)
cad2d1ac 435{
cad2d1ac 436 struct memory_block blk;
e4319a10 437 char buf[128];
cad2d1ac
HC
438 int i;
439
e4319a10
KZ
440 if (ul_path_read_buffer(lsmem->sysmem, buf, sizeof(buf), "block_size_bytes") <= 0)
441 err(EXIT_FAILURE, _("failed to read memory block size"));
442 lsmem->block_size = strtoumax(buf, NULL, 16);
cad2d1ac 443
0cf0710a
KZ
444 for (i = 0; i < lsmem->ndirs; i++) {
445 memory_block_read_attrs(lsmem, lsmem->dirs[i]->d_name, &blk);
49834246
GS
446 if (blk.state == MEMORY_STATE_ONLINE)
447 lsmem->mem_online += lsmem->block_size;
448 else
449 lsmem->mem_offline += lsmem->block_size;
0cf0710a
KZ
450 if (is_mergeable(lsmem, &blk)) {
451 lsmem->blocks[lsmem->nblocks - 1].count++;
cad2d1ac
HC
452 continue;
453 }
0cf0710a
KZ
454 lsmem->nblocks++;
455 lsmem->blocks = xrealloc(lsmem->blocks, lsmem->nblocks * sizeof(blk));
456 *&lsmem->blocks[lsmem->nblocks - 1] = blk;
cad2d1ac 457 }
cad2d1ac
HC
458}
459
460static int memory_block_filter(const struct dirent *de)
461{
462 if (strncmp("memory", de->d_name, 6))
463 return 0;
464 return isdigit_string(de->d_name + 6);
465}
466
0cf0710a 467static void read_basic_info(struct lsmem *lsmem)
cad2d1ac 468{
e4319a10 469 char dir[PATH_MAX];
cad2d1ac 470
e4319a10 471 if (ul_path_access(lsmem->sysmem, F_OK, "block_size_bytes") != 0)
cad2d1ac
HC
472 errx(EXIT_FAILURE, _("This system does not support memory blocks"));
473
e4319a10 474 ul_path_get_abspath(lsmem->sysmem, dir, sizeof(dir), NULL);
81435af3 475
0cf0710a 476 lsmem->ndirs = scandir(dir, &lsmem->dirs, memory_block_filter, versionsort);
0cf0710a 477 if (lsmem->ndirs <= 0)
e4319a10 478 err(EXIT_FAILURE, _("Failed to read %s"), dir);
cad2d1ac 479
e4319a10 480 if (memory_block_get_node(lsmem, lsmem->dirs[0]->d_name) != -1)
0cf0710a 481 lsmem->have_nodes = 1;
60a7e9e9 482
e4319a10
KZ
483 /* The valid_zones sysmem attribute was introduced with kernel 3.18 */
484 if (ul_path_access(lsmem->sysmem, F_OK, "memory0/valid_zones") == 0)
60a7e9e9 485 lsmem->have_zones = 1;
cad2d1ac
HC
486}
487
6e1eda6f 488static void __attribute__((__noreturn__)) usage(void)
cad2d1ac 489{
6e1eda6f 490 FILE *out = stdout;
9c41d270 491 size_t i;
cad2d1ac
HC
492
493 fputs(USAGE_HEADER, out);
494 fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
495
496 fputs(USAGE_SEPARATOR, out);
497 fputs(_("List the ranges of available memory with their online status.\n"), out);
498
499 fputs(USAGE_OPTIONS, out);
0a1ed6df
KZ
500 fputs(_(" -J, --json use JSON output format\n"), out);
501 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
0508f347 502 fputs(_(" -a, --all list each individual memory block\n"), out);
0a1ed6df
KZ
503 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
504 fputs(_(" -n, --noheadings don't print headings\n"), out);
505 fputs(_(" -o, --output <list> output columns\n"), out);
fcd4bbff 506 fputs(_(" --output-all output all columns\n"), out);
0a1ed6df 507 fputs(_(" -r, --raw use raw output format\n"), out);
d12e945d 508 fputs(_(" -S, --split <list> split ranges by specified columns\n"), out);
0a1ed6df 509 fputs(_(" -s, --sysroot <dir> use the specified directory as system root\n"), out);
c5332fbb 510 fputs(_(" --summary[=when] print summary information (never,always or only)\n"), out);
0a1ed6df 511
cad2d1ac 512 fputs(USAGE_SEPARATOR, out);
f45f3ec3 513 printf(USAGE_HELP_OPTIONS(22));
cad2d1ac 514
6e2d5a44 515 fputs(USAGE_COLUMNS, out);
cad2d1ac 516 for (i = 0; i < ARRAY_SIZE(coldescs); i++)
6e2d5a44 517 fprintf(out, " %10s %s\n", coldescs[i].name, _(coldescs[i].help));
cad2d1ac 518
f45f3ec3 519 printf(USAGE_MAN_TAIL("lsmem(1)"));
cad2d1ac 520
2c308875 521 exit(EXIT_SUCCESS);
cad2d1ac
HC
522}
523
524int main(int argc, char **argv)
525{
c5332fbb
KZ
526 struct lsmem _lsmem = {
527 .want_table = 1,
528 .want_summary = 1
529 }, *lsmem = &_lsmem;
530
e4319a10 531 const char *outarg = NULL, *splitarg = NULL, *prefix = NULL;
cad2d1ac 532 int c;
0a1ed6df 533 size_t i;
cad2d1ac 534
c5332fbb 535 enum {
fcd4bbff
SK
536 LSMEM_OPT_SUMARRY = CHAR_MAX + 1,
537 OPT_OUTPUT_ALL
c5332fbb
KZ
538 };
539
cad2d1ac
HC
540 static const struct option longopts[] = {
541 {"all", no_argument, NULL, 'a'},
0a1ed6df 542 {"bytes", no_argument, NULL, 'b'},
cad2d1ac 543 {"help", no_argument, NULL, 'h'},
0a1ed6df
KZ
544 {"json", no_argument, NULL, 'J'},
545 {"noheadings", no_argument, NULL, 'n'},
546 {"output", required_argument, NULL, 'o'},
fcd4bbff 547 {"output-all", no_argument, NULL, OPT_OUTPUT_ALL},
0a1ed6df
KZ
548 {"pairs", no_argument, NULL, 'P'},
549 {"raw", no_argument, NULL, 'r'},
cad2d1ac 550 {"sysroot", required_argument, NULL, 's'},
d12e945d 551 {"split", required_argument, NULL, 'S'},
cad2d1ac 552 {"version", no_argument, NULL, 'V'},
c5332fbb 553 {"summary", optional_argument, NULL, LSMEM_OPT_SUMARRY },
cad2d1ac
HC
554 {NULL, 0, NULL, 0}
555 };
556 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
0a1ed6df 557 { 'J', 'P', 'r' },
d12e945d 558 { 'S', 'a' },
cad2d1ac
HC
559 { 0 }
560 };
561 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
562
563 setlocale(LC_ALL, "");
564 bindtextdomain(PACKAGE, LOCALEDIR);
565 textdomain(PACKAGE);
2c308875 566 close_stdout_atexit();
cad2d1ac 567
d12e945d 568 while ((c = getopt_long(argc, argv, "abhJno:PrS:s:V", longopts, NULL)) != -1) {
cad2d1ac
HC
569
570 err_exclusive_options(c, longopts, excl, excl_st);
571
572 switch (c) {
573 case 'a':
0cf0710a 574 lsmem->list_all = 1;
0a1ed6df
KZ
575 break;
576 case 'b':
0cf0710a 577 lsmem->bytes = 1;
cad2d1ac 578 break;
0a1ed6df 579 case 'J':
0cf0710a 580 lsmem->json = 1;
019ad6c1 581 lsmem->want_summary = 0;
0a1ed6df
KZ
582 break;
583 case 'n':
0cf0710a 584 lsmem->noheadings = 1;
0a1ed6df
KZ
585 break;
586 case 'o':
587 outarg = optarg;
588 break;
fcd4bbff
SK
589 case OPT_OUTPUT_ALL:
590 for (ncolumns = 0; (size_t)ncolumns < ARRAY_SIZE(coldescs); ncolumns++)
591 columns[ncolumns] = ncolumns;
592 break;
0a1ed6df 593 case 'P':
0cf0710a 594 lsmem->export = 1;
019ad6c1 595 lsmem->want_summary = 0;
0a1ed6df
KZ
596 break;
597 case 'r':
0cf0710a 598 lsmem->raw = 1;
019ad6c1 599 lsmem->want_summary = 0;
cad2d1ac
HC
600 break;
601 case 's':
e4319a10 602 prefix = optarg;
cad2d1ac 603 break;
d12e945d
KZ
604 case 'S':
605 splitarg = optarg;
606 break;
c5332fbb
KZ
607 case LSMEM_OPT_SUMARRY:
608 if (optarg) {
609 if (strcmp(optarg, "never") == 0)
610 lsmem->want_summary = 0;
611 else if (strcmp(optarg, "only") == 0)
612 lsmem->want_table = 0;
613 else if (strcmp(optarg, "always") == 0)
614 lsmem->want_summary = 1;
615 else
616 errx(EXIT_FAILURE, _("unsupported --summary argument"));
617 } else
618 lsmem->want_table = 0;
619 break;
2c308875
KZ
620
621 case 'h':
622 usage();
623 case 'V':
624 print_version(EXIT_SUCCESS);
cad2d1ac 625 default:
677ec86c 626 errtryhelp(EXIT_FAILURE);
cad2d1ac
HC
627 }
628 }
629
6e1eda6f
RM
630 if (argc != optind) {
631 warnx(_("bad usage"));
632 errtryhelp(EXIT_FAILURE);
633 }
cad2d1ac 634
d4625442
KZ
635 if (lsmem->want_table + lsmem->want_summary == 0)
636 errx(EXIT_FAILURE, _("options --{raw,json,pairs} and --summary=only are mutually exclusive"));
637
e4319a10
KZ
638 ul_path_init_debug();
639
640 lsmem->sysmem = ul_new_path(_PATH_SYS_MEMORY);
641 if (!lsmem->sysmem)
642 err(EXIT_FAILURE, _("failed to initialize %s handler"), _PATH_SYS_MEMORY);
643 if (prefix && ul_path_set_prefix(lsmem->sysmem, prefix) != 0)
644 err(EXIT_FAILURE, _("invalid argument to --sysroot"));
645
d4625442
KZ
646 /* Shortcut to avoid scols machinery on --summary=only */
647 if (lsmem->want_table == 0 && lsmem->want_summary) {
648 read_basic_info(lsmem);
649 read_info(lsmem);
650 print_summary(lsmem);
651 return EXIT_SUCCESS;
652 }
653
0a1ed6df
KZ
654 /*
655 * Default columns
656 */
cad2d1ac 657 if (!ncolumns) {
0a1ed6df
KZ
658 add_column(columns, ncolumns++, COL_RANGE);
659 add_column(columns, ncolumns++, COL_SIZE);
660 add_column(columns, ncolumns++, COL_STATE);
661 add_column(columns, ncolumns++, COL_REMOVABLE);
662 add_column(columns, ncolumns++, COL_BLOCK);
cad2d1ac
HC
663 }
664
0a1ed6df
KZ
665 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
666 &ncolumns, column_name_to_id) < 0)
667 return EXIT_FAILURE;
668
669 /*
670 * Initialize output
671 */
672 scols_init_debug(0);
673
0cf0710a 674 if (!(lsmem->table = scols_new_table()))
0a1ed6df 675 errx(EXIT_FAILURE, _("failed to initialize output table"));
0cf0710a
KZ
676 scols_table_enable_raw(lsmem->table, lsmem->raw);
677 scols_table_enable_export(lsmem->table, lsmem->export);
678 scols_table_enable_json(lsmem->table, lsmem->json);
679 scols_table_enable_noheadings(lsmem->table, lsmem->noheadings);
0a1ed6df 680
0cf0710a
KZ
681 if (lsmem->json)
682 scols_table_set_name(lsmem->table, "memory");
cad2d1ac 683
0a1ed6df
KZ
684 for (i = 0; i < ncolumns; i++) {
685 struct coldesc *ci = get_column_desc(i);
704f9ba6
KZ
686 struct libscols_column *cl;
687
688 cl = scols_table_new_column(lsmem->table, ci->name, ci->whint, ci->flags);
689 if (!cl)
0a1ed6df 690 err(EXIT_FAILURE, _("Failed to initialize output column"));
704f9ba6
KZ
691
692 if (lsmem->json) {
693 int id = get_column_id(i);
694
695 switch (id) {
696 case COL_SIZE:
697 if (!lsmem->bytes)
698 break;
699 /* fallthrough */
700 case COL_NODE:
701 scols_column_set_json_type(cl, SCOLS_JSON_NUMBER);
702 break;
703 case COL_REMOVABLE:
704 scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN);
705 break;
706 }
707 }
cad2d1ac 708 }
0a1ed6df 709
d12e945d
KZ
710 if (splitarg) {
711 int split[ARRAY_SIZE(coldescs)] = { 0 };
712 static size_t nsplits = 0;
713
d12e945d
KZ
714 if (strcasecmp(splitarg, "none") == 0)
715 ;
716 else if (string_add_to_idarray(splitarg, split, ARRAY_SIZE(split),
717 &nsplits, column_name_to_id) < 0)
718 return EXIT_FAILURE;
719
96cbe362
KZ
720 set_split_policy(lsmem, split, nsplits);
721
d12e945d 722 } else
96cbe362
KZ
723 /* follow output columns */
724 set_split_policy(lsmem, columns, ncolumns);
0a1ed6df
KZ
725
726 /*
727 * Read data and print output
728 */
0cf0710a
KZ
729 read_basic_info(lsmem);
730 read_info(lsmem);
0a1ed6df 731
c5332fbb
KZ
732 if (lsmem->want_table) {
733 fill_scols_table(lsmem);
734 scols_print_table(lsmem->table);
735
736 if (lsmem->want_summary)
737 fputc('\n', stdout);
738 }
0a1ed6df 739
c5332fbb
KZ
740 if (lsmem->want_summary)
741 print_summary(lsmem);
0a1ed6df 742
0cf0710a 743 scols_unref_table(lsmem->table);
e4319a10 744 ul_unref_path(lsmem->sysmem);
cad2d1ac
HC
745 return 0;
746}