Skip to content

Commit

Permalink
Ensure correct behaviour of debug_file
Browse files Browse the repository at this point in the history
- Factorize re-opening of debug file according to the configuration

- Ensure that the debug file is replaced only if opening the new one is
  successful.
  • Loading branch information
dacav committed Dec 5, 2024
1 parent 9fda03a commit 30c9ad9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
19 changes: 12 additions & 7 deletions cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ static void cfg_load_arg(cfg_t *cfg, const char *source, const char *arg) {
} else if (strncmp(arg, "cue_prompt=", 11) == 0) {
cfg->cue_prompt = arg + 11;
} else if (strncmp(arg, "debug_file=", 11) == 0) {
const char *filename = arg + 11;
debug_close(cfg->debug_file);
cfg->debug_file = debug_open(filename);
cfg->debug_file = debug_replace(cfg->debug_file, arg + 11);
} else {
debug_dbg(cfg, "WARNING: ignored config \"%s\" from %s", arg, source);
}
Expand Down Expand Up @@ -102,14 +100,17 @@ static void cfg_load_defaults(cfg_t *cfg, const char *config_path) {

void cfg_init(cfg_t *cfg, int flags, int argc, const char **argv) {
int i;
const char *config_path = NULL;
const char *config_path = NULL, *debug_file_path = NULL;

memset(cfg, 0, sizeof(cfg_t));
cfg->debug_file = DEFAULT_DEBUG_FILE;
cfg->userpresence = -1;
cfg->userverification = -1;
cfg->pinverification = -1;

// Early-loaded arguments
// 'config=', if existing, it supplies the default configuration.
// 'debug' is enabled immediately to avoid blind spots.
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "debug") == 0) {
cfg->debug = 1;
Expand All @@ -120,12 +121,16 @@ void cfg_init(cfg_t *cfg, int flags, int argc, const char **argv) {
continue;
}
if (strncmp(argv[i], "debug_file=", 11) == 0) {
const char *filename = argv[i] + 11;
debug_close(cfg->debug_file);
cfg->debug_file = debug_open(filename);
debug_file_path = argv[i] + 11;
continue;
}
}

// 'debug_file' is loaded after the loop to make sure that
// debug_file opening issues can be debugged too.
if (debug_file_path)
cfg->debug_file = debug_open(debug_file_path);

cfg_load_defaults(cfg, config_path);

for (i = 0; i < argc; i++) {
Expand Down
61 changes: 47 additions & 14 deletions debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@
#define DEBUG_FMT "debug(pam_u2f): %s:%d (%s): %s%s"
#define MSGLEN 2048

FILE *debug_open(const char *filename) {
static int debug_get_file(const char *name, FILE **out)
{
struct stat st;
FILE *file;
int fd;

if (strcmp(filename, "stdout") == 0)
return stdout;
if (strcmp(filename, "stderr") == 0)
return stderr;
if (strcmp(filename, "syslog") == 0)
return NULL;
if (strcmp(name, "stdout") == 0) {
*out = stdout;
return 0;
}
if (strcmp(name, "stderr") == 0) {
*out = stderr;
return 0;
}
if (strcmp(name, "syslog") == 0) {
*out = NULL;
return 0;
}

fd = open(filename, O_WRONLY | O_APPEND | O_CLOEXEC | O_NOFOLLOW | O_NOCTTY | O_CREAT);
fd = open(name, O_WRONLY | O_APPEND | O_CLOEXEC | O_NOFOLLOW | O_NOCTTY | O_CREAT);
if (fd == -1 || fstat(fd, &st) != 0) {
D(DEFAULT_DEBUG_FILE, "Could not open %s: %s", filename, strerror(errno));
D(DEFAULT_DEBUG_FILE, "Could not open %s: %s", name, strerror(errno));
goto err;
}

Expand All @@ -39,19 +46,45 @@ FILE *debug_open(const char *filename) {
goto err;
#endif

if ((file = fdopen(fd, "a")) != NULL)
return file;
if ((file = fdopen(fd, "a")) != NULL) {
*out = file;
return 0;
}

err:
if (fd != -1)
close(fd);

return DEFAULT_DEBUG_FILE; /* fallback to default */
return -1;
}

FILE *debug_open(const char *name) {
FILE *ret;

if (debug_get_file(name, &ret))
return DEFAULT_DEBUG_FILE;

return ret;
}

void debug_close(FILE *f) {
if (f != NULL && f != stdout && f != stderr)
fclose(f);
if (f == NULL || f == stdout || f == stderr)
return;

if (fflush(f) == EOF)
D(DEFAULT_DEBUG_FILE, "Could not close debug file: %s", strerror(errno));

fclose(f);
}

FILE *debug_replace(FILE *old, const char *new_name) {
FILE *new;

if (debug_get_file(new_name, &new))
return old;

debug_close(old);
return new;
}

static void do_log(FILE *debug_file, const char *file, int line,
Expand Down
2 changes: 2 additions & 0 deletions debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

FILE *debug_open(const char *);
void debug_close(FILE *f);
FILE *debug_replace(FILE *old, const char *new_path);

void debug_fprintf(FILE *, const char *, int, const char *, const char *, ...)
ATTRIBUTE_FORMAT(printf, 5, 6);

Expand Down

0 comments on commit 30c9ad9

Please sign in to comment.