Debian Codegraph @

servers/slapd/slapi/printmsg.c

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