daemon(0, 0) 이거 혹시 아시는 분..

doyaa의 이미지

제가 소스를 보다가 이런 문장을 보았습니다.

if (foreground == 0) {
if (daemon(0, 0) < 0)
err(1, "daemon");
}

대충 내용을 살펴보니..포그라운드로 돌리는 함수 같은데요..

정확한 내용을 아시는 분이 계시면.. 답변 부탁드립니다.

불량청년의 이미지

Quote:
man daemon

자세하게 나와있습니다. *^^*

H/W가 컴퓨터의 심장이라면 S/W는 컴퓨터의 영혼이다!

hyper9의 이미지

SYNOPSIS #include

int daemon(int nochdir, int noclose);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

daemon(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

DESCRIPTION
The daemon() function is for programs wishing to detach themselves from
the controlling terminal and run in the background as system daemons.

Unless the argument nochdir is non-zero, daemon() changes the current
working directory to the root ("/").

Unless the argument noclose is non-zero, daemon() will redirect stan-
dard input, standard output and standard error to /dev/null.

이라고 나와있는데요,, 첫번째 parameter인 nochdir이 0이 아니면, daemon()은 working directory를 /로 바꾼다고 되어 있는 것 같은데요. 만일 nochdir이 0이면 어떻게 되는 걸까요?

그리고 두 번째 parameter인 noclose도 0이 아니면, daemon()이 standard input, standard output, standard error를
/dev/null로 redirect하는 것 같은데요..

그럼 noclose가 1이거나 2이거나 같은 결과를 만드는 것일까요?

잘 이해가 안되어서 질문드렸습니다.
조언 주시면 감사하겠습니다.

webispy의 이미지

아래 링크로 daemon() 의 실제 코드 확인 가능합니다.

http://findsrc.net/glibc/HTML/S/2417.html#L44

빠른 답변을 원하신다면..

daemon() 소스코드에서
if (!nochdir)
(void)__chdir("/");

이렇게 구현되어 있습니다.