LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 05-14-2009, 09:21 AM   #1
shiv.singh
LQ Newbie
 
Registered: May 2009
Posts: 16

Rep: Reputation: 0
Unhappy Need to write a file in delete protected mode


Hi all,
I have made a utility like tcpdump which sniff the RTP packets for specified ports on the network. I am buffering RTP packets and writing these packets into pcap file and compressing in gz format.

Issues:
While writing into the file (means the file is busy in writing), If user try to delete the same file, Linux is allowing deletion of file without any warning or error.

So please can you suggest me how can I lock this file so the user will not be able to delete the file while writing. And after closing the file if user delete the file then it wil be fine for my application.

I do not want to lock/protect the file from command line using chmod/chattr because I am writing files of size 1MB and once the file size reaches 1MB I will be opening the new file so there will be large number of files of size 1MB and it will be difficult to protect the file using command prompt and again unprotect each and every file.




Thanks & Regards,
Shiv
 
Old 05-14-2009, 10:26 AM   #2
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Lenny
Posts: 351

Rep: Reputation: 47
linux implements advisory lock and not mandatory locks like M$
But your problem is easier to handle.

Create the file and when the program opens the file (make sure the file is in opened condition
and that the program is writing to the file) just issue these commands as root

chattr +i <filename>

this will set the inhibit bit. This will not affect the already opened file descriptor but
other processes (users) will not be able to delete or modify or any other form of file operations
on the file.

Moreover, there is no alternative to proper file security using the UGO options. So, a file created by
one user cannot be easily deleted or changed by other users unless the owner gives permissions for the same.
 
Old 05-14-2009, 08:18 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
FYI, *nix does not actually delete the file if any process has it open. The entry may not be seen using ls, but the file is held open/exists until the last process accessing has closed the file.
http://man-wiki.net/index.php/2:unlink
 
Old 05-15-2009, 05:16 AM   #4
shiv.singh
LQ Newbie
 
Registered: May 2009
Posts: 16

Original Poster
Rep: Reputation: 0
Hi All,
Thanks for your valuable responses.

I also had an idea of using the chattr but I can not use the CLI commands here in my application. On the network it takes hardly 10sec to write 1MB data of RTP packets So I need to execute the chattr manually very freqently which is not feasible.

So do you know any function call or API which functions like chattr. As we have flock/fcntl for making file to protect the access of file from other process.


Thansk & Regards,
Shiv
 
Old 05-15-2009, 05:34 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,693
Blog Entries: 54

Rep: Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961
Quote:
Originally Posted by shiv.singh View Post
While writing into the file (means the file is busy in writing), If user try to delete the same file, Linux is allowing deletion of file without any warning or error.
If you don't mind me asking: is this a theoretical exercise or has it practical application? Who runs the application? Does it output to a directory shared with others? Provided directory and file DAC rights are set properly, in what scenario's would that user actually delete those files?
 
Old 05-15-2009, 07:56 AM   #6
shiv.singh
LQ Newbie
 
Registered: May 2009
Posts: 16

Original Poster
Rep: Reputation: 0
Hi,

1)is this a theoretical exercise or has it practical application?
SHIV>> This is a practical application which I have developed here.

2)Who runs the application?
SHIV>> User is running the applition and she wants to see the RTP packet flowing through the rtp ports for the purpose of debugging the issues.

3)Does it output to a directory shared with others?
SHIV>> Yes. This output to a shared directory.

4)Provided directory and file DAC rights are set properly, in what scenario's would that user actually delete those files?
SHIV>> I am opening the file in 'wb' mode.
Actually If the user deletes the active file by mistake. So for in this case application expects some write error from Linux platform but as the Linux does not delete file physically before the file get close so application keeps writing into the same stream which doesn't look valid scenario for my application. Because anyway user will not be able to retrieve the deleted file after closing the file. And application will be writing the data which user will not be able to get.

So I need that the application should get some indication if user deletes the open/active file or User should not be able to delete the open/active file, any of these approach looks fine for me.

I hope I would have clarified your questions to some extent and I will happy to answer your further query if any.

Thansk & Regards,
Shiv
 
Old 05-16-2009, 11:20 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,693
Blog Entries: 54

Rep: Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961
Quote:
Originally Posted by shiv.singh View Post
Actually If the user deletes the active file by mistake. (..) So I need that the application should get some indication if user deletes the open/active file
If you look around LQ (threads like "help I deleted my /tmp") you would see that there is enough dumb software (as opposed to those having routines) including major applications that simply don't check for deleted dirs or files (or override those with built-in defaults in absence of). I would not bother with the type of scenario you explained because 0) that's a wetware error and 1) you open another file within a minute anyway. This may sound harsh but so what the user loses 1 minute worth of data due to their own stupidity?
 
Old 05-17-2009, 01:59 AM   #8
shiv.singh
LQ Newbie
 
Registered: May 2009
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks for your valuable inputs and I have gone through the links you provided.
It helped me to change my mind about deletion of files in Linux.
 
  


Reply

Tags
file, mode, open, protected, write


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File write protected without chattr??? abefroman Linux - Server 1 12-06-2008 07:12 AM
How to implement memory mapping between protected mode and real mode? me_too Linux - Newbie 0 05-13-2008 02:12 AM
Error in switching from protected mode to real mode in Fedora East Linux - General 0 04-02-2008 03:03 AM
Cant remove write protected weird file tuxombie Slackware 13 12-07-2004 10:44 AM
remove write-protected file randomtask Linux - Newbie 5 04-22-2004 04:06 AM


All times are GMT -5. The time now is 04:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration