tools.c

来自「Magic C++!的源代码」· C语言 代码 · 共 972 行 · 第 1/2 页

C
972
字号
/* tool.c -- Magic C++ utilities programme   (Mostly) portable public-domain implementation   -- Copyright(C) 2003 Magicunix Infomation Technology Limited    This file is part of magicd.   magicd is free software; you can redistribute it and/or   modify it under the terms of the GNU General Public   License as published by the Free Software Foundation; either   version 2 of the License, or (at your option) any later version.	    For details, see the Magic C++ World-Wide-Web page,    `http://www.magicunix.com',   or send a mail to the Magic C++ developers <support@magicunix.com>. */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <stdarg.h>#include <string.h>#include <ctype.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/sem.h>#include <sys/shm.h>#include <time.h>#include <sys/time.h>#include <sys/stat.h>#include <string.h>#include <unistd.h>#include "config.h"#include "tools.h"extern int debug;   /*from magicd.c*/extern char szInstallDir[ 200 ]; /*from info.h*/#ifdef SOLARIS struct tm *localtime_r(const time_t *clock, struct tm *res);#endif#if /*defined(__GNU_LIBRARY__) &&*/ !defined(_SEM_SEMUN_UNDEFINED)       /* union semun is defined by including <sys/sem.h> */#else       /* according to X/OPEN we have to define it ourselves */       union semun {             int val;                  /* value for SETVAL */             struct semid_ds *buf;     /* buffer for IPC_STAT, IPC_SET */             unsigned short *array;    /* array for GETALL, SETALL */                                       /* Linux specific part: */             struct seminfo *__buf;    /* buffer for IPC_INFO */       };#endif       int util_err_log(char * info ,char *  filename , int line , long err){	return util_log( "Programme error: %s :filename:%s , line :%d , error:%s",		info,filename , line , strerror( err ) );}/*output log infomation to log fileusage: util_log("%s%s",......);output [time] ...*/int util_log(  const char *fmt, ...){	FILE *lfd;	char	lname[50];	time_t	dt;	struct	tm dc;	va_list args;	char buf[102400];	int i;	int max_fd;	char *envhome;	memset( buf , 0 , sizeof( buf ));	va_start(args, fmt);	i=vsprintf(buf,fmt,args);	va_end(args);	/*get time*/	memset(lname,0,sizeof(lname));	time(&dt);	localtime_r(&dt,&dc);	/*not initialize installation path yet*/	if( szInstallDir[ 0 ] != '/' )	{		fprintf(stderr,"[%04d%02d%02d %02d:%02d:%02d]|%s|%d|%s\n",dc.tm_year+1900,dc.tm_mon+1,dc.tm_mday,dc.tm_hour,dc.tm_min,dc.tm_sec, __FILE__ ,__LINE__,strerror( errno ));		return -1;	}	sprintf( lname , "%s/log/dup.log"  , szInstallDir);		if ( (lfd = fopen(lname,"a+")) == NULL )	{		lfd = stderr;		max_fd = sysconf(_SC_OPEN_MAX);		for(i=3;i<max_fd;i++)			close(i);		fprintf(lfd,"[%04d%02d%02d %02d:%02d:%02d]|%s|%d|%s\n",dc.tm_year+1900,dc.tm_mon+1,dc.tm_mday,dc.tm_hour,dc.tm_min,dc.tm_sec, __FILE__ ,__LINE__,strerror( errno ));		return -1;			}	fprintf(lfd,"[%04d%02d%02d %02d:%02d:%02d]%s\n",dc.tm_year+1900,dc.tm_mon+1,dc.tm_mday,dc.tm_hour,dc.tm_min,dc.tm_sec,buf); 	fflush(lfd);	fclose(lfd);		return 1;}/*initialize semaphore and shared memory*/int util_ipc_init(){	if( util_sem_init() == -1 )	{		util_log("Init semphone failed!\n");		return -1;	}	if( util_shm_init() == -1 )	{		util_log("Init share memory failed!\n");		return -1;	}	return 0;}key_t	shmkey;key_t   semkey;int util_sem_init(){	char lname[100];	FILE *lfd;	char *envhome;	int semid;	u_short	init[16];	semid = util_getsem( IPC_CREAT|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP| S_IROTH|S_IWOTH );	if( semid == -1 )		return -1;	init[0]=1;	/*write thie semid to a record file*/	if( szInstallDir[ 0 ] != '/' )	{		fprintf(stderr,"Can not get install dir\n");		return -1;	}	sprintf( lname , "%s/log/sem.log" , szInstallDir);	lfd = fopen( lname , "w+" );	if( lfd == NULL )	{		util_err_log("Can not open sem log file!\n",__FILE__,__LINE__,errno );			}	fprintf( lfd , "%d\n", semid );	fclose( lfd );	if ( semctl(semid, 1, SETALL, init) == -1 )	{		util_err_log("semctl() error!",__FILE__,__LINE__,errno);		return -1;	}	util_log("Init Semaphore finished...\n");	return 0;	}int util_shm_init(){	char lname[100];	int shmid;	FILE *lfd;	int *global;	char * envhome;	shmid = util_getshm( IPC_CREAT|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP| S_IROTH|S_IWOTH  );	if( shmid == -1 )		return -1;	/*write semid to log file*/	if( szInstallDir[ 0 ] != '/' )	{		fprintf(stderr,"Can not get install dir\n");		return -1;	}	sprintf( lname , "%s/log/shm.log" , szInstallDir );	lfd = fopen( lname , "w+" );	if( lfd == NULL )	{		util_err_log("Can not open sem log file!\n",__FILE__,__LINE__,errno );			}	fprintf( lfd , "%d\n", shmid );	fclose( lfd );	if( ( global = (int *)shmat(shmid , 0 , 0 ) ) == (void *)-1 )	{		util_err_log("shmat() error!",__FILE__,__LINE__,errno);		return -1;			}	*global = 0;	shmdt( (char *)global );	util_log("Init global finished...\n");	return 0;}/*lock the shared memory*//*nMillionSec -- timeout (usecond)*/int util_sem_lock(int nMillionSec){	int	semid;	struct	sembuf lock[1];	int times;	semid = util_getsem( S_IRUSR|S_IWUSR );	if( semid == -1 )		return -1;	lock[0].sem_num=0;	lock[0].sem_op=-1;	lock[0].sem_flg=IPC_NOWAIT;	times = 0;	/*retry three times*/	while( times < RETRY_TIMES )	{		if ( semop(semid, lock, 1) == -1 )		{			/*retry*/			if( errno == EAGAIN )			{				/*sleep 1/5 of timeout duration*/				usleep( nMillionSec * 1000 / RETRY_TIMES  );				times ++;				continue;			}			else			{				util_err_log("semop error!" , __FILE__,__LINE__,errno);				return -1;			}		}		else		{			break;		}	}	if( times == RETRY_TIMES )	{		return -1;	}	return 0;}/*unlock semaphore*/int util_sem_unlock(){	int	semid;	int z;			union semun un;	semid = util_getsem( S_IRUSR|S_IWUSR );	if( semid == -1 )		return -1;	un.val = 1;	z = semctl(semid, 0, SETVAL, un)  ;	if( z == -1 )	{		util_err_log("semctl getval error!", __FILE__,__LINE__,errno);		return -1;	}	return 0;}/*add shared memory by 1*//*if it is up to limitation,return -1 */int util_add_global(int maxval){	int *global;	int shmid;	shmid = util_getshm(S_IRUSR|S_IWUSR);	if( shmid == -1 )		return -1;	if( ( global = (int *)shmat(shmid , 0 , 0 ) ) == (void *)-1 )	{		util_err_log("shmat() error!",__FILE__,__LINE__,errno);		return 0;			}	if( util_sem_lock(1000) == -1 )	{		util_err_log( "Can not get lock!",__FILE__,__LINE__,errno );		return -1;			}	if( *global >= maxval )	{				memcpy( global , &maxval , sizeof( int ));		util_log("The Licence count is up to limit!Can not apply any more...\n");		util_sem_unlock();		return -1;	}	else	{		int ntmp;		ntmp = *global + 1;		memcpy( global , &ntmp , sizeof( int ));		util_log("Apply a Licence,Current Licence count is %d Now...\n",*global);	}	shmdt((char *) global );	util_sem_unlock();	return 0;		}   /*subtract value in shared memory ,if it's 0,return -1 */int util_subtract_global(){	int *global;	int shmid;	shmid = util_getshm( S_IRUSR|S_IWUSR );	if( shmid == -1 )		return -1;	if( ( global = (int *)shmat(shmid , 0 , 0 ) ) == (void *)-1 )	{		util_err_log("shmat() error!",__FILE__,__LINE__,errno);		return 0;			}		if( util_sem_lock(1000) == -1 )	{		util_err_log( "Can not get lock!",__FILE__,__LINE__,errno );		return -1;			}	if( *global <= 0 )	{		int ntmp = 0;		memcpy( global , &ntmp , sizeof( int ));		util_log("The Licence Count is zero Now...Can not release any more...\n");		util_sem_unlock();		return -1;	}	else	{		int ntmp = *global - 1;		memcpy( global , &ntmp , sizeof( int ));		util_log("Release a licence...Current Licence count is %d Now...\n",*global);			}	shmdt((char *) global );	util_sem_unlock();	return 0;		}int util_getshm(int flag){	 	char	keyfile[100];	int	shmid;	int *global;		memset(keyfile,0,sizeof(keyfile));	if( szInstallDir[ 0 ] != '/' )	{		fprintf(stderr,"Can not get install dir\n");		return -1;	}	sprintf( keyfile , "%s/etc/global.etc" , szInstallDir);	shmkey = ftok( keyfile,'Y' );	if ( shmkey == -1 )	{		util_err_log("ftok() error!" ,__FILE__,__LINE__,errno);		return -1;	}	if ( ( shmid = shmget(shmkey, sizeof(int) , flag) ) < 0 )	{		util_err_log("shmget() error!" ,__FILE__,__LINE__,errno);		return -1;	}	return shmid;}int util_getsem(int flag){ 		char	keyfile[100];	int	semid;	u_short	init[16];	memset(keyfile,0,sizeof(keyfile));	if( szInstallDir[ 0 ] != '/' )	{		fprintf(stderr,"Can not get install dir\n");		return -1;	}	sprintf( keyfile , "%s/etc/sems.etc" , szInstallDir );	semkey = ftok( keyfile,'Y' );	if ( semkey == -1 )	{		util_err_log("ftok() error!" ,__FILE__,__LINE__,errno);		return -1;	}	if ( ( semid = semget(semkey, 1 , flag) ) < 0 )	{		util_err_log("semget() error!" ,__FILE__,__LINE__,errno);		return -1;	}	return semid;	}/*remove semaphore*/int util_ipc_remove(){	if( util_sem_remove() == -1 )	{		util_log("remove semphone failed!\n");		return -1;	}		if( util_shm_remove() ==  -1 )	{		util_log("Remove Share memmory failed!\n");		return -1;			}	return 0;		}int util_sem_remove(){	int semid ;	semid = util_getsem(S_IRUSR|S_IWUSR);	if( semid == -1 )		return -1;	if ( semctl(semid, 0 , IPC_RMID) == -1 )	{		util_err_log("semctl() error!", __FILE__,__LINE__,errno);		return -1;	}		return 0; 	}int util_shm_remove(){		int	shmid;	shmid = util_getshm(S_IRUSR|S_IWUSR);	if( shmid == -1 )		return -1;	if ( shmctl(shmid, 0 , (void *)IPC_RMID) == -1 )	{		util_err_log("shmctl() error!", __FILE__,__LINE__,errno);		return -1;	}	return 0;	}/*get a int value in configsection - name of sectionkey -   name of keyvalue - value of key*/int util_getinfo_int(char *section , char *key , int *nvalue , char * szFileName){	char szvalue[100];	memset( szvalue , 0 , sizeof( szvalue));	if( util_getinfo_str( section , key , szvalue , szFileName) == -1 )	{		*nvalue = 0;		return -1;	}	*nvalue = atoi( szvalue );	if( errno == EINVAL || errno == ERANGE)		return -1;	return 0;

⌨️ 快捷键说明

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