Add foreground option to daemon

Signed-off-by: Michael Jumper <zhangmaike@users.sourceforge.net>
This commit is contained in:
David PHAM-VAN 2012-05-23 11:56:49 +02:00 committed by Michael Jumper
parent fb93af4ce6
commit d39b2e8208

View File

@ -20,6 +20,7 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* David PHAM-VAN <d.pham-van@ulteo.com> Ulteo SAS - http://www.ulteo.com
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -199,6 +200,7 @@ int main(int argc, char* argv[]) {
char* listen_port = "4822"; /* Default port */
char* pidfile = NULL;
int opt;
int foreground = 0;
/* General */
int retval;
@ -207,13 +209,16 @@ int main(int argc, char* argv[]) {
pid_t daemon_pid;
/* Parse arguments */
while ((opt = getopt(argc, argv, "l:b:p:")) != -1) {
while ((opt = getopt(argc, argv, "l:b:p:f")) != -1) {
if (opt == 'l') {
listen_port = strdup(optarg);
}
else if (opt == 'b') {
listen_address = strdup(optarg);
}
else if (opt == 'f') {
foreground = 1;
}
else if (opt == 'p') {
pidfile = strdup(optarg);
}
@ -222,6 +227,7 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "USAGE: %s"
" [-l LISTENPORT]"
" [-b LISTENADDRESS]"
" [-f foreground]"
" [-p PIDFILE]\n", argv[0]);
exit(EXIT_FAILURE);
@ -295,6 +301,7 @@ int main(int argc, char* argv[]) {
exit(EXIT_FAILURE);
}
if (!foreground) {
/* Fork into background */
daemon_pid = fork();
@ -326,6 +333,7 @@ int main(int argc, char* argv[]) {
exit(EXIT_SUCCESS);
}
}
/* Open log */
openlog(NULL, LOG_PID, LOG_DAEMON);