From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Michael Paquier <michael.paquier@gmail.com>
Cc: Egon Kocjan <ekocjan@gmail.com>
Cc: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
Date: Wed, 6 Jan 2016 18:42:48 -0300
Message-ID: <20160106214248.GA387954@alvherre.pgsql> (raw)
In-Reply-To: <CAB7nPqQ+CybqD8-MemC0qQrUwC3eCOTGqpmTSKfSvBm6GdBsjg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Michael Paquier wrote:
> On Mon, Aug 31, 2015 at 11:56 PM, Egon Kocjan wrote:
> Thanks for the additional tests, that's still a win so this patch is a
> good thing. Relying on isatty is definitely something that we should
> treat as a bug here and backpatch. On master and REL9_5_STABLE, what I
> sent previously, with the call of pgwin32_is_service fixed in pg_ctl.c
> is fine. For back branches, we could simply copy pgwin32_is_service in
> pg_ctl.c and rely on that. It seems like the safer approach. Thoughts
> from other hackers on the matter?
I looked at applying this patch, but as it turns out we don't have the
backpatchable version. I don't think it's a good idea to backpatch the
move of the file from backend to src/port -- there's precedent for
copying the entire function (and assorted infrastructure) to pg_ctl
instead. Would you submit a patch to do that?
Here's a rebased version of what you submitted last. I tweaked a few
lines here and there, nothing major. I thought your patch is ready to
apply and we only need the backpatchable versions.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:win32_service.patch (text/x-diff, 7.8K) downloaddiff --git a/src/backend/port/win32/Makefile b/src/backend/port/win32/Makefileindex 820a3b3..a6ace93 100644--- a/src/backend/port/win32/Makefile+++ b/src/backend/port/win32/Makefile@@ -12,7 +12,7 @@ subdir = src/backend/port/win32
top_builddir = ../../../..
include $(top_builddir)/src/Makefile.global
-OBJS = timer.o socket.o signal.o security.o mingwcompat.o+OBJS = timer.o socket.o signal.o mingwcompat.o
ifeq ($(have_win32_dbghelp), yes)
OBJS += crashdump.o
endif
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.cindex bd82a05..28d3cf2 100644--- a/src/bin/pg_ctl/pg_ctl.c+++ b/src/bin/pg_ctl/pg_ctl.c@@ -216,7 +216,7 @@ write_stderr(const char *fmt,...)
* On Win32, we print to stderr if running on a console, or write to
* eventlog if running as a service
*/
- if (!isatty(fileno(stderr))) /* Running as a service */+ if (!pgwin32_is_service()) /* Running as a service */
{
char errbuf[2048]; /* Arbitrary size? */
diff --git a/src/include/port/win32.h b/src/include/port/win32.hindex 4cb51ec..69cd1c4 100644--- a/src/include/port/win32.h+++ b/src/include/port/win32.h@@ -382,9 +382,6 @@ int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
extern int pgwin32_noblock;
-/* in backend/port/win32/security.c */-extern int pgwin32_is_admin(void);-extern int pgwin32_is_service(void);
#endif
/* in backend/port/win32_shmem.c */
@@ -400,6 +397,10 @@ extern void _dosmaperr(unsigned long);
extern int pgwin32_putenv(const char *);
extern void pgwin32_unsetenv(const char *);
+/* in port/win32security.c */+extern int pgwin32_is_service(void);+extern int pgwin32_is_admin(void);+
#define putenv(x) pgwin32_putenv(x)
#define unsetenv(x) pgwin32_unsetenv(x)
diff --git a/src/backend/port/win32/security.c b/src/port/win32security.csimilarity index 71%rename from src/backend/port/win32/security.crename to src/port/win32security.cindex 327ed40..cb40dda 100644--- a/src/backend/port/win32/security.c+++ b/src/port/win32security.c@@ -1,22 +1,47 @@
/*-------------------------------------------------------------------------
*
- * security.c+ * win32security.c
* Microsoft Windows Win32 Security Support Functions
*
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * src/backend/port/win32/security.c+ * src/port/win32security.c
*
*-------------------------------------------------------------------------
*/
+#ifndef FRONTEND
#include "postgres.h"
+#else+#include "postgres_fe.h"+#endif
static BOOL pgwin32_get_dynamic_tokeninfo(HANDLE token,
- TOKEN_INFORMATION_CLASS class, char **InfoBuffer,- char *errbuf, int errsize);+ TOKEN_INFORMATION_CLASS class,+ char **InfoBuffer, char *errbuf, int errsize);+++/*+ * Utility wrapper for frontend and backend when reporting an error+ * message.+ */+static+pg_attribute_printf(1, 2)+void+log_error(const char *fmt,...)+{+ va_list ap;++ va_start(fmt, ap);+#ifndef FRONTEND+ write_stderr(fmt, ap);+#else+ fprintf(stderr, fmt, ap);+#endif+ va_end(ap);+}
/*
* Returns nonzero if the current user has administrative privileges,
@@ -40,15 +65,15 @@ pgwin32_is_admin(void)
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken))
{
- write_stderr("could not open process token: error code %lu\n",- GetLastError());+ log_error("could not open process token: error code %lu\n",+ GetLastError());
exit(1);
}
if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenGroups,
&InfoBuffer, errbuf, sizeof(errbuf)))
{
- write_stderr("%s", errbuf);+ log_error("%s", errbuf);
exit(1);
}
@@ -57,20 +82,22 @@ pgwin32_is_admin(void)
CloseHandle(AccessToken);
if (!AllocateAndInitializeSid(&NtAuthority, 2,
- SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,+ SECURITY_BUILTIN_DOMAIN_RID,+ DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
0, &AdministratorsSid))
{
- write_stderr("could not get SID for Administrators group: error code %lu\n",- GetLastError());+ log_error("could not get SID for Administrators group: error code %lu\n",+ GetLastError());
exit(1);
}
if (!AllocateAndInitializeSid(&NtAuthority, 2,
- SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,+ SECURITY_BUILTIN_DOMAIN_RID,+ DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
0, &PowerUsersSid))
{
- write_stderr("could not get SID for PowerUsers group: error code %lu\n",- GetLastError());+ log_error("could not get SID for PowerUsers group: error code %lu\n",+ GetLastError());
exit(1);
}
@@ -78,8 +105,10 @@ pgwin32_is_admin(void)
for (x = 0; x < Groups->GroupCount; x++)
{
- if ((EqualSid(AdministratorsSid, Groups->Groups[x].Sid) && (Groups->Groups[x].Attributes & SE_GROUP_ENABLED)) ||- (EqualSid(PowerUsersSid, Groups->Groups[x].Sid) && (Groups->Groups[x].Attributes & SE_GROUP_ENABLED)))+ if ((EqualSid(AdministratorsSid, Groups->Groups[x].Sid) &&+ (Groups->Groups[x].Attributes & SE_GROUP_ENABLED)) ||+ (EqualSid(PowerUsersSid, Groups->Groups[x].Sid) &&+ (Groups->Groups[x].Attributes & SE_GROUP_ENABLED)))
{
success = TRUE;
break;
@@ -105,9 +134,11 @@ pgwin32_is_admin(void)
* 1 = Service
* -1 = Error
*
- * Note: we can't report errors via either ereport (we're called too early)- * or write_stderr (because that calls this). We are therefore reduced to- * writing directly on stderr, which sucks, but we have few alternatives.+ * Note: we can't report errors via either ereport (we're called too early+ * in the backend) or write_stderr (because that calls this). We are+ * therefore reduced to writing directly on stderr, which sucks, but we+ * have few alternatives. Backend-side code needs as well calls to write+ * directly to stderr, so that's not something to worry about in this case.
*/
int
pgwin32_is_service(void)
@@ -217,13 +248,15 @@ pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class,
if (GetTokenInformation(token, class, NULL, 0, &InfoBufferSize))
{
- snprintf(errbuf, errsize, "could not get token information: got zero size\n");+ snprintf(errbuf, errsize,+ "could not get token information: got zero size\n");
return FALSE;
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
- snprintf(errbuf, errsize, "could not get token information: error code %lu\n",+ snprintf(errbuf, errsize,+ "could not get token information: error code %lu\n",
GetLastError());
return FALSE;
}
@@ -231,7 +264,8 @@ pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class,
*InfoBuffer = malloc(InfoBufferSize);
if (*InfoBuffer == NULL)
{
- snprintf(errbuf, errsize, "could not allocate %d bytes for token information\n",+ snprintf(errbuf, errsize,+ "could not allocate %d bytes for token information\n",
(int) InfoBufferSize);
return FALSE;
}
@@ -239,7 +273,8 @@ pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class,
if (!GetTokenInformation(token, class, *InfoBuffer,
InfoBufferSize, &InfoBufferSize))
{
- snprintf(errbuf, errsize, "could not get token information: error code %lu\n",+ snprintf(errbuf, errsize,+ "could not get token information: error code %lu\n",
GetLastError());
return FALSE;
}
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pmindex c122f02..1dba7d9 100644--- a/src/tools/msvc/Mkvcbuild.pm+++ b/src/tools/msvc/Mkvcbuild.pm@@ -90,7 +90,7 @@ sub mkvcbuild
pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pqsignal.c
mkdtemp.c qsort.c qsort_arg.c quotes.c system.c
sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c
- win32env.c win32error.c win32setlocale.c);+ win32env.c win32error.c win32security.c win32setlocale.c);
push(@pgportfiles, 'rint.c') if ($vsVersion < '12.00');
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: alvherre@2ndquadrant.com, michael.paquier@gmail.com, ekocjan@gmail.com
Subject: Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
In-Reply-To: <20160106214248.GA387954@alvherre.pgsql>
References: <CAB7nPqQ+CybqD8-MemC0qQrUwC3eCOTGqpmTSKfSvBm6GdBsjg@mail.gmail.com> <20160106214248.GA387954@alvherre.pgsql>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox