Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Functions | Variables

syslogger.h File Reference

#include <limits.h>
Include dependency graph for syslogger.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PipeProtoHeader
union  PipeProtoChunk

Defines

#define PIPE_CHUNK_SIZE   512
#define PIPE_HEADER_SIZE   offsetof(PipeProtoHeader, data)
#define PIPE_MAX_PAYLOAD   ((int) (PIPE_CHUNK_SIZE - PIPE_HEADER_SIZE))

Functions

int SysLogger_Start (void)
void write_syslogger_file (const char *buffer, int count, int dest)

Variables

bool Logging_collector
int Log_RotationAge
int Log_RotationSize
PGDLLIMPORT char * Log_directory
PGDLLIMPORT char * Log_filename
bool Log_truncate_on_rotation
int Log_file_mode
bool am_syslogger
int syslogPipe [2]

Define Documentation

#define PIPE_CHUNK_SIZE   512

Definition at line 41 of file syslogger.h.

#define PIPE_HEADER_SIZE   offsetof(PipeProtoHeader, data)

Definition at line 60 of file syslogger.h.

Referenced by process_pipe_input(), and write_pipe_chunks().

#define PIPE_MAX_PAYLOAD   ((int) (PIPE_CHUNK_SIZE - PIPE_HEADER_SIZE))

Definition at line 61 of file syslogger.h.

Referenced by process_pipe_input(), and write_pipe_chunks().


Function Documentation

int SysLogger_Start ( void   ) 

Definition at line 535 of file syslogger.c.

References close, ClosePostmasterPorts(), ereport, errcode_for_file_access(), errcode_for_socket_access(), errmsg(), FATAL, filename, first_syslogger_file_time, fork_process(), LOG, Log_directory, logfile_getname(), logfile_open(), Logging_collector, mkdir, NULL, on_exit_reset(), pfree(), PGSharedMemoryDetach(), redirection_done, syslogFile, SysLoggerMain(), and syslogPipe.

Referenced by PostmasterMain(), reaper(), and ServerLoop().

{
    pid_t       sysloggerPid;
    char       *filename;

    if (!Logging_collector)
        return 0;

    /*
     * If first time through, create the pipe which will receive stderr
     * output.
     *
     * If the syslogger crashes and needs to be restarted, we continue to use
     * the same pipe (indeed must do so, since extant backends will be writing
     * into that pipe).
     *
     * This means the postmaster must continue to hold the read end of the
     * pipe open, so we can pass it down to the reincarnated syslogger. This
     * is a bit klugy but we have little choice.
     */
#ifndef WIN32
    if (syslogPipe[0] < 0)
    {
        if (pipe(syslogPipe) < 0)
            ereport(FATAL,
                    (errcode_for_socket_access(),
                     (errmsg("could not create pipe for syslog: %m"))));
    }
#else
    if (!syslogPipe[0])
    {
        SECURITY_ATTRIBUTES sa;

        memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
        sa.nLength = sizeof(SECURITY_ATTRIBUTES);
        sa.bInheritHandle = TRUE;

        if (!CreatePipe(&syslogPipe[0], &syslogPipe[1], &sa, 32768))
            ereport(FATAL,
                    (errcode_for_file_access(),
                     (errmsg("could not create pipe for syslog: %m"))));
    }
#endif

    /*
     * Create log directory if not present; ignore errors
     */
    mkdir(Log_directory, S_IRWXU);

    /*
     * The initial logfile is created right in the postmaster, to verify that
     * the Log_directory is writable.  We save the reference time so that
     * the syslogger child process can recompute this file name.
     *
     * It might look a bit strange to re-do this during a syslogger restart,
     * but we must do so since the postmaster closed syslogFile after the
     * previous fork (and remembering that old file wouldn't be right anyway).
     * Note we always append here, we won't overwrite any existing file.  This
     * is consistent with the normal rules, because by definition this is not
     * a time-based rotation.
     */
    first_syslogger_file_time = time(NULL);
    filename = logfile_getname(first_syslogger_file_time, NULL);

    syslogFile = logfile_open(filename, "a", false);

    pfree(filename);

#ifdef EXEC_BACKEND
    switch ((sysloggerPid = syslogger_forkexec()))
#else
    switch ((sysloggerPid = fork_process()))
#endif
    {
        case -1:
            ereport(LOG,
                    (errmsg("could not fork system logger: %m")));
            return 0;

#ifndef EXEC_BACKEND
        case 0:
            /* in postmaster child ... */
            /* Close the postmaster's sockets */
            ClosePostmasterPorts(true);

            /* Lose the postmaster's on-exit routines */
            on_exit_reset();

            /* Drop our connection to postmaster's shared memory, as well */
            PGSharedMemoryDetach();

            /* do the work */
            SysLoggerMain(0, NULL);
            break;
#endif

        default:
            /* success, in postmaster */

            /* now we redirect stderr, if not done already */
            if (!redirection_done)
            {
#ifndef WIN32
                fflush(stdout);
                if (dup2(syslogPipe[1], fileno(stdout)) < 0)
                    ereport(FATAL,
                            (errcode_for_file_access(),
                             errmsg("could not redirect stdout: %m")));
                fflush(stderr);
                if (dup2(syslogPipe[1], fileno(stderr)) < 0)
                    ereport(FATAL,
                            (errcode_for_file_access(),
                             errmsg("could not redirect stderr: %m")));
                /* Now we are done with the write end of the pipe. */
                close(syslogPipe[1]);
                syslogPipe[1] = -1;
#else
                int         fd;

                /*
                 * open the pipe in binary mode and make sure stderr is binary
                 * after it's been dup'ed into, to avoid disturbing the pipe
                 * chunking protocol.
                 */
                fflush(stderr);
                fd = _open_osfhandle((intptr_t) syslogPipe[1],
                                     _O_APPEND | _O_BINARY);
                if (dup2(fd, _fileno(stderr)) < 0)
                    ereport(FATAL,
                            (errcode_for_file_access(),
                             errmsg("could not redirect stderr: %m")));
                close(fd);
                _setmode(_fileno(stderr), _O_BINARY);

                /*
                 * Now we are done with the write end of the pipe.
                 * CloseHandle() must not be called because the preceding
                 * close() closes the underlying handle.
                 */
                syslogPipe[1] = 0;
#endif
                redirection_done = true;
            }

            /* postmaster will never write the file; close it */
            fclose(syslogFile);
            syslogFile = NULL;
            return (int) sysloggerPid;
    }

    /* we should never reach here */
    return 0;
}

void write_syslogger_file ( const char *  buffer,
int  count,
int  dest 
)

Definition at line 997 of file syslogger.c.

References csvlogFile, LOG_DESTINATION_CSVLOG, logfile, NULL, open_csvlogfile(), strerror(), syslogFile, and write_stderr.

Referenced by flush_pipe_input(), process_pipe_input(), send_message_to_server_log(), and write_csvlog().

{
    int         rc;
    FILE       *logfile;

    if (destination == LOG_DESTINATION_CSVLOG && csvlogFile == NULL)
        open_csvlogfile();

    logfile = destination == LOG_DESTINATION_CSVLOG ? csvlogFile : syslogFile;
    rc = fwrite(buffer, 1, count, logfile);

    /* can't use ereport here because of possible recursion */
    if (rc != count)
        write_stderr("could not write to log file: %s\n", strerror(errno));
}


Variable Documentation

Definition at line 83 of file syslogger.c.

Referenced by send_message_to_server_log(), SysLoggerMain(), and write_csvlog().

PGDLLIMPORT char* Log_directory

Definition at line 78 of file syslogger.c.

Referenced by logfile_open(), and show_log_file_mode().

PGDLLIMPORT char* Log_filename

Definition at line 76 of file syslogger.c.

Referenced by logfile_getname(), pg_logdir_ls(), and SysLoggerMain().

Definition at line 73 of file syslogger.c.

Referenced by set_next_rotation_time(), and SysLoggerMain().

Definition at line 74 of file syslogger.c.

Referenced by SysLoggerMain().

Definition at line 77 of file syslogger.c.

Referenced by logfile_rotate().

Definition at line 72 of file syslogger.c.

Referenced by pg_rotate_logfile(), ServerLoop(), and SysLogger_Start().

int syslogPipe[2]

Definition at line 122 of file syslogger.c.

Referenced by ClosePostmasterPorts(), SysLogger_Start(), and SysLoggerMain().