Skip to content

Commit

Permalink
console: extract console_printf from gprocess
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed Oct 20, 2024
1 parent b660f7b commit d627e47
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 79 deletions.
37 changes: 36 additions & 1 deletion lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,44 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <syslog.h>

GMutex console_lock;
gboolean console_present = FALSE;
gboolean using_initial_console = TRUE;
const gchar *console_prefix;

/**
* console_printf:
* @fmt: format string
* @...: arguments to @fmt
*
* This function sends a message to the client preferring to use the stderr
* channel as long as it is available and switching to using syslog() if it
* isn't. Generally the stderr channell will be available in the startup
* process and in the beginning of the first startup in the
* supervisor/daemon processes. Later on the stderr fd will be closed and we
* have to fall back to using the system log.
**/
void
console_printf(const gchar *fmt, ...)
{
gchar buf[2048];
va_list ap;

va_start(ap, fmt);
g_vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (console_is_present(FALSE))
fprintf(stderr, "%s: %s\n", console_prefix, buf);
else
{
openlog(console_prefix, LOG_PID, LOG_DAEMON);
syslog(LOG_CRIT, "%s\n", buf);
closelog();
}
}


/* NOTE: this is not synced with any changes and is just an indication whether we have a console */
gboolean
Expand Down Expand Up @@ -120,9 +154,10 @@ console_release(void)
}

void
console_global_init(void)
console_global_init(const gchar *console_prefix_)
{
g_mutex_init(&console_lock);
console_prefix = console_prefix_;
}

void
Expand Down
4 changes: 3 additions & 1 deletion lib/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@

#include "syslog-ng.h"

void console_printf(const gchar *fmt, ...) __attribute__ ((format (printf, 1, 2)));

gboolean console_is_present(gboolean exclude_initial);
void console_acquire_from_fds(gint fds[3]);
void console_acquire_from_stdio(void);
void console_release(void);

void console_global_init(void);
void console_global_init(const gchar *console_prefix);
void console_global_deinit(void);

#endif
113 changes: 40 additions & 73 deletions lib/gprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,41 +615,6 @@ g_process_set_check(gint check_period, gboolean (*check_fn)(void))
process_opts.check_fn = check_fn;
}


/**
* g_process_message:
* @fmt: format string
* @...: arguments to @fmt
*
* This function sends a message to the client preferring to use the stderr
* channel as long as it is available and switching to using syslog() if it
* isn't. Generally the stderr channell will be available in the startup
* process and in the beginning of the first startup in the
* supervisor/daemon processes. Later on the stderr fd will be closed and we
* have to fall back to using the system log.
**/
void
g_process_message(const gchar *fmt, ...)
{
gchar buf[2048];
va_list ap;

va_start(ap, fmt);
g_vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (console_is_present(FALSE))
fprintf(stderr, "%s: %s\n", process_opts.name, buf);
else
{
gchar name[32];

g_snprintf(name, sizeof(name), "%s/%s", process_kind == G_PK_SUPERVISOR ? "supervise" : "daemon", process_opts.name);
openlog(name, LOG_PID, LOG_DAEMON);
syslog(LOG_CRIT, "%s\n", buf);
closelog();
}
}

/**
* g_process_setup_console:
*
Expand Down Expand Up @@ -701,8 +666,8 @@ g_process_change_limits(void)
}

if (setrlimit(RLIMIT_NOFILE, &limit) < 0)
g_process_message("Error setting file number limit; limit='%d'; error='%s'", process_opts.fd_limit_min,
g_strerror(errno));
console_printf("Error setting file number limit; limit='%d'; error='%s'", process_opts.fd_limit_min,
g_strerror(errno));
}

/**
Expand Down Expand Up @@ -731,7 +696,7 @@ g_process_set_dumpable(void)
rc = prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);

if (rc < 0)
g_process_message("Cannot set process to be dumpable; error='%s'", g_strerror(errno));
console_printf("Cannot set process to be dumpable; error='%s'", g_strerror(errno));
}
#endif
}
Expand All @@ -753,7 +718,7 @@ g_process_enable_core(void)

limit.rlim_cur = limit.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &limit) < 0)
g_process_message("Error setting core limit to infinity; error='%s'", g_strerror(errno));
console_printf("Error setting core limit to infinity; error='%s'", g_strerror(errno));

}
}
Expand Down Expand Up @@ -811,7 +776,7 @@ g_process_write_pidfile(pid_t pid)
}
else
{
g_process_message("Error creating pid file; file='%s', error='%s'", pidfile, g_strerror(errno));
console_printf("Error creating pid file; file='%s', error='%s'", pidfile, g_strerror(errno));
}

}
Expand All @@ -831,7 +796,7 @@ g_process_remove_pidfile(void)

if (unlink(pidfile) < 0)
{
g_process_message("Error removing pid file; file='%s', error='%s'", pidfile, g_strerror(errno));
console_printf("Error removing pid file; file='%s', error='%s'", pidfile, g_strerror(errno));
}
}

Expand All @@ -851,13 +816,13 @@ g_process_change_root(void)
{
if (chroot(process_opts.chroot_dir) < 0)
{
g_process_message("Error in chroot(); chroot='%s', error='%s'\n", process_opts.chroot_dir, g_strerror(errno));
console_printf("Error in chroot(); chroot='%s', error='%s'\n", process_opts.chroot_dir, g_strerror(errno));
return FALSE;
}
if (chdir("/") < 0)
{
g_process_message("Error in chdir() after chroot; chroot='%s', error='%s'\n", process_opts.chroot_dir,
g_strerror(errno));
console_printf("Error in chdir() after chroot; chroot='%s', error='%s'\n", process_opts.chroot_dir,
g_strerror(errno));
return FALSE;
}
}
Expand Down Expand Up @@ -891,14 +856,14 @@ g_process_change_user(void)
{
if (setgid((gid_t) process_opts.gid) < 0)
{
g_process_message("Error in setgid(); group='%s', gid='%d', error='%s'", process_opts.group, process_opts.gid,
g_strerror(errno));
console_printf("Error in setgid(); group='%s', gid='%d', error='%s'", process_opts.group, process_opts.gid,
g_strerror(errno));
if (getuid() == 0)
return FALSE;
}
if (process_opts.user && initgroups(process_opts.user, (gid_t) process_opts.gid) < 0)
{
g_process_message("Error in initgroups(); user='%s', error='%s'", process_opts.user, g_strerror(errno));
console_printf("Error in initgroups(); user='%s', error='%s'", process_opts.user, g_strerror(errno));
if (getuid() == 0)
return FALSE;
}
Expand All @@ -908,8 +873,8 @@ g_process_change_user(void)
{
if (setuid((uid_t) process_opts.uid) < 0)
{
g_process_message("Error in setuid(); user='%s', uid='%d', error='%s'", process_opts.user, process_opts.uid,
g_strerror(errno));
console_printf("Error in setuid(); user='%s', uid='%d', error='%s'", process_opts.user, process_opts.uid,
g_strerror(errno));
if (getuid() == 0)
return FALSE;
}
Expand Down Expand Up @@ -938,15 +903,15 @@ g_process_change_caps(void)

if (cap == NULL)
{
g_process_message("Error parsing capabilities: %s", process_opts.caps);
console_printf("Error parsing capabilities: %s", process_opts.caps);
g_process_disable_caps();
return FALSE;
}
else
{
if (cap_set_proc(cap) == -1)
{
g_process_message("Error setting capabilities, capability management disabled; error='%s'", g_strerror(errno));
console_printf("Error setting capabilities, capability management disabled; error='%s'", g_strerror(errno));
g_process_disable_caps();

}
Expand All @@ -972,13 +937,13 @@ g_process_resolve_names(void)
gboolean result = TRUE;
if (process_opts.user && !resolve_user(process_opts.user, &process_opts.uid))
{
g_process_message("Error resolving user; user='%s'", process_opts.user);
console_printf("Error resolving user; user='%s'", process_opts.user);
process_opts.uid = -1;
result = FALSE;
}
if (process_opts.group && !resolve_group(process_opts.group, &process_opts.gid))
{
g_process_message("Error resolving group; group='%s'", process_opts.group);
console_printf("Error resolving group; group='%s'", process_opts.group);
process_opts.gid = -1;
result = FALSE;
}
Expand Down Expand Up @@ -1007,8 +972,10 @@ g_process_change_dir(void)
cwd = get_installation_path_for(SYSLOG_NG_PATH_PIDFILEDIR);

if (cwd)
if (chdir(cwd))
g_process_message("Error changing to directory=%s, errcode=%d", cwd, errno);
{
if (chdir(cwd))
console_printf("Error changing to directory=%s, errcode=%d", cwd, errno);
}
}

/* this check is here to avoid having to change directory early in the startup process */
Expand All @@ -1018,8 +985,8 @@ g_process_change_dir(void)

if (!getcwd(buf, sizeof(buf)))
strncpy(buf, "unable-to-query", sizeof(buf));
g_process_message("Unable to write to current directory, core dumps will not be generated; dir='%s', error='%s'", buf,
g_strerror(errno));
console_printf("Unable to write to current directory, core dumps will not be generated; dir='%s', error='%s'", buf,
g_strerror(errno));
}

}
Expand Down Expand Up @@ -1159,14 +1126,14 @@ g_process_perform_supervise(void)
{
if (pipe(init_result_pipe) != 0)
{
g_process_message("Error daemonizing process, cannot open pipe; error='%s'", g_strerror(errno));
console_printf("Error daemonizing process, cannot open pipe; error='%s'", g_strerror(errno));
g_process_startup_failed(1, TRUE);
}

/* fork off a child process */
if ((pid = fork()) < 0)
{
g_process_message("Error forking child process; error='%s'", g_strerror(errno));
console_printf("Error forking child process; error='%s'", g_strerror(errno));
g_process_startup_failed(1, TRUE);
}
else if (pid != 0)
Expand Down Expand Up @@ -1203,8 +1170,8 @@ g_process_perform_supervise(void)
i++;
}
if (i == 6)
g_process_message("Initialization failed but the daemon did not exit, even when forced to, trying to recover; pid='%d'",
pid);
console_printf("Initialization failed but the daemon did not exit, even when forced to, trying to recover; pid='%d'",
pid);
continue;
}

Expand All @@ -1226,7 +1193,7 @@ g_process_perform_supervise(void)
if (!exited)
{
gint j = 0;
g_process_message("Daemon deadlock detected, killing process;");
console_printf("Daemon deadlock detected, killing process;");
deadlock = TRUE;

while (j < 6 && waitpid(pid, &rc, WNOHANG) == 0)
Expand All @@ -1237,7 +1204,7 @@ g_process_perform_supervise(void)
j++;
}
if (j == 6)
g_process_message("The daemon did not exit after deadlock, even when forced to, trying to recover; pid='%d'", pid);
console_printf("The daemon did not exit after deadlock, even when forced to, trying to recover; pid='%d'", pid);
}
}
else
Expand All @@ -1257,14 +1224,14 @@ g_process_perform_supervise(void)
switch (npid)
{
case -1:
g_process_message("Could not fork for external notification; reason='%s'", strerror(errno));
console_printf("Could not fork for external notification; reason='%s'", strerror(errno));
break;

case 0:
switch(fork())
{
case -1:
g_process_message("Could not fork for external notification; reason='%s'", strerror(errno));
console_printf("Could not fork for external notification; reason='%s'", strerror(errno));
exit(1);
break;
case 0:
Expand Down Expand Up @@ -1292,7 +1259,7 @@ g_process_perform_supervise(void)
argbuf,
(deadlock || !WIFSIGNALED(rc) || WTERMSIG(rc) != SIGKILL) ? "restarting" : "not-restarting",
(gchar *) NULL);
g_process_message("Could not execute external notification; reason='%s'", strerror(errno));
console_printf("Could not execute external notification; reason='%s'", strerror(errno));
break;

default:
Expand All @@ -1306,18 +1273,18 @@ g_process_perform_supervise(void)
}
if (deadlock || !WIFSIGNALED(rc) || WTERMSIG(rc) != SIGKILL)
{
g_process_message("Daemon exited due to a deadlock/signal/failure, restarting; exitcode='%d'", rc);
console_printf("Daemon exited due to a deadlock/signal/failure, restarting; exitcode='%d'", rc);
sleep(1);
}
else
{
g_process_message("Daemon was killed, not restarting; exitcode='%d'", rc);
console_printf("Daemon was killed, not restarting; exitcode='%d'", rc);
break;
}
}
else
{
g_process_message("Daemon exited gracefully, not restarting; exitcode='%d'", rc);
console_printf("Daemon exited gracefully, not restarting; exitcode='%d'", rc);
break;
}
}
Expand Down Expand Up @@ -1362,13 +1329,13 @@ g_process_start(void)
/* no supervisor, sends result to startup process directly */
if (pipe(init_result_pipe) != 0)
{
g_process_message("Error daemonizing process, cannot open pipe; error='%s'", g_strerror(errno));
console_printf("Error daemonizing process, cannot open pipe; error='%s'", g_strerror(errno));
exit(1);
}

if ((pid = fork()) < 0)
{
g_process_message("Error forking child process; error='%s'", g_strerror(errno));
console_printf("Error forking child process; error='%s'", g_strerror(errno));
exit(1);
}
else if (pid != 0)
Expand Down Expand Up @@ -1399,13 +1366,13 @@ g_process_start(void)
/* full blown startup/supervisor/daemon */
if (pipe(startup_result_pipe) != 0)
{
g_process_message("Error daemonizing process, cannot open pipe; error='%s'", g_strerror(errno));
console_printf("Error daemonizing process, cannot open pipe; error='%s'", g_strerror(errno));
exit(1);
}
/* first fork off supervisor process */
if ((pid = fork()) < 0)
{
g_process_message("Error forking child process; error='%s'", g_strerror(errno));
console_printf("Error forking child process; error='%s'", g_strerror(errno));
exit(1);
}
else if (pid != 0)
Expand Down
Loading

0 comments on commit d627e47

Please sign in to comment.