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 #include <stddef.h>
28 #include <assert.h>
29 #include <string.h>
30 #include <syslog.h>
31 
32 #include "jsyslog.h"
33 
34 #define	ILL_ARG_EX_CLASS_DESC	"java/lang/IllegalArgumentException"
35 #define	THROWABLE_CLASS_DESC	"java/lang/Throwable"
36 
37 #define	CLASS_FIELD_DESC(class_desc)	"L" class_desc ";"
38 
39 /*
40  * syslog(3c) ident string
41  */
42 static char jsyslog_ident[32];
43 
44 /*
45  * Log the given message with the given severity.
46  */
47 /*ARGSUSED*/
48 JNIEXPORT void JNICALL
Java_com_sun_solaris_service_logging_SyslogHandler_syslog(JNIEnv * env,jclass clazz,jint severity,jstring messageObj)49 Java_com_sun_solaris_service_logging_SyslogHandler_syslog(JNIEnv *env,
50     jclass clazz, jint severity, jstring messageObj)
51 {
52 	const char *message;
53 
54 	if (messageObj == NULL) {
55 		jclass exceptionClass;
56 
57 		if (!(exceptionClass = (*env)->FindClass(env,
58 		    ILL_ARG_EX_CLASS_DESC)))
59 			return; /* exception thrown */
60 		(*env)->Throw(env, (*env)->NewObject(env, exceptionClass,
61 		    (*env)->GetStaticMethodID(env, exceptionClass, "<init>",
62 		    "()" CLASS_FIELD_DESC(THROWABLE_CLASS_DESC))));
63 		return;
64 	}
65 
66 	if (!(message = (*env)->GetStringUTFChars(env, messageObj, NULL)))
67 		return; /* exception thrown */
68 	syslog(severity, "%s", message);
69 	(*env)->ReleaseStringUTFChars(env, messageObj, message);
70 }
71 
72 /*
73  * Invoke openlog(3c).
74  */
75 /*ARGSUSED*/
76 JNIEXPORT void JNICALL
Java_com_sun_solaris_service_logging_SyslogHandler_openlog(JNIEnv * env,jclass clazz,jstring identObj,jint logopt,jint facility)77 Java_com_sun_solaris_service_logging_SyslogHandler_openlog(JNIEnv *env,
78     jclass clazz, jstring identObj, jint logopt, jint facility)
79 {
80 	const char *ident;
81 
82 	if (identObj == NULL) {
83 		jclass exceptionClass;
84 
85 		if (!(exceptionClass = (*env)->FindClass(env,
86 		    ILL_ARG_EX_CLASS_DESC)))
87 			return; /* exception thrown */
88 		(*env)->Throw(env, (*env)->NewObject(env, exceptionClass,
89 		    (*env)->GetStaticMethodID(env, exceptionClass, "<init>",
90 		    "()" CLASS_FIELD_DESC(THROWABLE_CLASS_DESC))));
91 		return;
92 	}
93 
94 	if (!(ident = (*env)->GetStringUTFChars(env, identObj, NULL)))
95 		return; /* exception thrown */
96 	(void) strlcpy(jsyslog_ident, ident, sizeof (jsyslog_ident));
97 	openlog(jsyslog_ident, logopt, facility);
98 
99 	(*env)->ReleaseStringUTFChars(env, identObj, ident);
100 }
101 
102 /*
103  * Invoke closelog(3c).
104  */
105 /*ARGSUSED*/
106 JNIEXPORT void JNICALL
Java_com_sun_solaris_service_logging_SyslogHandler_closelog(JNIEnv * env,jclass clazz)107 Java_com_sun_solaris_service_logging_SyslogHandler_closelog(JNIEnv *env,
108     jclass clazz)
109 {
110 	closelog();
111 }
112