Skip to content

Commit

Permalink
Relocate device path sanity check to main(), early feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Jan 2, 2024
1 parent 5a1f8a0 commit cfbb154
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
26 changes: 20 additions & 6 deletions src/watchdogd.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,7 @@ extern int __wdog_loglevel(char *level);

int main(int argc, char *argv[])
{
int background = 1;
int use_syslog = 1;
int c, status;
int log_opts = LOG_NDELAY | LOG_NOWAIT | LOG_PID;
char *dev = NULL;
struct option long_options[] = {
{"config", 1, 0, 'f'},
{"foreground", 0, 0, 'n'},
Expand All @@ -212,6 +208,11 @@ int main(int argc, char *argv[])
{"timeout", 1, 0, 'T'},
{NULL, 0, 0, 0}
};
int background = 1;
int use_syslog = 1;
char devnode[256];
char *dev = NULL;
int c, status;
uev_ctx_t ctx;

prognm = progname(argv[0]);
Expand Down Expand Up @@ -280,8 +281,21 @@ int main(int argc, char *argv[])
uev_init(&ctx);

/* BusyBox watchdogd compat. */
if (optind < argc)
dev = argv[optind];
if (optind < argc) {
strlcpy(devnode, argv[optind], sizeof(devnode));

/* Sanity check device name */
if (strncmp(devnode, _PATH_DEV, strlen(_PATH_DEV)) || strstr(devnode, "..")) {
fprintf(stderr, "Invalid watchdog device: %s\n", devnode);
return 1;
}
if (!fexist(devnode)) {
fprintf(stderr, "Cannot find watchdog device: %s\n", devnode);
return 1;
}

dev = devnode;
}

/* Check for command line options */
if (!opt_config) {
Expand Down
13 changes: 2 additions & 11 deletions src/wdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,18 +656,9 @@ int wdt_init(uev_ctx_t *ctx, const char *name)
return 0;

if (name) {
char devnode[256];

/* Sanity check device name */
strlcpy(devnode, name, sizeof(devnode));
if (strncmp(devnode, _PATH_DEV, strlen(_PATH_DEV)) || strstr(devnode, ".."))
goto skip;

/* Check if already in .conf file */
dev = find(devnode);
if (!dev)
wdt_add(devnode, period, timeout, magic, 1);
skip:
if (!find(name))
wdt_add(name, period, timeout, magic, 1);
}

TAILQ_FOREACH(dev, &devices, link) {
Expand Down

0 comments on commit cfbb154

Please sign in to comment.