Skip to content

Commit

Permalink
clients/upssched.c: enable command-line argument handling [networkups…
Browse files Browse the repository at this point in the history
  • Loading branch information
jimklimov committed Mar 1, 2023
1 parent e8227be commit 9011470
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions clients/upssched.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ typedef struct ttype_s {
static ttype_t *thead = NULL;
static conn_t *connhead = NULL;
static char *cmdscript = NULL, *pipefn = NULL, *lockfn = NULL;
static int verbose = 0; /* use for debugging */

/* ups name and notify type (string) as received from upsmon */
static const char *upsname, *notify_type;
Expand Down Expand Up @@ -163,7 +162,7 @@ static void checktimers(void)
if (emptyctr < EMPTY_WAIT)
return;

if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "Timer queue empty, exiting");

#ifdef UPSSCHED_RACE_TEST
Expand All @@ -185,7 +184,7 @@ static void checktimers(void)
tmpnext = tmp->next;

if (now >= tmp->etime) {
if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "Event: %s ", tmp->name);

exec_cmd(tmp->name);
Expand Down Expand Up @@ -215,7 +214,7 @@ static void start_timer(const char *name, const char *ofsstr)
return;
}

if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "New timer: %s (%ld seconds)", name, ofs);

/* now add to the queue */
Expand Down Expand Up @@ -243,7 +242,7 @@ static void cancel_timer(const char *name, const char *cname)

for (tmp = thead; tmp != NULL; tmp = tmp->next) {
if (!strcmp(tmp->name, name)) { /* match */
if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "Cancelling timer: %s", name);
removetimer(tmp);
return;
Expand All @@ -252,7 +251,7 @@ static void cancel_timer(const char *name, const char *cname)

/* this is not necessarily an error */
if (cname && cname[0]) {
if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "Cancel %s, event: %s", name, cname);

exec_cmd(cname);
Expand Down Expand Up @@ -779,7 +778,7 @@ static void start_daemon(TYPE_FD lockfd)

pipefd = open_sock();

if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "Timer daemon started");

/* release the parent */
Expand Down Expand Up @@ -851,7 +850,7 @@ static void start_daemon(TYPE_FD lockfd)
}
pipefd = open_sock();

if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "Timer daemon started");

/* drop the lock now that the background is running */
Expand Down Expand Up @@ -1217,7 +1216,7 @@ static void parse_at(const char *ntype, const char *un, const char *cmd,
return;
}

if (verbose)
if (nut_debug_level)
upslogx(LOG_INFO, "Executing command: %s", ca1);

exec_cmd(ca1);
Expand Down Expand Up @@ -1324,11 +1323,48 @@ static void checkconf(void)
pconf_finish(&ctx);
}

static void help(const char *arg_progname)
__attribute__((noreturn));

static void help(const char *arg_progname)
{
printf("upssched: upsmon's scheduling helper for offset timers\n");
printf("Practical behavior is managed by UPSNAME and NOTIFYTYPE envvars\n");

printf("\nUsage: %s [OPTIONS]\n\n", arg_progname);
printf(" -D raise debugging level (and stay foreground by default)\n");
printf(" -V display the version of this software\n");
printf(" -h display this help\n");

nut_report_config_flags();

exit(EXIT_SUCCESS);
}


int main(int argc, char **argv)
{
const char *prog = xbasename(argv[0]);
int i;

while ((i = getopt(argc, argv, "+DVh")) != -1) {
switch (i) {
case 'D':
nut_debug_level++;
break;

case 'h':
help(argv[0]);
#ifndef HAVE___ATTRIBUTE__NORETURN
break;
#endif

verbose = 1; /* TODO: remove when done testing */
case 'V':
/* just show the optional CONFIG_FLAGS banner */
nut_report_config_flags();
exit(EXIT_SUCCESS);
}
}

/* normally we don't have stderr, so get this going to syslog early */
open_syslog(prog);
Expand Down

0 comments on commit 9011470

Please sign in to comment.