1ee4499a921abf55343a9839ad60af5ec07987ed
2 * syslog-client.c - syslog client implementation for windows
4 * Created by Alexander Yaworsky
6 * THIS SOFTWARE IS NOT COPYRIGHTED
8 * This source code is offered for use in the public domain. You may
9 * use, modify or distribute it freely.
11 * This code is distributed in the hope that it will be useful but
12 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
13 * DISCLAIMED. This includes but is not limited to warranties of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 /* define SYSLOG_CONF_DIR where syslog.host should be
21 #ifndef SYSLOG_CONF_DIR
22 static const char *syslog_conf_dir
= ".";
24 static const char *syslog_conf_dir
= SYSLOG_CONF_DIR
;
34 # define SYSLOG_DGRAM_SIZE 80
36 # define SYSLOG_DGRAM_SIZE 1024
39 static BOOL initialized
= FALSE
;
40 static int log_mask
= 0xFF;
41 static char *syslog_ident
;
42 static int syslog_facility
;
43 static char str_pid
[ 40 ];
44 static SOCKADDR_IN sa_logger
;
46 static char local_hostname
[ MAX_COMPUTERNAME_LENGTH
+ 1 ];
47 static char datagramm
[ SYSLOG_DGRAM_SIZE
];
48 static int datagramm_size
;
50 /******************************************************************************
53 * maybe this function will be useful...
55 const char* set_syslog_conf_dir( const char* dir
)
57 const char *ret
= syslog_conf_dir
;
58 syslog_conf_dir
= dir
;
62 /******************************************************************************
65 * Read configuration file syslog.host. This file should contain host address
66 * and, optionally, port. Initialize sa_logger. If the configuration file does
67 * not exist, use localhost:514.
68 * Returns: 0 - ok, -1 - error.
70 static void init_logger_addr()
72 char pathname
[ FILENAME_MAX
];
78 memset( &sa_logger
, 0, sizeof(SOCKADDR_IN
) );
79 sa_logger
.sin_family
= AF_INET
;
81 if( '\\' == syslog_conf_dir
[0] || '/' == syslog_conf_dir
[0] || ':' == syslog_conf_dir
[1] )
84 strcpy( pathname
, syslog_conf_dir
);
91 strcpy( pathname
, __argv
[0] );
92 p
= strrchr( pathname
, '\\' ) + 1;
93 q
= strrchr( pathname
, '/' ) + 1;
100 strcat( pathname
, syslog_conf_dir
);
102 p
= &pathname
[ strlen( pathname
) - 1 ];
103 if( '\\' != *p
&& '/' != *p
)
107 strcpy( ++p
, "syslog.host" );
109 /* read destination host name */
110 fd
= fopen( pathname
, "r" );
114 if( NULL
== fgets( host
, sizeof(host
), fd
) )
118 p
= strchr( host
, '\n' );
121 p
= strchr( host
, '\r' );
127 p
= strchr( host
, ':' );
131 phe
= gethostbyname( host
);
135 memcpy( &sa_logger
.sin_addr
.s_addr
, phe
->h_addr
, phe
->h_length
);
138 sa_logger
.sin_port
= htons( (unsigned short) strtoul( p
, NULL
, 0 ) );
140 sa_logger
.sin_port
= htons( SYSLOG_PORT
);
144 sa_logger
.sin_addr
.S_un
.S_addr
= htonl( 0x7F000001 );
145 sa_logger
.sin_port
= htons( SYSLOG_PORT
);
148 /******************************************************************************
151 * Close desriptor used to write to system logger.
162 /******************************************************************************
165 * Open connection to system logger.
167 void openlog( char* ident
, int option
, int facility
)
169 BOOL failed
= TRUE
, wsa_initialized
= FALSE
;
171 SOCKADDR_IN sa_local
;
178 syslog_facility
= facility
? facility
: LOG_USER
;
180 /* FIXME: should we reset logmask? */
182 if( option
& LOG_PID
)
183 snprintf( str_pid
, sizeof(str_pid
), "[%lu]", GetCurrentProcessId() );
187 /* FIXME: handle other options */
189 n
= sizeof(local_hostname
);
190 if( !GetComputerName( local_hostname
, &n
) )
193 sock
= INVALID_SOCKET
;
194 if( WSAStartup( MAKEWORD( 2, 2 ), &wsd
) )
196 wsa_initialized
= TRUE
;
202 sock
= socket( AF_INET
, SOCK_DGRAM
, 0 );
203 if( INVALID_SOCKET
== sock
)
206 memset( &sa_local
, 0, sizeof(SOCKADDR_IN
) );
207 sa_local
.sin_family
= AF_INET
;
208 if( bind( sock
, (SOCKADDR
*) &sa_local
, sizeof(SOCKADDR_IN
) ) == 0 )
211 sock
= INVALID_SOCKET
;
217 /* get size of datagramm */
218 size
= sizeof(datagramm_size
);
219 if( getsockopt( sock
, SOL_SOCKET
, SO_MAX_MSG_SIZE
, (char*) &datagramm_size
, &size
) )
221 if( datagramm_size
- strlen(local_hostname
) - (ident
? strlen(ident
) : 0) < 64 )
223 if( datagramm_size
> sizeof(datagramm
) )
224 datagramm_size
= sizeof(datagramm
);
226 if( atexit( closelog
) )
229 syslog_ident
= ident
;
230 syslog_facility
= facility
;
236 if( sock
!= INVALID_SOCKET
) closesocket( sock
);
237 if( wsa_initialized
) WSACleanup();
239 initialized
= !failed
;
242 /******************************************************************************
245 * Set the log mask level.
247 int setlogmask( int mask
)
256 /******************************************************************************
259 * Generate a log message using FMT string and option arguments.
261 void syslog( int pri
, char* fmt
, ... )
266 vsyslog( pri
, fmt
, ap
);
270 /******************************************************************************
273 * Generate a log message using FMT and using arguments pointed to by AP.
275 void vsyslog( int pri
, char* fmt
, va_list ap
)
277 static char *month
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
278 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
283 if( !(LOG_MASK( LOG_PRI( pri
)) & log_mask
) )
286 openlog( NULL
, 0, pri
& LOG_FACMASK
);
290 if( !(pri
& LOG_FACMASK
) )
291 pri
|= syslog_facility
;
293 GetLocalTime( &stm
);
294 len
= sprintf( datagramm
, "<%d>%s %2d %02d:%02d:%02d %s %s%s: ",
296 month
[ stm
.wMonth
- 1 ], stm
.wDay
, stm
.wHour
, stm
.wMinute
, stm
.wSecond
,
297 local_hostname
, syslog_ident
? syslog_ident
: "", str_pid
);
298 vsnprintf( datagramm
+ len
, datagramm_size
- len
, fmt
, ap
);
299 p
= strchr( datagramm
, '\n' );
302 p
= strchr( datagramm
, '\r' );
306 sendto( sock
, datagramm
, strlen(datagramm
), 0, (SOCKADDR
*) &sa_logger
, sizeof(SOCKADDR_IN
) );