/* Local subroutines */
#ifdef EXEC_BACKEND
+static int syslogger_fdget(FILE *file);
+static FILE *syslogger_fdopen(int fd);
static pid_t syslogger_forkexec(void);
static void syslogger_parseArgs(int argc, char *argv[]);
#endif
#ifdef EXEC_BACKEND
+/*
+ * syslogger_fdget() -
+ *
+ * Utility wrapper to grab the file descriptor of an opened error output
+ * file. Used when building the command to fork the logging collector.
+ */
+static int
+syslogger_fdget(FILE *file)
+{
+#ifndef WIN32
+ if (file != NULL)
+ return fileno(file);
+ else
+ return -1;
+#else
+ if (file != NULL)
+ return (int) _get_osfhandle(_fileno(file));
+ else
+ return 0;
+#endif /* WIN32 */
+}
+
+/*
+ * syslogger_fdopen() -
+ *
+ * Utility wrapper to re-open an error output file, using the given file
+ * descriptor. Used when parsing arguments in a forked logging collector.
+ */
+static FILE *
+syslogger_fdopen(int fd)
+{
+ FILE *file = NULL;
+
+#ifndef WIN32
+ if (fd != -1)
+ {
+ file = fdopen(fd, "a");
+ setvbuf(file, NULL, PG_IOLBF, 0);
+ }
+#else /* WIN32 */
+ if (fd != 0)
+ {
+ fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
+ if (fd > 0)
+ {
+ file = fdopen(fd, "a");
+ setvbuf(file, NULL, PG_IOLBF, 0);
+ }
+ }
+#endif /* WIN32 */
+
+ return file;
+}
+
/*
* syslogger_forkexec() -
*
av[ac++] = NULL; /* filled in by postmaster_forkexec */
/* static variables (those not passed by write_backend_variables) */
-#ifndef WIN32
- if (syslogFile != NULL)
- snprintf(filenobuf, sizeof(filenobuf), "%d",
- fileno(syslogFile));
- else
- strcpy(filenobuf, "-1");
-#else /* WIN32 */
- if (syslogFile != NULL)
- snprintf(filenobuf, sizeof(filenobuf), "%ld",
- (long) _get_osfhandle(_fileno(syslogFile)));
- else
- strcpy(filenobuf, "0");
-#endif /* WIN32 */
+ snprintf(filenobuf, sizeof(filenobuf), "%d",
+ syslogger_fdget(syslogFile));
av[ac++] = filenobuf;
-
-#ifndef WIN32
- if (csvlogFile != NULL)
- snprintf(csvfilenobuf, sizeof(csvfilenobuf), "%d",
- fileno(csvlogFile));
- else
- strcpy(csvfilenobuf, "-1");
-#else /* WIN32 */
- if (csvlogFile != NULL)
- snprintf(csvfilenobuf, sizeof(csvfilenobuf), "%ld",
- (long) _get_osfhandle(_fileno(csvlogFile)));
- else
- strcpy(csvfilenobuf, "0");
-#endif /* WIN32 */
+ snprintf(csvfilenobuf, sizeof(csvfilenobuf), "%d",
+ syslogger_fdget(csvlogFile));
av[ac++] = csvfilenobuf;
av[ac] = NULL;
* fails there's not a lot we can do to report the problem anyway. As
* coded, we'll just crash on a null pointer dereference after failure...
*/
-#ifndef WIN32
- fd = atoi(*argv++);
- if (fd != -1)
- {
- syslogFile = fdopen(fd, "a");
- setvbuf(syslogFile, NULL, PG_IOLBF, 0);
- }
- fd = atoi(*argv++);
- if (fd != -1)
- {
- csvlogFile = fdopen(fd, "a");
- setvbuf(csvlogFile, NULL, PG_IOLBF, 0);
- }
-#else /* WIN32 */
fd = atoi(*argv++);
- if (fd != 0)
- {
- fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
- if (fd > 0)
- {
- syslogFile = fdopen(fd, "a");
- setvbuf(syslogFile, NULL, PG_IOLBF, 0);
- }
- }
+ syslogFile = syslogger_fdopen(fd);
fd = atoi(*argv++);
- if (fd != 0)
- {
- fd = _open_osfhandle(fd, _O_APPEND | _O_TEXT);
- if (fd > 0)
- {
- csvlogFile = fdopen(fd, "a");
- setvbuf(csvlogFile, NULL, PG_IOLBF, 0);
- }
- }
-#endif /* WIN32 */
+ csvlogFile = syslogger_fdopen(fd);
}
#endif /* EXEC_BACKEND */