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

📄 myftp_serv.c

📁 Linux下的ftp服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
	}
	else if (strcmp(argv,cmd_string_stop) == 0)
	{
		cmd_enum = stop_cmd;
		goto BACK;
	}
	else if (strcmp(argv,cmd_string_restart) == 0)
	{
		cmd_enum = restart_cmd;
		goto BACK;
	}
	else
		cmd_enum = undefine_cmd;
BACK:
	return cmd_enum;
}

/*=====================================================================================
function:判断用户输入的参数类型
======================================================================================*/
enum EVmyftpcmdflags myftp_chk_cmd(int argc, char* argv)
{
	if (1 == argc)
	{
		die("no arguments!\n用法:./zjwftp {start|stop|restart}");
	}
	else if (argc > 2)
	{
		die("many arguments!\n用法:./zjwftp {start|stop|restart}");
	}
	
	return myftp_serv_cmd_trans(argv);
	
}

/*=======================================================================
function:读取myftp_pid.pid文件中的pid值。
========================================================================*/
int myftp_read_pidfile(char* pid_str)
{
	int is_read_null;
	FILE *fp=fopen(myftp_pid_file,"r");
	if (!fp)
	{
		die("myftp_read_pidfile() open file myftp_pid.pid fail.\n");
	}
	if (fread(pid_str,sizeof(char),5,fp))
		is_read_null = FALSE;

	else
		is_read_null = TRUE;

	fclose(fp);
	return is_read_null;
}

/*=======================================================================
function:调用程序popen,以查找进程中是否有与pid_val相同的进程
=======================================================================*/
int myftp_call_popen(char* pid_str,char* pid_val)
{
	FILE *fd;
	int is_pidfile_empty;
	char PS_CMD[50]={0};

	strcpy(PS_CMD, "ps -ef|awk '{print $2}'|grep ");
	strcat(PS_CMD, pid_val);
	if((fd = popen(PS_CMD, "r")) == NULL) // 在进程表中查找PID_Value值 
	{
		die("call popen failed.\n");
	} 
	else 
	{//fgets读出的数据后面会有一个'\n',会导致strcmp函数输出错误的结果
		if(!fread(pid_str,sizeof(char),strlen(pid_val),fd))
		{
			is_pidfile_empty = TRUE;
		}
		else
			is_pidfile_empty = FALSE;
	}
	pclose(fd);
	return is_pidfile_empty;
}

/*=======================================================================
function:判断程序是否已经运行
=======================================================================*/
int myftp_if_running()
{
	int IS_running;
	char pid_str[10]={0};
	int pid_file_fd;

	if(!myftp_is_pidfile_exist())
	{
		IS_running = FALSE;
	}
	else
	{
		char pid_val[10]={0};
		if(myftp_read_pidfile(pid_val))//若读出来为空
		{
			IS_running = FALSE;
		}	
		else
		{
			if(myftp_call_popen(pid_str,pid_val))//读出的值为空
			{
				IS_running = FALSE;
				goto BACK;
			}
			else if(strcmp(pid_str,pid_val)==0)//也许不需要判断??
			{
				IS_running = TRUE;
			}
			else
				IS_running = FALSE;
		}
	}

BACK:
	return IS_running;
}

/*=====================================================================
function: 判断myftp_pid.pid文件是否存在
=====================================================================*/
int myftp_is_pidfile_exist()
{
	int IS_exist;
	if (-1 == access(myftp_pid_file,F_OK))//if file not exist
	{
		IS_exist = FALSE;
	}
	else
		IS_exist = TRUE;
	return IS_exist;
}

/*=====================================================================
function:停止ftp服务器
=====================================================================*/
void myftp_stop()
{
	int is_running=myftp_if_running();
	if (is_running == FALSE)
	{
		die("Program is not running.\n");
	}
	char pid_str[10]={0};
	myftp_read_pidfile(pid_str);
	if (kill((pid_t)atoi(pid_str),SIGKILL) == -1)
	{
		die("can not stop prog.\n");
	}
	truncate(myftp_pid_file,0);//清空文件内容
	puts("stop myftp ..............................................OK");
		
}

/*=========================================================================
function: 将进程号写入文件myftp_pid.pid中
=========================================================================*/
void myftp_write_pid_to_file()
{
	truncate(myftp_pid_file,0);//清空内容
	FILE *fp=fopen(myftp_pid_file,"w");
	char pid_str[10]={0};
	if(!fp)
	{
		die("3 open file myftp_pid.pid fail\n");
	}
	sprintf(pid_str,"%d",getpid());
	int write_count=fwrite(pid_str,sizeof(char),strlen(pid_str),fp);
	fclose(fp);
	if (!write_count)
	{
		die("write pid to file myftp_pid.pid fail\n");
	}
}

/*===================================================================
function:创建pid文件
===================================================================*/
void myftp_creat_pidfile()
{
	int pid_file_fd = open(myftp_pid_file,O_CREAT|O_EXCL|O_RDWR,0700);//O_CREAT|O_EXCL,可防止两个程序同时创建一个文件。
	if (-1 == pid_file_fd)
	{
		die("create file myftp_pid.pid fail!\n");
	}
}

/*=====================================================================
function:读取配置文件
======================================================================*/
void myftp_read_conf()
{
	FILE *fp;
	char buffer[128];
	int len = 0;
	int i = 0;
	int j = 0;
	char getStr[5][20]={'\0'};
	if (!(fp=fopen("zjwftp.conf","r")))
	{
		perror("read zjwftp.conf error\n");
	}
	while(fgets(buffer,128,fp)!= NULL)
	{
		if (buffer[0]=='\r' || buffer[0]=='\n' || buffer[0]=='#')   //删除空行和注释行
		{
			memset(buffer,0,sizeof(buffer));
			continue;
		}
		len = strlen(buffer);
		i = len -1;
		while(buffer[i]==' ' || buffer[i]=='\r' || buffer[i]=='\n')  //找到尾部第1个不是空格的
		{
			i--;
		}
		buffer[i+1] = '\0';	//去除右空格
		while(buffer[i]!='=')
			i--;
		strcpy(getStr[j],&buffer[i+1]);
		j++;
	}	
	if (strcmp(getStr[0],"yes")==0)//是否允许匿名登录设置
	{
		myftp_conf.is_allowed_anoymous=TRUE;
	}
	else
		myftp_conf.is_allowed_anoymous=FALSE;
	
	myftp_conf.file_trans_rate=(unsigned int)atoi(getStr[1]);//设置传输速率
	myftp_conf.listen_port=(unsigned short)atoi(getStr[2]);//监听端口号
	myftp_conf.timeout_val=(time_t)atoi(getStr[3]);//超时设置
	if (strcmp(getStr[4],"yes")==0)//是否允许root用户登录
	{
		myftp_conf.is_allowed_root=TRUE;
	}
	else
		myftp_conf.is_allowed_root=FALSE;
	fclose(fp);
}

/*====================================================================
function:启动ftp服务器
====================================================================*/
void myftp_start()
{
	int is_running=myftp_if_running();
	if(is_running == TRUE)
	{
		die("Program  already is running.\n");
	}
	else
	{	
		puts("start myftp .............................................OK");
		
		myftp_read_conf();
		//myftp_open_
		init_ftp_daemon();
		myftp_main_server();
	}
}

/*=====================================================================
function:命令行参数处理,根据此参数,决定是启动、停止、还是重启。
======================================================================*/
void myftp_arg_handler(enum EVmyftpcmdflags cmd_enum)
{
	switch (cmd_enum)
	{
	case start_cmd:
		myftp_start();
		break;
	case stop_cmd:
		myftp_stop();
		break;
	case restart_cmd:
		myftp_stop();
		myftp_start();
		break;
	case undefine_cmd:
		die("command not found\n用法:./zjwftp {start|stop|restart}");
	}
}

/*===================================================================
function:主函数
===================================================================*/
int main(int argc, char** argv)
{
	umask(0023);
	enum EVmyftpcmdflags cmd_enum=myftp_chk_cmd(argc,argv[1]);
	myftp_arg_handler(cmd_enum);
	
}

⌨️ 快捷键说明

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