Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aGvrH-00020a-75 for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Jan 2016 21:43:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aGvrG-00027S-96 for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Jan 2016 21:43:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aGvrF-00027L-Px for pgsql-bugs@postgresql.org; Wed, 06 Jan 2016 21:43:05 +0000 Received: from out.smtp-auth.no-ip.com ([8.23.224.60]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aGvr9-0000hd-Sl for pgsql-bugs@postgresql.org; Wed, 06 Jan 2016 21:43:04 +0000 X-No-IP: alvh.no-ip.org@noip-smtp X-Report-Spam-To: abuse@no-ip.com Received: from alvin.alvh.no-ip.org (unknown [186.9.64.89]) (Authenticated sender: alvh.no-ip.org@noip-smtp) by smtp-auth.no-ip.com (Postfix) with ESMTPA id 035B64009A2; Wed, 6 Jan 2016 13:42:52 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id B204F610; Wed, 6 Jan 2016 18:42:48 -0300 (CLT) Date: Wed, 6 Jan 2016 18:42:48 -0300 From: Alvaro Herrera To: Michael Paquier Cc: Egon Kocjan , PostgreSQL mailing lists Subject: Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Message-ID: <20160106214248.GA387954@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ikeVEW9yuYc//A+q" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -1.1 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --ikeVEW9yuYc//A+q Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 --ikeVEW9yuYc//A+q Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="win32_service.patch" diff --git a/src/backend/port/win32/Makefile b/src/backend/port/win32/Makefile index 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.c index 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.h index 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.c similarity index 71% rename from src/backend/port/win32/security.c rename to src/port/win32security.c index 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.pm index 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'); --ikeVEW9yuYc//A+q Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --ikeVEW9yuYc//A+q--