1

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("/")?

chessweb
  • 4,613
  • 5
  • 27
  • 32
  • Can you quote a source for advice of executing chdir("/") I doubt that this is a good idea (for a multitude of reasons) and would be interested to find out where this was advised and why? – Rob Kielty Oct 20 '12 at 21:07
  • 1
    What do you mean by "it doesn't have the proper permissions"? What happens? What doesn't work? – John Kugelman Oct 20 '12 at 21:08
  • @RobKielty See http://www.itp.uzh.ch/~dpotter/howto/daemonize. "This prevents the current directory from being locked; hence not being able to remove it." – John Kugelman Oct 20 '12 at 21:10
  • If it's exiting for not having permission, why is it writing to the disk anyway? chdir("/") shouldn't be a problem if you're being specific about where to write your files. Also, just because it's recomended to do chdir("/") doesn't mean it's suitable in all instances. – goji Oct 20 '12 at 21:10
  • @Troy: It needs write permission in the working directory. Would it be OK to chdir to some other directory where it has those permissions (e.g. /tmp)? – chessweb Oct 20 '12 at 21:19
  • @JohnKugelman I have done some more digging around and have found other references to this practise I have to say I would be reluctant to chdir to / and would seek to avoid doing this. I think that root file permissions are protecting the root file system in this instance. – Rob Kielty Oct 20 '12 at 21:20
  • If you look at `sudo lsof / | grep cwd`, you see how many processes have `/` as their cwd, so it cannot be such a bad idea. – glglgl Oct 20 '12 at 21:25
  • The idea is that root is always there. As @JohnKugelman points out it is done to prevent locking a directory but there is a caveat, again in JKs answer, the program should only write to files with non relative file paths and preferably not the root filesystem. User level applications, even daemons, as a rule should not write to the root file system for fear of filling it up. – Rob Kielty Oct 20 '12 at 21:32
  • If a program wasn't specifically written with support to run as a daemon, don't expect it to work without modifying it. Daemons have to take care of some details. See: http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html – Nikos C. Oct 20 '12 at 21:33

2 Answers2

3

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 /tmp and 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 /tmp if you can avoid it. Make it a configuration option, or use the $TMPDIR environment variable, or best of all, use mktemp().

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

You can call the daemon(3) function (with a non-zero first nochdir argument) so that it daemonize a process without doing the chdir("/"))

But as John Kugelman suggests, you should not write (nor read) any relative path in a daemonized program (or you should do an explicit chdir to a directory that you can read and write and search).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547