[examples] Added daemonize option to services in examples;

master
leonid-ed 9 years ago
parent 3c0d3570f8
commit 4d802aaf0d

@ -325,9 +325,13 @@ int main(int argc, char **argv)
sxhub_t *sxmphub = sxhub_create(); sxhub_t *sxmphub = sxhub_create();
int port = DEFAULT_PORT; int port = DEFAULT_PORT;
int opt; int opt;
int is_daemon = 0;
while((opt = getopt(argc, argv, "p:r:u:")) != -1) { while((opt = getopt(argc, argv, "dp:r:u:")) != -1) {
switch(opt) { switch(opt) {
case 'd':
is_daemon = 1;
break;
case 'p': case 'p':
port = atoi(optarg); port = atoi(optarg);
break; break;
@ -339,7 +343,7 @@ int main(int argc, char **argv)
break; break;
default: default:
fprintf(stderr, "usage: %s [-p <PORTNUM>] -r <PATH to Root CA> -u <PATH" fprintf(stderr, "usage: %s [-p <PORTNUM>] -r <PATH to Root CA> -u <PATH"
" to SSL certificate>\n", argv[0]); " to SSL certificate> [-d]\n", argv[0]);
return EINVAL; return EINVAL;
} }
} }
@ -354,6 +358,12 @@ int main(int argc, char **argv)
return EINVAL; return EINVAL;
} }
/* become daemon if it's necessary */
if (is_daemon && (opt = daemonize())) {
fprintf(stderr, "daemonize() failed (%d).\n Failure.\n", opt);
return EINVAL;
}
sxmp_init(); sxmp_init();
/* all is fine let's init connection subsystem */ /* all is fine let's init connection subsystem */

@ -233,7 +233,8 @@ static void __help_print(FILE *fso, const char *fmtname)
/* usage options */ /* usage options */
fprintf(fso, "Usage:\n"); fprintf(fso, "Usage:\n");
fprintf(fso, "\t%s -r | --root-x509 <path> -u | --master-x509 <path> " fprintf(fso, "\t%s -r | --root-x509 <path> -u | --master-x509 <path> "
" [-p | --listen-port <port>] [-R | --root-dir <directory>]\n", fmtname); " [-p | --listen-port <port>] [-R | --root-dir <directory>] "
" [-d | --daemonize]\n", fmtname);
/* defaults */ /* defaults */
fprintf(fso, "\t%s -h | --help\n", fmtname); fprintf(fso, "\t%s -h | --help\n", fmtname);
@ -249,6 +250,7 @@ static void __help_print(FILE *fso, const char *fmtname)
"-p | --listen-port <port>", DEFAULT_PORT); "-p | --listen-port <port>", DEFAULT_PORT);
fprintf(fso, "\t%-33s Remote directory becoming root for user [default: %s].\n", fprintf(fso, "\t%-33s Remote directory becoming root for user [default: %s].\n",
"-R | --root-dir <directory>", DEFAULT_ROOT_DIR); "-R | --root-dir <directory>", DEFAULT_ROOT_DIR);
fprintf(fso, "\t%-33s Work as a daemon.\n", "-d | --daemonize");
fprintf(fso, "\t%-33s Show help screen.\n", "-h | --help"); fprintf(fso, "\t%-33s Show help screen.\n", "-h | --help");
fprintf(fso, "\t%-33s Print version information.\n", "-v | --version"); fprintf(fso, "\t%-33s Print version information.\n", "-v | --version");
return; return;
@ -511,6 +513,7 @@ int main(int argc, char **argv)
sxhub_t *sxmphub = sxhub_create(); sxhub_t *sxmphub = sxhub_create();
int port = DEFAULT_PORT; int port = DEFAULT_PORT;
int opt; int opt;
int is_daemon = 0;
while(1) { while(1) {
int option_index = 0; int option_index = 0;
@ -523,10 +526,11 @@ int main(int argc, char **argv)
{"master-x509", required_argument, NULL, 'm'}, {"master-x509", required_argument, NULL, 'm'},
{"root-dir", required_argument, NULL, 'R'}, {"root-dir", required_argument, NULL, 'R'},
{"listen-port", required_argument, NULL, 'p'}, {"listen-port", required_argument, NULL, 'p'},
{"daemonize", required_argument, NULL, 'd'},
{ NULL, 0, NULL, 0 }, { NULL, 0, NULL, 0 },
}; };
if((opt = getopt_long(argc, argv, "hvr:m:R:p:", long_options, if((opt = getopt_long(argc, argv, "dhvr:m:R:p:", long_options,
&option_index)) == -1) break; &option_index)) == -1) break;
switch(opt) { switch(opt) {
@ -550,6 +554,9 @@ int main(int argc, char **argv)
case 'R': case 'R':
root_dir = strdup(optarg); root_dir = strdup(optarg);
break; break;
case 'd':
is_daemon = 1;
break;
} }
} }
@ -565,6 +572,12 @@ int main(int argc, char **argv)
return EINVAL; return EINVAL;
} }
/* become daemon if it's necessary */
if (is_daemon && (opt = daemonize())) {
fprintf(stderr, "daemonize() failed (%d).\n Failure.\n", opt);
return EINVAL;
}
sxmp_init(); sxmp_init();
/* all is fine let's init connection subsystem */ /* all is fine let's init connection subsystem */

@ -235,7 +235,7 @@ static void __help_print(FILE *fso, const char *fmtname)
/* usage options */ /* usage options */
fprintf(fso, "Usage:\n"); fprintf(fso, "Usage:\n");
fprintf(fso, "\t%s -r | --root-x509 <path> -u | --master-x509 <path> " fprintf(fso, "\t%s -r | --root-x509 <path> -u | --master-x509 <path> "
" [-p | --listen-port <port>]\n", fmtname); " [-p | --listen-port <port>] [-d | --daemonize]\n", fmtname);
/* defaults */ /* defaults */
fprintf(fso, "\t%s -h | --help\n", fmtname); fprintf(fso, "\t%s -h | --help\n", fmtname);
@ -249,6 +249,7 @@ static void __help_print(FILE *fso, const char *fmtname)
"-m | --master-x509 <path>"); "-m | --master-x509 <path>");
fprintf(fso, "\t%-33s Remote host listening port number [default: %d].\n", fprintf(fso, "\t%-33s Remote host listening port number [default: %d].\n",
"-p | --listen-port <port>", DEFAULT_PORT); "-p | --listen-port <port>", DEFAULT_PORT);
fprintf(fso, "\t%-33s Work as a daemon.\n", "-d | --daemonize");
fprintf(fso, "\t%-33s Show help screen.\n", "-h | --help"); fprintf(fso, "\t%-33s Show help screen.\n", "-h | --help");
fprintf(fso, "\t%-33s Print version information.\n", "-v | --version"); fprintf(fso, "\t%-33s Print version information.\n", "-v | --version");
return; return;
@ -260,6 +261,7 @@ int main(int argc, char **argv)
sxhub_t *sxmphub = sxhub_create(); sxhub_t *sxmphub = sxhub_create();
int port = DEFAULT_PORT; int port = DEFAULT_PORT;
int opt; int opt;
int is_daemon = 0;
while(1) { while(1) {
int option_index = 0; int option_index = 0;
@ -271,10 +273,11 @@ int main(int argc, char **argv)
{"root-x509", required_argument, NULL, 'r'}, {"root-x509", required_argument, NULL, 'r'},
{"master-x509", required_argument, NULL, 'm'}, {"master-x509", required_argument, NULL, 'm'},
{"listen-port", required_argument, NULL, 'p'}, {"listen-port", required_argument, NULL, 'p'},
{"daemonize", required_argument, NULL, 'd'},
{ NULL, 0, NULL, 0 }, { NULL, 0, NULL, 0 },
}; };
if((opt = getopt_long(argc, argv, "hvr:m:p:", long_options, if((opt = getopt_long(argc, argv, "dhvr:m:p:", long_options,
&option_index)) == -1) break; &option_index)) == -1) break;
switch(opt) { switch(opt) {
@ -295,6 +298,9 @@ int main(int argc, char **argv)
case 'p': case 'p':
port = atoi(optarg); port = atoi(optarg);
break; break;
case 'd':
is_daemon = 1;
break;
} }
} }
@ -310,6 +316,12 @@ int main(int argc, char **argv)
return EINVAL; return EINVAL;
} }
/* become daemon if it's necessary */
if (is_daemon && (opt = daemonize())) {
fprintf(stderr, "daemonize() failed (%d).\n Failure.\n", opt);
return EINVAL;
}
sxmp_init(); sxmp_init();
/* all is fine let's init connection subsystem */ /* all is fine let's init connection subsystem */

Loading…
Cancel
Save