📄 pub_fun.c
字号:
write(1,".",1); usleep(10000); } write(1,"[ OK ]\n",strlen("[ OK ]\n")); daemon(1,0); startfun(); return 0;}/*---------------------------------------------------------------------------------------------------------*//*FTP 重启*/int Ftp_Restart(char *appname, int *startfun()){ int iret=0; iret = Ftp_Stop(appname); usleep(100000); iret = Ftp_Start(appname, startfun); return iret;}/*---------------------------------------------------------------------------------------------------------*//*FTP 停止*/int Ftp_Stop(char *appname){ int thisid=0; thisid=getpid(); char buff[1024]={0}; Get_Pid(appname, buff, 1024); char tmppid[256]; char *p=buff; int tmpid,i=0; if (!strchr(buff,' ')) {//当前只有一个进程 printf( "[ Services are not running ]\n"); return -1; } char *strtmp="Ftp Server Stop "; write(1,strtmp,strlen(strtmp)); while (*p) { while (isspace(*p) && *p)p++; i=0; while (!isspace(*p) && *p) tmppid[i++]=*p++; if(*p == '\0')break; tmpid = atoi(tmppid); if (thisid != tmpid) { kill(tmpid, SIGTERM); //杀死进程 } } int pointcount=20; for (; pointcount>0; pointcount--) { write(1, ".", 1); usleep(10000); } write(1, "[ OK ]\n", strlen("[ OK ]\n")); return 0;}/*---------------------------------------------------------------------------------------------------------*//*获取进程PID*/int Get_Pid(char *appname,char *strpids, int bufflen){ int pips[2]; pipe(pips); switch(fork()) { case 0: close(1); if (dup(pips[1])==-1) { perror("dup"); } execlp("pidof","pidof",appname,0); return -1; break; case -1: exit(-1); break; default: close(pips[1]); read(pips[0],strpids,bufflen); return 0; break; }}/*---------------------------------------------------------------------------------------------------------*//*出错信息*/int errlog(char *logfile,char *msg){ FILE *fp; char *logpath=(logfile!=NULL)?logfile:"errorlog.log"; if ((fp=fopen(logpath,"a"))!=NULL) { fputs(msg, fp); fputs("\n", fp); fclose(fp); return 0; } return -1;}/*-----------------------------------------------------------------------------------------------------*//*去除左空格*/void LTrim(char *ch){ char *buff=ch; for(;;){ if (buff[0] ==' ' || '\t'==buff[0]) { buff++; } else break; } strcpy(ch, buff);}/*-----------------------------------------------------------------------------------------------------*//*去除右空格*/void RTrim(char *ch){ int i; for ( i=strlen(ch)-1; i>=0; i--) { if (ch[i]==' ' || ch[i]=='\t' || ch[i]=='\n' || ch[i]=='\r') ch[i]='\0'; else break; }}/*-----------------------------------------------------------------------------------------------------*//*读取所有配置信息*/void Read_Conf(){ FILE *fp_opt; FILE *fp_user; int i,j; char *p, *value; char *temp[] = { " ", "enable_anonymous", "max_upload_speed", "max_download_speed", "timeout", "enable_userlist", "ip", "port", 0 }; char buf[256]; memset(buf, 0, 256); //打开系统信息配置文件 fp_opt = fopen("opt.conf", "r"); if(fp_opt == NULL) { printf("opt.conf open error"); } while(fgets(buf, 256, fp_opt) != NULL) { LTrim(buf); RTrim(buf); if((buf[0] == '#') || strlen(buf) == 0 ) { continue; } i=0; p = strtok(buf,"="); if(p == NULL) { continue; } LTrim(p); RTrim(p); for (j=1; j<=7; j++) { if (strncmp(p, temp[j],strlen(temp[j]))==0) { i = j; break; } } value=strtok(0,"="); if ((value == NULL) || (!isascii(value[0]))) { continue; } LTrim(value); RTrim(value); switch (i) { case 1: ENBALE_ANONYMOUS = atoi(value); break; case 2: MAX_UPLOAD_SPEED = atoi(value); break; case 3: MIN_DOWNLOAD_SPEED = atoi(value); break; case 4: TIME_OUT = atoi(value); break; case 5: ENABLE_USERLIST = atoi(value); break; case 6: strcpy(IP, value); break; case 7: PORT = atoi(value); break; } memset(buf, 0, 256); } fclose(fp_opt); //打开可登录用户配置文件 fp_user = fopen("userlist.conf", "r"); if(fp_user == NULL) { printf("userlist.conf open error"); } memset(buf, 0, 256); i = 0; while(fgets(buf, 256, fp_user) != NULL) { LTrim(buf); RTrim(buf); if((buf[0] == '#') || strlen(buf) == 0 ) { continue; } if(p == NULL) { continue; } strcpy(ENABLE_LOGIN_USER[i], p); i++; } fclose(fp_user);}/*-----------------------------------------------------------------------------------------------------*//*验证用户名和密码*/int Check_User(char *name, char *password, int *uid, int *gid, char *dir, int state){ //只有root用户才可以访问/etc/shadow文件 //把当前进程设置为有效用户为root seteuid(0); setegid(0); //取出/etc/shadow密码文件的用户密码 struct spwd *spw=getspnam(name); char *passwd; char tmp[13]; if (spw == NULL || (passwd = spw->sp_pwdp) == NULL) { return -1; } else { //要对密码进行验证的情况 if(state == 1) { //取出公钥 strncpy(tmp, passwd, 12); if (strcmp(passwd, crypt(password, tmp)) !=0) { return -2; } else { //验证通过 //获取该用户的UID和GID struct passwd *pw=getpwnam(name); if (pw==NULL) return -3; *uid = pw->pw_uid; *gid = pw->pw_gid; dir = pw->pw_dir; return 1; } } //直接读取数据的情况 else { struct passwd *pw=getpwnam(name); if (pw==NULL) return -3; *uid = pw->pw_uid; *gid = pw->pw_gid; strcpy(dir, pw->pw_dir); return 1; } } }/*-----------------------------------------------------------------------------------------------------*//*转换IP*/void Ip_Change(char *buf){ int i; for(i=0; i<strlen(buf) ;i++) { if(buf[i] == '.') { buf[i] = ','; } }}/*-----------------------------------------------------------------------------------------------------*//*信号处理的句柄注册*/int Reg_Signal(void (*Handle_Signal)(int)){ signal(SIGHUP, SIG_IGN); signal(SIGINT, Handle_Signal); signal(SIGQUIT, Handle_Signal); signal(SIGTERM, Handle_Signal); signal(SIGCHLD, SIG_IGN); return 1;}/*-----------------------------------------------------------------------------------------------------*//*信号处理函数*/void Handle_Signal(int sig_num){ //删除共享内存 shmdt(share_memory); shmctl(SHM_ID, IPC_RMID, 0); close(SOCKFD); close(CMD_SOCKFD); exit(0);}/*-----------------------------------------------------------------------------------------------------*//*创建共享内存*/void Init_Memory(){ void *share_mem = (void *)0; //创建一个共享内存 SHM_ID = shmget((key_t)1234, sizeof(struct Share_Memory), 0666|IPC_CREAT); if(SHM_ID == -1) { printf("Init_Memory shmget error\n"); exit(-1); } share_mem = shmat(SHM_ID, (void *)0, 0); if(share_mem == (void *)-1) { printf("Init_Memory shmat error \n"); exit(-2); } share_memory = (struct Share_Memory *)share_mem;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -