/**************************************************************************
* vhsyslogd.c
**************************************************************************
* $Date: 2001/04/17 16:36:24 $
* $Revision: 1.1 $
**************************************************************************
*
* vhsyslogd daemon.
*
* reads the following sources:
*
* a TCP listen/accept socket.
* a static list of UDP ports (up to MAX_UDP).
* a static list of AF_LOCAL sockets. (up to MAX_UNIX).
*
* writes to the following consumers:
*
* VHLOGFILE.
*
* VHLOGFILE rolls over after MAX_LOGFILE_SIZE.
* This code supports a remote monitor via TCP.
*
* Here is the current calling tree for each source
* (note: '>' means fork() ):
*
* main()
* ---------
* |
* |
vhsyslogd()
*
>----------
* | |
* | | service_tcp_accepts()
* exit fg |
>--------------------
* ---------
|
|
*
|
| accept()
*
|
----------
*
|
|
*
|
| handle_tcp_input()
*
|
--------------------
*
| handle_udp_input()
*
>------------------
*
|
*
| handle_unix_input()
*
----------------------
*
*
* Revision History
*
who
when
what
*
---
----
----
*
martinj991@home.com
19/04/01 chmod
/dev/log after opening.
*
martinj991@home.com
26/02/01 started.
*
* $Log: vhsyslogd.c,v $
* Revision 1.1
2001/04/17 16:36:24 root
* Initial revision
*
*************************************************************************/
#include "version.cpp"
#include "vhsyslog.h"
#include <paths.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/un.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define _GNU_SOURCE
#include <getopt.h>
/* log file sources
*/
#define INTERNAL 2
#define SYSLOG 1
#define REMOTE 0
int
debug = 0;
int
do_fork = 1;
/* defined in getopt
*/
extern char *optarg;
extern int optind, opterr, optopt;
/* list of active tcp buffers */
tcp_buffer_t *TcpBuffers = (tcp_buffer_t *) NULL;
/* VHheader
*/
vhsyslog_msg_t *VHmsg;
/* UNIX sockets to listen on */
char
*UNIX_SOCKNAMES[MAX_UNIX*PATH_FILENAME_MAX];
int UNIX_DESCRIPTORS[MAX_UNIX];
struct sockaddr_un UNIX_SOCKETS[MAX_UNIX];
/* TCP port to listen on */
int TCP_PORT;
int iVHsyslogTCP = -1;
struct sockaddr_in sVHsyslogTCP;
int
sndrcv_buffers = SNDRCV_BUFFERS;
char
xlen[LEN_TAG+1];
int
MAX_TCP_HANDLERS = 1; /* -T switch */
int
MAX_UDP_HANDLERS = 1; /* -U switch */
int
I_AM_TCP = 0; /* handler
*/
int
I_AM_UDP = 0; /* type */
/* UDP ports to listen on */
int
UDP_PORTS[MAX_UDP];
int
UDP_DESCRIPTORS[MAX_UDP];
struct sockaddr_in UDP_SOCKETS[MAX_UDP];
int
active_UDP_fds = 0;
/* run time stats
*/
vhsyslogd_stats_t *rtStats;
int shmkey = vhsyslogSHM;
int
shmid = -1;
void
*rtShmem = (void *)0;
/* local system info
*/
struct utsname MyUTSname;
struct hostent *host;
char
MyIP[TINYSTRING];
char
sSource[PATH_FILENAME_MAX];
char
*proc_name;
/* TCP remote peer info
*/
char
peerIP[TINYSTRING];
char
peerPort[TINYSTRING];
/* used to log local messages */
char
INT_MSG[MSGSIZE];
char
StartupMsg[MEDIUMSTRING];
/* files
*/
int fpLogFileOut = -1;
FILE *fpLastWrite;
struct stat stbuf;
char
BufferFile[PATH_FILENAME_MAX];
char
BaseName[PATH_FILENAME_MAX];
/* BufferFIle LOCK token */
char
LOCKtoken[PATH_FILENAME_MAX];
int
fdLOCK = -1;
int
lock_timeout = 0;
int CurrentFile = 0;
long
MaxBufferSize = 0.00;
/* list of ready unix/udp fd's */
fd_set readfds_base, readfds;
fd_set writefds_base, writefds;
fd_set exceptfds_base, exceptfds;
/* list of ready tcp fd's */
fd_set TCPreadfds_base, TCPreadfds;
fd_set TCPwritefds_base, TCPwritefds;
fd_set TCPexceptfds_base, TCPexceptfds;
/* max fd
*/
int
maxfd = -1;
/* UNIX/UDP select timeouts */
struct timeval input_timeout_base, input_timeout;
/* TCP select timout
*/
struct timeval TCPinput_timeout_base, TCPinput_timeout;
/* global flags
*/
int tcp_connected =
0;
int iInit
= 1; /* cleared in init_server */
int parentPID = 0; /*
daemon PID
*/
int tcpPID
= 0; /* tcp listener PID */
int udpPID
= 0; /* udp listener PID */
int VERBOSE = 0; /*
log more info/events
*/
/* prototypes
*/
int decode_args(int,
char **);
int init_server(int,
char **);
int open_log();
int open_unix(void);
int open_udp(void);
int open_tcp(void);
int handle_unix_input(void);
int handle_udp_input(void);
int handle_tcp_input();
int has_input(int);
int
allocate_tcp_buffer(int);
int
deallocate_tcp_buffer(int);
int
get_msg_len(int );
vhsyslog_msg_t *ParseVHsyslogMsg(char *);
void
vhgetpeername(int);
int tag_msg (char
*, char *, int);
int get_current_buffer(void);
char
*vhsyslog_getdate(int);
int
vhsyslogd(void);
int
fork2(void);
int
update_BUFPTRS(void);
int send_to_log(char
*, int);
int split_log(void);
void
log_internal(int, char
*);
int
log_err(int, char
*);
int
lock_BufferFile(int);
int service_tcp_accepts();
void
record_rtStats(int, long);
int
send_rtStats(int);
void
spin(long, int,
int);
void
beep(int);
char
*strrmc(char *,int);
char
*find_proc( char *);
void
VHshutdown();
void
sigpipe(int);
void
sigdie(int);
/*************************************************************************/
/**************************************************************************
* main
*
* log message reciever
*
* returns: -1 if a fatal error occurred.
*
* Revision History
*
who
when
what
*
---
----
----
*
*************************************************************************/
int
main(int argc, char *argv[] ) {
/**************************************************************************/
/* init sockets, logs, etc.... */
if( init_server(argc, argv ) == -1 )
VHshutdown();
if( do_fork) {
/* de-associate
the users control terminal with the daemon */
setpgrp();
switch(
fork() ) {
case 0: /* daemon code comes here */
parentPID = getpid();
vhsyslogd();
break;
case -1:
snprintf(INT_MSG,MSGSIZE,"%s:%d fork() \n%s\n",
__FILE__,__LINE__,strerror(errno));
VHshutdown();
break;
default:
exit(0); /* fg exit */
}/* switch */
}
if( !do_fork) {
parentPID = getpid();
vhsyslogd();
}
/* we should never get here */
snprintf(INT_MSG,MSGSIZE,"%s:%d
[%d] exiting at bottom of main()\n",
__FILE__,__LINE__, getpid() );
VHshutdown();
exit(-1);
}/**************************
end of main *******************************/
/***********************************************************************
* vhsyslogd
*
* waits for syslog messages
* writes them to BufferFile.
*
*
* returns: -1 if a fatal error
occurred.
*
* Revision History
* who
when
what
* ---
----
----
*
**********************************************************************/
int
vhsyslogd() {
int
uidx = 0;
/*********************************************************************/
/* TT 236: TCP input streams drop
almost all messages.
seperate UDP, UNIX and TCP processing into three
threads.
Increase system runtime parameter fs.file-max to
16384 using sysctl.
*/
/* fork TCP handlers
*/
fprintf(stdout,"vhsyslogd pid is:
[%d]\n", parentPID);
while( MAX_TCP_HANDLERS--) {
switch (fork2()) { /*
vhsyslog_common.c */
case 0:
tcpPID=getpid();
(void) service_tcp_accepts();
VHshutdown();
break;
case -1: snprintf(INT_MSG,MSGSIZE,"%s:%d
fork2(tcp)\n %s. Exiting.\n",
__FILE__,__LINE__,strerror(errno));
VHshutdown();
break;
default: break;
}
}
/* TT 236 UDP input streams drop
msgs */
/* fork UDP handlers
*/
while( MAX_