Print this page
8225 passing invalid global pattern to coreadm wedges it nicely

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/coreadm/coreadm.c
          +++ new/usr/src/cmd/coreadm/coreadm.c
↓ open down ↓ 13 lines elided ↑ open up ↑
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
       24 + *
       25 + * Copyright 2017 RackTop Systems.
  24   26   */
  25   27  
  26   28  #include <stdio.h>
  27   29  #include <fcntl.h>
  28   30  #include <ctype.h>
  29   31  #include <string.h>
  30   32  #include <stdlib.h>
  31   33  #include <unistd.h>
  32   34  #include <errno.h>
  33   35  #include <limits.h>
↓ open down ↓ 36 lines elided ↑ open up ↑
  70   72  static  core_content_t  glob_content = CC_CONTENT_INVALID;
  71   73  static  char            *init_pattern;
  72   74  static  char            ipattern[PATH_MAX];
  73   75  static  core_content_t  init_content = CC_CONTENT_INVALID;
  74   76  static  char            *proc_pattern;
  75   77  static  size_t          proc_size;
  76   78  static  core_content_t  proc_content = CC_CONTENT_INVALID;
  77   79  
  78   80  static  int             report_settings(void);
  79   81  static  int             do_processes(int, char **);
  80      -static  int             do_modify(boolean_t);
       82 +static  int             do_modify(void);
  81   83  static  int             do_update(void);
  82   84  static  int             do_legacy(void);
  83   85  
  84   86  static scf_propvec_t prop_gpattern = { GLOBAL_PATTERN, NULL, SCF_TYPE_ASTRING };
  85   87  static scf_propvec_t prop_gcontent = { GLOBAL_CONTENT, NULL, SCF_TYPE_ASTRING };
  86   88  static scf_propvec_t prop_ipattern = { INIT_PATTERN, NULL, SCF_TYPE_ASTRING };
  87   89  static scf_propvec_t prop_icontent = { INIT_CONTENT, NULL, SCF_TYPE_ASTRING };
  88   90  static scf_propvec_t prop_option[] = {
  89      -    { GLOBAL_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_GLOBAL_PATH },
  90      -    { PROCESS_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_PROCESS_PATH },
  91      -    { GLOBAL_SETID_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_GLOBAL_SETID },
  92      -    { PROCESS_SETID_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_PROCESS_SETID },
  93      -    { GLOBAL_LOG_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_GLOBAL_LOG },
  94      -    { NULL }
       91 +        { GLOBAL_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_GLOBAL_PATH },
       92 +        { PROCESS_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_PROCESS_PATH },
       93 +        { GLOBAL_SETID_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_GLOBAL_SETID },
       94 +        { PROCESS_SETID_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL,
       95 +            CC_PROCESS_SETID },
       96 +        { GLOBAL_LOG_ENABLED, NULL, SCF_TYPE_BOOLEAN, NULL, CC_GLOBAL_LOG },
       97 +        { NULL }
  95   98  };
  96   99  #define MAX_PROPS       (4 + (sizeof (prop_option) / sizeof (scf_propvec_t)))
  97  100  
  98  101  static void
  99  102  usage(void)
 100  103  {
 101  104          (void) fprintf(stderr, gettext(
 102  105  "usage:\n"));
 103  106          (void) fprintf(stderr, gettext(
 104  107  "    %s [ -g pattern ] [ -i pattern ] [ -G content ] [ -I content ]\n"),
↓ open down ↓ 131 lines elided ↑ open up ↑
 236  239                      "%s: -[GIgied] and -[Pp] options are mutually exclusive\n"),
 237  240                      command);
 238  241                  usage();
 239  242          }
 240  243          if (modify && npids != 0) {
 241  244                  (void) fprintf(stderr, gettext(
 242  245                      "%s: -[GIgied] options cannot have a process-id list\n"),
 243  246                      command);
 244  247                  usage();
 245  248          }
      249 +        if (glob_pattern != NULL && glob_pattern[0] != '/') {
      250 +                (void) fprintf(stderr, gettext(
      251 +                    "%s: the -g option must specify an absolute path\n"),
      252 +                    command);
      253 +                usage();
      254 +        }
 246  255          if ((proc_pattern != NULL || proc_content != CC_CONTENT_INVALID) &&
 247  256              npids == 0) {
 248  257                  (void) sprintf(curpid, "%u", (uint_t)getppid());
 249  258                  npids = 1;
 250  259                  pidlist = &curpid_ptr;
 251  260          }
 252  261  
 253  262          if (legacy_update)
 254  263                  return (do_legacy());
 255  264          if (update)
 256  265                  return (do_update());
 257  266          if (modify)
 258      -                return (do_modify(B_FALSE));
      267 +                return (do_modify());
 259  268          if (npids != 0)
 260  269                  return (do_processes(npids, pidlist));
 261  270  
 262  271          return (report_settings());
 263  272  }
 264  273  
 265  274  static int
 266  275  report_settings(void)
 267  276  {
 268  277          char content_str[PRCONTENTBUFSZ];
↓ open down ↓ 106 lines elided ↑ open up ↑
 375  384  }
 376  385  
 377  386  static void
 378  387  addprop(scf_propvec_t *props, int size, int count, scf_propvec_t *pv, void *ptr)
 379  388  {
 380  389          assert(count + 1 < size);
 381  390          props[count] = *pv;
 382  391          props[count].pv_ptr = ptr;
 383  392  }
 384  393  
 385      -static boolean_t
 386      -is_online(const char *fmri)
      394 +static int
      395 +make_active(void)
 387  396  {
 388      -        char *state = smf_get_state(fmri);
 389      -        boolean_t result = state != NULL &&
 390      -            strcmp(state, SCF_STATE_STRING_ONLINE) == 0;
      397 +        char *state = smf_get_state(COREADM_INST_FMRI);
 391  398  
 392      -        free(state);
 393      -        return (result);
      399 +        switch (state == NULL ? -1 : smf_state_from_string(state)) {
      400 +        case SCF_STATE_ONLINE:
      401 +                if (smf_refresh_instance_synchronous(COREADM_INST_FMRI) != 0) {
      402 +                        (void) fprintf(stderr,
      403 +                            gettext("%s: Unable to refresh %s: %s\n"),
      404 +                            command, COREADM_INST_FMRI,
      405 +                            scf_strerror(scf_error()));
      406 +                        return (-1);
      407 +                }
      408 +                break;
      409 +        case SCF_STATE_DEGRADED:
      410 +        case SCF_STATE_MAINT:
      411 +                if (smf_restore_instance_synchronous(COREADM_INST_FMRI) != 0) {
      412 +                        (void) fprintf(stderr,
      413 +                            gettext("%s: Unable to clear %s: %s\n"),
      414 +                            command, COREADM_INST_FMRI,
      415 +                            scf_strerror(scf_error()));
      416 +                        return (-1);
      417 +                }
      418 +                break;
      419 +        case SCF_STATE_DISABLED:
      420 +                if (smf_enable_instance_synchronous(COREADM_INST_FMRI,
      421 +                    0) != 0) {
      422 +                        (void) fprintf(stderr,
      423 +                            gettext("%s: Unable to enable %s: %s\n"),
      424 +                            command, COREADM_INST_FMRI,
      425 +                            scf_strerror(scf_error()));
      426 +                        return (-1);
      427 +                }
      428 +                break;
      429 +        case SCF_STATE_OFFLINE:
      430 +        default:
      431 +                (void) fprintf(stderr,
      432 +                    gettext("%s: coreadm service not online\n"), command);
      433 +                return (-1);
      434 +        }
      435 +
      436 +        return (0);
 394  437  }
 395  438  
 396  439  /*
 397  440   * The user has specified the -g, -G, -i, -I, -d, or -e options to
 398  441   * modify the given configuration parameter. Perform the modification
 399  442   * in the smf repository and then perform a smf_refresh_instance which
 400  443   * will cause a coreadm -u to occur which will transfer ALL coreadm
 401  444   * configuration information from the repository to the kernel.
 402  445   */
 403  446  static int
 404      -do_modify(boolean_t method)
      447 +do_modify(void)
 405  448  {
 406  449          char gcontentstr[PRCONTENTBUFSZ];
 407  450          char icontentstr[PRCONTENTBUFSZ];
 408  451          scf_propvec_t *prop;
 409  452          scf_propvec_t properties[MAX_PROPS + 1];
 410  453          int count = 0;
 411  454  
 412      -        if (!method && !is_online(COREADM_INST_FMRI)) {
 413      -                (void) fprintf(stderr,
 414      -                    gettext("%s: coreadm service not online\n"), command);
 415      -                return (E_ERROR);
 416      -        }
 417      -
 418  455          if (glob_pattern != NULL)
 419  456                  addprop(properties, MAX_PROPS, count++, &prop_gpattern,
 420  457                      glob_pattern);
 421  458  
 422  459          if (glob_content != CC_CONTENT_INVALID) {
 423  460                  (void) proc_content2str(glob_content, gcontentstr,
 424  461                      sizeof (gcontentstr));
 425  462                  addprop(properties, MAX_PROPS, count++, &prop_gcontent,
 426  463                      gcontentstr);
 427  464          }
↓ open down ↓ 23 lines elided ↑ open up ↑
 451  488                              "%s: Unable to write property '%s': %s"), command,
 452  489                              prop->pv_prop, scf_strerror(scf_error()));
 453  490                  } else {
 454  491                          (void) fprintf(stderr, gettext(
 455  492                              "%s: Unable to write configuration: %s\n"),
 456  493                              command, scf_strerror(scf_error()));
 457  494                  }
 458  495                  return (E_ERROR);
 459  496          }
 460  497  
 461      -        if (smf_refresh_instance(COREADM_INST_FMRI) != 0) {
 462      -                (void) fprintf(stderr,
 463      -                    gettext("%s: Unable to refresh %s: %s\n"
 464      -                    "Configuration stored but not made active.\n"),
 465      -                    command, COREADM_INST_FMRI, scf_strerror(scf_error()));
      498 +        if (make_active() != 0) {
      499 +                (void) fprintf(stderr, gettext(
      500 +                    "Configuration stored but not made active.\n"));
 466  501                  return (E_ERROR);
 467  502          }
 468  503  
 469  504          return (E_SUCCESS);
 470  505  }
 471  506  
 472  507  static const char *
 473  508  write_kernel(void)
 474  509  {
 475  510          if (core_set_global_path(glob_pattern, strlen(glob_pattern) + 1) != 0)
↓ open down ↓ 139 lines elided ↑ open up ↑
 615  650          char            *gcstr, *icstr;
 616  651          scf_propvec_t   properties[MAX_PROPS + 1];
 617  652          scf_propvec_t   *prop;
 618  653          int             count = 0;
 619  654          const char      *errstr;
 620  655  
 621  656          if (read_legacy()) {
 622  657                  if ((errstr = write_kernel()) != NULL)
 623  658                          goto error;
 624  659  
 625      -                if (do_modify(B_TRUE) != 0 ||
      660 +                if (do_modify() != 0 ||
 626  661                      rename(PATH_CONFIG, PATH_CONFIG_OLD) != 0) {
 627  662                          (void) fprintf(stderr, gettext(
 628  663                              "%s: failed to import legacy configuration.\n"),
 629  664                              command);
 630  665                          return (SMF_EXIT_ERR_FATAL);
 631  666                  }
 632  667                  return (SMF_EXIT_OK);
 633  668          }
 634  669  
 635  670          addprop(properties, MAX_PROPS, count++, &prop_gpattern, &glob_pattern);
 636  671          addprop(properties, MAX_PROPS, count++, &prop_gcontent, &gcstr);
 637  672          addprop(properties, MAX_PROPS, count++, &prop_ipattern, &init_pattern);
 638  673          addprop(properties, MAX_PROPS, count++, &prop_icontent, &icstr);
 639  674          for (prop = prop_option; prop->pv_prop != NULL; prop++)
 640  675                  addprop(properties, MAX_PROPS, count++, prop, &options);
 641  676          properties[count].pv_prop = NULL;
 642  677  
 643  678          alloptions = CC_OPTIONS;
 644      -        if (scf_read_propvec(COREADM_INST_FMRI, CONFIG_PARAMS, B_TRUE,
      679 +        if (scf_read_propvec(COREADM_INST_FMRI, CONFIG_PARAMS, B_FALSE,
 645  680              properties, &prop) == SCF_FAILED) {
 646  681                  if (prop != NULL) {
 647  682                          (void) fprintf(stderr, gettext(
 648  683                              "%s: configuration property '%s' not found.\n"),
 649  684                              command, prop->pv_prop);
 650  685                  } else {
 651  686                          (void) fprintf(stderr, gettext(
 652  687                              "%s: unable to read configuration: %s\n"),
 653  688                              command, scf_strerror(scf_error()));
 654  689                  }
↓ open down ↓ 7 lines elided ↑ open up ↑
 662  697          scf_clean_propvec(properties);
 663  698          if (errstr == NULL)
 664  699                  return (SMF_EXIT_OK);
 665  700  
 666  701  error:
 667  702          if (errno == EPERM) {
 668  703                  (void) perm();
 669  704                  return (SMF_EXIT_ERR_PERM);
 670  705          }
 671  706          perror(errstr);
 672      -        return (SMF_EXIT_ERR_FATAL);
      707 +        return (errno == EINVAL ? SMF_EXIT_ERR_CONFIG : SMF_EXIT_ERR_FATAL);
 673  708  }
 674  709  
 675  710  static int do_legacy()
 676  711  {
 677  712          const char *errstr;
 678  713  
 679  714          if (read_legacy() && (errstr = write_kernel()) != NULL) {
 680  715                  if (errno == EPERM)
 681  716                          return (perm());
 682  717                  perror(errstr);
 683  718                  return (E_ERROR);
 684  719          }
 685  720  
 686  721          return (E_SUCCESS);
 687  722  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX