Print this page
2989 Eliminate use of LOGNAME_MAX in ON
1166 useradd have warning with name more 8 chars


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*


  23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  29  */
  30 
  31 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  32 /*        All Rights Reserved   */
  33 
  34 /*
  35  * ps -- print things about processes.
  36  */
  37 #include <stdio.h>
  38 #include <ctype.h>
  39 #include <string.h>
  40 #include <errno.h>
  41 #include <fcntl.h>
  42 #include <pwd.h>


  59 #include <libw.h>
  60 #include <stdarg.h>
  61 #include <sys/proc.h>
  62 #include <sys/pset.h>
  63 #include <project.h>
  64 #include <zone.h>
  65 
  66 #define min(a, b)       ((a) > (b) ? (b) : (a))
  67 #define max(a, b)       ((a) < (b) ? (b) : (a))
  68 
  69 #define NTTYS   20      /* initial size of table for -t option  */
  70 #define SIZ     30      /* initial size of tables for -p, -s, -g, -h and -z */
  71 
  72 /*
  73  * Size of buffer holding args for t, p, s, g, u, U, G, z options.
  74  * Set to ZONENAME_MAX, the minimum value needed to allow any
  75  * zone to be specified.
  76  */
  77 #define ARGSIZ ZONENAME_MAX
  78 
  79 #define MAXUGNAME 10    /* max chars in a user/group name or printed u/g id */

  80 
  81 /* Structure for storing user or group info */
  82 struct ugdata {
  83         id_t    id;                     /* numeric user-id or group-id */
  84         char    name[MAXUGNAME+1];      /* user/group name, null terminated */
  85 };
  86 
  87 struct ughead {
  88         size_t  size;           /* number of ugdata structs allocated */
  89         size_t  nent;           /* number of active entries */
  90         struct ugdata *ent;     /* pointer to array of actual entries */
  91 };
  92 
  93 enum fname {    /* enumeration of field names */
  94         F_USER,         /* effective user of the process */
  95         F_RUSER,        /* real user of the process */
  96         F_GROUP,        /* effective group of the process */
  97         F_RGROUP,       /* real group of the process */
  98         F_UID,          /* numeric effective uid of the process */
  99         F_RUID,         /* numeric real uid of the process */


 207         { "pset",       "PSET",         3,      3       },
 208         { "zone",       "ZONE",         8,      8       },
 209         { "zoneid",     "ZONEID",       5,      5       },
 210         { "ctid",       "CTID",         5,      5       },
 211         { "lgrp",       "LGRP",         4,      2       },
 212         { "dmodel",     "DMODEL",       6,      6       },
 213 };
 214 
 215 #define NFIELDS (sizeof (fname) / sizeof (fname[0]))
 216 
 217 static  int     retcode = 1;
 218 static  int     lflg;
 219 static  int     Aflg;
 220 static  int     uflg;
 221 static  int     Uflg;
 222 static  int     Gflg;
 223 static  int     aflg;
 224 static  int     dflg;
 225 static  int     Lflg;
 226 static  int     Pflg;

 227 static  int     yflg;
 228 static  int     pflg;
 229 static  int     fflg;
 230 static  int     cflg;
 231 static  int     jflg;
 232 static  int     gflg;
 233 static  int     sflg;
 234 static  int     tflg;
 235 static  int     zflg;
 236 static  int     Zflg;
 237 static  int     hflg;
 238 static  int     Hflg;
 239 static  uid_t   tuid = (uid_t)-1;
 240 static  int     errflg;
 241 
 242 static  int     ndev;           /* number of devices */
 243 static  int     maxdev;         /* number of devl structures allocated */
 244 
 245 #define DNINCR  100
 246 #define DNSIZE  14


 407         pidwidth = 1;
 408         while ((id /= 10) > 0)
 409                 ++pidwidth;
 410         pidwidth = pidwidth < 5 ? 5 : pidwidth;
 411 
 412         fname[F_PID].width = fname[F_PPID].width = pidwidth;
 413         fname[F_PGID].width = fname[F_SID].width = pidwidth;
 414 
 415         /*
 416          * TRANSLATION_NOTE
 417          * Specify the printf format with width and precision for
 418          * the STIME field.
 419          */
 420         len = snprintf(loc_stime_str, sizeof (loc_stime_str),
 421             dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
 422         if (len >= sizeof (loc_stime_str))
 423                 len = sizeof (loc_stime_str) - 1;
 424 
 425         fname[F_STIME].width = fname[F_STIME].minwidth = len;
 426 
 427         while ((c = getopt(argc, argv, "jlfceAadLPyZHh:t:p:g:u:U:G:n:s:o:z:"))
 428             != EOF)
 429                 switch (c) {
 430                 case 'H':               /* Show home lgroups */
 431                         Hflg++;
 432                         break;
 433                 case 'h':
 434                         /*
 435                          * Show processes/threads with given home lgroups
 436                          */
 437                         hflg++;
 438                         p1 = optarg;
 439                         do {
 440                                 int id;
 441 
 442                                 /*
 443                                  * Get all IDs in the list, verify for
 444                                  * correctness and place in lgrps array.
 445                                  */
 446                                 parg = getarg(&p1);
 447                                 /* Convert string to integer */


 501                         Aflg++;
 502                         tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
 503                         zflg = hflg = 0;
 504                         break;
 505                 case 'a':
 506                         /*
 507                          * Same as 'e' except no session group leaders
 508                          * and no non-terminal processes.
 509                          */
 510                         aflg++;
 511                         break;
 512                 case 'd':       /* same as e except no session leaders */
 513                         dflg++;
 514                         break;
 515                 case 'L':       /* show lwps */
 516                         Lflg++;
 517                         break;
 518                 case 'P':       /* show bound processor */
 519                         Pflg++;
 520                         break;



 521                 case 'y':       /* omit F & ADDR, report RSS & SZ in Kby */
 522                         yflg++;
 523                         break;
 524                 case 'n':       /* no longer needed; retain as no-op */
 525                         (void) fprintf(stderr,
 526                             gettext("ps: warning: -n option ignored\n"));
 527                         break;
 528                 case 't':               /* terminals */
 529 #define TSZ     30
 530                         tflg++;
 531                         p1 = optarg;
 532                         do {
 533                                 char nambuf[TSZ+6];     /* for "/dev/" + '\0' */
 534                                 struct stat64 s;
 535                                 parg = getarg(&p1);
 536                                 p = Realloc(NULL, TSZ+1);       /* for '\0' */
 537                                 /* zero the buffer before using it */
 538                                 p[0] = '\0';
 539                                 size = TSZ;
 540                                 if (isdigit(*parg)) {


1040         }
1041         return (0);
1042 }
1043 
1044 static int
1045 field_cmp(const void *l, const void *r)
1046 {
1047         struct def_field *lhs = *((struct def_field **)l);
1048         struct def_field *rhs = *((struct def_field **)r);
1049 
1050         return (strcmp(lhs->fname, rhs->fname));
1051 }
1052 
1053 static void
1054 usage(void)             /* print usage message and quit */
1055 {
1056         struct def_field *df, *sorted[NFIELDS];
1057         int pos = 80, i = 0;
1058 
1059         static char usage1[] =
1060             "ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]";
1061         static char usage2[] =
1062             "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
1063         static char usage3[] =
1064             "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ]";
1065         static char usage4[] =
1066             "\t[ -z zonelist ] [-h lgrplist]";
1067         static char usage5[] =
1068             "  'format' is one or more of:";
1069 
1070         (void) fprintf(stderr,
1071             gettext("usage: %s\n%s\n%s\n%s\n%s"),
1072             gettext(usage1), gettext(usage2), gettext(usage3),
1073             gettext(usage4), gettext(usage5));
1074 
1075         /*
1076          * Now print out the possible output formats such that they neatly fit
1077          * into eighty columns.  Note that the fact that we are determining
1078          * this output programmatically means that a gettext() is impossible --
1079          * but it would be a mistake to localize the output formats anyway as
1080          * they are tokens for input, not output themselves.


1380                 pr_fields(psinfo, ttyp, print_field);
1381                 return;
1382         }
1383 
1384         /*
1385          * All fields before 'PID' are printed with a trailing space as a
1386          * separator, rather than keeping track of which column is first.  All
1387          * other fields are printed with a leading space.
1388          */
1389         if (lflg) {
1390                 if (!yflg)
1391                         (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
1392                 (void) printf("%c ", psinfo->pr_lwp.pr_sname);       /* S */
1393         }
1394 
1395         if (Zflg) {                                             /* ZONE */
1396                 if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1397                     sizeof (zonename)) < 0) {
1398                         (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
1399                 } else {



1400                         (void) printf("%8.8s ", zonename);
1401                 }
1402         }
1403 
1404         if (fflg) {                                             /* UID */
1405                 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
1406                         (void) printf("%8.8s ", pwd->pw_name);

1407                 else


1408                         (void) printf(" %7.7u ", psinfo->pr_euid);

1409         } else if (lflg) {
1410                 (void) printf("%6u ", psinfo->pr_euid);
1411         }
1412         (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
1413         if (lflg || fflg)
1414                 (void) printf(" %*d", pidwidth,
1415                     (int)psinfo->pr_ppid); /* PPID */
1416         if (jflg) {
1417                 (void) printf(" %*d", pidwidth,
1418                     (int)psinfo->pr_pgid);   /* PGID */
1419                 (void) printf(" %*d", pidwidth,
1420                     (int)psinfo->pr_sid);    /* SID  */
1421         }
1422         if (Lflg)
1423                 (void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
1424         if (Pflg) {
1425                 if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE) /* PSR */
1426                         (void) printf("   -");
1427                 else
1428                         (void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);


1611 static void
1612 print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
1613 {
1614         int width = f->width;
1615         struct passwd *pwd;
1616         struct group *grp;
1617         time_t cputime;
1618         int bytesleft;
1619         int wcnt;
1620         wchar_t wchar;
1621         char *cp;
1622         int length;
1623         ulong_t mask;
1624         char c, *csave;
1625         int zombie_lwp;
1626 
1627         zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
1628 
1629         switch (f->fname) {
1630         case F_RUSER:
1631                 if ((pwd = getpwuid(psinfo->pr_uid)) != NULL)
1632                         (void) printf("%*s", width, pwd->pw_name);


1633                 else


1634                         (void) printf("%*u", width, psinfo->pr_uid);

1635                 break;
1636         case F_USER:
1637                 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
1638                         (void) printf("%*s", width, pwd->pw_name);


1639                 else


1640                         (void) printf("%*u", width, psinfo->pr_euid);

1641                 break;
1642         case F_RGROUP:
1643                 if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
1644                         (void) printf("%*s", width, grp->gr_name);
1645                 else
1646                         (void) printf("%*u", width, psinfo->pr_gid);
1647                 break;
1648         case F_GROUP:
1649                 if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
1650                         (void) printf("%*s", width, grp->gr_name);
1651                 else
1652                         (void) printf("%*u", width, psinfo->pr_egid);
1653                 break;
1654         case F_RUID:
1655                 (void) printf("%*u", width, psinfo->pr_uid);
1656                 break;
1657         case F_UID:
1658                 (void) printf("%*u", width, psinfo->pr_euid);
1659                 break;
1660         case F_RGID:


1862                         (void) printf("%-*.*s", width, wcnt,
1863                             psinfo->pr_psargs);
1864                 else
1865                         (void) printf("%-.*s", wcnt,
1866                             psinfo->pr_psargs);
1867                 if (f->fname == F_COMM && csave)
1868                         *csave = c;
1869                 break;
1870         case F_TASKID:
1871                 (void) printf("%*d", width, (int)psinfo->pr_taskid);
1872                 break;
1873         case F_PROJID:
1874                 (void) printf("%*d", width, (int)psinfo->pr_projid);
1875                 break;
1876         case F_PROJECT:
1877                 {
1878                         struct project cproj;
1879                         char proj_buf[PROJECT_BUFSZ];
1880 
1881                         if ((getprojbyid(psinfo->pr_projid, &cproj,
1882                             (void *)&proj_buf, PROJECT_BUFSZ)) == NULL)
1883                                 (void) printf("%*d", width,
1884                                     (int)psinfo->pr_projid);





1885                         else
1886                                 (void) printf("%*s", width,
1887                                     (cproj.pj_name != NULL) ?
1888                                     cproj.pj_name : "---");
1889                 }

1890                 break;
1891         case F_PSET:
1892                 if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
1893                         (void) printf("%*s", width, "-");
1894                 else
1895                         (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
1896                 break;
1897         case F_ZONEID:
1898                 (void) printf("%*d", width, (int)psinfo->pr_zoneid);
1899                 break;
1900         case F_ZONE:
1901                 {
1902                         char zonename[ZONENAME_MAX];
1903 
1904                         if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1905                             sizeof (zonename)) < 0) {
1906                                 (void) printf("%*d", width,
1907                                     ((int)psinfo->pr_zoneid));
1908                         } else {




1909                                 (void) printf("%*s", width, zonename);
1910                         }
1911                 }
1912                 break;
1913         case F_CTID:
1914                 if (psinfo->pr_contract == -1)
1915                         (void) printf("%*s", width, "-");
1916                 else
1917                         (void) printf("%*ld", width, (long)psinfo->pr_contract);
1918                 break;
1919         case F_LGRP:
1920                 /* Display home lgroup */
1921                 (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
1922                 break;
1923 
1924         case F_DMODEL:
1925                 (void) printf("%*s", width,
1926                     psinfo->pr_dmodel == PR_MODEL_LP64 ? "_LP64" : "_ILP32");
1927                 break;
1928         }


2166 {
2167         long    tm;
2168         struct passwd *pwd;
2169         char zonename[ZONENAME_MAX];
2170 
2171         /*
2172          * All fields before 'PID' are printed with a trailing space as a
2173          * spearator, rather than keeping track of which column is first.  All
2174          * other fields are printed with a leading space.
2175          */
2176         if (lflg) {     /* F S */
2177                 if (!yflg)
2178                         (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
2179                 (void) printf("%c ", psinfo->pr_lwp.pr_sname);       /* S */
2180         }
2181         if (Zflg) {
2182                 if (getzonenamebyid(psinfo->pr_zoneid, zonename,
2183                     sizeof (zonename)) < 0) {
2184                         (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
2185                 } else {



2186                         (void) printf("%8.8s ", zonename);
2187                 }
2188         }
2189         if (Hflg) {
2190                 /* Display home lgroup */
2191                 (void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
2192         }
2193         if (fflg) {
2194                 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
2195                         (void) printf("%8.8s ", pwd->pw_name);

2196                 else


2197                         (void) printf(" %7.7u ", psinfo->pr_euid);

2198         } else if (lflg)
2199                 (void) printf("%6u ", psinfo->pr_euid);
2200 
2201         (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
2202         if (lflg || fflg)
2203                 (void) printf(" %*d", pidwidth,
2204                     (int)psinfo->pr_ppid);                   /* PPID */
2205 
2206         if (jflg) {
2207                 (void) printf(" %*d", pidwidth,
2208                     (int)psinfo->pr_pgid);                   /* PGID */
2209                 (void) printf(" %*d", pidwidth,
2210                     (int)psinfo->pr_sid);                    /* SID  */
2211         }
2212 
2213         if (Lflg)
2214                 (void) printf(" %5d", 0);                       /* LWP */
2215         if (Pflg)
2216                 (void) printf("   -");                          /* PSR */
2217         if (Lflg && fflg)




   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2013 Gary Mills
  24  *
  25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 /*
  30  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  31  */
  32 
  33 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  34 /*        All Rights Reserved   */
  35 
  36 /*
  37  * ps -- print things about processes.
  38  */
  39 #include <stdio.h>
  40 #include <ctype.h>
  41 #include <string.h>
  42 #include <errno.h>
  43 #include <fcntl.h>
  44 #include <pwd.h>


  61 #include <libw.h>
  62 #include <stdarg.h>
  63 #include <sys/proc.h>
  64 #include <sys/pset.h>
  65 #include <project.h>
  66 #include <zone.h>
  67 
  68 #define min(a, b)       ((a) > (b) ? (b) : (a))
  69 #define max(a, b)       ((a) < (b) ? (b) : (a))
  70 
  71 #define NTTYS   20      /* initial size of table for -t option  */
  72 #define SIZ     30      /* initial size of tables for -p, -s, -g, -h and -z */
  73 
  74 /*
  75  * Size of buffer holding args for t, p, s, g, u, U, G, z options.
  76  * Set to ZONENAME_MAX, the minimum value needed to allow any
  77  * zone to be specified.
  78  */
  79 #define ARGSIZ ZONENAME_MAX
  80 
  81 #define MAXUGNAME (LOGNAME_MAX+2)       /* max chars in a user/group */
  82                                                 /* name or printed u/g id */
  83 
  84 /* Structure for storing user or group info */
  85 struct ugdata {
  86         id_t    id;                     /* numeric user-id or group-id */
  87         char    name[MAXUGNAME+1];      /* user/group name, null terminated */
  88 };
  89 
  90 struct ughead {
  91         size_t  size;           /* number of ugdata structs allocated */
  92         size_t  nent;           /* number of active entries */
  93         struct ugdata *ent;     /* pointer to array of actual entries */
  94 };
  95 
  96 enum fname {    /* enumeration of field names */
  97         F_USER,         /* effective user of the process */
  98         F_RUSER,        /* real user of the process */
  99         F_GROUP,        /* effective group of the process */
 100         F_RGROUP,       /* real group of the process */
 101         F_UID,          /* numeric effective uid of the process */
 102         F_RUID,         /* numeric real uid of the process */


 210         { "pset",       "PSET",         3,      3       },
 211         { "zone",       "ZONE",         8,      8       },
 212         { "zoneid",     "ZONEID",       5,      5       },
 213         { "ctid",       "CTID",         5,      5       },
 214         { "lgrp",       "LGRP",         4,      2       },
 215         { "dmodel",     "DMODEL",       6,      6       },
 216 };
 217 
 218 #define NFIELDS (sizeof (fname) / sizeof (fname[0]))
 219 
 220 static  int     retcode = 1;
 221 static  int     lflg;
 222 static  int     Aflg;
 223 static  int     uflg;
 224 static  int     Uflg;
 225 static  int     Gflg;
 226 static  int     aflg;
 227 static  int     dflg;
 228 static  int     Lflg;
 229 static  int     Pflg;
 230 static  int     Wflg;
 231 static  int     yflg;
 232 static  int     pflg;
 233 static  int     fflg;
 234 static  int     cflg;
 235 static  int     jflg;
 236 static  int     gflg;
 237 static  int     sflg;
 238 static  int     tflg;
 239 static  int     zflg;
 240 static  int     Zflg;
 241 static  int     hflg;
 242 static  int     Hflg;
 243 static  uid_t   tuid = (uid_t)-1;
 244 static  int     errflg;
 245 
 246 static  int     ndev;           /* number of devices */
 247 static  int     maxdev;         /* number of devl structures allocated */
 248 
 249 #define DNINCR  100
 250 #define DNSIZE  14


 411         pidwidth = 1;
 412         while ((id /= 10) > 0)
 413                 ++pidwidth;
 414         pidwidth = pidwidth < 5 ? 5 : pidwidth;
 415 
 416         fname[F_PID].width = fname[F_PPID].width = pidwidth;
 417         fname[F_PGID].width = fname[F_SID].width = pidwidth;
 418 
 419         /*
 420          * TRANSLATION_NOTE
 421          * Specify the printf format with width and precision for
 422          * the STIME field.
 423          */
 424         len = snprintf(loc_stime_str, sizeof (loc_stime_str),
 425             dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
 426         if (len >= sizeof (loc_stime_str))
 427                 len = sizeof (loc_stime_str) - 1;
 428 
 429         fname[F_STIME].width = fname[F_STIME].minwidth = len;
 430 
 431         while ((c = getopt(argc, argv, "jlfceAadLPWyZHh:t:p:g:u:U:G:n:s:o:z:"))
 432             != EOF)
 433                 switch (c) {
 434                 case 'H':               /* Show home lgroups */
 435                         Hflg++;
 436                         break;
 437                 case 'h':
 438                         /*
 439                          * Show processes/threads with given home lgroups
 440                          */
 441                         hflg++;
 442                         p1 = optarg;
 443                         do {
 444                                 int id;
 445 
 446                                 /*
 447                                  * Get all IDs in the list, verify for
 448                                  * correctness and place in lgrps array.
 449                                  */
 450                                 parg = getarg(&p1);
 451                                 /* Convert string to integer */


 505                         Aflg++;
 506                         tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
 507                         zflg = hflg = 0;
 508                         break;
 509                 case 'a':
 510                         /*
 511                          * Same as 'e' except no session group leaders
 512                          * and no non-terminal processes.
 513                          */
 514                         aflg++;
 515                         break;
 516                 case 'd':       /* same as e except no session leaders */
 517                         dflg++;
 518                         break;
 519                 case 'L':       /* show lwps */
 520                         Lflg++;
 521                         break;
 522                 case 'P':       /* show bound processor */
 523                         Pflg++;
 524                         break;
 525                 case 'W':       /* truncate long names */
 526                         Wflg++;
 527                         break;
 528                 case 'y':       /* omit F & ADDR, report RSS & SZ in Kby */
 529                         yflg++;
 530                         break;
 531                 case 'n':       /* no longer needed; retain as no-op */
 532                         (void) fprintf(stderr,
 533                             gettext("ps: warning: -n option ignored\n"));
 534                         break;
 535                 case 't':               /* terminals */
 536 #define TSZ     30
 537                         tflg++;
 538                         p1 = optarg;
 539                         do {
 540                                 char nambuf[TSZ+6];     /* for "/dev/" + '\0' */
 541                                 struct stat64 s;
 542                                 parg = getarg(&p1);
 543                                 p = Realloc(NULL, TSZ+1);       /* for '\0' */
 544                                 /* zero the buffer before using it */
 545                                 p[0] = '\0';
 546                                 size = TSZ;
 547                                 if (isdigit(*parg)) {


1047         }
1048         return (0);
1049 }
1050 
1051 static int
1052 field_cmp(const void *l, const void *r)
1053 {
1054         struct def_field *lhs = *((struct def_field **)l);
1055         struct def_field *rhs = *((struct def_field **)r);
1056 
1057         return (strcmp(lhs->fname, rhs->fname));
1058 }
1059 
1060 static void
1061 usage(void)             /* print usage message and quit */
1062 {
1063         struct def_field *df, *sorted[NFIELDS];
1064         int pos = 80, i = 0;
1065 
1066         static char usage1[] =
1067             "ps [ -aAdefHlcjLPWyZ ] [ -o format ] [ -t termlist ]";
1068         static char usage2[] =
1069             "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
1070         static char usage3[] =
1071             "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ]";
1072         static char usage4[] =
1073             "\t[ -z zonelist ] [-h lgrplist]";
1074         static char usage5[] =
1075             "  'format' is one or more of:";
1076 
1077         (void) fprintf(stderr,
1078             gettext("usage: %s\n%s\n%s\n%s\n%s"),
1079             gettext(usage1), gettext(usage2), gettext(usage3),
1080             gettext(usage4), gettext(usage5));
1081 
1082         /*
1083          * Now print out the possible output formats such that they neatly fit
1084          * into eighty columns.  Note that the fact that we are determining
1085          * this output programmatically means that a gettext() is impossible --
1086          * but it would be a mistake to localize the output formats anyway as
1087          * they are tokens for input, not output themselves.


1387                 pr_fields(psinfo, ttyp, print_field);
1388                 return;
1389         }
1390 
1391         /*
1392          * All fields before 'PID' are printed with a trailing space as a
1393          * separator, rather than keeping track of which column is first.  All
1394          * other fields are printed with a leading space.
1395          */
1396         if (lflg) {
1397                 if (!yflg)
1398                         (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
1399                 (void) printf("%c ", psinfo->pr_lwp.pr_sname);       /* S */
1400         }
1401 
1402         if (Zflg) {                                             /* ZONE */
1403                 if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1404                     sizeof (zonename)) < 0) {
1405                         (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
1406                 } else {
1407                         if (strlen(zonename) > 8)
1408                                 (void) printf("%7.7s%c ", zonename, '*');
1409                         else
1410                                 (void) printf("%8.8s ", zonename);
1411                 }
1412         }
1413 
1414         if (fflg) {                                             /* UID */
1415                 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
1416                         if (strlen(pwd->pw_name) > 8)
1417                                 (void) printf("%7.7s%c ", pwd->pw_name, '*');
1418                         else
1419                                 (void) printf("%8.8s ", pwd->pw_name);
1420                 } else {
1421                         (void) printf(" %7.7u ", psinfo->pr_euid);
1422                 }
1423         } else if (lflg) {
1424                 (void) printf("%6u ", psinfo->pr_euid);
1425         }
1426         (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
1427         if (lflg || fflg)
1428                 (void) printf(" %*d", pidwidth,
1429                     (int)psinfo->pr_ppid); /* PPID */
1430         if (jflg) {
1431                 (void) printf(" %*d", pidwidth,
1432                     (int)psinfo->pr_pgid);   /* PGID */
1433                 (void) printf(" %*d", pidwidth,
1434                     (int)psinfo->pr_sid);    /* SID  */
1435         }
1436         if (Lflg)
1437                 (void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
1438         if (Pflg) {
1439                 if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE) /* PSR */
1440                         (void) printf("   -");
1441                 else
1442                         (void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);


1625 static void
1626 print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
1627 {
1628         int width = f->width;
1629         struct passwd *pwd;
1630         struct group *grp;
1631         time_t cputime;
1632         int bytesleft;
1633         int wcnt;
1634         wchar_t wchar;
1635         char *cp;
1636         int length;
1637         ulong_t mask;
1638         char c, *csave;
1639         int zombie_lwp;
1640 
1641         zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
1642 
1643         switch (f->fname) {
1644         case F_RUSER:
1645                 if ((pwd = getpwuid(psinfo->pr_uid)) != NULL) {
1646                         if (Wflg && strlen(pwd->pw_name) > width)
1647                                 (void) printf("%.*s%c", width - 1,
1648                                     pwd->pw_name, '*');
1649                         else
1650                                 (void) printf("%*s", width, pwd->pw_name);
1651                 } else {
1652                         (void) printf("%*u", width, psinfo->pr_uid);
1653                 }
1654                 break;
1655         case F_USER:
1656                 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
1657                         if (Wflg && strlen(pwd->pw_name) > width)
1658                                 (void) printf("%.*s%c", width - 1,
1659                                     pwd->pw_name, '*');
1660                         else
1661                                 (void) printf("%*s", width, pwd->pw_name);
1662                 } else {
1663                         (void) printf("%*u", width, psinfo->pr_euid);
1664                 }
1665                 break;
1666         case F_RGROUP:
1667                 if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
1668                         (void) printf("%*s", width, grp->gr_name);
1669                 else
1670                         (void) printf("%*u", width, psinfo->pr_gid);
1671                 break;
1672         case F_GROUP:
1673                 if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
1674                         (void) printf("%*s", width, grp->gr_name);
1675                 else
1676                         (void) printf("%*u", width, psinfo->pr_egid);
1677                 break;
1678         case F_RUID:
1679                 (void) printf("%*u", width, psinfo->pr_uid);
1680                 break;
1681         case F_UID:
1682                 (void) printf("%*u", width, psinfo->pr_euid);
1683                 break;
1684         case F_RGID:


1886                         (void) printf("%-*.*s", width, wcnt,
1887                             psinfo->pr_psargs);
1888                 else
1889                         (void) printf("%-.*s", wcnt,
1890                             psinfo->pr_psargs);
1891                 if (f->fname == F_COMM && csave)
1892                         *csave = c;
1893                 break;
1894         case F_TASKID:
1895                 (void) printf("%*d", width, (int)psinfo->pr_taskid);
1896                 break;
1897         case F_PROJID:
1898                 (void) printf("%*d", width, (int)psinfo->pr_projid);
1899                 break;
1900         case F_PROJECT:
1901                 {
1902                         struct project cproj;
1903                         char proj_buf[PROJECT_BUFSZ];
1904 
1905                         if ((getprojbyid(psinfo->pr_projid, &cproj,
1906                             (void *)&proj_buf, PROJECT_BUFSZ)) == NULL) {
1907                                 (void) printf("%*d", width,
1908                                     (int)psinfo->pr_projid);
1909                         } else {
1910                                 if (Wflg && cproj.pj_name != NULL &&
1911                                     strlen(cproj.pj_name) > width)
1912                                         (void) printf("%.*s%c", width - 1,
1913                                             cproj.pj_name, '*');
1914                                 else
1915                                         (void) printf("%*s", width,
1916                                             (cproj.pj_name != NULL) ?
1917                                             cproj.pj_name : "---");
1918                         }
1919                 }
1920                 break;
1921         case F_PSET:
1922                 if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
1923                         (void) printf("%*s", width, "-");
1924                 else
1925                         (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
1926                 break;
1927         case F_ZONEID:
1928                 (void) printf("%*d", width, (int)psinfo->pr_zoneid);
1929                 break;
1930         case F_ZONE:
1931                 {
1932                         char zonename[ZONENAME_MAX];
1933 
1934                         if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1935                             sizeof (zonename)) < 0) {
1936                                 (void) printf("%*d", width,
1937                                     ((int)psinfo->pr_zoneid));
1938                         } else {
1939                                 if (Wflg && strlen(zonename) > width)
1940                                         (void) printf("%.*s%c", width - 1,
1941                                             zonename, '*');
1942                                 else
1943                                         (void) printf("%*s", width, zonename);
1944                         }
1945                 }
1946                 break;
1947         case F_CTID:
1948                 if (psinfo->pr_contract == -1)
1949                         (void) printf("%*s", width, "-");
1950                 else
1951                         (void) printf("%*ld", width, (long)psinfo->pr_contract);
1952                 break;
1953         case F_LGRP:
1954                 /* Display home lgroup */
1955                 (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
1956                 break;
1957 
1958         case F_DMODEL:
1959                 (void) printf("%*s", width,
1960                     psinfo->pr_dmodel == PR_MODEL_LP64 ? "_LP64" : "_ILP32");
1961                 break;
1962         }


2200 {
2201         long    tm;
2202         struct passwd *pwd;
2203         char zonename[ZONENAME_MAX];
2204 
2205         /*
2206          * All fields before 'PID' are printed with a trailing space as a
2207          * spearator, rather than keeping track of which column is first.  All
2208          * other fields are printed with a leading space.
2209          */
2210         if (lflg) {     /* F S */
2211                 if (!yflg)
2212                         (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
2213                 (void) printf("%c ", psinfo->pr_lwp.pr_sname);       /* S */
2214         }
2215         if (Zflg) {
2216                 if (getzonenamebyid(psinfo->pr_zoneid, zonename,
2217                     sizeof (zonename)) < 0) {
2218                         (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
2219                 } else {
2220                         if (strlen(zonename) > 8)
2221                                 (void) printf("%7.7s%c ", zonename, '*');
2222                         else
2223                                 (void) printf("%8.8s ", zonename);
2224                 }
2225         }
2226         if (Hflg) {
2227                 /* Display home lgroup */
2228                 (void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
2229         }
2230         if (fflg) {
2231                 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
2232                         if (strlen(pwd->pw_name) > 8)
2233                                 (void) printf("%7.7s%c ", pwd->pw_name, '*');
2234                         else
2235                                 (void) printf("%8.8s ", pwd->pw_name);
2236                 } else {
2237                         (void) printf(" %7.7u ", psinfo->pr_euid);
2238                 }
2239         } else if (lflg)
2240                 (void) printf("%6u ", psinfo->pr_euid);
2241 
2242         (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
2243         if (lflg || fflg)
2244                 (void) printf(" %*d", pidwidth,
2245                     (int)psinfo->pr_ppid);                   /* PPID */
2246 
2247         if (jflg) {
2248                 (void) printf(" %*d", pidwidth,
2249                     (int)psinfo->pr_pgid);                   /* PGID */
2250                 (void) printf(" %*d", pidwidth,
2251                     (int)psinfo->pr_sid);                    /* SID  */
2252         }
2253 
2254         if (Lflg)
2255                 (void) printf(" %5d", 0);                       /* LWP */
2256         if (Pflg)
2257                 (void) printf("   -");                          /* PSR */
2258         if (Lflg && fflg)