⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main_broker.c

📁 uClinux下用的数据库
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** Copyright (c) 1995-2001  Hughes Technologies Pty Ltd.  All rights** reserved.  **** Terms under which this software may be used or copied are** provided in the  specific license associated with this product.**** Hughes Technologies disclaims all warranties with regard to this ** software, including all implied warranties of merchantability and ** fitness, in no event shall Hughes Technologies be liable for any ** special, indirect or consequential damages or any damages whatsoever ** resulting from loss of use, data or profits, whether in an action of ** contract, negligence or other tortious action, arising out of or in ** connection with the use or performance of this software.****** $Id: main_broker.c,v 1.18 2004/02/01 04:59:05 bambi Exp $***//*** Module	: main : msqld_main** Purpose	: ** Exports	: ** Depends Upon	: *//**************************************************************************** STANDARD INCLUDES**************************************************************************/#include <common/config.h>#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#ifdef HAVE_UNISTD_H#  include <unistd.h>#endif#ifdef HAVE_STRING_H#  include <string.h>#endif#ifdef HAVE_STRINGS_H#  include <strings.h>#endif/**************************************************************************** MODULE SPECIFIC INCLUDES**************************************************************************/#include <fcntl.h>#include <limits.h>#include <locale.h>#include <errno.h>#include <pwd.h>#include <time.h>#include <sys/stat.h>#include <sys/time.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/socket.h>#include <netdb.h>#include <sys/un.h>#include <signal.h>#ifdef HAVE_SETRLIMIT#  include <sys/resource.h>#endif#if HAVE_SYS_MMAN_H#  include <sys/mman.h>#endif#include <common/msql_defs.h>#include <common/debug/debug.h>#include <common/portability.h>#include <common/config/config.h>#include <msqld/index/index.h>#include <msqld/includes/msqld.h>#include <msqld/includes/errmsg.h>#include <msqld/main/main.h>#include <msqld/broker/broker.h>#include <msqld/main/version.h>#include <msqld/main/process.h>#include <msqld/main/table.h>#include <msqld/main/net.h>#include <msqld/main/acl.h>#include <msqld/main/cache.h>#include <msqld/main/util.h>#include <msqld/main/parse.h>#include <msqld/main/tcp.h>#include <msqld/lock/lock.h>/**************************************************************************** GLOBAL VARIABLES**************************************************************************/msqld	*globalServer;/**************************************************************************** PRIVATE ROUTINES**************************************************************************/static void _zeroMessageStruct(msg)	mMsg_t	*msg;{	msg->command = msg->access = msg->client = 0;	*msg->db = *msg->table = *msg->user = *msg->client_ip = 0;}	/**************************************************************************** PUBLIC ROUTINES**************************************************************************/extern  u_char  *yytext,		*yyprev;extern	int	yylineno;extern	int	yydebug;extern	char	errMsg[];int	curSock,	IPsock = 0,	UNIXsock = 0,	numCons = 0,	maxCons = 0,	conCount = 0,	numKids = 0,	eintrCount = 0,	msyncTimer,	startTime,	explainOnly;FILE	*logFP;u_int	serverStartTime = 0,	serverNumCons = 0,	serverNumQueries = 0;char	*unixPort,	confFile[MSQL_PATH_LEN];cinfo_t	conArray[256];extern	char	*packet;char	PROGNAME[] = "msqld",	BLANK_ARGV[] = "                                                  ";static char *comTable[] = {        "???", "Quit", "Init DB", "Query", "DB List", "Table List",        "Field List", "Create DB", "Drop DB", "Reload ACL",        "Shutdown", "Index List", "Stats", "Seq Info", "Move DB",	"Copy DB", "???" };#if defined(_OS_WIN32)RETSIGTYPE sigTrap(int sig);RETSIGTYPE puntServer(int sig);RETSIGTYPE puntClient(int sig);#endif/****************************************************************************** 	_initServer****	Purpose	: **	Args	: **	Returns	: **	Notes	: */void initServer(){	int	tcpPort,		opt;	struct	sockaddr_in	IPaddr;#ifdef HAVE_SYS_UN_H	struct	sockaddr_un	UNIXaddr;#endif	cacheSetupTableCache();	/*	** Create an IP socket	*/	if (configGetIntEntry("system", "remote_access"))	{		tcpPort = configGetIntEntry("general", "tcp_port");		msqlDebug1(MOD_GENERAL,"IP Socket is %d\n",tcpPort);		IPsock = socket(AF_INET, SOCK_STREAM, 0);		if (IPsock < 0)		{			perror("Can't start server : IP Socket ");			exit(1);		}#		ifdef SO_REUSEADDR		opt = 1;		setsockopt(IPsock, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,			sizeof(int));#		endif		bzero(&IPaddr, sizeof(IPaddr));		IPaddr.sin_family = AF_INET;		IPaddr.sin_addr.s_addr = htonl(INADDR_ANY);		IPaddr.sin_port = htons((u_short)tcpPort);		if (bind(IPsock,(struct sockaddr *)&IPaddr,sizeof(IPaddr)) < 0)		{			perror("Can't start server : IP Bind ");			exit(1);		}		listen(IPsock,128);	}	else	{		IPsock = -1;	}#	ifdef HAVE_SYS_UN_H	/*	** Create the UNIX socket	*/	if (configGetIntEntry("system", "local_access"))	{		unixPort = (char *)strdup((char *)configGetCharEntry("general", 			"unix_port"));			msqlDebug1(MOD_GENERAL,"UNIX Socket is %s\n",unixPort);		UNIXsock = socket(AF_UNIX, SOCK_STREAM, 0);		if (UNIXsock < 0)		{			perror("Can't start server : UNIX Socket ");			exit(1);		}		bzero(&UNIXaddr, sizeof(UNIXaddr));		UNIXaddr.sun_family = AF_UNIX;		strcpy(UNIXaddr.sun_path, unixPort);		unlink(unixPort);		if (bind(UNIXsock, (struct sockaddr *)&UNIXaddr, 			sizeof(UNIXaddr)) < 0)		{			perror("Can't start server : UNIX Bind ");			exit(1);		}		listen(UNIXsock,128);		chmod(unixPort,0777);	}	else#	endif	{		UNIXsock = -1;	}}RETSIGTYPE puntServer(sig)	int	sig;{	int	clientSock;	signal(sig,SIG_IGN);	clientSock = 3;	if (sig == -1)	{		printf("\n\nNormal Server shutdown!\n\n");	}	else	{		printf("\nServer Aborting!\n");	}	while(clientSock < maxCons + globalServer->config.cacheDescriptors)	{		if (conArray[clientSock].db)		{			printf("Forcing close on Socket %d\n",clientSock);			shutdown(clientSock,2);			close(clientSock);		}		clientSock++;	}	if (IPsock >= 0)	{		shutdown(IPsock,2);		close(IPsock);	}	if (UNIXsock >= 0)	{		shutdown(UNIXsock,2);		close(UNIXsock);		unlink(unixPort);		free(unixPort);	}	printf("\n");	cacheDropTableCache();	if (debugSet(MOD_MALLOC))	{		/*		**		fprintf(stderr,"\n\nmalloc() leak detection .....\n");		checkBlocks(MALLOC_BLK);		**		*/	}	if (debugSet(MOD_MMAP))	{		/*		**		fprintf(stderr,"\n\nmmap() leak detection .....\n");		checkBlocks(MMAP_BLK);		**		*/	}	printf("\n\nmSQL Daemon Shutdown Complete.\n\n");	if (sig >= 0)	{		exit(1);	}	return;}void freeClientConnection(server, sock)	msqld	*server;	int	sock;{	if(conArray[sock].db)	{		free(conArray[sock].db);		conArray[sock].db = NULL;	}	if(conArray[sock].host)	{		free(conArray[sock].host);		conArray[sock].host = NULL;	}	if(conArray[sock].user)	{		free(conArray[sock].user);		conArray[sock].user = NULL;	}	conArray[sock].sock = -1;	numCons--;}RETSIGTYPE puntClient(sig)	int	sig;{	mMsg_t	message;	extern	int netCurrentSock;	if(conArray[netCurrentSock].sock < 0)	{		/* Must have already been "force closed" */		return;	}	if (sig)	{		signal(sig, puntClient);		msqlDebug1(MOD_GENERAL,			"Forced close of client on socket %d due to pipe sig\n",			netCurrentSock);		_zeroMessageStruct(&message);		message.command = CMD_CLIENT_CLOSE;		message.client = netCurrentSock;		brokerChildSendMessage(&message);	}	else	{		msqlDebug1(MOD_GENERAL,			"Closing client on socket %d\n", netCurrentSock);	}	freeClientConnection(NULL, netCurrentSock);	return;}char *calcUptime(){	int	days,		hours,		mins,		secs;	u_int	tmp,		uptime;	static 	char msg[80];	tmp = uptime = time(NULL) - serverStartTime;	days = tmp / 3600 / 24;	tmp -= (days * 3600 * 24);	hours = tmp  / 3600;	tmp -= hours * 3600;	mins = tmp / 60;	tmp -= mins * 60;	secs = tmp;	snprintf(msg,80,"%d days, %d hours, %d mins, %d secs",		days, hours, mins, secs);	return(msg);}void sendServerStats(sock)	int	sock;{	struct	passwd *pw;	int	uid,		loop,		cTime;	u_int	curTime = time(NULL);	char	buf[10];	snprintf(packet,PKT_LEN, "%s%s\n%s\n%s\n%s\n\n", 		"Mini SQL Version ", SERVER_VERSION,		"Copyright (c) 1993-94 David J. Hughes",		"Copyright (c) 1995-2004 Hughes Technologies Pty Ltd.",		"All rights reserved.");	netWritePacket(sock);	snprintf(packet,PKT_LEN,"Config file      : %s\n",confFile);	netWritePacket(sock);	snprintf(packet,PKT_LEN,"Max connections  : %d\n",maxCons);	netWritePacket(sock);	snprintf(packet,PKT_LEN,"Cur connections  : %d\n",numCons);	netWritePacket(sock);	snprintf(packet,PKT_LEN,"Backend processes: %d\n",numKids);	netWritePacket(sock);#if defined(_OS_UNIX) || defined(_OS_OS2)	uid = getuid();	pw = getpwuid(uid);	if (pw != NULL)	{		snprintf(packet,PKT_LEN,"Running as user  : %s\n", pw->pw_name);	}	else	{		snprintf(packet,PKT_LEN,"Running as user  : UID %d\n", uid);	}	netWritePacket(sock);#endif	snprintf(packet,PKT_LEN,"Server uptime    : %s\n", calcUptime());	netWritePacket(sock);	snprintf(packet,PKT_LEN,"Connection count : %d\n", conCount);	netWritePacket(sock);	strcpy(packet, "\nConnection table :\n");	netWritePacket(sock);	loop = 0;	strcpy(packet,	    "  Sock    Username       Hostname        Database    Connect   Idle   Queries\n");	strcat(packet,	    " +-----+------------+-----------------+------------+---------+------+--------+\n");	netWritePacket(sock);	while(loop < maxCons)	{		if (conArray[loop].user)		{			cTime = (int)(curTime - conArray[loop].connectTime)/60;			snprintf(buf,sizeof(buf),				"%2dH %2dM",cTime/60, cTime%60);			snprintf(packet,PKT_LEN,			 " | %3d | %-10s | %-15s | %-10s | %s | %4d | %6d |\n",			    loop, conArray[loop].user, 			    conArray[loop].host?conArray[loop].host:"UNIX Sock",			    conArray[loop].db ? conArray[loop].db : "No DB",			    buf,			/*			    (int)(curTime - conArray[loop].lastQuery)/60,			*/ 			    0,			    conArray[loop].numQueries);			netWritePacket(sock);		}		loop++;	}	strcpy(packet,	    " +-----+------------+-----------------+------------+---------+------+--------+\n");	netWritePacket(sock);	strcpy(packet,"\n");	netWritePacket(sock);}void logQuery(logFP,con, query) 	FILE	*logFP;	cinfo_t	*con;	char	*query;{	char	dateBuf[80];	struct  tm *locTime;	time_t	clock;	clock = time(NULL);        locTime = localtime(&clock);        strftime(dateBuf,sizeof(dateBuf),"%d-%b-%Y %H:%M:%S",locTime);	fprintf(logFP,"%s %s %s %s %d\n%s\n",		dateBuf,		con->user?con->user:"NO_USER",		con->host?con->host:"UNIX_SOCK",		con->db?con->db:"NO_DB",		query?(int)strlen(query):13,		query?query:"MISSING_QUERY");	fflush(logFP);}void usage(){	printf("Usage :  msql2d [-f ConfFile]\n\n");}RETSIGTYPE childHandler(sig)	int	sig;{	int	clientSock;	signal(SIGCHLD,SIG_IGN);	printf("\n\nBackend process exited!  Terminating server.\n");	clientSock = 3;	while(clientSock < maxCons + globalServer->config.cacheDescriptors)	{		if (conArray[clientSock].db)		{			printf("Forcing close on Socket %d\n",clientSock);			shutdown(clientSock,2);			close(clientSock);		}		clientSock++;	}	if (IPsock >= 0)	{		shutdown(IPsock,2);		close(IPsock);	}	if (UNIXsock >= 0)	{		shutdown(UNIXsock,2);		close(UNIXsock);		unlink(unixPort);		free(unixPort);	}	/*	** Close off the children	*/	sync();	fprintf(stderr,"Closing backend connections.\n\n");	brokerCloseChildren();	printf("\n");	exit(1);}RETSIGTYPE sigTrap(sig)	int	sig;{	int	clientSock;#ifdef WIN32	fprintf(stderr,"\nHit by signal\n\n");#else	signal(sig,SIG_IGN);	fprintf(stderr,"\nHit by a sig %d\n\n",sig);#endif	clientSock = 3;	msqlDebug0(MOD_ANY,"Forced server shutdown due to bad signal!\n");	while(clientSock < maxCons + globalServer->config.cacheDescriptors)	{		if (conArray[clientSock].db)		{			printf("Forcing close on Socket %d\n",clientSock);			shutdown(clientSock,2);			close(clientSock);		}		clientSock++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -