Newbie here, have looked using search engine and can find nothing revelant to my problem..
Using Linux
Here, I want to create a unix daemon (using C) that run to check user idle time, then if the idle time match the timeout, that console will be locked automatically
(user need to enter password to unlock it, and the password is determined in C code and not using real user password).
Here's the C code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#define PSW_PATH "/home/abuzaid/Script/CSiSop/modul3/wajib/pkey"
#define LOG_PATH "/home/abuzaid/Script/CSiSop/modul3/wajib/log"
#define TIME_PATH "/home/abuzaid/Script/CSiSop/modul3/wajib/time"
#define SCRIPT_PATH "/home/abuzaid/Script/CSiSop/modul3/wajib/lockscript.sh"
#define PASS_PATH "/home/abuzaid/Script/CSiSop/modul3/wajib/checkpass.sh"
#define PASSWORD "well"
void check_file(char argv[10]) {
FILE *idleTime;
FILE *myLog;
char time[10];
if((idleTime = fopen(TIME_PATH, "r")) == NULL)
exit(EXIT_FAILURE);
if((fscanf(idleTime, "%s", time)) == -1) {
if((myLog = fopen(LOG_PATH, "a")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "Error reading!\n");
fclose(myLog);
}
fclose(idleTime);
if((myLog = fopen(LOG_PATH, "a")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "%s vs %s\n", time, argv);
fclose(myLog);
if((strcmp(time, argv)) == 0) { //THIS PART IS GONNA USED TO LOCK THE CONSOLE
if((myLog = fopen(LOG_PATH, "a")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "DO IT NOW\n");
fclose(myLog);
}
printf("\ntes");
}
void daemonTask(char argv[10]) {
FILE *myLog;
while(1) {
int status;
int rV = 0;
pid_t pid;
pid = fork();
switch(pid) {
case -1:
if((myLog = fopen(LOG_PATH, "a")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "MasyaAllah gagal membuat proses untuk exec\n");
fclose(myLog);
exit(EXIT_FAILURE);
break;
case 0:
rV = execl(SCRIPT_PATH, SCRIPT_PATH, 0, (char *) 0);
if(rV == -1) {
if((myLog = fopen(LOG_PATH, "a")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "MasyaAllah anak gagal melakukan exec\n");
fclose(myLog);
}
_exit(1);
break;
default:
wait(&status);
kill(pid, SIGKILL);
}
check_file(argv);
sleep (10);
}
}
int main(int argc, char *argv[]) {
FILE *myLog;
pid_t pid,
sid;
char *time = argv[1];
pid = fork();
if(pid < 0) {
exit(EXIT_FAILURE);
}
if(pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if(sid < 0) {
if((myLog = fopen(LOG_PATH, "w")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "MasyaAllah SID gagal didapatkan.\n");
fclose(myLog);
exit(EXIT_FAILURE);
}
else {
if((myLog = fopen(LOG_PATH, "w")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "Alhamdulillah daemon mulai dengan SID %d.\n", sid);
fclose(myLog);
}
if((chdir("/")) < 0) {
if((myLog = fopen(LOG_PATH, "w")) == NULL)
exit(EXIT_FAILURE);
fprintf(myLog, "MasyaAllah gagal pindahkan working directory.\n");
fclose(myLog);
exit(EXIT_FAILURE);
}
daemonTask(time);
exit(EXIT_SUCCESS);
}
and I want to do console locking mechanism in that bold part (strcmp ...)
I've tried to use printf and scanf and didn't work..
use exec to call shell script to read user input, just same.. won't work
is there any idea?
thx before.
develop_5889