📄 dhcp-daemon.c
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-daemon.c,v 1.5 2003/05/25 02:18:46 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tmh@whitefang.com> * * 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. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * daemon functions * */#define MODULE_NAME "dhcp-daemon"#include "dhcp-local.h"#include "dhcp-limits.h"#include "dhcp-libutil.h"#include "dhcp-log.h"/* setup our interrupt handles. */void setup_interrupt_handlers(void){ block_interrupts(); /* treat term/quit/interrupt as shutdown */ add_interrupt_handler(SIGTERM, handle_shutdown); add_interrupt_handler(SIGQUIT, handle_shutdown); add_interrupt_handler(SIGINT, handle_shutdown); /* setup hup. */ add_interrupt_handler(SIGHUP, handle_hup);}/* Our go background routine. * here. We do all the necessary * steps to fork completely into * the background. * * Calling this will hose any * module we're using that * has descriptors open. * * Only call in a pre-initialized state. * *//* The name describes the function: we fork and only return as the child. Useful utility routine. */#ifndef HAVE_DAEMONstatic void do_fork_and_return_as_child(void){ switch (fork()) { case -1: /* Not good. */ /* Also we haven't opened logging yet, so do it now and exit. */ init_log(getprogname()); FATAL_ERROR("initialization: fork: %s", strerror(errno)); case 0: /* We're the child. Just break out of here. */ break; default: /* We're the parent. * Die. */ exit(0); } return;}#endif /* HAVE_DAEMON */void go_background(char *dir){ long i, max_descriptors;#ifdef RLIMIT_NOFILES struct rlimit usage_limit;#endif /* RLIMIT_NOFILES */ /* If we have daemon() use it. */#ifdef HAVE_DAEMON daemon(0, 0);#else /* Take process into the background. * * There are seven steps we need to * take. */ /* Step 1: fork out of current parent. */ do_fork_and_return_as_child(); /* Step 2: Grab a new session and relieve ourselves of session leadership. */ setsid(); /* Get new session. */ /* Now we need to fork out again, * this time to relieve ourselves * of the responsibility of being * a session leader. */ do_fork_and_return_as_child();#endif /* HAVE_DAEMON */ /* * Step 4: chdir() in case * we're running in a directory * that needs to be unmounted. * */ chdir(dir); /* We won't rely on daemon() to set * umask and close descriptors. * If you want something done right, * do it yourself. */ /* Setup umask() */ umask(0); /* Close all file descriptors. */ /* If we have sysconf and _SC_OPEN_MAX * use them */#ifdef HAVE_SYSCONF max_descriptors = sysconf(_SC_OPEN_MAX); if(max_descriptors <= 0) /* indeterminate or error. */ max_descriptors = SENSIBLE_DESCRIPTOR_MAX; /* Otherwise check for getrusage and use it. */#elif HAVE_GETRUSAGE if(getrusage(RLIMIT_NOFILES, &usage_limit) < 0) max_descriptors = SENSIBLE_DESCRIPTOR_MAX; max_descriptors = usage_limit.rlim_cur; /* If we have neither of the above see if MAX_FILES is defined. */#elif MAX_FILES max_descriptors = MAX_FILES; /* If we have none of the above just use our sensible default. */#else /* _SC_OPEN_MAX */ /* If we don't have any of these * use our sensible default. */ max_descriptors = SENSIBLE_DESCRIPTOR_MAX;#endif /* HAVE_SYSCONF */ for(i = 0; i < max_descriptors; i++) { close(i); } /* * In case library routines do something * brainless like spit out to stdout/stderr * let's open up all the first three * descriptors to /dev/null * We know that for each open call * we'll get the lowest descriptor. * Don't check for errors, * If it doesn't work just move on. * */ open("/dev/null", O_RDONLY); /* stdin */ open("/dev/null", O_WRONLY); /* stdout */ open("/dev/null", O_WRONLY); /* stderr */ /* initialize logging. */ init_log(getprogname()); /* Now we're done. */ return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -