How to Lock In HP-UX


 
Thread Tools Search this Thread
Operating Systems HP-UX How to Lock In HP-UX
# 1  
Old 06-30-2006
How to Lock In HP-UX

Hi ,

How should i implemet a lock function Hp-ux .

I want to a lock a file through fcntl in Hp-ux .

But is not locking properly .



Thanks
Narendra
# 2  
Old 06-30-2006
hpux does not have mandatory locking - that means every process that opens the file has to play by the rules. So, if you lock your file, then run another program that does not play by those rules, the lock will not work. It's like obeying a traffic light. Some people break the rules and do not stop on red. There is no physical barrier to stop them - a physical barrier at a traffic light is like manadtory locking. They cannot run the red light/They can't play with the file no matter what

Please show your code.
# 3  
Old 07-06-2006
can I use IPC's to lock file in HP-UX?

As HP-UX does not provide mandactory locking, so is there way to lock a file?
If yes can you please provide me more details in to the same?

can I use IPC to lock file ?

Thanks,
Manju
# 4  
Old 07-06-2006
When you drive a car and come to a traffic light - and the light is green - you go thru the intersection. If the light is red, you stop. The traffic light works because everyone agrees to stop on red, well most of the time.

A system without mandatory locking works just like the traffic light. Every program accessing the file has to agree to check the lock and abide by it.

A not-very-good option:
1. mv the file to a directory you own while your program accesses it.
2. mv the file back to where it belongs when you are done.

What problem are you trying to solve?
# 5  
Old 07-06-2006
I just tested manditory locking in HP-UX and it works as well as it works for any other version of unix.
Code:
$ chmod 666 datafile
$ cp datafile1 datafile
$ ./locktest &
[1]     1302
$ cp datafile2 datafile
$ datafile2 line 1
datafile2 line 2
$
[1] +  Done                    ./locktest &
$
$
$ cp datafile1 datafile
$ chmod 2666 datafile
$ ./locktest &
[1]     1330
$ cp datafile2 datafile
cp: cannot create datafile: Resource temporarily unavailable
$ datafile1 line 1
datafile1 line 2
$
[1] +  Done                    ./locktest &
$
$ uname -srv
HP-UX B.11.00 A
$ cat locktest.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

main(void)
{
        char buff[250];
        FILE *F;
        struct flock mylock;
        int fd, iret;
        fd = open("datafile", O_RDWR);

        mylock.l_type = F_WRLCK;
        mylock.l_whence = SEEK_SET;
        mylock.l_start = 0;
        mylock.l_len = 0;
        iret = fcntl(fd,F_SETLK, &mylock);
        if (iret == -1) {
                 if ((errno == EACCES) || (errno == EAGAIN)) {
                        printf("locked \n");
                        exit(1);
                } else {
                        printf("fcntl failed\n");
                        exit(1);
                }
        }
        sleep(30);
        F = fdopen(fd, "r");
        while (fgets(buff,250,F)) {
                fputs(buff,stdout);
        }
        exit(0);
}

# 6  
Old 07-06-2006
I guess I wasn't clear. HPUX (11.0) follows the POSIX.1 standard for fcntl.

http://www.opengroup.org/onlinepubs/...ons/fcntl.html
Please note the last paragraph that states why mandatory locking is not part of
the standard.

mandatory locking: locks enforced by the kernel on the open, read, write etc. system calls. Not based on fcntl. As long as the process calls fcntl first to get a lock, it's obeying the traffic cop rules. Mandaory locking does not allow circumventing the rules.

This is my take on it anyway, YMMV
# 7  
Old 07-07-2006
I checked the fcntl locking with two programs, as Jim mentioned in his reply the mandatory locking didn't worked for me in HP-UX.
I have cut pasted both programs for reference.

The scenario I followed to test the locking.

1. first I ran ./locktest executable in background

2. I ran ./locktest1 executable, This program will check the lock and after seeing it locked it tries to write into that file. According to my understanding as the file is locked by "locktest" process
It should not allow "locktest1" process to write into it, but in this case the "locktest1" process is able to write into that file.

Please let me know if I'm missing anything.

Jim:
>> What problem are you trying to solve?
I'm trying to lock file using fcntl or lockf function on HP-UX, In my case I have 2 programs which are trying to update single file. In order to synchronize this I thought locking is better option.

Now also I can do it, I can modify my program to work based on the return value of fcntl() function.

I was just curious and wanted to know why the mandatory locking is not working in HP-UX. Is there any other way so that I can lock (mandatory) file in HP-UX? I,e using IPC's.

Thanks Jim and Perderbo for your answers.

$ cat locktest.c

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

main(void)
{
char buff[250];
FILE *F;
struct flock mylock;
int fd, iret,i;
fd = open("datafile", O_APPEND | O_RDWR);

mylock.l_type = F_WRLCK;
mylock.l_whence = SEEK_SET;
mylock.l_start = 0;
mylock.l_len = 0;
iret = fcntl(fd,F_SETLK, &mylock);
if (iret == -1) {
if ((errno == EACCES) || (errno == EAGAIN)) {
printf("locked \n");
exit(1);
} else {
printf("fcntl failed\n");
exit(1);
}
}
for(i=0;i<20;i++)
{
sleep(2);
write(fd,"A",1);
}
F = fdopen(fd, "r");
while (fgets(buff,250,F)) {
fputs(buff,stdout);
}
close(fd);
exit(0);
}

$cat locktest1.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

main(void)
{
char buff[250];
FILE *F;
struct flock mylock;
int fd, iret,i;
fd = open("datafile", O_APPEND | O_RDWR);

mylock.l_type = F_WRLCK;
mylock.l_whence = SEEK_SET;
mylock.l_start = 0;
mylock.l_len = 0;
iret = fcntl(fd,F_SETLK, &mylock);
if (iret == -1) {
if ((errno == EACCES) || (errno == EAGAIN)) {
printf("locked \n");
for(i=0;i<20;i++)
{
write(fd,"B",1);
}
exit(1);
} else {
printf("fcntl failed\n");
exit(1);
}
}
F = fdopen(fd, "r");
while (fgets(buff,250,F)) {
fputs(buff,stdout);
}
close(fd);
exit(0);
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in lock and unlock and after the changes

Requirement:First i need to unlock the directory which i had a script for it.If i select app1 it should unlock the directory and after chnages in the script once need to lock the directory with lock command The below highlighed variables in lock and unlock has to be changed according... (2 Replies)
Discussion started by: bhas85
2 Replies

2. Ubuntu

Help me to lock my system.

Hi All! I am not able to lock my Ubuntu 12.04 LXDE. Can anybody tell me the shortcut to lock the system. I have tried all conventional keyboard shortcuts as well as buttons. (2 Replies)
Discussion started by: nixhead
2 Replies

3. Shell Programming and Scripting

lock on files

Hi, I want to write a code where in the file once taken genrates a lock number and other developer/user cannot take the file to make any changes in it. He gets the file only as read only. (2 Replies)
Discussion started by: rac
2 Replies

4. 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

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. Solaris

Not able to lock the port

Hi, I am working with Sun-Solaris 9 and in our application I need to connect the UPS with Serial port,after that I need to monitor it..but before stating communication,It should lock that port,To check it,first i am trying to make sure the lock directory exists and I am able to find it(/var/lock)... (0 Replies)
Discussion started by: smartgupta
0 Replies

7. Shell Programming and Scripting

lock an account

hi how can I Lock an account, by prepending ”*LK*” to the password field in /etc/shadow. I dont want to use passwd -l . Any idea? (3 Replies)
Discussion started by: tjay83
3 Replies

8. Shell Programming and Scripting

Lock for this script

Hi, My requirement is to service a process and below is the script which i wrote for that and works fine, I have kept it in a crontab and running this everyminute, how do I lock this if its already running and i dont want to open if its running and not completed yet. The crontab need to run... (4 Replies)
Discussion started by: strunz
4 Replies

9. UNIX for Dummies Questions & Answers

Lock File

Hi, We have a lock file being created called lck8c0001 created in Unixware 2.1.2. This is locking a printer. According to some websites, 8c0001 relates to the device name. How does one link 8c0001 to those devices listed in the /dev folder? I have done a ps -lp for all printers and have... (4 Replies)
Discussion started by: canman
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