Print this page
XXX zone aliases in "prstat -ZA"

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/prstat/prstat.c
          +++ new/usr/src/cmd/prstat/prstat.c
↓ open down ↓ 16 lines elided ↑ open up ↑
  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 2009 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   *
  26   26   * Portions Copyright 2009 Chad Mynhier
  27      - * Copyright 2012 Joyent, Inc.  All rights reserved.
       27 + * Copyright 2013 Joyent, Inc.  All rights reserved.
  28   28   */
  29   29  
  30   30  #include <sys/types.h>
  31   31  #include <sys/resource.h>
  32   32  #include <sys/loadavg.h>
  33   33  #include <sys/time.h>
  34   34  #include <sys/pset.h>
  35   35  #include <sys/vm_usage.h>
       36 +#include <sys/queue.h>
  36   37  #include <zone.h>
  37   38  #include <libzonecfg.h>
  38   39  
  39   40  #include <stdio.h>
  40   41  #include <stdlib.h>
  41   42  #include <unistd.h>
  42   43  #include <dirent.h>
  43   44  #include <string.h>
       45 +#include <strings.h>
  44   46  #include <errno.h>
  45   47  #include <poll.h>
  46   48  #include <ctype.h>
  47   49  #include <fcntl.h>
  48   50  #include <limits.h>
  49   51  #include <signal.h>
  50   52  #include <time.h>
  51   53  #include <project.h>
  52   54  
  53   55  #include <langinfo.h>
  54   56  #include <libintl.h>
  55   57  #include <locale.h>
  56   58  
       59 +#include <sasl/saslutil.h>
       60 +
  57   61  #include "prstat.h"
  58   62  #include "prutil.h"
  59   63  #include "prtable.h"
  60   64  #include "prsort.h"
  61   65  #include "prfile.h"
  62   66  
  63   67  /*
  64   68   * x86 <sys/regs.h> ERR conflicts with <curses.h> ERR.  For the purposes
  65   69   * of this file, we care about the curses.h ERR so include that last.
  66   70   */
↓ open down ↓ 44 lines elided ↑ open up ↑
 111  115  #define USAGE_LINE \
 112  116  "%6d %-8s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s "\
 113  117  "%3.3s %-.12s/%d"
 114  118  #define USER_LINE \
 115  119  "%6d %-8s %5.5s %5.5s   %3.3s%% %9s %3.3s%%"
 116  120  #define TASK_LINE \
 117  121  "%6d %8d %5s %5s   %3.3s%% %9s %3.3s%% %28s"
 118  122  #define PROJECT_LINE \
 119  123  "%6d %8d %5s %5s   %3.3s%% %9s %3.3s%% %28s"
 120  124  #define ZONE_LINE \
 121      -"%6d %8d %5s %5s   %3.3s%% %9s %3.3s%% %28s"
      125 +"%6d %8d %5s %5s   %3.3s%% %9s %3.3s%% %-28s"
 122  126  
 123  127  #define TOTAL_LINE \
 124  128  "Total: %d processes, %d lwps, load averages: %3.2f, %3.2f, %3.2f"
 125  129  
 126  130  /* global variables */
 127  131  
 128  132  static char     *t_ulon;                        /* termcap: start underline */
 129  133  static char     *t_uloff;                       /* termcap: end underline */
 130  134  static char     *t_up;                          /* termcap: cursor 1 line up */
 131  135  static char     *t_eol;                         /* termcap: clear end of line */
↓ open down ↓ 26 lines elided ↑ open up ↑
 158  162  static list_t   projects;                       /* list of projects */
 159  163  static list_t   zones;                          /* list of zones */
 160  164  static list_t   lgroups;                        /* list of lgroups */
 161  165  
 162  166  static volatile uint_t sigwinch = 0;
 163  167  static volatile uint_t sigtstp = 0;
 164  168  static volatile uint_t sigterm = 0;
 165  169  
 166  170  static long pagesize;
 167  171  
      172 +/*
      173 + * Cache for mapping 'alias' names (an attribute of the zone configuration) to
      174 + * zone names, for use in -Z output.
      175 + */
      176 +static uint_t enable_alias_cache = FALSE;
      177 +static SLIST_HEAD(alias_cache_head, alias_cache_entry) alias_cache =
      178 +    SLIST_HEAD_INITIALIZER(alias_cache);
      179 +
      180 +typedef struct alias_cache_entry {
      181 +        char *ace_zonename;
      182 +        char *ace_alias;
      183 +
      184 +        SLIST_ENTRY(alias_cache_entry) ace_linkage;
      185 +} alias_cache_entry_t;
      186 +
      187 +static const char *lookup_zone_alias(const char *);
      188 +
 168  189  /* default settings */
 169  190  
 170  191  static optdesc_t opts = {
 171  192          5,                      /* interval between updates, seconds */
 172  193          15,                     /* number of lines in top part */
 173  194          5,                      /* number of lines in bottom part */
 174  195          -1,                     /* number of iterations; infinitely */
 175  196          OPT_PSINFO | OPT_FULLSCREEN | OPT_USEHOME | OPT_TERMCAP,
 176  197          -1                      /* sort in decreasing order */
 177  198  };
