]> granicus.if.org Git - postgresql/blob - src/backend/utils/init/miscinit.c
Add REPLICATION privilege for ROLEs
[postgresql] / src / backend / utils / init / miscinit.c
1 /*-------------------------------------------------------------------------
2  *
3  * miscinit.c
4  *        miscellaneous initialization support stuff
5  *
6  * Portions Copyright (c) 1996-2010, 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 ProcessingMode Mode = InitProcessing;
51
52 /* Note: we rely on this to initialize as zeroes */
53 static char socketLockFile[MAXPGPATH];
54
55
56 /* ----------------------------------------------------------------
57  *              ignoring system indexes support stuff
58  *
59  * NOTE: "ignoring system indexes" means we do not use the system indexes
60  * for lookups (either in hardwired catalog accesses or in planner-generated
61  * plans).      We do, however, still update the indexes when a catalog
62  * modification is made.
63  * ----------------------------------------------------------------
64  */
65
66 bool            IgnoreSystemIndexes = false;
67
68
69 /* ----------------------------------------------------------------
70  *                              database path / name support stuff
71  * ----------------------------------------------------------------
72  */
73
74 void
75 SetDatabasePath(const char *path)
76 {
77         /* This should happen only once per process */
78         Assert(!DatabasePath);
79         DatabasePath = MemoryContextStrdup(TopMemoryContext, path);
80 }
81
82 /*
83  * Set data directory, but make sure it's an absolute path.  Use this,
84  * never set DataDir directly.
85  */
86 void
87 SetDataDir(const char *dir)
88 {
89         char       *new;
90
91         AssertArg(dir);
92
93         /* If presented path is relative, convert to absolute */
94         new = make_absolute_path(dir);
95
96         if (DataDir)
97                 free(DataDir);
98         DataDir = new;
99 }
100
101 /*
102  * Change working directory to DataDir.  Most of the postmaster and backend
103  * code assumes that we are in DataDir so it can use relative paths to access
104  * stuff in and under the data directory.  For convenience during path
105  * setup, however, we don't force the chdir to occur during SetDataDir.
106  */
107 void
108 ChangeToDataDir(void)
109 {
110         AssertState(DataDir);
111
112         if (chdir(DataDir) < 0)
113                 ereport(FATAL,
114                                 (errcode_for_file_access(),
115                                  errmsg("could not change directory to \"%s\": %m",
116                                                 DataDir)));
117 }
118
119 /*
120  * If the given pathname isn't already absolute, make it so, interpreting
121  * it relative to the current working directory.
122  *
123  * Also canonicalizes the path.  The result is always a malloc'd copy.
124  *
125  * Note: interpretation of relative-path arguments during postmaster startup
126  * should happen before doing ChangeToDataDir(), else the user will probably
127  * not like the results.
128  */
129 char *
130 make_absolute_path(const char *path)
131 {
132         char       *new;
133
134         /* Returning null for null input is convenient for some callers */
135         if (path == NULL)
136                 return NULL;
137
138         if (!is_absolute_path(path))
139         {
140                 char       *buf;
141                 size_t          buflen;
142
143                 buflen = MAXPGPATH;
144                 for (;;)
145                 {
146                         buf = malloc(buflen);
147                         if (!buf)
148                                 ereport(FATAL,
149                                                 (errcode(ERRCODE_OUT_OF_MEMORY),
150                                                  errmsg("out of memory")));
151
152                         if (getcwd(buf, buflen))
153                                 break;
154                         else if (errno == ERANGE)
155                         {
156                                 free(buf);
157                                 buflen *= 2;
158                                 continue;
159                         }
160                         else
161                         {
162                                 free(buf);
163                                 elog(FATAL, "could not get current working directory: %m");
164                         }
165                 }
166
167                 new = malloc(strlen(buf) + strlen(path) + 2);
168                 if (!new)
169                         ereport(FATAL,
170                                         (errcode(ERRCODE_OUT_OF_MEMORY),
171                                          errmsg("out of memory")));
172                 sprintf(new, "%s/%s", buf, path);
173                 free(buf);
174         }
175         else
176         {
177                 new = strdup(path);
178                 if (!new)
179                         ereport(FATAL,
180                                         (errcode(ERRCODE_OUT_OF_MEMORY),
181                                          errmsg("out of memory")));
182         }
183
184         /* Make sure punctuation is canonical, too */
185         canonicalize_path(new);
186
187         return new;
188 }
189
190
191 /* ----------------------------------------------------------------
192  *      User ID state
193  *
194  * We have to track several different values associated with the concept
195  * of "user ID".
196  *
197  * AuthenticatedUserId is determined at connection start and never changes.
198  *
199  * SessionUserId is initially the same as AuthenticatedUserId, but can be
200  * changed by SET SESSION AUTHORIZATION (if AuthenticatedUserIsSuperuser).
201  * This is the ID reported by the SESSION_USER SQL function.
202  *
203  * OuterUserId is the current user ID in effect at the "outer level" (outside
204  * any transaction or function).  This is initially the same as SessionUserId,
205  * but can be changed by SET ROLE to any role that SessionUserId is a
206  * member of.  (XXX rename to something like CurrentRoleId?)
207  *
208  * CurrentUserId is the current effective user ID; this is the one to use
209  * for all normal permissions-checking purposes.  At outer level this will
210  * be the same as OuterUserId, but it changes during calls to SECURITY
211  * DEFINER functions, as well as locally in some specialized commands.
212  *
213  * SecurityRestrictionContext holds flags indicating reason(s) for changing
214  * CurrentUserId.  In some cases we need to lock down operations that are
215  * not directly controlled by privilege settings, and this provides a
216  * convenient way to do it.
217  * ----------------------------------------------------------------
218  */
219 static Oid      AuthenticatedUserId = InvalidOid;
220 static Oid      SessionUserId = InvalidOid;
221 static Oid      OuterUserId = InvalidOid;
222 static Oid      CurrentUserId = InvalidOid;
223
224 /* We also have to remember the superuser state of some of these levels */
225 static bool AuthenticatedUserIsSuperuser = false;
226 static bool SessionUserIsSuperuser = false;
227
228 static int      SecurityRestrictionContext = 0;
229
230 /* We also remember if a SET ROLE is currently active */
231 static bool SetRoleIsActive = false;
232
233
234
235 /*
236  * GetUserId - get the current effective user ID.
237  *
238  * Note: there's no SetUserId() anymore; use SetUserIdAndSecContext().
239  */
240 Oid
241 GetUserId(void)
242 {
243         AssertState(OidIsValid(CurrentUserId));
244         return CurrentUserId;
245 }
246
247
248 /*
249  * GetOuterUserId/SetOuterUserId - get/set the outer-level user ID.
250  */
251 Oid
252 GetOuterUserId(void)
253 {
254         AssertState(OidIsValid(OuterUserId));
255         return OuterUserId;
256 }
257
258
259 static void
260 SetOuterUserId(Oid userid)
261 {
262         AssertState(SecurityRestrictionContext == 0);
263         AssertArg(OidIsValid(userid));
264         OuterUserId = userid;
265
266         /* We force the effective user ID to match, too */
267         CurrentUserId = userid;
268 }
269
270
271 /*
272  * GetSessionUserId/SetSessionUserId - get/set the session user ID.
273  */
274 Oid
275 GetSessionUserId(void)
276 {
277         AssertState(OidIsValid(SessionUserId));
278         return SessionUserId;
279 }
280
281
282 static void
283 SetSessionUserId(Oid userid, bool is_superuser)
284 {
285         AssertState(SecurityRestrictionContext == 0);
286         AssertArg(OidIsValid(userid));
287         SessionUserId = userid;
288         SessionUserIsSuperuser = is_superuser;
289         SetRoleIsActive = false;
290
291         /* We force the effective user IDs to match, too */
292         OuterUserId = userid;
293         CurrentUserId = userid;
294 }
295
296
297 /*
298  * GetUserIdAndSecContext/SetUserIdAndSecContext - get/set the current user ID
299  * and the SecurityRestrictionContext flags.
300  *
301  * Currently there are two valid bits in SecurityRestrictionContext:
302  *
303  * SECURITY_LOCAL_USERID_CHANGE indicates that we are inside an operation
304  * that is temporarily changing CurrentUserId via these functions.      This is
305  * needed to indicate that the actual value of CurrentUserId is not in sync
306  * with guc.c's internal state, so SET ROLE has to be disallowed.
307  *
308  * SECURITY_RESTRICTED_OPERATION indicates that we are inside an operation
309  * that does not wish to trust called user-defined functions at all.  This
310  * bit prevents not only SET ROLE, but various other changes of session state
311  * that normally is unprotected but might possibly be used to subvert the
312  * calling session later.  An example is replacing an existing prepared
313  * statement with new code, which will then be executed with the outer
314  * session's permissions when the prepared statement is next used.  Since
315  * these restrictions are fairly draconian, we apply them only in contexts
316  * where the called functions are really supposed to be side-effect-free
317  * anyway, such as VACUUM/ANALYZE/REINDEX.
318  *
319  * Unlike GetUserId, GetUserIdAndSecContext does *not* Assert that the current
320  * value of CurrentUserId is valid; nor does SetUserIdAndSecContext require
321  * the new value to be valid.  In fact, these routines had better not
322  * ever throw any kind of error.  This is because they are used by
323  * StartTransaction and AbortTransaction to save/restore the settings,
324  * and during the first transaction within a backend, the value to be saved
325  * and perhaps restored is indeed invalid.      We have to be able to get
326  * through AbortTransaction without asserting in case InitPostgres fails.
327  */
328 void
329 GetUserIdAndSecContext(Oid *userid, int *sec_context)
330 {
331         *userid = CurrentUserId;
332         *sec_context = SecurityRestrictionContext;
333 }
334
335 void
336 SetUserIdAndSecContext(Oid userid, int sec_context)
337 {
338         CurrentUserId = userid;
339         SecurityRestrictionContext = sec_context;
340 }
341
342
343 /*
344  * InLocalUserIdChange - are we inside a local change of CurrentUserId?
345  */
346 bool
347 InLocalUserIdChange(void)
348 {
349         return (SecurityRestrictionContext & SECURITY_LOCAL_USERID_CHANGE) != 0;
350 }
351
352 /*
353  * InSecurityRestrictedOperation - are we inside a security-restricted command?
354  */
355 bool
356 InSecurityRestrictedOperation(void)
357 {
358         return (SecurityRestrictionContext & SECURITY_RESTRICTED_OPERATION) != 0;
359 }
360
361
362 /*
363  * These are obsolete versions of Get/SetUserIdAndSecContext that are
364  * only provided for bug-compatibility with some rather dubious code in
365  * pljava.      We allow the userid to be set, but only when not inside a
366  * security restriction context.
367  */
368 void
369 GetUserIdAndContext(Oid *userid, bool *sec_def_context)
370 {
371         *userid = CurrentUserId;
372         *sec_def_context = InLocalUserIdChange();
373 }
374
375 void
376 SetUserIdAndContext(Oid userid, bool sec_def_context)
377 {
378         /* We throw the same error SET ROLE would. */
379         if (InSecurityRestrictedOperation())
380                 ereport(ERROR,
381                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
382                                  errmsg("cannot set parameter \"%s\" within security-restricted operation",
383                                                 "role")));
384         CurrentUserId = userid;
385         if (sec_def_context)
386                 SecurityRestrictionContext |= SECURITY_LOCAL_USERID_CHANGE;
387         else
388                 SecurityRestrictionContext &= ~SECURITY_LOCAL_USERID_CHANGE;
389 }
390
391
392 /*
393  * Check if the authenticated user is a replication role
394  */
395 bool
396 is_authenticated_user_replication_role(void)
397 {
398         bool            result = false;
399         HeapTuple       utup;
400
401         utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(AuthenticatedUserId));
402         if (HeapTupleIsValid(utup))
403         {
404                 result = ((Form_pg_authid) GETSTRUCT(utup))->rolreplication;
405                 ReleaseSysCache(utup);
406         }
407         return result;
408 }
409
410 /*
411  * Initialize user identity during normal backend startup
412  */
413 void
414 InitializeSessionUserId(const char *rolename)
415 {
416         HeapTuple       roleTup;
417         Form_pg_authid rform;
418         Oid                     roleid;
419
420         /*
421          * Don't do scans if we're bootstrapping, none of the system catalogs
422          * exist yet, and they should be owned by postgres anyway.
423          */
424         AssertState(!IsBootstrapProcessingMode());
425
426         /* call only once */
427         AssertState(!OidIsValid(AuthenticatedUserId));
428
429         roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename));
430         if (!HeapTupleIsValid(roleTup))
431                 ereport(FATAL,
432                                 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
433                                  errmsg("role \"%s\" does not exist", rolename)));
434
435         rform = (Form_pg_authid) GETSTRUCT(roleTup);
436         roleid = HeapTupleGetOid(roleTup);
437
438         AuthenticatedUserId = roleid;
439         AuthenticatedUserIsSuperuser = rform->rolsuper;
440
441         /* This sets OuterUserId/CurrentUserId too */
442         SetSessionUserId(roleid, AuthenticatedUserIsSuperuser);
443
444         /* Also mark our PGPROC entry with the authenticated user id */
445         /* (We assume this is an atomic store so no lock is needed) */
446         MyProc->roleId = roleid;
447
448         /*
449          * These next checks are not enforced when in standalone mode, so that
450          * there is a way to recover from sillinesses like "UPDATE pg_authid SET
451          * rolcanlogin = false;".
452          */
453         if (IsUnderPostmaster)
454         {
455                 /*
456                  * Is role allowed to login at all?
457                  */
458                 if (!rform->rolcanlogin)
459                         ereport(FATAL,
460                                         (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
461                                          errmsg("role \"%s\" is not permitted to log in",
462                                                         rolename)));
463
464                 /*
465                  * Check connection limit for this role.
466                  *
467                  * There is a race condition here --- we create our PGPROC before
468                  * checking for other PGPROCs.  If two backends did this at about the
469                  * same time, they might both think they were over the limit, while
470                  * ideally one should succeed and one fail.  Getting that to work
471                  * exactly seems more trouble than it is worth, however; instead we
472                  * just document that the connection limit is approximate.
473                  */
474                 if (rform->rolconnlimit >= 0 &&
475                         !AuthenticatedUserIsSuperuser &&
476                         CountUserBackends(roleid) > rform->rolconnlimit)
477                         ereport(FATAL,
478                                         (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
479                                          errmsg("too many connections for role \"%s\"",
480                                                         rolename)));
481         }
482
483         /* Record username and superuser status as GUC settings too */
484         SetConfigOption("session_authorization", rolename,
485                                         PGC_BACKEND, PGC_S_OVERRIDE);
486         SetConfigOption("is_superuser",
487                                         AuthenticatedUserIsSuperuser ? "on" : "off",
488                                         PGC_INTERNAL, PGC_S_OVERRIDE);
489
490         ReleaseSysCache(roleTup);
491 }
492
493
494 /*
495  * Initialize user identity during special backend startup
496  */
497 void
498 InitializeSessionUserIdStandalone(void)
499 {
500         /*
501          * This function should only be called in single-user mode and in
502          * autovacuum workers.
503          */
504         AssertState(!IsUnderPostmaster || IsAutoVacuumWorkerProcess());
505
506         /* call only once */
507         AssertState(!OidIsValid(AuthenticatedUserId));
508
509         AuthenticatedUserId = BOOTSTRAP_SUPERUSERID;
510         AuthenticatedUserIsSuperuser = true;
511
512         SetSessionUserId(BOOTSTRAP_SUPERUSERID, true);
513 }
514
515
516 /*
517  * Change session auth ID while running
518  *
519  * Only a superuser may set auth ID to something other than himself.  Note
520  * that in case of multiple SETs in a single session, the original userid's
521  * superuserness is what matters.  But we set the GUC variable is_superuser
522  * to indicate whether the *current* session userid is a superuser.
523  *
524  * Note: this is not an especially clean place to do the permission check.
525  * It's OK because the check does not require catalog access and can't
526  * fail during an end-of-transaction GUC reversion, but we may someday
527  * have to push it up into assign_session_authorization.
528  */
529 void
530 SetSessionAuthorization(Oid userid, bool is_superuser)
531 {
532         /* Must have authenticated already, else can't make permission check */
533         AssertState(OidIsValid(AuthenticatedUserId));
534
535         if (userid != AuthenticatedUserId &&
536                 !AuthenticatedUserIsSuperuser)
537                 ereport(ERROR,
538                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
539                                  errmsg("permission denied to set session authorization")));
540
541         SetSessionUserId(userid, is_superuser);
542
543         SetConfigOption("is_superuser",
544                                         is_superuser ? "on" : "off",
545                                         PGC_INTERNAL, PGC_S_OVERRIDE);
546 }
547
548 /*
549  * Report current role id
550  *              This follows the semantics of SET ROLE, ie return the outer-level ID
551  *              not the current effective ID, and return InvalidOid when the setting
552  *              is logically SET ROLE NONE.
553  */
554 Oid
555 GetCurrentRoleId(void)
556 {
557         if (SetRoleIsActive)
558                 return OuterUserId;
559         else
560                 return InvalidOid;
561 }
562
563 /*
564  * Change Role ID while running (SET ROLE)
565  *
566  * If roleid is InvalidOid, we are doing SET ROLE NONE: revert to the
567  * session user authorization.  In this case the is_superuser argument
568  * is ignored.
569  *
570  * When roleid is not InvalidOid, the caller must have checked whether
571  * the session user has permission to become that role.  (We cannot check
572  * here because this routine must be able to execute in a failed transaction
573  * to restore a prior value of the ROLE GUC variable.)
574  */
575 void
576 SetCurrentRoleId(Oid roleid, bool is_superuser)
577 {
578         /*
579          * Get correct info if it's SET ROLE NONE
580          *
581          * If SessionUserId hasn't been set yet, just do nothing --- the eventual
582          * SetSessionUserId call will fix everything.  This is needed since we
583          * will get called during GUC initialization.
584          */
585         if (!OidIsValid(roleid))
586         {
587                 if (!OidIsValid(SessionUserId))
588                         return;
589
590                 roleid = SessionUserId;
591                 is_superuser = SessionUserIsSuperuser;
592
593                 SetRoleIsActive = false;
594         }
595         else
596                 SetRoleIsActive = true;
597
598         SetOuterUserId(roleid);
599
600         SetConfigOption("is_superuser",
601                                         is_superuser ? "on" : "off",
602                                         PGC_INTERNAL, PGC_S_OVERRIDE);
603 }
604
605
606 /*
607  * Get user name from user oid
608  */
609 char *
610 GetUserNameFromId(Oid roleid)
611 {
612         HeapTuple       tuple;
613         char       *result;
614
615         tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
616         if (!HeapTupleIsValid(tuple))
617                 ereport(ERROR,
618                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
619                                  errmsg("invalid role OID: %u", roleid)));
620
621         result = pstrdup(NameStr(((Form_pg_authid) GETSTRUCT(tuple))->rolname));
622
623         ReleaseSysCache(tuple);
624         return result;
625 }
626
627
628 /*-------------------------------------------------------------------------
629  *                              Interlock-file support
630  *
631  * These routines are used to create both a data-directory lockfile
632  * ($DATADIR/postmaster.pid) and a Unix-socket-file lockfile ($SOCKFILE.lock).
633  * Both kinds of files contain the same info:
634  *
635  *              Owning process' PID
636  *              Data directory path
637  *
638  * By convention, the owning process' PID is negated if it is a standalone
639  * backend rather than a postmaster.  This is just for informational purposes.
640  * The path is also just for informational purposes (so that a socket lockfile
641  * can be more easily traced to the associated postmaster).
642  *
643  * A data-directory lockfile can optionally contain a third line, containing
644  * the key and ID for the shared memory block used by this postmaster.
645  *
646  * On successful lockfile creation, a proc_exit callback to remove the
647  * lockfile is automatically created.
648  *-------------------------------------------------------------------------
649  */
650
651 /*
652  * proc_exit callback to remove a lockfile.
653  */
654 static void
655 UnlinkLockFile(int status, Datum filename)
656 {
657         char       *fname = (char *) DatumGetPointer(filename);
658
659         if (fname != NULL)
660         {
661                 if (unlink(fname) != 0)
662                 {
663                         /* Should we complain if the unlink fails? */
664                 }
665                 free(fname);
666         }
667 }
668
669 /*
670  * Create a lockfile.
671  *
672  * filename is the name of the lockfile to create.
673  * amPostmaster is used to determine how to encode the output PID.
674  * isDDLock and refName are used to determine what error message to produce.
675  */
676 static void
677 CreateLockFile(const char *filename, bool amPostmaster,
678                            bool isDDLock, const char *refName)
679 {
680         int                     fd;
681         char            buffer[MAXPGPATH * 2 + 256];
682         int                     ntries;
683         int                     len;
684         int                     encoded_pid;
685         pid_t           other_pid;
686         pid_t           my_pid,
687                                 my_p_pid,
688                                 my_gp_pid;
689         const char *envvar;
690
691         /*
692          * If the PID in the lockfile is our own PID or our parent's or
693          * grandparent's PID, then the file must be stale (probably left over from
694          * a previous system boot cycle).  We need to check this because of the
695          * likelihood that a reboot will assign exactly the same PID as we had in
696          * the previous reboot, or one that's only one or two counts larger and
697          * hence the lockfile's PID now refers to an ancestor shell process.  We
698          * allow pg_ctl to pass down its parent shell PID (our grandparent PID)
699          * via the environment variable PG_GRANDPARENT_PID; this is so that
700          * launching the postmaster via pg_ctl can be just as reliable as
701          * launching it directly.  There is no provision for detecting
702          * further-removed ancestor processes, but if the init script is written
703          * carefully then all but the immediate parent shell will be root-owned
704          * processes and so the kill test will fail with EPERM.  Note that we
705          * cannot get a false negative this way, because an existing postmaster
706          * would surely never launch a competing postmaster or pg_ctl process
707          * directly.
708          */
709         my_pid = getpid();
710
711 #ifndef WIN32
712         my_p_pid = getppid();
713 #else
714
715         /*
716          * Windows hasn't got getppid(), but doesn't need it since it's not using
717          * real kill() either...
718          */
719         my_p_pid = 0;
720 #endif
721
722         envvar = getenv("PG_GRANDPARENT_PID");
723         if (envvar)
724                 my_gp_pid = atoi(envvar);
725         else
726                 my_gp_pid = 0;
727
728         /*
729          * We need a loop here because of race conditions.      But don't loop forever
730          * (for example, a non-writable $PGDATA directory might cause a failure
731          * that won't go away).  100 tries seems like plenty.
732          */
733         for (ntries = 0;; ntries++)
734         {
735                 /*
736                  * Try to create the lock file --- O_EXCL makes this atomic.
737                  *
738                  * Think not to make the file protection weaker than 0600.      See
739                  * comments below.
740                  */
741                 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
742                 if (fd >= 0)
743                         break;                          /* Success; exit the retry loop */
744
745                 /*
746                  * Couldn't create the pid file. Probably it already exists.
747                  */
748                 if ((errno != EEXIST && errno != EACCES) || ntries > 100)
749                         ereport(FATAL,
750                                         (errcode_for_file_access(),
751                                          errmsg("could not create lock file \"%s\": %m",
752                                                         filename)));
753
754                 /*
755                  * Read the file to get the old owner's PID.  Note race condition
756                  * here: file might have been deleted since we tried to create it.
757                  */
758                 fd = open(filename, O_RDONLY, 0600);
759                 if (fd < 0)
760                 {
761                         if (errno == ENOENT)
762                                 continue;               /* race condition; try again */
763                         ereport(FATAL,
764                                         (errcode_for_file_access(),
765                                          errmsg("could not open lock file \"%s\": %m",
766                                                         filename)));
767                 }
768                 if ((len = read(fd, buffer, sizeof(buffer) - 1)) < 0)
769                         ereport(FATAL,
770                                         (errcode_for_file_access(),
771                                          errmsg("could not read lock file \"%s\": %m",
772                                                         filename)));
773                 close(fd);
774
775                 buffer[len] = '\0';
776                 encoded_pid = atoi(buffer);
777
778                 /* if pid < 0, the pid is for postgres, not postmaster */
779                 other_pid = (pid_t) (encoded_pid < 0 ? -encoded_pid : encoded_pid);
780
781                 if (other_pid <= 0)
782                         elog(FATAL, "bogus data in lock file \"%s\": \"%s\"",
783                                  filename, buffer);
784
785                 /*
786                  * Check to see if the other process still exists
787                  *
788                  * Per discussion above, my_pid, my_p_pid, and my_gp_pid can be
789                  * ignored as false matches.
790                  *
791                  * Normally kill() will fail with ESRCH if the given PID doesn't
792                  * exist.
793                  *
794                  * We can treat the EPERM-error case as okay because that error
795                  * implies that the existing process has a different userid than we
796                  * do, which means it cannot be a competing postmaster.  A postmaster
797                  * cannot successfully attach to a data directory owned by a userid
798                  * other than its own.  (This is now checked directly in
799                  * checkDataDir(), but has been true for a long time because of the
800                  * restriction that the data directory isn't group- or
801                  * world-accessible.)  Also, since we create the lockfiles mode 600,
802                  * we'd have failed above if the lockfile belonged to another userid
803                  * --- which means that whatever process kill() is reporting about
804                  * isn't the one that made the lockfile.  (NOTE: this last
805                  * consideration is the only one that keeps us from blowing away a
806                  * Unix socket file belonging to an instance of Postgres being run by
807                  * someone else, at least on machines where /tmp hasn't got a
808                  * stickybit.)
809                  */
810                 if (other_pid != my_pid && other_pid != my_p_pid &&
811                         other_pid != my_gp_pid)
812                 {
813                         if (kill(other_pid, 0) == 0 ||
814                                 (errno != ESRCH && errno != EPERM))
815                         {
816                                 /* lockfile belongs to a live process */
817                                 ereport(FATAL,
818                                                 (errcode(ERRCODE_LOCK_FILE_EXISTS),
819                                                  errmsg("lock file \"%s\" already exists",
820                                                                 filename),
821                                                  isDDLock ?
822                                                  (encoded_pid < 0 ?
823                                                   errhint("Is another postgres (PID %d) running in data directory \"%s\"?",
824                                                                   (int) other_pid, refName) :
825                                                   errhint("Is another postmaster (PID %d) running in data directory \"%s\"?",
826                                                                   (int) other_pid, refName)) :
827                                                  (encoded_pid < 0 ?
828                                                   errhint("Is another postgres (PID %d) using socket file \"%s\"?",
829                                                                   (int) other_pid, refName) :
830                                                   errhint("Is another postmaster (PID %d) using socket file \"%s\"?",
831                                                                   (int) other_pid, refName))));
832                         }
833                 }
834
835                 /*
836                  * No, the creating process did not exist.      However, it could be that
837                  * the postmaster crashed (or more likely was kill -9'd by a clueless
838                  * admin) but has left orphan backends behind.  Check for this by
839                  * looking to see if there is an associated shmem segment that is
840                  * still in use.
841                  *
842                  * Note: because postmaster.pid is written in two steps, we might not
843                  * find the shmem ID values in it; we can't treat that as an error.
844                  */
845                 if (isDDLock)
846                 {
847                         char       *ptr = buffer;
848                         unsigned long id1,
849                                                 id2;
850                         int lineno;
851
852                         for (lineno = 1; lineno <= 4; lineno++)
853                         {
854                                 if ((ptr = strchr(ptr, '\n')) == NULL)
855                                 {
856                                         elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
857                                         break;
858                                 }
859                                 ptr++;
860                         }
861
862                         if (ptr && sscanf(ptr, "%lu %lu", &id1, &id2) == 2)
863                         {
864                                 if (PGSharedMemoryIsInUse(id1, id2))
865                                         ereport(FATAL,
866                                                         (errcode(ERRCODE_LOCK_FILE_EXISTS),
867                                                          errmsg("pre-existing shared memory block "
868                                                                         "(key %lu, ID %lu) is still in use",
869                                                                         id1, id2),
870                                                          errhint("If you're sure there are no old "
871                                                                 "server processes still running, remove "
872                                                                          "the shared memory block "
873                                                                          "or just delete the file \"%s\".",
874                                                                          filename)));
875                         }
876                 }
877
878                 /*
879                  * Looks like nobody's home.  Unlink the file and try again to create
880                  * it.  Need a loop because of possible race condition against other
881                  * would-be creators.
882                  */
883                 if (unlink(filename) < 0)
884                         ereport(FATAL,
885                                         (errcode_for_file_access(),
886                                          errmsg("could not remove old lock file \"%s\": %m",
887                                                         filename),
888                                          errhint("The file seems accidentally left over, but "
889                                                    "it could not be removed. Please remove the file "
890                                                          "by hand and try again.")));
891         }
892
893         /*
894          * Successfully created the file, now fill it.
895          */
896         snprintf(buffer, sizeof(buffer), "%d\n%s\n%d\n%s\n",
897                          amPostmaster ? (int) my_pid : -((int) my_pid),
898                          DataDir, PostPortNumber, UnixSocketDir);
899         errno = 0;
900         if (write(fd, buffer, strlen(buffer)) != strlen(buffer))
901         {
902                 int                     save_errno = errno;
903
904                 close(fd);
905                 unlink(filename);
906                 /* if write didn't set errno, assume problem is no disk space */
907                 errno = save_errno ? save_errno : ENOSPC;
908                 ereport(FATAL,
909                                 (errcode_for_file_access(),
910                                  errmsg("could not write lock file \"%s\": %m", filename)));
911         }
912         if (pg_fsync(fd) != 0)
913         {
914                 int                     save_errno = errno;
915
916                 close(fd);
917                 unlink(filename);
918                 errno = save_errno;
919                 ereport(FATAL,
920                                 (errcode_for_file_access(),
921                                  errmsg("could not write lock file \"%s\": %m", filename)));
922         }
923         if (close(fd) != 0)
924         {
925                 int                     save_errno = errno;
926
927                 unlink(filename);
928                 errno = save_errno;
929                 ereport(FATAL,
930                                 (errcode_for_file_access(),
931                                  errmsg("could not write lock file \"%s\": %m", filename)));
932         }
933
934         /*
935          * Arrange for automatic removal of lockfile at proc_exit.
936          */
937         on_proc_exit(UnlinkLockFile, PointerGetDatum(strdup(filename)));
938 }
939
940 /*
941  * Create the data directory lockfile.
942  *
943  * When this is called, we must have already switched the working
944  * directory to DataDir, so we can just use a relative path.  This
945  * helps ensure that we are locking the directory we should be.
946  */
947 void
948 CreateDataDirLockFile(bool amPostmaster)
949 {
950         CreateLockFile(DIRECTORY_LOCK_FILE, amPostmaster, true, DataDir);
951 }
952
953 /*
954  * Create a lockfile for the specified Unix socket file.
955  */
956 void
957 CreateSocketLockFile(const char *socketfile, bool amPostmaster)
958 {
959         char            lockfile[MAXPGPATH];
960
961         snprintf(lockfile, sizeof(lockfile), "%s.lock", socketfile);
962         CreateLockFile(lockfile, amPostmaster, false, socketfile);
963         /* Save name of lockfile for TouchSocketLockFile */
964         strcpy(socketLockFile, lockfile);
965 }
966
967 /*
968  * TouchSocketLockFile -- mark socket lock file as recently accessed
969  *
970  * This routine should be called every so often to ensure that the lock file
971  * has a recent mod or access date.  That saves it
972  * from being removed by overenthusiastic /tmp-directory-cleaner daemons.
973  * (Another reason we should never have put the socket file in /tmp...)
974  */
975 void
976 TouchSocketLockFile(void)
977 {
978         /* Do nothing if we did not create a socket... */
979         if (socketLockFile[0] != '\0')
980         {
981                 /*
982                  * utime() is POSIX standard, utimes() is a common alternative; if we
983                  * have neither, fall back to actually reading the file (which only
984                  * sets the access time not mod time, but that should be enough in
985                  * most cases).  In all paths, we ignore errors.
986                  */
987 #ifdef HAVE_UTIME
988                 utime(socketLockFile, NULL);
989 #else                                                   /* !HAVE_UTIME */
990 #ifdef HAVE_UTIMES
991                 utimes(socketLockFile, NULL);
992 #else                                                   /* !HAVE_UTIMES */
993                 int                     fd;
994                 char            buffer[1];
995
996                 fd = open(socketLockFile, O_RDONLY | PG_BINARY, 0);
997                 if (fd >= 0)
998                 {
999                         read(fd, buffer, sizeof(buffer));
1000                         close(fd);
1001                 }
1002 #endif   /* HAVE_UTIMES */
1003 #endif   /* HAVE_UTIME */
1004         }
1005 }
1006
1007 /*
1008  * Append information about a shared memory segment to the data directory
1009  * lock file.
1010  *
1011  * This may be called multiple times in the life of a postmaster, if we
1012  * delete and recreate shmem due to backend crash.      Therefore, be prepared
1013  * to overwrite existing information.  (As of 7.1, a postmaster only creates
1014  * one shm seg at a time; but for the purposes here, if we did have more than
1015  * one then any one of them would do anyway.)
1016  */
1017 void
1018 RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
1019 {
1020         int                     fd;
1021         int                     len;
1022         int                     lineno;
1023         char       *ptr;
1024         char            buffer[MAXPGPATH * 2 + 256];
1025
1026         fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
1027         if (fd < 0)
1028         {
1029                 ereport(LOG,
1030                                 (errcode_for_file_access(),
1031                                  errmsg("could not open file \"%s\": %m",
1032                                                 DIRECTORY_LOCK_FILE)));
1033                 return;
1034         }
1035         len = read(fd, buffer, sizeof(buffer) - 100);
1036         if (len < 0)
1037         {
1038                 ereport(LOG,
1039                                 (errcode_for_file_access(),
1040                                  errmsg("could not read from file \"%s\": %m",
1041                                                 DIRECTORY_LOCK_FILE)));
1042                 close(fd);
1043                 return;
1044         }
1045         buffer[len] = '\0';
1046
1047         /*
1048          * Skip over first four lines (PID, pgdata, portnum, socketdir).
1049          */
1050         ptr = buffer;
1051         for (lineno = 1; lineno <= 4; lineno++)
1052         {
1053                 if ((ptr = strchr(ptr, '\n')) == NULL)
1054                 {
1055                         elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
1056                         close(fd);
1057                         return;
1058                 }
1059                 ptr++;
1060         }
1061         
1062         /*
1063          * Append key information.      Format to try to keep it the same length
1064          * always (trailing junk won't hurt, but might confuse humans).
1065          */
1066         sprintf(ptr, "%9lu %9lu\n", id1, id2);
1067
1068         /*
1069          * And rewrite the data.  Since we write in a single kernel call, this
1070          * update should appear atomic to onlookers.
1071          */
1072         len = strlen(buffer);
1073         errno = 0;
1074         if (lseek(fd, (off_t) 0, SEEK_SET) != 0 ||
1075                 (int) write(fd, buffer, len) != len)
1076         {
1077                 /* if write didn't set errno, assume problem is no disk space */
1078                 if (errno == 0)
1079                         errno = ENOSPC;
1080                 ereport(LOG,
1081                                 (errcode_for_file_access(),
1082                                  errmsg("could not write to file \"%s\": %m",
1083                                                 DIRECTORY_LOCK_FILE)));
1084                 close(fd);
1085                 return;
1086         }
1087         if (pg_fsync(fd) != 0)
1088         {
1089                 ereport(LOG,
1090                                 (errcode_for_file_access(),
1091                                  errmsg("could not write to file \"%s\": %m",
1092                                                 DIRECTORY_LOCK_FILE)));
1093         }
1094         if (close(fd) != 0)
1095         {
1096                 ereport(LOG,
1097                                 (errcode_for_file_access(),
1098                                  errmsg("could not write to file \"%s\": %m",
1099                                                 DIRECTORY_LOCK_FILE)));
1100         }
1101 }
1102
1103
1104 /*-------------------------------------------------------------------------
1105  *                              Version checking support
1106  *-------------------------------------------------------------------------
1107  */
1108
1109 /*
1110  * Determine whether the PG_VERSION file in directory `path' indicates
1111  * a data version compatible with the version of this program.
1112  *
1113  * If compatible, return. Otherwise, ereport(FATAL).
1114  */
1115 void
1116 ValidatePgVersion(const char *path)
1117 {
1118         char            full_path[MAXPGPATH];
1119         FILE       *file;
1120         int                     ret;
1121         long            file_major,
1122                                 file_minor;
1123         long            my_major = 0,
1124                                 my_minor = 0;
1125         char       *endptr;
1126         const char *version_string = PG_VERSION;
1127
1128         my_major = strtol(version_string, &endptr, 10);
1129         if (*endptr == '.')
1130                 my_minor = strtol(endptr + 1, NULL, 10);
1131
1132         snprintf(full_path, sizeof(full_path), "%s/PG_VERSION", path);
1133
1134         file = AllocateFile(full_path, "r");
1135         if (!file)
1136         {
1137                 if (errno == ENOENT)
1138                         ereport(FATAL,
1139                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1140                                          errmsg("\"%s\" is not a valid data directory",
1141                                                         path),
1142                                          errdetail("File \"%s\" is missing.", full_path)));
1143                 else
1144                         ereport(FATAL,
1145                                         (errcode_for_file_access(),
1146                                          errmsg("could not open file \"%s\": %m", full_path)));
1147         }
1148
1149         ret = fscanf(file, "%ld.%ld", &file_major, &file_minor);
1150         if (ret != 2)
1151                 ereport(FATAL,
1152                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1153                                  errmsg("\"%s\" is not a valid data directory",
1154                                                 path),
1155                                  errdetail("File \"%s\" does not contain valid data.",
1156                                                    full_path),
1157                                  errhint("You might need to initdb.")));
1158
1159         FreeFile(file);
1160
1161         if (my_major != file_major || my_minor != file_minor)
1162                 ereport(FATAL,
1163                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1164                                  errmsg("database files are incompatible with server"),
1165                                  errdetail("The data directory was initialized by PostgreSQL version %ld.%ld, "
1166                                                    "which is not compatible with this version %s.",
1167                                                    file_major, file_minor, version_string)));
1168 }
1169
1170 /*-------------------------------------------------------------------------
1171  *                              Library preload support
1172  *-------------------------------------------------------------------------
1173  */
1174
1175 /*
1176  * GUC variables: lists of library names to be preloaded at postmaster
1177  * start and at backend start
1178  */
1179 char       *shared_preload_libraries_string = NULL;
1180 char       *local_preload_libraries_string = NULL;
1181
1182 /* Flag telling that we are loading shared_preload_libraries */
1183 bool            process_shared_preload_libraries_in_progress = false;
1184
1185 /*
1186  * load the shared libraries listed in 'libraries'
1187  *
1188  * 'gucname': name of GUC variable, for error reports
1189  * 'restricted': if true, force libraries to be in $libdir/plugins/
1190  */
1191 static void
1192 load_libraries(const char *libraries, const char *gucname, bool restricted)
1193 {
1194         char       *rawstring;
1195         List       *elemlist;
1196         int                     elevel;
1197         ListCell   *l;
1198
1199         if (libraries == NULL || libraries[0] == '\0')
1200                 return;                                 /* nothing to do */
1201
1202         /* Need a modifiable copy of string */
1203         rawstring = pstrdup(libraries);
1204
1205         /* Parse string into list of identifiers */
1206         if (!SplitIdentifierString(rawstring, ',', &elemlist))
1207         {
1208                 /* syntax error in list */
1209                 pfree(rawstring);
1210                 list_free(elemlist);
1211                 ereport(LOG,
1212                                 (errcode(ERRCODE_SYNTAX_ERROR),
1213                                  errmsg("invalid list syntax in parameter \"%s\"",
1214                                                 gucname)));
1215                 return;
1216         }
1217
1218         /*
1219          * Choose notice level: avoid repeat messages when re-loading a library
1220          * that was preloaded into the postmaster.      (Only possible in EXEC_BACKEND
1221          * configurations)
1222          */
1223 #ifdef EXEC_BACKEND
1224         if (IsUnderPostmaster && process_shared_preload_libraries_in_progress)
1225                 elevel = DEBUG2;
1226         else
1227 #endif
1228                 elevel = LOG;
1229
1230         foreach(l, elemlist)
1231         {
1232                 char       *tok = (char *) lfirst(l);
1233                 char       *filename;
1234
1235                 filename = pstrdup(tok);
1236                 canonicalize_path(filename);
1237                 /* If restricting, insert $libdir/plugins if not mentioned already */
1238                 if (restricted && first_dir_separator(filename) == NULL)
1239                 {
1240                         char       *expanded;
1241
1242                         expanded = palloc(strlen("$libdir/plugins/") + strlen(filename) + 1);
1243                         strcpy(expanded, "$libdir/plugins/");
1244                         strcat(expanded, filename);
1245                         pfree(filename);
1246                         filename = expanded;
1247                 }
1248                 load_file(filename, restricted);
1249                 ereport(elevel,
1250                                 (errmsg("loaded library \"%s\"", filename)));
1251                 pfree(filename);
1252         }
1253
1254         pfree(rawstring);
1255         list_free(elemlist);
1256 }
1257
1258 /*
1259  * process any libraries that should be preloaded at postmaster start
1260  */
1261 void
1262 process_shared_preload_libraries(void)
1263 {
1264         process_shared_preload_libraries_in_progress = true;
1265         load_libraries(shared_preload_libraries_string,
1266                                    "shared_preload_libraries",
1267                                    false);
1268         process_shared_preload_libraries_in_progress = false;
1269 }
1270
1271 /*
1272  * process any libraries that should be preloaded at backend start
1273  */
1274 void
1275 process_local_preload_libraries(void)
1276 {
1277         load_libraries(local_preload_libraries_string,
1278                                    "local_preload_libraries",
1279                                    true);
1280 }
1281
1282 void
1283 pg_bindtextdomain(const char *domain)
1284 {
1285 #ifdef ENABLE_NLS
1286         if (my_exec_path[0] != '\0')
1287         {
1288                 char            locale_path[MAXPGPATH];
1289
1290                 get_locale_path(my_exec_path, locale_path);
1291                 bindtextdomain(domain, locale_path);
1292                 pg_bind_textdomain_codeset(domain);
1293         }
1294 #endif
1295 }