syslogger.c 21.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*-------------------------------------------------------------------------
 *
 * syslogger.c
 *
 * The system logger (syslogger) is new in Postgres 8.0. It catches all
 * stderr output from the postmaster, backends, and other subprocesses
 * by redirecting to a pipe, and writes it to a set of logfiles.
 * It's possible to have size and age limits for the logfile configured
 * in postgresql.conf. If these limits are reached or passed, the
 * current logfile is closed and a new one is created (rotated).
 * The logfiles are stored in a subdirectory (configurable in
 * postgresql.conf), using an internal naming scheme that mangles
 * creation time and current postmaster pid.
 *
 * Author: Andreas Pflug <pgadmin@pse-consulting.de>
 *
17
 * Copyright (c) 2004-2006, PostgreSQL Global Development Group
18 19 20
 *
 *
 * IDENTIFICATION
21
 *	  $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.27 2006/07/11 18:26:10 momjian Exp $
22 23 24 25 26 27 28 29 30 31
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
32
#include <sys/time.h>
33 34 35

#include "libpq/pqsignal.h"
#include "miscadmin.h"
36
#include "pgtime.h"
37
#include "postmaster/fork_process.h"
38 39 40 41 42 43
#include "postmaster/postmaster.h"
#include "postmaster/syslogger.h"
#include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
44
#include "utils/timestamp.h"
45

46 47 48 49 50 51 52 53 54 55 56 57
/*
 * We really want line-buffered mode for logfile output, but Windows does
 * not have it, and interprets _IOLBF as _IOFBF (bozos).  So use _IONBF
 * instead on Windows.
 */
#ifdef WIN32
#define LBF_MODE	_IONBF
#else
#define LBF_MODE	_IOLBF
#endif


58
/*
B
Bruce Momjian 已提交
59
 * GUC parameters.	Redirect_stderr cannot be changed after postmaster
60 61 62
 * start, but the rest can change at SIGHUP.
 */
bool		Redirect_stderr = false;
B
Bruce Momjian 已提交
63
int			Log_RotationAge = HOURS_PER_DAY * MINS_PER_HOUR;
B
Bruce Momjian 已提交
64
int			Log_RotationSize = 10 * 1024;
65 66 67
char	   *Log_directory = NULL;
char	   *Log_filename = NULL;
bool		Log_truncate_on_rotation = false;
68 69 70 71

/*
 * Globally visible state (used by elog.c)
 */
B
Bruce Momjian 已提交
72
bool		am_syslogger = false;
73 74 75 76

/*
 * Private state
 */
77
static pg_time_t next_rotation_time;
78

B
Bruce Momjian 已提交
79
static bool redirection_done = false;
80

B
Bruce Momjian 已提交
81
static bool pipe_eof_seen = false;
82 83 84

static FILE *syslogFile = NULL;

85 86
static char *last_file_name = NULL;

87 88
/* These must be exported for EXEC_BACKEND case ... annoying */
#ifndef WIN32
B
Bruce Momjian 已提交
89
int			syslogPipe[2] = {-1, -1};
90
#else
B
Bruce Momjian 已提交
91
HANDLE		syslogPipe[2] = {0, 0};
92 93 94
#endif

#ifdef WIN32
B
Bruce Momjian 已提交
95
static HANDLE threadHandle = 0;
96 97 98 99 100 101 102
static CRITICAL_SECTION sysfileSection;
#endif

/*
 * Flags set by interrupt handlers for later service in the main loop.
 */
static volatile sig_atomic_t got_SIGHUP = false;
103
static volatile sig_atomic_t rotation_requested = false;
104 105 106 107 108 109 110


/* Local subroutines */
#ifdef EXEC_BACKEND
static pid_t syslogger_forkexec(void);
static void syslogger_parseArgs(int argc, char *argv[]);
#endif
111
static void write_syslogger_file_binary(const char *buffer, int count);
B
Bruce Momjian 已提交
112

113 114 115
#ifdef WIN32
static unsigned int __stdcall pipeThread(void *arg);
#endif
116
static void logfile_rotate(bool time_based_rotation);
B
Bruce Momjian 已提交
117
static char *logfile_getname(pg_time_t timestamp);
118
static void set_next_rotation_time(void);
119
static void sigHupHandler(SIGNAL_ARGS);
120
static void sigUsr1Handler(SIGNAL_ARGS);
121 122 123 124 125 126 127 128 129


/*
 * Main entry point for syslogger process
 * argc/argv parameters are valid only in EXEC_BACKEND case.
 */
NON_EXEC_STATIC void
SysLoggerMain(int argc, char *argv[])
{
130 131 132
	char	   *currentLogDir;
	char	   *currentLogFilename;
	int			currentLogRotationAge;
133 134 135 136 137 138 139

	IsUnderPostmaster = true;	/* we are a postmaster subprocess now */

	MyProcPid = getpid();		/* reset MyProcPid */

#ifdef EXEC_BACKEND
	syslogger_parseArgs(argc, argv);
B
Bruce Momjian 已提交
140
#endif   /* EXEC_BACKEND */
141 142 143

	am_syslogger = true;

144
	init_ps_display("logger process", "", "", "");
145 146

	/*
B
Bruce Momjian 已提交
147 148 149 150 151
	 * If we restarted, our stderr is already redirected into our own input
	 * pipe.  This is of course pretty useless, not to mention that it
	 * interferes with detecting pipe EOF.	Point stderr to /dev/null. This
	 * assumes that all interesting messages generated in the syslogger will
	 * come through elog.c and will be sent to write_syslogger_file.
152 153 154
	 */
	if (redirection_done)
	{
155
		int			fd = open(NULL_DEV, O_WRONLY, 0);
156

157
		/*
B
Bruce Momjian 已提交
158 159 160 161
		 * The closes might look redundant, but they are not: we want to be
		 * darn sure the pipe gets closed even if the open failed.	We can
		 * survive running with stderr pointing nowhere, but we can't afford
		 * to have extra pipe input descriptors hanging around.
162 163 164 165 166 167
		 */
		close(fileno(stdout));
		close(fileno(stderr));
		dup2(fd, fileno(stdout));
		dup2(fd, fileno(stderr));
		close(fd);
168 169 170
	}

	/*
B
Bruce Momjian 已提交
171 172 173
	 * Also close our copy of the write end of the pipe.  This is needed to
	 * ensure we can detect pipe EOF correctly.  (But note that in the restart
	 * case, the postmaster already did this.)
174 175 176 177 178 179 180 181 182 183 184 185 186 187
	 */
#ifndef WIN32
	if (syslogPipe[1] >= 0)
		close(syslogPipe[1]);
	syslogPipe[1] = -1;
#else
	if (syslogPipe[1])
		CloseHandle(syslogPipe[1]);
	syslogPipe[1] = 0;
#endif

	/*
	 * Properly accept or ignore signals the postmaster might send us
	 *
B
Bruce Momjian 已提交
188 189 190
	 * Note: we ignore all termination signals, and instead exit only when all
	 * upstream processes are gone, to ensure we don't miss any dying gasps of
	 * broken backends...
191 192 193
	 */

	pqsignal(SIGHUP, sigHupHandler);	/* set flag to read config file */
B
Bruce Momjian 已提交
194
	pqsignal(SIGINT, SIG_IGN);
195 196 197 198
	pqsignal(SIGTERM, SIG_IGN);
	pqsignal(SIGQUIT, SIG_IGN);
	pqsignal(SIGALRM, SIG_IGN);
	pqsignal(SIGPIPE, SIG_IGN);
B
Bruce Momjian 已提交
199
	pqsignal(SIGUSR1, sigUsr1Handler);	/* request log rotation */
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	pqsignal(SIGUSR2, SIG_IGN);

	/*
	 * Reset some signals that are accepted by postmaster but not here
	 */
	pqsignal(SIGCHLD, SIG_DFL);
	pqsignal(SIGTTIN, SIG_DFL);
	pqsignal(SIGTTOU, SIG_DFL);
	pqsignal(SIGCONT, SIG_DFL);
	pqsignal(SIGWINCH, SIG_DFL);

	PG_SETMASK(&UnBlockSig);

#ifdef WIN32
	/* Fire up separate data transfer thread */
	InitializeCriticalSection(&sysfileSection);

	{
		unsigned int tid;

B
Bruce Momjian 已提交
220
		threadHandle = (HANDLE) _beginthreadex(0, 0, pipeThread, 0, 0, &tid);
221
	}
B
Bruce Momjian 已提交
222
#endif   /* WIN32 */
223

224 225 226 227 228 229
	/* remember active logfile parameters */
	currentLogDir = pstrdup(Log_directory);
	currentLogFilename = pstrdup(Log_filename);
	currentLogRotationAge = Log_RotationAge;
	/* set next planned rotation time */
	set_next_rotation_time();
230 231 232 233

	/* main worker loop */
	for (;;)
	{
234
		bool		time_based_rotation = false;
B
Bruce Momjian 已提交
235

236
#ifndef WIN32
B
Bruce Momjian 已提交
237 238 239
		char		logbuffer[1024];
		int			bytesRead;
		int			rc;
240 241 242 243 244 245 246 247 248 249
		fd_set		rfds;
		struct timeval timeout;
#endif

		if (got_SIGHUP)
		{
			got_SIGHUP = false;
			ProcessConfigFile(PGC_SIGHUP);

			/*
B
Bruce Momjian 已提交
250 251
			 * Check if the log directory or filename pattern changed in
			 * postgresql.conf. If so, force rotation to make sure we're
252
			 * writing the logfiles in the right place.
253
			 */
254
			if (strcmp(Log_directory, currentLogDir) != 0)
255
			{
256 257
				pfree(currentLogDir);
				currentLogDir = pstrdup(Log_directory);
258 259
				rotation_requested = true;
			}
260 261 262 263 264 265
			if (strcmp(Log_filename, currentLogFilename) != 0)
			{
				pfree(currentLogFilename);
				currentLogFilename = pstrdup(Log_filename);
				rotation_requested = true;
			}
B
Bruce Momjian 已提交
266

267 268 269 270 271 272 273 274 275
			/*
			 * If rotation time parameter changed, reset next rotation time,
			 * but don't immediately force a rotation.
			 */
			if (currentLogRotationAge != Log_RotationAge)
			{
				currentLogRotationAge = Log_RotationAge;
				set_next_rotation_time();
			}
276 277
		}

278
		if (!rotation_requested && Log_RotationAge > 0)
279
		{
280
			/* Do a logfile rotation if it's time */
B
Bruce Momjian 已提交
281
			pg_time_t	now = time(NULL);
282

283 284
			if (now >= next_rotation_time)
				rotation_requested = time_based_rotation = true;
285 286 287 288
		}

		if (!rotation_requested && Log_RotationSize > 0)
		{
289
			/* Do a rotation if file is too big */
290 291 292 293 294
			if (ftell(syslogFile) >= Log_RotationSize * 1024L)
				rotation_requested = true;
		}

		if (rotation_requested)
295
			logfile_rotate(time_based_rotation);
296 297

#ifndef WIN32
B
Bruce Momjian 已提交
298

299 300 301 302 303
		/*
		 * Wait for some data, timing out after 1 second
		 */
		FD_ZERO(&rfds);
		FD_SET(syslogPipe[0], &rfds);
B
Bruce Momjian 已提交
304 305
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;
306

B
Bruce Momjian 已提交
307
		rc = select(syslogPipe[0] + 1, &rfds, NULL, NULL, &timeout);
308 309 310 311 312 313

		if (rc < 0)
		{
			if (errno != EINTR)
				ereport(LOG,
						(errcode_for_socket_access(),
B
Bruce Momjian 已提交
314
						 errmsg("select() failed in logger process: %m")));
315 316 317
		}
		else if (rc > 0 && FD_ISSET(syslogPipe[0], &rfds))
		{
B
Bruce Momjian 已提交
318
			bytesRead = piperead(syslogPipe[0],
319 320 321 322 323 324 325
								 logbuffer, sizeof(logbuffer));

			if (bytesRead < 0)
			{
				if (errno != EINTR)
					ereport(LOG,
							(errcode_for_socket_access(),
B
Bruce Momjian 已提交
326
							 errmsg("could not read from logger pipe: %m")));
327 328 329
			}
			else if (bytesRead > 0)
			{
330
				write_syslogger_file_binary(logbuffer, bytesRead);
331 332 333 334 335
				continue;
			}
			else
			{
				/*
B
Bruce Momjian 已提交
336 337 338 339
				 * Zero bytes read when select() is saying read-ready means
				 * EOF on the pipe: that is, there are no longer any processes
				 * with the pipe write end open.  Therefore, the postmaster
				 * and all backends are shut down, and we are done.
340 341 342 343
				 */
				pipe_eof_seen = true;
			}
		}
B
Bruce Momjian 已提交
344 345
#else							/* WIN32 */

346
		/*
B
Bruce Momjian 已提交
347 348 349
		 * On Windows we leave it to a separate thread to transfer data and
		 * detect pipe EOF.  The main thread just wakes up once a second to
		 * check for SIGHUP and rotation conditions.
350 351
		 */
		pgwin32_backend_usleep(1000000);
B
Bruce Momjian 已提交
352
#endif   /* WIN32 */
353 354 355 356 357

		if (pipe_eof_seen)
		{
			ereport(LOG,
					(errmsg("logger shutting down")));
B
Bruce Momjian 已提交
358

359
			/*
B
Bruce Momjian 已提交
360
			 * Normal exit from the syslogger is here.	Note that we
B
Bruce Momjian 已提交
361 362 363 364
			 * deliberately do not close syslogFile before exiting; this is to
			 * allow for the possibility of elog messages being generated
			 * inside proc_exit.  Regular exit() will take care of flushing
			 * and closing stdio channels.
365
			 */
366 367 368 369 370 371 372 373 374 375 376
			proc_exit(0);
		}
	}
}

/*
 * Postmaster subroutine to start a syslogger subprocess.
 */
int
SysLogger_Start(void)
{
B
Bruce Momjian 已提交
377 378
	pid_t		sysloggerPid;
	char	   *filename;
379 380

	if (!Redirect_stderr)
B
Bruce Momjian 已提交
381
		return 0;
382 383

	/*
B
Bruce Momjian 已提交
384 385
	 * If first time through, create the pipe which will receive stderr
	 * output.
386
	 *
387 388
	 * 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
B
Bruce Momjian 已提交
389
	 * into that pipe).
390
	 *
391 392 393
	 * 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.
394 395
	 */
#ifndef WIN32
B
Bruce Momjian 已提交
396
	if (syslogPipe[0] < 0)
397
	{
B
Bruce Momjian 已提交
398 399
		if (pgpipe(syslogPipe) < 0)
			ereport(FATAL,
400
					(errcode_for_socket_access(),
B
Bruce Momjian 已提交
401
					 (errmsg("could not create pipe for syslog: %m"))));
402 403
	}
#else
B
Bruce Momjian 已提交
404
	if (!syslogPipe[0])
405 406 407 408 409 410 411 412
	{
		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))
B
Bruce Momjian 已提交
413
			ereport(FATAL,
414
					(errcode_for_file_access(),
B
Bruce Momjian 已提交
415
					 (errmsg("could not create pipe for syslog: %m"))));
416 417 418 419
	}
#endif

	/*
420
	 * Create log directory if not present; ignore errors
421
	 */
422
	mkdir(Log_directory, 0700);
423 424

	/*
B
Bruce Momjian 已提交
425 426
	 * The initial logfile is created right in the postmaster, to verify that
	 * the Log_directory is writable.
427
	 */
428
	filename = logfile_getname(time(NULL));
429 430 431 432

	syslogFile = fopen(filename, "a");

	if (!syslogFile)
B
Bruce Momjian 已提交
433
		ereport(FATAL,
434
				(errcode_for_file_access(),
P
Peter Eisentraut 已提交
435
				 (errmsg("could not create log file \"%s\": %m",
436 437
						 filename))));

438
	setvbuf(syslogFile, NULL, LBF_MODE, 0);
439 440 441 442 443 444

	pfree(filename);

#ifdef EXEC_BACKEND
	switch ((sysloggerPid = syslogger_forkexec()))
#else
445
	switch ((sysloggerPid = fork_process()))
446 447 448 449 450 451 452 453 454 455 456 457 458
#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);

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

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
			/* 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
B
Bruce Momjian 已提交
491
				int			fd;
492

493
				fflush(stderr);
494 495 496
				fd = _open_osfhandle((long) syslogPipe[1],
									 _O_APPEND | _O_TEXT);
				if (dup2(fd, _fileno(stderr)) < 0)
497 498 499
					ereport(FATAL,
							(errcode_for_file_access(),
							 errmsg("could not redirect stderr: %m")));
500
				close(fd);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
				/* Now we are done with the write end of the pipe. */
				CloseHandle(syslogPipe[1]);
				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;
}


#ifdef EXEC_BACKEND

/*
 * syslogger_forkexec() -
 *
 * Format up the arglist for, then fork and exec, a syslogger process
 */
static pid_t
syslogger_forkexec(void)
{
B
Bruce Momjian 已提交
529 530 531 532 533
	char	   *av[10];
	int			ac = 0,
				bufc = 0,
				i;
	char		numbuf[2][32];
534 535

	av[ac++] = "postgres";
536
	av[ac++] = "--forklog";
537 538 539 540 541
	av[ac++] = NULL;			/* filled in by postmaster_forkexec */

	/* static variables (those not passed by write_backend_variables) */
#ifndef WIN32
	if (syslogFile != NULL)
B
Bruce Momjian 已提交
542 543 544
		snprintf(numbuf[bufc++], 32, "%d", fileno(syslogFile));
	else
		strcpy(numbuf[bufc++], "-1");
545
	snprintf(numbuf[bufc++], 32, "%d", (int) redirection_done);
B
Bruce Momjian 已提交
546
#else							/* WIN32 */
547
	if (syslogFile != NULL)
B
Bruce Momjian 已提交
548
		snprintf(numbuf[bufc++], 32, "%ld",
549
				 _get_osfhandle(_fileno(syslogFile)));
B
Bruce Momjian 已提交
550 551
	else
		strcpy(numbuf[bufc++], "0");
552
	snprintf(numbuf[bufc++], 32, "%d", (int) redirection_done);
B
Bruce Momjian 已提交
553
#endif   /* WIN32 */
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

	/* Add to the arg list */
	Assert(bufc <= lengthof(numbuf));
	for (i = 0; i < bufc; i++)
		av[ac++] = numbuf[i];

	av[ac] = NULL;
	Assert(ac < lengthof(av));

	return postmaster_forkexec(ac, av);
}

/*
 * syslogger_parseArgs() -
 *
 * Extract data from the arglist for exec'ed syslogger process
 */
static void
syslogger_parseArgs(int argc, char *argv[])
{
B
Bruce Momjian 已提交
574
	int			fd;
575 576 577 578 579 580 581 582

	Assert(argc == 5);
	argv += 3;

#ifndef WIN32
	fd = atoi(*argv++);
	if (fd != -1)
	{
B
Bruce Momjian 已提交
583
		syslogFile = fdopen(fd, "a");
584
		setvbuf(syslogFile, NULL, LBF_MODE, 0);
585 586
	}
	redirection_done = (bool) atoi(*argv++);
B
Bruce Momjian 已提交
587
#else							/* WIN32 */
588 589 590 591
	fd = atoi(*argv++);
	if (fd != 0)
	{
		fd = _open_osfhandle(fd, _O_APPEND);
592
		if (fd > 0)
593 594
		{
			syslogFile = fdopen(fd, "a");
595
			setvbuf(syslogFile, NULL, LBF_MODE, 0);
596 597 598
		}
	}
	redirection_done = (bool) atoi(*argv++);
B
Bruce Momjian 已提交
599
#endif   /* WIN32 */
600
}
B
Bruce Momjian 已提交
601
#endif   /* EXEC_BACKEND */
602 603 604 605 606 607 608 609


/* --------------------------------
 *		logfile routines
 * --------------------------------
 */

/*
610
 * Write text to the currently open logfile
611 612 613 614 615 616 617
 *
 * This is exported so that elog.c can call it when am_syslogger is true.
 * This allows the syslogger process to record elog messages of its own,
 * even though its stderr does not point at the syslog pipe.
 */
void
write_syslogger_file(const char *buffer, int count)
618 619
{
#ifdef WIN32
B
Bruce Momjian 已提交
620

621 622 623
	/*
	 * On Windows we need to do our own newline-to-CRLF translation.
	 */
B
Bruce Momjian 已提交
624 625 626
	char		convbuf[256];
	char	   *p;
	int			n;
627 628 629 630 631

	p = convbuf;
	n = 0;
	while (count-- > 0)
	{
B
Bruce Momjian 已提交
632 633 634
		if (*buffer == '\n')
		{
			*p++ = '\r';
635
			n++;
B
Bruce Momjian 已提交
636 637
		}
		*p++ = *buffer++;
638 639 640 641 642 643 644
		n++;
		if (n >= sizeof(convbuf) - 1)
		{
			write_syslogger_file_binary(convbuf, n);
			p = convbuf;
			n = 0;
		}
B
Bruce Momjian 已提交
645
	}
646 647
	if (n > 0)
		write_syslogger_file_binary(convbuf, n);
B
Bruce Momjian 已提交
648
#else							/* !WIN32 */
649 650 651 652 653 654 655 656 657 658 659 660
	write_syslogger_file_binary(buffer, count);
#endif
}

/*
 * Write binary data to the currently open logfile
 *
 * On Windows the data arriving in the pipe already has CR/LF newlines,
 * so we must send it to the file without further translation.
 */
static void
write_syslogger_file_binary(const char *buffer, int count)
661
{
B
Bruce Momjian 已提交
662
	int			rc;
663 664

#ifndef WIN32
B
Bruce Momjian 已提交
665
	rc = fwrite(buffer, 1, count, syslogFile);
666
#else
B
Bruce Momjian 已提交
667 668 669
	EnterCriticalSection(&sysfileSection);
	rc = fwrite(buffer, 1, count, syslogFile);
	LeaveCriticalSection(&sysfileSection);
670 671
#endif

672
	/* can't use ereport here because of possible recursion */
B
Bruce Momjian 已提交
673
	if (rc != count)
674
		write_stderr("could not write to log file: %s\n", strerror(errno));
675 676 677 678 679 680 681 682 683 684 685 686 687 688
}

#ifdef WIN32

/*
 * Worker thread to transfer data from the pipe to the current logfile.
 *
 * We need this because on Windows, WaitForSingleObject does not work on
 * unnamed pipes: it always reports "signaled", so the blocking ReadFile won't
 * allow for SIGHUP; and select is for sockets only.
 */
static unsigned int __stdcall
pipeThread(void *arg)
{
B
Bruce Momjian 已提交
689 690
	DWORD		bytesRead;
	char		logbuffer[1024];
691

B
Bruce Momjian 已提交
692 693 694
	for (;;)
	{
		if (!ReadFile(syslogPipe[0], logbuffer, sizeof(logbuffer),
695 696
					  &bytesRead, 0))
		{
B
Bruce Momjian 已提交
697
			DWORD		error = GetLastError();
698

699 700
			if (error == ERROR_HANDLE_EOF ||
				error == ERROR_BROKEN_PIPE)
701
				break;
702
			_dosmaperr(error);
703 704 705 706
			ereport(LOG,
					(errcode_for_file_access(),
					 errmsg("could not read from logger pipe: %m")));
		}
B
Bruce Momjian 已提交
707 708 709
		else if (bytesRead > 0)
			write_syslogger_file_binary(logbuffer, bytesRead);
	}
710 711 712

	/* We exit the above loop only upon detecting pipe EOF */
	pipe_eof_seen = true;
B
Bruce Momjian 已提交
713 714
	_endthread();
	return 0;
715
}
B
Bruce Momjian 已提交
716
#endif   /* WIN32 */
717 718 719 720 721

/*
 * perform logfile rotation
 */
static void
722
logfile_rotate(bool time_based_rotation)
723
{
B
Bruce Momjian 已提交
724 725
	char	   *filename;
	FILE	   *fh;
726

727 728
	rotation_requested = false;

729
	/*
B
Bruce Momjian 已提交
730 731 732
	 * When doing a time-based rotation, invent the new logfile name based on
	 * the planned rotation time, not current time, to avoid "slippage" in the
	 * file name when we don't do the rotation immediately.
733 734 735 736 737 738
	 */
	if (time_based_rotation)
		filename = logfile_getname(next_rotation_time);
	else
		filename = logfile_getname(time(NULL));

739 740 741
	/*
	 * Decide whether to overwrite or append.  We can overwrite if (a)
	 * Log_truncate_on_rotation is set, (b) the rotation was triggered by
B
Bruce Momjian 已提交
742 743
	 * elapsed time and not something else, and (c) the computed file name is
	 * different from what we were previously logging into.
744 745 746
	 *
	 * Note: during the first rotation after forking off from the postmaster,
	 * last_file_name will be NULL.  (We don't bother to set it in the
B
Bruce Momjian 已提交
747 748 749
	 * postmaster because it ain't gonna work in the EXEC_BACKEND case.) So we
	 * will always append in that situation, even though truncating would
	 * usually be safe.
750 751 752
	 */
	if (Log_truncate_on_rotation && time_based_rotation &&
		last_file_name != NULL && strcmp(filename, last_file_name) != 0)
753 754 755
		fh = fopen(filename, "w");
	else
		fh = fopen(filename, "a");
756 757 758

	if (!fh)
	{
B
Bruce Momjian 已提交
759
		int			saveerrno = errno;
760 761 762

		ereport(LOG,
				(errcode_for_file_access(),
P
Peter Eisentraut 已提交
763
				 errmsg("could not open new log file \"%s\": %m",
764 765 766
						filename)));

		/*
B
Bruce Momjian 已提交
767 768 769 770
		 * ENFILE/EMFILE are not too surprising on a busy system; just keep
		 * using the old file till we manage to get a new one. Otherwise,
		 * assume something's wrong with Log_directory and stop trying to
		 * create files.
771 772 773 774
		 */
		if (saveerrno != ENFILE && saveerrno != EMFILE)
		{
			ereport(LOG,
B
Bruce Momjian 已提交
775
					(errmsg("disabling automatic rotation (use SIGHUP to reenable)")));
776 777 778 779 780 781 782
			Log_RotationAge = 0;
			Log_RotationSize = 0;
		}
		pfree(filename);
		return;
	}

783
	setvbuf(fh, NULL, LBF_MODE, 0);
784 785 786 787 788 789 790 791 792 793 794

	/* On Windows, need to interlock against data-transfer thread */
#ifdef WIN32
	EnterCriticalSection(&sysfileSection);
#endif
	fclose(syslogFile);
	syslogFile = fh;
#ifdef WIN32
	LeaveCriticalSection(&sysfileSection);
#endif

795
	set_next_rotation_time();
796

797 798 799 800
	/* instead of pfree'ing filename, remember it for next time */
	if (last_file_name != NULL)
		pfree(last_file_name);
	last_file_name = filename;
801 802 803 804 805 806 807 808
}


/*
 * construct logfile name using timestamp information
 *
 * Result is palloc'd.
 */
B
Bruce Momjian 已提交
809
static char *
810 811
logfile_getname(pg_time_t timestamp)
{
B
Bruce Momjian 已提交
812
	char	   *filename;
813 814
	int			len;
	struct pg_tm *tm;
815 816 817

	filename = palloc(MAXPGPATH);

818
	snprintf(filename, MAXPGPATH, "%s/", Log_directory);
819 820 821 822 823 824

	len = strlen(filename);

	if (strchr(Log_filename, '%'))
	{
		/* treat it as a strftime pattern */
825
		tm = pg_localtime(&timestamp, global_timezone);
826 827
		pg_strftime(filename + len, MAXPGPATH - len, Log_filename, tm);
	}
B
Bruce Momjian 已提交
828
	else
829 830 831 832 833
	{
		/* no strftime escapes, so append timestamp to new filename */
		snprintf(filename + len, MAXPGPATH - len, "%s.%lu",
				 Log_filename, (unsigned long) timestamp);
	}
834 835 836 837

	return filename;
}

838 839 840 841 842 843 844
/*
 * Determine the next planned rotation time, and store in next_rotation_time.
 */
static void
set_next_rotation_time(void)
{
	pg_time_t	now;
845
	struct pg_tm *tm;
846 847 848 849 850 851 852 853 854
	int			rotinterval;

	/* nothing to do if time-based rotation is disabled */
	if (Log_RotationAge <= 0)
		return;

	/*
	 * The requirements here are to choose the next time > now that is a
	 * "multiple" of the log rotation interval.  "Multiple" can be interpreted
B
Bruce Momjian 已提交
855
	 * fairly loosely.	In this version we align to local time rather than
856
	 * GMT.
857
	 */
B
Bruce Momjian 已提交
858
	rotinterval = Log_RotationAge * SECS_PER_MINUTE;	/* convert to seconds */
859
	now = time(NULL);
860
	tm = pg_localtime(&now, global_timezone);
861
	now += tm->tm_gmtoff;
862 863
	now -= now % rotinterval;
	now += rotinterval;
864
	now -= tm->tm_gmtoff;
865 866 867
	next_rotation_time = now;
}

868 869 870 871 872 873 874 875 876
/* --------------------------------
 *		signal handler routines
 * --------------------------------
 */

/* SIGHUP: set flag to reload config file */
static void
sigHupHandler(SIGNAL_ARGS)
{
B
Bruce Momjian 已提交
877
	got_SIGHUP = true;
878
}
879 880 881 882 883 884 885

/* SIGUSR1: set flag to rotate logfile */
static void
sigUsr1Handler(SIGNAL_ARGS)
{
	rotation_requested = true;
}