Query in SQL function still not schema-safe; add a couple
[PostgreSQL.git] / src / backend / main / main.c
blobd8ef569f7d633e988bf0ae43b6ea755de7b3bbc3
1 /*-------------------------------------------------------------------------
3 * main.c
4 * Stub main() routine for the postgres executable.
6 * This does some essential startup tasks for any incarnation of postgres
7 * (postmaster, standalone backend, or standalone bootstrap mode) and then
8 * dispatches to the proper FooMain() routine for the incarnation.
11 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
15 * IDENTIFICATION
16 * $PostgreSQL$
18 *-------------------------------------------------------------------------
20 #include "postgres.h"
22 #include <pwd.h>
23 #include <unistd.h>
25 #if defined(__alpha) && defined(__osf__) /* no __alpha__ ? */
26 #include <sys/sysinfo.h>
27 #include "machine/hal_sysinfo.h"
28 #define ASSEMBLER
29 #include <sys/proc.h>
30 #undef ASSEMBLER
31 #endif
33 #if defined(__NetBSD__)
34 #include <sys/param.h>
35 #endif
37 #include "bootstrap/bootstrap.h"
38 #include "postmaster/postmaster.h"
39 #include "tcop/tcopprot.h"
40 #include "utils/help_config.h"
41 #include "utils/pg_locale.h"
42 #include "utils/ps_status.h"
43 #ifdef WIN32
44 #include "libpq/pqsignal.h"
45 #endif
48 const char *progname;
51 static void startup_hacks(const char *progname);
52 static void help(const char *progname);
53 static void check_root(const char *progname);
54 static char *get_current_username(const char *progname);
58 int
59 main(int argc, char *argv[])
61 progname = get_progname(argv[0]);
64 * Platform-specific startup hacks
66 startup_hacks(progname);
69 * Remember the physical location of the initially given argv[] array for
70 * possible use by ps display. On some platforms, the argv[] storage must
71 * be overwritten in order to set the process title for ps. In such cases
72 * save_ps_display_args makes and returns a new copy of the argv[] array.
74 * save_ps_display_args may also move the environment strings to make
75 * extra room. Therefore this should be done as early as possible during
76 * startup, to avoid entanglements with code that might save a getenv()
77 * result pointer.
79 argv = save_ps_display_args(argc, argv);
82 * Set up locale information from environment. Note that LC_CTYPE and
83 * LC_COLLATE will be overridden later from pg_control if we are in an
84 * already-initialized database. We set them here so that they will be
85 * available to fill pg_control during initdb. LC_MESSAGES will get set
86 * later during GUC option processing, but we set it here to allow startup
87 * error messages to be localized.
90 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("postgres"));
92 #ifdef WIN32
95 * Windows uses codepages rather than the environment, so we work around
96 * that by querying the environment explicitly first for LC_COLLATE and
97 * LC_CTYPE. We have to do this because initdb passes those values in the
98 * environment. If there is nothing there we fall back on the codepage.
101 char *env_locale;
103 if ((env_locale = getenv("LC_COLLATE")) != NULL)
104 pg_perm_setlocale(LC_COLLATE, env_locale);
105 else
106 pg_perm_setlocale(LC_COLLATE, "");
108 if ((env_locale = getenv("LC_CTYPE")) != NULL)
109 pg_perm_setlocale(LC_CTYPE, env_locale);
110 else
111 pg_perm_setlocale(LC_CTYPE, "");
113 #else
114 pg_perm_setlocale(LC_COLLATE, "");
115 pg_perm_setlocale(LC_CTYPE, "");
116 #endif
118 #ifdef LC_MESSAGES
119 pg_perm_setlocale(LC_MESSAGES, "");
120 #endif
123 * We keep these set to "C" always, except transiently in pg_locale.c; see
124 * that file for explanations.
126 pg_perm_setlocale(LC_MONETARY, "C");
127 pg_perm_setlocale(LC_NUMERIC, "C");
128 pg_perm_setlocale(LC_TIME, "C");
131 * Now that we have absorbed as much as we wish to from the locale
132 * environment, remove any LC_ALL setting, so that the environment
133 * variables installed by pg_perm_setlocale have force.
135 unsetenv("LC_ALL");
138 * Catch standard options before doing much else
140 if (argc > 1)
142 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
144 help(progname);
145 exit(0);
147 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
149 puts("postgres (PostgreSQL) " PG_VERSION);
150 exit(0);
155 * Make sure we are not running as root.
157 check_root(progname);
160 * Dispatch to one of various subprograms depending on first argument.
163 #ifdef EXEC_BACKEND
164 if (argc > 1 && strncmp(argv[1], "--fork", 6) == 0)
165 exit(SubPostmasterMain(argc, argv));
166 #endif
168 #ifdef WIN32
171 * Start our win32 signal implementation
173 * SubPostmasterMain() will do this for itself, but the remaining modes
174 * need it here
176 pgwin32_signal_initialize();
177 #endif
179 if (argc > 1 && strcmp(argv[1], "--boot") == 0)
180 AuxiliaryProcessMain(argc, argv); /* does not return */
182 if (argc > 1 && strcmp(argv[1], "--describe-config") == 0)
183 exit(GucInfoMain());
185 if (argc > 1 && strcmp(argv[1], "--single") == 0)
186 exit(PostgresMain(argc, argv, get_current_username(progname)));
188 exit(PostmasterMain(argc, argv));
194 * Place platform-specific startup hacks here. This is the right
195 * place to put code that must be executed early in launch of either a
196 * postmaster, a standalone backend, or a standalone bootstrap run.
197 * Note that this code will NOT be executed when a backend or
198 * sub-bootstrap run is forked by the server.
200 * XXX The need for code here is proof that the platform in question
201 * is too brain-dead to provide a standard C execution environment
202 * without help. Avoid adding more here, if you can.
204 static void
205 startup_hacks(const char *progname)
207 #if defined(__alpha) /* no __alpha__ ? */
208 #ifdef NOFIXADE
209 int buffer[] = {SSIN_UACPROC, UAC_SIGBUS | UAC_NOPRINT};
210 #endif
211 #endif /* __alpha */
215 * On some platforms, unaligned memory accesses result in a kernel trap;
216 * the default kernel behavior is to emulate the memory access, but this
217 * results in a significant performance penalty. We ought to fix PG not to
218 * make such unaligned memory accesses, so this code disables the kernel
219 * emulation: unaligned accesses will result in SIGBUS instead.
221 #ifdef NOFIXADE
223 #if defined(ultrix4)
224 syscall(SYS_sysmips, MIPS_FIXADE, 0, NULL, NULL, NULL);
225 #endif
227 #if defined(__alpha) /* no __alpha__ ? */
228 if (setsysinfo(SSI_NVPAIRS, buffer, 1, (caddr_t) NULL,
229 (unsigned long) NULL) < 0)
230 write_stderr("%s: setsysinfo failed: %s\n",
231 progname, strerror(errno));
232 #endif
233 #endif /* NOFIXADE */
236 #ifdef WIN32
238 WSADATA wsaData;
239 int err;
241 /* Make output streams unbuffered by default */
242 setvbuf(stdout, NULL, _IONBF, 0);
243 setvbuf(stderr, NULL, _IONBF, 0);
245 /* Prepare Winsock */
246 err = WSAStartup(MAKEWORD(2, 2), &wsaData);
247 if (err != 0)
249 write_stderr("%s: WSAStartup failed: %d\n",
250 progname, err);
251 exit(1);
254 /* In case of general protection fault, don't show GUI popup box */
255 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
257 #endif /* WIN32 */
262 * Help display should match the options accepted by PostmasterMain()
263 * and PostgresMain().
265 static void
266 help(const char *progname)
268 printf(_("%s is the PostgreSQL server.\n\n"), progname);
269 printf(_("Usage:\n %s [OPTION]...\n\n"), progname);
270 printf(_("Options:\n"));
271 #ifdef USE_ASSERT_CHECKING
272 printf(_(" -A 1|0 enable/disable run-time assert checking\n"));
273 #endif
274 printf(_(" -B NBUFFERS number of shared buffers\n"));
275 printf(_(" -c NAME=VALUE set run-time parameter\n"));
276 printf(_(" -d 1-5 debugging level\n"));
277 printf(_(" -D DATADIR database directory\n"));
278 printf(_(" -e use European date input format (DMY)\n"));
279 printf(_(" -F turn fsync off\n"));
280 printf(_(" -h HOSTNAME host name or IP address to listen on\n"));
281 printf(_(" -i enable TCP/IP connections\n"));
282 printf(_(" -k DIRECTORY Unix-domain socket location\n"));
283 #ifdef USE_SSL
284 printf(_(" -l enable SSL connections\n"));
285 #endif
286 printf(_(" -N MAX-CONNECT maximum number of allowed connections\n"));
287 printf(_(" -o OPTIONS pass \"OPTIONS\" to each server process (obsolete)\n"));
288 printf(_(" -p PORT port number to listen on\n"));
289 printf(_(" -s show statistics after each query\n"));
290 printf(_(" -S WORK-MEM set amount of memory for sorts (in kB)\n"));
291 printf(_(" --NAME=VALUE set run-time parameter\n"));
292 printf(_(" --describe-config describe configuration parameters, then exit\n"));
293 printf(_(" --help show this help, then exit\n"));
294 printf(_(" --version output version information, then exit\n"));
296 printf(_("\nDeveloper options:\n"));
297 printf(_(" -f s|i|n|m|h forbid use of some plan types\n"));
298 printf(_(" -n do not reinitialize shared memory after abnormal exit\n"));
299 printf(_(" -O allow system table structure changes\n"));
300 printf(_(" -P disable system indexes\n"));
301 printf(_(" -t pa|pl|ex show timings after each query\n"));
302 printf(_(" -T send SIGSTOP to all backend servers if one dies\n"));
303 printf(_(" -W NUM wait NUM seconds to allow attach from a debugger\n"));
305 printf(_("\nOptions for single-user mode:\n"));
306 printf(_(" --single selects single-user mode (must be first argument)\n"));
307 printf(_(" DBNAME database name (defaults to user name)\n"));
308 printf(_(" -d 0-5 override debugging level\n"));
309 printf(_(" -E echo statement before execution\n"));
310 printf(_(" -j do not use newline as interactive query delimiter\n"));
311 printf(_(" -r FILENAME send stdout and stderr to given file\n"));
313 printf(_("\nOptions for bootstrapping mode:\n"));
314 printf(_(" --boot selects bootstrapping mode (must be first argument)\n"));
315 printf(_(" DBNAME database name (mandatory argument in bootstrapping mode)\n"));
316 printf(_(" -r FILENAME send stdout and stderr to given file\n"));
317 printf(_(" -x NUM internal use\n"));
319 printf(_("\nPlease read the documentation for the complete list of run-time\n"
320 "configuration settings and how to set them on the command line or in\n"
321 "the configuration file.\n\n"
322 "Report bugs to <pgsql-bugs@postgresql.org>.\n"));
327 static void
328 check_root(const char *progname)
330 #ifndef WIN32
331 if (geteuid() == 0)
333 write_stderr("\"root\" execution of the PostgreSQL server is not permitted.\n"
334 "The server must be started under an unprivileged user ID to prevent\n"
335 "possible system security compromise. See the documentation for\n"
336 "more information on how to properly start the server.\n");
337 exit(1);
341 * Also make sure that real and effective uids are the same. Executing as
342 * a setuid program from a root shell is a security hole, since on many
343 * platforms a nefarious subroutine could setuid back to root if real uid
344 * is root. (Since nobody actually uses postgres as a setuid program,
345 * trying to actively fix this situation seems more trouble than it's
346 * worth; we'll just expend the effort to check for it.)
348 if (getuid() != geteuid())
350 write_stderr("%s: real and effective user IDs must match\n",
351 progname);
352 exit(1);
354 #else /* WIN32 */
355 if (pgwin32_is_admin())
357 write_stderr("Execution of PostgreSQL by a user with administrative permissions is not\n"
358 "permitted.\n"
359 "The server must be started under an unprivileged user ID to prevent\n"
360 "possible system security compromises. See the documentation for\n"
361 "more information on how to properly start the server.\n");
362 exit(1);
364 #endif /* WIN32 */
369 static char *
370 get_current_username(const char *progname)
372 #ifndef WIN32
373 struct passwd *pw;
375 pw = getpwuid(geteuid());
376 if (pw == NULL)
378 write_stderr("%s: invalid effective UID: %d\n",
379 progname, (int) geteuid());
380 exit(1);
382 /* Allocate new memory because later getpwuid() calls can overwrite it. */
383 return strdup(pw->pw_name);
384 #else
385 long namesize = 256 /* UNLEN */ + 1;
386 char *name;
388 name = malloc(namesize);
389 if (!GetUserName(name, &namesize))
391 write_stderr("%s: could not determine user name (GetUserName failed)\n",
392 progname);
393 exit(1);
396 return name;
397 #endif