]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/lscpu.c
libblkid: do not interpret NTFS as MBR
[thirdparty/util-linux.git] / sys-utils / lscpu.c
index c853406efb1d700651a70e4ea6c67809d3d7c6ad..4f1ab47e90380ad58ca56fd6a6fea57e43807f0e 100644 (file)
@@ -61,6 +61,7 @@
 
 #include "closestream.h"
 #include "optutils.h"
+#include "fileutils.h"
 
 #include "lscpu.h"
 
@@ -177,6 +178,11 @@ enum {
        COL_CACHE_ONESIZE,
        COL_CACHE_TYPE,
        COL_CACHE_WAYS,
+       COL_CACHE_ALLOCPOL,
+       COL_CACHE_WRITEPOL,
+       COL_CACHE_PHYLINE,
+       COL_CACHE_SETS,
+       COL_CACHE_COHERENCYSIZE
 };
 
 
@@ -214,7 +220,12 @@ static struct lscpu_coldesc coldescs_cache[] =
        [COL_CACHE_NAME]       = { "NAME", N_("cache name") },
        [COL_CACHE_ONESIZE]    = { "ONE-SIZE", N_("size of one cache"), SCOLS_FL_RIGHT },
        [COL_CACHE_TYPE]       = { "TYPE", N_("cache type") },
-       [COL_CACHE_WAYS]       = { "WAYS", N_("ways of associativity"), SCOLS_FL_RIGHT }
+       [COL_CACHE_WAYS]       = { "WAYS", N_("ways of associativity"), SCOLS_FL_RIGHT },
+       [COL_CACHE_ALLOCPOL]   = { "ALLOC-POLICY", N_("allocation policy") },
+       [COL_CACHE_WRITEPOL]   = { "WRITE-POLICY", N_("write policy") },
+       [COL_CACHE_PHYLINE]    = { "PHY-LINE", N_("number of physical cache line per cache t"), SCOLS_FL_RIGHT },
+       [COL_CACHE_SETS]       = { "SETS", N_("number of sets in the cache; set lines has the same cache index"), SCOLS_FL_RIGHT },
+       [COL_CACHE_COHERENCYSIZE] = { "COHERENCY-SIZE", N_("minimum amount of data in bytes transferred from memory to cache"), SCOLS_FL_RIGHT }
 };
 
 
@@ -436,6 +447,67 @@ static void read_physical_info_powerpc(
 }
 #endif
 
+static int cmp_vulnerability_name(const void *a0, const void *b0)
+{
+       const struct cpu_vulnerability *a = (const struct cpu_vulnerability *) a0,
+                                      *b = (const struct cpu_vulnerability *) b0;
+       return strcmp(a->name, b->name);
+}
+
+static void read_vulnerabilities(struct lscpu_desc *desc)
+{
+       struct dirent *d;
+       DIR *dir = ul_path_opendir(desc->syscpu, "vulnerabilities");
+       int n = 0;
+
+       if (!dir)
+               return;
+
+       desc->nvuls = n = 0;
+
+       while (xreaddir(dir))
+               n++;
+       if (!n)
+               return;
+
+       rewinddir(dir);
+       desc->vuls = xcalloc(n, sizeof(struct cpu_vulnerability));
+
+       while (desc->nvuls < n && (d = xreaddir(dir))) {
+               char *str, *p;
+               struct cpu_vulnerability *vu;
+
+#ifdef _DIRENT_HAVE_D_TYPE
+               if (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN)
+                       continue;
+#endif
+               if (ul_path_readf_string(desc->syscpu, &str,
+                                       "vulnerabilities/%s", d->d_name) <= 0)
+                       continue;
+
+               vu = &desc->vuls[desc->nvuls++];
+
+               /* Name */
+               vu->name = xstrdup(d->d_name);
+               *vu->name = toupper(*vu->name);
+               strrep(vu->name, '_', ' ');
+
+               /* Description */
+               vu->text = str;
+               p = (char *) startswith(vu->text, "Mitigation");
+               if (p) {
+                       *p = ';';
+                       strrem(vu->text, ':');
+               }
+       }
+       closedir(dir);
+
+       qsort(desc->vuls, desc->nvuls,
+             sizeof(struct cpu_vulnerability), cmp_vulnerability_name);
+}
+
+
+
 
 static void
 read_basicinfo(struct lscpu_desc *desc, struct lscpu_modifier *mod)
@@ -564,10 +636,16 @@ read_basicinfo(struct lscpu_desc *desc, struct lscpu_modifier *mod)
                read_physical_info_powerpc(desc);
 
        if ((fp = ul_path_fopen(desc->procfs, "r", "sysinfo"))) {
-               while (fgets(buf, sizeof(buf), fp) != NULL && !desc->machinetype)
-                       lookup(buf, "Type", &desc->machinetype);
+               while (fgets(buf, sizeof(buf), fp) != NULL) {
+                       if (lookup(buf, "Type", &desc->machinetype))
+                               break;
+               }
                fclose(fp);
        }
+
+       /* vulnerabilities */
+       if (ul_path_access(desc->syscpu, F_OK, "vulnerabilities") == 0)
+               read_vulnerabilities(desc);
 }
 
 static int
@@ -1286,9 +1364,19 @@ read_cache(struct lscpu_desc *desc, int idx)
 
                        ca->name = xstrdup(buf);
 
-                       /* cache ways */
-                       ul_path_readf_s32(desc->syscpu, &ca->ways,
+                       ul_path_readf_u32(desc->syscpu, &ca->ways_of_associativity,
                                        "cpu%d/cache/index%d/ways_of_associativity", num, i);
+                       ul_path_readf_u32(desc->syscpu, &ca->physical_line_partition,
+                                       "cpu%d/cache/index%d/physical_line_partition", num, i);
+                       ul_path_readf_u32(desc->syscpu, &ca->number_of_sets,
+                                       "cpu%d/cache/index%d/number_of_sets", num, i);
+                       ul_path_readf_u32(desc->syscpu, &ca->coherency_line_size,
+                                       "cpu%d/cache/index%d/coherency_line_size", num, i);
+
+                       ul_path_readf_string(desc->syscpu, &ca->allocation_policy,
+                                       "cpu%d/cache/index%d/allocation_policy", num, i);
+                       ul_path_readf_string(desc->syscpu, &ca->write_policy,
+                                       "cpu%d/cache/index%d/write_policy", num, i);
 
                        /* cache size */
                        if (ul_path_readf_buffer(desc->syscpu, buf, sizeof(buf),
@@ -1614,9 +1702,10 @@ print_caches_readable(struct lscpu_desc *desc, int cols[], int ncols,
                                break;
                        }
                        case COL_CACHE_WAYS:
-                               if (ca->ways)
-                                       xasprintf(&data, "%d", ca->ways);
+                               if (ca->ways_of_associativity)
+                                       xasprintf(&data, "%u", ca->ways_of_associativity);
                                break;
+
                        case COL_CACHE_TYPE:
                                if (ca->type)
                                        data = xstrdup(ca->type);
@@ -1625,6 +1714,26 @@ print_caches_readable(struct lscpu_desc *desc, int cols[], int ncols,
                                if (ca->level)
                                        xasprintf(&data, "%d", ca->level);
                                break;
+                       case COL_CACHE_ALLOCPOL:
+                               if (ca->allocation_policy)
+                                       data = xstrdup(ca->allocation_policy);
+                               break;
+                       case COL_CACHE_WRITEPOL:
+                               if (ca->write_policy)
+                                       data = xstrdup(ca->write_policy);
+                               break;
+                       case COL_CACHE_PHYLINE:
+                               if (ca->physical_line_partition)
+                                       xasprintf(&data, "%u", ca->physical_line_partition);
+                               break;
+                       case COL_CACHE_SETS:
+                               if (ca->number_of_sets)
+                                       xasprintf(&data, "%u", ca->number_of_sets);
+                               break;
+                       case COL_CACHE_COHERENCYSIZE:
+                               if (ca->coherency_line_size)
+                                       xasprintf(&data, "%u", ca->coherency_line_size);
+                               break;
                        }
 
                        if (data && scols_line_refer_data(line, c, data))
@@ -1712,10 +1821,12 @@ print_cpus_parsable(struct lscpu_desc *desc, int cols[], int ncols,
                int c;
                int cpu = real_cpu_num(desc, i);
 
-               if (!mod->offline && desc->online && !is_cpu_online(desc, cpu))
-                       continue;
-               if (!mod->online && desc->online && is_cpu_online(desc, cpu))
-                       continue;
+               if (desc->online) {
+                       if (!mod->offline && !is_cpu_online(desc, cpu))
+                               continue;
+                       if (!mod->online && is_cpu_online(desc, cpu))
+                               continue;
+               }
                if (desc->present && !is_cpu_present(desc, cpu))
                        continue;
                for (c = 0; c < ncols; c++) {
@@ -1769,10 +1880,12 @@ print_cpus_readable(struct lscpu_desc *desc, int cols[], int ncols,
                struct libscols_line *line;
                int cpu = real_cpu_num(desc, i);
 
-               if (!mod->offline && desc->online && !is_cpu_online(desc, cpu))
-                       continue;
-               if (!mod->online && desc->online && is_cpu_online(desc, cpu))
-                       continue;
+               if (desc->online) {
+                       if (!mod->offline && !is_cpu_online(desc, cpu))
+                               continue;
+                       if (!mod->online && is_cpu_online(desc, cpu))
+                               continue;
+               }
                if (desc->present && !is_cpu_present(desc, cpu))
                        continue;
 
@@ -1809,7 +1922,8 @@ static void __attribute__ ((__format__(printf, 3, 4)))
                err(EXIT_FAILURE, _("failed to allocate output line"));
 
        /* description column */
-       scols_line_set_data(ln, 0, txt);
+       if (txt && scols_line_set_data(ln, 0, txt))
+               err(EXIT_FAILURE, _("failed to add output data"));
 
        /* data column */
        va_start(args, fmt);
@@ -1859,6 +1973,8 @@ static int get_cache_full_size(struct lscpu_desc *desc,
        /* Correction for CPU threads */
        if (desc->nthreads > desc->ncores)
                nshares /= (desc->nthreads / desc->ncores);
+       if (nshares < 1)
+               nshares = 1;
 
        *res = (desc->ncores / nshares) * ca->size;
        return 0;
@@ -1888,7 +2004,7 @@ print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod)
        }
 
        if (scols_table_new_column(tb, "field", 0, 0) == NULL ||
-           scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES) == NULL)
+           scols_table_new_column(tb, "data", 0, SCOLS_FL_NOEXTREMES | SCOLS_FL_WRAP) == NULL)
                err(EXIT_FAILURE, _("failed to initialize output column"));
 
        add_summary_s(tb, _("Architecture:"), desc->arch);
@@ -2051,7 +2167,7 @@ print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod)
                                tmp = size_to_human_string(
                                        SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE,
                                        sz);
-                       snprintf(buf, sizeof(buf), _("%s cache: "), ca->name);
+                       snprintf(buf, sizeof(buf), _("%s cache:"), ca->name);
                        add_summary_s(tb, buf, tmp);
                        free(tmp);
                }
@@ -2069,7 +2185,7 @@ print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod)
                                tmp = size_to_human_string(
                                        SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE,
                                        ca->size);
