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

📄 main.c

📁 uClinux下用的数据库
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** Copyright (c) 2002  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.c,v 1.47 2004/02/01 04:59:05 bambi Exp $***//* #define DEBUG_BSD_MALLOC *//* #define DEBUG_MEMTRACE *//*** Module	: main** Purpose	: Normal single server backend** Exports	: ** Depends Upon	: *//**************************************************************************** STANDARD INCLUDES**************************************************************************/#include <common/config.h>#include <stdio.h>#include <stdlib.h>#include <time.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 <sys/stat.h>#include <sys/time.h>#include <sys/socket.h>#include <sys/un.h>#include <signal.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/socket.h>#include <netdb.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/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/main/memory.h>#include <msqld/lock/lock.h>#ifdef DEBUG_MEMTRACE#  include </usr/local/include/memtrace.h>#endif/**************************************************************************** GLOBAL VARIABLES**************************************************************************//**************************************************************************** PRIVATE ROUTINES**************************************************************************//**************************************************************************** PUBLIC ROUTINES**************************************************************************/extern  u_char  *yytext,		*yyprev;extern	int	yylineno;extern	int	yydebug;extern	char	errMsg[];extern	char	*packet;int	curSock,	numCons = 0,	maxCons = 0,	conCount = 0,	numKids = 0,	eintrCount = 0,	startTime,	explainOnly;u_int	serverStartTime = 0,	serverNumCons = 0,	serverNumQueries = 0;char	confFile[MSQL_PATH_LEN];msqld	*globalServer;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(server)	msqld	*server;{	int	tcpPort,		opt;	struct	sockaddr_un	unixAddr;	struct	sockaddr_in	ipAddr;	cacheSetupTableCache();        /*        ** Create an IP socket        */        if (configGetIntEntry("system", "remote_access"))        {                tcpPort = configGetIntEntry("general", "tcp_port");                msqlDebug1(MOD_GENERAL,"IP Socket is %d\n",tcpPort);                server->ipSock = socket(AF_INET, SOCK_STREAM, 0);                if (server->ipSock < 0)                {                        perror("Can't start server : IP Socket ");			exit(1);                }#               ifdef SO_REUSEADDR                opt = 1;                setsockopt(server->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(server->ipSock,(struct sockaddr *)&ipAddr,			sizeof(ipAddr)) < 0)                {                        perror("Can't start server : IP Bind ");			exit(1);                }                listen(server->ipSock,128);        }        else        {                server->ipSock = -1;        }	/*	** Setup the UNIX domain socket	*/	server->unixPort = strdup(configGetCharEntry("general","unix_port"));	msqlDebug1(MOD_GENERAL,"UNIX Socket is %s\n", server->unixPort);	server->unixSock = socket(AF_UNIX, SOCK_STREAM, 0);	if (server->unixSock < 0)	{		perror("Can't start server : UNIX Socket ");		exit(1);	}	bzero(&unixAddr, sizeof(unixAddr));	unixAddr.sun_family = AF_UNIX;	strcpy(unixAddr.sun_path, server->unixPort);	unlink(server->unixPort);	if( bind(server->unixSock, (struct sockaddr *)&unixAddr,		sizeof(unixAddr)) < 0)	{		perror("Can't start server : UNIX Bind ");		exit(1);	}	listen(server->unixSock,128);	chmod(server->unixPort,0777);}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 (globalServer->conArray[clientSock].db)		{			printf("Forcing close on Socket %d\n",clientSock);			shutdown(clientSock,2);			close(clientSock);		}		clientSock++;	}	if (globalServer->unixSock >= 0)	{		shutdown(globalServer->unixSock,2);		close(globalServer->unixSock);		unlink(globalServer->unixPort);		free(globalServer->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);		**		*/	}	memDropCaches();	printf("\n\nmSQL Daemon Shutdown Complete.\n\n");	if (sig >= 0)	{		exit(1);	}	return;}void freeClientConnection(server, sock)	msqld	*server;	int	sock;{	if(server->conArray[sock].db)	{		free(server->conArray[sock].db);		server->conArray[sock].db = NULL;	}	if(server->conArray[sock].host)	{		free(server->conArray[sock].host);		server->conArray[sock].host = NULL;	}	if(server->conArray[sock].user)	{		free(server->conArray[sock].user);		server->conArray[sock].user = NULL;	}	server->conArray[sock].sock = -1;	numCons--;}RETSIGTYPE puntClient(sig)	int	sig;{	extern	int netCurrentSock;	if(globalServer->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);	}	else	{		msqlDebug1(MOD_GENERAL,			"Closing client on socket %d\n", netCurrentSock);	}	shutdown(netCurrentSock,2);	close(netCurrentSock);	FD_CLR(netCurrentSock,&(globalServer->clientFDs));	freeClientConnection(globalServer, 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(server, sock)	msqld	*server;	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 (server->conArray[loop].user)		{			cTime = (int)(curTime - server->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, server->conArray[loop].user, 			    server->conArray[loop].host ?				server->conArray[loop].host:"UNIX Sock",			    server->conArray[loop].db ? 				server->conArray[loop].db : "No DB",			    buf,			/*			    (int)(curTime - conArray[loop].lastQuery)/60,			*/ 			    0,			    server->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);}

⌨️ 快捷键说明

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