Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A program that outputs error five times when a password has not been entered for 30 seconds, and stops login attempts for 10 minutes. What
A program that outputs error five times when a password has not been entered for 30 seconds, and stops login attempts for 10 minutes.
What should I do if I didn't enter my password for 30 seconds?
#include#include #include #include #include #include #include #define BUFFLEN_MAX 4096 void signup(int sig){ char buf_ID[BUFFLEN_MAX], buf_password[BUFFLEN_MAX]; char *admin_ID = "root"; char use_ID[BUFFLEN_MAX]; FILE* fp; int fd; fd = open("account.txt", O_RDWR | O_CREAT, 0644); printf("sign up "); int k; while(1){ lseek(fd,0,SEEK_SET); fp = fdopen(fd,"r"); k=0; printf("insert id: "); scanf("%s",buf_ID); printf("insert password: "); scanf("%s",buf_password); if(strcmp(buf_ID,admin_ID)==0){ printf("insert other id "); continue; } while((fgets(use_ID, BUFFLEN_MAX, fp))!=NULL){ use_ID[strlen(use_ID)-1] = '\0'; if(strcmp(use_ID,buf_ID)==0 && k==0) { k=1; printf("insert other id "); break; } if((fgets(use_ID, BUFFLEN_MAX, fp))==NULL) break; memset(use_ID, '\0', BUFFLEN_MAX); } if(k==0) break; } char new_line = ' '; write(fd,buf_ID,strlen(buf_ID)); write(fd,&new_line, sizeof(new_line)); write(fd,buf_password,strlen(buf_password)); write(fd,&new_line, sizeof(new_line)); printf("Success "); close(fd); fclose(fp); } void alarm_handler(int singno){ printf(" Fail to login "); exit(0); } void login(int sig){ char buf_ID[BUFFLEN_MAX], buf_password[BUFFLEN_MAX]; char use_ID[BUFFLEN_MAX], use_password[BUFFLEN_MAX]; char *admin_ID = "root", *admin_password = "root"; FILE* fp; int fd; signal(SIGALRM, alarm_handler); fd = open("account.txt", O_RDWR | O_CREAT, 0644); printf("log in "); int k, try=5; int i=0; while(try--){ lseek(fd,0,SEEK_SET); fp = fdopen(fd,"r"); k=0; printf("insert id: "); alarm(30); scanf("%s",buf_ID); alarm(0); printf("PASSWORD: "); alarm(30); scanf("%s",buf_password); alarm(0); if(strcmp(buf_ID,admin_ID)==0 && strcmp(buf_password,admin_password)==0){ k=1; break; } while((fgets(use_ID, BUFFLEN_MAX, fp))!=NULL){ fgets(use_password, BUFFLEN_MAX, fp); use_ID[strlen(use_ID)-1] = '\0'; use_password[strlen(use_password)-1] = '\0'; if(strcmp(use_ID,buf_ID) == 0 && strcmp(use_password,buf_password) == 0 ){ k=1; break; } memset(use_ID, '\0', BUFFLEN_MAX); memset(use_password,'\0', BUFFLEN_MAX); } if(k==1) break; printf("You entered wrong ID OR PASSWORD!!! "); } if(k==0) printf("login failed "); else if(k==1){ printf("Success "); } close(fd); fclose(fp); exit(0); } void selec(int signo){ int sel; printf("1. sign up 2. login "); scanf("%d",&sel); if(sel == 1) kill(getppid(), SIGUSR2); else if(sel == 2) kill(getppid(), SIGCONT); } int main(int argc, char* argv[]){ setbuf(stdout,NULL); pid_t pid; int fd; signal(SIGUSR1, selec); signal(SIGUSR2, signup); signal(SIGCONT, login); fd = creat("account.txt",0644); close(fd); if((pid = fork()) < 0){ perror("fork error"); exit(0); } if(pid != 0){ while(1){ pause(); kill(pid,SIGUSR1); } }else { kill(getpid(),SIGUSR1); while(1) pause(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started