1819870 Members
2545 Online
109607 Solutions
New Discussion юеВ

C/C++ access() problem

 
SOLVED
Go to solution
Craig Smith_13
Frequent Advisor

C/C++ access() problem

When using the access() function in a program that has super-user privs, it will no longer detect presense of a file located under a different directory, with directory and file will only root permissions.

This used to work fine using the C compiler and HP OS 10.x. Never noticed till now that it does not on HP OS 11iv2 and the aCC/C v3.7 compiler...

Don't know if this is/was a bug and whether the OS or compiler is causing this...any ideas?

Thx.

#include
#include

int main(int argc, char *argv[])
{
int tmp1 = 0;

tmp1 = access("/root_owned_dir/testfile.txt", 0);

printf("result= %d\n", tmp1);

}


8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: C/C++ access() problem

Hi Craig:

The code works for me on an 11.23 Itanium box. It correctly returns a result of zero when a file exists and -1 when it doesn't.

By the way, you really should replace the mode parameter with 'F_OK' :-)

Regards!

...JRF...
Craig Smith_13
Frequent Advisor

Re: C/C++ access() problem

James,

Ok thanks for checking.

I assume you did the following:
mkdir /root_own_dir
touch /root_own_dir/testifile.txt
chmod -R 700 /root_own_dir
chown -R root /root_own_dir
chmod 6755 testit
Then run it as a regular user...
Don Morris_1
Honored Contributor
Solution

Re: C/C++ access() problem

I don't know how to address this (I've never played with ACLs yet, sorry) -- but the man page indicates this is to be expected:

-- start man page quote ---
The access() system call checks the file pointed to by path for accessibility according to the bit pattern contained in amode. access() uses the real user ID, not the effective user ID, and the real group ID, not the effective group ID.

--- end man page quote ---

Superuser privileges only change the effective user ID -- the real user ID is still the original user. Hence the behavior you see. Same man page on v1 and v3 and 11.0 -- which makes sense as this could break existing programs and could only have been changed across a major release number version.
James R. Ferguson
Acclaimed Contributor

Re: C/C++ access() problem

Hi (again):

> I assume you did the following...

Well no, not the first time; sorry that was silly :-)

OK, so correcting your typos of "/root_own_dir" and "/root_owned_dir" to match and adding a display of the actual 'errno' I do see failure when running as a regular user:

# ./access.o
result=-1 errno=13

# ls -l ./access.o
ls -l access.o
-rwsr-sr-x 1 root sys 70288 Sep 17 15:46 access.o

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: C/C++ access() problem

Hi (again):

Don just showed me how to read better :-)

I looked at that manpage and completely missed that even having seen errno=13 as the darn result!

Regards!

...JRF...
Don Morris_1
Honored Contributor

Re: C/C++ access() problem

Further reading -- I may be wrong, but it looks to me that the getaccess(2) function provides what you want, just in a bit more arcane fashion.

If you check the example at the bottom of the man page -- they're checking whether the file "test" is accessible based on the Effective UID instead of the Real UID, which is what you want.
Craig Smith_13
Frequent Advisor

Re: C/C++ access() problem

Don,

Thanks, should have read the man page first. Replacing it with getaccess() does the trick.


James,

Thanks for testing it out for me.

Regards,
Craig.

Code:

#include
#include
#include
#include


int main(int argc, char *argv[])
{
int tmp1 = 0, err = 0;

tmp1 = getaccess("/root_owned_dir/testfile.txt", UID_EUID, NGROUPS_EGID_SUPP,
(int *)0, (void *)0, (void *)0);

if (tmp1 < 0 )
err = errno;

printf("result= %d, err = %d\n", tmp1, err);

}
Craig Smith_13
Frequent Advisor

Re: C/C++ access() problem


Replaced access() with getaccess()