haha....judulnya aneh ya, namun itu yang terjadi ada sebuah soal begini :
buatlah sebuah daemon yang dapat mengunci terminal setiap waktu tertentu. jika dalam waktu yang telah ditentukan user tidak menginput apapun di terminal maka, daemon akan menampilkan sebuah dialog untuk meminta password....
#include "sys/types.h"
#include "sys/stat.h"
#include "stdio.h"
#include "stdlib.h"
#include "fcntl.h"
#include "errno.h"
#include "unistd.h"
#include "syslog.h"
#include "string.h"
#include "signal.h"
#include "string.h"
#include "termios.h"
int main() {
pid_t pid, sid, gpid;
char pass[20], password[20]="huda";
/*passwordnya "huda" kalo mau diganti silakan...*/
pid = fork();
gpid=getpid();
if (0 > pid) {
exit(-1);
}
else if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
system("clear");
sid = setsid();
if ( 0 > sid ) {
exit(EXIT_FAILURE);
}
if (0 > (chdir("/")) ) {
exit(EXIT_FAILURE);
}
int y=0;
while (1) {
y++; //ini counter untuk mendelay waktu keluarnya password
if(kbhit())y=0;
if(y>20000){
printf("Masukkan password: ");
fflush(stdin);
scanf("%s", &pass);
printf("\nIsi pass= %s \n", pass);
if(strcmp(pass,password)==0){
fflush(stdout);
printf("Password Benar\n");
y=0;
}else{
printf("Password Salah!!\n");
printf("Daemon masih berjalan\n");
}
}
}
exit(EXIT_SUCCESS);
}
int kbhit(void)
{
/*ini dapat sari dodot gak tau dapat dari mana,
udah aku edit biar hanya mendeteksi keyboard sekali ketik....*/
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch=EOF; //tambahan dari saya, ini yang bikin selelu looping terus.
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ch=EOF; // ch kembali di buat clear
ungetc(ch, stdin);
return 1;
}
return 0;
}
Categories:
Bash


Langganan:
Posting Komentar (Atom)
wis.. siph.. keren hud..
ijin copas buat tak plajarin..
makasi..
oya klo ada waktu mampir ke www.mudpacks.co.cc
monggo