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. * the Initial Developer. All Rights Reserved.
* *
* Contributor(s): * 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 * 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 * 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* listen_port = "4822"; /* Default port */
char* pidfile = NULL; char* pidfile = NULL;
int opt; int opt;
int foreground = 0;
/* General */ /* General */
int retval; int retval;
@ -207,13 +209,16 @@ int main(int argc, char* argv[]) {
pid_t daemon_pid; pid_t daemon_pid;
/* Parse arguments */ /* Parse arguments */
while ((opt = getopt(argc, argv, "l:b:p:")) != -1) { while ((opt = getopt(argc, argv, "l:b:p:f")) != -1) {
if (opt == 'l') { if (opt == 'l') {
listen_port = strdup(optarg); listen_port = strdup(optarg);
} }
else if (opt == 'b') { else if (opt == 'b') {
listen_address = strdup(optarg); listen_address = strdup(optarg);
} }
else if (opt == 'f') {
foreground = 1;
}
else if (opt == 'p') { else if (opt == 'p') {
pidfile = strdup(optarg); pidfile = strdup(optarg);
} }
@ -222,6 +227,7 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "USAGE: %s" fprintf(stderr, "USAGE: %s"
" [-l LISTENPORT]" " [-l LISTENPORT]"
" [-b LISTENADDRESS]" " [-b LISTENADDRESS]"
" [-f foreground]"
" [-p PIDFILE]\n", argv[0]); " [-p PIDFILE]\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -295,6 +301,7 @@ int main(int argc, char* argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!foreground) {
/* Fork into background */ /* Fork into background */
daemon_pid = fork(); daemon_pid = fork();
@ -326,6 +333,7 @@ int main(int argc, char* argv[]) {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
}
/* Open log */ /* Open log */
openlog(NULL, LOG_PID, LOG_DAEMON); openlog(NULL, LOG_PID, LOG_DAEMON);