Print this page
OS-???? [lx] SIGEV_THREAD_ID emulation needed

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/brand/lx/lx_brand/common/clock.c
          +++ new/usr/src/lib/brand/lx/lx_brand/common/clock.c
↓ open down ↓ 17 lines elided ↑ open up ↑
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   * Copyright 2015 Joyent, Inc.
  26   26   */
  27   27  
       28 +#include <sys/syscall.h>
       29 +
  28   30  #include <errno.h>
       31 +#include <stdlib.h>
  29   32  #include <string.h>
  30   33  #include <time.h>
  31   34  #include <sys/resource.h>
  32   35  #include <sys/lx_misc.h>
  33   36  #include <sys/lx_syscall.h>
  34   37  #include <lx_signum.h>
  35   38  
  36   39  /*
  37   40   * Translating from the Linux clock types to the Illumos types is a bit of a
  38   41   * mess.
↓ open down ↓ 61 lines elided ↑ open up ↑
 100  103          union {
 101  104                  int     lx_pad[LX_SIGEV_PAD_SIZE];
 102  105                  int     lx_tid;
 103  106                  struct {
 104  107                          void (*lx_notify_function)(union sigval);
 105  108                          void *lx_notify_attribute;
 106  109                  } lx_sigev_thread;
 107  110          } lx_sigev_un;
 108  111  } lx_sigevent_t;
 109  112  
      113 +#define lx_sigev_notify_attributes      lx_sigev_un.lx_sigev_thread.lx_notify_attribute
      114 +
 110  115  /* sigevent sigev_notify conversion table */
 111  116  static int ltos_sigev[] = {
 112  117          SIGEV_SIGNAL,
 113  118          SIGEV_NONE,
 114  119          SIGEV_THREAD,
 115  120          0,              /* Linux skips event 3 */
 116      -        SIGEV_THREAD    /* the Linux SIGEV_THREAD_ID */
      121 +        SIGEV_THREAD    /* Linux SIGEV_THREAD_ID -- see lx_sigev_thread_id() */
 117  122  };
 118  123  
 119      -#define LX_SIGEV_MAX    (sizeof (ltos_sigev) / sizeof (ltos_sigev[0]))
      124 +#define LX_SIGEV_MAX            (sizeof (ltos_sigev) / sizeof (ltos_sigev[0]))
      125 +#define LX_SIGEV_THREAD_ID      4
 120  126  
 121  127  long
 122  128  lx_clock_nanosleep(int clock, int flags, struct timespec *rqtp,
 123  129      struct timespec *rmtp)
 124  130  {
 125  131          int ret = 0;
 126  132          int err;
 127  133          struct timespec rqt, rmt;
 128  134  
 129  135          if (clock < 0 || clock >= LX_CLOCK_MAX)
↓ open down ↓ 26 lines elided ↑ open up ↑
 156  162  }
 157  163  
 158  164  /*ARGSUSED*/
 159  165  long
 160  166  lx_adjtimex(void *tp)
 161  167  {
 162  168          return (-EPERM);
 163  169  }
 164  170  
 165  171  /*
      172 + * Notification function for use with native SIGEV_THREAD in order to
      173 + * emulate Linux SIGEV_THREAD_ID. Native SIGEV_THREAD is used as the
      174 + * timer mechanism and B_SIGEV_THREAD_ID performs the actual event
      175 + * delivery to the appropriate lx tid.
      176 + */
      177 +static void
      178 +lx_sigev_thread_id(union sigval sival)
      179 +{
      180 +        lx_sigevent_t *lev = (lx_sigevent_t *)sival.sival_ptr;
      181 +        syscall(SYS_brand, B_SIGEV_THREAD_ID, lev->lx_sigev_un.lx_tid,
      182 +            lev->lx_sigev_signo, lev->lx_sigev_value);
      183 +        free(lev);
      184 +}
      185 +
      186 +
      187 +/*
 166  188   * The Illumos timer_create man page says it accepts the following clocks:
 167  189   *   CLOCK_REALTIME (3) wall clock
 168  190   *   CLOCK_VIRTUAL (1)  user CPU usage clock - No Backend
 169  191   *   CLOCK_PROF (2)     user and system CPU usage clock - No Backend
 170  192   *   CLOCK_HIGHRES (4)  non-adjustable, high-resolution clock
 171  193   * However, in reality the Illumos timer_create only accepts CLOCK_REALTIME
 172  194   * and CLOCK_HIGHRES, and since we can't use CLOCK_HIGHRES in a zone, we're
 173  195   * down to one clock.
 174  196   */
 175  197  long
↓ open down ↓ 10 lines elided ↑ open up ↑
 186  208                  return (-EFAULT);
 187  209  
 188  210          if (lev.lx_sigev_notify < 0 || lev.lx_sigev_notify > LX_SIGEV_MAX)
 189  211                  return (-EINVAL);
 190  212  
 191  213          sev.sigev_notify = ltos_sigev[lev.lx_sigev_notify];
 192  214          sev.sigev_signo = ltos_signo[lev.lx_sigev_signo];
 193  215          sev.sigev_value = lev.lx_sigev_value;
 194  216  
 195  217          /*
 196      -         * The sigevent sigev_notify_function and sigev_notify_attributes
 197      -         * members are not used by timer_create, so no conversion is needed.
      218 +         * Assume all Linux libc implementations map SIGEV_THREAD to
      219 +         * SIGEV_THREAD_ID and ignore passed-in attributes.
 198  220           */
      221 +        sev.sigev_notify_attributes = NULL;
 199  222  
      223 +        if (lev.lx_sigev_notify == LX_SIGEV_THREAD_ID) {
      224 +                /*
      225 +                 * Pass the original lx sigevent_t to the native
      226 +                 * notify function so that it may pass it to the lx
      227 +                 * helper thread.
      228 +                 */
      229 +                lx_sigevent_t *lev_copy = malloc(sizeof (lx_sigevent_t));
      230 +                if (lev_copy == NULL)
      231 +                        return (-ENOMEM);
      232 +
      233 +                if (uucopy(&lev, lev_copy, sizeof (lx_sigevent_t)) < 0)
      234 +                        return (-EFAULT);
      235 +
      236 +                sev.sigev_notify_function = lx_sigev_thread_id;
      237 +                sev.sigev_value.sival_ptr = lev_copy;
      238 +        }
      239 +
 200  240          return ((timer_create(ltos_timer[clock], &sev, tid) < 0) ? -errno : 0);
 201  241  }
 202  242  
 203  243  long
 204  244  lx_timer_settime(timer_t tid, int flags, struct itimerspec *new_val,
 205  245      struct itimerspec *old_val)
 206  246  {
 207  247          return ((timer_settime(tid, flags, new_val, old_val) < 0) ? -errno : 0);
 208  248  }
 209  249  
↓ open down ↓ 20 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX