1 /*-------------------------------------------------------------------------
4 * miscellaneous initialization support stuff
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/init/miscinit.c
13 *-------------------------------------------------------------------------
17 #include <sys/param.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
32 #include "catalog/pg_authid.h"
33 #include "mb/pg_wchar.h"
34 #include "miscadmin.h"
35 #include "postmaster/autovacuum.h"
36 #include "postmaster/postmaster.h"
37 #include "storage/fd.h"
38 #include "storage/ipc.h"
39 #include "storage/pg_shmem.h"
40 #include "storage/proc.h"
41 #include "storage/procarray.h"
42 #include "utils/builtins.h"
43 #include "utils/guc.h"
44 #include "utils/memutils.h"
45 #include "utils/syscache.h"
48 #define DIRECTORY_LOCK_FILE "postmaster.pid"
50 * The lock file contents are:
54 * 2 postmaster start time
57 * 5 user-specified socket directory
58 * (the lines below are added later)
59 * 6 first valid listen_address
63 ProcessingMode Mode = InitProcessing;
65 /* Note: we rely on this to initialize as zeroes */
66 static char socketLockFile[MAXPGPATH];
69 /* ----------------------------------------------------------------
70 * ignoring system indexes support stuff
72 * NOTE: "ignoring system indexes" means we do not use the system indexes
73 * for lookups (either in hardwired catalog accesses or in planner-generated
74 * plans). We do, however, still update the indexes when a catalog
75 * modification is made.
76 * ----------------------------------------------------------------
79 bool IgnoreSystemIndexes = false;
82 /* ----------------------------------------------------------------
83 * database path / name support stuff
84 * ----------------------------------------------------------------
88 SetDatabasePath(const char *path)
90 /* This should happen only once per process */
91 Assert(!DatabasePath);
92 DatabasePath = MemoryContextStrdup(TopMemoryContext, path);
96 * Set data directory, but make sure it's an absolute path. Use this,
97 * never set DataDir directly.
100 SetDataDir(const char *dir)
106 /* If presented path is relative, convert to absolute */
107 new = make_absolute_path(dir);
115 * Change working directory to DataDir. Most of the postmaster and backend
116 * code assumes that we are in DataDir so it can use relative paths to access
117 * stuff in and under the data directory. For convenience during path
118 * setup, however, we don't force the chdir to occur during SetDataDir.
121 ChangeToDataDir(void)
123 AssertState(DataDir);
125 if (chdir(DataDir) < 0)
127 (errcode_for_file_access(),
128 errmsg("could not change directory to \"%s\": %m",
133 * If the given pathname isn't already absolute, make it so, interpreting
134 * it relative to the current working directory.
136 * Also canonicalizes the path. The result is always a malloc'd copy.
138 * Note: interpretation of relative-path arguments during postmaster startup
139 * should happen before doing ChangeToDataDir(), else the user will probably
140 * not like the results.
143 make_absolute_path(const char *path)
147 /* Returning null for null input is convenient for some callers */
151 if (!is_absolute_path(path))
159 buf = malloc(buflen);
162 (errcode(ERRCODE_OUT_OF_MEMORY),
163 errmsg("out of memory")));
165 if (getcwd(buf, buflen))
167 else if (errno == ERANGE)
176 elog(FATAL, "could not get current working directory: %m");
180 new = malloc(strlen(buf) + strlen(path) + 2);
183 (errcode(ERRCODE_OUT_OF_MEMORY),
184 errmsg("out of memory")));
185 sprintf(new, "%s/%s", buf, path);
193 (errcode(ERRCODE_OUT_OF_MEMORY),
194 errmsg("out of memory")));
197 /* Make sure punctuation is canonical, too */
198 canonicalize_path(new);
204 /* ----------------------------------------------------------------
207 * We have to track several different values associated with the concept
210 * AuthenticatedUserId is determined at connection start and never changes.
212 * SessionUserId is initially the same as AuthenticatedUserId, but can be
213 * changed by SET SESSION AUTHORIZATION (if AuthenticatedUserIsSuperuser).
214 * This is the ID reported by the SESSION_USER SQL function.
216 * OuterUserId is the current user ID in effect at the "outer level" (outside
217 * any transaction or function). This is initially the same as SessionUserId,
218 * but can be changed by SET ROLE to any role that SessionUserId is a
219 * member of. (XXX rename to something like CurrentRoleId?)
221 * CurrentUserId is the current effective user ID; this is the one to use
222 * for all normal permissions-checking purposes. At outer level this will
223 * be the same as OuterUserId, but it changes during calls to SECURITY
224 * DEFINER functions, as well as locally in some specialized commands.
226 * SecurityRestrictionContext holds flags indicating reason(s) for changing
227 * CurrentUserId. In some cases we need to lock down operations that are
228 * not directly controlled by privilege settings, and this provides a
229 * convenient way to do it.
230 * ----------------------------------------------------------------
232 static Oid AuthenticatedUserId = InvalidOid;
233 static Oid SessionUserId = InvalidOid;
234 static Oid OuterUserId = InvalidOid;
235 static Oid CurrentUserId = InvalidOid;
237 /* We also have to remember the superuser state of some of these levels */
238 static bool AuthenticatedUserIsSuperuser = false;
239 static bool SessionUserIsSuperuser = false;
241 static int SecurityRestrictionContext = 0;
243 /* We also remember if a SET ROLE is currently active */
244 static bool SetRoleIsActive = false;
249 * GetUserId - get the current effective user ID.
251 * Note: there's no SetUserId() anymore; use SetUserIdAndSecContext().
256 AssertState(OidIsValid(CurrentUserId));
257 return CurrentUserId;
262 * GetOuterUserId/SetOuterUserId - get/set the outer-level user ID.
267 AssertState(OidIsValid(OuterUserId));
273 SetOuterUserId(Oid userid)
275 AssertState(SecurityRestrictionContext == 0);
276 AssertArg(OidIsValid(userid));
277 OuterUserId = userid;
279 /* We force the effective user ID to match, too */
280 CurrentUserId = userid;
285 * GetSessionUserId/SetSessionUserId - get/set the session user ID.
288 GetSessionUserId(void)
290 AssertState(OidIsValid(SessionUserId));
291 return SessionUserId;
296 SetSessionUserId(Oid userid, bool is_superuser)
298 AssertState(SecurityRestrictionContext == 0);
299 AssertArg(OidIsValid(userid));
300 SessionUserId = userid;
301 SessionUserIsSuperuser = is_superuser;
302 SetRoleIsActive = false;
304 /* We force the effective user IDs to match, too */
305 OuterUserId = userid;
306 CurrentUserId = userid;
311 * GetUserIdAndSecContext/SetUserIdAndSecContext - get/set the current user ID
312 * and the SecurityRestrictionContext flags.
314 * Currently there are two valid bits in SecurityRestrictionContext:
316 * SECURITY_LOCAL_USERID_CHANGE indicates that we are inside an operation
317 * that is temporarily changing CurrentUserId via these functions. This is
318 * needed to indicate that the actual value of CurrentUserId is not in sync
319 * with guc.c's internal state, so SET ROLE has to be disallowed.
321 * SECURITY_RESTRICTED_OPERATION indicates that we are inside an operation
322 * that does not wish to trust called user-defined functions at all. This
323 * bit prevents not only SET ROLE, but various other changes of session state
324 * that normally is unprotected but might possibly be used to subvert the
325 * calling session later. An example is replacing an existing prepared
326 * statement with new code, which will then be executed with the outer
327 * session's permissions when the prepared statement is next used. Since
328 * these restrictions are fairly draconian, we apply them only in contexts
329 * where the called functions are really supposed to be side-effect-free
330 * anyway, such as VACUUM/ANALYZE/REINDEX.
332 * Unlike GetUserId, GetUserIdAndSecContext does *not* Assert that the current
333 * value of CurrentUserId is valid; nor does SetUserIdAndSecContext require
334 * the new value to be valid. In fact, these routines had better not
335 * ever throw any kind of error. This is because they are used by
336 * StartTransaction and AbortTransaction to save/restore the settings,
337 * and during the first transaction within a backend, the value to be saved
338 * and perhaps restored is indeed invalid. We have to be able to get
339 * through AbortTransaction without asserting in case InitPostgres fails.
342 GetUserIdAndSecContext(Oid *userid, int *sec_context)
344 *userid = CurrentUserId;
345 *sec_context = SecurityRestrictionContext;
349 SetUserIdAndSecContext(Oid userid, int sec_context)
351 CurrentUserId = userid;
352 SecurityRestrictionContext = sec_context;
357 * InLocalUserIdChange - are we inside a local change of CurrentUserId?
360 InLocalUserIdChange(void)
362 return (SecurityRestrictionContext & SECURITY_LOCAL_USERID_CHANGE) != 0;
366 * InSecurityRestrictedOperation - are we inside a security-restricted command?
369 InSecurityRestrictedOperation(void)
371 return (SecurityRestrictionContext & SECURITY_RESTRICTED_OPERATION) != 0;
376 * These are obsolete versions of Get/SetUserIdAndSecContext that are
377 * only provided for bug-compatibility with some rather dubious code in
378 * pljava. We allow the userid to be set, but only when not inside a
379 * security restriction context.
382 GetUserIdAndContext(Oid *userid, bool *sec_def_context)
384 *userid = CurrentUserId;
385 *sec_def_context = InLocalUserIdChange();
389 SetUserIdAndContext(Oid userid, bool sec_def_context)
391 /* We throw the same error SET ROLE would. */
392 if (InSecurityRestrictedOperation())
394 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
395 errmsg("cannot set parameter \"%s\" within security-restricted operation",
397 CurrentUserId = userid;
399 SecurityRestrictionContext |= SECURITY_LOCAL_USERID_CHANGE;
401 SecurityRestrictionContext &= ~SECURITY_LOCAL_USERID_CHANGE;
406 * Check if the authenticated user is a replication role
409 is_authenticated_user_replication_role(void)
414 utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(AuthenticatedUserId));
415 if (HeapTupleIsValid(utup))
417 result = ((Form_pg_authid) GETSTRUCT(utup))->rolreplication;
418 ReleaseSysCache(utup);
424 * Initialize user identity during normal backend startup
427 InitializeSessionUserId(const char *rolename)
430 Form_pg_authid rform;
434 * Don't do scans if we're bootstrapping, none of the system catalogs
435 * exist yet, and they should be owned by postgres anyway.
437 AssertState(!IsBootstrapProcessingMode());
440 AssertState(!OidIsValid(AuthenticatedUserId));
442 roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename));
443 if (!HeapTupleIsValid(roleTup))
445 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
446 errmsg("role \"%s\" does not exist", rolename)));
448 rform = (Form_pg_authid) GETSTRUCT(roleTup);
449 roleid = HeapTupleGetOid(roleTup);
451 AuthenticatedUserId = roleid;
452 AuthenticatedUserIsSuperuser = rform->rolsuper;
454 /* This sets OuterUserId/CurrentUserId too */
455 SetSessionUserId(roleid, AuthenticatedUserIsSuperuser);
457 /* Also mark our PGPROC entry with the authenticated user id */
458 /* (We assume this is an atomic store so no lock is needed) */
459 MyProc->roleId = roleid;
462 * These next checks are not enforced when in standalone mode, so that
463 * there is a way to recover from sillinesses like "UPDATE pg_authid SET
464 * rolcanlogin = false;".
466 if (IsUnderPostmaster)
469 * Is role allowed to login at all?
471 if (!rform->rolcanlogin)
473 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
474 errmsg("role \"%s\" is not permitted to log in",
478 * Check connection limit for this role.
480 * There is a race condition here --- we create our PGPROC before
481 * checking for other PGPROCs. If two backends did this at about the
482 * same time, they might both think they were over the limit, while
483 * ideally one should succeed and one fail. Getting that to work
484 * exactly seems more trouble than it is worth, however; instead we
485 * just document that the connection limit is approximate.
487 if (rform->rolconnlimit >= 0 &&
488 !AuthenticatedUserIsSuperuser &&
489 CountUserBackends(roleid) > rform->rolconnlimit)
491 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
492 errmsg("too many connections for role \"%s\"",
496 /* Record username and superuser status as GUC settings too */
497 SetConfigOption("session_authorization", rolename,
498 PGC_BACKEND, PGC_S_OVERRIDE);
499 SetConfigOption("is_superuser",
500 AuthenticatedUserIsSuperuser ? "on" : "off",
501 PGC_INTERNAL, PGC_S_OVERRIDE);
503 ReleaseSysCache(roleTup);
508 * Initialize user identity during special backend startup
511 InitializeSessionUserIdStandalone(void)
514 * This function should only be called in single-user mode and in
515 * autovacuum workers.
517 AssertState(!IsUnderPostmaster || IsAutoVacuumWorkerProcess());
520 AssertState(!OidIsValid(AuthenticatedUserId));
522 AuthenticatedUserId = BOOTSTRAP_SUPERUSERID;
523 AuthenticatedUserIsSuperuser = true;
525 SetSessionUserId(BOOTSTRAP_SUPERUSERID, true);
530 * Change session auth ID while running
532 * Only a superuser may set auth ID to something other than himself. Note
533 * that in case of multiple SETs in a single session, the original userid's
534 * superuserness is what matters. But we set the GUC variable is_superuser
535 * to indicate whether the *current* session userid is a superuser.
537 * Note: this is not an especially clean place to do the permission check.
538 * It's OK because the check does not require catalog access and can't
539 * fail during an end-of-transaction GUC reversion, but we may someday
540 * have to push it up into assign_session_authorization.
543 SetSessionAuthorization(Oid userid, bool is_superuser)
545 /* Must have authenticated already, else can't make permission check */
546 AssertState(OidIsValid(AuthenticatedUserId));
548 if (userid != AuthenticatedUserId &&
549 !AuthenticatedUserIsSuperuser)
551 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
552 errmsg("permission denied to set session authorization")));
554 SetSessionUserId(userid, is_superuser);
556 SetConfigOption("is_superuser",
557 is_superuser ? "on" : "off",
558 PGC_INTERNAL, PGC_S_OVERRIDE);
562 * Report current role id
563 * This follows the semantics of SET ROLE, ie return the outer-level ID
564 * not the current effective ID, and return InvalidOid when the setting
565 * is logically SET ROLE NONE.
568 GetCurrentRoleId(void)
577 * Change Role ID while running (SET ROLE)
579 * If roleid is InvalidOid, we are doing SET ROLE NONE: revert to the
580 * session user authorization. In this case the is_superuser argument
583 * When roleid is not InvalidOid, the caller must have checked whether
584 * the session user has permission to become that role. (We cannot check
585 * here because this routine must be able to execute in a failed transaction
586 * to restore a prior value of the ROLE GUC variable.)
589 SetCurrentRoleId(Oid roleid, bool is_superuser)
592 * Get correct info if it's SET ROLE NONE
594 * If SessionUserId hasn't been set yet, just do nothing --- the eventual
595 * SetSessionUserId call will fix everything. This is needed since we
596 * will get called during GUC initialization.
598 if (!OidIsValid(roleid))
600 if (!OidIsValid(SessionUserId))
603 roleid = SessionUserId;
604 is_superuser = SessionUserIsSuperuser;
606 SetRoleIsActive = false;
609 SetRoleIsActive = true;
611 SetOuterUserId(roleid);
613 SetConfigOption("is_superuser",
614 is_superuser ? "on" : "off",
615 PGC_INTERNAL, PGC_S_OVERRIDE);
620 * Get user name from user oid
623 GetUserNameFromId(Oid roleid)
628 tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
629 if (!HeapTupleIsValid(tuple))
631 (errcode(ERRCODE_UNDEFINED_OBJECT),
632 errmsg("invalid role OID: %u", roleid)));
634 result = pstrdup(NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname));
636 ReleaseSysCache(tuple);
641 /*-------------------------------------------------------------------------
642 * Interlock-file support
644 * These routines are used to create both a data-directory lockfile
645 * ($DATADIR/postmaster.pid) and a Unix-socket-file lockfile ($SOCKFILE.lock).
646 * Both kinds of files contain the same info:
648 * Owning process' PID
649 * Data directory path
651 * By convention, the owning process' PID is negated if it is a standalone
652 * backend rather than a postmaster. This is just for informational purposes.
653 * The path is also just for informational purposes (so that a socket lockfile
654 * can be more easily traced to the associated postmaster).
656 * A data-directory lockfile can optionally contain a third line, containing
657 * the key and ID for the shared memory block used by this postmaster.
659 * On successful lockfile creation, a proc_exit callback to remove the
660 * lockfile is automatically created.
661 *-------------------------------------------------------------------------
665 * proc_exit callback to remove a lockfile.
668 UnlinkLockFile(int status, Datum filename)
670 char *fname = (char *) DatumGetPointer(filename);
674 if (unlink(fname) != 0)
676 /* Should we complain if the unlink fails? */
685 * filename is the name of the lockfile to create.
686 * amPostmaster is used to determine how to encode the output PID.
687 * isDDLock and refName are used to determine what error message to produce.
690 CreateLockFile(const char *filename, bool amPostmaster,
691 bool isDDLock, const char *refName)
694 char buffer[MAXPGPATH * 3 + 256];
705 * If the PID in the lockfile is our own PID or our parent's or
706 * grandparent's PID, then the file must be stale (probably left over from
707 * a previous system boot cycle). We need to check this because of the
708 * likelihood that a reboot will assign exactly the same PID as we had in
709 * the previous reboot, or one that's only one or two counts larger and
710 * hence the lockfile's PID now refers to an ancestor shell process. We
711 * allow pg_ctl to pass down its parent shell PID (our grandparent PID)
712 * via the environment variable PG_GRANDPARENT_PID; this is so that
713 * launching the postmaster via pg_ctl can be just as reliable as
714 * launching it directly. There is no provision for detecting
715 * further-removed ancestor processes, but if the init script is written
716 * carefully then all but the immediate parent shell will be root-owned
717 * processes and so the kill test will fail with EPERM. Note that we
718 * cannot get a false negative this way, because an existing postmaster
719 * would surely never launch a competing postmaster or pg_ctl process
725 my_p_pid = getppid();
729 * Windows hasn't got getppid(), but doesn't need it since it's not using
730 * real kill() either...
735 envvar = getenv("PG_GRANDPARENT_PID");
737 my_gp_pid = atoi(envvar);
742 * We need a loop here because of race conditions. But don't loop forever
743 * (for example, a non-writable $PGDATA directory might cause a failure
744 * that won't go away). 100 tries seems like plenty.
746 for (ntries = 0;; ntries++)
749 * Try to create the lock file --- O_EXCL makes this atomic.
751 * Think not to make the file protection weaker than 0600. See
754 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
756 break; /* Success; exit the retry loop */
759 * Couldn't create the pid file. Probably it already exists.
761 if ((errno != EEXIST && errno != EACCES) || ntries > 100)
763 (errcode_for_file_access(),
764 errmsg("could not create lock file \"%s\": %m",
768 * Read the file to get the old owner's PID. Note race condition
769 * here: file might have been deleted since we tried to create it.
771 fd = open(filename, O_RDONLY, 0600);
775 continue; /* race condition; try again */
777 (errcode_for_file_access(),
778 errmsg("could not open lock file \"%s\": %m",
781 if ((len = read(fd, buffer, sizeof(buffer) - 1)) < 0)
783 (errcode_for_file_access(),
784 errmsg("could not read lock file \"%s\": %m",
789 encoded_pid = atoi(buffer);
791 /* if pid < 0, the pid is for postgres, not postmaster */
792 other_pid = (pid_t) (encoded_pid < 0 ? -encoded_pid : encoded_pid);
795 elog(FATAL, "bogus data in lock file \"%s\": \"%s\"",
799 * Check to see if the other process still exists
801 * Per discussion above, my_pid, my_p_pid, and my_gp_pid can be
802 * ignored as false matches.
804 * Normally kill() will fail with ESRCH if the given PID doesn't
807 * We can treat the EPERM-error case as okay because that error
808 * implies that the existing process has a different userid than we
809 * do, which means it cannot be a competing postmaster. A postmaster
810 * cannot successfully attach to a data directory owned by a userid
811 * other than its own. (This is now checked directly in
812 * checkDataDir(), but has been true for a long time because of the
813 * restriction that the data directory isn't group- or
814 * world-accessible.) Also, since we create the lockfiles mode 600,
815 * we'd have failed above if the lockfile belonged to another userid
816 * --- which means that whatever process kill() is reporting about
817 * isn't the one that made the lockfile. (NOTE: this last
818 * consideration is the only one that keeps us from blowing away a
819 * Unix socket file belonging to an instance of Postgres being run by
820 * someone else, at least on machines where /tmp hasn't got a
823 if (other_pid != my_pid && other_pid != my_p_pid &&
824 other_pid != my_gp_pid)
826 if (kill(other_pid, 0) == 0 ||
827 (errno != ESRCH && errno != EPERM))
829 /* lockfile belongs to a live process */
831 (errcode(ERRCODE_LOCK_FILE_EXISTS),
832 errmsg("lock file \"%s\" already exists",
836 errhint("Is another postgres (PID %d) running in data directory \"%s\"?",
837 (int) other_pid, refName) :
838 errhint("Is another postmaster (PID %d) running in data directory \"%s\"?",
839 (int) other_pid, refName)) :
841 errhint("Is another postgres (PID %d) using socket file \"%s\"?",
842 (int) other_pid, refName) :
843 errhint("Is another postmaster (PID %d) using socket file \"%s\"?",
844 (int) other_pid, refName))));
849 * No, the creating process did not exist. However, it could be that
850 * the postmaster crashed (or more likely was kill -9'd by a clueless
851 * admin) but has left orphan backends behind. Check for this by
852 * looking to see if there is an associated shmem segment that is
855 * Note: because postmaster.pid is written in two steps, we might not
856 * find the shmem ID values in it; we can't treat that as an error.
861 unsigned long id1, id2;
864 for (lineno = 1; lineno <= LOCK_FILE_LINES - 1; lineno++)
866 if ((ptr = strchr(ptr, '\n')) == NULL)
868 elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
874 if (ptr && sscanf(ptr, "%lu %lu", &id1, &id2) == 2)
876 if (PGSharedMemoryIsInUse(id1, id2))
878 (errcode(ERRCODE_LOCK_FILE_EXISTS),
879 errmsg("pre-existing shared memory block "
880 "(key %lu, ID %lu) is still in use",
882 errhint("If you're sure there are no old "
883 "server processes still running, remove "
884 "the shared memory block "
885 "or just delete the file \"%s\".",
891 * Looks like nobody's home. Unlink the file and try again to create
892 * it. Need a loop because of possible race condition against other
895 if (unlink(filename) < 0)
897 (errcode_for_file_access(),
898 errmsg("could not remove old lock file \"%s\": %m",
900 errhint("The file seems accidentally left over, but "
901 "it could not be removed. Please remove the file "
902 "by hand and try again.")));
906 * Successfully created the file, now fill it.
908 snprintf(buffer, sizeof(buffer), "%d\n%ld\n%s\n%d\n%s\n",
909 amPostmaster ? (int) my_pid : -((int) my_pid),
910 (long) MyStartTime, DataDir, PostPortNumber,
913 if (write(fd, buffer, strlen(buffer)) != strlen(buffer))
915 int save_errno = errno;
919 /* if write didn't set errno, assume problem is no disk space */
920 errno = save_errno ? save_errno : ENOSPC;
922 (errcode_for_file_access(),
923 errmsg("could not write lock file \"%s\": %m", filename)));
925 if (pg_fsync(fd) != 0)
927 int save_errno = errno;
933 (errcode_for_file_access(),
934 errmsg("could not write lock file \"%s\": %m", filename)));
938 int save_errno = errno;
943 (errcode_for_file_access(),
944 errmsg("could not write lock file \"%s\": %m", filename)));
948 * Arrange for automatic removal of lockfile at proc_exit.
950 on_proc_exit(UnlinkLockFile, PointerGetDatum(strdup(filename)));
954 * Create the data directory lockfile.
956 * When this is called, we must have already switched the working
957 * directory to DataDir, so we can just use a relative path. This
958 * helps ensure that we are locking the directory we should be.
961 CreateDataDirLockFile(bool amPostmaster)
963 CreateLockFile(DIRECTORY_LOCK_FILE, amPostmaster, true, DataDir);
967 * Create a lockfile for the specified Unix socket file.
970 CreateSocketLockFile(const char *socketfile, bool amPostmaster)
972 char lockfile[MAXPGPATH];
974 snprintf(lockfile, sizeof(lockfile), "%s.lock", socketfile);
975 CreateLockFile(lockfile, amPostmaster, false, socketfile);
976 /* Save name of lockfile for TouchSocketLockFile */
977 strcpy(socketLockFile, lockfile);
981 * TouchSocketLockFile -- mark socket lock file as recently accessed
983 * This routine should be called every so often to ensure that the lock file
984 * has a recent mod or access date. That saves it
985 * from being removed by overenthusiastic /tmp-directory-cleaner daemons.
986 * (Another reason we should never have put the socket file in /tmp...)
989 TouchSocketLockFile(void)
991 /* Do nothing if we did not create a socket... */
992 if (socketLockFile[0] != '\0')
995 * utime() is POSIX standard, utimes() is a common alternative; if we
996 * have neither, fall back to actually reading the file (which only
997 * sets the access time not mod time, but that should be enough in
998 * most cases). In all paths, we ignore errors.
1001 utime(socketLockFile, NULL);
1002 #else /* !HAVE_UTIME */
1004 utimes(socketLockFile, NULL);
1005 #else /* !HAVE_UTIMES */
1009 fd = open(socketLockFile, O_RDONLY | PG_BINARY, 0);
1012 read(fd, buffer, sizeof(buffer));
1015 #endif /* HAVE_UTIMES */
1016 #endif /* HAVE_UTIME */
1022 * Add lines to the data directory lock file. This erases all following
1023 * lines, but that is OK because lines are added in order.
1026 AddToLockFile(int target_line, const char *str)
1032 char buffer[MAXPGPATH * 3 + 256];
1034 fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
1038 (errcode_for_file_access(),
1039 errmsg("could not open file \"%s\": %m",
1040 DIRECTORY_LOCK_FILE)));
1043 len = read(fd, buffer, sizeof(buffer) - 100);
1047 (errcode_for_file_access(),
1048 errmsg("could not read from file \"%s\": %m",
1049 DIRECTORY_LOCK_FILE)));
1056 * Skip over first four lines (PID, pgdata, portnum, socketdir).
1059 for (lineno = 1; lineno < target_line; lineno++)
1061 if ((ptr = strchr(ptr, '\n')) == NULL)
1063 elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
1070 strlcat(buffer, str, sizeof(buffer));
1073 * And rewrite the data. Since we write in a single kernel call, this
1074 * update should appear atomic to onlookers.
1076 len = strlen(buffer);
1078 if (lseek(fd, (off_t) 0, SEEK_SET) != 0 ||
1079 (int) write(fd, buffer, len) != len)
1081 /* if write didn't set errno, assume problem is no disk space */
1085 (errcode_for_file_access(),
1086 errmsg("could not write to file \"%s\": %m",
1087 DIRECTORY_LOCK_FILE)));
1091 if (pg_fsync(fd) != 0)
1094 (errcode_for_file_access(),
1095 errmsg("could not write to file \"%s\": %m",
1096 DIRECTORY_LOCK_FILE)));
1101 (errcode_for_file_access(),
1102 errmsg("could not write to file \"%s\": %m",
1103 DIRECTORY_LOCK_FILE)));
1108 /*-------------------------------------------------------------------------
1109 * Version checking support
1110 *-------------------------------------------------------------------------
1114 * Determine whether the PG_VERSION file in directory `path' indicates
1115 * a data version compatible with the version of this program.
1117 * If compatible, return. Otherwise, ereport(FATAL).
1120 ValidatePgVersion(const char *path)
1122 char full_path[MAXPGPATH];
1130 const char *version_string = PG_VERSION;
1132 my_major = strtol(version_string, &endptr, 10);
1134 my_minor = strtol(endptr + 1, NULL, 10);
1136 snprintf(full_path, sizeof(full_path), "%s/PG_VERSION", path);
1138 file = AllocateFile(full_path, "r");
1141 if (errno == ENOENT)
1143 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1144 errmsg("\"%s\" is not a valid data directory",
1146 errdetail("File \"%s\" is missing.", full_path)));
1149 (errcode_for_file_access(),
1150 errmsg("could not open file \"%s\": %m", full_path)));
1153 ret = fscanf(file, "%ld.%ld", &file_major, &file_minor);
1156 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1157 errmsg("\"%s\" is not a valid data directory",
1159 errdetail("File \"%s\" does not contain valid data.",
1161 errhint("You might need to initdb.")));
1165 if (my_major != file_major || my_minor != file_minor)
1167 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1168 errmsg("database files are incompatible with server"),
1169 errdetail("The data directory was initialized by PostgreSQL version %ld.%ld, "
1170 "which is not compatible with this version %s.",
1171 file_major, file_minor, version_string)));
1174 /*-------------------------------------------------------------------------
1175 * Library preload support
1176 *-------------------------------------------------------------------------
1180 * GUC variables: lists of library names to be preloaded at postmaster
1181 * start and at backend start
1183 char *shared_preload_libraries_string = NULL;
1184 char *local_preload_libraries_string = NULL;
1186 /* Flag telling that we are loading shared_preload_libraries */
1187 bool process_shared_preload_libraries_in_progress = false;
1190 * load the shared libraries listed in 'libraries'
1192 * 'gucname': name of GUC variable, for error reports
1193 * 'restricted': if true, force libraries to be in $libdir/plugins/
1196 load_libraries(const char *libraries, const char *gucname, bool restricted)
1203 if (libraries == NULL || libraries[0] == '\0')
1204 return; /* nothing to do */
1206 /* Need a modifiable copy of string */
1207 rawstring = pstrdup(libraries);
1209 /* Parse string into list of identifiers */
1210 if (!SplitIdentifierString(rawstring, ',', &elemlist))
1212 /* syntax error in list */
1214 list_free(elemlist);
1216 (errcode(ERRCODE_SYNTAX_ERROR),
1217 errmsg("invalid list syntax in parameter \"%s\"",
1223 * Choose notice level: avoid repeat messages when re-loading a library
1224 * that was preloaded into the postmaster. (Only possible in EXEC_BACKEND
1228 if (IsUnderPostmaster && process_shared_preload_libraries_in_progress)
1234 foreach(l, elemlist)
1236 char *tok = (char *) lfirst(l);
1239 filename = pstrdup(tok);
1240 canonicalize_path(filename);
1241 /* If restricting, insert $libdir/plugins if not mentioned already */
1242 if (restricted && first_dir_separator(filename) == NULL)
1246 expanded = palloc(strlen("$libdir/plugins/") + strlen(filename) + 1);
1247 strcpy(expanded, "$libdir/plugins/");
1248 strcat(expanded, filename);
1250 filename = expanded;
1252 load_file(filename, restricted);
1254 (errmsg("loaded library \"%s\"", filename)));
1259 list_free(elemlist);
1263 * process any libraries that should be preloaded at postmaster start
1266 process_shared_preload_libraries(void)
1268 process_shared_preload_libraries_in_progress = true;
1269 load_libraries(shared_preload_libraries_string,
1270 "shared_preload_libraries",
1272 process_shared_preload_libraries_in_progress = false;
1276 * process any libraries that should be preloaded at backend start
1279 process_local_preload_libraries(void)
1281 load_libraries(local_preload_libraries_string,
1282 "local_preload_libraries",
1287 pg_bindtextdomain(const char *domain)
1290 if (my_exec_path[0] != '\0')
1292 char locale_path[MAXPGPATH];
1294 get_locale_path(my_exec_path, locale_path);
1295 bindtextdomain(domain, locale_path);
1296 pg_bind_textdomain_codeset(domain);