1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Copyright (c) 2014 Balabit
* Copyright (c) 2014 Viktor Juhasz <viktor.juhasz@balabit.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "java_machine.h"
#include "java-class-loader.h"
#include "syslog-ng.h"
#include "messages.h"
#include "atomic.h"
#include "reloc.h"
#include "plugin.h"
#include "resolved-configurable-paths.h"
#include "apphook.h"
#include <string.h>
struct _JavaVMSingleton
{
GAtomicCounter ref_cnt;
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
GString *class_path;
ClassLoader *loader;
};
static JavaVMSingleton *global_jvm;
static JavaVMSingleton *
_jvm_new(void)
{
msg_debug("Java machine new");
JavaVMSingleton *jvm = g_new0(JavaVMSingleton, 1);
g_atomic_counter_set(&jvm->ref_cnt, 1);
jvm->class_path = g_string_new(get_installation_path_for(SYSLOG_NG_JAVA_MODULE_PATH));
g_string_append(jvm->class_path, "/syslog-ng-core.jar");
return jvm;
}
void
java_machine_unref_callback(gint hook_type, gpointer user_data)
{
JavaVMSingleton *jvm = (JavaVMSingleton *)user_data;
java_machine_unref(jvm);
}
JavaVMSingleton *
java_machine_ref(void)
{
if (global_jvm)
{
g_atomic_counter_inc(&global_jvm->ref_cnt);
}
else
{
global_jvm = _jvm_new();
/* The application hook is going to hold a reference to the global g_jvm_s,
* therefore the reference counter must be incremented before that.
* But we are in the _ref() function, so the counter must be updated as below. */
g_atomic_counter_inc(&global_jvm->ref_cnt);
register_application_hook(AH_SHUTDOWN, java_machine_unref_callback, global_jvm, AHM_RUN_ONCE);
}
return global_jvm;
}
static inline void
_jvm_free(JavaVMSingleton *self)
{
msg_debug("Java machine free");
g_string_free(self->class_path, TRUE);
if (self->jvm)
{
JavaVM jvm = *(self->jvm);
if (self->loader)
{
class_loader_free(self->loader, java_machine_get_env(self));
}
jvm->DestroyJavaVM(self->jvm);
}
for (gint i = 0; i < self->vm_args.nOptions; i++)
g_free(self->vm_args.options[i].optionString);
g_free(self->vm_args.options);
g_free(self);
}
void
java_machine_unref(JavaVMSingleton *self)
{
g_assert(self == global_jvm);
/* The last reference is always hold by a AH_SHUTDOWN app hook, when there is no java configured.
* The only way to get rid of it to shutdown syslog-ng. */
if (g_atomic_counter_get(&self->ref_cnt) == 2)
{
msg_warning("If you have reloaded syslog-ng, the JVM is not used anymore, but it is still running. If you want to stop JVM, stop syslog-ng and then start syslog-ng again");
}
if (g_atomic_counter_dec_and_test(&self->ref_cnt))
{
_jvm_free(self);
global_jvm = NULL;
}
}
static GArray *
_jvm_options_array_append(GArray *jvm_options_array, char *option_string)
{
JavaVMOption opt;
opt.optionString = option_string;
return g_array_append_val(jvm_options_array, opt);
}
static gboolean
_is_jvm_option_predefined(const gchar *option)
{
static const gchar *predefined_options[] =
{
"Djava.system.class.loader",
"Xshare",
"Djava.class.path",
"Djava.library.path",
"Dlog4j.configurationFactory",
NULL
};
for (gint i = 0; predefined_options[i] != NULL; i++)
{
if (!strcmp(option, predefined_options[i]))
{
msg_info("JVM option is set by syslog-ng, cannot be overridden by user-defined values.",
evt_tag_str("option", option));
return TRUE;
}
}
return FALSE;
}
static GArray *
_jvm_options_split(const gchar *jvm_options_str)
{
GArray *jvm_options_array = g_array_new(FALSE, TRUE, sizeof(JavaVMOption));
if (!jvm_options_str)
return jvm_options_array;
gchar **options_str_array = g_strsplit_set(jvm_options_str, " \t", 0);
for (gint i = 0; options_str_array[i]; i++)
{
if (options_str_array[i][0] == '\0' || _is_jvm_option_predefined(options_str_array[i]))
{
g_free(options_str_array[i]);
continue;
}
jvm_options_array = _jvm_options_array_append(jvm_options_array, g_strdup(options_str_array[i]));
}
g_free(options_str_array);
return jvm_options_array;
}
static void
_setup_jvm_options_array(JavaVMSingleton *self, const gchar *jvm_options_str)
{
GArray *jvm_options_array = _jvm_options_split(jvm_options_str);
jvm_options_array = _jvm_options_array_append(jvm_options_array,
g_strdup_printf("-Djava.system.class.loader=org.syslog_ng.SyslogNgClassLoader"));
jvm_options_array = _jvm_options_array_append(jvm_options_array,
g_strdup_printf("-Xshare:off"));
jvm_options_array = _jvm_options_array_append(jvm_options_array,
g_strdup_printf("-Djava.class.path=%s",
self->class_path->str));
jvm_options_array = _jvm_options_array_append(jvm_options_array,
g_strdup_printf("-Djava.library.path=%s",
resolved_configurable_paths.initial_module_path));
jvm_options_array = _jvm_options_array_append(jvm_options_array,
g_strdup_printf("-Dlog4j.configurationFactory=org.syslog_ng.logging.CustomConfigurationFactory"));
jvm_options_array = _jvm_options_array_append(jvm_options_array,
g_strdup("-Xrs"));
self->vm_args.nOptions = jvm_options_array->len;
self->vm_args.options = (JavaVMOption *)jvm_options_array->data;
g_array_free(jvm_options_array, FALSE);
}
gboolean
java_machine_start(JavaVMSingleton *self, const gchar *jvm_options)
{
g_assert(self == global_jvm);
if (!self->jvm)
{
long status;
_setup_jvm_options_array(self, jvm_options);
self->vm_args.version = JNI_VERSION_1_6;
status = JNI_CreateJavaVM(&self->jvm, (void **) &self->env,
&self->vm_args);
if (status == JNI_ERR)
{
return FALSE;
}
}
return TRUE;
}
static ClassLoader *
java_machine_get_class_loader(JavaVMSingleton *self)
{
if (self->loader)
return self->loader;
JNIEnv *env = NULL;
(*(self->jvm))->GetEnv(self->jvm, (void **)&env, JNI_VERSION_1_6);
self->loader = class_loader_new(env);
g_assert(self->loader);
return self->loader;
}
void
java_machine_attach_thread(JavaVMSingleton *self, JNIEnv **penv)
{
g_assert(self == global_jvm);
if ((*(self->jvm))->AttachCurrentThread(self->jvm, (void **)penv, &self->vm_args) == JNI_OK)
{
class_loader_init_current_thread(java_machine_get_class_loader(self), *penv);
}
}
void
java_machine_detach_thread(void)
{
(*(global_jvm->jvm))->DetachCurrentThread(global_jvm->jvm);
}
jclass
java_machine_load_class(JavaVMSingleton *self, const gchar *class_name, const gchar *class_path)
{
return class_loader_load_class(java_machine_get_class_loader(self), java_machine_get_env(self), class_name,
class_path);
}
JNIEnv *
java_machine_get_env(JavaVMSingleton *self)
{
JNIEnv *penv = NULL;
if ((*(self->jvm))->GetEnv(self->jvm, (void **)&penv, JNI_VERSION_1_6) != JNI_OK)
{
java_machine_attach_thread(self, &penv);
}
return penv;
}
|