tags:

views:

87

answers:

2

I'm currently using dirent.h and ftw.h for directory traversal at my CGI website 100% programmed in C. I am not sure if they are process safe; would various users interfere with each other while on my site ?

Which functions would you recommend for this purpose ?

A: 

You probably mean "thread-safe" instead of process-safe. All libc calls are process-safe on Linux as processes (normally) live in separate memory spaces. On the other hand, readdir is not thread-safe, as it keeps an internal static storage for the context. Use readdir_r in that case (the _r means reentrant). The other functions in dirent.h are reentrant by default.

wump
He seems to actually mean *process* safe, given the context of worrying about different users running the same CGI script (which would usually mean different processes, unless FastCGI is involved).
Tyler McHenry
Yes, process safe. Just normal CGI, not FastCGI.
bobby
Well in that case, no reason to worry, as all libc functions are process-safe.Of course, users could still interfere with each other if one process is writing a file and another process is reading the same file, but that's not what you're doing.
wump
+1  A: 

It is safe for multiple processes to, for example, use ftw() to walk the same directory tree at the same time.

However it is not necessarily safe for one process to be walking the directory tree while another process is updating the same directory tree structure (ie adding, removing or renaming directories). If you have this situation, then you will need to make your CGI processes use an flock() advisory lock (you could just have a single empty lockfile in the root of the shared directory tree; processes that want to walk the tree have to take a shared lock on that lockfile, and processes that want to alter the tree have to take an exclusive lock on the lockfile).

caf