I have a program (Crafty chess) that works just fine when started from the console. For my application I have daemonized it. When daemonizing a process, one is supposed to change the working directory to “/” via chdir("/").
When I follow that advice, the program exits in some use cases because it doesn’t have the proper permissions in “/”. When I don’t chdir, the program works, but just leaving out chdir is a crutch.
Is there a sound alternative to omitting chdir("/")?
It probably should not write to the current directory. It’d be better to write to some specific directory instead. Instead of cd’ing to
/tmpand writing files to the current directory, write files to/tmp/whatever— i.e. always use absolute paths.And on a related note, don’t hardcode
/tmpif you can avoid it. Make it a configuration option, or use the$TMPDIRenvironment variable, or best of all, usemktemp().