-                       snprintf(buf, sizeof(buf), _("%s cache: "), ca->name);
+                       snprintf(buf, sizeof(buf), _("%s cache:"), ca->name);
                        add_summary_s(tb, buf, tmp);
                        free(tmp);
                }
@@ -2086,6 +2202,13 @@ print_summary(struct lscpu_desc *desc, struct lscpu_modifier *mod)
                add_summary_n(tb, _("Physical cores/chip:"), desc->physcoresperchip);
        }
 
+       if (desc->vuls) {
+               for (i = 0; i < desc->nvuls; i++) {
+                       snprintf(buf, sizeof(buf), ("Vulnerability %s:"), desc->vuls[i].name);
+                       add_summary_s(tb, buf, desc->vuls[i].text);
+               }
+       }
+
        if (desc->flags)
                add_summary_s(tb, _("Flags:"), desc->flags);
 
@@ -2173,7 +2296,7 @@ int main(int argc, char *argv[])
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((c = getopt_long(argc, argv, "aBbC::ce::hJp::s:xyV", longopts, NULL)) != -1) {
 
@@ -2207,8 +2330,6 @@ int main(int argc, char *argv[])
                        }
                        mod->mode = OUTPUT_CACHES;
                        break;
-               case 'h':
-                       usage();
                case 'J':
                        mod->json = 1;
                        break;
@@ -2235,12 +2356,14 @@ int main(int argc, char *argv[])
                case 'y':
                        mod->physical = 1;
                        break;
-               case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
                case OPT_OUTPUT_ALL:
                        all = 1;
                        break;
+
+               case 'h':
+                       usage();
+               case 'V':
+                       print_version(EXIT_SUCCESS);
                default:
                        errtryhelp(EXIT_FAILURE);
                }
@@ -2332,6 +2455,9 @@ int main(int argc, char *argv[])
                        columns[ncolumns++] = COL_CACHE_WAYS;
                        columns[ncolumns++] = COL_CACHE_TYPE;
                        columns[ncolumns++] = COL_CACHE_LEVEL;
+                       columns[ncolumns++] = COL_CACHE_SETS;
+                       columns[ncolumns++] = COL_CACHE_PHYLINE;
+                       columns[ncolumns++] = COL_CACHE_COHERENCYSIZE;
                }
                print_caches_readable(desc, columns, ncolumns, mod);
                break;