↓ open down ↓ 298 lines elided ↑ open up ↑
 476  497                                  (void) printf(PROJECT_LINE, (int)id->id_projid,
 477  498                                      id->id_nproc, psize, prssize, pmem, ptime,
 478  499                                      pcpu, projname);
 479  500                          else if (list->l_type == LT_TASKS)
 480  501                                  (void) printf(TASK_LINE, (int)id->id_taskid,
 481  502                                      id->id_nproc, psize, prssize, pmem, ptime,
 482  503                                      pcpu, projname);
 483  504                          else if (list->l_type == LT_ZONES)
 484  505                                  (void) printf(ZONE_LINE, (int)id->id_zoneid,
 485  506                                      id->id_nproc, psize, prssize, pmem, ptime,
 486      -                                    pcpu, zonename);
      507 +                                    pcpu, lookup_zone_alias(zonename));
 487  508                          else
 488  509                                  (void) printf(USER_LINE, id->id_nproc, pname,
 489  510                                      psize, prssize, pmem, ptime, pcpu);
 490  511                          (void) putp(t_eol);
 491  512                          (void) putchar('\n');
 492  513                          break;
 493  514                  case LT_LWPS:
 494  515                          lwp = list->l_ptrs[i];
 495  516                          if (opts.o_outpmode & OPT_LWPS)
 496  517                                  lwpid = lwp->li_info.pr_lwp.pr_lwpid;
↓ open down ↓ 331 lines elided ↑ open up ↑
 828  849                  lwp->li_lat = (TIME2NSEC(usage->pr_wtime) -
 829  850                      TIME2NSEC(lwp->li_usage.pr_wtime))/period;
 830  851                  lwp->li_vcx = usage->pr_vctx - lwp->li_usage.pr_vctx;
 831  852                  lwp->li_icx = usage->pr_ictx - lwp->li_usage.pr_ictx;
 832  853                  lwp->li_scl = usage->pr_sysc - lwp->li_usage.pr_sysc;
 833  854                  lwp->li_sig = usage->pr_sigs - lwp->li_usage.pr_sigs;
 834  855                  (void) memcpy(&lwp->li_usage, usage, sizeof (prusage_t));
 835  856          }
 836  857  }
 837  858  
      859 +static alias_cache_entry_t *
      860 +fetch_zone_alias(const char *zonen)
      861 +{
      862 +        zone_dochandle_t zch;
      863 +        alias_cache_entry_t *ace;
      864 +        struct zone_attrtab attrtab;
      865 +        char out[4 * sizeof (attrtab.zone_attr_value)];
      866 +        unsigned outlen = 0;
      867 +        int err;
      868 +
      869 +        /*
      870 +         * Get handle to zone configuration:
      871 +         */
      872 +        if ((zch = zonecfg_init_handle()) == NULL)
      873 +                return (NULL);
      874 +        if (zonecfg_get_handle(zonen, zch) != Z_OK) {
      875 +                zonecfg_fini_handle(zch);
      876 +                return (NULL);
      877 +        }
      878 +
      879 +        /*
      880 +         * Lookup the 'alias' attribute:
      881 +         */
      882 +        bzero(&attrtab, sizeof (attrtab));
      883 +        (void) strlcpy(attrtab.zone_attr_name, "alias",
      884 +            sizeof (attrtab.zone_attr_name));
      885 +        if ((zonecfg_lookup_attr(zch, &attrtab)) != Z_OK) {
      886 +                zonecfg_fini_handle(zch);
      887 +                return (NULL);
      888 +        }
      889 +        zonecfg_fini_handle(zch);
      890 +
      891 +        /*
      892 +         * Attempt to base64 decode the 'alias' attribute value:
      893 +         */
      894 +        if ((err = sasl_decode64(attrtab.zone_attr_value,
      895 +            strlen(attrtab.zone_attr_value), out, sizeof (out), &outlen)) !=
      896 +            SASL_OK) {
      897 +                if (err == SASL_BADPROT) {
      898 +                        /*
      899 +                         * If it was invalid base64, hand the raw value on:
      900 +                         */
      901 +                        (void) strlcpy(out, attrtab.zone_attr_value,
      902 +                            sizeof (out));
      903 +                } else {
      904 +                        /*
      905 +                         * If we failed for any other reason, error out:
      906 +                         */
      907 +                        return (NULL);
      908 +                }
      909 +        }
      910 +
      911 +        /*
      912 +         * Store it in the cache:
      913 +         */
      914 +        if ((ace = calloc(1, sizeof (*ace))) == NULL) {
      915 +                return (NULL);
      916 +        }
      917 +        if ((ace->ace_zonename = strdup(zonen)) == NULL) {
      918 +                free(ace);
      919 +                return (NULL);
      920 +        }
      921 +        if ((ace->ace_alias = strdup(out)) == NULL) {
      922 +                free(ace->ace_zonename);
      923 +                free(ace);
      924 +                return (NULL);
      925 +        }
      926 +
      927 +        SLIST_INSERT_HEAD(&alias_cache, ace, ace_linkage);
      928 +
      929 +        return (ace);
      930 +}
      931 +
      932 +static const char *
      933 +lookup_zone_alias(const char *zonen)
      934 +{
      935 +        alias_cache_entry_t *ace;
      936 +
      937 +        if (enable_alias_cache != TRUE)
      938 +                return (zonen);
      939 +
      940 +        /*
      941 +         * Check if we have a value cached:
      942 +         */
      943 +        SLIST_FOREACH(ace, &alias_cache, ace_linkage) {
      944 +                if (strcmp(ace->ace_zonename, zonen) == 0)
      945 +                        return (ace->ace_alias);
      946 +        }
      947 +
      948 +        /*
      949 +         * If not, try and fetch it from libzonecfg:
      950 +         */
      951 +        if ((ace = fetch_zone_alias(zonen)) != NULL)
      952 +                return (ace->ace_alias);
      953 +
      954 +        return (zonen);
      955 +}
      956 +
 838  957  static int
 839  958  read_procfile(fd_t **fd, char *pidstr, char *file, void *buf, size_t bufsize)
 840  959  {
 841  960          char procfile[MAX_PROCFS_PATH];
 842  961  
 843  962          (void) snprintf(procfile, MAX_PROCFS_PATH,
 844  963              "/proc/%s/%s", pidstr, file);
 845  964          if ((*fd = fd_open(procfile, O_RDONLY, *fd)) == NULL)
 846  965                  return (1);
 847  966          if (pread(fd_getfd(*fd), buf, bufsize, 0) != bufsize) {
↓ open down ↓ 521 lines elided ↑ open up ↑
1369 1488  
1370 1489          (void) setlocale(LC_ALL, "");
1371 1490          (void) textdomain(TEXT_DOMAIN);
1372 1491          Progname(argv[0]);
1373 1492          lwpid_init();
1374 1493          fd_init(Setrlimit());
1375 1494  
1376 1495          pagesize = sysconf(_SC_PAGESIZE);
1377 1496  
1378 1497          while ((opt = getopt(argc, argv,
1379      -            "vVcd:HmarRLtu:U:n:p:C:P:h:s:S:j:k:TJz:Z")) != (int)EOF) {
     1498 +            "vVcd:HmAarRLtu:U:n:p:C:P:h:s:S:j:k:TJz:Z")) != (int)EOF) {
1380 1499                  switch (opt) {
     1500 +                case 'A':
     1501 +                        enable_alias_cache = TRUE;
     1502 +                        break;
1381 1503                  case 'r':
1382 1504                          opts.o_outpmode |= OPT_NORESOLVE;
1383 1505                          break;
1384 1506                  case 'R':
1385 1507                          opts.o_outpmode |= OPT_REALTIME;
1386 1508                          break;
1387 1509                  case 'c':
1388 1510                          opts.o_outpmode &= ~OPT_TERMCAP;
1389 1511                          opts.o_outpmode &= ~OPT_FULLSCREEN;
1390 1512                          break;
↓ open down ↓ 284 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX