Page 1 of 1

Any "fopen for write" with owner-params?

Posted: Wed Dec 07, 2016 11:28 am
by Haddock99
I have a RPI with Raspbian and I develop a project in Ansi-C. I use fopen() to write and read files. Are there any function where I can set the ownership and/or permissions in the same time I write the file? Like a fopen with more params....

Re: Any "fopen for write" with owner-params?

Posted: Wed Dec 07, 2016 11:32 am
by RaTTuS
chmod($fname, 0777);
chown($fname, 'user');
I take it you are running as root and want to create it as another user ?

Re: Any "fopen for write" with owner-params?

Posted: Wed Dec 07, 2016 11:57 am
by Haddock99
The program running as root (and creates files with root:root) and I'm a USER. The PHP/Apache2 have also problems to deal with those files.
I think I use the chmod/chown() already. As I remember, the files needed to be opened before calling chmod/chown?
Of course, I can do this when the files are opened for writing..

Thanks!

Re: Any "fopen for write" with owner-params?

Posted: Thu Dec 08, 2016 7:35 pm
by mutley
open (not fopen) will allow you to set permissions of the file.
setuid will allow you to set the uid of the process.

So, as an example (from memory not tested)

setuid(501) // assuming www-data is 501
fd = open(filename, O_RDRW | O_CREAT, 0644)

Re: Any "fopen for write" with owner-params?

Posted: Wed Dec 14, 2016 7:38 am
by Haddock99
Great. That's a very nice way of setting the ownership and the p-bits as I want them.