Home | History | Annotate | Line # | Download | only in slapi
      1 /*	$NetBSD: printmsg.c,v 1.4 2025/09/05 21:16:33 christos Exp $	*/
      2 
      3 /* $OpenLDAP$ */
      4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5  *
      6  * Copyright 2002-2024 The OpenLDAP Foundation.
      7  * Portions Copyright 1997,2002-2003 IBM Corporation.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted only as authorized by the OpenLDAP
     12  * Public License.
     13  *
     14  * A copy of this license is available in the file LICENSE in the
     15  * top-level directory of the distribution or, alternatively, at
     16  * <http://www.OpenLDAP.org/license.html>.
     17  */
     18 /* ACKNOWLEDGEMENTS:
     19  * This work was initially developed by IBM Corporation for use in
     20  * IBM products and subsequently ported to OpenLDAP Software by
     21  * Steve Omrani.
     22  */
     23 
     24 #include <portable.h>
     25 #include <stdio.h>
     26 #include <ac/string.h>
     27 #include <ac/stdarg.h>
     28 #include <ac/unistd.h>
     29 #include <fcntl.h>
     30 #include <ac/errno.h>
     31 
     32 #include <ldap.h>
     33 #include <ldap_config.h>
     34 #include <slap.h>
     35 #include <slapi.h>
     36 
     37 #ifdef _WIN32
     38 #include <io.h>
     39 #endif
     40 
     41 #include <ldap_pvt_thread.h>
     42 
     43 /* Single threads access to routine */
     44 ldap_pvt_thread_mutex_t slapi_printmessage_mutex;
     45 char			*slapi_log_file = NULL;
     46 int			slapi_log_level = SLAPI_LOG_PLUGIN;
     47 
     48 int
     49 slapi_int_log_error(
     50 	int		level,
     51 	char		*subsystem,
     52 	char		*fmt,
     53 	va_list		arglist )
     54 {
     55 	int		rc = 0;
     56 	FILE		*fp = NULL;
     57 
     58 	char		timeStr[100];
     59 	struct tm	*ltm;
     60 	time_t		currentTime;
     61 
     62 	assert( subsystem != NULL );
     63 	assert( fmt != NULL );
     64 
     65 	ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
     66 
     67 	/* for now, we log all severities */
     68 	if ( level <= slapi_log_level ) {
     69 #ifdef _WIN32
     70 		intptr_t fhandle;
     71 #endif
     72 		fp = fopen( slapi_log_file, "a" );
     73 		if ( fp == NULL) {
     74 			rc = -1;
     75 			goto done;
     76 		}
     77 
     78 #ifdef _WIN32
     79 		fhandle = _get_osfhandle( fileno( fp ));
     80 #endif
     81 		/*
     82 		 * FIXME: could block
     83 		 */
     84 #ifdef _WIN32
     85 		while ( LockFile( fhandle, 0, 0, UINT_MAX, UINT_MAX ) == 0 ) {
     86 			/* DO NOTHING */ ;
     87 		}
     88 #else
     89 		while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) {
     90 			/* DO NOTHING */ ;
     91 		}
     92 #endif
     93 
     94 		time( &currentTime );
     95 		ltm = localtime( &currentTime );
     96 		strftime( timeStr, sizeof(timeStr), "%x %X", ltm );
     97 		fputs( timeStr, fp );
     98 
     99 		fprintf( fp, " %s: ", subsystem );
    100 		vfprintf( fp, fmt, arglist );
    101 		if ( fmt[ strlen( fmt ) - 1 ] != '\n' ) {
    102 			fputs( "\n", fp );
    103 		}
    104 		fflush( fp );
    105 
    106 #ifdef _WIN32
    107 		UnlockFile( fhandle, 0, 0, UINT_MAX, UINT_MAX );
    108 #else
    109 		lockf( fileno( fp ), F_ULOCK, 0 );
    110 #endif
    111 
    112 		fclose( fp );
    113 
    114 	} else {
    115 		rc = -1;
    116 	}
    117 
    118 done:
    119 	ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
    120 
    121 	return rc;
    122 }
    123