Print this page
OS-4514 [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 ↓ 5 lines elided ↑ open up ↑
 181  202          if (clock < 0 || clock >= LX_TIMER_MAX)
 182  203                  return (-EINVAL);
 183  204  
 184  205          /* We have to convert the Linux sigevent layout to the Illumos layout */
 185  206          if (uucopy(lx_sevp, &lev, sizeof (lev)) < 0)
 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  
      212 +        if ((lev.lx_sigev_signo < 0) || (lev.lx_sigev_signo > LX_NSIG))
      213 +                return (-EINVAL);
      214 +
 191  215          sev.sigev_notify = ltos_sigev[lev.lx_sigev_notify];
 192      -        sev.sigev_signo = ltos_signo[lev.lx_sigev_signo];
      216 +        sev.sigev_signo = lx_ltos_signo(lev.lx_sigev_signo, -1);
 193  217          sev.sigev_value = lev.lx_sigev_value;
 194  218  
 195  219          /*
 196      -         * The sigevent sigev_notify_function and sigev_notify_attributes
 197      -         * members are not used by timer_create, so no conversion is needed.
      220 +         * Assume all Linux libc implementations map SIGEV_THREAD to
      221 +         * SIGEV_THREAD_ID and ignore passed-in attributes.
 198  222           */
      223 +        sev.sigev_notify_attributes = NULL;
 199  224  
      225 +        if (lev.lx_sigev_notify == LX_SIGEV_THREAD_ID) {
      226 +                pid_t caller_pid = getpid();
      227 +                pid_t target_pid;
      228 +                lwpid_t ignore;
      229 +                int rval;
      230 +
      231 +                if ((rval = lx_lpid_to_spair(lev.lx_sigev_un.lx_tid,
      232 +                            &target_pid, &ignore)) != 0) {
      233 +
      234 +                        /*
      235 +                         * Attempt to stick to the defined ERRORS in
      236 +                         * timer_create(2).
      237 +                         */
      238 +                        if (rval == -ESRCH)
      239 +                                return (-EINVAL);
      240 +
      241 +                        return (rval);
      242 +                }
      243 +
      244 +                /*
      245 +                 * The caller of SIGEV_THREAD_ID must be in the same
      246 +                 * process as the target thread.
      247 +                 */
      248 +                if (caller_pid != target_pid)
      249 +                        return (-EINVAL);
      250 +
      251 +                /*
      252 +                 * Pass the original lx sigevent_t to the native
      253 +                 * notify function so that it may pass it to the lx
      254 +                 * helper thread. It is the responsibility of
      255 +                 * lx_sigev_thread_id() to free lev_copy after the
      256 +                 * information is relayed to lx.
      257 +                 */
      258 +                lx_sigevent_t *lev_copy = malloc(sizeof (lx_sigevent_t));
      259 +                if (lev_copy == NULL)
      260 +                        return (-ENOMEM);
      261 +
      262 +                if (uucopy(&lev, lev_copy, sizeof (lx_sigevent_t)) < 0) {
      263 +                        free(lev_copy);
      264 +                        return (-EFAULT);
      265 +                }
      266 +
      267 +                sev.sigev_notify_function = lx_sigev_thread_id;
      268 +                sev.sigev_value.sival_ptr = lev_copy;
      269 +        }
      270 +
 200  271          return ((timer_create(ltos_timer[clock], &sev, tid) < 0) ? -errno : 0);
 201  272  }
 202  273  
 203  274  long
 204  275  lx_timer_settime(timer_t tid, int flags, struct itimerspec *new_val,
 205  276      struct itimerspec *old_val)
 206  277  {
 207  278          return ((timer_settime(tid, flags, new_val, old_val) < 0) ? -errno : 0);
 208  279  }
 209  280  
↓ open down ↓ 20 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX