]> granicus.if.org Git - postgresql/blob - src/backend/utils/init/miscinit.c
Stamp copyrights for year 2011.
[postgresql] / src / backend / utils / init / miscinit.c
1 /*-------------------------------------------------------------------------
2  *
3  * miscinit.c
4  *        miscellaneous initialization support stuff
5  *
6  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/utils/init/miscinit.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <sys/param.h>
18 #include <signal.h>
19 #include <sys/file.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <grp.h>
25 #include <pwd.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #ifdef HAVE_UTIME_H
29 #include <utime.h>
30 #endif
31
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"
46
47
48 #define DIRECTORY_LOCK_FILE             "postmaster.pid"
49 /*
50  *      The lock file contents are:
51  *
52  * line #
53  *              1       pid
54  *              2       postmaster start time
55  *              3       data directory
56  *              4       port #
57  *              5       user-specified socket directory
58  *                      (the lines below are added later)
59  *              6       first valid listen_address
60  *              7       shared memory key
61  */
62         
63 ProcessingMode Mode = InitProcessing;
64
65 /* Note: we rely on this to initialize as zeroes */
66 static char socketLockFile[MAXPGPATH];
67
68
69 /* ----------------------------------------------------------------
70  *              ignoring system indexes support stuff
71  *
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  * ----------------------------------------------------------------
77  */
78
79 bool            IgnoreSystemIndexes = false;
80
81
82 /* ----------------------------------------------------------------
83  *                              database path / name support stuff
84  * ----------------------------------------------------------------
85  */
86
87 void
88 SetDatabasePath(const char *path)
89 {
90         /* This should happen only once per process */
91         Assert(!DatabasePath);
92         DatabasePath = MemoryContextStrdup(TopMemoryContext, path);
93 }
94
95 /*
96  * Set data directory, but make sure it's an absolute path.  Use this,
97  * never set DataDir directly.
98  */
99 void
100 SetDataDir(const char *dir)
101 {
102         char       *new;
103
104         AssertArg(dir);
105
106         /* If presented path is relative, convert to absolute */
107         new = make_absolute_path(dir);
108
109         if (DataDir)
110                 free(DataDir);
111         DataDir = new;
112 }
113
114 /*
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.
119  */
120 void
121 ChangeToDataDir(void)
122 {
123         AssertState(DataDir);
124
125         if (chdir(DataDir) < 0)
126                 ereport(FATAL,
127                                 (errcode_for_file_access(),
128                                  errmsg("could not change directory to \"%s\": %m",
129                                                 DataDir)));
130 }
131
132 /*
133  * If the given pathname isn't already absolute, make it so, interpreting
134  * it relative to the current working directory.
135  *
136  * Also canonicalizes the path.  The result is always a malloc'd copy.
137  *
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.
141  */
142 char *
143 make_absolute_path(const char *path)
144 {
145         char       *new;
146
147         /* Returning null for null input is convenient for some callers */
148         if (path == NULL)
149                 return NULL;
150
151         if (!is_absolute_path(path))
152         {
153                 char       *buf;
154                 size_t          buflen;
155
156                 buflen = MAXPGPATH;
157                 for (;;)
158                 {
159                         buf = malloc(buflen);
160                         if (!buf)
161                                 ereport(FATAL,
162                                                 (errcode(ERRCODE_OUT_OF_MEMORY),
163                                                  errmsg("out of memory")));
164
165                         if (getcwd(buf, buflen))
166                                 break;
167                         else if (errno == ERANGE)
168                         {
169                                 free(buf);
170                                 buflen *= 2;
171                                 continue;
172                         }
173                         else
174                         {
175                                 free(buf);
176                                 elog(FATAL, "could not get current working directory: %m");
177                         }
178                 }
179
180                 new = malloc(strlen(buf) + strlen(path) + 2);
181                 if (!new)
182                         ereport(FATAL,
183                                         (errcode(ERRCODE_OUT_OF_MEMORY),
184                                          errmsg("out of memory")));
185                 sprintf(new, "%s/%s", buf, path);
186                 free(buf);
187         }
188         else
189         {
190                 new = strdup(path);
191                 if (!new)
192                         ereport(FATAL,
193                                         (errcode(ERRCODE_OUT_OF_MEMORY),
194                                          errmsg("out of memory")));
195         }
196
197         /* Make sure punctuation is canonical, too */
198         canonicalize_path(new);
199
200         return new;
201 }
202
203
204 /* ----------------------------------------------------------------
205  *      User ID state
206  *
207  * We have to track several different values associated with the concept
208  * of "user ID".
209  *
210  * AuthenticatedUserId is determined at connection start and never changes.
211  *
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.
215  *
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?)
220  *
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.
225  *
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  * ----------------------------------------------------------------
231  */
232 static Oid      AuthenticatedUserId = InvalidOid;
233 static Oid      SessionUserId = InvalidOid;
234 static Oid      OuterUserId = InvalidOid;
235 static Oid      CurrentUserId = InvalidOid;
236
237 /* We also have to remember the superuser state of some of these levels */
238 static bool AuthenticatedUserIsSuperuser = false;
239 static bool SessionUserIsSuperuser = false;
240
241 static int      SecurityRestrictionContext = 0;
242
243 /* We also remember if a SET ROLE is currently active */
244 static bool SetRoleIsActive = false;
245
246
247
248 /*
249  * GetUserId - get the current effective user ID.
250  *
251  * Note: there's no SetUserId() anymore; use SetUserIdAndSecContext().
252  */
253 Oid
254 GetUserId(void)
255 {
256         AssertState(OidIsValid(CurrentUserId));
257         return CurrentUserId;
258 }
259
260
261 /*
262  * GetOuterUserId/SetOuterUserId - get/set the outer-level user ID.
263  */
264 Oid
265 GetOuterUserId(void)
266 {
267         AssertState(OidIsValid(OuterUserId));
268         return OuterUserId;
269 }
270
271
272 static void
273 SetOuterUserId(Oid userid)
274 {
275         AssertState(SecurityRestrictionContext == 0);
276         AssertArg(OidIsValid(userid));
277         OuterUserId = userid;
278
279         /* We force the effective user ID to match, too */
280         CurrentUserId = userid;
281 }
282
283
284 /*
285  * GetSessionUserId/SetSessionUserId - get/set the session user ID.
286  */
287 Oid
288 GetSessionUserId(void)
289 {
290         AssertState(OidIsValid(SessionUserId));
291         return SessionUserId;
292 }
293
294
295 static void
296 SetSessionUserId(Oid userid, bool is_superuser)
297 {
298         AssertState(SecurityRestrictionContext == 0);
299         AssertArg(OidIsValid(userid));
300         SessionUserId = userid;
301         SessionUserIsSuperuser = is_superuser;
302         SetRoleIsActive = false;
303
304         /* We force the effective user IDs to match, too */
305         OuterUserId = userid;
306         CurrentUserId = userid;
307 }
308
309
310 /*
311  * GetUserIdAndSecContext/SetUserIdAndSecContext - get/set the current user ID
312  * and the SecurityRestrictionContext flags.
313  *
314  * Currently there are two valid bits in SecurityRestrictionContext:
315  *
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.
320  *
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.
331  *
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.
340  */
341 void
342 GetUserIdAndSecContext(Oid *userid, int *sec_context)
343 {
344         *userid = CurrentUserId;
345         *sec_context = SecurityRestrictionContext;
346 }
347
348 void
349 SetUserIdAndSecContext(Oid userid, int sec_context)
350 {
351         CurrentUserId = userid;
352         SecurityRestrictionContext = sec_context;
353 }
354
355
356 /*
357  * InLocalUserIdChange - are we inside a local change of CurrentUserId?
358  */
359 bool
360 InLocalUserIdChange(void)
361 {
362         return (SecurityRestrictionContext & SECURITY_LOCAL_USERID_CHANGE) != 0;
363 }
364
365 /*
366  * InSecurityRestrictedOperation - are we inside a security-restricted command?
367  */
368 bool
369 InSecurityRestrictedOperation(void)
370 {
371         return (SecurityRestrictionContext & SECURITY_RESTRICTED_OPERATION) != 0;
372 }
373
374
375 /*
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.
380  */
381 void
382 GetUserIdAndContext(Oid *userid, bool *sec_def_context)
383 {
384         *userid = CurrentUserId;
385         *sec_def_context = InLocalUserIdChange();
386 }
387
388 void
389 SetUserIdAndContext(Oid userid, bool sec_def_context)
390 {
391         /* We throw the same error SET ROLE would. */
392         if (InSecurityRestrictedOperation())
393                 ereport(ERROR,
394                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
395                                  errmsg("cannot set parameter \"%s\" within security-restricted operation",
396                                                 "role")));
397         CurrentUserId = userid;
398         if (sec_def_context)
399                 SecurityRestrictionContext |= SECURITY_LOCAL_USERID_CHANGE;
400         else
401                 SecurityRestrictionContext &= ~SECURITY_LOCAL_USERID_CHANGE;
402 }
403
404
405 /*
406  * Check if the authenticated user is a replication role
407  */
408 bool
409 is_authenticated_user_replication_role(void)
410 {
411         bool            result = false;
412         HeapTuple       utup;
413
414         utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(AuthenticatedUserId));
415         if (HeapTupleIsValid(utup))
416         {
417                 result = ((Form_pg_authid) GETSTRUCT(utup))->rolreplication;
418                 ReleaseSysCache(utup);
419         }
420         return result;
421 }
422
423 /*
424  * Initialize user identity during normal backend startup
425  */
426 void
427 InitializeSessionUserId(const char *rolename)
428 {
429         HeapTuple       roleTup;
430         Form_pg_authid rform;
431         Oid                     roleid;
432
433         /*
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.
436          */
437         AssertState(!IsBootstrapProcessingMode());
438
439         /* call only once */
440         AssertState(!OidIsValid(AuthenticatedUserId));
441
442         roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename));
443         if (!HeapTupleIsValid(roleTup))
444                 ereport(FATAL,
445                                 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
446                                  errmsg("role \"%s\" does not exist", rolename)));
447
448         rform = (Form_pg_authid) GETSTRUCT(roleTup);
449         roleid = HeapTupleGetOid(roleTup);
450
451         AuthenticatedUserId = roleid;
452         AuthenticatedUserIsSuperuser = rform->rolsuper;
453
454         /* This sets OuterUserId/CurrentUserId too */
455         SetSessionUserId(roleid, AuthenticatedUserIsSuperuser);
456
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;
460
461         /*
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;".
465          */
466         if (IsUnderPostmaster)
467         {
468                 /*
469                  * Is role allowed to login at all?
470                  */
471                 if (!rform->rolcanlogin)
472                         ereport(FATAL,
473                                         (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
474                                          errmsg("role \"%s\" is not permitted to log in",
475                                                         rolename)));
476
477                 /*
478                  * Check connection limit for this role.
479                  *
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.
486                  */
487                 if (rform->rolconnlimit >= 0 &&
488                         !AuthenticatedUserIsSuperuser &&
489                         CountUserBackends(roleid) > rform->rolconnlimit)
490                         ereport(FATAL,
491                                         (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
492                                          errmsg("too many connections for role \"%s\"",
493                                                         rolename)));
494         }
495
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);
502
503         ReleaseSysCache(roleTup);
504 }
505
506
507 /*
508  * Initialize user identity during special backend startup
509  */
510 void
511 InitializeSessionUserIdStandalone(void)
512 {
513         /*
514          * This function should only be called in single-user mode and in
515          * autovacuum workers.
516          */
517         AssertState(!IsUnderPostmaster || IsAutoVacuumWorkerProcess());
518
519         /* call only once */
520         AssertState(!OidIsValid(AuthenticatedUserId));
521
522         AuthenticatedUserId = BOOTSTRAP_SUPERUSERID;
523         AuthenticatedUserIsSuperuser = true;
524
525         SetSessionUserId(BOOTSTRAP_SUPERUSERID, true);
526 }
527
528
529 /*
530  * Change session auth ID while running
531  *
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.
536  *
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.
541  */
542 void
543 SetSessionAuthorization(Oid userid, bool is_superuser)
544 {
545         /* Must have authenticated already, else can't make permission check */
546         AssertState(OidIsValid(AuthenticatedUserId));
547
548         if (userid != AuthenticatedUserId &&
549                 !AuthenticatedUserIsSuperuser)
550                 ereport(ERROR,
551                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
552                                  errmsg("permission denied to set session authorization")));
553
554         SetSessionUserId(userid, is_superuser);
555
556         SetConfigOption("is_superuser",
557                                         is_superuser ? "on" : "off",
558                                         PGC_INTERNAL, PGC_S_OVERRIDE);
559 }
560
561 /*
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.
566  */
567 Oid
568 GetCurrentRoleId(void)
569 {
570         if (SetRoleIsActive)
571                 return OuterUserId;
572         else
573                 return InvalidOid;
574 }
575
576 /*
577  * Change Role ID while running (SET ROLE)
578  *
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
581  * is ignored.
582  *
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.)
587  */
588 void
589 SetCurrentRoleId(Oid roleid, bool is_superuser)
590 {
591         /*
592          * Get correct info if it's SET ROLE NONE
593          *
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.
597          */
598         if (!OidIsValid(roleid))
599         {
600                 if (!OidIsValid(SessionUserId))
601                         return;
602
603                 roleid = SessionUserId;
604                 is_superuser = SessionUserIsSuperuser;
605
606                 SetRoleIsActive = false;
607         }
608         else
609                 SetRoleIsActive = true;
610
611         SetOuterUserId(roleid);
612
613         SetConfigOption("is_superuser",
614                                         is_superuser ? "on" : "off",
615                                         PGC_INTERNAL, PGC_S_OVERRIDE);
616 }
617
618
619 /*
620  * Get user name from user oid
621  */
622 char *
623 GetUserNameFromId(Oid roleid)
624 {
625         HeapTuple       tuple;
626         char       *result;
627
628         tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
629         if (!HeapTupleIsValid(tuple))
630                 ereport(ERROR,
631                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
632                                  errmsg("invalid role OID: %u", roleid)));
633
634         result = pstrdup(NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname));
635
636         ReleaseSysCache(tuple);
637         return result;
638 }
639
640
641 /*-------------------------------------------------------------------------
642  *                              Interlock-file support
643  *
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:
647  *
648  *              Owning process' PID
649  *              Data directory path
650  *
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).
655  *
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.
658  *
659  * On successful lockfile creation, a proc_exit callback to remove the
660  * lockfile is automatically created.
661  *-------------------------------------------------------------------------
662  */
663
664 /*
665  * proc_exit callback to remove a lockfile.
666  */
667 static void
668 UnlinkLockFile(int status, Datum filename)
669 {
670         char       *fname = (char *) DatumGetPointer(filename);
671
672         if (fname != NULL)
673         {
674                 if (unlink(fname) != 0)
675                 {
676                         /* Should we complain if the unlink fails? */
677                 }
678                 free(fname);
679         }
680 }
681
682 /*
683  * Create a lockfile.
684  *
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.
688  */
689 static void
690 CreateLockFile(const char *filename, bool amPostmaster,
691                            bool isDDLock, const char *refName)
692 {
693         int                     fd;
694         char            buffer[MAXPGPATH * 3 + 256];
695         int                     ntries;
696         int                     len;
697         int                     encoded_pid;
698         pid_t           other_pid;
699         pid_t           my_pid,
700                                 my_p_pid,
701                                 my_gp_pid;
702         const char *envvar;
703
704         /*
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
720          * directly.
721          */
722         my_pid = getpid();
723
724 #ifndef WIN32
725         my_p_pid = getppid();
726 #else
727
728         /*
729          * Windows hasn't got getppid(), but doesn't need it since it's not using
730          * real kill() either...
731          */
732         my_p_pid = 0;
733 #endif
734
735         envvar = getenv("PG_GRANDPARENT_PID");
736         if (envvar)
737                 my_gp_pid = atoi(envvar);
738         else
739                 my_gp_pid = 0;
740
741         /*
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.
745          */
746         for (ntries = 0;; ntries++)
747         {
748                 /*
749                  * Try to create the lock file --- O_EXCL makes this atomic.
750                  *
751                  * Think not to make the file protection weaker than 0600.      See
752                  * comments below.
753                  */
754                 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
755                 if (fd >= 0)
756                         break;                          /* Success; exit the retry loop */
757
758                 /*
759                  * Couldn't create the pid file. Probably it already exists.
760                  */
761                 if ((errno != EEXIST && errno != EACCES) || ntries > 100)
762                         ereport(FATAL,
763                                         (errcode_for_file_access(),
764                                          errmsg("could not create lock file \"%s\": %m",
765                                                         filename)));
766
767                 /*
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.
770                  */
771                 fd = open(filename, O_RDONLY, 0600);
772                 if (fd < 0)
773                 {
774                         if (errno == ENOENT)
775                                 continue;               /* race condition; try again */
776                         ereport(FATAL,
777                                         (errcode_for_file_access(),
778                                          errmsg("could not open lock file \"%s\": %m",
779                                                         filename)));
780                 }
781                 if ((len = read(fd, buffer, sizeof(buffer) - 1)) < 0)
782                         ereport(FATAL,
783                                         (errcode_for_file_access(),
784                                          errmsg("could not read lock file \"%s\": %m",
785                                                         filename)));
786                 close(fd);
787
788                 buffer[len] = '\0';
789                 encoded_pid = atoi(buffer);
790
791                 /* if pid < 0, the pid is for postgres, not postmaster */
792                 other_pid = (pid_t) (encoded_pid < 0 ? -encoded_pid : encoded_pid);
793
794                 if (other_pid <= 0)
795                         elog(FATAL, "bogus data in lock file \"%s\": \"%s\"",
796                                  filename, buffer);
797
798                 /*
799                  * Check to see if the other process still exists
800                  *
801                  * Per discussion above, my_pid, my_p_pid, and my_gp_pid can be
802                  * ignored as false matches.
803                  *
804                  * Normally kill() will fail with ESRCH if the given PID doesn't
805                  * exist.
806                  *
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
821                  * stickybit.)
822                  */
823                 if (other_pid != my_pid && other_pid != my_p_pid &&
824                         other_pid != my_gp_pid)
825                 {
826                         if (kill(other_pid, 0) == 0 ||
827                                 (errno != ESRCH && errno != EPERM))
828                         {
829                                 /* lockfile belongs to a live process */
830                                 ereport(FATAL,
831                                                 (errcode(ERRCODE_LOCK_FILE_EXISTS),
832                                                  errmsg("lock file \"%s\" already exists",
833                                                                 filename),
834                                                  isDDLock ?
835                                                  (encoded_pid < 0 ?
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)) :
840                                                  (encoded_pid < 0 ?
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))));
845                         }
846                 }
847
848                 /*
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
853                  * still in use.
854                  *
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.
857                  */
858                 if (isDDLock)
859                 {
860                         char       *ptr = buffer;
861                         unsigned long id1, id2;
862                         int lineno;
863
864                         for (lineno = 1; lineno <= LOCK_FILE_LINES - 1; lineno++)
865                         {
866                                 if ((ptr = strchr(ptr, '\n')) == NULL)
867                                 {
868                                         elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
869                                         break;
870                                 }
871                                 ptr++;
872                         }
873
874                         if (ptr && sscanf(ptr, "%lu %lu", &id1, &id2) == 2)
875                         {
876                                 if (PGSharedMemoryIsInUse(id1, id2))
877                                         ereport(FATAL,
878                                                         (errcode(ERRCODE_LOCK_FILE_EXISTS),
879                                                          errmsg("pre-existing shared memory block "
880                                                                         "(key %lu, ID %lu) is still in use",
881                                                                         id1, id2),
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\".",
886                                                                          filename)));
887                         }
888                 }
889
890                 /*
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
893                  * would-be creators.
894                  */
895                 if (unlink(filename) < 0)
896                         ereport(FATAL,
897                                         (errcode_for_file_access(),
898                                          errmsg("could not remove old lock file \"%s\": %m",
899                                                         filename),
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.")));
903         }
904
905         /*
906          * Successfully created the file, now fill it.
907          */
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,
911                          UnixSocketDir);
912         errno = 0;
913         if (write(fd, buffer, strlen(buffer)) != strlen(buffer))
914         {
915                 int                     save_errno = errno;
916
917                 close(fd);
918                 unlink(filename);
919                 /* if write didn't set errno, assume problem is no disk space */
920                 errno = save_errno ? save_errno : ENOSPC;
921                 ereport(FATAL,
922                                 (errcode_for_file_access(),
923                                  errmsg("could not write lock file \"%s\": %m", filename)));
924         }
925         if (pg_fsync(fd) != 0)
926         {
927                 int                     save_errno = errno;
928
929                 close(fd);
930                 unlink(filename);
931                 errno = save_errno;
932                 ereport(FATAL,
933                                 (errcode_for_file_access(),
934                                  errmsg("could not write lock file \"%s\": %m", filename)));
935         }
936         if (close(fd) != 0)
937         {
938                 int                     save_errno = errno;
939
940                 unlink(filename);
941                 errno = save_errno;
942                 ereport(FATAL,
943                                 (errcode_for_file_access(),
944                                  errmsg("could not write lock file \"%s\": %m", filename)));
945         }
946
947         /*
948          * Arrange for automatic removal of lockfile at proc_exit.
949          */
950         on_proc_exit(UnlinkLockFile, PointerGetDatum(strdup(filename)));
951 }
952
953 /*
954  * Create the data directory lockfile.
955  *
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.
959  */
960 void
961 CreateDataDirLockFile(bool amPostmaster)
962 {
963         CreateLockFile(DIRECTORY_LOCK_FILE, amPostmaster, true, DataDir);
964 }
965
966 /*
967  * Create a lockfile for the specified Unix socket file.
968  */
969 void
970 CreateSocketLockFile(const char *socketfile, bool amPostmaster)
971 {
972         char            lockfile[MAXPGPATH];
973
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);
978 }
979
980 /*
981  * TouchSocketLockFile -- mark socket lock file as recently accessed
982  *
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...)
987  */
988 void
989 TouchSocketLockFile(void)
990 {
991         /* Do nothing if we did not create a socket... */
992         if (socketLockFile[0] != '\0')
993         {
994                 /*
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.
999                  */
1000 #ifdef HAVE_UTIME
1001                 utime(socketLockFile, NULL);
1002 #else                                                   /* !HAVE_UTIME */
1003 #ifdef HAVE_UTIMES
1004                 utimes(socketLockFile, NULL);
1005 #else                                                   /* !HAVE_UTIMES */
1006                 int                     fd;
1007                 char            buffer[1];
1008
1009                 fd = open(socketLockFile, O_RDONLY | PG_BINARY, 0);
1010                 if (fd >= 0)
1011                 {
1012                         read(fd, buffer, sizeof(buffer));
1013                         close(fd);
1014                 }
1015 #endif   /* HAVE_UTIMES */
1016 #endif   /* HAVE_UTIME */
1017         }
1018 }
1019
1020
1021 /*
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.
1024  */
1025 void
1026 AddToLockFile(int target_line, const char *str)
1027 {
1028         int                     fd;
1029         int                     len;
1030         int                     lineno;
1031         char       *ptr;
1032         char            buffer[MAXPGPATH * 3 + 256];
1033
1034         fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
1035         if (fd < 0)
1036         {
1037                 ereport(LOG,
1038                                 (errcode_for_file_access(),
1039                                  errmsg("could not open file \"%s\": %m",
1040                                                 DIRECTORY_LOCK_FILE)));
1041                 return;
1042         }
1043         len = read(fd, buffer, sizeof(buffer) - 100);
1044         if (len < 0)
1045         {
1046                 ereport(LOG,
1047                                 (errcode_for_file_access(),
1048                                  errmsg("could not read from file \"%s\": %m",
1049                                                 DIRECTORY_LOCK_FILE)));
1050                 close(fd);
1051                 return;
1052         }
1053         buffer[len] = '\0';
1054
1055         /*
1056          * Skip over first four lines (PID, pgdata, portnum, socketdir).
1057          */
1058         ptr = buffer;
1059         for (lineno = 1; lineno < target_line; lineno++)
1060         {
1061                 if ((ptr = strchr(ptr, '\n')) == NULL)
1062                 {
1063                         elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
1064                         close(fd);
1065                         return;
1066                 }
1067                 ptr++;
1068         }
1069         
1070         strlcat(buffer, str, sizeof(buffer));
1071
1072         /*
1073          * And rewrite the data.  Since we write in a single kernel call, this
1074          * update should appear atomic to onlookers.
1075          */
1076         len = strlen(buffer);
1077         errno = 0;
1078         if (lseek(fd, (off_t) 0, SEEK_SET) != 0 ||
1079                 (int) write(fd, buffer, len) != len)
1080         {
1081                 /* if write didn't set errno, assume problem is no disk space */
1082                 if (errno == 0)
1083                         errno = ENOSPC;
1084                 ereport(LOG,
1085                                 (errcode_for_file_access(),
1086                                  errmsg("could not write to file \"%s\": %m",
1087                                                 DIRECTORY_LOCK_FILE)));
1088                 close(fd);
1089                 return;
1090         }
1091         if (pg_fsync(fd) != 0)
1092         {
1093                 ereport(LOG,
1094                                 (errcode_for_file_access(),
1095                                  errmsg("could not write to file \"%s\": %m",
1096                                                 DIRECTORY_LOCK_FILE)));
1097         }
1098         if (close(fd) != 0)
1099         {
1100                 ereport(LOG,
1101                                 (errcode_for_file_access(),
1102                                  errmsg("could not write to file \"%s\": %m",
1103                                                 DIRECTORY_LOCK_FILE)));
1104         }
1105 }
1106
1107
1108 /*-------------------------------------------------------------------------
1109  *                              Version checking support
1110  *-------------------------------------------------------------------------
1111  */
1112
1113 /*
1114  * Determine whether the PG_VERSION file in directory `path' indicates
1115  * a data version compatible with the version of this program.
1116  *
1117  * If compatible, return. Otherwise, ereport(FATAL).
1118  */
1119 void
1120 ValidatePgVersion(const char *path)
1121 {
1122         char            full_path[MAXPGPATH];
1123         FILE       *file;
1124         int                     ret;
1125         long            file_major,
1126                                 file_minor;
1127         long            my_major = 0,
1128                                 my_minor = 0;
1129         char       *endptr;
1130         const char *version_string = PG_VERSION;
1131
1132         my_major = strtol(version_string, &endptr, 10);
1133         if (*endptr == '.')
1134                 my_minor = strtol(endptr + 1, NULL, 10);
1135
1136         snprintf(full_path, sizeof(full_path), "%s/PG_VERSION", path);
1137
1138         file = AllocateFile(full_path, "r");
1139         if (!file)
1140         {
1141                 if (errno == ENOENT)
1142                         ereport(FATAL,
1143                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1144                                          errmsg("\"%s\" is not a valid data directory",
1145                                                         path),
1146                                          errdetail("File \"%s\" is missing.", full_path)));
1147                 else
1148                         ereport(FATAL,
1149                                         (errcode_for_file_access(),
1150                                          errmsg("could not open file \"%s\": %m", full_path)));
1151         }
1152
1153         ret = fscanf(file, "%ld.%ld", &file_major, &file_minor);
1154         if (ret != 2)
1155                 ereport(FATAL,
1156                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1157                                  errmsg("\"%s\" is not a valid data directory",
1158                                                 path),
1159                                  errdetail("File \"%s\" does not contain valid data.",
1160                                                    full_path),
1161                                  errhint("You might need to initdb.")));
1162
1163         FreeFile(file);
1164
1165         if (my_major != file_major || my_minor != file_minor)
1166                 ereport(FATAL,
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)));
1172 }
1173
1174 /*-------------------------------------------------------------------------
1175  *                              Library preload support
1176  *-------------------------------------------------------------------------
1177  */
1178
1179 /*
1180  * GUC variables: lists of library names to be preloaded at postmaster
1181  * start and at backend start
1182  */
1183 char       *shared_preload_libraries_string = NULL;
1184 char       *local_preload_libraries_string = NULL;
1185
1186 /* Flag telling that we are loading shared_preload_libraries */
1187 bool            process_shared_preload_libraries_in_progress = false;
1188
1189 /*
1190  * load the shared libraries listed in 'libraries'
1191  *
1192  * 'gucname': name of GUC variable, for error reports
1193  * 'restricted': if true, force libraries to be in $libdir/plugins/
1194  */
1195 static void
1196 load_libraries(const char *libraries, const char *gucname, bool restricted)
1197 {
1198         char       *rawstring;
1199         List       *elemlist;
1200         int                     elevel;
1201         ListCell   *l;
1202
1203         if (libraries == NULL || libraries[0] == '\0')
1204                 return;                                 /* nothing to do */
1205
1206         /* Need a modifiable copy of string */
1207         rawstring = pstrdup(libraries);
1208
1209         /* Parse string into list of identifiers */
1210         if (!SplitIdentifierString(rawstring, ',', &elemlist))
1211         {
1212                 /* syntax error in list */
1213                 pfree(rawstring);
1214                 list_free(elemlist);
1215                 ereport(LOG,
1216                                 (errcode(ERRCODE_SYNTAX_ERROR),
1217                                  errmsg("invalid list syntax in parameter \"%s\"",
1218                                                 gucname)));
1219                 return;
1220         }
1221
1222         /*
1223          * Choose notice level: avoid repeat messages when re-loading a library
1224          * that was preloaded into the postmaster.      (Only possible in EXEC_BACKEND
1225          * configurations)
1226          */
1227 #ifdef EXEC_BACKEND
1228         if (IsUnderPostmaster && process_shared_preload_libraries_in_progress)
1229                 elevel = DEBUG2;
1230         else
1231 #endif
1232                 elevel = LOG;
1233
1234         foreach(l, elemlist)
1235         {
1236                 char       *tok = (char *) lfirst(l);
1237                 char       *filename;
1238
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)
1243                 {
1244                         char       *expanded;
1245
1246                         expanded = palloc(strlen("$libdir/plugins/") + strlen(filename) + 1);
1247                         strcpy(expanded, "$libdir/plugins/");
1248                         strcat(expanded, filename);
1249                         pfree(filename);
1250                         filename = expanded;
1251                 }
1252                 load_file(filename, restricted);
1253                 ereport(elevel,
1254                                 (errmsg("loaded library \"%s\"", filename)));
1255                 pfree(filename);
1256         }
1257
1258         pfree(rawstring);
1259         list_free(elemlist);
1260 }
1261
1262 /*
1263  * process any libraries that should be preloaded at postmaster start
1264  */
1265 void
1266 process_shared_preload_libraries(void)
1267 {
1268         process_shared_preload_libraries_in_progress = true;
1269         load_libraries(shared_preload_libraries_string,
1270                                    "shared_preload_libraries",
1271                                    false);
1272         process_shared_preload_libraries_in_progress = false;
1273 }
1274
1275 /*
1276  * process any libraries that should be preloaded at backend start
1277  */
1278 void
1279 process_local_preload_libraries(void)
1280 {
1281         load_libraries(local_preload_libraries_string,
1282                                    "local_preload_libraries",
1283                                    true);
1284 }
1285
1286 void
1287 pg_bindtextdomain(const char *domain)
1288 {
1289 #ifdef ENABLE_NLS
1290         if (my_exec_path[0] != '\0')
1291         {
1292                 char            locale_path[MAXPGPATH];
1293
1294                 get_locale_path(my_exec_path, locale_path);
1295                 bindtextdomain(domain, locale_path);
1296                 pg_bind_textdomain_codeset(domain);
1297         }
1298 #endif
1299 }