Determining file access perms for current process


 
Thread Tools Search this Thread
Top Forums Programming Determining file access perms for current process
# 1  
Old 10-29-2010
Determining file access perms for current process

Stupid question, but is there an ANSI C stdlib function that will do this for me? I want to pass the function a path and determine if the current process can read/write/execute on the path. I suppose I can whip something up using fstat and then determining the current process's user/group IDs and checking against the file's mode...but I don't want to reinvent the wheel if something already exists.

Thanks!

-- DreamWarrior

P.S. I am sure this is a n00b question, and I'm in fact even surprised in all my years I've never actually done anything like this. I typically just try to open the file in read/write mode and if it fails assume I can't...but for this particular instance, that's not going to work nicely.
# 2  
Old 10-29-2010
man 2 access, but often I do this sort of thing in the wrapper ksh "if [ ! -w $file ] ; then . . . .".
man 2 stat tells you all the ls -ld stuff for a file and more, but then you have to decode permissions.
# 3  
Old 10-30-2010
FWIW: use access(), calling shell from C obviates the use of C in the first place, especially when there is already an systen call for the exact problem you have. If you think about it, the "shell shortcut" must be using the system call.

test -<one letter goes here> or the [ -<one letter goes here> [filename] ] construct in shell does EXACTLY what stat & access can do in C. There they must call... you guessed it.
# 4  
Old 10-30-2010
I find it is less code and especially less testing to do the once-daily or once-hourly housekeeping tasks in shell, especially when shell has many handy tools for that, and do just the need-for-speed bits in very simple C. I am not much into system calls, popen() occasionally so as not to have to rewrite thing and so as to get multiprocessing. I did find this was a nice system call, if you want a sort, like in COBOL (-:
Code:
system 'mknod /tmp/xxx p ; sort . . . -o /tmp/xxx /tmp/xxx &'
Open, Write data to, close /tmp/xxx
open, read result from, close /tmp/xxx
unlink() /tmp/xxx

So, was access() what you needed?
# 5  
Old 10-31-2010
All this is in the 'Art of UNIX Programming':
http://www.catb.org/esr/writings/tao...hing-guide.pdf
This was a small book for years, now it is a pdf online. Still available as a book, too.


A generally accepted guideline for language choice UNIX programming:

1. do it all in shell first.
2. if #1 won't work, don't use shell, resort to perl, python, ruby
3. if #2 won't work or speed is the primary requirement, don't use perl etc., use all C/C++

Going back up from C to shell kinda defeats having gone down to a compiled language to start with. What you are doing is calling another gigantic C program.

Note --

We have some extraordinary shell programmers here. See some of CFA Johnson's posts, or google his book. You can do a lot more than you realize just in shell. So going to #2 & #3 is not as common as you might think.

Also some shells like bash are easily extensible. Need a command - create your own builtin.
Using and Writing Bash Dynamically Loadable Built-In Commands by Chris F.A. Johnson
# 6  
Old 10-31-2010
Yes, thanks. I'm amazed I've never needed this call before, but it does exactly what I need and I'm glad I don't have to resort to doing myself with stat.

Thanks y'all...
# 7  
Old 11-01-2010
I write a lot of my C programs as relatively simple shell assists, so I can very easily test them and then use them with confidence. I like to write streaming tools (pipe fittings), where the input is usually not the command line, but lines on stdin. Before getting hung up on single app specific code that is hard to test, consider the simple shell assist C program.

For instance, mystat takes in paths from stdin as lines and spits them back out prefixed with one stat structure element and a tab, sort of like 'ls' but with no line length limit, headers, summaries or sorting. It's great for finding the latest in a list too big for ls -ltr. The code is so simple, you should be able to imagine it just from this. Originally, it just did mtime. I made up one letter mnemonic codes for every stat element, and default to mtime.

Another toy, xdemux, starts $1 instances of popen() $2 to write FILE*'s and sends incoming lines to them, in simple rotation. This does up to 32 parallel fgreps (great for 16 CPUs) looking for a string in a big file list:
Code:
find / -type f | xargs 32 'xargs -n999 fgrep -l aardvark'

I am sure some of my C toys could be done in *sh, but in C they can be both very heavy-duty in terms of speed and very robust, with no metacharacter worries. When possible, they have no char[] to be too short (or I go really big), using one character at a time I/O and state variables. If you do not write your commands for a big future, I guess you do not believe you have a big future? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Unable to access current user's mailbox.

In any non-root account, whenever I enter mail, it gives me: /var/spool/mail/root: Permission deniedI am not logged in as root, why is mail accessing root's mailbox ? I am unable to enter the currently logged in user's mailbox. Any help is appreciated :) (2 Replies)
Discussion started by: Hijanoqu
2 Replies

2. UNIX for Dummies Questions & Answers

Determining if a process is active in UNIX

We have written a bare bones scheduling app using bash scripts. The input to the scheduler is from a mainframe scheduling tool, and the scripts exit code is returned to the MF. The problem is that every now and again I have a script that does not complete and this is left in my Q. I am in the... (1 Reply)
Discussion started by: Charles Swart
1 Replies

3. UNIX for Dummies Questions & Answers

Running different process from current process?

I have been having some trouble trying to get some code working, so I was wondering...what system calls are required to execute a different program from an already running process? (1 Reply)
Discussion started by: Midwest Product
1 Replies

4. UNIX for Advanced & Expert Users

Determining interface to access IP

Hello I've got a server with multiple NICS. In a script I want to log the outbound interface. Is there an easy way I can do this so that the output looks something like this: host(xxx.xxx.xxx.xxx): Opening connection to ... Obviously, getting the host is simple with hostname. But how... (4 Replies)
Discussion started by: brsett
4 Replies

5. Solaris

file open/read/write/close/access by process

Hi want to know what file (descriptor+filename+socket) is being accessed by particular process on solaris. Purpose : while running perf. test, needs to find where is the bottleneck. We are providing concurrnet load for around 1 hr and needs to capture data related to file usage pattern... (1 Reply)
Discussion started by: raxitsheth
1 Replies

6. Solaris

shell-init: could not get current directory: getcwd: cannot access parent directories

Hello root@ne-ocadev-1:/root/scripts>su espos -c find /a35vol100/ESPOS/oracle/db/9.2.0/oradata/ESPOS/archive -type f -atime +10 -exec ls {} shell-init: could not get current directory: getcwd: cannot access parent directories: Permission denied find: insufficient number of... (6 Replies)
Discussion started by: babu.knb
6 Replies

7. UNIX for Dummies Questions & Answers

Current Process Running.

Hi all, When I issued command ps -ef|grep Vinay in a UNIX machine, I got the following Vinay 22491 1 255 Jun 18 ? 294248:53 -sh Vinay 26628 1 255 Jun 18 ? 294237:33 -sh Could you tell me what all process is running ? Please explain each of the fields. Thanks... (4 Replies)
Discussion started by: coolbhai
4 Replies

8. Shell Programming and Scripting

Displaying current user process

When I typed in ps -a I get this: PID TTY TIME CMD 31799 pts/3 00:00:00 vim 31866 pts/3 00:00:00 vim And to check who is currently logged in, I type who Felix Whoals Tada Whoals Lala Whoals How can I get the user process for all current users who logged in?? I think I need to combine... (14 Replies)
Discussion started by: felixwhoals
14 Replies

9. Shell Programming and Scripting

current running process in shell

hi what is the shell programming code to know the number of processes currently running on the machine & information about those processes. Another one is the configuration and usage of the UNIX file system? requesting all for help. thanks (1 Reply)
Discussion started by: moco
1 Replies

10. Programming

to find current running process

Hi All, The scenario is like this: There is a process say "A" which create a child process say "B" if some condition is true and process "A" terminates. "B" invokes some C program say "C" using 'execl' function. The job of program "C" is to keep polling the server until the server will be up.... (2 Replies)
Discussion started by: ranjkuma692
2 Replies
Login or Register to Ask a Question