Print this page
    
OS-3280 need a way to specify the root of a native system in the lx brand
OS-3279 lx brand should allow delegated datasets
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/cmd/ptools/pwait/pwait.c
          +++ new/usr/src/cmd/ptools/pwait/pwait.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  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
  
    | 
      ↓ open down ↓ | 
    15 lines elided | 
    
      ↑ open up ↑ | 
  
  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   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  27      -
  28   26  #include <stdio.h>
  29   27  #include <stdio_ext.h>
  30   28  #include <ctype.h>
  31   29  #include <stdlib.h>
  32   30  #include <unistd.h>
  33   31  #include <fcntl.h>
  34   32  #include <string.h>
  35   33  #include <dirent.h>
  36   34  #include <errno.h>
  37   35  #include <sys/types.h>
  38   36  #include <stropts.h>
  39   37  #include <poll.h>
  40   38  #include <procfs.h>
  41   39  #include <sys/resource.h>
       40 +#include <limits.h>
       41 +#include "ptools_common.h"
  42   42  
  43   43  static int count_my_files();
  44   44  static char *command;
  45   45  
  46   46  /* slop to account for extra file descriptors opened by libraries we call */
  47   47  #define SLOP    5
  48   48  
  49   49  int
  50   50  main(int argc, char **argv)
  51   51  {
       52 +        char buf[PATH_MAX];
  52   53          unsigned long remain = 0;
  53   54          struct pollfd *pollfd;
  54   55          struct pollfd *pfd;
  55   56          struct rlimit rlim;
  56   57          char *arg;
  57   58          unsigned i;
  58   59          int verbose = 0;
  59   60  
  60   61          if ((command = strrchr(argv[0], '/')) != NULL)
  61   62                  command++;
  62   63          else
  63   64                  command = argv[0];
  64   65  
  65   66          argc--;
  66   67          argv++;
  67   68  
  
    | 
      ↓ open down ↓ | 
    6 lines elided | 
    
      ↑ open up ↑ | 
  
  68   69          if (argc > 0 && strcmp(argv[0], "-v") == 0) {
  69   70                  verbose = 1;
  70   71                  argc--;
  71   72                  argv++;
  72   73          }
  73   74  
  74   75          if (argc <= 0) {
  75   76                  (void) fprintf(stderr, "usage:\t%s [-v] pid ...\n", command);
  76   77                  (void) fprintf(stderr, "  (wait for processes to terminate)\n");
  77   78                  (void) fprintf(stderr,
  78      -                        "  -v: verbose; report terminations to standard out\n");
       79 +                    "  -v: verbose; report terminations to standard out\n");
  79   80                  return (2);
  80   81          }
  81   82  
       83 +        (void) proc_snprintf(buf, sizeof (buf), "/proc/");
       84 +
  82   85          /* make sure we have enough file descriptors */
  83   86          if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
  84   87                  int nfiles = count_my_files();
  85   88  
  86   89                  if (rlim.rlim_cur < argc + nfiles + SLOP) {
  87   90                          rlim.rlim_cur = argc + nfiles + SLOP;
  88   91                          if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
  89   92                                  (void) fprintf(stderr,
  90      -                                        "%s: insufficient file descriptors\n",
  91      -                                        command);
       93 +                                    "%s: insufficient file descriptors\n",
       94 +                                    command);
  92   95                                  return (2);
  93   96                          }
  94   97                  }
  95   98                  (void) enable_extended_FILE_stdio(-1, -1);
  96   99          }
  97  100  
  98  101          pollfd = (struct pollfd *)malloc(argc*sizeof (struct pollfd));
  99  102          if (pollfd == NULL) {
 100  103                  perror("malloc");
 101  104                  return (2);
 102  105          }
 103  106  
 104  107          for (i = 0; i < argc; i++) {
 105  108                  char psinfofile[100];
 106  109  
 107  110                  arg = argv[i];
 108  111                  if (strchr(arg, '/') != NULL)
 109  112                          (void) strncpy(psinfofile, arg, sizeof (psinfofile));
 110  113                  else {
 111      -                        (void) strcpy(psinfofile, "/proc/");
      114 +                        (void) strcpy(psinfofile, buf);
 112  115                          (void) strncat(psinfofile, arg, sizeof (psinfofile)-6);
 113  116                  }
 114  117                  (void) strncat(psinfofile, "/psinfo",
 115      -                        sizeof (psinfofile)-strlen(psinfofile));
      118 +                    sizeof (psinfofile)-strlen(psinfofile));
 116  119  
 117  120                  pfd = &pollfd[i];
 118  121                  if ((pfd->fd = open(psinfofile, O_RDONLY)) >= 0) {
 119  122                          remain++;
 120  123                          /*
 121  124                           * We set POLLPRI to detect system processes.
 122  125                           * We will get POLLNVAL below for a POLLPRI
 123  126                           * requested event on a system process.
 124  127                           */
 125  128                          pfd->events = POLLPRI;
 126  129                          pfd->revents = 0;
 127  130                  } else if (errno == ENOENT) {
 128  131                          (void) fprintf(stderr, "%s: no such process: %s\n",
 129      -                                command, arg);
      132 +                            command, arg);
 130  133                  } else {
 131  134                          perror(arg);
 132  135                  }
 133  136          }
 134  137  
 135  138          while (remain != 0) {
 136  139                  while (poll(pollfd, argc, INFTIM) < 0) {
 137  140                          if (errno != EAGAIN) {
 138  141                                  perror("poll");
 139  142                                  return (2);
 140  143                          }
 141  144                          (void) sleep(2);
 142  145                  }
 143  146                  for (i = 0; i < argc; i++) {
 144  147                          pfd = &pollfd[i];
 145  148                          if (pfd->fd < 0 || (pfd->revents & ~POLLPRI) == 0) {
 146  149                                  /*
 147  150                                   * We don't care if a non-system process
 148  151                                   * stopped.  Don't check for that again.
 149  152                                   */
 150  153                                  pfd->events = 0;
 151  154                                  pfd->revents = 0;
 152  155                                  continue;
  
    | 
      ↓ open down ↓ | 
    13 lines elided | 
    
      ↑ open up ↑ | 
  
 153  156                          }
 154  157  
 155  158                          if (verbose) {
 156  159                                  arg = argv[i];
 157  160                                  if (pfd->revents & POLLHUP) {
 158  161                                          psinfo_t psinfo;
 159  162  
 160  163                                          if (pread(pfd->fd, &psinfo,
 161  164                                              sizeof (psinfo), (off_t)0)
 162  165                                              == sizeof (psinfo)) {
 163      -                                                (void) printf(
 164      -                                        "%s: terminated, wait status 0x%.4x\n",
 165      -                                                        arg, psinfo.pr_wstat);
      166 +                                                (void) printf("%s: terminated, "
      167 +                                                    "wait status 0x%.4x\n",
      168 +                                                    arg, psinfo.pr_wstat);
 166  169                                          } else {
 167  170                                                  (void) printf(
 168  171                                                      "%s: terminated\n", arg);
 169  172                                          }
 170  173                                  }
 171  174                                  if (pfd->revents & POLLNVAL)
 172  175                                          (void) printf("%s: system process\n",
 173      -                                                arg);
      176 +                                            arg);
 174  177                                  if (pfd->revents & ~(POLLPRI|POLLHUP|POLLNVAL))
 175  178                                          (void) printf("%s: unknown error\n",
 176      -                                                arg);
      179 +                                            arg);
 177  180                          }
 178  181  
 179  182                          (void) close(pfd->fd);
 180  183                          pfd->fd = -1;
 181  184                          remain--;
 182  185                  }
 183  186          }
 184  187  
 185  188          return (0);
 186  189  }
 187  190  
 188  191  /* ARGSUSED1 */
 189  192  static int
 190  193  do_count(void *nofilesp, int fd)
 191  194  {
 192  195          (*(int *)nofilesp)++;
 193  196          return (0);
 194  197  }
 195  198  
 196  199  static int
 197  200  count_my_files()
 198  201  {
 199  202          int nofiles = 0;
 200  203  
 201  204          (void) fdwalk(do_count, &nofiles);
 202  205          return (nofiles);
 203  206  }
  
    | 
      ↓ open down ↓ | 
    17 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX