From a9fad44372881bc3fd1a5d194d6b3f80c54ceae0 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 18 May 2004 20:18:59 +0000
Subject: [PATCH] Move find_my_exec() way up into main.c so it is available to
 the timezone code and other places.

Remove elog() calls from find_my_exec;  do fprintf(stderr) instead.  We
can then remove the exec.c handling in the makefile because it doesn't
have to be built to suppress elog calls.
---
 src/backend/main/main.c              | 10 +++-
 src/backend/postmaster/postmaster.c  | 11 +---
 src/backend/tcop/postgres.c          | 15 ++---
 src/bin/initdb/Makefile              | 10 +---
 src/bin/pg_dump/Makefile             | 12 ++--
 src/bin/psql/Makefile                | 10 +---
 src/bin/psql/print.c                 |  5 +-
 src/interfaces/ecpg/preproc/Makefile | 10 +---
 src/port/exec.c                      | 82 ++++++++++++++++++++--------
 src/port/pipe.c                      | 43 +--------------
 10 files changed, 92 insertions(+), 116 deletions(-)

diff --git a/src/backend/main/main.c b/src/backend/main/main.c
index b0b394fe8b9..e735f487b87 100644
--- a/src/backend/main/main.c
+++ b/src/backend/main/main.c
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/main/main.c,v 1.77 2004/04/19 17:42:57 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/main/main.c,v 1.78 2004/05/18 20:18:57 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -241,6 +241,14 @@ main(int argc, char *argv[])
 #endif   /* !WIN32 */
 	}
 
+	if (find_my_exec(argv[0], my_exec_path) < 0)
+	{
+		fprintf(stderr,
+				gettext("%s: could not locate my own executable path"),
+						argv[0]);
+		exit(1);
+	}
+	
 	/*
 	 * Now dispatch to one of PostmasterMain, PostgresMain, GucInfoMain,
 	 * SubPostmasterMain, pgstat_main, pgstat_mainChild or BootstrapMain
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index c37cb259df2..c9a0d737c44 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.388 2004/05/17 14:35:29 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.389 2004/05/18 20:18:57 momjian Exp $
  *
  * NOTES
  *
@@ -295,8 +295,6 @@ __attribute__((format(printf, 1, 2)));
 
 #ifdef EXEC_BACKEND
 
-static char	postgres_exec_path[MAXPGPATH];
-
 #ifdef WIN32
 pid_t win32_forkexec(const char* path, char *argv[]);
 
@@ -690,13 +688,6 @@ PostmasterMain(int argc, char *argv[])
 				(errmsg_internal("-----------------------------------------")));
 	}
 
-	/*
-	 * On some systems our dynloader code needs the executable's pathname.
-	 */
-	if (find_my_exec(argv[0], my_exec_path) < 0)
-		ereport(FATAL,
-				(errmsg("%s: could not locate my own executable path",
-						progname)));
 	if (strlen(pkglib_path) == 0)
 		get_pkglib_path(my_exec_path, pkglib_path);
 
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index f06340a54e4..77fc42e0a54 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.407 2004/05/17 14:35:31 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.408 2004/05/18 20:18:58 momjian Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -2549,6 +2549,9 @@ PostgresMain(int argc, char *argv[], const char *username)
 	}
 	Assert(DataDir);
 
+	if (strlen(pkglib_path) == 0)
+		get_pkglib_path(my_exec_path, pkglib_path);
+			
 	/* Acquire configuration parameters */
 	if (IsUnderPostmaster)
 	{
@@ -2645,16 +2648,6 @@ PostgresMain(int argc, char *argv[], const char *username)
 							argv[0])));
 		}
 
-		/*
-		 * On some systems our dynloader code needs the executable's pathname.
-		 */
-		if (strlen(my_exec_path) == 0 && find_my_exec(argv[0], my_exec_path) < 0)
-			ereport(FATAL,
-					(errmsg("%s: could not locate postgres executable",
-							argv[0])));
-		if (strlen(pkglib_path) == 0)
-			get_pkglib_path(my_exec_path, pkglib_path);
-			
 		/*
 		 * Validate we have been given a reasonable-looking DataDir (if
 		 * under postmaster, assume postmaster did this already).
diff --git a/src/bin/initdb/Makefile b/src/bin/initdb/Makefile
index 3fcdccfa4a3..0df36c62562 100644
--- a/src/bin/initdb/Makefile
+++ b/src/bin/initdb/Makefile
@@ -5,7 +5,7 @@
 # Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/bin/initdb/Makefile,v 1.38 2004/05/17 14:35:33 momjian Exp $
+# $PostgreSQL: pgsql/src/bin/initdb/Makefile,v 1.39 2004/05/18 20:18:58 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -15,17 +15,13 @@ include $(top_builddir)/src/Makefile.global
 
 override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) $(CPPFLAGS)
 
-OBJS=	initdb.o \
-	$(filter exec.o, $(LIBOBJS))
+OBJS=	initdb.o
 
 all: submake-libpq submake-libpgport initdb
 
 initdb: $(OBJS) $(libpq_builddir)/libpq.a
 	$(CC) $(CFLAGS) $(OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@$(X)
 
-exec.c: % : $(top_srcdir)/src/port/%
-	rm -f $@ && $(LN_S) $< .
-
 install: all installdirs
 	$(INSTALL_PROGRAM) initdb$(X) $(DESTDIR)$(bindir)/initdb$(X)
 
@@ -36,7 +32,7 @@ uninstall:
 	rm -f $(DESTDIR)$(bindir)/initdb$(X)
 
 clean distclean maintainer-clean:
-	rm -f initdb$(X) $(OBJS) exec.c
+	rm -f initdb$(X) $(OBJS)
 
 
 # ensure that changes in datadir propagate into object file
diff --git a/src/bin/pg_dump/Makefile b/src/bin/pg_dump/Makefile
index 414194cd30b..23d1890f1ce 100644
--- a/src/bin/pg_dump/Makefile
+++ b/src/bin/pg_dump/Makefile
@@ -5,7 +5,7 @@
 # Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/bin/pg_dump/Makefile,v 1.47 2004/05/11 21:57:14 momjian Exp $
+# $PostgreSQL: pgsql/src/bin/pg_dump/Makefile,v 1.48 2004/05/18 20:18:58 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -18,7 +18,6 @@ override CPPFLAGS += -DFRONTEND
 OBJS=	pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o \
 	pg_backup_files.o pg_backup_null.o pg_backup_tar.o \
 	dumputils.o
-PG_DUMPALL_OBJS =	$(filter exec.o, $(LIBOBJS))
 
 EXTRA_OBJS = $(top_builddir)/src/backend/parser/keywords.o
 
@@ -33,11 +32,8 @@ pg_dump: pg_dump.o common.o pg_dump_sort.o $(OBJS) $(libpq_builddir)/libpq.a
 pg_restore: pg_restore.o $(OBJS) $(libpq_builddir)/libpq.a
 	$(CC) $(CFLAGS) pg_restore.o $(OBJS) $(EXTRA_OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@$(X)
 
-pg_dumpall: pg_dumpall.o dumputils.o $(PG_DUMPALL_OBJS) $(libpq_builddir)/libpq.a
-	$(CC) $(CFLAGS) pg_dumpall.o dumputils.o $(PG_DUMPALL_OBJS) $(EXTRA_OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@$(X)
-
-exec.c: % : $(top_srcdir)/src/port/%
-	rm -f $@ && $(LN_S) $< .
+pg_dumpall: pg_dumpall.o dumputils.o $(libpq_builddir)/libpq.a
+	$(CC) $(CFLAGS) pg_dumpall.o dumputils.o $(EXTRA_OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@$(X)
 
 .PHONY: submake-backend
 submake-backend:
@@ -56,4 +52,4 @@ uninstall:
 	rm -f $(addprefix $(DESTDIR)$(bindir)/, pg_dump$(X) pg_restore$(X) pg_dumpall$(X))
 
 clean distclean maintainer-clean:
-	rm -f pg_dump$(X) pg_restore$(X) pg_dumpall$(X) $(OBJS) pg_dump.o common.o pg_dump_sort.o pg_restore.o pg_dumpall.o exec.c
+	rm -f pg_dump$(X) pg_restore$(X) pg_dumpall$(X) $(OBJS) pg_dump.o common.o pg_dump_sort.o pg_restore.o pg_dumpall.o
diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile
index 44a66b03783..46ee47161bc 100644
--- a/src/bin/psql/Makefile
+++ b/src/bin/psql/Makefile
@@ -5,7 +5,7 @@
 # Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/bin/psql/Makefile,v 1.44 2004/05/17 14:35:33 momjian Exp $
+# $PostgreSQL: pgsql/src/bin/psql/Makefile,v 1.45 2004/05/18 20:18:58 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -19,8 +19,7 @@ override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) -DFRONTEND
 
 OBJS=	command.o common.o help.o input.o stringutils.o mainloop.o copy.o \
 	startup.o prompt.o variables.o large_obj.o print.o describe.o \
-	psqlscan.o tab-complete.o mbprint.o \
-	$(filter exec.o, $(LIBOBJS))
+	psqlscan.o tab-complete.o mbprint.o
 
 FLEXFLAGS = -Cfe
 
@@ -30,9 +29,6 @@ all: submake-libpq submake-libpgport psql
 psql: $(OBJS) $(libpq_builddir)/libpq.a
 	$(CC) $(CFLAGS) $(OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@$(X)
 
-exec.c: % : $(top_srcdir)/src/port/%
-	rm -f $@ && $(LN_S) $< .
-
 help.o: $(srcdir)/sql_help.h
 
 ifdef PERL
@@ -64,7 +60,7 @@ uninstall:
 
 # psqlscan.c is in the distribution tarball, so is not cleaned here
 clean distclean:
-	rm -f psql$(X) $(OBJS) exec.c
+	rm -f psql$(X) $(OBJS)
 
 maintainer-clean: distclean
 	rm -f $(srcdir)/sql_help.h $(srcdir)/psqlscan.c
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 9dd493201c9..229b4e0024e 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.46 2004/01/24 20:43:26 neilc Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.47 2004/05/18 20:18:58 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "common.h"
@@ -388,7 +388,10 @@ print_aligned_text(const char *title, const char *const * headers,
 		for (ptr = footers; *ptr; ptr++)
 			fprintf(fout, "%s\n", *ptr);
 
+#ifndef __MINGW32__
+	/* for some reason MinGW outputs an extra newline, so this supresses it */	
 	fputc('\n', fout);
+#endif
 
 	/* clean up */
 	free(cell_w);
diff --git a/src/interfaces/ecpg/preproc/Makefile b/src/interfaces/ecpg/preproc/Makefile
index 51f5e4c9f1f..05ebe74cd0a 100644
--- a/src/interfaces/ecpg/preproc/Makefile
+++ b/src/interfaces/ecpg/preproc/Makefile
@@ -1,4 +1,4 @@
-# $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/Makefile,v 1.104 2004/05/17 14:35:34 momjian Exp $
+# $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/Makefile,v 1.105 2004/05/18 20:18:58 momjian Exp $
 
 subdir = src/interfaces/ecpg/preproc
 top_builddir = ../../../..
@@ -19,17 +19,13 @@ endif
 override CFLAGS += $(PTHREAD_CFLAGS)
 
 OBJS=	preproc.o type.o ecpg.o ecpg_keywords.o output.o\
-	keywords.o c_keywords.o ../ecpglib/typename.o descriptor.o variable.o \
-	$(filter exec.o, $(LIBOBJS))
+	keywords.o c_keywords.o ../ecpglib/typename.o descriptor.o variable.o
 
 all: submake-libpgport ecpg
 
 ecpg: $(OBJS)
 	$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LIBS) $(PTHREAD_LIBS) -o $@$(X)
 
-exec.c: % : $(top_srcdir)/src/port/%
-	rm -f $@ && $(LN_S) $< .
-
 # pgc is compiled as part of preproc
 preproc.o: $(srcdir)/pgc.c
 
@@ -66,7 +62,7 @@ uninstall:
 	rm -f $(DESTDIR)$(bindir)/ecpg$(X)
 
 clean distclean:
-	rm -f *.o ecpg$(X) exec.c
+	rm -f *.o ecpg$(X)
 # garbage from partial builds
 	@rm -f y.tab.c y.tab.h
 # garbage from development
diff --git a/src/port/exec.c b/src/port/exec.c
index f98acda3984..baa655c2ff0 100644
--- a/src/port/exec.c
+++ b/src/port/exec.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/exec.c,v 1.7 2004/05/18 03:36:45 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/port/exec.c,v 1.8 2004/05/18 20:18:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -23,6 +23,10 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <sys/wait.h>
+
+#define _(x) gettext((x))
+
 #include "miscadmin.h"
 
 /* $PATH (or %PATH%) path separator */
@@ -44,13 +48,6 @@
 #define S_IXOTH		 ((S_IXUSR)>>6)
 #endif
 
-#ifndef FRONTEND
-/* We use only 3-parameter elog calls in this file, for simplicity */
-#define log_debug(str, param)	elog(DEBUG2, str, param)
-#else
-#define log_debug(str, param)	{}	/* do nothing */
-#endif
-
 static void win32_make_absolute(char *path);
 
 /*
@@ -96,13 +93,13 @@ validate_exec(char *path)
 	 */
 	if (stat(path, &buf) < 0)
 	{
-		log_debug("could not stat \"%s\": %m", path);
+		fprintf(stderr, "could not stat \"%s\": %m", path);
 		return -1;
 	}
 
 	if ((buf.st_mode & S_IFMT) != S_IFREG)
 	{
-		log_debug("\"%s\" is not a regular file", path);
+		fprintf(stderr, "\"%s\" is not a regular file", path);
 		return -1;
 	}
 
@@ -127,7 +124,7 @@ validate_exec(char *path)
 		is_r = buf.st_mode & S_IRUSR;
 		is_x = buf.st_mode & S_IXUSR;
 		if (!(is_r && is_x))
-			log_debug("\"%s\" is not user read/execute", path);
+			fprintf(stderr, "\"%s\" is not user read/execute", path);
 		return is_x ? (is_r ? 0 : -2) : -1;
 	}
 
@@ -156,7 +153,7 @@ validate_exec(char *path)
 			is_r = buf.st_mode & S_IRGRP;
 			is_x = buf.st_mode & S_IXGRP;
 			if (!(is_r && is_x))
-				log_debug("\"%s\" is not group read/execute", path);
+				fprintf(stderr, "\"%s\" is not group read/execute", path);
 			return is_x ? (is_r ? 0 : -2) : -1;
 		}
 	}
@@ -165,7 +162,7 @@ validate_exec(char *path)
 	is_r = buf.st_mode & S_IROTH;
 	is_x = buf.st_mode & S_IXOTH;
 	if (!(is_r && is_x))
-		log_debug("\"%s\" is not other read/execute", path);
+		fprintf(stderr, "\"%s\" is not other read/execute", path);
 	return is_x ? (is_r ? 0 : -2) : -1;
 
 #endif
@@ -207,7 +204,7 @@ find_my_exec(const char *argv0, char *full_path)
 	{
 		if (*++p == '\0')
 		{
-			log_debug("argv[0] ends with a path separator \"%s\"", argv0);
+			fprintf(stderr, "argv[0] ends with a path separator \"%s\"", argv0);
 			return -1;
 		}
 		if (is_absolute_path(argv0) || !getcwd(buf, MAXPGPATH))
@@ -219,11 +216,13 @@ find_my_exec(const char *argv0, char *full_path)
 		{
 			strncpy(full_path, buf, MAXPGPATH);
 			win32_make_absolute(full_path);
-			log_debug("found \"%s\" using argv[0]", full_path);
 			return 0;
 		}
-		log_debug("invalid binary \"%s\"", buf);
-		return -1;
+		else
+		{
+			fprintf(stderr, "invalid binary \"%s\"", buf);
+			return -1;
+		}
 	}
 
 	/*
@@ -232,7 +231,6 @@ find_my_exec(const char *argv0, char *full_path)
 	 */
 	if ((p = getenv("PATH")) && *p)
 	{
-		log_debug("searching PATH for executable%s", "");
 		path = strdup(p);		/* make a modifiable copy */
 		for (startp = path, endp = strchr(path, PATHSEP);
 			 startp && *startp;
@@ -254,13 +252,12 @@ find_my_exec(const char *argv0, char *full_path)
 				case 0: /* found ok */
 					strncpy(full_path, buf, MAXPGPATH);
 					win32_make_absolute(full_path);
-					log_debug("found \"%s\" using PATH", full_path);
 					free(path);
 					return 0;
 				case -1:		/* wasn't even a candidate, keep looking */
 					break;
 				case -2:		/* found but disqualified */
-					log_debug("could not read binary \"%s\"", buf);
+					fprintf(stderr, "could not read binary \"%s\"", buf);
 					free(path);
 					return -1;
 			}
@@ -270,7 +267,7 @@ find_my_exec(const char *argv0, char *full_path)
 		free(path);
 	}
 
-	log_debug("could not find a \"%s\" to execute", argv0);
+	fprintf(stderr, "could not find a \"%s\" to execute", argv0);
 	return -1;
 
 #if 0
@@ -330,6 +327,46 @@ int find_other_exec(const char *argv0, char const *target,
 }
 
 
+/*
+ * pclose() plus useful error reporting
+ * Is this necessary?  bjm 2004-05-11
+ * It is better here because pipe.c has win32 backend linkage.
+ */
+int
+pclose_check(FILE *stream)
+{
+	int		exitstatus;
+
+	exitstatus = pclose(stream);
+
+	if (exitstatus == 0)
+		return 0;					/* all is well */
+
+	if (exitstatus == -1)
+	{
+		/* pclose() itself failed, and hopefully set errno */
+		perror("pclose failed");
+	}
+	else if (WIFEXITED(exitstatus))
+	{
+		fprintf(stderr, _("child process exited with exit code %d\n"),
+				WEXITSTATUS(exitstatus));
+	}
+	else if (WIFSIGNALED(exitstatus))
+	{
+		fprintf(stderr, _("child process was terminated by signal %d\n"),
+				WTERMSIG(exitstatus));
+	}
+	else
+	{
+		fprintf(stderr, _("child process exited with unrecognized status %d\n"),
+				exitstatus);
+	}
+
+	return -1;
+}
+
+
 /*
  * Windows doesn't like relative paths to executables (other things work fine)
  * so we call its builtin function to expand them. Elsewhere this is a NOOP
@@ -344,7 +381,7 @@ win32_make_absolute(char *path)
 
 	if (_fullpath(abspath, path, MAXPGPATH) == NULL)
 	{
-		log_debug("Win32 path expansion failed:  %s", strerror(errno));
+		fprintf(stderr, "Win32 path expansion failed:  %s", strerror(errno));
 		StrNCpy(abspath, path, MAXPGPATH);
 	}
 	canonicalize_path(abspath);
@@ -354,3 +391,4 @@ win32_make_absolute(char *path)
 	return;
 }
 
+
diff --git a/src/port/pipe.c b/src/port/pipe.c
index 9fdab8ca738..9bbef1164e7 100644
--- a/src/port/pipe.c
+++ b/src/port/pipe.c
@@ -10,17 +10,13 @@
  *	must be replaced with recv/send.
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/pipe.c,v 1.3 2004/05/11 21:57:15 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/port/pipe.c,v 1.4 2004/05/18 20:18:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "postgres.h"
 
-#include <sys/wait.h>
-
-#define _(x) gettext((x))
-
 #ifdef WIN32
 int
 pgpipe(int handles[2])
@@ -70,40 +66,3 @@ int piperead(int s, char* buf, int len)
 }
 #endif
 
-/*
- * pclose() plus useful error reporting
- * Is this necessary?  bjm 2004-05-11
- */
-int
-pclose_check(FILE *stream)
-{
-	int		exitstatus;
-
-	exitstatus = pclose(stream);
-
-	if (exitstatus == 0)
-		return 0;					/* all is well */
-
-	if (exitstatus == -1)
-	{
-		/* pclose() itself failed, and hopefully set errno */
-		perror("pclose failed");
-	}
-	else if (WIFEXITED(exitstatus))
-	{
-		fprintf(stderr, _("child process exited with exit code %d\n"),
-				WEXITSTATUS(exitstatus));
-	}
-	else if (WIFSIGNALED(exitstatus))
-	{
-		fprintf(stderr, _("child process was terminated by signal %d\n"),
-				WTERMSIG(exitstatus));
-	}
-	else
-	{
-		fprintf(stderr, _("child process exited with unrecognized status %d\n"),
-				exitstatus);
-	}
-
-	return -1;
-}
-- 
GitLab