hotspot/src/os/linux/vm/jsig.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2006 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
/* CopyrightVersion 1.2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
/* This is a special library that should be loaded before libc &
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
 * libthread to interpose the signal handler installation functions:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
 * sigaction(), signal(), sigset().
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
 * Used for signal-chaining. See RFE 4381843.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#include <signal.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
#include <dlfcn.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#include <pthread.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#include <stdio.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#include <stdlib.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#define bool int
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#define true 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#define false 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
#define MAXSIGNUM 32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
#define MASK(sig) ((unsigned int)1 << sig)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
static struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
static unsigned int jvmsigs = 0; /* signals used by jvm */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
/* used to synchronize the installation of signal handlers */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
static pthread_t tid = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
typedef void (*sa_handler_t)(int);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
typedef sa_handler_t (*signal_t)(int, sa_handler_t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
static signal_t os_signal = 0; /* os's version of signal()/sigset() */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
static bool jvm_signal_installing = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
static bool jvm_signal_installed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
static void signal_lock() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  pthread_mutex_lock(&mutex);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  /* When the jvm is installing its set of signal handlers, threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
   * other than the jvm thread should wait */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  if (jvm_signal_installing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    if (tid != pthread_self()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      pthread_cond_wait(&cond, &mutex);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
static void signal_unlock() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  pthread_mutex_unlock(&mutex);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                                   bool is_sigset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  if (os_signal == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    if (!is_sigset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
      os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    if (os_signal == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      printf("%s\n", dlerror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
      exit(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  return (*os_signal)(sig, disp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
static void save_signal_handler(int sig, sa_handler_t disp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  sigset_t set;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  sact[sig].sa_handler = disp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  sigemptyset(&set);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  sact[sig].sa_mask = set;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  sact[sig].sa_flags = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  sa_handler_t oldhandler;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  bool sigused;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  signal_lock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  sigused = (MASK(sig) & jvmsigs) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  if (jvm_signal_installed && sigused) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    /* jvm has installed its signal handler for this signal. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    /* Save the handler. Don't really install it. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    oldhandler = sact[sig].sa_handler;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    save_signal_handler(sig, disp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    return oldhandler;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  } else if (jvm_signal_installing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    /* jvm is installing its signal handlers. Install the new
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
     * handlers and save the old ones. jvm uses sigaction().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
     * Leave the piece here just in case. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    oldhandler = call_os_signal(sig, disp, is_sigset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    save_signal_handler(sig, oldhandler);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    /* Record the signals used by jvm */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    jvmsigs |= MASK(sig);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    return oldhandler;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    /* jvm has no relation with this signal (yet). Install the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
     * the handler. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    oldhandler = call_os_signal(sig, disp, is_sigset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    return oldhandler;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
sa_handler_t signal(int sig, sa_handler_t disp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  return set_signal(sig, disp, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
sa_handler_t sigset(int sig, sa_handler_t disp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  return set_signal(sig, disp, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
 }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
static int call_os_sigaction(int sig, const struct sigaction  *act,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
                             struct sigaction *oact) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  if (os_sigaction == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    if (os_sigaction == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      printf("%s\n", dlerror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      exit(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  return (*os_sigaction)(sig, act, oact);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  int res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  bool sigused;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  struct sigaction oldAct;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  signal_lock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  sigused = (MASK(sig) & jvmsigs) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  if (jvm_signal_installed && sigused) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    /* jvm has installed its signal handler for this signal. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    /* Save the handler. Don't really install it. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    if (oact != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      *oact = sact[sig];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    if (act != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
      sact[sig] = *act;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  } else if (jvm_signal_installing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    /* jvm is installing its signal handlers. Install the new
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
     * handlers and save the old ones. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    res = call_os_sigaction(sig, act, &oldAct);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    sact[sig] = oldAct;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    if (oact != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      *oact = oldAct;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    /* Record the signals used by jvm */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    jvmsigs |= MASK(sig);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    /* jvm has no relation with this signal (yet). Install the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
     * the handler. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    res = call_os_sigaction(sig, act, oact);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
/* The three functions for the jvm to call into */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
void JVM_begin_signal_setting() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  signal_lock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  jvm_signal_installing = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  tid = pthread_self();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
void JVM_end_signal_setting() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  signal_lock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  jvm_signal_installed = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  jvm_signal_installing = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  pthread_cond_broadcast(&cond);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  signal_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
struct sigaction *JVM_get_signal_action(int sig) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  /* Does race condition make sense here? */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  if ((MASK(sig) & jvmsigs) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    return &sact[sig];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
}