Обсуждение: pg_ctl start broken on windows

Поиск
Список
Период
Сортировка

pg_ctl start broken on windows

От
Andrew Dunstan
Дата:
As I feared might happen, the use of a system() call to start the
postmaster in Windows is broken because of the brain dead way its shell
handles quotes.  I was able to get around this in initdb by making sure
that only one thing on the command line was quoted (by putting PGDATA in
the environment instead of the command line). We can't do that here ;-(
. Here's what happens:

C:\msys\1.0\local\pgsql>bin\pg_ctl -D data -l logfile start
'C:/msys/1.0/local/pgsql/bin/postmaster.exe"  < nul >>"logfile' is not recognized as an internal or external command,
operableprogram or batch file. 


I think we'll have to bite the bullet and replace that system() call
with a direct call to CreateProcess() with the appropriate parameters. I
haven't ever done this, so some help from one of the more experienced
Windows programmers would be appreciated.

cheers

andrew

Re: pg_ctl start broken on windows

От
Bruce Momjian
Дата:
Andrew Dunstan wrote:
>
> As I feared might happen, the use of a system() call to start the
> postmaster in Windows is broken because of the brain dead way its shell
> handles quotes.  I was able to get around this in initdb by making sure
> that only one thing on the command line was quoted (by putting PGDATA in
> the environment instead of the command line). We can't do that here ;-(
> . Here's what happens:
>
> C:\msys\1.0\local\pgsql>bin\pg_ctl -D data -l logfile start
> 'C:/msys/1.0/local/pgsql/bin/postmaster.exe"  < nul >>"logfile' is not recognized as an internal or external command,
operableprogram or batch file. 
>
>
> I think we'll have to bite the bullet and replace that system() call
> with a direct call to CreateProcess() with the appropriate parameters. I
> haven't ever done this, so some help from one of the more experienced
> Windows programmers would be appreciated.

Yes, I assumed pg_ctl would have such issues.  pg_dumpall has them as
well calling pg_dump.

Here is a strange idea that I am looking for someone to check. According
to someone, Win32 system just strips off the first and last quotes, and
that is why it is failing.  Try adding a quote to the beginning and end
of the system string, and leave the existing quote intact.  That might
fix is and if it does I can add this to CVS.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: pg_ctl start broken on windows

От
Andrew Dunstan
Дата:
Bruce Momjian wrote:

>Andrew Dunstan wrote:
>
>
>>As I feared might happen, the use of a system() call to start the
>>postmaster in Windows is broken because of the brain dead way its shell
>>handles quotes.  I was able to get around this in initdb by making sure
>>that only one thing on the command line was quoted (by putting PGDATA in
>>the environment instead of the command line). We can't do that here ;-(
>>. Here's what happens:
>>
>>C:\msys\1.0\local\pgsql>bin\pg_ctl -D data -l logfile start
>>'C:/msys/1.0/local/pgsql/bin/postmaster.exe"  < nul >>"logfile' is not recognized as an internal or external command,
operableprogram or batch file. 
>>
>>
>>I think we'll have to bite the bullet and replace that system() call
>>with a direct call to CreateProcess() with the appropriate parameters. I
>>haven't ever done this, so some help from one of the more experienced
>>Windows programmers would be appreciated.
>>
>>
>
>Yes, I assumed pg_ctl would have such issues.  pg_dumpall has them as
>well calling pg_dump.
>
>Here is a strange idea that I am looking for someone to check. According
>to someone, Win32 system just strips off the first and last quotes, and
>that is why it is failing.  Try adding a quote to the beginning and end
>of the system string, and leave the existing quote intact.  That might
>fix is and if it does I can add this to CVS.
>
>
>

I'll test to make sure, but I can tell you now it won't work. It will
see the inner quotes and interpret them as part of the command. (I tried
all these variants when I was testing initdb).

cheers

andrew

Re: pg_ctl start broken on windows

От
Bruce Momjian
Дата:
Andrew Dunstan wrote:
> >>C:\msys\1.0\local\pgsql>bin\pg_ctl -D data -l logfile start
> >>'C:/msys/1.0/local/pgsql/bin/postmaster.exe"  < nul >>"logfile' is not recognized as an internal or external
command,operable program or batch file. 
> >>
> >>
> >>I think we'll have to bite the bullet and replace that system() call
> >>with a direct call to CreateProcess() with the appropriate parameters. I
> >>haven't ever done this, so some help from one of the more experienced
> >>Windows programmers would be appreciated.
> >>
> >>
> >
> >Yes, I assumed pg_ctl would have such issues.  pg_dumpall has them as
> >well calling pg_dump.
> >
> >Here is a strange idea that I am looking for someone to check. According
> >to someone, Win32 system just strips off the first and last quotes, and
> >that is why it is failing.  Try adding a quote to the beginning and end
> >of the system string, and leave the existing quote intact.  That might
> >fix is and if it does I can add this to CVS.
> >
> >
> >
>
> I'll test to make sure, but I can tell you now it won't work. It will
> see the inner quotes and interpret them as part of the command. (I tried
> all these variants when I was testing initdb).

Claudio reported it worked for pg_dumpall for single quotes:

    system(''cmd.exe' 'arg1' 'arg2'');

What exactly is the logic of the stripping?

In fact, pg_ctl is wrong because it is using double quotes instead of
the safer single quotes.  Let me make that change.
--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: pg_ctl start broken on windows

От
Andrew Dunstan
Дата:
Bruce Momjian wrote:

>Andrew Dunstan wrote:
>
>
>>>>C:\msys\1.0\local\pgsql>bin\pg_ctl -D data -l logfile start
>>>>'C:/msys/1.0/local/pgsql/bin/postmaster.exe"  < nul >>"logfile' is not recognized as an internal or external
command,operable program or batch file. 
>>>>
>>>>
>>>>I think we'll have to bite the bullet and replace that system() call
>>>>with a direct call to CreateProcess() with the appropriate parameters. I
>>>>haven't ever done this, so some help from one of the more experienced
>>>>Windows programmers would be appreciated.
>>>>
>>>>
>>>>
>>>>
>>>Yes, I assumed pg_ctl would have such issues.  pg_dumpall has them as
>>>well calling pg_dump.
>>>
>>>Here is a strange idea that I am looking for someone to check. According
>>>to someone, Win32 system just strips off the first and last quotes, and
>>>that is why it is failing.  Try adding a quote to the beginning and end
>>>of the system string, and leave the existing quote intact.  That might
>>>fix is and if it does I can add this to CVS.
>>>
>>>
>>>
>>>
>>>
>>I'll test to make sure, but I can tell you now it won't work. It will
>>see the inner quotes and interpret them as part of the command. (I tried
>>all these variants when I was testing initdb).
>>
>>
>
>Claudio reported it worked for pg_dumpall for single quotes:
>
>    system(''cmd.exe' 'arg1' 'arg2'');
>
>What exactly is the logic of the stripping?
>
>In fact, pg_ctl is wrong because it is using double quotes instead of
>the safer single quotes.  Let me make that change.
>
>

No. It chokes on forward slashes.

and it tries to make a log file called 'logfile' (quotes included).

I really don't see what the problem is with calling CreateProcess - it
should only be a few lines of code unless I'm much mistaken, and would
get us out of this setup that is really fragile at best.

cheers

andrew

Re: pg_ctl start broken on windows

От
"Gary Doades"
Дата:
You might want to try ShellExecute first before delving into
CreateProcess

e.g.

ShellExecute(GetDesktopWindow(), "open", "cmd.exe", "arg1 arg2 arg3", NULL, SW_SHOWNORMAL);

The path to the executable (e.g. cmd.exe) must be fully qualified and
use the .exe extension in this case. Also you must use backslashes.
You will need to use backslashes if you use CreateProcess anyway.

If this doesn't work I can give you an example for CreateProcess.

Regards,
Gary.


On 9 Jun 2004 at 14:07, Andrew Dunstan wrote:

> Bruce Momjian wrote:
>
> >Andrew Dunstan wrote:
> >
> >
> >>>>C:\msys\1.0\local\pgsql>bin\pg_ctl -D data -l logfile start
> >>>>'C:/msys/1.0/local/pgsql/bin/postmaster.exe"  < nul >>"logfile' is not recognized as an internal or external
command,operable program or batch file. 
> >>>>
> >>>>
> >>>>I think we'll have to bite the bullet and replace that system() call
> >>>>with a direct call to CreateProcess() with the appropriate parameters. I
> >>>>haven't ever done this, so some help from one of the more experienced
> >>>>Windows programmers would be appreciated.
> >>>>
> >>>>
> >>>>
> >>>>
> >>>Yes, I assumed pg_ctl would have such issues.  pg_dumpall has them as
> >>>well calling pg_dump.
> >>>
> >>>Here is a strange idea that I am looking for someone to check. According
> >>>to someone, Win32 system just strips off the first and last quotes, and
> >>>that is why it is failing.  Try adding a quote to the beginning and end
> >>>of the system string, and leave the existing quote intact.  That might
> >>>fix is and if it does I can add this to CVS.
> >>>
> >>>
> >>>
> >>>
> >>>
> >>I'll test to make sure, but I can tell you now it won't work. It will
> >>see the inner quotes and interpret them as part of the command. (I tried
> >>all these variants when I was testing initdb).
> >>
> >>
> >
> >Claudio reported it worked for pg_dumpall for single quotes:
> >
> >    system(''cmd.exe' 'arg1' 'arg2'');
> >
> >What exactly is the logic of the stripping?
> >
> >In fact, pg_ctl is wrong because it is using double quotes instead of
> >the safer single quotes.  Let me make that change.
> >
> >
>
> No. It chokes on forward slashes.
>
> and it tries to make a log file called 'logfile' (quotes included).
>
> I really don't see what the problem is with calling CreateProcess - it
> should only be a few lines of code unless I'm much mistaken, and would
> get us out of this setup that is really fragile at best.
>
> cheers
>
> andrew
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
>                http://www.postgresql.org/docs/faqs/FAQ.html



Re: pg_ctl start broken on windows

От
Andrew Dunstan
Дата:
I don't see that it buys you much. But maybe you'd like to submit a
patch? (What is there isn't a desktop window, e.g. we are starting from
the service manager, as has been suggested for pg_ctl?)

cheers

andrew


Gary Doades wrote:

>You might want to try ShellExecute first before delving into
>CreateProcess
>
>e.g.
>
>ShellExecute(GetDesktopWindow(), "open", "cmd.exe", "arg1 arg2 arg3", NULL, SW_SHOWNORMAL);
>
>The path to the executable (e.g. cmd.exe) must be fully qualified and
>use the .exe extension in this case. Also you must use backslashes.
>You will need to use backslashes if you use CreateProcess anyway.
>
>If this doesn't work I can give you an example for CreateProcess.
>
>
>On 9 Jun 2004 at 14:07, Andrew Dunstan wrote:
>
>
>
>>I really don't see what the problem is with calling CreateProcess - it
>>should only be a few lines of code unless I'm much mistaken, and would
>>get us out of this setup that is really fragile at best.
>>
>>
>>


Re: pg_ctl start broken on windows

От
"Gary Doades"
Дата:
On 9 Jun 2004 at 15:43, Andrew Dunstan wrote:

>
> I don't see that it buys you much. But maybe you'd like to submit a
> patch? (What is there isn't a desktop window, e.g. we are starting from
> the service manager, as has been suggested for pg_ctl?)
>

It doesn't buy you anything except a few lines of code. I thought you
were looking for simplicity. I suggested this as a simple drop in
replacement for the system() call. I could do a patch, but by the time I
figured out where to put it etc, someone who knows the code well could
just drop this in.

Is it pg_ctl that is a service and this starts postmaster? If so then I
agree that you will probably need CreateProcess. This will give you
much more control over the created process anyway.

Here is a minimal example for CreateProcess


    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( "c:\\windows\system32\cmd.exe",  // executable
        "arg1 arg2 arg3", // Command line.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        FALSE,            // Set handle inheritance to FALSE.
        0,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        &si,              // Pointer to STARTUPINFO structure.
        &pi )             // Pointer to PROCESS_INFORMATION structure.
    )
    {
        ErrorExit( "CreateProcess failed." );
    }

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

where ZeroMemory is whatever convenient function you have for
clearing a block of memory.

You MUST close the process and thread handles afterwards to avoid
handle leaks. You could of course use (not close) the process handle in
pg_ctl (if it was the permanently running service) to monitor postmaster
for crashing out.

Regards,
Gary.


Re: pg_ctl start broken on windows

От
Andrew Dunstan
Дата:
Gary Doades wrote:

>On 9 Jun 2004 at 15:43, Andrew Dunstan wrote:
>
>
>
>>I don't see that it buys you much. But maybe you'd like to submit a
>>patch? (What is there isn't a desktop window, e.g. we are starting from
>>the service manager, as has been suggested for pg_ctl?)
>>
>>
>>
>
>It doesn't buy you anything except a few lines of code. I thought you
>were looking for simplicity. I suggested this as a simple drop in
>replacement for the system() call. I could do a patch, but by the time I
>figured out where to put it etc, someone who knows the code well could
>just drop this in.
>
>Is it pg_ctl that is a service and this starts postmaster? If so then I
>agree that you will probably need CreateProcess. This will give you
>much more control over the created process anyway.
>
>Here is a minimal example for CreateProcess
>
>
>    STARTUPINFO si;
>    PROCESS_INFORMATION pi;
>
>    ZeroMemory( &si, sizeof(si) );
>    si.cb = sizeof(si);
>    ZeroMemory( &pi, sizeof(pi) );
>
>    // Start the child process.
>    if( !CreateProcess( "c:\\windows\system32\cmd.exe",  // executable
>        "arg1 arg2 arg3", // Command line.
>        NULL,             // Process handle not inheritable.
>        NULL,             // Thread handle not inheritable.
>        FALSE,            // Set handle inheritance to FALSE.
>        0,                // No creation flags.
>        NULL,             // Use parent's environment block.
>        NULL,             // Use parent's starting directory.
>        &si,              // Pointer to STARTUPINFO structure.
>        &pi )             // Pointer to PROCESS_INFORMATION structure.
>    )
>    {
>        ErrorExit( "CreateProcess failed." );
>    }
>
>    // Close process and thread handles.
>    CloseHandle( pi.hProcess );
>    CloseHandle( pi.hThread );
>
>where ZeroMemory is whatever convenient function you have for
>clearing a block of memory.
>
>You MUST close the process and thread handles afterwards to avoid
>handle leaks. You could of course use (not close) the process handle in
>pg_ctl (if it was the permanently running service) to monitor postmaster
>for crashing out.
>
>

Thanks. The file to patch is src/bin/pg_ctl/pg_ctl.c

We'll need to do a bit more work than this, though. We have 2 cases -
one where there's a logfile parameter and one where there isn't.  Both
cases need stdin to be the null device (is that the default if you say
no handle inheritance?). If there is no logfile parameter then the
process's stdout and stderr handles need to be those of the calling
process (i.e. pg_ctl). If there is such a param then the postmaster's
stdout and stderr both need to be a handle on that file, opened in
append mode. I believe this needs to be set up in the STARTUPINFO
(reading MSDN makes my brain hurt).

Not sure of any other wrinkles.

cheers

andrew

Re: pg_ctl start broken on windows

От
Bruce Momjian
Дата:
Andrew Dunstan wrote:
> Bruce Momjian wrote:
> 
> >Andrew Dunstan wrote:
> >  
> >
> >>>>C:\msys\1.0\local\pgsql>bin\pg_ctl -D data -l logfile start
> >>>>'C:/msys/1.0/local/pgsql/bin/postmaster.exe"  < nul >>"logfile' is not recognized as an internal or external
command,operable program or batch file.
 
> >>>>
> >>>>
> >>I'll test to make sure, but I can tell you now it won't work. It will 
> >>see the inner quotes and interpret them as part of the command. (I tried 
> >>all these variants when I was testing initdb).
> >>    
> >>
> >
> >Claudio reported it worked for pg_dumpall for single quotes:
> >
> >    system(''cmd.exe' 'arg1' 'arg2'');
> >
> >What exactly is the logic of the stripping?
> >
> >In fact, pg_ctl is wrong because it is using double quotes instead of
> >the safer single quotes.  Let me make that change.
> >  
> >
> 
> No. It chokes on forward slashes.
> 
> and it tries to make a log file called 'logfile' (quotes included).
> 
> I really don't see what the problem is with calling CreateProcess - it 
> should only be a few lines of code unless I'm much mistaken, and would 
> get us out of this setup that is really fragile at best.

I did a lot of poking around in MinGW and found some basic rules. First,
MinGW's system() can't use single quotes for the executable name or any
file used in redirection (as you showed above).  Second, it gets
confused with multiple double-quoted strings.  The fix is to add a
double-quote to the beginning and end of the system string, so a typical
system string becomes:

    system("\"\"ls\" 'a' 'b c' > \"out\"\");

Single quotes are fine for arguments.

The applied patch makes this change and documents the behavior.  Would
folks test these changes on Win32 please?

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/bin/initdb/initdb.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/initdb/initdb.c,v
retrieving revision 1.35
diff -c -c -r1.35 initdb.c
*** src/bin/initdb/initdb.c    3 Jun 2004 00:07:36 -0000    1.35
--- src/bin/initdb/initdb.c    10 Jun 2004 16:24:17 -0000
***************
*** 812,823 ****
      for (i = 0; i < len; i++)
      {
          snprintf(cmd, sizeof(cmd),
!                  "\"%s\" -boot -x0 %s "
                   "-c shared_buffers=%d -c max_connections=%d template1 "
!                  "<%s >%s 2>&1",
!                  backend_exec, boot_options,
                   conns[i] * 5, conns[i],
!                  DEVNULL, DEVNULL);
          status = system(cmd);
          if (status == 0)
              break;
--- 812,823 ----
      for (i = 0; i < len; i++)
      {
          snprintf(cmd, sizeof(cmd),
!                  "%s\"%s\" -boot -x0 %s "
                   "-c shared_buffers=%d -c max_connections=%d template1 "
!                  "< \"%s\" > \"%s\" 2>&1%s",
!                  SYSTEMQUOTE, backend_exec, boot_options,
                   conns[i] * 5, conns[i],
!                  DEVNULL, DEVNULL, SYSTEMQUOTE);
          status = system(cmd);
          if (status == 0)
              break;
***************
*** 848,859 ****
      for (i = 0; i < len; i++)
      {
          snprintf(cmd, sizeof(cmd),
!                  "\"%s\" -boot -x0 %s "
                   "-c shared_buffers=%d -c max_connections=%d template1 "
!                  "<%s >%s 2>&1",
!                  backend_exec, boot_options,
                   bufs[i], n_connections,
!                  DEVNULL, DEVNULL);
          status = system(cmd);
          if (status == 0)
              break;
--- 848,859 ----
      for (i = 0; i < len; i++)
      {
          snprintf(cmd, sizeof(cmd),
!                  "%s\"%s\" -boot -x0 %s "
                   "-c shared_buffers=%d -c max_connections=%d template1 "
!                  "< \"%s\" > \"%s\" 2>&1%s",
!                  SYSTEMQUOTE, backend_exec, boot_options,
                   bufs[i], n_connections,
!                  DEVNULL, DEVNULL, SYSTEMQUOTE);
          status = system(cmd);
          if (status == 0)
              break;
Index: src/bin/pg_ctl/pg_ctl.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.c,v
retrieving revision 1.8
diff -c -c -r1.8 pg_ctl.c
*** src/bin/pg_ctl/pg_ctl.c    9 Jun 2004 17:36:07 -0000    1.8
--- src/bin/pg_ctl/pg_ctl.c    10 Jun 2004 16:24:17 -0000
***************
*** 224,234 ****
  
      /* Does '&' work on Win32? */
      if (log_file != NULL)
!         snprintf(cmd, MAXPGPATH, "'%s' %s < %s >> '%s' 2>&1 &",
!                  postgres_path, post_opts, DEVNULL, log_file);
      else
!         snprintf(cmd, MAXPGPATH, "'%s' %s < %s 2>&1 &",
!                  postgres_path, post_opts, DEVNULL);
      return system(cmd);
  }
  
--- 224,235 ----
  
      /* Does '&' work on Win32? */
      if (log_file != NULL)
!         snprintf(cmd, MAXPGPATH, "%s\"%s\" %s < %s >> \"%s\" 2>&1 &%s",
!                  SYSTEMQUOTE, postgres_path, post_opts, DEVNULL, log_file,
!                  SYSTEMQUOTE);
      else
!         snprintf(cmd, MAXPGPATH, "%s\"%s\" %s < \"%s\" 2>&1 &%s",
!                  SYSTEMQUOTE, postgres_path, post_opts, DEVNULL, SYSTEMQUOTE);
      return system(cmd);
  }
  
Index: src/bin/pg_dump/pg_dumpall.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/pg_dump/pg_dumpall.c,v
retrieving revision 1.40
diff -c -c -r1.40 pg_dumpall.c
*** src/bin/pg_dump/pg_dumpall.c    9 Jun 2004 17:37:28 -0000    1.40
--- src/bin/pg_dump/pg_dumpall.c    10 Jun 2004 16:24:18 -0000
***************
*** 690,696 ****
      const char *p;
      int            ret;
  
!     appendPQExpBuffer(cmd, "'%s' %s -Fp '", pg_dump_bin, pgdumpopts->data);
  
      /* Shell quoting is not quite like SQL quoting, so can't use fmtId */
      for (p = dbname; *p; p++)
--- 690,697 ----
      const char *p;
      int            ret;
  
!     appendPQExpBuffer(cmd, "%s\"%s\" %s -Fp '", SYSTEMQUOTE, pg_dump_bin,
!                       pgdumpopts->data);
  
      /* Shell quoting is not quite like SQL quoting, so can't use fmtId */
      for (p = dbname; *p; p++)
***************
*** 702,707 ****
--- 703,709 ----
      }
  
      appendPQExpBufferChar(cmd, '\'');
+     appendStringLiteral(cmd, SYSTEMQUOTE, false);
  
      if (verbose)
          fprintf(stderr, _("%s: running \"%s\"\n"), progname, cmd->data);
Index: src/include/port.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/include/port.h,v
retrieving revision 1.40
diff -c -c -r1.40 port.h
*** src/include/port.h    3 Jun 2004 00:07:38 -0000    1.40
--- src/include/port.h    10 Jun 2004 16:24:18 -0000
***************
*** 72,77 ****
--- 72,89 ----
  #define DEVNULL "/dev/null"
  #endif
  
+ /*
+  *    Win32 needs double quotes at the beginning and end of system()
+  *    strings.  If not, it gets confused with multiple quoted strings.
+  *    It also must use double-quotes around the executable name
+  *    and any files use for redirection.  Other args can use single-quotes.
+  */
+ #ifdef WIN32
+ #define SYSTEMQUOTE "\""
+ #else
+ #define SYSTEMQUOTE ""
+ #endif
+ 
  /* Portable delay handling */
  extern void pg_usleep(long microsec);


Re: pg_ctl start broken on windows

От
"Gary Doades"
Дата:
On 10 Jun 2004 at 10:45, Andrew Dunstan wrote:
>
> Thanks. The file to patch is src/bin/pg_ctl/pg_ctl.c
>
> We'll need to do a bit more work than this, though. We have 2 cases -
> one where there's a logfile parameter and one where there isn't.  Both
> cases need stdin to be the null device (is that the default if you say
> no handle inheritance?). If there is no logfile parameter then the
> process's stdout and stderr handles need to be those of the calling
> process (i.e. pg_ctl). If there is such a param then the postmaster's
> stdout and stderr both need to be a handle on that file, opened in
> append mode. I believe this needs to be set up in the STARTUPINFO
> (reading MSDN makes my brain hurt).
>
> Not sure of any other wrinkles.
>
> cheers
>
> andrew

Hmm, I have managed to look at the pg_ctl.c code and I can't find any code in there for
the service manager. If pg_ctl is to be run as a true service then there is a *lot* more
work to do on this.

I have used CreateProcess many times, but not redirecting the standard filestreams, I
would have to think about that and do some experimentation. I thought that it might just
be a case of dropping the CreateProcess code in and I could have done that with no
probs.

I believe at first glance that what you want can be done, but I don't think it is trivial.
MSDN can be a real brain killer. Try taking a look at the service code. This is the code
that would need to be put into pg_ctl to respond to the service manager
(SERVICE_CONTROL_STOP, SERVICE_CONTROL_INTERROGATE,
SERVICE_CONTROL_SHUTDOWN at a minimum) This also inherently would make
pg_ctl multi threaded.

Unfortunately I just don't have the few days that I would expect it to take to knock this
into proper shape for a service with full redirection of the filestreams. I don't currently
have anything set up for compiling postgres on my PC. I might be able to have a go at
something next week or I could throw things around with another Windows person.

Regards,
Gary.

Re: pg_ctl start broken on windows

От
Andrew Dunstan
Дата:
Gary Doades wrote:

>On 10 Jun 2004 at 10:45, Andrew Dunstan wrote:
>
>
>>Thanks. The file to patch is src/bin/pg_ctl/pg_ctl.c
>>
>>We'll need to do a bit more work than this, though. We have 2 cases -
>>one where there's a logfile parameter and one where there isn't.  Both
>>cases need stdin to be the null device (is that the default if you say
>>no handle inheritance?). If there is no logfile parameter then the
>>process's stdout and stderr handles need to be those of the calling
>>process (i.e. pg_ctl). If there is such a param then the postmaster's
>>stdout and stderr both need to be a handle on that file, opened in
>>append mode. I believe this needs to be set up in the STARTUPINFO
>>(reading MSDN makes my brain hurt).
>>
>>Not sure of any other wrinkles.
>>
>>cheers
>>
>>andrew
>>
>>
>
>Hmm, I have managed to look at the pg_ctl.c code and I can't find any code in there for
>the service manager. If pg_ctl is to be run as a true service then there is a *lot* more
>work to do on this.
>
>I have used CreateProcess many times, but not redirecting the standard filestreams, I
>would have to think about that and do some experimentation. I thought that it might just
>be a case of dropping the CreateProcess code in and I could have done that with no
>probs.
>
>I believe at first glance that what you want can be done, but I don't think it is trivial.
>MSDN can be a real brain killer. Try taking a look at the service code. This is the code
>that would need to be put into pg_ctl to respond to the service manager
>(SERVICE_CONTROL_STOP, SERVICE_CONTROL_INTERROGATE,
>SERVICE_CONTROL_SHUTDOWN at a minimum) This also inherently would make
>pg_ctl multi threaded.
>
>Unfortunately I just don't have the few days that I would expect it to take to knock this
>into proper shape for a service with full redirection of the filestreams. I don't currently
>have anything set up for compiling postgres on my PC. I might be able to have a go at
>something next week or I could throw things around with another Windows person.
>
>
>

We don't have the service code yet. Right now we are wrestling with
getting pg_ctl to start the postmaster when called from the command line.

cheers

andrew

Re: pg_ctl start broken on windows

От
"Gary Doades"
Дата:
On 10 Jun 2004 at 17:41, Andrew Dunstan wrote:

> >
>
> We don't have the service code yet. Right now we are wrestling with
> getting pg_ctl to start the postmaster when called from the command line.
>
> cheers
>
> andrew
>

OK, I will try and have a go at that bit next week, if no-one else fixes it
in the mean time.

Thanks,
Gary.


Re: pg_ctl start broken on windows

От
Claudio Natoli
Дата:
Bruce Momjian writes:
> I did a lot of poking around in MinGW and found some basic
> rules. First,
> MinGW's system() can't use single quotes for the executable
> name or any
> file used in redirection (as you showed above).  Second, it gets
> confused with multiple double-quoted strings.  The fix is to add a
> double-quote to the beginning and end of the system string,
> so a typical
> system string becomes:
>
>     system("\"\"ls\" 'a' 'b c' > \"out\"\");
>
> Single quotes are fine for arguments.
>
> The applied patch makes this change and documents the behavior.  Would
> folks test these changes on Win32 please?

FWIW, that's pretty much what I've been using.

Cheers,
Claudio

---
Certain disclaimers and policies apply to all email sent from Memetrics.
For the full text of these disclaimers and policies see
<a
href="http://www.memetrics.com/emailpolicy.html">http://www.memetrics.com/em
ailpolicy.html</a>

Re: pg_ctl start broken on windows

От
Bruce Momjian
Дата:
Claudio Natoli wrote:
>
> Bruce Momjian writes:
> > I did a lot of poking around in MinGW and found some basic
> > rules. First,
> > MinGW's system() can't use single quotes for the executable
> > name or any
> > file used in redirection (as you showed above).  Second, it gets
> > confused with multiple double-quoted strings.  The fix is to add a
> > double-quote to the beginning and end of the system string,
> > so a typical
> > system string becomes:
> >
> >     system("\"\"ls\" 'a' 'b c' > \"out\"\");
> >
> > Single quotes are fine for arguments.
> >
> > The applied patch makes this change and documents the behavior.  Would
> > folks test these changes on Win32 please?
>
> FWIW, that's pretty much what I've been using.

Yes, you were the person who originally suggested the extra quotes, and
I found it documented here:

     http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073