pg_ctl using START with paths needing quotes - Mailing list pgsql-patches

From Bruce Momjian
Subject pg_ctl using START with paths needing quotes
Date
Msg-id 200406110100.i5B10eG14036@candle.pha.pa.us
Whole thread Raw
Responses Re: pg_ctl using START with paths needing quotes
List pgsql-patches
This applied patch changes the way pg_ctl starts on Win32.

Using START, it is not possible to quote the executable name, who's
directory might have spaces:

    START /B /program files/x.exe

The fix is to create a temporary batch file in C:\ containing:

    /program files/x.exe

and run START with the batch name:

    START /B C:\PG_CTL_323223.BAT

then unlink the batch file.

--
  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/pg_ctl/pg_ctl.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.c,v
retrieving revision 1.13
diff -c -c -r1.13 pg_ctl.c
*** src/bin/pg_ctl/pg_ctl.c    10 Jun 2004 22:20:53 -0000    1.13
--- src/bin/pg_ctl/pg_ctl.c    11 Jun 2004 00:56:22 -0000
***************
*** 221,226 ****
--- 221,261 ----
       * to pass everything to a shell to process them.
       */
      char        cmd[MAXPGPATH];
+     int            ret;
+     char        *pgexec = postgres_path;
+
+ #ifdef WIN32
+     /*
+      *    Win32 has a problem with the interaction between START and system().
+      *    There is no way to quote the executable name passed to START.
+      *    Therefore, we put the executable name in a temporary batch file
+      *    and run it via START.
+      */
+     char        tmp[MAXPGPATH] = "C:\\PG_CTL_XXXXXX", /* good location? */
+                 bat[MAXPGPATH];
+     int            fd = mkstemp(tmp);
+
+     if (fd == -1)
+     {
+         fprintf(stderr, _("%s: cannot create batch file %s to start server: %s\n"),
+                         progname, tmp, strerror(errno));
+         exit(1);
+     }
+     write(fd, postgres_path, strlen(postgres_path));
+     write(fd, "\n", 1);
+     close(fd);
+
+     strcpy(bat, tmp);
+     strcat(bat, ".BAT");
+     pgexec = bat;
+     if (rename(tmp, bat) == -1)
+     {
+         fprintf(stderr, _("%s: cannot rename batch file %s to %s: %s\n"),
+                         progname, tmp, bat, strerror(errno));
+         unlink(tmp);
+         exit(1);
+     }
+ #endif

      if (log_file != NULL)
          /* Win32 needs START /B rather than "&" */
***************
*** 229,235 ****
  #else
          snprintf(cmd, MAXPGPATH, "%sSTART /B \"%s\" %s < \"%s\" >> \"%s\" 2>&1%s",
  #endif
!                  SYSTEMQUOTE, postgres_path, post_opts, DEVNULL, log_file,
                   SYSTEMQUOTE);
      else
  #ifndef WIN32
--- 264,270 ----
  #else
          snprintf(cmd, MAXPGPATH, "%sSTART /B \"%s\" %s < \"%s\" >> \"%s\" 2>&1%s",
  #endif
!                  SYSTEMQUOTE, pgexec, post_opts, DEVNULL, log_file,
                   SYSTEMQUOTE);
      else
  #ifndef WIN32
***************
*** 237,244 ****
  #else
          snprintf(cmd, MAXPGPATH, "%sSTART /B \"%s\" %s < \"%s\" 2>&1%s",
  #endif
!                  SYSTEMQUOTE, postgres_path, post_opts, DEVNULL, SYSTEMQUOTE);
!     return system(cmd);
  }


--- 272,291 ----
  #else
          snprintf(cmd, MAXPGPATH, "%sSTART /B \"%s\" %s < \"%s\" 2>&1%s",
  #endif
!                  SYSTEMQUOTE, pgexec, post_opts, DEVNULL, SYSTEMQUOTE);
!
!     ret = system(cmd);
!
! #ifdef WIN32
!     /* We are unlinking it while it is running, but that should be OK */
!     if (unlink(bat))
!     {
!         fprintf(stderr, _("%s: cannot remove batch file %s: %s\n"),
!                         progname, bat, strerror(errno));
!         exit(1);
!     }
! #endif
!     return ret;
  }



pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] serverlog function (log_destination file)
Next
From: Bruce Momjian
Date:
Subject: Re: delayed planning of unnamed statements