Print this page
10323 cpcgen_parse_model() gets value check wrong
   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright (c) 2018, Joyent, Inc.
  14  */
  15 
  16 /*
  17  * This file transforms the perfmon data files into C files and manual pages.
  18  */
  19 
  20 #include <stdio.h>
  21 #include <stdarg.h>
  22 #include <unistd.h>
  23 #include <err.h>
  24 #include <libgen.h>
  25 #include <libnvpair.h>
  26 #include <strings.h>
  27 #include <errno.h>
  28 #include <limits.h>
  29 #include <sys/mman.h>
  30 #include <sys/param.h>
  31 #include <assert.h>
  32 #include <ctype.h>
  33 #include <sys/types.h>


 300 cpcgen_parse_model(char *fsr, uint_t *family, uint_t *model)
 301 {
 302         const char *bstr = "GenuineIntel";
 303         const char *brand, *fam, *mod;
 304         char *last;
 305         long l;
 306 
 307         if ((brand = strtok_r(fsr, "-", &last)) == NULL ||
 308             (fam = strtok_r(NULL, "-", &last)) == NULL ||
 309             (mod = strtok_r(NULL, "-", &last)) == NULL) {
 310                 errx(EXIT_FAILURE, "failed to parse processor id \"%s\"", fsr);
 311         }
 312 
 313         if (strcmp(bstr, brand) != 0) {
 314                 errx(EXIT_FAILURE, "brand string \"%s\" did not match \"%s\"",
 315                     brand, bstr);
 316         }
 317 
 318         errno = 0;
 319         l = strtol(fam, &last, 16);
 320         if (errno != 0 || l < 0 || l > UINT_MAX || *last != '\0') {
 321                 errx(EXIT_FAILURE, "failed to parse family \"%s\"", fam);
 322         }
 323         *family = (uint_t)l;
 324 
 325         l = strtol(mod, &last, 16);
 326         if (errno != 0 || l < 0 || l > UINT_MAX || *last != '\0') {
 327                 errx(EXIT_FAILURE, "failed to parse model \"%s\"", mod);
 328         }
 329         *model = (uint_t)l;
 330 }
 331 
 332 static nvlist_t *
 333 cpcgen_read_datafile(const char *datadir, const char *file)
 334 {
 335         int fd;
 336         char *path;
 337         struct stat st;
 338         void *map;
 339         nvlist_t *nvl;
 340         nvlist_parse_json_error_t jerr;
 341 
 342         if (asprintf(&path, "%s/%s", datadir, file) == -1) {
 343                 err(EXIT_FAILURE, "failed to construct path to data file %s",
 344                     file);
 345         }
 346 


   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright (c) 2019, Joyent, Inc.
  14  */
  15 
  16 /*
  17  * This file transforms the perfmon data files into C files and manual pages.
  18  */
  19 
  20 #include <stdio.h>
  21 #include <stdarg.h>
  22 #include <unistd.h>
  23 #include <err.h>
  24 #include <libgen.h>
  25 #include <libnvpair.h>
  26 #include <strings.h>
  27 #include <errno.h>
  28 #include <limits.h>
  29 #include <sys/mman.h>
  30 #include <sys/param.h>
  31 #include <assert.h>
  32 #include <ctype.h>
  33 #include <sys/types.h>


 300 cpcgen_parse_model(char *fsr, uint_t *family, uint_t *model)
 301 {
 302         const char *bstr = "GenuineIntel";
 303         const char *brand, *fam, *mod;
 304         char *last;
 305         long l;
 306 
 307         if ((brand = strtok_r(fsr, "-", &last)) == NULL ||
 308             (fam = strtok_r(NULL, "-", &last)) == NULL ||
 309             (mod = strtok_r(NULL, "-", &last)) == NULL) {
 310                 errx(EXIT_FAILURE, "failed to parse processor id \"%s\"", fsr);
 311         }
 312 
 313         if (strcmp(bstr, brand) != 0) {
 314                 errx(EXIT_FAILURE, "brand string \"%s\" did not match \"%s\"",
 315                     brand, bstr);
 316         }
 317 
 318         errno = 0;
 319         l = strtol(fam, &last, 16);
 320         if (errno != 0 || l < 0 || l >= INT_MAX || *last != '\0') {
 321                 errx(EXIT_FAILURE, "failed to parse family \"%s\"", fam);
 322         }
 323         *family = (uint_t)l;
 324 
 325         l = strtol(mod, &last, 16);
 326         if (errno != 0 || l < 0 || l >= INT_MAX || *last != '\0') {
 327                 errx(EXIT_FAILURE, "failed to parse model \"%s\"", mod);
 328         }
 329         *model = (uint_t)l;
 330 }
 331 
 332 static nvlist_t *
 333 cpcgen_read_datafile(const char *datadir, const char *file)
 334 {
 335         int fd;
 336         char *path;
 337         struct stat st;
 338         void *map;
 339         nvlist_t *nvl;
 340         nvlist_parse_json_error_t jerr;
 341 
 342         if (asprintf(&path, "%s/%s", datadir, file) == -1) {
 343                 err(EXIT_FAILURE, "failed to construct path to data file %s",
 344                     file);
 345         }
 346