Header And Logo

PostgreSQL
| The world's most advanced open source database.

miscinit.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * miscinit.c
00004  *    miscellaneous initialization support stuff
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/utils/init/miscinit.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 #include "postgres.h"
00016 
00017 #include <sys/param.h>
00018 #include <signal.h>
00019 #include <sys/file.h>
00020 #include <sys/stat.h>
00021 #include <sys/time.h>
00022 #include <fcntl.h>
00023 #include <unistd.h>
00024 #include <grp.h>
00025 #include <pwd.h>
00026 #include <netinet/in.h>
00027 #include <arpa/inet.h>
00028 #ifdef HAVE_UTIME_H
00029 #include <utime.h>
00030 #endif
00031 
00032 #include "access/htup_details.h"
00033 #include "catalog/pg_authid.h"
00034 #include "mb/pg_wchar.h"
00035 #include "miscadmin.h"
00036 #include "postmaster/autovacuum.h"
00037 #include "postmaster/postmaster.h"
00038 #include "storage/fd.h"
00039 #include "storage/ipc.h"
00040 #include "storage/pg_shmem.h"
00041 #include "storage/proc.h"
00042 #include "storage/procarray.h"
00043 #include "utils/builtins.h"
00044 #include "utils/guc.h"
00045 #include "utils/memutils.h"
00046 #include "utils/syscache.h"
00047 
00048 
00049 #define DIRECTORY_LOCK_FILE     "postmaster.pid"
00050 
00051 ProcessingMode Mode = InitProcessing;
00052 
00053 /* List of lock files to be removed at proc exit */
00054 static List *lock_files = NIL;
00055 
00056 
00057 /* ----------------------------------------------------------------
00058  *      ignoring system indexes support stuff
00059  *
00060  * NOTE: "ignoring system indexes" means we do not use the system indexes
00061  * for lookups (either in hardwired catalog accesses or in planner-generated
00062  * plans).  We do, however, still update the indexes when a catalog
00063  * modification is made.
00064  * ----------------------------------------------------------------
00065  */
00066 
00067 bool        IgnoreSystemIndexes = false;
00068 
00069 
00070 /* ----------------------------------------------------------------
00071  *              database path / name support stuff
00072  * ----------------------------------------------------------------
00073  */
00074 
00075 void
00076 SetDatabasePath(const char *path)
00077 {
00078     /* This should happen only once per process */
00079     Assert(!DatabasePath);
00080     DatabasePath = MemoryContextStrdup(TopMemoryContext, path);
00081 }
00082 
00083 /*
00084  * Set data directory, but make sure it's an absolute path.  Use this,
00085  * never set DataDir directly.
00086  */
00087 void
00088 SetDataDir(const char *dir)
00089 {
00090     char       *new;
00091 
00092     AssertArg(dir);
00093 
00094     /* If presented path is relative, convert to absolute */
00095     new = make_absolute_path(dir);
00096 
00097     if (DataDir)
00098         free(DataDir);
00099     DataDir = new;
00100 }
00101 
00102 /*
00103  * Change working directory to DataDir.  Most of the postmaster and backend
00104  * code assumes that we are in DataDir so it can use relative paths to access
00105  * stuff in and under the data directory.  For convenience during path
00106  * setup, however, we don't force the chdir to occur during SetDataDir.
00107  */
00108 void
00109 ChangeToDataDir(void)
00110 {
00111     AssertState(DataDir);
00112 
00113     if (chdir(DataDir) < 0)
00114         ereport(FATAL,
00115                 (errcode_for_file_access(),
00116                  errmsg("could not change directory to \"%s\": %m",
00117                         DataDir)));
00118 }
00119 
00120 /*
00121  * If the given pathname isn't already absolute, make it so, interpreting
00122  * it relative to the current working directory.
00123  *
00124  * Also canonicalizes the path.  The result is always a malloc'd copy.
00125  *
00126  * Note: interpretation of relative-path arguments during postmaster startup
00127  * should happen before doing ChangeToDataDir(), else the user will probably
00128  * not like the results.
00129  */
00130 char *
00131 make_absolute_path(const char *path)
00132 {
00133     char       *new;
00134 
00135     /* Returning null for null input is convenient for some callers */
00136     if (path == NULL)
00137         return NULL;
00138 
00139     if (!is_absolute_path(path))
00140     {
00141         char       *buf;
00142         size_t      buflen;
00143 
00144         buflen = MAXPGPATH;
00145         for (;;)
00146         {
00147             buf = malloc(buflen);
00148             if (!buf)
00149                 ereport(FATAL,
00150                         (errcode(ERRCODE_OUT_OF_MEMORY),
00151                          errmsg("out of memory")));
00152 
00153             if (getcwd(buf, buflen))
00154                 break;
00155             else if (errno == ERANGE)
00156             {
00157                 free(buf);
00158                 buflen *= 2;
00159                 continue;
00160             }
00161             else
00162             {
00163                 free(buf);
00164                 elog(FATAL, "could not get current working directory: %m");
00165             }
00166         }
00167 
00168         new = malloc(strlen(buf) + strlen(path) + 2);
00169         if (!new)
00170             ereport(FATAL,
00171                     (errcode(ERRCODE_OUT_OF_MEMORY),
00172                      errmsg("out of memory")));
00173         sprintf(new, "%s/%s", buf, path);
00174         free(buf);
00175     }
00176     else
00177     {
00178         new = strdup(path);
00179         if (!new)
00180             ereport(FATAL,
00181                     (errcode(ERRCODE_OUT_OF_MEMORY),
00182                      errmsg("out of memory")));
00183     }
00184 
00185     /* Make sure punctuation is canonical, too */
00186     canonicalize_path(new);
00187 
00188     return new;
00189 }
00190 
00191 
00192 /* ----------------------------------------------------------------
00193  *  User ID state
00194  *
00195  * We have to track several different values associated with the concept
00196  * of "user ID".
00197  *
00198  * AuthenticatedUserId is determined at connection start and never changes.
00199  *
00200  * SessionUserId is initially the same as AuthenticatedUserId, but can be
00201  * changed by SET SESSION AUTHORIZATION (if AuthenticatedUserIsSuperuser).
00202  * This is the ID reported by the SESSION_USER SQL function.
00203  *
00204  * OuterUserId is the current user ID in effect at the "outer level" (outside
00205  * any transaction or function).  This is initially the same as SessionUserId,
00206  * but can be changed by SET ROLE to any role that SessionUserId is a
00207  * member of.  (XXX rename to something like CurrentRoleId?)
00208  *
00209  * CurrentUserId is the current effective user ID; this is the one to use
00210  * for all normal permissions-checking purposes.  At outer level this will
00211  * be the same as OuterUserId, but it changes during calls to SECURITY
00212  * DEFINER functions, as well as locally in some specialized commands.
00213  *
00214  * SecurityRestrictionContext holds flags indicating reason(s) for changing
00215  * CurrentUserId.  In some cases we need to lock down operations that are
00216  * not directly controlled by privilege settings, and this provides a
00217  * convenient way to do it.
00218  * ----------------------------------------------------------------
00219  */
00220 static Oid  AuthenticatedUserId = InvalidOid;
00221 static Oid  SessionUserId = InvalidOid;
00222 static Oid  OuterUserId = InvalidOid;
00223 static Oid  CurrentUserId = InvalidOid;
00224 
00225 /* We also have to remember the superuser state of some of these levels */
00226 static bool AuthenticatedUserIsSuperuser = false;
00227 static bool SessionUserIsSuperuser = false;
00228 
00229 static int  SecurityRestrictionContext = 0;
00230 
00231 /* We also remember if a SET ROLE is currently active */
00232 static bool SetRoleIsActive = false;
00233 
00234 
00235 /*
00236  * GetUserId - get the current effective user ID.
00237  *
00238  * Note: there's no SetUserId() anymore; use SetUserIdAndSecContext().
00239  */
00240 Oid
00241 GetUserId(void)
00242 {
00243     AssertState(OidIsValid(CurrentUserId));
00244     return CurrentUserId;
00245 }
00246 
00247 
00248 /*
00249  * GetOuterUserId/SetOuterUserId - get/set the outer-level user ID.
00250  */
00251 Oid
00252 GetOuterUserId(void)
00253 {
00254     AssertState(OidIsValid(OuterUserId));
00255     return OuterUserId;
00256 }
00257 
00258 
00259 static void
00260 SetOuterUserId(Oid userid)
00261 {
00262     AssertState(SecurityRestrictionContext == 0);
00263     AssertArg(OidIsValid(userid));
00264     OuterUserId = userid;
00265 
00266     /* We force the effective user ID to match, too */
00267     CurrentUserId = userid;
00268 }
00269 
00270 
00271 /*
00272  * GetSessionUserId/SetSessionUserId - get/set the session user ID.
00273  */
00274 Oid
00275 GetSessionUserId(void)
00276 {
00277     AssertState(OidIsValid(SessionUserId));
00278     return SessionUserId;
00279 }
00280 
00281 
00282 static void
00283 SetSessionUserId(Oid userid, bool is_superuser)
00284 {
00285     AssertState(SecurityRestrictionContext == 0);
00286     AssertArg(OidIsValid(userid));
00287     SessionUserId = userid;
00288     SessionUserIsSuperuser = is_superuser;
00289     SetRoleIsActive = false;
00290 
00291     /* We force the effective user IDs to match, too */
00292     OuterUserId = userid;
00293     CurrentUserId = userid;
00294 }
00295 
00296 
00297 /*
00298  * GetUserIdAndSecContext/SetUserIdAndSecContext - get/set the current user ID
00299  * and the SecurityRestrictionContext flags.
00300  *
00301  * Currently there are two valid bits in SecurityRestrictionContext:
00302  *
00303  * SECURITY_LOCAL_USERID_CHANGE indicates that we are inside an operation
00304  * that is temporarily changing CurrentUserId via these functions.  This is
00305  * needed to indicate that the actual value of CurrentUserId is not in sync
00306  * with guc.c's internal state, so SET ROLE has to be disallowed.
00307  *
00308  * SECURITY_RESTRICTED_OPERATION indicates that we are inside an operation
00309  * that does not wish to trust called user-defined functions at all.  This
00310  * bit prevents not only SET ROLE, but various other changes of session state
00311  * that normally is unprotected but might possibly be used to subvert the
00312  * calling session later.  An example is replacing an existing prepared
00313  * statement with new code, which will then be executed with the outer
00314  * session's permissions when the prepared statement is next used.  Since
00315  * these restrictions are fairly draconian, we apply them only in contexts
00316  * where the called functions are really supposed to be side-effect-free
00317  * anyway, such as VACUUM/ANALYZE/REINDEX.
00318  *
00319  * Unlike GetUserId, GetUserIdAndSecContext does *not* Assert that the current
00320  * value of CurrentUserId is valid; nor does SetUserIdAndSecContext require
00321  * the new value to be valid.  In fact, these routines had better not
00322  * ever throw any kind of error.  This is because they are used by
00323  * StartTransaction and AbortTransaction to save/restore the settings,
00324  * and during the first transaction within a backend, the value to be saved
00325  * and perhaps restored is indeed invalid.  We have to be able to get
00326  * through AbortTransaction without asserting in case InitPostgres fails.
00327  */
00328 void
00329 GetUserIdAndSecContext(Oid *userid, int *sec_context)
00330 {
00331     *userid = CurrentUserId;
00332     *sec_context = SecurityRestrictionContext;
00333 }
00334 
00335 void
00336 SetUserIdAndSecContext(Oid userid, int sec_context)
00337 {
00338     CurrentUserId = userid;
00339     SecurityRestrictionContext = sec_context;
00340 }
00341 
00342 
00343 /*
00344  * InLocalUserIdChange - are we inside a local change of CurrentUserId?
00345  */
00346 bool
00347 InLocalUserIdChange(void)
00348 {
00349     return (SecurityRestrictionContext & SECURITY_LOCAL_USERID_CHANGE) != 0;
00350 }
00351 
00352 /*
00353  * InSecurityRestrictedOperation - are we inside a security-restricted command?
00354  */
00355 bool
00356 InSecurityRestrictedOperation(void)
00357 {
00358     return (SecurityRestrictionContext & SECURITY_RESTRICTED_OPERATION) != 0;
00359 }
00360 
00361 
00362 /*
00363  * These are obsolete versions of Get/SetUserIdAndSecContext that are
00364  * only provided for bug-compatibility with some rather dubious code in
00365  * pljava.  We allow the userid to be set, but only when not inside a
00366  * security restriction context.
00367  */
00368 void
00369 GetUserIdAndContext(Oid *userid, bool *sec_def_context)
00370 {
00371     *userid = CurrentUserId;
00372     *sec_def_context = InLocalUserIdChange();
00373 }
00374 
00375 void
00376 SetUserIdAndContext(Oid userid, bool sec_def_context)
00377 {
00378     /* We throw the same error SET ROLE would. */
00379     if (InSecurityRestrictedOperation())
00380         ereport(ERROR,
00381                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
00382                  errmsg("cannot set parameter \"%s\" within security-restricted operation",
00383                         "role")));
00384     CurrentUserId = userid;
00385     if (sec_def_context)
00386         SecurityRestrictionContext |= SECURITY_LOCAL_USERID_CHANGE;
00387     else
00388         SecurityRestrictionContext &= ~SECURITY_LOCAL_USERID_CHANGE;
00389 }
00390 
00391 
00392 /*
00393  * Check whether specified role has explicit REPLICATION privilege
00394  */
00395 bool
00396 has_rolreplication(Oid roleid)
00397 {
00398     bool        result = false;
00399     HeapTuple   utup;
00400 
00401     utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
00402     if (HeapTupleIsValid(utup))
00403     {
00404         result = ((Form_pg_authid) GETSTRUCT(utup))->rolreplication;
00405         ReleaseSysCache(utup);
00406     }
00407     return result;
00408 }
00409 
00410 /*
00411  * Initialize user identity during normal backend startup
00412  */
00413 void
00414 InitializeSessionUserId(const char *rolename)
00415 {
00416     HeapTuple   roleTup;
00417     Form_pg_authid rform;
00418     Oid         roleid;
00419 
00420     /*
00421      * Don't do scans if we're bootstrapping, none of the system catalogs
00422      * exist yet, and they should be owned by postgres anyway.
00423      */
00424     AssertState(!IsBootstrapProcessingMode());
00425 
00426     /* call only once */
00427     AssertState(!OidIsValid(AuthenticatedUserId));
00428 
00429     roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename));
00430     if (!HeapTupleIsValid(roleTup))
00431         ereport(FATAL,
00432                 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
00433                  errmsg("role \"%s\" does not exist", rolename)));
00434 
00435     rform = (Form_pg_authid) GETSTRUCT(roleTup);
00436     roleid = HeapTupleGetOid(roleTup);
00437 
00438     AuthenticatedUserId = roleid;
00439     AuthenticatedUserIsSuperuser = rform->rolsuper;
00440 
00441     /* This sets OuterUserId/CurrentUserId too */
00442     SetSessionUserId(roleid, AuthenticatedUserIsSuperuser);
00443 
00444     /* Also mark our PGPROC entry with the authenticated user id */
00445     /* (We assume this is an atomic store so no lock is needed) */
00446     MyProc->roleId = roleid;
00447 
00448     /*
00449      * These next checks are not enforced when in standalone mode, so that
00450      * there is a way to recover from sillinesses like "UPDATE pg_authid SET
00451      * rolcanlogin = false;".
00452      */
00453     if (IsUnderPostmaster)
00454     {
00455         /*
00456          * Is role allowed to login at all?
00457          */
00458         if (!rform->rolcanlogin)
00459             ereport(FATAL,
00460                     (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
00461                      errmsg("role \"%s\" is not permitted to log in",
00462                             rolename)));
00463 
00464         /*
00465          * Check connection limit for this role.
00466          *
00467          * There is a race condition here --- we create our PGPROC before
00468          * checking for other PGPROCs.  If two backends did this at about the
00469          * same time, they might both think they were over the limit, while
00470          * ideally one should succeed and one fail.  Getting that to work
00471          * exactly seems more trouble than it is worth, however; instead we
00472          * just document that the connection limit is approximate.
00473          */
00474         if (rform->rolconnlimit >= 0 &&
00475             !AuthenticatedUserIsSuperuser &&
00476             CountUserBackends(roleid) > rform->rolconnlimit)
00477             ereport(FATAL,
00478                     (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
00479                      errmsg("too many connections for role \"%s\"",
00480                             rolename)));
00481     }
00482 
00483     /* Record username and superuser status as GUC settings too */
00484     SetConfigOption("session_authorization", rolename,
00485                     PGC_BACKEND, PGC_S_OVERRIDE);
00486     SetConfigOption("is_superuser",
00487                     AuthenticatedUserIsSuperuser ? "on" : "off",
00488                     PGC_INTERNAL, PGC_S_OVERRIDE);
00489 
00490     ReleaseSysCache(roleTup);
00491 }
00492 
00493 
00494 /*
00495  * Initialize user identity during special backend startup
00496  */
00497 void
00498 InitializeSessionUserIdStandalone(void)
00499 {
00500     /*
00501      * This function should only be called in single-user mode, in
00502      * autovacuum workers, and in background workers.
00503      */
00504     AssertState(!IsUnderPostmaster || IsAutoVacuumWorkerProcess() || IsBackgroundWorker);
00505 
00506     /* call only once */
00507     AssertState(!OidIsValid(AuthenticatedUserId));
00508 
00509     AuthenticatedUserId = BOOTSTRAP_SUPERUSERID;
00510     AuthenticatedUserIsSuperuser = true;
00511 
00512     SetSessionUserId(BOOTSTRAP_SUPERUSERID, true);
00513 }
00514 
00515 
00516 /*
00517  * Change session auth ID while running
00518  *
00519  * Only a superuser may set auth ID to something other than himself.  Note
00520  * that in case of multiple SETs in a single session, the original userid's
00521  * superuserness is what matters.  But we set the GUC variable is_superuser
00522  * to indicate whether the *current* session userid is a superuser.
00523  *
00524  * Note: this is not an especially clean place to do the permission check.
00525  * It's OK because the check does not require catalog access and can't
00526  * fail during an end-of-transaction GUC reversion, but we may someday
00527  * have to push it up into assign_session_authorization.
00528  */
00529 void
00530 SetSessionAuthorization(Oid userid, bool is_superuser)
00531 {
00532     /* Must have authenticated already, else can't make permission check */
00533     AssertState(OidIsValid(AuthenticatedUserId));
00534 
00535     if (userid != AuthenticatedUserId &&
00536         !AuthenticatedUserIsSuperuser)
00537         ereport(ERROR,
00538                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
00539                  errmsg("permission denied to set session authorization")));
00540 
00541     SetSessionUserId(userid, is_superuser);
00542 
00543     SetConfigOption("is_superuser",
00544                     is_superuser ? "on" : "off",
00545                     PGC_INTERNAL, PGC_S_OVERRIDE);
00546 }
00547 
00548 /*
00549  * Report current role id
00550  *      This follows the semantics of SET ROLE, ie return the outer-level ID
00551  *      not the current effective ID, and return InvalidOid when the setting
00552  *      is logically SET ROLE NONE.
00553  */
00554 Oid
00555 GetCurrentRoleId(void)
00556 {
00557     if (SetRoleIsActive)
00558         return OuterUserId;
00559     else
00560         return InvalidOid;
00561 }
00562 
00563 /*
00564  * Change Role ID while running (SET ROLE)
00565  *
00566  * If roleid is InvalidOid, we are doing SET ROLE NONE: revert to the
00567  * session user authorization.  In this case the is_superuser argument
00568  * is ignored.
00569  *
00570  * When roleid is not InvalidOid, the caller must have checked whether
00571  * the session user has permission to become that role.  (We cannot check
00572  * here because this routine must be able to execute in a failed transaction
00573  * to restore a prior value of the ROLE GUC variable.)
00574  */
00575 void
00576 SetCurrentRoleId(Oid roleid, bool is_superuser)
00577 {
00578     /*
00579      * Get correct info if it's SET ROLE NONE
00580      *
00581      * If SessionUserId hasn't been set yet, just do nothing --- the eventual
00582      * SetSessionUserId call will fix everything.  This is needed since we
00583      * will get called during GUC initialization.
00584      */
00585     if (!OidIsValid(roleid))
00586     {
00587         if (!OidIsValid(SessionUserId))
00588             return;
00589 
00590         roleid = SessionUserId;
00591         is_superuser = SessionUserIsSuperuser;
00592 
00593         SetRoleIsActive = false;
00594     }
00595     else
00596         SetRoleIsActive = true;
00597 
00598     SetOuterUserId(roleid);
00599 
00600     SetConfigOption("is_superuser",
00601                     is_superuser ? "on" : "off",
00602                     PGC_INTERNAL, PGC_S_OVERRIDE);
00603 }
00604 
00605 
00606 /*
00607  * Get user name from user oid
00608  */
00609 char *
00610 GetUserNameFromId(Oid roleid)
00611 {
00612     HeapTuple   tuple;
00613     char       *result;
00614 
00615     tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
00616     if (!HeapTupleIsValid(tuple))
00617         ereport(ERROR,
00618                 (errcode(ERRCODE_UNDEFINED_OBJECT),
00619                  errmsg("invalid role OID: %u", roleid)));
00620 
00621     result = pstrdup(NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname));
00622 
00623     ReleaseSysCache(tuple);
00624     return result;
00625 }
00626 
00627 
00628 /*-------------------------------------------------------------------------
00629  *              Interlock-file support
00630  *
00631  * These routines are used to create both a data-directory lockfile
00632  * ($DATADIR/postmaster.pid) and Unix-socket-file lockfiles ($SOCKFILE.lock).
00633  * Both kinds of files contain the same info initially, although we can add
00634  * more information to a data-directory lockfile after it's created, using
00635  * AddToDataDirLockFile().  See miscadmin.h for documentation of the contents
00636  * of these lockfiles.
00637  *
00638  * On successful lockfile creation, a proc_exit callback to remove the
00639  * lockfile is automatically created.
00640  *-------------------------------------------------------------------------
00641  */
00642 
00643 /*
00644  * proc_exit callback to remove lockfiles.
00645  */
00646 static void
00647 UnlinkLockFiles(int status, Datum arg)
00648 {
00649     ListCell   *l;
00650 
00651     foreach(l, lock_files)
00652     {
00653         char       *curfile = (char *) lfirst(l);
00654 
00655         unlink(curfile);
00656         /* Should we complain if the unlink fails? */
00657     }
00658     /* Since we're about to exit, no need to reclaim storage */
00659     lock_files = NIL;
00660 }
00661 
00662 /*
00663  * Create a lockfile.
00664  *
00665  * filename is the path name of the lockfile to create.
00666  * amPostmaster is used to determine how to encode the output PID.
00667  * socketDir is the Unix socket directory path to include (possibly empty).
00668  * isDDLock and refName are used to determine what error message to produce.
00669  */
00670 static void
00671 CreateLockFile(const char *filename, bool amPostmaster,
00672                const char *socketDir,
00673                bool isDDLock, const char *refName)
00674 {
00675     int         fd;
00676     char        buffer[MAXPGPATH * 2 + 256];
00677     int         ntries;
00678     int         len;
00679     int         encoded_pid;
00680     pid_t       other_pid;
00681     pid_t       my_pid,
00682                 my_p_pid,
00683                 my_gp_pid;
00684     const char *envvar;
00685 
00686     /*
00687      * If the PID in the lockfile is our own PID or our parent's or
00688      * grandparent's PID, then the file must be stale (probably left over from
00689      * a previous system boot cycle).  We need to check this because of the
00690      * likelihood that a reboot will assign exactly the same PID as we had in
00691      * the previous reboot, or one that's only one or two counts larger and
00692      * hence the lockfile's PID now refers to an ancestor shell process.  We
00693      * allow pg_ctl to pass down its parent shell PID (our grandparent PID)
00694      * via the environment variable PG_GRANDPARENT_PID; this is so that
00695      * launching the postmaster via pg_ctl can be just as reliable as
00696      * launching it directly.  There is no provision for detecting
00697      * further-removed ancestor processes, but if the init script is written
00698      * carefully then all but the immediate parent shell will be root-owned
00699      * processes and so the kill test will fail with EPERM.  Note that we
00700      * cannot get a false negative this way, because an existing postmaster
00701      * would surely never launch a competing postmaster or pg_ctl process
00702      * directly.
00703      */
00704     my_pid = getpid();
00705 
00706 #ifndef WIN32
00707     my_p_pid = getppid();
00708 #else
00709 
00710     /*
00711      * Windows hasn't got getppid(), but doesn't need it since it's not using
00712      * real kill() either...
00713      */
00714     my_p_pid = 0;
00715 #endif
00716 
00717     envvar = getenv("PG_GRANDPARENT_PID");
00718     if (envvar)
00719         my_gp_pid = atoi(envvar);
00720     else
00721         my_gp_pid = 0;
00722 
00723     /*
00724      * We need a loop here because of race conditions.  But don't loop forever
00725      * (for example, a non-writable $PGDATA directory might cause a failure
00726      * that won't go away).  100 tries seems like plenty.
00727      */
00728     for (ntries = 0;; ntries++)
00729     {
00730         /*
00731          * Try to create the lock file --- O_EXCL makes this atomic.
00732          *
00733          * Think not to make the file protection weaker than 0600.  See
00734          * comments below.
00735          */
00736         fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
00737         if (fd >= 0)
00738             break;              /* Success; exit the retry loop */
00739 
00740         /*
00741          * Couldn't create the pid file. Probably it already exists.
00742          */
00743         if ((errno != EEXIST && errno != EACCES) || ntries > 100)
00744             ereport(FATAL,
00745                     (errcode_for_file_access(),
00746                      errmsg("could not create lock file \"%s\": %m",
00747                             filename)));
00748 
00749         /*
00750          * Read the file to get the old owner's PID.  Note race condition
00751          * here: file might have been deleted since we tried to create it.
00752          */
00753         fd = open(filename, O_RDONLY, 0600);
00754         if (fd < 0)
00755         {
00756             if (errno == ENOENT)
00757                 continue;       /* race condition; try again */
00758             ereport(FATAL,
00759                     (errcode_for_file_access(),
00760                      errmsg("could not open lock file \"%s\": %m",
00761                             filename)));
00762         }
00763         if ((len = read(fd, buffer, sizeof(buffer) - 1)) < 0)
00764             ereport(FATAL,
00765                     (errcode_for_file_access(),
00766                      errmsg("could not read lock file \"%s\": %m",
00767                             filename)));
00768         close(fd);
00769 
00770         if (len == 0)
00771         {
00772             ereport(FATAL,
00773                     (errcode(ERRCODE_LOCK_FILE_EXISTS),
00774                      errmsg("lock file \"%s\" is empty", filename),
00775                      errhint("Either another server is starting, or the lock file is the remnant of a previous server startup crash.")));
00776         }
00777 
00778         buffer[len] = '\0';
00779         encoded_pid = atoi(buffer);
00780 
00781         /* if pid < 0, the pid is for postgres, not postmaster */
00782         other_pid = (pid_t) (encoded_pid < 0 ? -encoded_pid : encoded_pid);
00783 
00784         if (other_pid <= 0)
00785             elog(FATAL, "bogus data in lock file \"%s\": \"%s\"",
00786                  filename, buffer);
00787 
00788         /*
00789          * Check to see if the other process still exists
00790          *
00791          * Per discussion above, my_pid, my_p_pid, and my_gp_pid can be
00792          * ignored as false matches.
00793          *
00794          * Normally kill() will fail with ESRCH if the given PID doesn't
00795          * exist.
00796          *
00797          * We can treat the EPERM-error case as okay because that error
00798          * implies that the existing process has a different userid than we
00799          * do, which means it cannot be a competing postmaster.  A postmaster
00800          * cannot successfully attach to a data directory owned by a userid
00801          * other than its own.  (This is now checked directly in
00802          * checkDataDir(), but has been true for a long time because of the
00803          * restriction that the data directory isn't group- or
00804          * world-accessible.)  Also, since we create the lockfiles mode 600,
00805          * we'd have failed above if the lockfile belonged to another userid
00806          * --- which means that whatever process kill() is reporting about
00807          * isn't the one that made the lockfile.  (NOTE: this last
00808          * consideration is the only one that keeps us from blowing away a
00809          * Unix socket file belonging to an instance of Postgres being run by
00810          * someone else, at least on machines where /tmp hasn't got a
00811          * stickybit.)
00812          */
00813         if (other_pid != my_pid && other_pid != my_p_pid &&
00814             other_pid != my_gp_pid)
00815         {
00816             if (kill(other_pid, 0) == 0 ||
00817                 (errno != ESRCH && errno != EPERM))
00818             {
00819                 /* lockfile belongs to a live process */
00820                 ereport(FATAL,
00821                         (errcode(ERRCODE_LOCK_FILE_EXISTS),
00822                          errmsg("lock file \"%s\" already exists",
00823                                 filename),
00824                          isDDLock ?
00825                          (encoded_pid < 0 ?
00826                           errhint("Is another postgres (PID %d) running in data directory \"%s\"?",
00827                                   (int) other_pid, refName) :
00828                           errhint("Is another postmaster (PID %d) running in data directory \"%s\"?",
00829                                   (int) other_pid, refName)) :
00830                          (encoded_pid < 0 ?
00831                           errhint("Is another postgres (PID %d) using socket file \"%s\"?",
00832                                   (int) other_pid, refName) :
00833                           errhint("Is another postmaster (PID %d) using socket file \"%s\"?",
00834                                   (int) other_pid, refName))));
00835             }
00836         }
00837 
00838         /*
00839          * No, the creating process did not exist.  However, it could be that
00840          * the postmaster crashed (or more likely was kill -9'd by a clueless
00841          * admin) but has left orphan backends behind.  Check for this by
00842          * looking to see if there is an associated shmem segment that is
00843          * still in use.
00844          *
00845          * Note: because postmaster.pid is written in multiple steps, we might
00846          * not find the shmem ID values in it; we can't treat that as an
00847          * error.
00848          */
00849         if (isDDLock)
00850         {
00851             char       *ptr = buffer;
00852             unsigned long id1,
00853                         id2;
00854             int         lineno;
00855 
00856             for (lineno = 1; lineno < LOCK_FILE_LINE_SHMEM_KEY; lineno++)
00857             {
00858                 if ((ptr = strchr(ptr, '\n')) == NULL)
00859                     break;
00860                 ptr++;
00861             }
00862 
00863             if (ptr != NULL &&
00864                 sscanf(ptr, "%lu %lu", &id1, &id2) == 2)
00865             {
00866                 if (PGSharedMemoryIsInUse(id1, id2))
00867                     ereport(FATAL,
00868                             (errcode(ERRCODE_LOCK_FILE_EXISTS),
00869                              errmsg("pre-existing shared memory block "
00870                                     "(key %lu, ID %lu) is still in use",
00871                                     id1, id2),
00872                              errhint("If you're sure there are no old "
00873                                      "server processes still running, remove "
00874                                      "the shared memory block "
00875                                      "or just delete the file \"%s\".",
00876                                      filename)));
00877             }
00878         }
00879 
00880         /*
00881          * Looks like nobody's home.  Unlink the file and try again to create
00882          * it.  Need a loop because of possible race condition against other
00883          * would-be creators.
00884          */
00885         if (unlink(filename) < 0)
00886             ereport(FATAL,
00887                     (errcode_for_file_access(),
00888                      errmsg("could not remove old lock file \"%s\": %m",
00889                             filename),
00890                      errhint("The file seems accidentally left over, but "
00891                            "it could not be removed. Please remove the file "
00892                              "by hand and try again.")));
00893     }
00894 
00895     /*
00896      * Successfully created the file, now fill it.  See comment in miscadmin.h
00897      * about the contents.  Note that we write the same first five lines into
00898      * both datadir and socket lockfiles; although more stuff may get added to
00899      * the datadir lockfile later.
00900      */
00901     snprintf(buffer, sizeof(buffer), "%d\n%s\n%ld\n%d\n%s\n",
00902              amPostmaster ? (int) my_pid : -((int) my_pid),
00903              DataDir,
00904              (long) MyStartTime,
00905              PostPortNumber,
00906              socketDir);
00907 
00908     /*
00909      * In a standalone backend, the next line (LOCK_FILE_LINE_LISTEN_ADDR)
00910      * will never receive data, so fill it in as empty now.
00911      */
00912     if (isDDLock && !amPostmaster)
00913         strlcat(buffer, "\n", sizeof(buffer));
00914 
00915     errno = 0;
00916     if (write(fd, buffer, strlen(buffer)) != strlen(buffer))
00917     {
00918         int         save_errno = errno;
00919 
00920         close(fd);
00921         unlink(filename);
00922         /* if write didn't set errno, assume problem is no disk space */
00923         errno = save_errno ? save_errno : ENOSPC;
00924         ereport(FATAL,
00925                 (errcode_for_file_access(),
00926                  errmsg("could not write lock file \"%s\": %m", filename)));
00927     }
00928     if (pg_fsync(fd) != 0)
00929     {
00930         int         save_errno = errno;
00931 
00932         close(fd);
00933         unlink(filename);
00934         errno = save_errno;
00935         ereport(FATAL,
00936                 (errcode_for_file_access(),
00937                  errmsg("could not write lock file \"%s\": %m", filename)));
00938     }
00939     if (close(fd) != 0)
00940     {
00941         int         save_errno = errno;
00942 
00943         unlink(filename);
00944         errno = save_errno;
00945         ereport(FATAL,
00946                 (errcode_for_file_access(),
00947                  errmsg("could not write lock file \"%s\": %m", filename)));
00948     }
00949 
00950     /*
00951      * Arrange to unlink the lock file(s) at proc_exit.  If this is the
00952      * first one, set up the on_proc_exit function to do it; then add this
00953      * lock file to the list of files to unlink.
00954      */
00955     if (lock_files == NIL)
00956         on_proc_exit(UnlinkLockFiles, 0);
00957 
00958     lock_files = lappend(lock_files, pstrdup(filename));
00959 }
00960 
00961 /*
00962  * Create the data directory lockfile.
00963  *
00964  * When this is called, we must have already switched the working
00965  * directory to DataDir, so we can just use a relative path.  This
00966  * helps ensure that we are locking the directory we should be.
00967  *
00968  * Note that the socket directory path line is initially written as empty.
00969  * postmaster.c will rewrite it upon creating the first Unix socket.
00970  */
00971 void
00972 CreateDataDirLockFile(bool amPostmaster)
00973 {
00974     CreateLockFile(DIRECTORY_LOCK_FILE, amPostmaster, "", true, DataDir);
00975 }
00976 
00977 /*
00978  * Create a lockfile for the specified Unix socket file.
00979  */
00980 void
00981 CreateSocketLockFile(const char *socketfile, bool amPostmaster,
00982                      const char *socketDir)
00983 {
00984     char        lockfile[MAXPGPATH];
00985 
00986     snprintf(lockfile, sizeof(lockfile), "%s.lock", socketfile);
00987     CreateLockFile(lockfile, amPostmaster, socketDir, false, socketfile);
00988 }
00989 
00990 /*
00991  * TouchSocketLockFiles -- mark socket lock files as recently accessed
00992  *
00993  * This routine should be called every so often to ensure that the socket
00994  * lock files have a recent mod or access date.  That saves them
00995  * from being removed by overenthusiastic /tmp-directory-cleaner daemons.
00996  * (Another reason we should never have put the socket file in /tmp...)
00997  */
00998 void
00999 TouchSocketLockFiles(void)
01000 {
01001     ListCell   *l;
01002 
01003     foreach(l, lock_files)
01004     {
01005         char       *socketLockFile = (char *) lfirst(l);
01006 
01007         /* No need to touch the data directory lock file, we trust */
01008         if (strcmp(socketLockFile, DIRECTORY_LOCK_FILE) == 0)
01009             continue;
01010 
01011         /*
01012          * utime() is POSIX standard, utimes() is a common alternative; if we
01013          * have neither, fall back to actually reading the file (which only
01014          * sets the access time not mod time, but that should be enough in
01015          * most cases).  In all paths, we ignore errors.
01016          */
01017 #ifdef HAVE_UTIME
01018         utime(socketLockFile, NULL);
01019 #else                           /* !HAVE_UTIME */
01020 #ifdef HAVE_UTIMES
01021         utimes(socketLockFile, NULL);
01022 #else                           /* !HAVE_UTIMES */
01023         int         fd;
01024         char        buffer[1];
01025 
01026         fd = open(socketLockFile, O_RDONLY | PG_BINARY, 0);
01027         if (fd >= 0)
01028         {
01029             read(fd, buffer, sizeof(buffer));
01030             close(fd);
01031         }
01032 #endif   /* HAVE_UTIMES */
01033 #endif   /* HAVE_UTIME */
01034     }
01035 }
01036 
01037 
01038 /*
01039  * Add (or replace) a line in the data directory lock file.
01040  * The given string should not include a trailing newline.
01041  *
01042  * Note: because we don't truncate the file, if we were to rewrite a line
01043  * with less data than it had before, there would be garbage after the last
01044  * line.  We don't ever actually do that, so not worth adding another kernel
01045  * call to cover the possibility.
01046  */
01047 void
01048 AddToDataDirLockFile(int target_line, const char *str)
01049 {
01050     int         fd;
01051     int         len;
01052     int         lineno;
01053     char       *srcptr;
01054     char       *destptr;
01055     char        srcbuffer[BLCKSZ];
01056     char        destbuffer[BLCKSZ];
01057 
01058     fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
01059     if (fd < 0)
01060     {
01061         ereport(LOG,
01062                 (errcode_for_file_access(),
01063                  errmsg("could not open file \"%s\": %m",
01064                         DIRECTORY_LOCK_FILE)));
01065         return;
01066     }
01067     len = read(fd, srcbuffer, sizeof(srcbuffer) - 1);
01068     if (len < 0)
01069     {
01070         ereport(LOG,
01071                 (errcode_for_file_access(),
01072                  errmsg("could not read from file \"%s\": %m",
01073                         DIRECTORY_LOCK_FILE)));
01074         close(fd);
01075         return;
01076     }
01077     srcbuffer[len] = '\0';
01078 
01079     /*
01080      * Advance over lines we are not supposed to rewrite, then copy them
01081      * to destbuffer.
01082      */
01083     srcptr = srcbuffer;
01084     for (lineno = 1; lineno < target_line; lineno++)
01085     {
01086         if ((srcptr = strchr(srcptr, '\n')) == NULL)
01087         {
01088             elog(LOG, "incomplete data in \"%s\": found only %d newlines while trying to add line %d",
01089                  DIRECTORY_LOCK_FILE, lineno - 1, target_line);
01090             close(fd);
01091             return;
01092         }
01093         srcptr++;
01094     }
01095     memcpy(destbuffer, srcbuffer, srcptr - srcbuffer);
01096     destptr = destbuffer + (srcptr - srcbuffer);
01097 
01098     /*
01099      * Write or rewrite the target line.
01100      */
01101     snprintf(destptr, destbuffer + sizeof(destbuffer) - destptr, "%s\n", str);
01102     destptr += strlen(destptr);
01103 
01104     /*
01105      * If there are more lines in the old file, append them to destbuffer.
01106      */
01107     if ((srcptr = strchr(srcptr, '\n')) != NULL)
01108     {
01109         srcptr++;
01110         snprintf(destptr, destbuffer + sizeof(destbuffer) - destptr, "%s",
01111                  srcptr);
01112     }
01113 
01114     /*
01115      * And rewrite the data.  Since we write in a single kernel call, this
01116      * update should appear atomic to onlookers.
01117      */
01118     len = strlen(destbuffer);
01119     errno = 0;
01120     if (lseek(fd, (off_t) 0, SEEK_SET) != 0 ||
01121         (int) write(fd, destbuffer, len) != len)
01122     {
01123         /* if write didn't set errno, assume problem is no disk space */
01124         if (errno == 0)
01125             errno = ENOSPC;
01126         ereport(LOG,
01127                 (errcode_for_file_access(),
01128                  errmsg("could not write to file \"%s\": %m",
01129                         DIRECTORY_LOCK_FILE)));
01130         close(fd);
01131         return;
01132     }
01133     if (pg_fsync(fd) != 0)
01134     {
01135         ereport(LOG,
01136                 (errcode_for_file_access(),
01137                  errmsg("could not write to file \"%s\": %m",
01138                         DIRECTORY_LOCK_FILE)));
01139     }
01140     if (close(fd) != 0)
01141     {
01142         ereport(LOG,
01143                 (errcode_for_file_access(),
01144                  errmsg("could not write to file \"%s\": %m",
01145                         DIRECTORY_LOCK_FILE)));
01146     }
01147 }
01148 
01149 
01150 /*-------------------------------------------------------------------------
01151  *              Version checking support
01152  *-------------------------------------------------------------------------
01153  */
01154 
01155 /*
01156  * Determine whether the PG_VERSION file in directory `path' indicates
01157  * a data version compatible with the version of this program.
01158  *
01159  * If compatible, return. Otherwise, ereport(FATAL).
01160  */
01161 void
01162 ValidatePgVersion(const char *path)
01163 {
01164     char        full_path[MAXPGPATH];
01165     FILE       *file;
01166     int         ret;
01167     long        file_major,
01168                 file_minor;
01169     long        my_major = 0,
01170                 my_minor = 0;
01171     char       *endptr;
01172     const char *version_string = PG_VERSION;
01173 
01174     my_major = strtol(version_string, &endptr, 10);
01175     if (*endptr == '.')
01176         my_minor = strtol(endptr + 1, NULL, 10);
01177 
01178     snprintf(full_path, sizeof(full_path), "%s/PG_VERSION", path);
01179 
01180     file = AllocateFile(full_path, "r");
01181     if (!file)
01182     {
01183         if (errno == ENOENT)
01184             ereport(FATAL,
01185                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
01186                      errmsg("\"%s\" is not a valid data directory",
01187                             path),
01188                      errdetail("File \"%s\" is missing.", full_path)));
01189         else
01190             ereport(FATAL,
01191                     (errcode_for_file_access(),
01192                      errmsg("could not open file \"%s\": %m", full_path)));
01193     }
01194 
01195     ret = fscanf(file, "%ld.%ld", &file_major, &file_minor);
01196     if (ret != 2)
01197         ereport(FATAL,
01198                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
01199                  errmsg("\"%s\" is not a valid data directory",
01200                         path),
01201                  errdetail("File \"%s\" does not contain valid data.",
01202                            full_path),
01203                  errhint("You might need to initdb.")));
01204 
01205     FreeFile(file);
01206 
01207     if (my_major != file_major || my_minor != file_minor)
01208         ereport(FATAL,
01209                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
01210                  errmsg("database files are incompatible with server"),
01211                  errdetail("The data directory was initialized by PostgreSQL version %ld.%ld, "
01212                            "which is not compatible with this version %s.",
01213                            file_major, file_minor, version_string)));
01214 }
01215 
01216 /*-------------------------------------------------------------------------
01217  *              Library preload support
01218  *-------------------------------------------------------------------------
01219  */
01220 
01221 /*
01222  * GUC variables: lists of library names to be preloaded at postmaster
01223  * start and at backend start
01224  */
01225 char       *shared_preload_libraries_string = NULL;
01226 char       *local_preload_libraries_string = NULL;
01227 
01228 /* Flag telling that we are loading shared_preload_libraries */
01229 bool        process_shared_preload_libraries_in_progress = false;
01230 
01231 /*
01232  * load the shared libraries listed in 'libraries'
01233  *
01234  * 'gucname': name of GUC variable, for error reports
01235  * 'restricted': if true, force libraries to be in $libdir/plugins/
01236  */
01237 static void
01238 load_libraries(const char *libraries, const char *gucname, bool restricted)
01239 {
01240     char       *rawstring;
01241     List       *elemlist;
01242     int         elevel;
01243     ListCell   *l;
01244 
01245     if (libraries == NULL || libraries[0] == '\0')
01246         return;                 /* nothing to do */
01247 
01248     /* Need a modifiable copy of string */
01249     rawstring = pstrdup(libraries);
01250 
01251     /* Parse string into list of identifiers */
01252     if (!SplitIdentifierString(rawstring, ',', &elemlist))
01253     {
01254         /* syntax error in list */
01255         pfree(rawstring);
01256         list_free(elemlist);
01257         ereport(LOG,
01258                 (errcode(ERRCODE_SYNTAX_ERROR),
01259                  errmsg("invalid list syntax in parameter \"%s\"",
01260                         gucname)));
01261         return;
01262     }
01263 
01264     /*
01265      * Choose notice level: avoid repeat messages when re-loading a library
01266      * that was preloaded into the postmaster.  (Only possible in EXEC_BACKEND
01267      * configurations)
01268      */
01269 #ifdef EXEC_BACKEND
01270     if (IsUnderPostmaster && process_shared_preload_libraries_in_progress)
01271         elevel = DEBUG2;
01272     else
01273 #endif
01274         elevel = LOG;
01275 
01276     foreach(l, elemlist)
01277     {
01278         char       *tok = (char *) lfirst(l);
01279         char       *filename;
01280 
01281         filename = pstrdup(tok);
01282         canonicalize_path(filename);
01283         /* If restricting, insert $libdir/plugins if not mentioned already */
01284         if (restricted && first_dir_separator(filename) == NULL)
01285         {
01286             char       *expanded;
01287 
01288             expanded = palloc(strlen("$libdir/plugins/") + strlen(filename) + 1);
01289             strcpy(expanded, "$libdir/plugins/");
01290             strcat(expanded, filename);
01291             pfree(filename);
01292             filename = expanded;
01293         }
01294         load_file(filename, restricted);
01295         ereport(elevel,
01296                 (errmsg("loaded library \"%s\"", filename)));
01297         pfree(filename);
01298     }
01299 
01300     pfree(rawstring);
01301     list_free(elemlist);
01302 }
01303 
01304 /*
01305  * process any libraries that should be preloaded at postmaster start
01306  */
01307 void
01308 process_shared_preload_libraries(void)
01309 {
01310     process_shared_preload_libraries_in_progress = true;
01311     load_libraries(shared_preload_libraries_string,
01312                    "shared_preload_libraries",
01313                    false);
01314     process_shared_preload_libraries_in_progress = false;
01315 }
01316 
01317 /*
01318  * process any libraries that should be preloaded at backend start
01319  */
01320 void
01321 process_local_preload_libraries(void)
01322 {
01323     load_libraries(local_preload_libraries_string,
01324                    "local_preload_libraries",
01325                    true);
01326 }
01327 
01328 void
01329 pg_bindtextdomain(const char *domain)
01330 {
01331 #ifdef ENABLE_NLS
01332     if (my_exec_path[0] != '\0')
01333     {
01334         char        locale_path[MAXPGPATH];
01335 
01336         get_locale_path(my_exec_path, locale_path);
01337         bindtextdomain(domain, locale_path);
01338         pg_bind_textdomain_codeset(domain);
01339     }
01340 #endif
01341 }