xref: /illumos-gate/usr/src/cmd/pools/poold/libjsyslog/jsyslog.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stddef.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <syslog.h>
33 
34 #include "jsyslog.h"
35 
36 #define	ILL_ARG_EX_CLASS_DESC	"java/lang/IllegalArgumentException"
37 #define	THROWABLE_CLASS_DESC	"java/lang/Throwable"
38 
39 #define	CLASS_FIELD_DESC(class_desc)	"L" class_desc ";"
40 
41 /*
42  * syslog(3c) ident string
43  */
44 static char jsyslog_ident[32];
45 
46 /*
47  * Log the given message with the given severity.
48  */
49 /*ARGSUSED*/
50 JNIEXPORT void JNICALL
51 Java_com_sun_solaris_service_logging_SyslogHandler_syslog(JNIEnv *env,
52     jclass clazz, jint severity, jstring messageObj)
53 {
54 	const char *message;
55 
56 	if (messageObj == NULL) {
57 		jclass exceptionClass;
58 
59 		if (!(exceptionClass = (*env)->FindClass(env,
60 		    ILL_ARG_EX_CLASS_DESC)))
61 			return; /* exception thrown */
62 		(*env)->Throw(env, (*env)->NewObject(env, exceptionClass,
63 		    (*env)->GetStaticMethodID(env, exceptionClass, "<init>",
64 		    "()" CLASS_FIELD_DESC(THROWABLE_CLASS_DESC))));
65 		return;
66 	}
67 
68 	if (!(message = (*env)->GetStringUTFChars(env, messageObj, NULL)))
69 		return; /* exception thrown */
70 	syslog(severity, "%s", message);
71 	(*env)->ReleaseStringUTFChars(env, messageObj, message);
72 }
73 
74 /*
75  * Invoke openlog(3c).
76  */
77 /*ARGSUSED*/
78 JNIEXPORT void JNICALL
79 Java_com_sun_solaris_service_logging_SyslogHandler_openlog(JNIEnv *env,
80     jclass clazz, jstring identObj, jint logopt, jint facility)
81 {
82 	const char *ident;
83 
84 	if (identObj == NULL) {
85 		jclass exceptionClass;
86 
87 		if (!(exceptionClass = (*env)->FindClass(env,
88 		    ILL_ARG_EX_CLASS_DESC)))
89 			return; /* exception thrown */
90 		(*env)->Throw(env, (*env)->NewObject(env, exceptionClass,
91 		    (*env)->GetStaticMethodID(env, exceptionClass, "<init>",
92 		    "()" CLASS_FIELD_DESC(THROWABLE_CLASS_DESC))));
93 		return;
94 	}
95 
96 	if (!(ident = (*env)->GetStringUTFChars(env, identObj, NULL)))
97 		return; /* exception thrown */
98 	(void) strlcpy(jsyslog_ident, ident, sizeof (jsyslog_ident));
99 	openlog(jsyslog_ident, logopt, facility);
100 
101 	(*env)->ReleaseStringUTFChars(env, identObj, ident);
102 }
103 
104 /*
105  * Invoke closelog(3c).
106  */
107 /*ARGSUSED*/
108 JNIEXPORT void JNICALL
109 Java_com_sun_solaris_service_logging_SyslogHandler_closelog(JNIEnv *env,
110     jclass clazz)
111 {
112 	closelog();
113 }
114