?? tcpdmn.c
字號:
/**********************************************************************
程序名: tcpdmn.c
-----------------------------------------------------------------------
TCPIP daemon to call normal program. 服務器端程序
-----------------------------------------------------------------------
編制人:wsd編制時間:1999.08
**********************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <varargs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "tcpsvr.h"
static int Portno = 0 ;
static int Timeout = DEF_TIMEOUT;
static int ListenLen = DEF_LISTENLEN;
static int kill_daemon(void);
static void handle_request( int ,int );
int Rtv_Portid(int *,int) ;
static char logstr[200],CltAddr[20];
main(int argc, char *argv[])
{
int i;
int ret, port , portid ;
int optlen, optvar;
int sockid, newsockid, childpid, clilen ;
/**socklen_t clilen ; ***/
struct sockaddr_in cli_addr, localaddr;
for(i=1; i<argc; i++)
{
if ( !strcmp(argv[i],"-p") ) Portno = atoi( argv[++i] );
else
if ( !strcmp(argv[i],"-l") ) ListenLen = atoi( argv[++i] );
else
if ( strcmp(argv[i],"-k")==0 )
{
if( kill_daemon() )
fprintf(stderr,"\n%s: Kill TCPIP daemon failed!\n\n",argv[0]);
else
fprintf(stderr,"\nAll TCPIP daemon tcpdmn have been killed!\n\n"
);
return 0;
}
else
{
fprintf(stderr,"\nUsage: %s [-p portno] [-l listenbuflen] [-k].\n\n"
, argv[0]);
return 2;
}
}
if( !Portno )
{
printf("\n\n缺少參數 \n用法: \n tcpdmn <-p portno> [-l listenbuflen]
\n或\n tcpdmn <-k>\n\n") ;
return 1 ;
}
if (Timeout ==0) Timeout = DEF_TIMEOUT;
if (ListenLen==0) ListenLen = DEF_LISTENLEN;
port=Portno ;
if( Rtv_Portid(&portid,port) )
{
printf("\n取txjk的數據接口錯誤\n") ;
return 1 ;
}
printf("\n\ntcpdmn 通信端口:%d , 數據接口:%d\n\n",port,portid) ;
/* test if LOGDIR, FILDIR and BINDIR are set */
if (getenv("LOGDIR")==NULL || getenv("FILDIR")==NULL ||
getenv("BINDIR")==NULL )
{
fprintf(stderr, "\n%s: LOGDIR, FILDIR and BINDIR should be set.\n\n",
argv[0]);
return 1;
}
/* ignore the terminal stop signal */
signal(SIGINT, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
# ifdef SIGTTOU
signal(SIGTTOU, SIG_IGN);
# endif
# ifdef SIGTTIN
signal(SIGTTIN, SIG_IGN);
# endif
# ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
# endif
if( (childpid=fork())<0 )
{
fprintf(stderr, "\n%s: Can't fork first child process!\n\n", argv[0]);
return ( -1 );
}
else if( childpid > 0 ) /* parent exit */
return(0);
/* first child */
/* first child */
if(setpgrp() == -1)
{
fprintf(stderr, "\n%s: Can't change process group!\n\n", argv[0]);
return ( -1 );
}
signal(SIGHUP, SIG_IGN); /* immune from pgrp leader death */
errno = 0; /* probably got set to EBADF from a close */
chdir( "/" );
umask( 0 );
signal( SIGCLD, SIG_IGN );
/* create stream socket */
if( (sockid = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "\n%s: Creat socket failed!\n\n", argv[0]);
return ( -1 );
}
/* Allow socket option SO-REUSEADDR. */
optlen = sizeof(optvar);
optvar = 1;
ret = setsockopt(sockid,SOL_SOCKET,SO_REUSEADDR,(char *)&optvar,optlen);
if (ret == -1)
{
fprintf (stderr, "\n%s: setsockopt error!\n\n", argv[0]);
return(-1);
}
/* bind out local address */
memset( (char *)&localaddr, 0, sizeof(localaddr));
localaddr.sin_family = AF_INET;
localaddr.sin_addr.s_addr = htonl( INADDR_ANY );
localaddr.sin_port = htons( Portno );
if ( bind ( sockid, ( struct sockaddr * ) &localaddr,
sizeof(localaddr) ) < 0 )
{
fprintf (stderr, "\n%s: Bind failed because daemon already loaded!\n\n",
argv[0] );
close ( sockid );
return ( -1 );
}
/* Accept a connect request */
if ( listen ( sockid, ListenLen ) < 0 )
{
fprintf ( stderr, "\n%s: Listen socket port failed!\n\n", argv[0]);
close ( sockid );
return ( -1 );
}
while ( 1 )
{
memset( (char *)&cli_addr, 0, sizeof(cli_addr) );
clilen = sizeof(cli_addr);
newsockid = accept( sockid, ( struct sockaddr * ) &cli_addr, & clilen );
if ( newsockid < 0 )
{
vtcp_log("Accept request failed.");
continue;
}
sprintf( CltAddr, "%s",inet_ntoa(*(struct in_addr *)&(cli_addr.sin_addr.
s_addr)) );
sprintf( logstr, "Receive a request from %s: ", CltAddr);
errno =0;
while( (childpid=fork())<0 )
{
close ( newsockid );
vtcp_log("Fork process for a request failed.");
continue;
}
if( !childpid ) /* child process */
{
close( sockid );
handle_request( newsockid,portid);
close( newsockid );
return 0 ;
}
close( newsockid ); /* parent process */
} /* end socket manipulate */
}
static void handle_request( int sockid , int portid )
{
int rc;
char execstr[201] ;
char path[201],execfile[21];
char c_sock[21],c_port[21] ;
char cportid[6] ;
FILE *fp;
bzero(path,sizeof(path));
bzero(execfile,sizeof(execfile));
bzero(c_sock,sizeof(c_sock));
bzero(c_port,sizeof(c_port));
sprintf(path,"%s/%s",getenv("BINDIR"),getenv("EXEC_APP")) ;
sprintf(execfile,"%s",getenv("EXEC_APP")) ;
sprintf(c_sock,"%d",sockid) ;
sprintf(c_port,"%d",Portno) ;
sprintf(cportid,"%d",portid) ;
rc = execl(path,execfile,c_sock,CltAddr,c_port,cportid,(char *)0);
if (rc)
{
memset(execstr,0x00,sizeof(execstr));
sprintf(execstr,"%s %s %s %s %s %s",path,execfile,c_sock,CltAddr,c_port,c
portid) ;
vtcp_log("Execute [%s] failed: %s,%d.",execstr,strerror(errno),rc);
}
return ;
}
static int kill_daemon(void)
{
char fname[100];
char cmd[100];
FILE *fp;
int ret;
tmpnam( fname );
fp = fopen(fname,"w");
if (fp==NULL)
{
unlink(fname);
return -1;
}
fprintf(fp, "ps -aef | grep \"[-?].*tcpdmn\" >/tmp/$$ 2>/dev/null");
fprintf(fp, "\n");
fprintf(fp, "while read a1 a2 a3");
fprintf(fp, "\n");
fprintf(fp, "do");
fprintf(fp, "\n");
fprintf(fp, " kill -9 $a2 >/dev/null 2>&1");
fprintf(fp, "\n");
fprintf(fp, "done < /tmp/$$");
fprintf(fp, "\n");
fprintf(fp, "rm -f /tmp/$$");
fprintf(fp, "\n");
fclose( fp );
sprintf(cmd, "sh %s", fname);
ret = system( cmd );
unlink( fname );
return ret;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -