Skip to content

Commit

Permalink
Added function for testing conf file and some comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Hnidek committed Feb 28, 2015
1 parent 5d9c932 commit 3910bb6
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ int read_conf_file(void)
return ret;
}

/**
* \brief This function tries to test config file
*/
int test_conf_file(char *_conf_file_name)
{
FILE *conf_file = NULL;
int ret = -1;

conf_file = fopen(_conf_file_name, "r");

if(conf_file == NULL) {
fprintf(stderr, "Can't read config file %s\n",
_conf_file_name);
return EXIT_FAILURE;
}

ret = fscanf(conf_file, "%d", &delay);

if(ret <= 0) {
fprintf(stderr, "Wrong config file %s\n",
_conf_file_name);
}

fclose(conf_file);

if(ret > 0)
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}

/**
* \brief Callback function for handling signals.
* \param sig identifier of signal
Expand All @@ -88,16 +119,19 @@ void print_help(void)
{
printf("\n Usage: %s [OPTIONS]\n\n", app_name);
printf(" Options:\n");
printf(" -h --help Print this help\n");
printf(" -c --confile filename Read configuration from the file\n");
printf(" -l --logfile filename Write logs to the file\n");
printf(" -h --help Print this help\n");
printf(" -c --confile filename Read configuration from the file\n");
printf(" -t --testconf filename Test configuration file\n");
printf(" -l --logfile filename Write logs to the file\n");
printf("\n");
}

/* Main function */
int main(int argc, char *argv[])
{
static struct option long_options[] = {
{"confile", required_argument, 0, 'c'},
{"testconf", required_argument, 0, 't'},
{"logfile", required_argument, 0, 'l'},
{"help", no_argument, 0, 'h'},
{NULL, 0, 0, 0}
Expand All @@ -107,18 +141,20 @@ int main(int argc, char *argv[])

app_name = argv[0];

while( (value = getopt_long(argc, argv, "c:l:h", long_options, &option_index)) != -1) {
/* Try to process all command line arguments */
while( (value = getopt_long(argc, argv, "c:l:t:h", long_options, &option_index)) != -1) {
switch(value) {
case 'c':
conf_file_name = strdup(optarg);
break;
case 't':
return test_conf_file(optarg);
case 'l':
log_file_name = strdup(optarg);
break;
case 'h':
print_help();
return EXIT_SUCCESS;
break;
case '?':
print_help();
return EXIT_FAILURE;
Expand All @@ -127,15 +163,15 @@ int main(int argc, char *argv[])
}
}

running = 1;

openlog(argv[0], LOG_PID|LOG_CONS, LOG_USER);

/* Daemon will handle two signals */
signal(SIGINT, vs_handle_signal);
signal(SIGHUP, vs_handle_signal);

/* Open system log and write message to it */
openlog(argv[0], LOG_PID|LOG_CONS, LOG_USER);
syslog(LOG_INFO, "Started %s", app_name);

/* Try to open log file to this daemon */
if(log_file_name != NULL) {
log_stream = fopen(log_file_name, "a+");
if (log_stream == NULL)
Expand All @@ -149,6 +185,9 @@ int main(int argc, char *argv[])
/* Read configuration from config file */
read_conf_file();

/* This global variable can be changed in function handling signal */
running = 1;

/* Never ending loop of server */
while(running == 1) {
/* Debug print */
Expand All @@ -157,18 +196,22 @@ int main(int argc, char *argv[])
/* TODO: dome something useful here */

/* Real server should use select() or poll() for waiting at
* asynchronous event */
* asynchronous event. Note: sleep() is interrupted, when
* signal is received. */
sleep(delay);
}

/* Close log file, when it is used. */
if (log_stream != stdout)
{
fclose(log_stream);
}

/* Write system log and close it. */
syslog(LOG_INFO, "Stopped %s", app_name);
closelog();

/* Free allocated memory */
if(conf_file_name != NULL) free(conf_file_name);
if(log_file_name != NULL) free(log_file_name);

Expand Down

0 comments on commit 3910bb6

Please sign in to comment.