PaulPonomarev
PaulPonomarev

Reputation: 373

chmod can't give all permissions

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?

Upvotes: 3

Views: 1799

Answers (1)

cnicutar
cnicutar

Reputation: 182794

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".

Upvotes: 8

Related Questions