Print this page
4956 zonecfg won't use a valid pager

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/zonecfg/zonecfg.c
          +++ new/usr/src/cmd/zonecfg/zonecfg.c
↓ open down ↓ 14 lines elided ↑ open up ↑
  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  /*
  23   23   * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
       25 + * Copyright 2014 Gary Mills
  25   26   */
  26   27  
  27   28  /*
  28   29   * zonecfg is a lex/yacc based command interpreter used to manage zone
  29   30   * configurations.  The lexer (see zonecfg_lex.l) builds up tokens, which
  30   31   * the grammar (see zonecfg_grammar.y) builds up into commands, some of
  31   32   * which takes resources and/or properties as arguments.  See the block
  32   33   * comments near the end of zonecfg_grammar.y for how the data structures
  33   34   * which keep track of these resources and properties are built up.
  34   35   *
↓ open down ↓ 867 lines elided ↑ open up ↑
 902  903                              "specification."));
 903  904                  case CMD_END:
 904  905                          return (gettext("Ends resource/property "
 905  906                              "specification."));
 906  907          }
 907  908          /* NOTREACHED */
 908  909          return (NULL);
 909  910  }
 910  911  
 911  912  /*
      913 + * Return the input filename appended to each component of the path
      914 + * or the filename itself if it is absolute.
      915 + * Parameters: path string, file name, output string.
      916 + */
      917 +/* Copied almost verbatim from libtnfctl/prb_findexec.c */
      918 +static const char *
      919 +exec_cat(const char *s1, const char *s2, char *si)
      920 +{
      921 +        char               *s;
      922 +        /* Number of remaining characters in s */
      923 +        int                      cnt = PATH_MAX + 1;
      924 +
      925 +        s = si;
      926 +        while (*s1 && *s1 != ':') { /* Copy first component of path to si */
      927 +                if (cnt > 0) {
      928 +                        *s++ = *s1++;
      929 +                        cnt--;
      930 +                } else {
      931 +                        s1++;
      932 +                }
      933 +        }
      934 +        if (si != s && cnt > 0) { /* Add slash if s2 is not absolute */
      935 +                *s++ = '/';
      936 +                cnt--;
      937 +        }
      938 +        while (*s2 && cnt > 0) { /* Copy s2 to si */
      939 +                *s++ = *s2++;
      940 +                cnt--;
      941 +        }
      942 +        *s = '\0';  /* Terminate the output string */
      943 +        return (*s1 ? ++s1 : NULL);  /* Return next path component or NULL */
      944 +}
      945 +
      946 +/* Determine that a name exists in PATH */
      947 +/* Copied with changes from libtnfctl/prb_findexec.c */
      948 +static int
      949 +path_find(const char *name)
      950 +{
      951 +        const char       *pathstr;
      952 +        char            fname[PATH_MAX + 2];
      953 +        const char       *cp;
      954 +        struct stat      stat_buf;
      955 +
      956 +        if ((pathstr = getenv("PATH")) == NULL) {
      957 +                if (geteuid() == 0 || getuid() == 0)
      958 +                        pathstr = "/usr/sbin:/usr/bin";
      959 +                else
      960 +                        pathstr = "/usr/bin:";
      961 +        }
      962 +        cp = strchr(name, '/') ? (const char *) "" : pathstr;
      963 +
      964 +        do {
      965 +                cp = exec_cat(cp, name, fname);
      966 +                if (stat(fname, &stat_buf) != -1) {
      967 +                        /* successful find of the file */
      968 +                        return (0);
      969 +                }
      970 +        } while (cp != NULL);
      971 +
      972 +        return (-1);
      973 +}
      974 +
      975 +static FILE *
      976 +pager_open(void) {
      977 +        FILE *newfp;
      978 +        char *pager, *space;
      979 +
      980 +        pager = getenv("PAGER");
      981 +        if (pager == NULL || *pager == '\0')
      982 +                pager = PAGER;
      983 +
      984 +        space = strchr(pager, ' ');
      985 +        if (space)
      986 +                *space = '\0';
      987 +        if (path_find(pager) == 0) {
      988 +                if (space)
      989 +                        *space = ' ';
      990 +                if ((newfp = popen(pager, "w")) == NULL)
      991 +                        zerr(gettext("PAGER open failed (%s)."),
      992 +                            strerror(errno));
      993 +                return (newfp);
      994 +        } else {
      995 +                zerr(gettext("PAGER %s does not exist (%s)."),
      996 +                    pager, strerror(errno));
      997 +        }
      998 +        return (NULL);
      999 +}
     1000 +
     1001 +static void
     1002 +pager_close(FILE *fp) {
     1003 +        int status;
     1004 +
     1005 +        status = pclose(fp);
     1006 +        if (status == -1)
     1007 +                zerr(gettext("PAGER close failed (%s)."),
     1008 +                    strerror(errno));
     1009 +}
     1010 +
     1011 +/*
 912 1012   * Called with verbose TRUE when help is explicitly requested, FALSE for
 913 1013   * unexpected errors.
 914 1014   */
 915 1015  
 916 1016  void
 917 1017  usage(boolean_t verbose, uint_t flags)
 918 1018  {
 919 1019          FILE *fp = verbose ? stdout : stderr;
 920 1020          FILE *newfp;
 921 1021          boolean_t need_to_close = B_FALSE;
 922      -        char *pager, *space;
 923 1022          int i;
 924      -        struct stat statbuf;
 925 1023  
 926 1024          /* don't page error output */
 927 1025          if (verbose && interactive_mode) {
 928      -                if ((pager = getenv("PAGER")) == NULL)
 929      -                        pager = PAGER;
 930      -
 931      -                space = strchr(pager, ' ');
 932      -                if (space)
 933      -                        *space = '\0';
 934      -                if (stat(pager, &statbuf) == 0) {
 935      -                        if (space)
 936      -                                *space = ' ';
 937      -                        if ((newfp = popen(pager, "w")) != NULL) {
 938      -                                need_to_close = B_TRUE;
 939      -                                fp = newfp;
 940      -                        }
 941      -                } else {
 942      -                        zerr(gettext("PAGER %s does not exist (%s)."),
 943      -                            pager, strerror(errno));
     1026 +                if ((newfp = pager_open()) != NULL) {
     1027 +                        need_to_close = B_TRUE;
     1028 +                        fp = newfp;
 944 1029                  }
 945 1030          }
 946 1031  
 947 1032          if (flags & HELP_META) {
 948 1033                  (void) fprintf(fp, gettext("More help is available for the "
 949 1034                      "following:\n"));
 950 1035                  (void) fprintf(fp, "\n\tcommands ('%s commands')\n",
 951 1036                      cmd_to_str(CMD_HELP));
 952 1037                  (void) fprintf(fp, "\tsyntax ('%s syntax')\n",
 953 1038                      cmd_to_str(CMD_HELP));
↓ open down ↓ 313 lines elided ↑ open up ↑
1267 1352                      pt_to_str(PT_NCPUS), pt_to_str(PT_IMPORTANCE));
1268 1353                  (void) fprintf(fp, "\t%s\t%s\n", rt_to_str(RT_PCAP),
1269 1354                      pt_to_str(PT_NCPUS));
1270 1355                  (void) fprintf(fp, "\t%s\t%s, %s, %s\n", rt_to_str(RT_MCAP),
1271 1356                      pt_to_str(PT_PHYSICAL), pt_to_str(PT_SWAP),
1272 1357                      pt_to_str(PT_LOCKED));
1273 1358                  (void) fprintf(fp, "\t%s\t\t%s, %s\n", rt_to_str(RT_ADMIN),
1274 1359                      pt_to_str(PT_USER), pt_to_str(PT_AUTHS));
1275 1360          }
1276 1361          if (need_to_close)
1277      -                (void) pclose(fp);
     1362 +                (void) pager_close(fp);
1278 1363  }
1279 1364  
1280 1365  static void
1281 1366  zone_perror(char *prefix, int err, boolean_t set_saw)
1282 1367  {
1283 1368          zerr("%s: %s", prefix, zonecfg_strerror(err));
1284 1369          if (set_saw)
1285 1370                  saw_error = B_TRUE;
1286 1371  }
1287 1372  
↓ open down ↓ 4048 lines elided ↑ open up ↑
5336 5421          if (!output && cmd->cmd_prop_nv_pairs > 0)
5337 5422                  (void) printf(gettext("No such %s resource.\n"),
5338 5423                      rt_to_str(RT_ADMIN));
5339 5424  }
5340 5425  
5341 5426  void
5342 5427  info_func(cmd_t *cmd)
5343 5428  {
5344 5429          FILE *fp = stdout;
5345 5430          boolean_t need_to_close = B_FALSE;
5346      -        char *pager, *space;
5347 5431          int type;
5348 5432          int res1, res2;
5349 5433          uint64_t swap_limit;
5350 5434          uint64_t locked_limit;
5351      -        struct stat statbuf;
5352 5435  
5353 5436          assert(cmd != NULL);
5354 5437  
5355 5438          if (initialize(B_TRUE) != Z_OK)
5356 5439                  return;
5357 5440  
5358 5441          /* don't page error output */
5359 5442          if (interactive_mode) {
5360      -                if ((pager = getenv("PAGER")) == NULL)
5361      -                        pager = PAGER;
5362      -                space = strchr(pager, ' ');
5363      -                if (space)
5364      -                        *space = '\0';
5365      -                if (stat(pager, &statbuf) == 0) {
5366      -                        if (space)
5367      -                                *space = ' ';
5368      -                        if ((fp = popen(pager, "w")) != NULL)
5369      -                                need_to_close = B_TRUE;
5370      -                        else
5371      -                                fp = stdout;
5372      -                } else {
5373      -                        zerr(gettext("PAGER %s does not exist (%s)."),
5374      -                            pager, strerror(errno));
5375      -                }
     5443 +                if ((fp = pager_open()) != NULL)
     5444 +                        need_to_close = B_TRUE;
     5445 +                else
     5446 +                        fp = stdout;
5376 5447  
5377 5448                  setbuf(fp, NULL);
5378 5449          }
5379 5450  
5380 5451          if (!global_scope) {
5381 5452                  switch (resource_scope) {
5382 5453                  case RT_FS:
5383 5454                          output_fs(fp, &in_progress_fstab);
5384 5455                          break;
5385 5456                  case RT_NET:
↓ open down ↓ 169 lines elided ↑ open up ↑
5555 5626          case RT_FS_ALLOWED:
5556 5627                  info_fs_allowed(handle, fp);
5557 5628                  break;
5558 5629          default:
5559 5630                  zone_perror(rt_to_str(cmd->cmd_res_type), Z_NO_RESOURCE_TYPE,
5560 5631                      B_TRUE);
5561 5632          }
5562 5633  
5563 5634  cleanup:
5564 5635          if (need_to_close)
5565      -                (void) pclose(fp);
     5636 +                (void) pager_close(fp);
5566 5637  }
5567 5638  
5568 5639  /*
5569 5640   * Helper function for verify-- checks that a required string property
5570 5641   * exists.
5571 5642   */
5572 5643  static void
5573 5644  check_reqd_prop(char *attr, int rt, int pt, int *ret_val)
5574 5645  {
5575 5646          if (strlen(attr) == 0) {
↓ open down ↓ 1654 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX