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

📄 auctl.c

📁 采用非对称密钥加密方式的应用系统认证系统
💻 C
字号:
#include "common.h"



CTL_INFO *lpCtlInfo=NULL;

TAIL_INFO *lpTailInfo=NULL;

int       CtlShmid;

int       TailShmid;





/************************************************************/ 

void CommonShow(char *w)

{

    /*

    char message[200];

    GetSysTime(message);

    sprintf(message+14," : %s\n",w);

    printf(message);*/

    printf(w);   

} 

/************************************************************/ 

int GetTailShm()

{

  int iShmSize;

  int ShmID;

  void *pShm;

   

 

  iShmSize=sizeof(CTL_INFO);

  if ((ShmID=shmget((key_t)CTL_SHM_KEY,iShmSize,0666))<0) {

      CommonShow("shmget: CTL_SHM_KEY\n");

      return(-1);

  }   

  if ((pShm=shmat(ShmID,NULL,0))==(void *)-1) {

      CommonShow("shmat: CTL_SHM_KEY\n");

      return(-2);

  }

  lpCtlInfo=(CTL_INFO *)pShm;

  CtlShmid=ShmID;

  

  iShmSize=sizeof(TAIL_INFO);

  if ((ShmID=shmget((key_t)TAIL_SHM_KEY,sizeof(TAIL_INFO),S_IRUSR))<0) {

      CommonShow("shmget: TAIL_SHM_KEY\n");

      return(-3);

  }   

  if ((pShm=shmat(ShmID,NULL,SHM_RDONLY))==(void *)-1) {

      CommonShow("shmat: TAIL_SHM_KEY\n");

      return(-4);

  }

  lpTailInfo=(TAIL_INFO *)pShm;

  TailShmid=ShmID;

  return(0);

}

void FreeClientTailShm()

{

  shmdt((void*)lpCtlInfo);

  shmdt((void*)lpTailInfo);

}

/************************************************************/ 

void DoStart(char *ProgramName, int UseDefault)

{

  char CmdLine[100];

  FILE *fp;

  

  if (DoStatus(ProgramName)==0) {

      CommonShow("系统已经启动!\n");

  }

  else {

      system("chmod +x authen"); 

      if (UseDefault) strcpy(CmdLine,"./authen default");  

      else strcpy(CmdLine,"./authen");  

      if (system(CmdLine)<0) {

          CommonShow("要成功启动系统,必须保证authen文件在当前目录!\n");

      } 

      system("chmod -x authen"); 

  }  

  

}

/************************************************************/ 

void DoStop()

{

  if (lpCtlInfo) {

      lpCtlInfo->IsStop=1;

      CommonShow("正在等待服务停止,请稍侯......\n");

      kill((pid_t)lpCtlInfo->pid,SIGINT);

      shmctl(CtlShmid, IPC_RMID, NULL);

      shmctl(TailShmid, IPC_RMID, NULL);

      sleep(1);

      CommonShow("正在等待服务停止,请稍侯......\n");

      sleep(1);

      CommonShow("服务已停止......\n");

  }

  else CommonShow("调用出错。\n");

}

/************************************************************/ 

void DoLog()

{

  if (lpCtlInfo) {

      if (lpCtlInfo->IsLog!=1) {

          CommonShow("服务开始写日志文件。\n");

          lpCtlInfo->IsLog=1;

      }

      else CommonShow("服务已经在写日志文件。\n");

  }

  else CommonShow("调用出错。\n");

}

/************************************************************/ 

void DoNolog()

{

  if (lpCtlInfo) {

      if (lpCtlInfo->IsLog==1) {

          CommonShow("服务停止写日志文件。\n");

          lpCtlInfo->IsLog=0;

      }

      else CommonShow("服务已经停止写日志文件。\n");

  }

  else CommonShow("调用出错。\n");

}

/************************************************************/ 

void DoTail()

{

  int iCur;

  struct timestruc_t sleeptime;  

  

  if (lpTailInfo==NULL) {

      CommonShow("调用出错。\n");

      return;

  }

  sleeptime.tv_sec=2;

  sleeptime.tv_nsec=0;

  iCur=lpTailInfo->pos+1;      

  if (iCur>=MAX_TAIL_ROWS) iCur=0;

  while(1) {

      if (iCur > lpTailInfo->pos) {

          for(;iCur<MAX_TAIL_ROWS;iCur++) 

              if (*(lpTailInfo->queue[iCur].data)) CommonShow(lpTailInfo->queue[iCur].data);

          iCur=0;            

      } 

      else {

          for(;iCur<lpTailInfo->pos;iCur++)

              if (*(lpTailInfo->queue[iCur].data)) CommonShow(lpTailInfo->queue[iCur].data);

          }

      if (nsleep(&sleeptime,0)<0) {

          if (errno==EINTR) break;

      }

      if (DoStatus()!=0) {

          CommonShow("服务已停止运行......\n");

          break;

      }

  }

}

/************************************************************/ 

int DoStatus(char *ProgramName)

{

/*

  char CmdLine[100];

  FILE *fp;

  

  bzero(CmdLine,100);

  sprintf(CmdLine,"ps -ef|grep %s > auctl.tmp", ProgramName);     

  if (system(CmdLine)<0) {

      CommonShow("System error!");

      return;

  }

  if ((fp=fopen("authenctl.tmp","r"))==NULL) {

      CommonShow("System error!");

      return;

  }

  fgets(CmdLine,100,fp);

  if (fgets(CmdLine,100,fp)) 

      return(0);

  else return(-1);

  */

  int CtlShmid;

  int iShmSize=sizeof(CTL_INFO);

  if (shmget((key_t)CTL_SHM_KEY,iShmSize,0666)<0)

      return(-1);

  else return(0);



}

/************************************************************/ 

void DoHelp(char *ProgramName)

{

      printf("Usage:\n");

      printf("%s start        (启动服务)\n",ProgramName);

      printf("%s default      (使用默认数据库用户和口令启动服务)\n",ProgramName);

      printf("%s stop         (停止服务)\n",ProgramName);

      printf("%s log          (开始写日志文件)\n",ProgramName);

      printf("%s nolog        (暂停写日志文件)\n",ProgramName);

      printf("%s tail         (跟踪服务消息)\n",ProgramName);

      printf("%s status       (显示服务运行状态)\n",ProgramName);

}

/************************************************************/ 

void main(int argc,char **argv)

{

  

  if (argc==2 && strcmp(argv[1],"start")==0) {

      DoStart("authen",0);

  }

  else if (argc==2 && strcmp(argv[1], "default")==0) {

      DoStart("authen",1);

  }

  else if (argc==2 && strcmp(argv[1], "status")==0) {

      if (DoStatus("authen")==0) CommonShow("服务正在运行......\n");

      else CommonShow("服务已停止运行......\n");

  } 

  else {

  

  

  if (DoStatus("authen")!=0) {

      CommonShow("服务已停止运行......\n");      

      DoHelp(argv[0]);

  }

  else {        

      GetTailShm();

      if (argc==2 && strcmp(argv[1], "stop")==0) {

          DoStop();  

      }

      else if (argc==2 && strcmp(argv[1], "log")==0) {

          DoLog();

      }

      else if (argc==2 && strcmp(argv[1], "nolog")==0) {

          DoNolog();

      }

      else if (argc==2 && strcmp(argv[1], "tail")==0) {

          DoTail();

      }

      else {  /*help*/

          DoHelp(argv[0]);

      }

      FreeClientTailShm();  

  }

  

  

  }

}  

/************************************************************/   

  

  

  

  

⌨️ 快捷键说明

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