pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
20+ messages / 7 participants
[nested] [flat]

* BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
@ 2015-08-28 10:46 ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 20+ messages in thread

From: ekocjan@gmail.com @ 2015-08-28 10:46 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13594
Logged by:          Egon Kocjan
Email address:      ekocjan@gmail.com
PostgreSQL version: 9.5alpha2
Operating system:   Windows 8.1
Description:        

Hello

Context:

I run PostgreSQL as a part of a larger software package, pg_ctl is used to
control PostgreSQL. I capture all subprocess stderr logs to make a complete
report.

Problem:

On Windows (but not Unix!), PostgreSQL (any version) pg_ctl stops writing
into stderr if stderr is not a character device. As a result, it's not
possible to capture stderr of pg_ctl when using CreateProcess and
STARTUPINFO.hStdError. It might be possible to create a workaround with a
console buffer, but I think it's quite ugly:
http://www.codeproject.com/Articles/16163/Real-Time-Console-Output-Redirection

Solution:

Fix pg_ctl to write into stderr, if stderr is available and not just a
character device. Possibly, add a flag to force stderr. I don't have the big
picture, but this is the quick solution that I use right now:

diff -ur postgresql-9.3.6.orig\src\bin\pg_ctl\pg_ctl.c
postgresql-9.3.6\src\bin\pg_ctl\pg_ctl.c
inline patch 1: postgresql-9.3.6\src\bin\pg_ctl\pg_ctl.c (+1/-1 lines) download inline-1.patch--- postgresql-9.3.6.orig\src\bin\pg_ctl\pg_ctl.c Mon Feb 02 22:43:50 2015 +++ postgresql-9.3.6\src\bin\pg_ctl\pg_ctl.c Fri Aug 28 08:21:01 2015 @@ -215,7 +215,7 @@ * 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 (getenv("PG_CTL_STDERR") == NULL && !isatty(fileno(stderr))) /* Running
as a service */ { char errbuf[2048]; /* Arbitrary size? */ Then in my main process: _putenv("PG_CTL_STDERR=1") I also noticed, that write_stderr() in src/backend/utils/error/elog.c uses a different check (not isatty): if (pgwin32_is_service()) /* Running as a service */ Thank you -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ^ permalink raw reply [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
@ 2015-08-31 06:51 ` Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-03 11:36   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Andres Freund <andres@anarazel.de>
  0 siblings, 2 replies; 20+ messages in thread

From: Michael Paquier @ 2015-08-31 06:51 UTC (permalink / raw)
  To: ekocjan@gmail.com; +Cc: pgsql-bugs

On Fri, Aug 28, 2015 at 7:46 PM, <ekocjan@gmail.com> wrote:
>
> I run PostgreSQL as a part of a larger software package, pg_ctl is used to
> control PostgreSQL. I capture all subprocess stderr logs to make a complete
> report.
>
> Problem:
>
> On Windows (but not Unix!), PostgreSQL (any version) pg_ctl stops writing
> into stderr if stderr is not a character device. As a result, it's not
> possible to capture stderr of pg_ctl when using CreateProcess and
> STARTUPINFO.hStdError. It might be possible to create a workaround with a
> console buffer, but I think it's quite ugly:
> http://www.codeproject.com/Articles/16163/Real-Time-Console-Output-Redirection


Yep, that's ugly.

>
> Solution:
>
> Fix pg_ctl to write into stderr, if stderr is available and not just a
> character device. Possibly, add a flag to force stderr. I don't have the big
> picture, but this is the quick solution that I use right now:
>
> diff -ur postgresql-9.3.6.orig\src\bin\pg_ctl\pg_ctl.c
> postgresql-9.3.6\src\bin\pg_ctl\pg_ctl.c
> --- postgresql-9.3.6.orig\src\bin\pg_ctl\pg_ctl.c       Mon Feb 02 22:43:50 2015
> +++ postgresql-9.3.6\src\bin\pg_ctl\pg_ctl.c    Fri Aug 28 08:21:01 2015
> @@ -215,7 +215,7 @@
>          * 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 (getenv("PG_CTL_STDERR") == NULL && !isatty(fileno(stderr))) /* Running
> as a service */
>         {
>                 char            errbuf[2048];           /* Arbitrary size? */
>
>
> Then in my main process: _putenv("PG_CTL_STDERR=1")


Honestly I don't like that much.

>
> I also noticed, that write_stderr() in src/backend/utils/error/elog.c uses a
> different check (not isatty):
>
> if (pgwin32_is_service())       /* Running as a service */


The last time I poked at that, switching to use pgwin32_is_service was
not that straight-forward as it is a backend-only routine, relying on
write_stderr, write_eventlog and write_console, and pg_ctl has its own
concept of those routines:
http://www.postgresql.org/message-id/CAB7nPqSxWcUFpzL4LaOZAt4k-FPM7fy6Et7p50w8S5NbRfCwFw@mail.gmail....
But actually looking at it with fresh eyes it is not that complicated
if we switch backend/port/win32/security.c to port/win32security.c to
make pgwin32_is_service available as well for frontends. And it would
obviously still be an improvement to switch isatty() to
pgwin32_is_service() in pg_ctl.c, the trick being to remove the
dependency to write_stderr when pgwin32_is_admin is called from
frontend so as it does not interact badly with the existing logging
infrastructure of elog.c and the definition of write_stderr in
pg_ctl.c. Note that in any case we cannot rely on ereport when calling
pgwin32_is_service on backend because it is called very early at
process startup, so there is no need to dance with ifdef FRONTEND
there.

This change is too invasive so I think that making it HEAD-only is
better, and that's a change of behavior. I should perhaps have split
the patch into two: one for the move to src/port, the other for pg_ctl
but it's not really a big deal. I'll add it to the next CF I think
that's worth considering it.

Egon, would the patch attached help in your case?
Regards,
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

20150831_win32_service.patch (binary/octet-stream, 11.9K) download 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/backend/port/win32/security.c b/src/backend/port/win32/security.c index e9cfe15..e69de29 100644 --- a/src/backend/port/win32/security.c +++ b/src/backend/port/win32/security.c @@ -1,248 +0,0 @@ -/*------------------------------------------------------------------------- - * - * security.c - * Microsoft Windows Win32 Security Support Functions - * - * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group - * - * IDENTIFICATION - * src/backend/port/win32/security.c - * - *------------------------------------------------------------------------- - */ - -#include "postgres.h" - - -static BOOL pgwin32_get_dynamic_tokeninfo(HANDLE token, - TOKEN_INFORMATION_CLASS class, char **InfoBuffer, - char *errbuf, int errsize); - -/* - * Returns nonzero if the current user has administrative privileges, - * or zero if not. - * - * Note: this cannot use ereport() because it's called too early during - * startup. - */ -int -pgwin32_is_admin(void) -{ - HANDLE AccessToken; - char *InfoBuffer = NULL; - char errbuf[256]; - PTOKEN_GROUPS Groups; - PSID AdministratorsSid; - PSID PowerUsersSid; - SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY}; - UINT x; - BOOL success; - - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) - { - write_stderr("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); - exit(1); - } - - Groups = (PTOKEN_GROUPS) InfoBuffer; - - CloseHandle(AccessToken); - - if (!AllocateAndInitializeSid(&NtAuthority, 2, - 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()); - exit(1); - } - - if (!AllocateAndInitializeSid(&NtAuthority, 2, - 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()); - exit(1); - } - - success = FALSE; - - 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))) - { - success = TRUE; - break; - } - } - - free(InfoBuffer); - FreeSid(AdministratorsSid); - FreeSid(PowerUsersSid); - return success; -} - -/* - * We consider ourselves running as a service if one of the following is - * true: - * - * 1) We are running as Local System (only used by services) - * 2) Our token contains SECURITY_SERVICE_RID (automatically added to the - * process token by the SCM when starting a service) - * - * Return values: - * 0 = Not service - * 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. - */ -int -pgwin32_is_service(void) -{ - static int _is_service = -1; - HANDLE AccessToken; - char *InfoBuffer = NULL; - char errbuf[256]; - PTOKEN_GROUPS Groups; - PTOKEN_USER User; - PSID ServiceSid; - PSID LocalSystemSid; - SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY}; - UINT x; - - /* Only check the first time */ - if (_is_service != -1) - return _is_service; - - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) - { - fprintf(stderr, "could not open process token: error code %lu\n", - GetLastError()); - return -1; - } - - /* First check for local system */ - if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenUser, &InfoBuffer, - errbuf, sizeof(errbuf))) - { - fprintf(stderr, "%s", errbuf); - return -1; - } - - User = (PTOKEN_USER) InfoBuffer; - - if (!AllocateAndInitializeSid(&NtAuthority, 1, - SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, - &LocalSystemSid)) - { - fprintf(stderr, "could not get SID for local system account\n"); - CloseHandle(AccessToken); - return -1; - } - - if (EqualSid(LocalSystemSid, User->User.Sid)) - { - FreeSid(LocalSystemSid); - free(InfoBuffer); - CloseHandle(AccessToken); - _is_service = 1; - return _is_service; - } - - FreeSid(LocalSystemSid); - free(InfoBuffer); - - /* Now check for group SID */ - if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenGroups, &InfoBuffer, - errbuf, sizeof(errbuf))) - { - fprintf(stderr, "%s", errbuf); - return -1; - } - - Groups = (PTOKEN_GROUPS) InfoBuffer; - - if (!AllocateAndInitializeSid(&NtAuthority, 1, - SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, - &ServiceSid)) - { - fprintf(stderr, "could not get SID for service group\n"); - free(InfoBuffer); - CloseHandle(AccessToken); - return -1; - } - - _is_service = 0; - for (x = 0; x < Groups->GroupCount; x++) - { - if (EqualSid(ServiceSid, Groups->Groups[x].Sid)) - { - _is_service = 1; - break; - } - } - - free(InfoBuffer); - FreeSid(ServiceSid); - - CloseHandle(AccessToken); - - return _is_service; -} - - -/* - * Call GetTokenInformation() on a token and return a dynamically sized - * buffer with the information in it. This buffer must be free():d by - * the calling function! - */ -static BOOL -pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class, - char **InfoBuffer, char *errbuf, int errsize) -{ - DWORD InfoBufferSize; - - if (GetTokenInformation(token, class, NULL, 0, &InfoBufferSize)) - { - 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", - GetLastError()); - return FALSE; - } - - *InfoBuffer = malloc(InfoBufferSize); - if (*InfoBuffer == NULL) - { - snprintf(errbuf, errsize, "could not allocate %d bytes for token information\n", - (int) InfoBufferSize); - return FALSE; - } - - if (!GetTokenInformation(token, class, *InfoBuffer, - InfoBufferSize, &InfoBufferSize)) - { - snprintf(errbuf, errsize, "could not get token information: error code %lu\n", - GetLastError()); - return FALSE; - } - - return TRUE; -} diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 6a36d29..d9eca37 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -215,7 +215,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 85% copy from src/backend/port/win32/security.c copy to src/port/win32security.c index e9cfe15..a6d8287 100644 --- a/src/backend/port/win32/security.c +++ b/src/port/win32security.c @@ -1,22 +1,26 @@ /*------------------------------------------------------------------------- * - * security.c + * win32security.c * Microsoft Windows Win32 Security Support Functions * * Portions Copyright (c) 1996-2015, 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); /* * Returns nonzero if the current user has administrative privileges, @@ -40,15 +44,24 @@ pgwin32_is_admin(void) if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) { +#ifndef FRONTEND write_stderr("could not open process token: error code %lu\n", GetLastError()); +#else + fprintf(stderr, "could not open process token: error code %lu\n", + GetLastError()); +#endif exit(1); } if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenGroups, &InfoBuffer, errbuf, sizeof(errbuf))) { +#ifndef FRONTEND write_stderr("%s", errbuf); +#else + fprintf(stderr, "%s", errbuf); +#endif exit(1); } @@ -60,8 +73,14 @@ pgwin32_is_admin(void) SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsSid)) { +#ifndef FRONTEND write_stderr("could not get SID for Administrators group: error code %lu\n", GetLastError()); +#else + fprintf(stderr, + "could not get SID for Administrators group: error code %lu\n", + GetLastError()); +#endif exit(1); } @@ -69,8 +88,14 @@ pgwin32_is_admin(void) SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0, 0, &PowerUsersSid)) { +#ifndef FRONTEND write_stderr("could not get SID for PowerUsers group: error code %lu\n", GetLastError()); +#else + fprintf(stderr, + "could not get SID for PowerUsers group: error code %lu\n", + GetLastError()); +#endif exit(1); } @@ -92,6 +117,7 @@ pgwin32_is_admin(void) return success; } + /* * We consider ourselves running as a service if one of the following is * true: @@ -105,9 +131,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) diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 3abbb4c..224fdcd 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -89,7 +89,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');
^ permalink raw reply [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2015-08-31 14:56   ` Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  1 sibling, 1 reply; 20+ messages in thread

From: Egon Kocjan @ 2015-08-31 14:56 UTC (permalink / raw)
  To: pgsql-bugs

On 31.8.2015 8:51, Michael Paquier wrote:
> On Fri, Aug 28, 2015 at 7:46 PM, <ekocjan@gmail.com> wrote:
>> I also noticed, that write_stderr() in src/backend/utils/error/elog.c uses a
>> different check (not isatty):
>>
>> if (pgwin32_is_service())       /* Running as a service */
>
> The last time I poked at that, switching to use pgwin32_is_service was
> not that straight-forward as it is a backend-only routine, relying on
> write_stderr, write_eventlog and write_console, and pg_ctl has its own
> concept of those routines:
> http://www.postgresql.org/message-id/CAB7nPqSxWcUFpzL4LaOZAt4k-FPM7fy6Et7p50w8S5NbRfCwFw@mail.gmail....
> But actually looking at it with fresh eyes it is not that complicated
> if we switch backend/port/win32/security.c to port/win32security.c to
> make pgwin32_is_service available as well for frontends. And it would
> obviously still be an improvement to switch isatty() to
> pgwin32_is_service() in pg_ctl.c, the trick being to remove the
> dependency to write_stderr when pgwin32_is_admin is called from
> frontend so as it does not interact badly with the existing logging
> infrastructure of elog.c and the definition of write_stderr in
> pg_ctl.c. Note that in any case we cannot rely on ereport when calling
> pgwin32_is_service on backend because it is called very early at
> process startup, so there is no need to dance with ifdef FRONTEND
> there.
>
> This change is too invasive so I think that making it HEAD-only is
> better, and that's a change of behavior. I should perhaps have split
> the patch into two: one for the move to src/port, the other for pg_ctl
> but it's not really a big deal. I'll add it to the next CF I think
> that's worth considering it.
>
> Egon, would the patch attached help in your case?
>

I tested with these two changes:

diff -ur postgresql-9.6devel.orig\src\bin\pg_ctl\pg_ctl.c 
postgresql-9.6devel\src\bin\pg_ctl\pg_ctl.c
--- postgresql-9.6devel.orig\src\bin\pg_ctl\pg_ctl.c    Mon Aug 31 
13:16:51 2015
+++ postgresql-9.6devel\src\bin\pg_ctl\pg_ctl.c    Mon Aug 31 15:12:54 2015
@@ -215,7 +215,7 @@
       * On Win32, we print to stderr if running on a console, or write to
       * eventlog if running as a service
       */
-    if (!pgwin32_is_service())    /* Running as a service */
+    if (pgwin32_is_service())    /* Running as a service */
      {
          char        errbuf[2048];        /* Arbitrary size? */

@@ -2131,6 +2131,9 @@
      progname = get_progname(argv[0]);
      set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));
      start_time = time(NULL);
+
+    write_stderr("%s: test test\n",
+                     progname);

      /*
       * save argv[0] so do_start() can look for the postmaster if 
necessary. we


1) It now works ok for development under regular account.

2) However there is no output on stderr when pg_ctl is running as a 
subprocess of a service. So I would still have to patch pg_ctl if I want 
stderr. I haven't looked into elog.c yet, maybe stderr from there would 
be good to have as well? So far, pg_ctl output and pg_logs\* was enough 
for me to debug the installations. Technically, I could also monitor 
Windows event log and dump events on the fly into my report file. But it 
seems simpler to just patch PostgreSQL locally to force stderr and reuse 
all the logic from the Unix port...

Thank you for looking into this,
Egon



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
@ 2015-09-01 01:43     ` Michael Paquier <michael.paquier@gmail.com>
  2015-09-03 08:24       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 2 replies; 20+ messages in thread

From: Michael Paquier @ 2015-09-01 01:43 UTC (permalink / raw)
  To: Egon Kocjan <ekocjan@gmail.com>; +Cc: pgsql-bugs

On Mon, Aug 31, 2015 at 11:56 PM, Egon Kocjan wrote:
> On 31.8.2015 8:51, Michael Paquier wrote:
>> Egon, would the patch attached help in your case?
>>
>
> diff -ur postgresql-9.6devel.orig\src\bin\pg_ctl\pg_ctl.c
> postgresql-9.6devel\src\bin\pg_ctl\pg_ctl.c
> --- postgresql-9.6devel.orig\src\bin\pg_ctl\pg_ctl.c    Mon Aug 31 13:16:51
> 2015
> +++ postgresql-9.6devel\src\bin\pg_ctl\pg_ctl.c    Mon Aug 31 15:12:54 2015
> @@ -215,7 +215,7 @@
>       * On Win32, we print to stderr if running on a console, or write to
>       * eventlog if running as a service
>       */
> -    if (!pgwin32_is_service())    /* Running as a service */
> +    if (pgwin32_is_service())    /* Running as a service */
>      {
>          char        errbuf[2048];        /* Arbitrary size? */

This one is obviously a stupid mistake of my patch :)

> @@ -2131,6 +2131,9 @@
>      progname = get_progname(argv[0]);
>      set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));
>      start_time = time(NULL);
> +
> +    write_stderr("%s: test test\n",
> +                     progname);
>
>      /*
>       * save argv[0] so do_start() can look for the postmaster if necessary.
> we
>
>
> 1) It now works ok for development under regular account.

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?

> 2) However there is no output on stderr when pg_ctl is running as a
> subprocess of a service. So I would still have to patch pg_ctl if I want
> stderr. I haven't looked into elog.c yet, maybe stderr from there would be
> good to have as well? So far, pg_ctl output and pg_logs\* was enough for me
> to debug the installations. Technically, I could also monitor Windows event
> log and dump events on the fly into my report file. But it seems simpler to
> just patch PostgreSQL locally to force stderr and reuse all the logic from
> the Unix port...

I wished we had such a switch a couple of times and I have as well
some code paths that would benefit a redirection of those logs to
stderr instead of the event log. Still this sounds like a new feature
to me. Hence, what about for example adding a Windows-only option that
can be used to enforce where logs are redirected, let's say
--log-output with the following values:
- default, which is what we have now
- stderr, to enforce the output to stderr
- eventlog, to enforce redirection to the event logs.
Would you like to work on this patch?
Regards,
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2015-09-03 08:24       ` Egon Kocjan <ekocjan@gmail.com>
  1 sibling, 0 replies; 20+ messages in thread

From: Egon Kocjan @ 2015-09-03 08:24 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs

On 1.9.2015 3:43, Michael Paquier wrote:
> On Mon, Aug 31, 2015 at 11:56 PM, Egon Kocjan wrote:
>> 2) However there is no output on stderr when pg_ctl is running as a
>> subprocess of a service. So I would still have to patch pg_ctl if I want
>> stderr. I haven't looked into elog.c yet, maybe stderr from there would be
>> good to have as well? So far, pg_ctl output and pg_logs\* was enough for me
>> to debug the installations. Technically, I could also monitor Windows event
>> log and dump events on the fly into my report file. But it seems simpler to
>> just patch PostgreSQL locally to force stderr and reuse all the logic from
>> the Unix port...
> I wished we had such a switch a couple of times and I have as well
> some code paths that would benefit a redirection of those logs to
> stderr instead of the event log. Still this sounds like a new feature
> to me. Hence, what about for example adding a Windows-only option that
> can be used to enforce where logs are redirected, let's say
> --log-output with the following values:
> - default, which is what we have now
> - stderr, to enforce the output to stderr
> - eventlog, to enforce redirection to the event logs.
> Would you like to work on this patch?
>

Yes, I would start with pg_ctl itself. Then we can look into what else 
needs to be done for the backend, I'm not yet 100% sure how it all fits 
together.

Regards
Egon



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2016-01-06 21:42       ` Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  1 sibling, 1 reply; 20+ messages in thread

From: Alvaro Herrera @ 2016-01-06 21:42 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Egon Kocjan <ekocjan@gmail.com>; pgsql-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-bugs

Attachments:

win32_service.patch (text/x-diff, 7.8K) download 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');
^ permalink raw reply [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2016-01-07 04:13         ` Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Michael Paquier @ 2016-01-07 04:13 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

On Thu, Jan 7, 2016 at 6:42 AM, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:
> 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?

Yes (like restricted tokens and pg_upgrade stuff we did), but I was
just waiting that we reach an agreement for the version of master
before moving on with patches for back branches.

> 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.

This rebased patch looks fine for me. I am attaching as well versions
for back-branches for 9.1~9.5. In this case it is just necessary to
duplicate pgwin32_get_dynamic_tokeninfo and pgwin32_is_service to
pg_ctl.c to make things work properly.
Regards,
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

win32_service_back_branches.patch (application/x-patch, 4.8K) download diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 03b9770..5d5511d 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -149,6 +149,10 @@ static void WINAPI pgwin32_ServiceHandler(DWORD); static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *); static void pgwin32_doRunAsService(void); static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_service); +static bool pgwin32_get_dynamic_tokeninfo(HANDLE token, + TOKEN_INFORMATION_CLASS class, + char **InfoBuffer, char *errbuf, int errsize); +static int pgwin32_is_service(void); #endif static pgpid_t get_pgpid(bool is_status_request); @@ -216,7 +220,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? */ @@ -1689,6 +1693,160 @@ pgwin32_doRunAsService(void) } } +/* + * Call GetTokenInformation() on a token and return a dynamically sized + * buffer with the information in it. This buffer must be free():d by + * the calling function! + */ +static bool +pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class, + char **InfoBuffer, char *errbuf, int errsize) +{ + DWORD InfoBufferSize; + + if (GetTokenInformation(token, class, NULL, 0, &InfoBufferSize)) + { + 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", + GetLastError()); + return false; + } + + *InfoBuffer = malloc(InfoBufferSize); + if (*InfoBuffer == NULL) + { + snprintf(errbuf, errsize, "could not allocate %d bytes for token information\n", + (int) InfoBufferSize); + return false; + } + + if (!GetTokenInformation(token, class, *InfoBuffer, + InfoBufferSize, &InfoBufferSize)) + { + snprintf(errbuf, errsize, "could not get token information: error code %lu\n", + GetLastError()); + return false; + } + + return true; +} + +/* + * We consider ourselves running as a service if one of the following is + * true: + * + * 1) We are running as Local System (only used by services) + * 2) Our token contains SECURITY_SERVICE_RID (automatically added to the + * process token by the SCM when starting a service) + * + * Return values: + * 0 = Not service + * 1 = Service + * -1 = Error + * + * Note: we can't report errors via write_stderr (because that calls this) + * We are therefore reduced to writing directly on stderr, which sucks, but + * we have few alternatives. + */ +int +pgwin32_is_service(void) +{ + static int _is_service = -1; + HANDLE AccessToken; + char *InfoBuffer = NULL; + char errbuf[256]; + PTOKEN_GROUPS Groups; + PTOKEN_USER User; + PSID ServiceSid; + PSID LocalSystemSid; + SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY}; + UINT x; + + /* Only check the first time */ + if (_is_service != -1) + return _is_service; + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) + { + fprintf(stderr, "could not open process token: error code %lu\n", + GetLastError()); + return -1; + } + + /* First check for local system */ + if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenUser, &InfoBuffer, + errbuf, sizeof(errbuf))) + { + fprintf(stderr, "%s", errbuf); + return -1; + } + + User = (PTOKEN_USER) InfoBuffer; + + if (!AllocateAndInitializeSid(&NtAuthority, 1, + SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, + &LocalSystemSid)) + { + fprintf(stderr, "could not get SID for local system account\n"); + CloseHandle(AccessToken); + return -1; + } + + if (EqualSid(LocalSystemSid, User->User.Sid)) + { + FreeSid(LocalSystemSid); + free(InfoBuffer); + CloseHandle(AccessToken); + _is_service = 1; + return _is_service; + } + + FreeSid(LocalSystemSid); + free(InfoBuffer); + + /* Now check for group SID */ + if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenGroups, &InfoBuffer, + errbuf, sizeof(errbuf))) + { + fprintf(stderr, "%s", errbuf); + return -1; + } + + Groups = (PTOKEN_GROUPS) InfoBuffer; + + if (!AllocateAndInitializeSid(&NtAuthority, 1, + SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, + &ServiceSid)) + { + fprintf(stderr, "could not get SID for service group\n"); + free(InfoBuffer); + CloseHandle(AccessToken); + return -1; + } + + _is_service = 0; + for (x = 0; x < Groups->GroupCount; x++) + { + if (EqualSid(ServiceSid, Groups->Groups[x].Sid)) + { + _is_service = 1; + break; + } + } + + free(InfoBuffer); + FreeSid(ServiceSid); + + CloseHandle(AccessToken); + + return _is_service; +} + /* * Mingw headers are incomplete, and so are the libraries. So we have to load
^ permalink raw reply [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2016-01-07 15:11           ` Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Alvaro Herrera @ 2016-01-07 15:11 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

Michael Paquier wrote:
> On Thu, Jan 7, 2016 at 6:42 AM, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:

> > 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?
> 
> Yes (like restricted tokens and pg_upgrade stuff we did), but I was
> just waiting that we reach an agreement for the version of master
> before moving on with patches for back branches.

Right.

> > 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.
> 
> This rebased patch looks fine for me. I am attaching as well versions
> for back-branches for 9.1~9.5. In this case it is just necessary to
> duplicate pgwin32_get_dynamic_tokeninfo and pgwin32_is_service to
> pg_ctl.c to make things work properly.

Pushed.  Obviously I didn't compile any of this, so let's see how the
buildfarm likes it.

Thanks!

-- 
Á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-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2016-01-08 02:11             ` Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 02:18               ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 20+ messages in thread

From: Michael Paquier @ 2016-01-08 02:11 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

On Fri, Jan 8, 2016 at 12:11 AM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:
> Pushed.  Obviously I didn't compile any of this, so let's see how the
> buildfarm likes it.

It didn't like it much. Thanks for the push and for the post-turbulence fixes.
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2016-01-08 02:18               ` Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 03:08                 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Tom Lane @ 2016-01-08 02:18 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

Michael Paquier <michael.paquier@gmail.com> writes:
> On Fri, Jan 8, 2016 at 12:11 AM, Alvaro Herrera
> <alvherre@2ndquadrant.com> wrote:
>> Pushed.  Obviously I didn't compile any of this, so let's see how the
>> buildfarm likes it.

> It didn't like it much. Thanks for the push and for the post-turbulence fixes.

brolga is still unhappy:

gcc-4 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -O2 -I../../../src/interfaces/libpq -I../../../src/include -I/usr/include/libxml2   -c -o pg_ctl.o pg_ctl.c
pg_ctl.c: In function ‘write_stderr’:
pg_ctl.c:219: warning: implicit declaration of function ‘pgwin32_is_service’
gcc-4 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -O2 pg_ctl.o  -L../../../src/common -lpgcommon -L../../../src/port -lpgport -L../../../src/interfaces/libpq -lpq  -L../../../src/port -L../../../src/common -Wl,--allow-multiple-definition -Wl,--enable-auto-import -L/usr/lib  -L/usr/local/lib -Wl,--as-needed   -lpgcommon -lpgport -lxslt -lxml2 -lz -lreadline -lcrypt -o pg_ctl.exe
pg_ctl.o: In function `write_stderr':
/home/andrew/bf/root/HEAD/pgsql.build/src/bin/pg_ctl/pg_ctl.c:219: undefined reference to `_pgwin32_is_service'
collect2: ld returned 1 exit status
make[3]: *** [pg_ctl] Error 1

Too soon to tell about the other Windows critters.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 02:18               ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-01-08 03:08                 ` Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 06:02                   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 20+ messages in thread

From: Michael Paquier @ 2016-01-08 03:08 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

On Fri, Jan 8, 2016 at 11:18 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Michael Paquier <michael.paquier@gmail.com> writes:
>> On Fri, Jan 8, 2016 at 12:11 AM, Alvaro Herrera
>> <alvherre@2ndquadrant.com> wrote:
>>> Pushed.  Obviously I didn't compile any of this, so let's see how the
>>> buildfarm likes it.
>
>> It didn't like it much. Thanks for the push and for the post-turbulence fixes.
>
> brolga is still unhappy:
>
> gcc-4 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -O2 -I../../../src/interfaces/libpq -I../../../src/include -I/usr/include/libxml2   -c -o pg_ctl.o pg_ctl.c
> pg_ctl.c: In function ‘write_stderr’:
> pg_ctl.c:219: warning: implicit declaration of function ‘pgwin32_is_service’
> gcc-4 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -O2 pg_ctl.o  -L../../../src/common -lpgcommon -L../../../src/port -lpgport -L../../../src/interfaces/libpq -lpq  -L../../../src/port -L../../../src/common -Wl,--allow-multiple-definition -Wl,--enable-auto-import -L/usr/lib  -L/usr/local/lib -Wl,--as-needed   -lpgcommon -lpgport -lxslt -lxml2 -lz -lreadline -lcrypt -o pg_ctl.exe
> pg_ctl.o: In function `write_stderr':
> /home/andrew/bf/root/HEAD/pgsql.build/src/bin/pg_ctl/pg_ctl.c:219: undefined reference to `_pgwin32_is_service'
> collect2: ld returned 1 exit status
> make[3]: *** [pg_ctl] Error 1
>
> Too soon to tell about the other Windows critters.

Hm. It is not that straight-forward... and I have to admit that I will
have little time to look into that until Monday/Tuesday with a real
working environment. Now, looking at those logs, none of the win32*.c
files actually are included in the OBJS list in src/port/Makefile, so
it would seem that the correct way of going is to have the definition
of those new routines in include/port.h for all cases, and include
win32security.c in the list of objects compiled. But those are only
assumptions as I would need a cygwin environment to check that
properly at this stage.

Now, backbranches are proving that compiling pg_ctl.c with those
routines copied in it actually works fine, so based on my limited time
for the next couple of days, it may be better to just revert the patch
entirely on HEAD and apply what has been pushed into back branches to
stabilize the buildfarm, this will address the bug entirely. And it is
no good to keep the buildfarm red for long,
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 02:18               ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 03:08                 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2016-01-08 06:02                   ` Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 07:11                     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Tom Lane @ 2016-01-08 06:02 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Andrew Dunstan <andrew@dunslane.net>; Alvaro Herrera <alvherre@2ndquadrant.com>; Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

Michael Paquier <michael.paquier@gmail.com> writes:
> On Fri, Jan 8, 2016 at 11:18 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> brolga is still unhappy:

> ... Now, backbranches are proving that compiling pg_ctl.c with those
> routines copied in it actually works fine, so based on my limited time
> for the next couple of days, it may be better to just revert the patch
> entirely on HEAD and apply what has been pushed into back branches to
> stabilize the buildfarm, this will address the bug entirely. And it is
> no good to keep the buildfarm red for long,

Hm, narwhal just reported in green, which I was not expecting, and so did
frogmouth.  So brolga seems like an outlier.  I think reverting might be
an overreaction.  At this point the question is more like "how is brolga
different from the other non-MSVC Windows critters?".  Perhaps Andrew
can offer some insight.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 02:18               ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 03:08                 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 06:02                   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-01-08 07:11                     ` Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 14:16                       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Andrew Dunstan <andrew@dunslane.net>
  0 siblings, 1 reply; 20+ messages in thread

From: Michael Paquier @ 2016-01-08 07:11 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andrew Dunstan <andrew@dunslane.net>; Alvaro Herrera <alvherre@2ndquadrant.com>; Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

On Fri, Jan 8, 2016 at 3:02 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Michael Paquier <michael.paquier@gmail.com> writes:
>> On Fri, Jan 8, 2016 at 11:18 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> brolga is still unhappy:
>
>> ... Now, backbranches are proving that compiling pg_ctl.c with those
>> routines copied in it actually works fine, so based on my limited time
>> for the next couple of days, it may be better to just revert the patch
>> entirely on HEAD and apply what has been pushed into back branches to
>> stabilize the buildfarm, this will address the bug entirely. And it is
>> no good to keep the buildfarm red for long,
>
> Hm, narwhal just reported in green, which I was not expecting, and so did
> frogmouth.  So brolga seems like an outlier.  I think reverting might be
> an overreaction.  At this point the question is more like "how is brolga
> different from the other non-MSVC Windows critters?".  Perhaps Andrew
> can offer some insight.

That's a good sign at least :)

Looking at the logs, the main difference is that win32security.c is
not compiled in -lpgport for cygwin, and cygwin goes through
src/include/port/win32.h to refer to some routines, so I would think
that the correct fix is to add win32security.c to the list of OBJS for
cygwin. So the attached is correct perhaps? That's a blind fix I don't
have cygwin set up on my machines now.
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

20160107_cygwin_fix.patch (application/x-patch, 301B) download diff --git a/configure.in b/configure.in index 9398482..41402df 100644 --- a/configure.in +++ b/configure.in @@ -1596,6 +1596,7 @@ fi # Cygwin needs only a bit of that if test "$PORTNAME" = "cygwin"; then AC_LIBOBJ(dirmod) + AC_LIBOBJ(win32security) fi AC_CHECK_DECLS([sys_siglist], [], [],
^ permalink raw reply [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 02:18               ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 03:08                 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 06:02                   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 07:11                     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2016-01-08 14:16                       ` Andrew Dunstan <andrew@dunslane.net>
  2016-01-08 14:55                         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Andrew Dunstan @ 2016-01-08 14:16 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs



On 01/08/2016 02:11 AM, Michael Paquier wrote:
> On Fri, Jan 8, 2016 at 3:02 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Michael Paquier <michael.paquier@gmail.com> writes:
>>> On Fri, Jan 8, 2016 at 11:18 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>>> brolga is still unhappy:
>>> ... Now, backbranches are proving that compiling pg_ctl.c with those
>>> routines copied in it actually works fine, so based on my limited time
>>> for the next couple of days, it may be better to just revert the patch
>>> entirely on HEAD and apply what has been pushed into back branches to
>>> stabilize the buildfarm, this will address the bug entirely. And it is
>>> no good to keep the buildfarm red for long,
>> Hm, narwhal just reported in green, which I was not expecting, and so did
>> frogmouth.  So brolga seems like an outlier.  I think reverting might be
>> an overreaction.  At this point the question is more like "how is brolga
>> different from the other non-MSVC Windows critters?".  Perhaps Andrew
>> can offer some insight.
> That's a good sign at least :)
>
> Looking at the logs, the main difference is that win32security.c is
> not compiled in -lpgport for cygwin, and cygwin goes through
> src/include/port/win32.h to refer to some routines, so I would think
> that the correct fix is to add win32security.c to the list of OBJS for
> cygwin. So the attached is correct perhaps? That's a blind fix I don't
> have cygwin set up on my machines now.


I'll test it when I get a moment. Thanks for the patch.

I see this service code goes back probably to the first commit of 
pg_ctl.c, which means it's going to have been either my fault or Bruce's 
:-) TBH I'm not sure how to run postgres as a service on Cygwin. In 
general I would expect it to be via cygrunsrv, like other services I do 
actually run (cron and cygserver), rather than via pg_ctl. Anyway, for 
now lets see if we can get it building again.

cheers

andrew



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 02:18               ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 03:08                 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 06:02                   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 07:11                     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 14:16                       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Andrew Dunstan <andrew@dunslane.net>
@ 2016-01-08 14:55                         ` Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 23:04                           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Alvaro Herrera @ 2016-01-08 14:55 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: Michael Paquier <michael.paquier@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

Andrew Dunstan wrote:

> On 01/08/2016 02:11 AM, Michael Paquier wrote:

> >Looking at the logs, the main difference is that win32security.c is
> >not compiled in -lpgport for cygwin, and cygwin goes through
> >src/include/port/win32.h to refer to some routines, so I would think
> >that the correct fix is to add win32security.c to the list of OBJS for
> >cygwin. So the attached is correct perhaps? That's a blind fix I don't
> >have cygwin set up on my machines now.

> I'll test it when I get a moment. Thanks for the patch.
> 
> I see this service code goes back probably to the first commit of pg_ctl.c,
> which means it's going to have been either my fault or Bruce's :-)

Per IM conversation with Andrew, I pushed it blind and he'll set up an
immediate BF run on brolga so we know quickly whether it worked.

> TBH I'm not sure how to run postgres as a service on Cygwin. In
> general I would expect it to be via cygrunsrv, like other services I
> do actually run (cron and cygserver), rather than via pg_ctl. Anyway,
> for now lets see if we can get it building again.

Yeah, I guess you'd normally you'd do that, but the point is that the OP
was using pg_ctl to control one that's *not* a service and the logs were
still sent to Event Log because of the bug fixed here.

-- 
Á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-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-08-31 14:56   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Egon Kocjan <ekocjan@gmail.com>
  2015-09-01 01:43     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-06 21:42       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-07 04:13         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-07 15:11           ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-01-08 02:11             ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 02:18               ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 03:08                 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 06:02                   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Tom Lane <tgl@sss.pgh.pa.us>
  2016-01-08 07:11                     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2016-01-08 14:16                       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Andrew Dunstan <andrew@dunslane.net>
  2016-01-08 14:55                         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2016-01-08 23:04                           ` Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 20+ messages in thread

From: Michael Paquier @ 2016-01-08 23:04 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Andrew Dunstan <andrew@dunslane.net>; Tom Lane <tgl@sss.pgh.pa.us>; Egon Kocjan <ekocjan@gmail.com>; pgsql-bugs

On Fri, Jan 8, 2016 at 11:55 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:
> Andrew Dunstan wrote:
>
>> On 01/08/2016 02:11 AM, Michael Paquier wrote:
>
>> >Looking at the logs, the main difference is that win32security.c is
>> >not compiled in -lpgport for cygwin, and cygwin goes through
>> >src/include/port/win32.h to refer to some routines, so I would think
>> >that the correct fix is to add win32security.c to the list of OBJS for
>> >cygwin. So the attached is correct perhaps? That's a blind fix I don't
>> >have cygwin set up on my machines now.
>
>> I'll test it when I get a moment. Thanks for the patch.
>>
>> I see this service code goes back probably to the first commit of pg_ctl.c,
>> which means it's going to have been either my fault or Bruce's :-)
>
> Per IM conversation with Andrew, I pushed it blind and he'll set up an
> immediate BF run on brolga so we know quickly whether it worked.

And it failed. That's frustrating. I'll try to set up a cygwin
environment and address that.
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2015-09-03 11:36   ` Andres Freund <andres@anarazel.de>
  2015-09-03 12:28     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  1 sibling, 1 reply; 20+ messages in thread

From: Andres Freund @ 2015-09-03 11:36 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs

On 2015-08-31 15:51:57 +0900, Michael Paquier wrote:
>  	if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken))
>  	{
> +#ifndef FRONTEND
>  		write_stderr("could not open process token: error code %lu\n",
>  					 GetLastError());
> +#else
> +		fprintf(stderr, "could not open process token: error code %lu\n",
> +				GetLastError());
> +#endif
>  		exit(1);
>  	}

I find these kind of ifdefs rather ugly - why not just introduce a
wrapper?

Greetings,

Andres Freund


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-09-03 11:36   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Andres Freund <andres@anarazel.de>
@ 2015-09-03 12:28     ` Michael Paquier <michael.paquier@gmail.com>
  2015-09-04 01:28       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Michael Paquier @ 2015-09-03 12:28 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs

On Thu, Sep 3, 2015 at 8:36 PM, Andres Freund <andres@anarazel.de> wrote:
> On 2015-08-31 15:51:57 +0900, Michael Paquier wrote:
>>       if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken))
>>       {
>> +#ifndef FRONTEND
>>               write_stderr("could not open process token: error code %lu\n",
>>                                        GetLastError());
>> +#else
>> +             fprintf(stderr, "could not open process token: error code %lu\n",
>> +                             GetLastError());
>> +#endif
>>               exit(1);
>>       }
>
> I find these kind of ifdefs rather ugly - why not just introduce a
> wrapper?

I thought it was just not worth it for this file. I don't mind
updating if you think that's cleaner this way.
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-09-03 11:36   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Andres Freund <andres@anarazel.de>
  2015-09-03 12:28     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2015-09-04 01:28       ` Michael Paquier <michael.paquier@gmail.com>
  2015-12-01 13:04         ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 20+ messages in thread

From: Michael Paquier @ 2015-09-04 01:28 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs

On Thu, Sep 3, 2015 at 9:28 PM, Michael Paquier <michael.paquier@gmail.com>
wrote:

> On Thu, Sep 3, 2015 at 8:36 PM, Andres Freund <andres@anarazel.de> wrote:
> > On 2015-08-31 15:51:57 +0900, Michael Paquier wrote:
> >>       if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ,
> &AccessToken))
> >>       {
> >> +#ifndef FRONTEND
> >>               write_stderr("could not open process token: error code
> %lu\n",
> >>                                        GetLastError());
> >> +#else
> >> +             fprintf(stderr, "could not open process token: error code
> %lu\n",
> >> +                             GetLastError());
> >> +#endif
> >>               exit(1);
> >>       }
> >
> > I find these kind of ifdefs rather ugly - why not just introduce a
> > wrapper?
>
> I thought it was just not worth it for this file. I don't mind
> updating if you think that's cleaner this way.
>

New patch attached, updated in consequence.
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

20150904_win32_service_v2.patch (text/x-diff, 12.7K) download From e65fb3a4d6729d2a2739b1138548b3b7923b2338 Mon Sep 17 00:00:00 2001 From: Michael Paquier <michael@otacoo.com> Date: Thu, 3 Sep 2015 18:26:23 -0700 Subject: [PATCH] Switch pgwin32_is_service to src/port and make pg_ctl use it --- src/backend/port/win32/Makefile | 2 +- src/backend/port/win32/security.c | 248 --------------------- src/bin/pg_ctl/pg_ctl.c | 2 +- src/include/port/win32.h | 7 +- .../port/win32/security.c => port/win32security.c} | 53 +++-- src/tools/msvc/Mkvcbuild.pm | 2 +- 6 files changed, 47 insertions(+), 267 deletions(-) copy src/{backend/port/win32/security.c => port/win32security.c} (83%) 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/backend/port/win32/security.c b/src/backend/port/win32/security.c index e9cfe15..e69de29 100644 --- a/src/backend/port/win32/security.c +++ b/src/backend/port/win32/security.c @@ -1,248 +0,0 @@ -/*------------------------------------------------------------------------- - * - * security.c - * Microsoft Windows Win32 Security Support Functions - * - * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group - * - * IDENTIFICATION - * src/backend/port/win32/security.c - * - *------------------------------------------------------------------------- - */ - -#include "postgres.h" - - -static BOOL pgwin32_get_dynamic_tokeninfo(HANDLE token, - TOKEN_INFORMATION_CLASS class, char **InfoBuffer, - char *errbuf, int errsize); - -/* - * Returns nonzero if the current user has administrative privileges, - * or zero if not. - * - * Note: this cannot use ereport() because it's called too early during - * startup. - */ -int -pgwin32_is_admin(void) -{ - HANDLE AccessToken; - char *InfoBuffer = NULL; - char errbuf[256]; - PTOKEN_GROUPS Groups; - PSID AdministratorsSid; - PSID PowerUsersSid; - SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY}; - UINT x; - BOOL success; - - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) - { - write_stderr("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); - exit(1); - } - - Groups = (PTOKEN_GROUPS) InfoBuffer; - - CloseHandle(AccessToken); - - if (!AllocateAndInitializeSid(&NtAuthority, 2, - 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()); - exit(1); - } - - if (!AllocateAndInitializeSid(&NtAuthority, 2, - 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()); - exit(1); - } - - success = FALSE; - - 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))) - { - success = TRUE; - break; - } - } - - free(InfoBuffer); - FreeSid(AdministratorsSid); - FreeSid(PowerUsersSid); - return success; -} - -/* - * We consider ourselves running as a service if one of the following is - * true: - * - * 1) We are running as Local System (only used by services) - * 2) Our token contains SECURITY_SERVICE_RID (automatically added to the - * process token by the SCM when starting a service) - * - * Return values: - * 0 = Not service - * 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. - */ -int -pgwin32_is_service(void) -{ - static int _is_service = -1; - HANDLE AccessToken; - char *InfoBuffer = NULL; - char errbuf[256]; - PTOKEN_GROUPS Groups; - PTOKEN_USER User; - PSID ServiceSid; - PSID LocalSystemSid; - SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY}; - UINT x; - - /* Only check the first time */ - if (_is_service != -1) - return _is_service; - - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken)) - { - fprintf(stderr, "could not open process token: error code %lu\n", - GetLastError()); - return -1; - } - - /* First check for local system */ - if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenUser, &InfoBuffer, - errbuf, sizeof(errbuf))) - { - fprintf(stderr, "%s", errbuf); - return -1; - } - - User = (PTOKEN_USER) InfoBuffer; - - if (!AllocateAndInitializeSid(&NtAuthority, 1, - SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, - &LocalSystemSid)) - { - fprintf(stderr, "could not get SID for local system account\n"); - CloseHandle(AccessToken); - return -1; - } - - if (EqualSid(LocalSystemSid, User->User.Sid)) - { - FreeSid(LocalSystemSid); - free(InfoBuffer); - CloseHandle(AccessToken); - _is_service = 1; - return _is_service; - } - - FreeSid(LocalSystemSid); - free(InfoBuffer); - - /* Now check for group SID */ - if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenGroups, &InfoBuffer, - errbuf, sizeof(errbuf))) - { - fprintf(stderr, "%s", errbuf); - return -1; - } - - Groups = (PTOKEN_GROUPS) InfoBuffer; - - if (!AllocateAndInitializeSid(&NtAuthority, 1, - SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, - &ServiceSid)) - { - fprintf(stderr, "could not get SID for service group\n"); - free(InfoBuffer); - CloseHandle(AccessToken); - return -1; - } - - _is_service = 0; - for (x = 0; x < Groups->GroupCount; x++) - { - if (EqualSid(ServiceSid, Groups->Groups[x].Sid)) - { - _is_service = 1; - break; - } - } - - free(InfoBuffer); - FreeSid(ServiceSid); - - CloseHandle(AccessToken); - - return _is_service; -} - - -/* - * Call GetTokenInformation() on a token and return a dynamically sized - * buffer with the information in it. This buffer must be free():d by - * the calling function! - */ -static BOOL -pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class, - char **InfoBuffer, char *errbuf, int errsize) -{ - DWORD InfoBufferSize; - - if (GetTokenInformation(token, class, NULL, 0, &InfoBufferSize)) - { - 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", - GetLastError()); - return FALSE; - } - - *InfoBuffer = malloc(InfoBufferSize); - if (*InfoBuffer == NULL) - { - snprintf(errbuf, errsize, "could not allocate %d bytes for token information\n", - (int) InfoBufferSize); - return FALSE; - } - - if (!GetTokenInformation(token, class, *InfoBuffer, - InfoBufferSize, &InfoBufferSize)) - { - snprintf(errbuf, errsize, "could not get token information: error code %lu\n", - GetLastError()); - return FALSE; - } - - return TRUE; -} diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 6a36d29..d9eca37 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -215,7 +215,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 83% copy from src/backend/port/win32/security.c copy to src/port/win32security.c index e9cfe15..59c2a67 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-2015, 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); } @@ -60,8 +85,8 @@ pgwin32_is_admin(void) 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); } @@ -69,7 +94,7 @@ pgwin32_is_admin(void) 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", + log_error("could not get SID for PowerUsers group: error code %lu\n", GetLastError()); exit(1); } @@ -105,9 +130,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) diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 3abbb4c..224fdcd 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -89,7 +89,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'); -- 1.9.2.msysgit.0
^ permalink raw reply [nested|flat] 20+ messages in thread

* Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe
  2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
  2015-08-31 06:51 ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-09-03 11:36   ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Andres Freund <andres@anarazel.de>
  2015-09-03 12:28     ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
  2015-09-04 01:28       ` Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe Michael Paquier <michael.paquier@gmail.com>
@ 2015-12-01 13:04         ` Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 20+ messages in thread

From: Michael Paquier @ 2015-12-01 13:04 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs

On Fri, Sep 4, 2015 at 10:28 AM, Michael Paquier wrote:
> New patch attached, updated in consequence.

This was registered in 2015-11 CF, and that's a bug fix for a
long-existing bug. I am moving it to next CF of January.
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 20+ messages in thread


end of thread, other threads:[~2016-01-08 23:04 UTC | newest]

Thread overview: 20+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-08-28 10:46 BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe ekocjan@gmail.com
2015-08-31 06:51 ` Michael Paquier <michael.paquier@gmail.com>
2015-08-31 14:56   ` Egon Kocjan <ekocjan@gmail.com>
2015-09-01 01:43     ` Michael Paquier <michael.paquier@gmail.com>
2015-09-03 08:24       ` Egon Kocjan <ekocjan@gmail.com>
2016-01-06 21:42       ` Alvaro Herrera <alvherre@2ndquadrant.com>
2016-01-07 04:13         ` Michael Paquier <michael.paquier@gmail.com>
2016-01-07 15:11           ` Alvaro Herrera <alvherre@2ndquadrant.com>
2016-01-08 02:11             ` Michael Paquier <michael.paquier@gmail.com>
2016-01-08 02:18               ` Tom Lane <tgl@sss.pgh.pa.us>
2016-01-08 03:08                 ` Michael Paquier <michael.paquier@gmail.com>
2016-01-08 06:02                   ` Tom Lane <tgl@sss.pgh.pa.us>
2016-01-08 07:11                     ` Michael Paquier <michael.paquier@gmail.com>
2016-01-08 14:16                       ` Andrew Dunstan <andrew@dunslane.net>
2016-01-08 14:55                         ` Alvaro Herrera <alvherre@2ndquadrant.com>
2016-01-08 23:04                           ` Michael Paquier <michael.paquier@gmail.com>
2015-09-03 11:36   ` Andres Freund <andres@anarazel.de>
2015-09-03 12:28     ` Michael Paquier <michael.paquier@gmail.com>
2015-09-04 01:28       ` Michael Paquier <michael.paquier@gmail.com>
2015-12-01 13:04         ` Michael Paquier <michael.paquier@gmail.com>

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