How to lock own console when idle?


 
Thread Tools Search this Thread
Top Forums Programming How to lock own console when idle?
# 1  
Old 11-05-2008
How to lock own console when idle?

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

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

Last edited by develop_5889; 11-05-2008 at 12:08 PM..
# 2  
Old 11-05-2008
Use the command lock
# 3  
Old 11-06-2008
Quote:
Originally Posted by jim mcnamara
Use the command lock

Using Lock? how come?

can't find that function on unix standar lib.

Last edited by develop_5889; 11-06-2008 at 03:19 AM.. Reason: Wrong on writing words
# 4  
Old 11-06-2008
The command lock can do the job, but it can be killed!
# 5  
Old 11-06-2008
Could someone give example on using the lock command to lock the cosole? Smilie haven't found out how..
# 6  
Old 11-06-2008
To lock >> press ctrl+s
To unlock>>press ctrl+q

No password required
# 7  
Old 11-06-2008
Sorry, i need to use password to let user to open the console..

is there any possibility other than using vlock??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Idle sessions.

What kind of problems can be faced if any session which connects to unix server is open for longer time? How to find out from how much time that session is idle? :) (1 Reply)
Discussion started by: anandgodse
1 Replies

2. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

3. UNIX for Dummies Questions & Answers

IP Console Vs Serial console.

Hi Gurus, Can some one explain me the difference between a IP console and a serial console. (1 Reply)
Discussion started by: rama krishna
1 Replies

4. UNIX for Dummies Questions & Answers

lock the console after 5 minutes of inactivity.

How would i force The server console to be locked automatically after 5 minutes ? I want it to happen for X as well as terminal session of the machine. My Linux distribution details. LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch Distributor ID:... (11 Replies)
Discussion started by: pinga123
11 Replies

5. Red Hat

Security Question: Lock after invalid login, Session Lock and Required Minimum Password Length

Hello all, If anyone has time, I have a few questions: How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too. 1. How to lock the account after a few (like 3) invalid password attempts? 2. How do you lock a screen after 30... (1 Reply)
Discussion started by: nstarz
1 Replies

6. UNIX for Dummies Questions & Answers

Windows to Linux remote console using VNC brings up blank console screen with only mouse pointer

:confused:Hi This was installed on the Linux box a few weeks back by a guy that no longer works for us. All worked fine until last week. Now when we connect its just a blank screen with no icons. I get a whole bunch of errors when starting the service too: Tue Feb 23 14:29:45 2010 ... (1 Reply)
Discussion started by: wbdevilliers
1 Replies

7. UNIX for Advanced & Expert Users

idle time again

I need to find the idle time on a machine in the manner: How long time ago somebody did the last action with mouse or keyboard? Unfortunately "w" doesn't do this. It produced the following output on a machine a user was actually working on with an application: 15# w 15:55:28 up 15 days, ... (1 Reply)
Discussion started by: elbrand
1 Replies

8. UNIX for Advanced & Expert Users

console /dev/console get image

We are using software (Pegasys) which runs on SunOS 5.8 and reads images from a Philips nuclear camera. The software is designed to run from the console. I need to be able to capture the images it produces on the display. The caveat is that I cannot use the X Windows display because the X Server... (3 Replies)
Discussion started by: sreyes27
3 Replies

9. Solaris

CPU idle

hi when should we consider that CPU is loaded? When it is 100% idle or 0%idle?? tx (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

10. UNIX for Dummies Questions & Answers

how to lock keyboard without using lock command

how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device? thanks (7 Replies)
Discussion started by: dianayun
7 Replies
Login or Register to Ask a Question