Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move check for UID 0 (i.e. root) to after the options parsing. #101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 67 additions & 48 deletions aqualinkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <libgen.h>
#include <termios.h>
#include <signal.h>
#include <getopt.h>

#include <time.h> // Need GNU_SOURCE & XOPEN defined for strptime

Expand Down Expand Up @@ -785,6 +786,21 @@ void printHelp()
printf("\t-rsd (RS485 debug)\n");
printf("\t-rsrd (RS485 raw debug)\n");
}

static struct option aqualink_long_options[] =
{
{ "help", no_argument, NULL, 'h'},
{ "no-daemonize", no_argument, NULL, 'd'},
{ "config-file", required_argument, NULL, 'c'},
{ "debug", no_argument, NULL, 'v'},
{ "vv", no_argument, NULL, '0'},
iainchesworth marked this conversation as resolved.
Show resolved Hide resolved
{ "rsd", no_argument, NULL, '1'},
{ "rsrd", no_argument, NULL, '2'},
{0, 0, 0, 0}
};

static char* aqualink_short_options = "hdc:v0123";
iainchesworth marked this conversation as resolved.
Show resolved Hide resolved

int main(int argc, char *argv[])
{
// main_loop ();
Expand All @@ -797,60 +813,59 @@ int main(int argc, char *argv[])
bool cmdln_debugRS485 = false;
bool cmdln_lograwRS485 = false;

if (argc > 1 && strcmp(argv[1], "-h") == 0)
{
printHelp();
return 0;
}

// struct lws_context_creation_info info;
// Log only NOTICE messages and above. Debug and info messages
// will not be logged to syslog.
setlogmask(LOG_UPTO(LOG_NOTICE));

if (getuid() != 0)
{
//logMessage(LOG_ERR, "%s Can only be run as root\n", argv[0]);
fprintf(stderr, "ERROR %s Can only be run as root\n", argv[0]);
return EXIT_FAILURE;
}

// Initialize the daemon's parameters.
init_parameters(&_config_parameters);
cfgFile = defaultCfg;
//sprintf(cfgFile, "%s", DEFAULT_CONFIG_FILE);

for (i = 1; i < argc; i++)
int ch = 0;

while ((ch = getopt_long(argc, argv, aqualink_short_options, aqualink_long_options, NULL)) != -1)
{
if (strcmp(argv[i], "-h") == 0)
{
printHelp();
return 0;
}
if (strcmp(argv[i], "-d") == 0)
{
_config_parameters.deamonize = false;
}
else if (strcmp(argv[i], "-c") == 0)
{
cfgFile = argv[++i];
}
else if (strcmp(argv[i], "-vv") == 0)
{
cmdln_loglevel = LOG_DEBUG_SERIAL;
}
else if (strcmp(argv[i], "-v") == 0)
{
cmdln_loglevel = LOG_DEBUG;
}
else if (strcmp(argv[i], "-rsd") == 0)
{
cmdln_debugRS485 = true;
}
else if (strcmp(argv[i], "-rsrd") == 0)
{
cmdln_lograwRS485 = true;
}
// check to see if a single character or long option came through
switch (ch)
{
case 'd': // short option 'd' / long option "no-daemonize"
_config_parameters.deamonize = false;
break;

case 'c': // short option 'c' / long option "config-file"
cfgFile = optarg;
break;

case 'v': // short option 'v' / long option "debug"
cmdln_loglevel = LOG_DEBUG;
break;

case '0': // short option '0' / long option "vv"
cmdln_loglevel = LOG_DEBUG_SERIAL;
break;

case '1': // short option '1' / long option "rsd"
cmdln_debugRS485 = true;
break;

case '2': // short option '2' / long option "rsrd"
cmdln_lograwRS485 = true;
break;

case 'h': // short option 'h' / long option "help"
default: // any unknown options
printHelp();
return EXIT_SUCCESS;;
}
}

if (getuid() != 0)
{
//logMessage(LOG_ERR, "%s Can only be run as root\n", argv[0]);
fprintf(stderr, "ERROR %s Can only be run as root\n", argv[0]);
return EXIT_FAILURE;
}

initButtons(&_aqualink_data);
Expand All @@ -867,10 +882,14 @@ int main(int argc, char *argv[])
_config_parameters.log_raw_RS_bytes = true;


if (_config_parameters.display_warnings_web == true)
setLoggingPrms(_config_parameters.log_level, _config_parameters.deamonize, _config_parameters.log_file, _aqualink_data.last_display_message);
else
setLoggingPrms(_config_parameters.log_level, _config_parameters.deamonize, _config_parameters.log_file, NULL);
if (_config_parameters.display_warnings_web == true)
{
setLoggingPrms(_config_parameters.log_level, _config_parameters.deamonize, _config_parameters.log_file, _aqualink_data.last_display_message);
}
else
{
setLoggingPrms(_config_parameters.log_level, _config_parameters.deamonize, _config_parameters.log_file, NULL);
}

logMessage(LOG_NOTICE, "%s v%s\n", AQUALINKD_NAME, AQUALINKD_VERSION);

Expand Down Expand Up @@ -950,7 +969,7 @@ int main(int argc, char *argv[])
main_loop();
}

exit(EXIT_SUCCESS);
return EXIT_SUCCESS;
}

/*
Expand Down