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/psig/psig.c
          +++ new/usr/src/cmd/ptools/psig/psig.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
  
    | 
      ↓ open down ↓ | 
    13 lines elided | 
    
      ↑ open up ↑ | 
  
  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   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
       24 + * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
  24   25   */
  25   26  
  26      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  27      -
  28   27  #include <stdio.h>
  29   28  #include <stdio_ext.h>
  30   29  #include <stdlib.h>
  31   30  #include <unistd.h>
  32   31  #include <ctype.h>
  33   32  #include <fcntl.h>
  34   33  #include <string.h>
  35   34  #include <signal.h>
  36   35  #include <stddef.h>
  37   36  #include <sys/types.h>
  38   37  #include <sys/stat.h>
  39   38  #include <libproc.h>
       39 +#include "ptools_common.h"
  40   40  
  41   41  /* evil knowledge of libc internals */
  42   42  #include "../../../lib/libc/inc/thr_uberdata.h"
  43   43  
  44   44  #define MAX_SYMNAMLEN   1024    /* Recommended max symbol name length */
  45   45  
  46   46  static  char    *sigflags(int, int);
  47   47  static  int     look(char *);
  48   48  static  void    perr(char *);
  49   49  static  int     usage(void);
  50   50  static  uintptr_t deinterpose(int, void *, psinfo_t *, struct sigaction *);
  51   51  
  52   52  static  char    *command;
  53   53  static  char    *procname;
  54   54  static  int     all_flag = 0;
  55   55  static  int     lookuphandlers_flag = 1;
  56   56  
  57   57  int
  58   58  main(int argc, char **argv)
  59   59  {
  60   60          int rc = 0;
  61   61          int c;
  62   62          struct rlimit rlim;
  63   63  
  64   64          if ((command = strrchr(argv[0], '/')) != NULL)
  65   65                  command++;
  66   66          else
  67   67                  command = argv[0];
  68   68  
  69   69          while ((c = getopt(argc, argv, "an")) != EOF) {
  70   70                  switch (c) {
  71   71                  case 'a':
  72   72                          all_flag = 1;
  73   73                          break;
  74   74                  case 'n':
  75   75                          lookuphandlers_flag = 0;
  76   76                          break;
  77   77                  default:
  78   78                          return (usage());
  79   79                  }
  80   80          }
  81   81  
  82   82          if (argc - optind < 1) {
  83   83                  return (usage());
  84   84          }
  85   85  
  86   86          /*
  87   87           * Make sure we'll have enough file descriptors to handle a target
  88   88           * that has many many mappings.
  89   89           */
  90   90          if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
  91   91                  rlim.rlim_cur = rlim.rlim_max;
  92   92                  (void) setrlimit(RLIMIT_NOFILE, &rlim);
  93   93                  (void) enable_extended_FILE_stdio(-1, -1);
  94   94          }
  95   95  
  96   96          for (; optind != argc; optind++) {
  97   97                  rc += look(argv[optind]);
  98   98          }
  99   99  
 100  100          return (rc);
 101  101  }
 102  102  
 103  103  static int
 104  104  usage(void)
 105  105  {
 106  106          (void) fprintf(stderr, "usage:\t%s [-n] pid ...\n", command);
 107  107          (void) fprintf(stderr, "  (report process signal actions)\n");
 108  108  
 109  109          return (2);
 110  110  }
 111  111  
 112  112  static uintptr_t
 113  113  uberdata_addr(struct ps_prochandle *Pr, char dmodel)
 114  114  {
 115  115          GElf_Sym sym;
 116  116  
 117  117          if (Plookup_by_name(Pr, "libc.so", "_tdb_bootstrap", &sym) < 0)
 118  118                  return (NULL);
 119  119  #ifdef _LP64
 120  120          if (dmodel != PR_MODEL_NATIVE) {
 121  121                  caddr32_t uaddr;
 122  122                  caddr32_t addr;
 123  123  
 124  124                  if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
 125  125                      == sizeof (addr) &&
 126  126                      addr != 0 &&
 127  127                      Pread(Pr, &uaddr, sizeof (uaddr), (uintptr_t)addr)
 128  128                      == sizeof (uaddr) &&
 129  129                      uaddr != 0)
 130  130                          return ((uintptr_t)uaddr);
 131  131          }
 132  132  #endif
 133  133          if (dmodel == PR_MODEL_NATIVE) {
 134  134                  uintptr_t uaddr;
 135  135                  uintptr_t addr;
 136  136  
 137  137                  if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
 138  138                      == sizeof (addr) &&
 139  139                      addr != 0 &&
 140  140                      Pread(Pr, &uaddr, sizeof (uaddr), addr)
 141  141                      == sizeof (uaddr) &&
 142  142                      uaddr != 0)
 143  143                          return (uaddr);
 144  144          }
 145  145          if (Plookup_by_name(Pr, "libc.so", "_uberdata", &sym) < 0)
 146  146                  return (0);
 147  147          return (sym.st_value);
 148  148  }
 149  149  
 150  150  /*
 151  151   * Iterator function used to generate the process sigmask
 152  152   * from the individual lwp sigmasks.
 153  153   */
 154  154  static int
 155  155  lwp_iter(void *cd, const lwpstatus_t *lwpstatus)
 156  156  {
 157  157          sigset_t *ssp = cd;
 158  158  
 159  159          ssp->__sigbits[0] &= lwpstatus->pr_lwphold.__sigbits[0];
 160  160          ssp->__sigbits[1] &= lwpstatus->pr_lwphold.__sigbits[1];
 161  161          ssp->__sigbits[2] &= lwpstatus->pr_lwphold.__sigbits[2];
 162  162          ssp->__sigbits[3] &= lwpstatus->pr_lwphold.__sigbits[3];
 163  163  
 164  164          /*
  
    | 
      ↓ open down ↓ | 
    115 lines elided | 
    
      ↑ open up ↑ | 
  
 165  165           * Return non-zero to terminate the iteration
 166  166           * if the sigmask has become all zeros.
 167  167           */
 168  168          return ((ssp->__sigbits[0] | ssp->__sigbits[1] |
 169  169              ssp->__sigbits[2] | ssp->__sigbits[3]) == 0);
 170  170  }
 171  171  
 172  172  static int
 173  173  look(char *arg)
 174  174  {
 175      -        char pathname[100];
      175 +        char pathname[PATH_MAX];
 176  176          struct stat statb;
 177  177          int fd = -1;
 178  178          int sig, gcode;
 179  179          sigset_t holdmask;
 180  180          int maxsig;
 181  181          struct sigaction *action = NULL;
 182  182          psinfo_t psinfo;
 183  183          const psinfo_t *psinfop;
 184  184          struct ps_prochandle *Pr = NULL;
 185  185          uintptr_t uberaddr;
 186  186          uintptr_t aharraddr;
 187  187          uintptr_t intfnaddr;
 188  188          size_t aharrlen;
 189  189          void *aharr = NULL;
 190  190          int error = 1;
 191  191  
  
    | 
      ↓ open down ↓ | 
    6 lines elided | 
    
      ↑ open up ↑ | 
  
 192  192          procname = arg;         /* for perr() */
 193  193          if ((Pr = proc_arg_grab(arg, PR_ARG_PIDS, PGRAB_RDONLY|PGRAB_FORCE,
 194  194              &gcode)) == NULL || (psinfop = Ppsinfo(Pr)) == NULL) {
 195  195                  (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
 196  196                      command, arg, Pgrab_error(gcode));
 197  197                  goto look_error;
 198  198          }
 199  199          (void) memcpy(&psinfo, psinfop, sizeof (psinfo_t));
 200  200          proc_unctrl_psinfo(&psinfo);
 201  201  
 202      -        (void) sprintf(pathname, "/proc/%d/sigact", (int)psinfo.pr_pid);
      202 +        (void) proc_snprintf(pathname, sizeof (pathname), "/proc/%d/sigact",
      203 +            (int)psinfo.pr_pid);
 203  204          if ((fd = open(pathname, O_RDONLY)) < 0) {
 204  205                  perr("open sigact");
 205  206                  goto look_error;
 206  207          }
 207  208  
 208  209          if (fstat(fd, &statb) != 0) {
 209  210                  perr("fstat sigact");
 210  211                  goto look_error;
 211  212          }
 212  213          maxsig = statb.st_size / sizeof (struct sigaction);
 213  214          action = malloc(maxsig * sizeof (struct sigaction));
 214  215          if (action == NULL) {
 215  216                  (void) fprintf(stderr,
 216      -                "%s: cannot malloc() space for %d sigaction structures\n",
 217      -                        command, maxsig);
      217 +                    "%s: cannot malloc() space for %d sigaction structures\n",
      218 +                    command, maxsig);
 218  219                  goto look_error;
 219  220          }
 220  221          if (read(fd, (char *)action, maxsig * sizeof (struct sigaction)) !=
 221  222              maxsig * sizeof (struct sigaction)) {
 222  223                  perr("read sigact");
 223  224                  goto look_error;
 224  225          }
 225  226          (void) close(fd);
 226  227          fd = -1;
 227  228  
 228  229          (void) printf("%d:\t%.70s\n", (int)psinfo.pr_pid, psinfo.pr_psargs);
 229  230  
 230  231          (void) sigfillset(&holdmask);
 231  232          (void) Plwp_iter(Pr, lwp_iter, &holdmask);
  
    | 
      ↓ open down ↓ | 
    4 lines elided | 
    
      ↑ open up ↑ | 
  
 232  233  
 233  234          if ((uberaddr = uberdata_addr(Pr, psinfo.pr_dmodel)) == 0) {
 234  235                  aharraddr = 0;
 235  236                  aharrlen = 0;
 236  237                  intfnaddr = 0;
 237  238          } else {
 238  239  #ifdef _LP64
 239  240                  if (psinfo.pr_dmodel != PR_MODEL_NATIVE) {
 240  241                          caddr32_t addr;
 241  242                          aharraddr = uberaddr +
 242      -                                offsetof(uberdata32_t, siguaction);
      243 +                            offsetof(uberdata32_t, siguaction);
 243  244                          aharrlen = sizeof (siguaction32_t) * NSIG;
 244  245                          (void) Pread(Pr, &addr, sizeof (addr),
 245  246                              uberaddr + offsetof(uberdata32_t, sigacthandler));
 246  247                          intfnaddr = (uintptr_t)addr;
 247  248                  } else
 248  249  #endif
 249  250                  {
 250  251                          aharraddr = uberaddr +
 251      -                                offsetof(uberdata_t, siguaction);
      252 +                            offsetof(uberdata_t, siguaction);
 252  253                          aharrlen = sizeof (siguaction_t) * NSIG;
 253  254                          (void) Pread(Pr, &intfnaddr, sizeof (intfnaddr),
 254  255                              uberaddr + offsetof(uberdata_t, sigacthandler));
 255  256                  }
 256  257          }
 257  258  
 258  259          if (aharraddr) {
 259  260                  aharr = malloc(aharrlen);
 260  261                  if (aharr == NULL) {
 261  262                          (void) fprintf(stderr,
 262  263                          "%s: cannot malloc() space for actual handler array\n",
 263  264                              command);
 264  265                          goto look_error;
 265  266                  }
 266  267  
 267  268                  if (Pread(Pr, aharr, aharrlen, aharraddr) != aharrlen) {
 268  269                          (void) fprintf(stderr,
 269  270                              "%s: signal handler data at %p cannot be read.\n",
 270  271                              command, (void *)aharraddr);
 271  272                          free(aharr);
 272  273                          aharr = NULL;
 273  274                  }
 274  275          }
 275  276  
 276  277          for (sig = 1; sig <= maxsig; sig++) {
 277  278                  struct sigaction *sp = &action[sig - 1];
 278  279                  int caught = 0;
 279  280                  char buf[SIG2STR_MAX];
 280  281                  char *s;
 281  282  
 282  283                  /* proc_signame() returns "SIG..."; skip the "SIG" part */
 283  284                  (void) printf("%s\t", proc_signame(sig, buf, sizeof (buf)) + 3);
 284  285  
 285  286                  if (prismember(&holdmask, sig))
 286  287                          (void) printf("blocked,");
 287  288  
 288  289                  if (sp->sa_handler == SIG_DFL)
 289  290                          (void) printf("default");
 290  291                  else if (sp->sa_handler == SIG_IGN)
 291  292                          (void) printf("ignored");
 292  293                  else
 293  294                          caught = 1;
 294  295  
 295  296                  if (caught || all_flag) {
 296  297                          uintptr_t haddr;
 297  298                          GElf_Sym hsym;
 298  299                          char hname[MAX_SYMNAMLEN];
 299  300                          char buf[PRSIGBUFSZ];
 300  301  
 301  302                          haddr = (uintptr_t)sp->sa_handler;
 302  303  
 303  304                          if (aharr && intfnaddr && haddr == intfnaddr)
 304  305                                  haddr = deinterpose(sig, aharr, &psinfo, sp);
 305  306  
 306  307                          if (haddr == (uintptr_t)SIG_DFL) {
 307  308                                  if (caught)
 308  309                                          (void) printf("default");
 309  310                                  caught = 0;
 310  311                          } else if (haddr == (uintptr_t)SIG_IGN) {
 311  312                                  if (caught)
 312  313                                          (void) printf("ignored");
 313  314                                  caught = 0;
 314  315                          } else {
 315  316                                  if (caught)
  
    | 
      ↓ open down ↓ | 
    54 lines elided | 
    
      ↑ open up ↑ | 
  
 316  317                                          (void) printf("caught");
 317  318                          }
 318  319  
 319  320                          if (caught || all_flag) {
 320  321                                  if (lookuphandlers_flag && haddr > 1 &&
 321  322                                      Plookup_by_addr(Pr, haddr, hname,
 322  323                                      sizeof (hname), &hsym) == 0)
 323  324                                          (void) printf("\t%-8s", hname);
 324  325                                  else
 325  326                                          (void) printf("\t0x%-8lx",
 326      -                                                (ulong_t)haddr);
      327 +                                            (ulong_t)haddr);
 327  328  
 328  329                                  s = sigflags(sig, sp->sa_flags);
 329  330                                  (void) printf("%s", (*s != '\0')? s : "\t0");
 330  331                                  (void) proc_sigset2str(&sp->sa_mask, ",", 1,
 331  332                                      buf, sizeof (buf));
 332  333                                  if (buf[0] != '\0')
 333  334                                          (void) printf("\t%s", buf);
 334  335                          }
 335  336                  } else if (sig == SIGCLD) {
 336  337                          s = sigflags(sig,
 337      -                                sp->sa_flags & (SA_NOCLDWAIT|SA_NOCLDSTOP));
      338 +                            sp->sa_flags & (SA_NOCLDWAIT|SA_NOCLDSTOP));
 338  339                          if (*s != '\0')
 339  340                                  (void) printf("\t\t%s", s);
 340  341                  }
 341  342                  (void) printf("\n");
 342  343          }
 343  344  
 344  345          error = 0;
 345  346  
 346  347  look_error:
 347  348          if (fd >= 0)
 348  349                  (void) close(fd);
 349  350          if (aharr)
 350  351                  free(aharr);
 351  352          if (action)
 352  353                  free(action);
 353  354          if (Pr)
 354  355                  Prelease(Pr, 0);
 355  356          return (error);
 356  357  }
 357  358  
 358  359  static void
 359  360  perr(char *s)
 360  361  {
 361  362          if (s)
 362  363                  (void) fprintf(stderr, "%s: ", procname);
 363  364          else
  
    | 
      ↓ open down ↓ | 
    16 lines elided | 
    
      ↑ open up ↑ | 
  
 364  365                  s = procname;
 365  366          perror(s);
 366  367  }
 367  368  
 368  369  static char *
 369  370  sigflags(int sig, int flags)
 370  371  {
 371  372          static char code_buf[100];
 372  373          char *str = code_buf;
 373  374          int flagmask =
 374      -                (SA_ONSTACK|SA_RESETHAND|SA_RESTART|SA_SIGINFO|SA_NODEFER);
      375 +            (SA_ONSTACK|SA_RESETHAND|SA_RESTART|SA_SIGINFO|SA_NODEFER);
 375  376  
 376  377          if (sig == SIGCLD)
 377  378                  flagmask |= (SA_NOCLDSTOP|SA_NOCLDWAIT);
 378  379  
 379  380          *str = '\0';
 380  381          if (flags & ~flagmask)
 381  382                  (void) sprintf(str, ",0x%x,", flags & ~flagmask);
 382  383          else if (flags == 0)
 383  384                  return (str);
 384  385  
 385  386          if (flags & SA_RESTART)
 386  387                  (void) strcat(str, ",RESTART");
 387  388          if (flags & SA_RESETHAND)
 388  389                  (void) strcat(str, ",RESETHAND");
 389  390          if (flags & SA_ONSTACK)
 390  391                  (void) strcat(str, ",ONSTACK");
 391  392          if (flags & SA_SIGINFO)
 392  393                  (void) strcat(str, ",SIGINFO");
 393  394          if (flags & SA_NODEFER)
 394  395                  (void) strcat(str, ",NODEFER");
 395  396  
 396  397          if (sig == SIGCLD) {
 397  398                  if (flags & SA_NOCLDWAIT)
 398  399                          (void) strcat(str, ",NOCLDWAIT");
 399  400                  if (flags & SA_NOCLDSTOP)
 400  401                          (void) strcat(str, ",NOCLDSTOP");
 401  402          }
 402  403  
 403  404          *str = '\t';
 404  405  
 405  406          return (str);
 406  407  }
  
    | 
      ↓ open down ↓ | 
    22 lines elided | 
    
      ↑ open up ↑ | 
  
 407  408  
 408  409  /*ARGSUSED2*/
 409  410  static uintptr_t
 410  411  deinterpose(int sig, void *aharr, psinfo_t *psinfo, struct sigaction *sp)
 411  412  {
 412  413          if (sp->sa_handler == SIG_DFL || sp->sa_handler == SIG_IGN)
 413  414                  return ((uintptr_t)sp->sa_handler);
 414  415  #ifdef _LP64
 415  416          if (psinfo->pr_dmodel != PR_MODEL_NATIVE) {
 416  417                  struct sigaction32 *sa32 = (struct sigaction32 *)
 417      -                        ((uintptr_t)aharr + sig * sizeof (siguaction32_t) +
 418      -                        offsetof(siguaction32_t, sig_uaction));
      418 +                    ((uintptr_t)aharr + sig * sizeof (siguaction32_t) +
      419 +                    offsetof(siguaction32_t, sig_uaction));
 419  420  
 420  421                  sp->sa_flags = sa32->sa_flags;
 421  422                  sp->sa_handler = (void (*)())(uintptr_t)sa32->sa_handler;
 422  423                  (void) memcpy(&sp->sa_mask, &sa32->sa_mask,
 423      -                        sizeof (sp->sa_mask));
      424 +                    sizeof (sp->sa_mask));
 424  425          } else
 425  426  #endif
 426  427          {
 427  428                  struct sigaction *sa = (struct sigaction *)
 428      -                        ((uintptr_t)aharr + sig * sizeof (siguaction_t) +
 429      -                        offsetof(siguaction_t, sig_uaction));
      429 +                    ((uintptr_t)aharr + sig * sizeof (siguaction_t) +
      430 +                    offsetof(siguaction_t, sig_uaction));
 430  431  
 431  432                  sp->sa_flags = sa->sa_flags;
 432  433                  sp->sa_handler = sa->sa_handler;
 433  434                  sp->sa_mask = sa->sa_mask;
 434  435          }
 435  436          return ((uintptr_t)sp->sa_handler);
 436  437  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX