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 ↓ 866 lines elided ↑ open up ↑
 901  902                          return (gettext("Cancels resource/property "
 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  
      912 +/* Copied almost verbatim from libtnfctl/prb_findexec.c */
      913 +static const char *
      914 +exec_cat(const char *s1, const char *s2, char *si)
      915 +{
      916 +        char               *s;
      917 +        /* number of characters in s2 */
      918 +        int                      cnt = PATH_MAX + 1;
      919 +
      920 +        s = si;
      921 +        while (*s1 && *s1 != ':') {
      922 +                if (cnt > 0) {
      923 +                        *s++ = *s1++;
      924 +                        cnt--;
      925 +                } else
      926 +                        s1++;
      927 +        }
      928 +        if (si != s && cnt > 0) {
      929 +                *s++ = '/';
      930 +                cnt--;
      931 +        }
      932 +        while (*s2 && cnt > 0) {
      933 +                *s++ = *s2++;
      934 +                cnt--;
      935 +        }
      936 +        *s = '\0';
      937 +        return (*s1 ? ++s1 : NULL);
      938 +}
      939 +
      940 +/* Determine that a name exists in PATH */
      941 +/* Copied with changes from libtnfctl/prb_findexec.c */
      942 +static int
      943 +path_find(const char *name, char *ret_path)
      944 +{
      945 +        const char       *pathstr;
      946 +        char            fname[PATH_MAX + 2];
      947 +        const char       *cp;
      948 +        struct stat      stat_buf;
      949 +
      950 +        if (*name == '\0') {
      951 +                return (-1);
      952 +        }
      953 +        if ((pathstr = getenv("PATH")) == NULL) {
      954 +                if (geteuid() == 0 || getuid() == 0)
      955 +                        pathstr = "/usr/sbin:/usr/bin";
      956 +                else
      957 +                        pathstr = "/usr/bin:";
      958 +        }
      959 +        cp = strchr(name, '/') ? (const char *) "" : pathstr;
      960 +
      961 +        do {
      962 +                cp = exec_cat(cp, name, fname);
      963 +                if (stat(fname, &stat_buf) != -1) {
      964 +                        /* successful find of the file */
      965 +                        if (ret_path != NULL)
      966 +                                (void) strncpy(ret_path, fname, PATH_MAX + 2);
      967 +                        return (0);
      968 +                }
      969 +        } while (cp != NULL);
      970 +
      971 +        return (-1);
      972 +}
      973 +
      974 +static FILE *
      975 +pager_open(void) {
      976 +        FILE *newfp;
      977 +        char *pager, *space;
      978 +
      979 +        if ((pager = getenv("PAGER")) == NULL)
      980 +                pager = PAGER;
      981 +
      982 +        space = strchr(pager, ' ');
      983 +        if (space)
      984 +                *space = '\0';
      985 +        if (path_find(pager, NULL) == 0) {
      986 +                if (space)
      987 +                        *space = ' ';
      988 +                if ((newfp = popen(pager, "w")) == NULL)
      989 +                        zerr(gettext("PAGER open failed (%s)."),
      990 +                            strerror(errno));
      991 +                return (newfp);
      992 +        } else {
      993 +                zerr(gettext("PAGER %s does not exist (%s)."),
      994 +                    pager, strerror(errno));
      995 +        }
      996 +        return (NULL);
      997 +}
      998 +
      999 +static void
     1000 +pager_close(FILE *fp) {
     1001 +        int status;
     1002 +
     1003 +        status = pclose(fp);
     1004 +        if (status == -1)
     1005 +                zerr(gettext("PAGER close failed (%s)."),
     1006 +                    strerror(errno));
     1007 +}
     1008 +
 911 1009  /*
 912 1010   * Called with verbose TRUE when help is explicitly requested, FALSE for
 913 1011   * unexpected errors.
 914 1012   */
 915 1013  
 916 1014  void
 917 1015  usage(boolean_t verbose, uint_t flags)
 918 1016  {
 919 1017          FILE *fp = verbose ? stdout : stderr;
 920 1018          FILE *newfp;
 921 1019          boolean_t need_to_close = B_FALSE;
 922      -        char *pager, *space;
 923 1020          int i;
 924      -        struct stat statbuf;
 925 1021  
 926 1022          /* don't page error output */
 927 1023          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));
     1024 +                if ((newfp = pager_open()) != NULL) {
     1025 +                        need_to_close = B_TRUE;
     1026 +                        fp = newfp;
 944 1027                  }
 945 1028          }
 946 1029  
 947 1030          if (flags & HELP_META) {
 948 1031                  (void) fprintf(fp, gettext("More help is available for the "
 949 1032                      "following:\n"));
 950 1033                  (void) fprintf(fp, "\n\tcommands ('%s commands')\n",
 951 1034                      cmd_to_str(CMD_HELP));
 952 1035                  (void) fprintf(fp, "\tsyntax ('%s syntax')\n",
 953 1036                      cmd_to_str(CMD_HELP));
↓ open down ↓ 313 lines elided ↑ open up ↑
1267 1350                      pt_to_str(PT_NCPUS), pt_to_str(PT_IMPORTANCE));
1268 1351                  (void) fprintf(fp, "\t%s\t%s\n", rt_to_str(RT_PCAP),
1269 1352                      pt_to_str(PT_NCPUS));
1270 1353                  (void) fprintf(fp, "\t%s\t%s, %s, %s\n", rt_to_str(RT_MCAP),
1271 1354                      pt_to_str(PT_PHYSICAL), pt_to_str(PT_SWAP),
1272 1355                      pt_to_str(PT_LOCKED));
1273 1356                  (void) fprintf(fp, "\t%s\t\t%s, %s\n", rt_to_str(RT_ADMIN),
1274 1357                      pt_to_str(PT_USER), pt_to_str(PT_AUTHS));
1275 1358          }
1276 1359          if (need_to_close)
1277      -                (void) pclose(fp);
     1360 +                (void) pager_close(fp);
1278 1361  }
1279 1362  
1280 1363  static void
1281 1364  zone_perror(char *prefix, int err, boolean_t set_saw)
1282 1365  {
1283 1366          zerr("%s: %s", prefix, zonecfg_strerror(err));
1284 1367          if (set_saw)
1285 1368                  saw_error = B_TRUE;
1286 1369  }
1287 1370  
↓ open down ↓ 4048 lines elided ↑ open up ↑
5336 5419          if (!output && cmd->cmd_prop_nv_pairs > 0)
5337 5420                  (void) printf(gettext("No such %s resource.\n"),
5338 5421                      rt_to_str(RT_ADMIN));
5339 5422  }
5340 5423  
5341 5424  void
5342 5425  info_func(cmd_t *cmd)
5343 5426  {
5344 5427          FILE *fp = stdout;
5345 5428          boolean_t need_to_close = B_FALSE;
5346      -        char *pager, *space;
5347 5429          int type;
5348 5430          int res1, res2;
5349 5431          uint64_t swap_limit;
5350 5432          uint64_t locked_limit;
5351      -        struct stat statbuf;
5352 5433  
5353 5434          assert(cmd != NULL);
5354 5435  
5355 5436          if (initialize(B_TRUE) != Z_OK)
5356 5437                  return;
5357 5438  
5358 5439          /* don't page error output */
5359 5440          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      -                }
     5441 +                if ((fp = pager_open()) != NULL)
     5442 +                        need_to_close = B_TRUE;
     5443 +                else
     5444 +                        fp = stdout;
5376 5445  
5377 5446                  setbuf(fp, NULL);
5378 5447          }
5379 5448  
5380 5449          if (!global_scope) {
5381 5450                  switch (resource_scope) {
5382 5451                  case RT_FS:
5383 5452                          output_fs(fp, &in_progress_fstab);
5384 5453                          break;
5385 5454                  case RT_NET:
↓ open down ↓ 169 lines elided ↑ open up ↑
5555 5624          case RT_FS_ALLOWED:
5556 5625                  info_fs_allowed(handle, fp);
5557 5626                  break;
5558 5627          default:
5559 5628                  zone_perror(rt_to_str(cmd->cmd_res_type), Z_NO_RESOURCE_TYPE,
5560 5629                      B_TRUE);
5561 5630          }
5562 5631  
5563 5632  cleanup:
5564 5633          if (need_to_close)
5565      -                (void) pclose(fp);
     5634 +                (void) pager_close(fp);
5566 5635  }
5567 5636  
5568 5637  /*
5569 5638   * Helper function for verify-- checks that a required string property
5570 5639   * exists.
5571 5640   */
5572 5641  static void
5573 5642  check_reqd_prop(char *attr, int rt, int pt, int *ret_val)
5574 5643  {
5575 5644          if (strlen(attr) == 0) {
↓ open down ↓ 1654 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX