3

I am trying to change file permissions using chmod function in C program

chmod("/tmp/toBoard", S_IRWXU | S_IRWXG | S_IRWXO);

But after program run I check permissions and get only

-rwxr-xr-x 1 root root

I run this program on Linux embedded board. toBoard is a file copied inside the program from other file from /var directory, source file has all permissions(set manually from terminal). When I tried to copy it manually and set permissions it worked, but when I copy the file and try give it all permissions - it fails without errors

copy("/var/www/defaults.dat", "/tmp/toBoard");
int err;
if(err = chmod("/tmp/toBoard", S_IRWXU | S_IRWXG | S_IRWXO)){
    perror("chmod");
}
struct stat buffer;
int status = stat("/tmp/toBoard", &buffer);

How can I set all permissions to green light?

Community
  • 1
  • 1
PaulPonomarev
  • 355
  • 1
  • 4
  • 20
  • 2
    You have to run it as the owner, in this case root. But you shouldn't use root unless you really have to, and certainly not for experimenting like this. – Kevin Jan 15 '14 at 14:50
  • this works as given for me. Have to ``#include `` to get the S_IRWXU etc constants – Vorsprung Jan 15 '14 at 14:59
  • Running as root. I need this to be able to rewrite this file from webpage, but it has no permissions. Vorsprung, contains all constants needed, thoug I tried your advice and it didn't help either – PaulPonomarev Jan 15 '14 at 15:13

1 Answers1

8

As it stands, your call is correct but it is probably failing, and you're not checking the returned code. You could try:

if (chmod("/tmp/toBoard", S_IRWXU | S_IRWXG | S_IRWXO)) {
    perror("chmod");
    /* more error handling. */
}

In this instance it is likely a case of "Permission denied".

cnicutar
  • 178,505
  • 25
  • 365
  • 392