diff options
author | Andres Freund <andres@anarazel.de> | 2015-01-13 13:12:37 +0100 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2015-01-14 00:33:14 +0100 |
commit | 31c453165b5a656044ce1dbce89f5828c1c7e23c (patch) | |
tree | ccf0a682b390361d581a7116d6244d7feb80cd45 /src/backend/utils/init/miscinit.c | |
parent | 2be82dcf17a18511df5153bcafe67a9c1387be1e (diff) | |
download | postgresql-31c453165b5a656044ce1dbce89f5828c1c7e23c.tar.gz postgresql-31c453165b5a656044ce1dbce89f5828c1c7e23c.zip |
Commonalize process startup code.
Move common code, that was duplicated in every postmaster child/every
standalone process, into two functions in miscinit.c. Not only does
that already result in a fair amount of net code reduction but it also
makes it much easier to remove more duplication in the future. The
prime motivation wasn't code deduplication though, but easier addition
of new common code.
Diffstat (limited to 'src/backend/utils/init/miscinit.c')
-rw-r--r-- | src/backend/utils/init/miscinit.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 7f386aee805..414b05e3b8c 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -16,9 +16,11 @@ #include <sys/param.h> #include <signal.h> +#include <time.h> #include <sys/file.h> #include <sys/stat.h> #include <sys/time.h> +#include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <grp.h> @@ -160,6 +162,70 @@ static int SecurityRestrictionContext = 0; /* We also remember if a SET ROLE is currently active */ static bool SetRoleIsActive = false; +/* + * Initialize the basic environment for a postmaster child + * + * Should be called as early as possible after the child's startup. + */ +void +InitPostmasterChild(void) +{ + IsUnderPostmaster = true; /* we are a postmaster subprocess now */ + + MyProcPid = getpid(); /* reset MyProcPid */ + + MyStartTime = time(NULL); /* set our start time in case we call elog */ + + /* + * make sure stderr is in binary mode before anything can possibly be + * written to it, in case it's actually the syslogger pipe, so the pipe + * chunking protocol isn't disturbed. Non-logpipe data gets translated on + * redirection (e.g. via pg_ctl -l) anyway. + */ +#ifdef WIN32 + _setmode(fileno(stderr), _O_BINARY); +#endif + + /* We don't want the postmaster's proc_exit() handlers */ + on_exit_reset(); + + /* + * If possible, make this process a group leader, so that the postmaster + * can signal any child processes too. Not all processes will have + * children, but for consistency we , but for consistency we make all + * postmaster child processes do this. + */ +#ifdef HAVE_SETSID + if (setsid() < 0) + elog(FATAL, "setsid() failed: %m"); +#endif +} + +/* + * Initialize the basic environment for a standalone process. + * + * argv0 has to be suitable to find the program's executable. + */ +void +InitStandaloneProcess(const char *argv0) +{ + Assert(!IsPostmasterEnvironment); + + MyProcPid = getpid(); /* reset MyProcPid */ + + MyStartTime = time(NULL); /* set our start time in case we call elog */ + + /* Compute paths, no postmaster to inherit from */ + if (my_exec_path[0] == '\0') + { + if (find_my_exec(argv0, my_exec_path) < 0) + elog(FATAL, "%s: could not locate my own executable path", + argv0); + } + + if (pkglib_path[0] == '\0') + get_pkglib_path(my_exec_path, pkglib_path); +} /* * GetUserId - get the current effective user ID. |