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 +#define MAXUGNAME (LOGNAME_MAX+2)       /* max chars in a user/group */
       82 +                                                /* name or printed u/g id */
  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 +                        if (strlen(zonename) > 8)
     1414 +                                (void) printf("%7.7s%c ", zonename, '*');
     1415 +                        else
     1416 +                                (void) printf("%8.8s ", zonename);
1401 1417                  }
1402 1418          }
1403 1419  
1404 1420          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);
     1421 +                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
     1422 +                        if (strlen(pwd->pw_name) > 8)
     1423 +                                (void) printf("%7.7s%c ", pwd->pw_name, '*');
     1424 +                        else
     1425 +                                (void) printf("%8.8s ", pwd->pw_name);
     1426 +                } else {
     1427 +                        if (snprintf(NULL, 0, "%u",
     1428 +                            ((int)psinfo->pr_euid)) > 7)
     1429 +                                (void) printf(" %6.6u%c ", psinfo->pr_euid,
     1430 +                                    '*');
     1431 +                        else
     1432 +                                (void) printf(" %7.7u ", psinfo->pr_euid);
     1433 +                }
1409 1434          } else if (lflg) {
1410      -                (void) printf("%6u ", psinfo->pr_euid);
     1435 +                if (snprintf(NULL, 0, "%u", ((int)psinfo->pr_euid)) > 6)
     1436 +                        (void) printf("%5.5u%c ", psinfo->pr_euid, '*');
     1437 +                else
     1438 +                        (void) printf("%6u ", psinfo->pr_euid);
1411 1439          }
1412 1440          (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
1413 1441          if (lflg || fflg)
1414 1442                  (void) printf(" %*d", pidwidth,
1415 1443                      (int)psinfo->pr_ppid); /* PPID */
1416 1444          if (jflg) {
1417 1445                  (void) printf(" %*d", pidwidth,
1418 1446                      (int)psinfo->pr_pgid);      /* PGID */
1419 1447                  (void) printf(" %*d", pidwidth,
1420 1448                      (int)psinfo->pr_sid);       /* SID  */
↓ open down ↓ 200 lines elided ↑ open up ↑
1621 1649          char *cp;
1622 1650          int length;
1623 1651          ulong_t mask;
1624 1652          char c, *csave;
1625 1653          int zombie_lwp;
1626 1654  
1627 1655          zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
1628 1656  
1629 1657          switch (f->fname) {
1630 1658          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);
     1659 +                if ((pwd = getpwuid(psinfo->pr_uid)) != NULL) {
     1660 +                        if (Wflg && strlen(pwd->pw_name) > width)
     1661 +                                (void) printf("%.*s%c", width - 1,
     1662 +                                    pwd->pw_name, '*');
     1663 +                        else
     1664 +                                (void) printf("%*s", width, pwd->pw_name);
     1665 +                } else {
     1666 +                        if (Wflg && snprintf(NULL, 0, "%u",
     1667 +                            ((int)psinfo->pr_uid)) > width)
     1668 +
     1669 +                                (void) printf("%*u%c", width - 1,
     1670 +                                    psinfo->pr_uid, '*');
     1671 +                        else
     1672 +                                (void) printf("%*u", width, psinfo->pr_uid);
     1673 +                }
1635 1674                  break;
1636 1675          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);
     1676 +                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
     1677 +                        if (Wflg && strlen(pwd->pw_name) > width)
     1678 +                                (void) printf("%.*s%c", width - 1,
     1679 +                                    pwd->pw_name, '*');
     1680 +                        else
     1681 +                                (void) printf("%*s", width, pwd->pw_name);
     1682 +                } else {
     1683 +                        if (Wflg && snprintf(NULL, 0, "%u",
     1684 +                            ((int)psinfo->pr_euid)) > width)
     1685 +
     1686 +                                (void) printf("%*u%c", width - 1,
     1687 +                                    psinfo->pr_euid, '*');
     1688 +                        else
     1689 +                                (void) printf("%*u", width, psinfo->pr_euid);
     1690 +                }
1641 1691                  break;
1642 1692          case F_RGROUP:
1643 1693                  if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
1644 1694                          (void) printf("%*s", width, grp->gr_name);
1645 1695                  else
1646 1696                          (void) printf("%*u", width, psinfo->pr_gid);
1647 1697                  break;
1648 1698          case F_GROUP:
1649 1699                  if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
1650 1700                          (void) printf("%*s", width, grp->gr_name);
↓ open down ↓ 221 lines elided ↑ open up ↑
1872 1922                  break;
1873 1923          case F_PROJID:
1874 1924                  (void) printf("%*d", width, (int)psinfo->pr_projid);
1875 1925                  break;
1876 1926          case F_PROJECT:
1877 1927                  {
1878 1928                          struct project cproj;
1879 1929                          char proj_buf[PROJECT_BUFSZ];
1880 1930  
1881 1931                          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 : "---");
     1932 +                            (void *)&proj_buf, PROJECT_BUFSZ)) == NULL) {
     1933 +                                if (Wflg && snprintf(NULL, 0, "%d",
     1934 +                                    ((int)psinfo->pr_projid)) > width)
     1935 +                                        (void) printf("%.*d%c", width - 1,
     1936 +                                            ((int)psinfo->pr_projid), '*');
     1937 +                                else
     1938 +                                        (void) printf("%*d", width,
     1939 +                                            (int)psinfo->pr_projid);
     1940 +                        } else {
     1941 +                                if (Wflg && cproj.pj_name != NULL &&
     1942 +                                    strlen(cproj.pj_name) > width)
     1943 +                                        (void) printf("%.*s%c", width - 1,
     1944 +                                            cproj.pj_name, '*');
     1945 +                                else
     1946 +                                        (void) printf("%*s", width,
     1947 +                                            (cproj.pj_name != NULL) ?
     1948 +                                            cproj.pj_name : "---");
     1949 +                        }
1889 1950                  }
1890 1951                  break;
1891 1952          case F_PSET:
1892 1953                  if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
1893 1954                          (void) printf("%*s", width, "-");
1894 1955                  else
1895 1956                          (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
1896 1957                  break;
1897 1958          case F_ZONEID:
1898 1959                  (void) printf("%*d", width, (int)psinfo->pr_zoneid);
1899 1960                  break;
1900 1961          case F_ZONE:
1901 1962                  {
1902 1963                          char zonename[ZONENAME_MAX];
1903 1964  
1904 1965                          if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1905 1966                              sizeof (zonename)) < 0) {
1906      -                                (void) printf("%*d", width,
1907      -                                    ((int)psinfo->pr_zoneid));
     1967 +                                if (Wflg && snprintf(NULL, 0, "%d",
     1968 +                                    ((int)psinfo->pr_zoneid)) > width)
     1969 +                                        (void) printf("%.*d%c", width - 1,
     1970 +                                            ((int)psinfo->pr_zoneid), '*');
     1971 +                                else
     1972 +                                        (void) printf("%*d", width,
     1973 +                                            (int)psinfo->pr_zoneid);
1908 1974                          } else {
1909      -                                (void) printf("%*s", width, zonename);
     1975 +                                if (Wflg && strlen(zonename) > width)
     1976 +                                        (void) printf("%.*s%c", width - 1,
     1977 +                                            zonename, '*');
     1978 +                                else
     1979 +                                        (void) printf("%*s", width, zonename);
1910 1980                          }
1911 1981                  }
1912 1982                  break;
1913 1983          case F_CTID:
1914 1984                  if (psinfo->pr_contract == -1)
1915 1985                          (void) printf("%*s", width, "-");
1916 1986                  else
1917 1987                          (void) printf("%*ld", width, (long)psinfo->pr_contract);
1918 1988                  break;
1919 1989          case F_LGRP:
↓ open down ↓ 254 lines elided ↑ open up ↑
2174 2244           * other fields are printed with a leading space.
2175 2245           */
2176 2246          if (lflg) {     /* F S */
2177 2247                  if (!yflg)
2178 2248                          (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
2179 2249                  (void) printf("%c ", psinfo->pr_lwp.pr_sname);  /* S */
2180 2250          }
2181 2251          if (Zflg) {
2182 2252                  if (getzonenamebyid(psinfo->pr_zoneid, zonename,
2183 2253                      sizeof (zonename)) < 0) {
2184      -                        (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
     2254 +                        if (snprintf(NULL, 0, "%d",
     2255 +                            ((int)psinfo->pr_zoneid)) > 7)
     2256 +                                (void) printf(" %6.6d%c ",
     2257 +                                    ((int)psinfo->pr_zoneid), '*');
     2258 +                        else
     2259 +                                (void) printf(" %7.7d ",
     2260 +                                    ((int)psinfo->pr_zoneid));
2185 2261                  } else {
2186      -                        (void) printf("%8.8s ", zonename);
     2262 +                        if (strlen(zonename) > 8)
     2263 +                                (void) printf("%7.7s%c ", zonename, '*');
     2264 +                        else
     2265 +                                (void) printf("%8.8s ", zonename);
2187 2266                  }
2188 2267          }
2189 2268          if (Hflg) {
2190 2269                  /* Display home lgroup */
2191 2270                  (void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
2192 2271          }
2193 2272          if (fflg) {
2194      -                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
2195      -                        (void) printf("%8.8s ", pwd->pw_name);
     2273 +                if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) {
     2274 +                        if (strlen(pwd->pw_name) > 8)
     2275 +                                (void) printf("%7.7s%c ", pwd->pw_name, '*');
     2276 +                        else
     2277 +                                (void) printf("%8.8s ", pwd->pw_name);
     2278 +                } else {
     2279 +                        if (snprintf(NULL, 0, "%u",
     2280 +                            ((int)psinfo->pr_euid)) > 7)
     2281 +                                (void) printf(" %6.6u%c ", psinfo->pr_euid,
     2282 +                                    '*');
     2283 +                        else
     2284 +                                (void) printf(" %7.7u ", psinfo->pr_euid);
     2285 +                }
     2286 +        } else if (lflg) {
     2287 +                if (snprintf(NULL, 0, "%u", ((int)psinfo->pr_euid)) > 6)
     2288 +                        (void) printf("%5.5u%c ", psinfo->pr_euid, '*');
2196 2289                  else
2197      -                        (void) printf(" %7.7u ", psinfo->pr_euid);
2198      -        } else if (lflg)
2199      -                (void) printf("%6u ", psinfo->pr_euid);
     2290 +                        (void) printf("%6u ", psinfo->pr_euid);
     2291 +        }
2200 2292  
2201 2293          (void) printf("%*d", pidwidth, (int)psinfo->pr_pid);    /* PID */
2202 2294          if (lflg || fflg)
2203 2295                  (void) printf(" %*d", pidwidth,
2204 2296                      (int)psinfo->pr_ppid);                      /* PPID */
2205 2297  
2206 2298          if (jflg) {
2207 2299                  (void) printf(" %*d", pidwidth,
2208 2300                      (int)psinfo->pr_pgid);                      /* PGID */
2209 2301                  (void) printf(" %*d", pidwidth,
↓ open down ↓ 186 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX