Print this page
8158 Want named threads API
9857 proc manpages should have LIBRARY section

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/ps/ps.c
          +++ new/usr/src/cmd/ps/ps.c
↓ open down ↓ 19 lines elided ↑ open up ↑
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2013 Gary Mills
  24   24   *
  25   25   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  26   26   * Use is subject to license terms.
  27   27   */
  28   28  
  29   29  /*
  30      - * Copyright (c) 2012, Joyent, Inc. All rights reserved.
       30 + * Copyright (c) 2018, Joyent, Inc.
  31   31   */
  32   32  
  33   33  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  34      -/*        All Rights Reserved   */
       34 +/*        All Rights Reserved   */
  35   35  
  36   36  /*
  37   37   * ps -- print things about processes.
  38   38   */
  39   39  #include <stdio.h>
  40   40  #include <ctype.h>
  41   41  #include <string.h>
  42   42  #include <errno.h>
  43   43  #include <fcntl.h>
  44   44  #include <pwd.h>
↓ open down ↓ 56 lines elided ↑ open up ↑
 101  101          F_UID,          /* numeric effective uid of the process */
 102  102          F_RUID,         /* numeric real uid of the process */
 103  103          F_GID,          /* numeric effective gid of the process */
 104  104          F_RGID,         /* numeric real gid of the process */
 105  105          F_PID,          /* process id */
 106  106          F_PPID,         /* parent process id */
 107  107          F_PGID,         /* process group id */
 108  108          F_SID,          /* session id */
 109  109          F_PSR,          /* bound processor */
 110  110          F_LWP,          /* lwp-id */
      111 +        F_LWPNAME,      /* lwp name */
 111  112          F_NLWP,         /* number of lwps */
 112  113          F_OPRI,         /* old priority (obsolete) */
 113  114          F_PRI,          /* new priority */
 114  115          F_F,            /* process flags */
 115  116          F_S,            /* letter indicating the state */
 116  117          F_C,            /* processor utilization (obsolete) */
 117  118          F_PCPU,         /* percent of recently used cpu time */
 118  119          F_PMEM,         /* percent of physical memory used (rss) */
 119  120          F_OSZ,          /* virtual size of the process in pages */
 120  121          F_VSZ,          /* virtual size of the process in kilobytes */
↓ open down ↓ 49 lines elided ↑ open up ↑
 170  171          { "uid",        "UID",          5,      5       },
 171  172          { "ruid",       "RUID",         5,      5       },
 172  173          { "gid",        "GID",          5,      5       },
 173  174          { "rgid",       "RGID",         5,      5       },
 174  175          { "pid",        "PID",          5,      5       },
 175  176          { "ppid",       "PPID",         5,      5       },
 176  177          { "pgid",       "PGID",         5,      5       },
 177  178          { "sid",        "SID",          5,      5       },
 178  179          { "psr",        "PSR",          3,      2       },
 179  180          { "lwp",        "LWP",          6,      2       },
      181 +        { "lwpname",    "LWPNAME",      32,     8       },
 180  182          { "nlwp",       "NLWP",         4,      2       },
 181  183          { "opri",       "PRI",          3,      2       },
 182  184          { "pri",        "PRI",          3,      2       },
 183  185          { "f",          "F",            2,      2       },
 184  186          { "s",          "S",            1,      1       },
 185  187          { "c",          "C",            2,      2       },
 186  188          { "pcpu",       "%CPU",         4,      4       },
 187  189          { "pmem",       "%MEM",         4,      4       },
 188  190          { "osz",        "SZ",           4,      4       },
 189  191          { "vsz",        "VSZ",          4,      4       },
↓ open down ↓ 14 lines elided ↑ open up ↑
 204  206          { "fname",      "COMMAND",      8,      8       },
 205  207          { "comm",       "COMMAND",      80,     8       },
 206  208          { "args",       "COMMAND",      80,     80      },
 207  209          { "taskid",     "TASKID",       5,      5       },
 208  210          { "projid",     "PROJID",       5,      5       },
 209  211          { "project",    "PROJECT",      8,      8       },
 210  212          { "pset",       "PSET",         3,      3       },
 211  213          { "zone",       "ZONE",         8,      8       },
 212  214          { "zoneid",     "ZONEID",       5,      5       },
 213  215          { "ctid",       "CTID",         5,      5       },
 214      -        { "lgrp",       "LGRP",         4,      2       },
 215      -        { "dmodel",     "DMODEL",       6,      6       },
      216 +        { "lgrp",       "LGRP",         4,      2       },
      217 +        { "dmodel",     "DMODEL",       6,      6       },
 216  218  };
 217  219  
 218  220  #define NFIELDS (sizeof (fname) / sizeof (fname[0]))
 219  221  
 220  222  static  int     retcode = 1;
 221  223  static  int     lflg;
 222  224  static  int     Aflg;
 223  225  static  int     uflg;
 224  226  static  int     Uflg;
 225  227  static  int     Gflg;
↓ open down ↓ 570 lines elided ↑ open up ↑
 796  798                          struct field *f;
 797  799  
 798  800                          for (f = fields; f != NULL; f = f->next) {
 799  801                                  if (f != fields)
 800  802                                          (void) printf(" ");
 801  803                                  switch (f->fname) {
 802  804                                  case F_TTY:
 803  805                                          (void) printf("%-*s",
 804  806                                              f->width, f->header);
 805  807                                          break;
      808 +                                case F_LWPNAME:
 806  809                                  case F_FNAME:
 807  810                                  case F_COMM:
 808  811                                  case F_ARGS:
 809  812                                          /*
 810  813                                           * Print these headers full width
 811  814                                           * unless they appear at the end.
 812  815                                           */
 813  816                                          if (f->next != NULL) {
 814  817                                                  (void) printf("%-*s",
 815  818                                                      f->width, f->header);
↓ open down ↓ 355 lines elided ↑ open up ↑
1171 1174                          width = strlen(header);
1172 1175                          s = header + width;
1173 1176                          while (s > header && isspace(*--s))
1174 1177                                  *s = '\0';
1175 1178                          while (isspace(*header))
1176 1179                                  header++;
1177 1180                  }
1178 1181          }
1179 1182          for (df = &fname[0]; df < &fname[NFIELDS]; df++)
1180 1183                  if (strcmp(name, df->fname) == 0) {
1181      -                        if (strcmp(name, "lwp") == 0)
     1184 +                        if (strcmp(name, "lwp") == 0 ||
     1185 +                            strcmp(name, "lwpname") == 0)
1182 1186                                  Lflg++;
1183 1187                          break;
1184 1188                  }
1185 1189          if (df >= &fname[NFIELDS]) {
1186 1190                  (void) fprintf(stderr,
1187 1191                      gettext("ps: unknown output format: -o %s\n"),
1188 1192                      name);
1189 1193                  errflg++;
1190 1194                  return (arg);
1191 1195          }
↓ open down ↓ 555 lines elided ↑ open up ↑
1747 1751                  break;
1748 1752          case F_PSR:
1749 1753                  if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE)
1750 1754                          (void) printf("%*s", width, "-");
1751 1755                  else
1752 1756                          (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro);
1753 1757                  break;
1754 1758          case F_LWP:
1755 1759                  (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid);
1756 1760                  break;
     1761 +        case F_LWPNAME: {
     1762 +                char lwpname[THREAD_NAME_MAX] = "";
     1763 +                char *path = NULL;
     1764 +                int fd;
     1765 +
     1766 +                if (asprintf(&path, "%s/%d/lwp/%d/lwpname", procdir,
     1767 +                    (int)psinfo->pr_pid, (int)psinfo->pr_lwp.pr_lwpid) != -1 &&
     1768 +                    (fd = open(path, O_RDONLY)) != -1) {
     1769 +                        (void) read(fd, lwpname, sizeof (lwpname));
     1770 +                        lwpname[THREAD_NAME_MAX - 1] = '\0';
     1771 +                        (void) close(fd);
     1772 +                }
     1773 +
     1774 +                free(path);
     1775 +
     1776 +                if (f->next != NULL)
     1777 +                        (void) printf("%-*s", width, lwpname);
     1778 +                else
     1779 +                        (void) printf("%s", lwpname);
     1780 +                break;
     1781 +        }
1757 1782          case F_NLWP:
1758 1783                  (void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb);
1759 1784                  break;
1760 1785          case F_OPRI:
1761 1786                  if (zombie_lwp)
1762 1787                          (void) printf("%*s", width, "-");
1763 1788                  else
1764 1789                          (void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri);
1765 1790                  break;
1766 1791          case F_PRI:
↓ open down ↓ 303 lines elided ↑ open up ↑
2070 2095                  break;
2071 2096  
2072 2097          default:
2073 2098                  print_field(psinfo, f, ttyp);
2074 2099                  break;
2075 2100          }
2076 2101  }
2077 2102  
2078 2103  static void
2079 2104  pr_fields(psinfo_t *psinfo, const char *ttyp,
2080      -        void (*print_fld)(psinfo_t *, struct field *, const char *))
     2105 +    void (*print_fld)(psinfo_t *, struct field *, const char *))
2081 2106  {
2082 2107          struct field *f;
2083 2108  
2084 2109          for (f = fields; f != NULL; f = f->next) {
2085 2110                  print_fld(psinfo, f, ttyp);
2086 2111                  if (f->next != NULL)
2087 2112                          (void) printf(" ");
2088 2113          }
2089 2114          (void) printf("\n");
2090 2115  }
↓ open down ↓ 269 lines elided ↑ open up ↑
2360 2385                              psinfo->pr_lwp.pr_oldpri, "-");     /* PRI NI */
2361 2386          }
2362 2387          if (lflg) {
2363 2388                  if (yflg)                               /* RSS SZ WCHAN */
2364 2389                          (void) printf(" %5d %6d %8s", 0, 0, "-");
2365 2390                  else                                    /* ADDR SZ WCHAN */
2366 2391                          (void) printf(" %8s %6d %8s", "-", 0, "-");
2367 2392          }
2368 2393          if (fflg) {
2369 2394                  int width = fname[F_STIME].width;
2370      -                (void) printf(" %*.*s", width, width, "-");     /* STIME */
     2395 +                (void) printf(" %*.*s", width, width, "-");     /* STIME */
2371 2396          }
2372 2397          (void) printf(" %-8.14s", "?");                         /* TTY */
2373 2398  
2374 2399          tm = psinfo->pr_time.tv_sec;
2375 2400          if (psinfo->pr_time.tv_nsec > 500000000)
2376 2401                  tm++;
2377 2402          (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* TIME */
2378 2403          (void) printf(" <defunct>\n");
2379 2404  }
2380 2405  
↓ open down ↓ 59 lines elided ↑ open up ↑
2440 2465                  seconds++;
2441 2466          else if (nanosecs < -(NANOSEC / 2))
2442 2467                  seconds--;
2443 2468  
2444 2469          return (seconds);
2445 2470  }
2446 2471  
2447 2472  /*
2448 2473   * Returns the following:
2449 2474   *
2450      - *      0       No error
2451      - *      EINVAL  Invalid number
2452      - *      ERANGE  Value exceeds (min, max) range
     2475 + *      0       No error
     2476 + *      EINVAL  Invalid number
     2477 + *      ERANGE  Value exceeds (min, max) range
2453 2478   */
2454 2479  static int
2455 2480  str2id(const char *p, pid_t *val, long min, long max)
2456 2481  {
2457 2482          char *q;
2458 2483          long number;
2459 2484          int error;
2460 2485  
2461 2486          errno = 0;
2462 2487          number = strtol(p, &q, 10);
↓ open down ↓ 14 lines elided ↑ open up ↑
2477 2502          }
2478 2503  
2479 2504          *val = number;
2480 2505  
2481 2506          return (error);
2482 2507  }
2483 2508  
2484 2509  /*
2485 2510   * Returns the following:
2486 2511   *
2487      - *      0       No error
2488      - *      EINVAL  Invalid number
2489      - *      ERANGE  Value exceeds (min, max) range
     2512 + *      0       No error
     2513 + *      EINVAL  Invalid number
     2514 + *      ERANGE  Value exceeds (min, max) range
2490 2515   */
2491 2516  static int
2492 2517  str2uid(const char *p, uid_t *val, unsigned long min, unsigned long max)
2493 2518  {
2494 2519          char *q;
2495 2520          unsigned long number;
2496 2521          int error;
2497 2522  
2498 2523          errno = 0;
2499 2524          number = strtoul(p, &q, 10);
↓ open down ↓ 29 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX