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

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/ps/ps.c
          +++ new/usr/src/cmd/ps/ps.c
↓ open down ↓ 12 lines elided ↑ open up ↑
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  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  /*
       23 + * Copyright (c) 2013 Gary Mills
       24 + *
  23   25   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24   26   * Use is subject to license terms.
  25   27   */
  26   28  
  27   29  /*
  28   30   * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  29   31   */
  30   32  
  31   33  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  32   34  /*        All Rights Reserved   */
↓ open down ↓ 36 lines elided ↑ open up ↑
  69   71  #define NTTYS   20      /* initial size of table for -t option  */
  70   72  #define SIZ     30      /* initial size of tables for -p, -s, -g, -h and -z */
  71   73  
  72   74  /*
  73   75   * Size of buffer holding args for t, p, s, g, u, U, G, z options.
  74   76   * Set to ZONENAME_MAX, the minimum value needed to allow any
  75   77   * zone to be specified.
  76   78   */
  77   79  #define ARGSIZ ZONENAME_MAX
  78   80  
  79      -#define MAXUGNAME 10    /* max chars in a user/group name or printed u/g id */
       81 +/* Max chars in a user/group name or printed u/g id */
       82 +#define MAXUGNAME (LOGNAME_MAX+2)
  80   83  
  81   84  /* Structure for storing user or group info */
  82   85  struct ugdata {
  83   86          id_t    id;                     /* numeric user-id or group-id */
  84   87          char    name[MAXUGNAME+1];      /* user/group name, null terminated */
  85   88  };
  86   89  
  87   90  struct ughead {
  88   91          size_t  size;           /* number of ugdata structs allocated */
  89   92          size_t  nent;           /* number of active entries */
↓ open down ↓ 127 lines elided ↑ open up ↑
 217  220  static  int     retcode = 1;
 218  221  static  int     lflg;
 219  222  static  int     Aflg;
 220  223  static  int     uflg;
 221  224  static  int     Uflg;
 222  225  static  int     Gflg;
 223  226  static  int     aflg;
 224  227  static  int     dflg;
 225  228  static  int     Lflg;
 226  229  static  int     Pflg;
      230 +static  int     Wflg;
 227  231  static  int     yflg;
 228  232  static  int     pflg;
 229  233  static  int     fflg;
 230  234  static  int     cflg;
 231  235  static  int     jflg;
 232  236  static  int     gflg;
 233  237  static  int     sflg;
 234  238  static  int     tflg;
 235  239  static  int     zflg;
 236  240  static  int     Zflg;
↓ open down ↓ 180 lines elided ↑ open up ↑
 417  421           * Specify the printf format with width and precision for
 418  422           * the STIME field.
 419  423           */
 420  424          len = snprintf(loc_stime_str, sizeof (loc_stime_str),
 421  425              dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
 422  426          if (len >= sizeof (loc_stime_str))
 423  427                  len = sizeof (loc_stime_str) - 1;
 424  428  
 425  429          fname[F_STIME].width = fname[F_STIME].minwidth = len;
 426  430  
 427      -        while ((c = getopt(argc, argv, "jlfceAadLPyZHh:t:p:g:u:U:G:n:s:o:z:"))
      431 +        while ((c = getopt(argc, argv, "jlfceAadLPWyZHh:t:p:g:u:U:G:n:s:o:z:"))
 428  432              != EOF)
 429  433                  switch (c) {
 430  434                  case 'H':               /* Show home lgroups */
 431  435                          Hflg++;
 432  436                          break;
 433  437                  case 'h':
 434  438                          /*
 435  439                           * Show processes/threads with given home lgroups
 436  440                           */
 437  441                          hflg++;
↓ open down ↓ 73 lines elided ↑ open up ↑
 511  515                          break;
 512  516                  case 'd':       /* same as e except no session leaders */
 513  517                          dflg++;
 514  518                          break;
 515  519                  case 'L':       /* show lwps */
 516  520                          Lflg++;
 517  521                          break;
 518  522                  case 'P':       /* show bound processor */
 519  523                          Pflg++;
 520  524                          break;
      525 +                case 'W':       /* truncate long names */
      526 +                        Wflg++;
      527 +                        break;
 521  528                  case 'y':       /* omit F & ADDR, report RSS & SZ in Kby */
 522  529                          yflg++;
 523  530                          break;
 524  531                  case 'n':       /* no longer needed; retain as no-op */
 525  532                          (void) fprintf(stderr,
 526  533                              gettext("ps: warning: -n option ignored\n"));
 527  534                          break;
 528  535                  case 't':               /* terminals */
 529  536  #define TSZ     30
 530  537                          tflg++;
↓ open down ↓ 519 lines elided ↑ open up ↑
1050 1057          return (strcmp(lhs->fname, rhs->fname));
1051 1058  }
1052 1059  
1053 1060  static void
1054 1061  usage(void)             /* print usage message and quit */
1055 1062  {
1056 1063          struct def_field *df, *sorted[NFIELDS];
1057 1064          int pos = 80, i = 0;
1058 1065  
1059 1066          static char usage1[] =
1060      -            "ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]";
     1067 +            "ps [ -aAdefHlcjLPWyZ ] [ -o format ] [ -t termlist ]";
1061 1068          static char usage2[] =
1062 1069              "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
1063 1070          static char usage3[] =
1064 1071              "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ]";
1065 1072          static char usage4[] =
1066 1073              "\t[ -z zonelist ] [-h lgrplist]";
1067 1074          static char usage5[] =
1068 1075              "  'format' is one or more of:";
1069 1076  
1070 1077          (void) fprintf(stderr,
↓ open down ↓ 317 lines elided ↑ open up ↑
1388 1395           */
1389 1396          if (lflg) {
1390 1397                  if (!yflg)
1391 1398                          (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
1392 1399                  (void) printf("%c ", psinfo->pr_lwp.pr_sname);  /* S */
1393 1400          }
1394 1401  
1395 1402          if (Zflg) {                                             /* ZONE */
1396 1403                  if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1397 1404                      sizeof (zonename)) < 0) {
1398      -                        (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
     1405 +                        if (snprintf(NULL, 0, "%d",
     1406 +                            ((int)psinfo->pr_zoneid)) > 7)
     1407 +                                (void) printf(" %6.6d%c ",
     1408 +                                    ((int)psinfo->pr_zoneid), '*');
     1409 +                        else
     1410 +                                (void) printf(" %7.7d ",
     1411 +                                    ((int)psinfo->pr_zoneid));
1399 1412                  } else {
1400      -                        (void) printf("%8.8s ", zonename);
     1413 +                        size_t nw;
     1414 +
     1415 +                        nw = mbstowcs(NULL, zonename, 0);
     1416 +                        if (nw == (size_t)-1)
     1417 +                                (void) printf("%8.8s ", "ERROR");
     1418 +                        else if (nw > 8)
     1419 +                                (void) wprintf(L"%7.7s%c ", zonename, '*');
     1420 +                        else
     1421 +                                (void) wprintf(L"%8.8s ", zonename);
1401 1422                  }
1402 1423          }
1403 1424  
1404 1425          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);
     1426 +                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
     1427 +                        size_t nw;
     1428 +
     1429 +                        nw = mbstowcs(NULL, pwd->pw_name, 0);
     1430 +                        if (nw == (size_t)-1)
     1431 +                                (void) printf("%8.8s ", "ERROR");
     1432 +                        else if (nw > 8)
     1433 +                                (void) wprintf(L"%7.7s%c ", pwd->pw_name, '*');
     1434 +                        else
     1435 +                                (void) wprintf(L"%8.8s ", pwd->pw_name);
     1436 +                } else {
     1437 +                        if (snprintf(NULL, 0, "%u",
     1438 +                            (psinfo->pr_euid)) > 7)
     1439 +                                (void) printf(" %6.6u%c ", psinfo->pr_euid,
     1440 +                                    '*');
     1441 +                        else
     1442 +                                (void) printf(" %7.7u ", psinfo->pr_euid);
     1443 +                }
1409 1444          } else if (lflg) {
1410      -                (void) printf("%6u ", psinfo->pr_euid);
     1445 +                if (snprintf(NULL, 0, "%u", (psinfo->pr_euid)) > 6)
     1446 +                        (void) printf("%5.5u%c ", psinfo->pr_euid, '*');
     1447 +                else
     1448 +                        (void) printf("%6u ", psinfo->pr_euid);
1411 1449          }
1412 1450          (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
1413 1451          if (lflg || fflg)
1414 1452                  (void) printf(" %*d", pidwidth,
1415 1453                      (int)psinfo->pr_ppid); /* PPID */
1416 1454          if (jflg) {
1417 1455                  (void) printf(" %*d", pidwidth,
1418 1456                      (int)psinfo->pr_pgid);      /* PGID */
1419 1457                  (void) printf(" %*d", pidwidth,
1420 1458                      (int)psinfo->pr_sid);       /* SID  */
↓ open down ↓ 200 lines elided ↑ open up ↑
1621 1659          char *cp;
1622 1660          int length;
1623 1661          ulong_t mask;
1624 1662          char c, *csave;
1625 1663          int zombie_lwp;
1626 1664  
1627 1665          zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
1628 1666  
1629 1667          switch (f->fname) {
1630 1668          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);
     1669 +                if ((pwd = getpwuid(psinfo->pr_uid)) != NULL) {
     1670 +                        size_t nw;
     1671 +
     1672 +                        nw = mbstowcs(NULL, pwd->pw_name, 0);
     1673 +                        if (nw == (size_t)-1)
     1674 +                                (void) printf("%*s ", width, "ERROR");
     1675 +                        else if (Wflg && nw > width)
     1676 +                                (void) wprintf(L"%.*s%c", width - 1,
     1677 +                                    pwd->pw_name, '*');
     1678 +                        else
     1679 +                                (void) wprintf(L"%*s", width, pwd->pw_name);
     1680 +                } else {
     1681 +                        if (Wflg && snprintf(NULL, 0, "%u",
     1682 +                            (psinfo->pr_uid)) > width)
     1683 +
     1684 +                                (void) printf("%*u%c", width - 1,
     1685 +                                    psinfo->pr_uid, '*');
     1686 +                        else
     1687 +                                (void) printf("%*u", width, psinfo->pr_uid);
     1688 +                }
1635 1689                  break;
1636 1690          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);
     1691 +                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
     1692 +                        size_t nw;
     1693 +
     1694 +                        nw = mbstowcs(NULL, pwd->pw_name, 0);
     1695 +                        if (nw == (size_t)-1)
     1696 +                                (void) printf("%*s ", width, "ERROR");
     1697 +                        else if (Wflg && nw > width)
     1698 +                                (void) wprintf(L"%.*s%c", width - 1,
     1699 +                                    pwd->pw_name, '*');
     1700 +                        else
     1701 +                                (void) wprintf(L"%*s", width, pwd->pw_name);
     1702 +                } else {
     1703 +                        if (Wflg && snprintf(NULL, 0, "%u",
     1704 +                            (psinfo->pr_euid)) > width)
     1705 +
     1706 +                                (void) printf("%*u%c", width - 1,
     1707 +                                    psinfo->pr_euid, '*');
     1708 +                        else
     1709 +                                (void) printf("%*u", width, psinfo->pr_euid);
     1710 +                }
1641 1711                  break;
1642 1712          case F_RGROUP:
1643 1713                  if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
1644 1714                          (void) printf("%*s", width, grp->gr_name);
1645 1715                  else
1646 1716                          (void) printf("%*u", width, psinfo->pr_gid);
1647 1717                  break;
1648 1718          case F_GROUP:
1649 1719                  if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
1650 1720                          (void) printf("%*s", width, grp->gr_name);
↓ open down ↓ 221 lines elided ↑ open up ↑
1872 1942                  break;
1873 1943          case F_PROJID:
1874 1944                  (void) printf("%*d", width, (int)psinfo->pr_projid);
1875 1945                  break;
1876 1946          case F_PROJECT:
1877 1947                  {
1878 1948                          struct project cproj;
1879 1949                          char proj_buf[PROJECT_BUFSZ];
1880 1950  
1881 1951                          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 : "---");
     1952 +                            (void *)&proj_buf, PROJECT_BUFSZ)) == NULL) {
     1953 +                                if (Wflg && snprintf(NULL, 0, "%d",
     1954 +                                    ((int)psinfo->pr_projid)) > width)
     1955 +                                        (void) printf("%.*d%c", width - 1,
     1956 +                                            ((int)psinfo->pr_projid), '*');
     1957 +                                else
     1958 +                                        (void) printf("%*d", width,
     1959 +                                            (int)psinfo->pr_projid);
     1960 +                        } else {
     1961 +                                size_t nw;
     1962 +
     1963 +                                if (cproj.pj_name != NULL)
     1964 +                                        nw = mbstowcs(NULL, cproj.pj_name, 0);
     1965 +                                if (cproj.pj_name == NULL)
     1966 +                                        (void) printf("%*s ", width, "---");
     1967 +                                else if (nw == (size_t)-1)
     1968 +                                        (void) printf("%*s ", width, "ERROR");
     1969 +                                else if (Wflg && nw > width)
     1970 +                                        (void) wprintf(L"%.*s%c", width - 1,
     1971 +                                            cproj.pj_name, '*');
     1972 +                                else
     1973 +                                        (void) wprintf(L"%*s", width,
     1974 +                                            cproj.pj_name);
     1975 +                        }
1889 1976                  }
1890 1977                  break;
1891 1978          case F_PSET:
1892 1979                  if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
1893 1980                          (void) printf("%*s", width, "-");
1894 1981                  else
1895 1982                          (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
1896 1983                  break;
1897 1984          case F_ZONEID:
1898 1985                  (void) printf("%*d", width, (int)psinfo->pr_zoneid);
1899 1986                  break;
1900 1987          case F_ZONE:
1901 1988                  {
1902 1989                          char zonename[ZONENAME_MAX];
1903 1990  
1904 1991                          if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1905 1992                              sizeof (zonename)) < 0) {
1906      -                                (void) printf("%*d", width,
1907      -                                    ((int)psinfo->pr_zoneid));
     1993 +                                if (Wflg && snprintf(NULL, 0, "%d",
     1994 +                                    ((int)psinfo->pr_zoneid)) > width)
     1995 +                                        (void) printf("%.*d%c", width - 1,
     1996 +                                            ((int)psinfo->pr_zoneid), '*');
     1997 +                                else
     1998 +                                        (void) printf("%*d", width,
     1999 +                                            (int)psinfo->pr_zoneid);
1908 2000                          } else {
1909      -                                (void) printf("%*s", width, zonename);
     2001 +                                size_t nw;
     2002 +
     2003 +                                nw = mbstowcs(NULL, zonename, 0);
     2004 +                                if (nw == (size_t)-1)
     2005 +                                        (void) printf("%*s ", width, "ERROR");
     2006 +                                else if (Wflg && nw > width)
     2007 +                                        (void) wprintf(L"%.*s%c", width - 1,
     2008 +                                            zonename, '*');
     2009 +                                else
     2010 +                                        (void) wprintf(L"%*s", width, zonename);
1910 2011                          }
1911 2012                  }
1912 2013                  break;
1913 2014          case F_CTID:
1914 2015                  if (psinfo->pr_contract == -1)
1915 2016                          (void) printf("%*s", width, "-");
1916 2017                  else
1917 2018                          (void) printf("%*ld", width, (long)psinfo->pr_contract);
1918 2019                  break;
1919 2020          case F_LGRP:
↓ open down ↓ 254 lines elided ↑ open up ↑
2174 2275           * other fields are printed with a leading space.
2175 2276           */
2176 2277          if (lflg) {     /* F S */
2177 2278                  if (!yflg)
2178 2279                          (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
2179 2280                  (void) printf("%c ", psinfo->pr_lwp.pr_sname);  /* S */
2180 2281          }
2181 2282          if (Zflg) {
2182 2283                  if (getzonenamebyid(psinfo->pr_zoneid, zonename,
2183 2284                      sizeof (zonename)) < 0) {
2184      -                        (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
     2285 +                        if (snprintf(NULL, 0, "%d",
     2286 +                            ((int)psinfo->pr_zoneid)) > 7)
     2287 +                                (void) printf(" %6.6d%c ",
     2288 +                                    ((int)psinfo->pr_zoneid), '*');
     2289 +                        else
     2290 +                                (void) printf(" %7.7d ",
     2291 +                                    ((int)psinfo->pr_zoneid));
2185 2292                  } else {
2186      -                        (void) printf("%8.8s ", zonename);
     2293 +                        size_t nw;
     2294 +
     2295 +                        nw = mbstowcs(NULL, zonename, 0);
     2296 +                        if (nw == (size_t)-1)
     2297 +                                (void) printf("%8.8s ", "ERROR");
     2298 +                        else if (nw > 8)
     2299 +                                (void) wprintf(L"%7.7s%c ", zonename, '*');
     2300 +                        else
     2301 +                                (void) wprintf(L"%8.8s ", zonename);
2187 2302                  }
2188 2303          }
2189 2304          if (Hflg) {
2190 2305                  /* Display home lgroup */
2191 2306                  (void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
2192 2307          }
2193 2308          if (fflg) {
2194      -                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
2195      -                        (void) printf("%8.8s ", pwd->pw_name);
     2309 +                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
     2310 +                        size_t nw;
     2311 +
     2312 +                        nw = mbstowcs(NULL, pwd->pw_name, 0);
     2313 +                        if (nw == (size_t)-1)
     2314 +                                (void) printf("%8.8s ", "ERROR");
     2315 +                        else if (nw > 8)
     2316 +                                (void) wprintf(L"%7.7s%c ", pwd->pw_name, '*');
     2317 +                        else
     2318 +                                (void) wprintf(L"%8.8s ", pwd->pw_name);
     2319 +                } else {
     2320 +                        if (snprintf(NULL, 0, "%u",
     2321 +                            (psinfo->pr_euid)) > 7)
     2322 +                                (void) printf(" %6.6u%c ", psinfo->pr_euid,
     2323 +                                    '*');
     2324 +                        else
     2325 +                                (void) printf(" %7.7u ", psinfo->pr_euid);
     2326 +                }
     2327 +        } else if (lflg) {
     2328 +                if (snprintf(NULL, 0, "%u", (psinfo->pr_euid)) > 6)
     2329 +                        (void) printf("%5.5u%c ", psinfo->pr_euid, '*');
2196 2330                  else
2197      -                        (void) printf(" %7.7u ", psinfo->pr_euid);
2198      -        } else if (lflg)
2199      -                (void) printf("%6u ", psinfo->pr_euid);
     2331 +                        (void) printf("%6u ", psinfo->pr_euid);
     2332 +        }
2200 2333  
2201 2334          (void) printf("%*d", pidwidth, (int)psinfo->pr_pid);    /* PID */
2202 2335          if (lflg || fflg)
2203 2336                  (void) printf(" %*d", pidwidth,
2204 2337                      (int)psinfo->pr_ppid);                      /* PPID */
2205 2338  
2206 2339          if (jflg) {
2207 2340                  (void) printf(" %*d", pidwidth,
2208 2341                      (int)psinfo->pr_pgid);                      /* PGID */
2209 2342                  (void) printf(" %*d", pidwidth,
↓ open down ↓ 186 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX