[c][unix]UNIXãããã¯ã¼ã¯ããã°ã©ãã³ã°ã®ãåå¼· (2) ãã¼ã¢ã³ã®ä½ãæ¹
ä¸æ©å¯ã¦ãããã¼ã¢ã³ããã»ã¹ã®ä½ãæ¹ãçºãã¦ã¨ã¦ãç°¡åã ã¨ãããã¨ã«æ°ãã¤ããï¼èªåã¡ã¢ï¼
ãã¼ã¢ã³ã¸ã®éã®ã
- fork()ãã¦åããã»ã¹ (次é) ãçæï¼è¦ªããã»ã¹ (太é) ãçµäºãã
- 次éããã»ã¹ã§setsid()ãã
- 次éããã»ã¹ã¯ã»ãã·ã§ã³ã°ã«ã¼ããªã¼ãã«ãªã
- ãã®æç¹ã§æ¬¡éããã»ã¹ã¯å¶å¾¡ç«¯æ«ãæããªããªã
- 次éããã»ã¹ãfork()ãã¦å«ããã»ã¹ (ä¸é) çæï¼æ¬¡éããã»ã¹çµäº
- chdir("/")ããï¼
- shutdownæã®umountå¦çã«è¿·æããããªããã
- æ¨æºå ¥åï¼æ¨æºåºåï¼æ¨æºã¨ã©ã¼åºåãéãã
ãµã³ãã«ã³ã¼ã
æ¯è¼ãè¡ãããã«3ã¤ã®ããã°ã©ã ãæ¸ããï¼
- daemon
- å ¨é¨è¡ã
- nondaemon
- 2度ç®ã®fork()ãããªã
- nondaemon2
- setsid()ããªã
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main (int argc, char *argv[]) { if (fork() != 0) { exit(0); } /* nondaemon2ã®å ´åï¼ãããããã¼ã¢ã³ãä½æ³ã«é£ã¶ */ if (setsid() < 0) { fprintf(stderr, "setsid()\n"); exit(1); } /* nondaemonã®å ´åï¼ãããããã¼ã¢ã³ãä½æ³ã«é£ã¶ */ if (fork() != 0) { exit(0); } // ãã¼ã¢ã³ãä½æ³ chdir("/"); close(0); close(1); close(2); while (1) { sleep(100); } return 0; }
ãããããã©ã®ãããªç¶æ ã§ããããpsã³ãã³ãã§èª¿ã¹ãï¼
% ps aux | grep daemon # USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND sleepy_yoshi 13836 0.0 0.0 1608 168 ? S 14:23 0:00 ./daemon sleepy_yoshi 13838 0.0 0.0 1608 176 ? Ss 14:23 0:00 ./nondaemon sleepy_yoshi 14013 0.0 0.0 1608 172 pts/10 S 14:23 0:00 ./nondaemon2
ãããè¦ãã¨ï¼daemon, nondaemonã«ã¤ãã¦ã¯å¶å¾¡ç«¯æ«ã?ã«ãªã£ã¦ããï¼ç¡äºã«å¶å¾¡ç«¯æ«ããåãé¢ãã¦ãããã¨ããããï¼ã¾ãï¼nondaemonã«ã¤ãã¦ã¯ï¼Sã®é£ã«å°ããªsãã¤ãã¦ããï¼ã»ãã·ã§ã³ãªã¼ãã¼ã§ãããã¨ããããï¼
nondaemon2ã¯ï¼å¶å¾¡ç«¯æ«ã®å¶å¾¡ä¸ã«ãã親ããã»ã¹ããfork()ãããç¶æ ãªã®ã§ï¼ã¾ã å¶å¾¡ç«¯æ«ãæã£ã¦ããç¶æ ã«ããï¼æç§æ¸ã«æ¸ããã¦ãããã¨ã ããã©ï¼setsid()ãããã¨ï¼åããã»ã¹ã親ããã»ã¹ã®å¶å¾¡ç«¯æ«ããé¢ãï¼å¶å¾¡ç«¯æ«ãæããªããªãï¼ã¨ããåä½ã確èªã§ããï¼
ãã¦ï¼ããã§ã²ã¨ã¤çåãæ®ãï¼
ã»ãã·ã§ã³ãªã¼ãã¼ã ã¨å°ããã¨ã¯ãªãã ããï¼å«ããã»ã¹ãä½ãå¿ è¦ãªãã®ã§ã¯ï¼
æç§æ¸ãã¦ã§ãã§èª¿ã¹ãã¨ï¼ãã»ãã·ã§ã³ãªã¼ãã¼ã§ãªããªãããï¼å¶å¾¡ç«¯æ«ã«é¢é£ä»ããããªããã¨ããè¨è¿°ãããï¼å¶å¾¡ç«¯æ«ãæããªãããã»ã¹ã«å¶å¾¡ç«¯æ«ãé¢é£ä»ããæä½ãããããªãã£ãã®ã§ï¼æ¨æ¸¬ã®åãåºãªãããã©ï¼
ã»ãã·ã§ã³ãªã¼ãã¼ã ã¨ï¼ã³ãã³ãã«ãã£ã¦å¶å¾¡ç«¯æ«ãé¢é£ä»ãããã¨ãå¯è½ã«ãªã
ã¨ããçç±ã ã¨åæã«è§£éãã¦ããï¼ããã©å¶å¾¡ç«¯æ«ã®é¢é£ä»ããèªåçã«è¡ããããã®ã§ãªããã°ï¼killã³ãã³ãã¨åæ§ã«èª°ããæ£æçã«è¡ãããªãéãèµ·ãããªããã¨ãªã®ã§ï¼ããã¾ã§ããå¿
è¦ããã®ããªãï¼ã¨æã£ãï¼
daemon(3)ã®ã½ã¼ã¹ã³ã¼ããè¦ã
ãã¦ãã¦ï¼glibcã«ã¯daemon(3)ã¨ããé¢æ°ãç¨æããã¦ããã®ã§åå¼·ã®æå¾ã«ãã®ä¸èº«ãè¦ããã¨ã«ããï¼glibcã®ã½ã¼ã¹ã³ã¼ããæå ã«ãªãã£ãã®ã§ï¼FreeBSDã®ã½ã¼ã¹ã³ã¼ããçºãã¦ã¿ãï¼(ã½ã¼ã¹ã³ã¼ã転è¼ã«ãªãã®ã§BSDã©ã¤ã»ã³ã¹é¨åãä»ãã¾ããï¼)
% cat /usr/src/lib/libc/gen/daemon.c /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)daemon.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include <sys/cdefs.h> __FBSDID("$FreeBSD: src/lib/libc/gen/daemon.c,v 1.6 2003/11/10 22:01:42 ghelmer Exp $"); #include "namespace.h" #include <errno.h> #include <fcntl.h> #include <paths.h> #include <signal.h> #include <unistd.h> #include "un-namespace.h" int daemon(nochdir, noclose) int nochdir, noclose; { struct sigaction osa, sa; int fd; pid_t newgrp; int oerrno; int osa_ok; /* A SIGHUP may be thrown when the parent exits below. */ sigemptyset(&sa.sa_mask); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; osa_ok = _sigaction(SIGHUP, &sa, &osa); switch (fork()) { case -1: return (-1); case 0: break; default: _exit(0); } newgrp = setsid(); oerrno = errno; if (osa_ok != -1) _sigaction(SIGHUP, &osa, NULL); if (newgrp == -1) { errno = oerrno; return (-1); } if (!nochdir) (void)chdir("/"); if (!noclose && (fd = _open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { (void)_dup2(fd, STDIN_FILENO); (void)_dup2(fd, STDOUT_FILENO); (void)_dup2(fd, STDERR_FILENO); if (fd > 2) (void)_close(fd); } return (0); }
ãããè¦ãã¨ãããã¨ããï¼å«ããã»ã¹ (ä¸é) ã¾ã§çæããã«ï¼å¼ã³åºãå ã®åããã»ã¹ (次é) ããã¼ã¢ã³ããã»ã¹ã¨ãã¦ããï¼ã©ãããå¶å¾¡ç«¯æ«ããåãé¢ããæç¹ã§å¤§ä½å¤§ä¸å¤«ãããï¼ã»ãã·ã§ã³ãªã¼ãã¼ã§ã¯ãããªãçç±ãããããªãã£ããã®ã®ï¼ããã§ãããã¨ãããã¨ãããã£ãï¼
ããã§æ¤ç´¢ã¨ã³ã¸ã³ã®ãã¼ã¢ã³åãã§ããï¼ã¾ãè²ã
ã¯ã¾ãããã ããã©ï¼ã¤ã³ãã¯ã¹ã®ãªã³ã¡ã¢ãªåã¨ä¸ç·ã«æ¸ãã¦ãã¾ããï¼
æ®ã£ãè¬
- ããããå«ããã»ã¹ããã¼ã¢ã³ã¨ããçç±ï¼ã»ãã·ã§ã³ãªã¼ãã¼ããã¼ã¢ã³ããã»ã¹ã¨ããªãçç±
åèæç®
- PC覚え書き | デーモン化の手順
- æ¨æºå ¥åï¼æ¨æºåºåï¼æ¨æºã¨ã©ã¼åºåã«éããï¼èããããå ¨ã¦ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãéããæä½ãæ¨å¥¨ãã¦ãã
- PCコマンドの使い方 - Linuxで自宅サーバ構築(新森からの雑記)