1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-05-25 18:15:34 +02:00
|
|
|
* findbe.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2004-03-15 17:18:43 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/init/findbe.c,v 1.42 2004/03/15 16:18:43 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2001-10-21 05:43:54 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
1997-08-27 05:48:50 +02:00
|
|
|
#include <sys/stat.h>
|
1996-11-06 11:32:10 +01:00
|
|
|
#include <unistd.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "miscadmin.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef S_IRUSR /* XXX [TRH] should be in a header */
|
|
|
|
#define S_IRUSR S_IREAD
|
|
|
|
#define S_IWUSR S_IWRITE
|
|
|
|
#define S_IXUSR S_IEXEC
|
|
|
|
#define S_IRGRP ((S_IRUSR)>>3)
|
|
|
|
#define S_IWGRP ((S_IWUSR)>>3)
|
|
|
|
#define S_IXGRP ((S_IXUSR)>>3)
|
|
|
|
#define S_IROTH ((S_IRUSR)>>6)
|
|
|
|
#define S_IWOTH ((S_IWUSR)>>6)
|
|
|
|
#define S_IXOTH ((S_IXUSR)>>6)
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
|
|
|
|
2000-06-09 00:38:00 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1998-06-09 19:13:06 +02:00
|
|
|
* ValidateBinary -- validate "path" as a POSTMASTER/POSTGRES executable file
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* returns 0 if the file is found and no error is encountered.
|
1997-09-07 07:04:48 +02:00
|
|
|
* -1 if the regular file "path" does not exist or cannot be executed.
|
|
|
|
* -2 if the file is otherwise valid but cannot be read.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-09 00:38:00 +02:00
|
|
|
static int
|
1998-06-09 19:13:06 +02:00
|
|
|
ValidateBinary(char *path)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
struct stat buf;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-05-15 18:35:30 +02:00
|
|
|
#ifndef WIN32
|
1997-09-08 04:41:22 +02:00
|
|
|
uid_t euid;
|
|
|
|
struct group *gp;
|
|
|
|
struct passwd *pwp;
|
2004-03-15 17:18:43 +01:00
|
|
|
int i;
|
|
|
|
int in_grp = 0;
|
2003-11-12 01:04:10 +01:00
|
|
|
#else
|
2003-11-11 04:53:33 +01:00
|
|
|
char path_exe[MAXPGPATH + 2 + strlen(".exe")];
|
2003-05-15 18:35:30 +02:00
|
|
|
#endif
|
1997-09-08 04:41:22 +02:00
|
|
|
int is_r = 0;
|
|
|
|
int is_x = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-11-11 04:53:33 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
/* Win32 requires a .exe suffix for stat() */
|
|
|
|
if (strlen(path) >= 4 && strcmp(path + strlen(path) - strlen(".exe"), ".exe") != 0)
|
|
|
|
{
|
|
|
|
strcpy(path_exe, path);
|
|
|
|
strcat(path_exe, ".exe");
|
|
|
|
path = path_exe;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* Ensure that the file exists and is a regular file.
|
|
|
|
*
|
|
|
|
* XXX if you have a broken system where stat() looks at the symlink
|
|
|
|
* instead of the underlying file, you lose.
|
|
|
|
*/
|
|
|
|
if (stat(path, &buf) < 0)
|
|
|
|
{
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG3, "could not stat \"%s\": %m", path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2001-05-09 21:28:31 +02:00
|
|
|
|
|
|
|
if ((buf.st_mode & S_IFMT) != S_IFREG)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG3, "\"%s\" is not a regular file", path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that we are using an authorized backend.
|
|
|
|
*
|
|
|
|
* XXX I'm open to suggestions here. I would like to enforce ownership
|
1998-06-09 19:13:06 +02:00
|
|
|
* of binaries by user "postgres" but people seem to like to run as
|
1997-09-07 07:04:48 +02:00
|
|
|
* users other than "postgres"...
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the file is both executable and readable (required for
|
|
|
|
* dynamic loading).
|
|
|
|
*/
|
2003-05-15 18:35:30 +02:00
|
|
|
#ifdef WIN32
|
2003-08-04 02:43:34 +02:00
|
|
|
is_r = buf.st_mode & S_IRUSR;
|
|
|
|
is_x = buf.st_mode & S_IXUSR;
|
|
|
|
return is_x ? (is_r ? 0 : -2) : -1;
|
2003-05-15 18:35:30 +02:00
|
|
|
#else
|
1997-09-07 07:04:48 +02:00
|
|
|
euid = geteuid();
|
|
|
|
if (euid == buf.st_uid)
|
|
|
|
{
|
|
|
|
is_r = buf.st_mode & S_IRUSR;
|
|
|
|
is_x = buf.st_mode & S_IXUSR;
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
if (!(is_r && is_x))
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG3, "\"%s\" is not user read/execute", path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return is_x ? (is_r ? 0 : -2) : -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
pwp = getpwuid(euid);
|
|
|
|
if (pwp)
|
|
|
|
{
|
|
|
|
if (pwp->pw_gid == buf.st_gid)
|
|
|
|
++in_grp;
|
|
|
|
else if (pwp->pw_name &&
|
2001-10-29 19:06:54 +01:00
|
|
|
(gp = getgrgid(buf.st_gid)) != NULL &&
|
|
|
|
gp->gr_mem != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
for (i = 0; gp->gr_mem[i]; ++i)
|
|
|
|
{
|
|
|
|
if (!strcmp(gp->gr_mem[i], pwp->pw_name))
|
|
|
|
{
|
|
|
|
++in_grp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in_grp)
|
|
|
|
{
|
|
|
|
is_r = buf.st_mode & S_IRGRP;
|
|
|
|
is_x = buf.st_mode & S_IXGRP;
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
if (!(is_r && is_x))
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG3, "\"%s\" is not group read/execute", path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return is_x ? (is_r ? 0 : -2) : -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
is_r = buf.st_mode & S_IROTH;
|
|
|
|
is_x = buf.st_mode & S_IXOTH;
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
if (!(is_r && is_x))
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG3, "\"%s\" is not other read/execute", path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return is_x ? (is_r ? 0 : -2) : -1;
|
2003-05-15 18:35:30 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-06-09 00:28:30 +02:00
|
|
|
* FindExec -- find an absolute path to a valid backend executable
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* The reason we have to work so hard to find an absolute path is that
|
2001-10-21 05:43:54 +02:00
|
|
|
* on some platforms we can't do dynamic loading unless we know the
|
|
|
|
* executable's location. Also, we need a full path not a relative
|
|
|
|
* path because we will later change working directory.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
2000-07-09 15:14:19 +02:00
|
|
|
FindExec(char *full_path, const char *argv0, const char *binary_name)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char buf[MAXPGPATH + 2];
|
|
|
|
char *p;
|
|
|
|
char *path,
|
|
|
|
*startp,
|
|
|
|
*endp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1998-09-01 06:40:42 +02:00
|
|
|
* for the postmaster: First try: use the binary that's located in the
|
|
|
|
* same directory as the postmaster, if it was invoked with an
|
1997-09-07 07:04:48 +02:00
|
|
|
* explicit path. Presumably the user used an explicit path because it
|
|
|
|
* wasn't in PATH, and we don't want to use incompatible executables.
|
|
|
|
*
|
|
|
|
* This has the neat property that it works for installed binaries, old
|
|
|
|
* source trees (obj/support/post{master,gres}) and new marc source
|
|
|
|
* trees (obj/post{master,gres}) because they all put the two binaries
|
|
|
|
* in the same place.
|
|
|
|
*
|
1998-09-01 06:40:42 +02:00
|
|
|
* for the binary: First try: if we're given some kind of path, use it
|
|
|
|
* (making sure that a relative path is made absolute before returning
|
|
|
|
* it).
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2003-04-04 22:42:13 +02:00
|
|
|
if (argv0 && (p = last_path_separator(argv0)) && *++p)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-04-04 22:42:13 +02:00
|
|
|
if (is_absolute_path(argv0) || !getcwd(buf, MAXPGPATH))
|
1997-09-07 07:04:48 +02:00
|
|
|
buf[0] = '\0';
|
|
|
|
else
|
|
|
|
strcat(buf, "/");
|
|
|
|
strcat(buf, argv0);
|
2003-04-04 22:42:13 +02:00
|
|
|
p = last_path_separator(buf);
|
1998-06-09 19:13:06 +02:00
|
|
|
strcpy(++p, binary_name);
|
2001-10-21 05:43:54 +02:00
|
|
|
if (ValidateBinary(buf) == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-06-09 19:13:06 +02:00
|
|
|
strncpy(full_path, buf, MAXPGPATH);
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG2, "found \"%s\" using argv[0]", full_path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG2, "invalid binary \"%s\"", buf);
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Second try: since no explicit path was supplied, the user must have
|
|
|
|
* been relying on PATH. We'll use the same PATH.
|
|
|
|
*/
|
|
|
|
if ((p = getenv("PATH")) && *p)
|
|
|
|
{
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG2, "searching PATH for executable");
|
2001-10-21 05:43:54 +02:00
|
|
|
path = strdup(p); /* make a modifiable copy */
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 01:11:31 +01:00
|
|
|
for (startp = path, endp = strchr(path, PATHSEP);
|
1997-09-07 07:04:48 +02:00
|
|
|
startp && *startp;
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 01:11:31 +01:00
|
|
|
startp = endp + 1, endp = strchr(startp, PATHSEP))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
if (startp == endp) /* it's a "::" */
|
|
|
|
continue;
|
|
|
|
if (endp)
|
|
|
|
*endp = '\0';
|
2003-04-04 22:42:13 +02:00
|
|
|
if (is_absolute_path(startp) || !getcwd(buf, MAXPGPATH))
|
1997-09-07 07:04:48 +02:00
|
|
|
buf[0] = '\0';
|
2001-10-21 05:43:54 +02:00
|
|
|
else
|
|
|
|
strcat(buf, "/");
|
1997-09-07 07:04:48 +02:00
|
|
|
strcat(buf, startp);
|
1998-06-09 19:13:06 +02:00
|
|
|
strcat(buf, "/");
|
|
|
|
strcat(buf, binary_name);
|
|
|
|
switch (ValidateBinary(buf))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 0: /* found ok */
|
1998-06-09 19:13:06 +02:00
|
|
|
strncpy(full_path, buf, MAXPGPATH);
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG2, "found \"%s\" using PATH", full_path);
|
1997-09-08 04:41:22 +02:00
|
|
|
free(path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1997-09-08 04:41:22 +02:00
|
|
|
case -1: /* wasn't even a candidate, keep looking */
|
|
|
|
break;
|
|
|
|
case -2: /* found but disqualified */
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG2, "could not read binary \"%s\"", buf);
|
1997-09-08 04:41:22 +02:00
|
|
|
free(path);
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if (!endp) /* last one */
|
|
|
|
break;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
2003-07-25 22:18:01 +02:00
|
|
|
elog(DEBUG2, "could not find a \"%s\" to execute", binary_name);
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|