How to inform other processes that a file has marked for deletion?


How to inform other processes that a file has marked for deletion?



I am currently writing a C application for an embedded system (limited disc space)
On this system, several processes access files which I have to delete with my application on certain events (e.g. running out of disc space). But since the other processes still can write to these files, the disc space situation doesn't improve.



Is there any possibility to actually delete the file and let the write access of the other processes fail?



I have only limited access to the behavior of the other processes so it would be nice if no cooperation of these processes is needed.





How hacky may the solution be? I can imagine something with ptracing those processes and closing the fds of those files...
– PlasmaHH
Feb 1 '13 at 12:58





@PlasmaHH lets say it this way: I would prefer a nice short and simple solution, but the files have to go and if these nifty little processes don't want to give up control over the files, I have to force them.
– Andreas Wilkes
Feb 1 '13 at 13:06





@Joze Protecting the file won't work, because that would need a large amount of cooperation of the other processes on linux systems and I can't know if i get this.
– Andreas Wilkes
Feb 1 '13 at 13:10





Can you restart those processes?
– cha0site
Feb 1 '13 at 13:23





Other Unix-like systems have a system call for this called revoke. Unfortunately it doesn't seem that Linux can do this (the stub in glibc only returns ENOSYS).
– Art
Feb 1 '13 at 13:24


revoke


ENOSYS




2 Answers
2



Two ideas come to mind to go around the fact that the file doesn't actually get deleted until all references to it are closed:





Truncation is the solution. +1
– R..
Feb 1 '13 at 14:10





Thanks for your answer the truncate looked great on the first view, processes can still write without problems to their open files, but since the filepointer doesn't get changed, the other processes seem to continue writing where they stopped. Which results in a file not smaller than before but with leading zeros.
– Andreas Wilkes
Feb 1 '13 at 16:09



If I was the owner of the "other processes", I would just let them close, rename and reopen the log files, as soon as their size reach a given threshold. In this way the log rotation can be performed in a "standard" way (eg: logrotate)



However it seems that you already have some legacy applications.



In this case I would suggest to use mkfifo to create named pipes in place of the log files, before the legacy applications are started.



Your C application will read from those special files, and in this way it will be able for example to create log files no longer than a specified amount. In this way you will be able to use logrotate, for example.



Drawback: if your C application is not running, the legacy applications will stay stuck in the write() system call, trying to write to a non-connected pipe.


write()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages