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>
       34 +#include <unistd.h>
  31   35  #include <sys/resource.h>
  32   36  #include <sys/lx_misc.h>
  33   37  #include <sys/lx_syscall.h>
  34   38  #include <lx_signum.h>
  35   39  
  36   40  /*
  37   41   * Translating from the Linux clock types to the Illumos types is a bit of a
  38   42   * mess.
  39   43   *
  40   44   * Linux uses different values for it clock identifiers, so we have to do basic
↓ open down ↓ 65 lines elided ↑ open up ↑
 106  110                  } lx_sigev_thread;
 107  111          } lx_sigev_un;
 108  112  } lx_sigevent_t;
 109  113  
 110  114  /* sigevent sigev_notify conversion table */
 111  115  static int ltos_sigev[] = {
 112  116          SIGEV_SIGNAL,
 113  117          SIGEV_NONE,
 114  118          SIGEV_THREAD,
 115  119          0,              /* Linux skips event 3 */
 116      -        SIGEV_THREAD    /* the Linux SIGEV_THREAD_ID */
      120 +        SIGEV_THREAD    /* Linux SIGEV_THREAD_ID -- see lx_sigev_thread_id() */
 117  121  };
 118  122  
 119      -#define LX_SIGEV_MAX    (sizeof (ltos_sigev) / sizeof (ltos_sigev[0]))
      123 +#define LX_SIGEV_MAX            (sizeof (ltos_sigev) / sizeof (ltos_sigev[0]))
      124 +#define LX_SIGEV_THREAD_ID      4
 120  125  
 121  126  long
 122  127  lx_clock_nanosleep(int clock, int flags, struct timespec *rqtp,
 123  128      struct timespec *rmtp)
 124  129  {
 125  130          int ret = 0;
 126  131          int err;
 127  132          struct timespec rqt, rmt;
 128  133  
 129  134          if (clock < 0 || clock >= LX_CLOCK_MAX)
↓ open down ↓ 26 lines elided ↑ open up ↑
 156  161  }
 157  162  
 158  163  /*ARGSUSED*/
 159  164  long
 160  165  lx_adjtimex(void *tp)
 161  166  {
 162  167          return (-EPERM);
 163  168  }
 164  169  
 165  170  /*
      171 + * Notification function for use with native SIGEV_THREAD in order to
      172 + * emulate Linux SIGEV_THREAD_ID. Native SIGEV_THREAD is used as the
      173 + * timer mechanism and B_SIGEV_THREAD_ID performs the actual event
      174 + * delivery to the appropriate lx tid.
      175 + */
      176 +static void
      177 +lx_sigev_thread_id(union sigval sival)
      178 +{
      179 +        lx_sigevent_t *lev = (lx_sigevent_t *)sival.sival_ptr;
      180 +        syscall(SYS_brand, B_SIGEV_THREAD_ID, lev->lx_sigev_un.lx_tid,
      181 +            lev->lx_sigev_signo, lev->lx_sigev_value);
      182 +        free(lev);
      183 +}
      184 +
      185 +
      186 +/*
 166  187   * The Illumos timer_create man page says it accepts the following clocks:
 167  188   *   CLOCK_REALTIME (3) wall clock
 168  189   *   CLOCK_VIRTUAL (1)  user CPU usage clock - No Backend
 169  190   *   CLOCK_PROF (2)     user and system CPU usage clock - No Backend
 170  191   *   CLOCK_HIGHRES (4)  non-adjustable, high-resolution clock
 171  192   * However, in reality the Illumos timer_create only accepts CLOCK_REALTIME
 172  193   * and CLOCK_HIGHRES, and since we can't use CLOCK_HIGHRES in a zone, we're
 173  194   * down to one clock.
 174  195   */
 175  196  long
↓ open down ↓ 10 lines elided ↑ open up ↑
 186  207                  return (-EFAULT);
 187  208  
 188  209          if (lev.lx_sigev_notify < 0 || lev.lx_sigev_notify > LX_SIGEV_MAX)
 189  210                  return (-EINVAL);
 190  211  
 191  212          sev.sigev_notify = ltos_sigev[lev.lx_sigev_notify];
 192  213          sev.sigev_signo = ltos_signo[lev.lx_sigev_signo];
 193  214          sev.sigev_value = lev.lx_sigev_value;
 194  215  
 195  216          /*
 196      -         * The sigevent sigev_notify_function and sigev_notify_attributes
 197      -         * members are not used by timer_create, so no conversion is needed.
      217 +         * Assume all Linux libc implementations map SIGEV_THREAD to
      218 +         * SIGEV_THREAD_ID and ignore passed-in attributes.
 198  219           */
      220 +        sev.sigev_notify_attributes = NULL;
 199  221  
      222 +        if (lev.lx_sigev_notify == LX_SIGEV_THREAD_ID) {
      223 +                pid_t caller_pid = getpid();
      224 +                pid_t target_pid;
      225 +                int rval;
      226 +
      227 +                if ((rval = lx_lpid_to_spair(lev.lx_sigev_un.lx_tid,
      228 +                            &target_pid, NULL)) != 0) {
      229 +
      230 +                        /*
      231 +                         * Attempt to stick to the defined ERRORS in
      232 +                         * timer_create(2).
      233 +                         */
      234 +                        if (rval == -ESRCH)
      235 +                                return (-EINVAL);
      236 +
      237 +                        return (rval);
      238 +                }
      239 +
      240 +                /*
      241 +                 * The caller of SIGEV_THREAD_ID must be in the same
      242 +                 * process as the target thread.
      243 +                 */
      244 +                if (caller_pid != target_pid)
      245 +                        return (-EINVAL);
      246 +
      247 +                /*
      248 +                 * Pass the original lx sigevent_t to the native
      249 +                 * notify function so that it may pass it to the lx
      250 +                 * helper thread. It is the responsibility of
      251 +                 * lx_sigev_thread_id() to free lev_copy after the
      252 +                 * information is relayed to lx.
      253 +                 */
      254 +                lx_sigevent_t *lev_copy = malloc(sizeof (lx_sigevent_t));
      255 +                if (lev_copy == NULL)
      256 +                        return (-ENOMEM);
      257 +
      258 +                if (uucopy(&lev, lev_copy, sizeof (lx_sigevent_t)) < 0) {
      259 +                        free(lev_copy);
      260 +                        return (-EFAULT);
      261 +                }
      262 +
      263 +                sev.sigev_notify_function = lx_sigev_thread_id;
      264 +                sev.sigev_value.sival_ptr = lev_copy;
      265 +        }
      266 +
 200  267          return ((timer_create(ltos_timer[clock], &sev, tid) < 0) ? -errno : 0);
 201  268  }
 202  269  
 203  270  long
 204  271  lx_timer_settime(timer_t tid, int flags, struct itimerspec *new_val,
 205  272      struct itimerspec *old_val)
 206  273  {
 207  274          return ((timer_settime(tid, flags, new_val, old_val) < 0) ? -errno : 0);
 208  275  }
 209  276  
↓ open down ↓ 20 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX