Provided by: manpages-ja-dev_0.5.0.0.20221215+dfsg-1_all bug

名前

       daemon - バックグラウンドで動作させる

書式

       #include <unistd.h>

       int daemon(int nochdir, int noclose);

   glibc 向けの機能検査マクロの要件 (feature_test_macros(7)  参照):

       daemon():
           glibc 2.21 以降:
               _DEFAULT_SOURCE
           glibc 2.19 と 2.20:
               _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
           glibc 2.19 以前:
               _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

説明

       daemon()  は、制御端末から分離させてシステムデーモンとして動作させたい プログラムのための関数である。

       nochdir  が 0 の場合、 daemon()  はプロセスの現在の作業ディレクトリ (current working directory)  をルート
       ディレクトリ ("/") に変更する。 それ以外の場合、現在の作業ディレクトリは変更されない。

       noclose が 0 の場合、 daemon()  は標準入力・標準出力・標準エラーを /dev/null にリダイレクトする。  それ以
       外の場合、これらのファイルディスクリプターは変更されない。

返り値

       (この関数が fork して fork(2)  が成功すると、親プロセスでは _exit(2) を呼び出すので、これ以降のエラーは子
       プロセスからしか見れない)。 成功した場合、 daemon()  は 0 を返す。 エラーが起こった場合、 daemon()  は -1
       を返す。 さらに、 errnofork(2)  と setsid(2) に関して規定されたエラーを設定する。

属性

       この節で使用されている用語の説明については、 attributes(7) を参照。
       ┌──────────────────┬───────────────┬─────────┐
       │ インターフェース属性      │
       ├──────────────────┼───────────────┼─────────┤
       │ daemon()         │ Thread safety │ MT-Safe │
       └──────────────────┴───────────────┴─────────┘

準拠

       POSIX.1 にはない。 同様の関数が BSD には見られる。 daemon()  関数は 4.4BSD で始めて登場した。

注意

       The  glibc implementation can also return -1 when /dev/null exists but is not a character device with the
       expected major and minor numbers.  In this case, errno need not be set.

バグ

       The GNU C library implementation of this function was taken from BSD, and does not employ the double-fork
       technique (i.e., fork(2), setsid(2), fork(2))  that is necessary to  ensure  that  the  resulting  daemon
       process  is  not  a  session leader.  Instead, the resulting daemon is a session leader.  On systems that
       follow System V semantics (e.g., Linux), this means that if the daemon  opens  a  terminal  that  is  not
       already  a  controlling  terminal  for  another session, then that terminal will inadvertently become the
       controlling terminal for the daemon.

関連項目

       fork(2), setsid(2), daemon(7), logrotate(8)

この文書について

       この man ページは Linux man-pages プロジェクトのリリース 5.10  の一部である。プロジェクトの説明とバグ報告
       に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

GNU                                                2017-11-26                                          DAEMON(3)