Skip to content
Advertisement

Linux: let a process fail, if it does opens a file for writing [closed]

I would like a command-line-tool to fail if it opens a particular file for writing.

Is there a way I can modify the environment (maybe via cgroups) of the command-line tool, so that the command/process gets (for example) “permission denied”?

chmod a-w file does not work. The process seems to unlink() and then re-create the file.

I know that I can watch the syscalls of a process with strace. But is there a way to alter some calls, so that the process gets a different result?

Background: unittesting

Advertisement

Answer

strace has an option called -e inject or simply --inject which can be used to alter system calls of the tracee. (See manpage here)

In particular, in can be combined with the -P option to only trace syscalls accessing a specified path.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement