11/*-------------------------------------------------------------------------
22 *
33 * main.c
4- * Stub main() routine for the postgres backend.
4+ * Stub main() routine for the postgres executable.
5+ *
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.
9+ *
510 *
611 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
712 * Portions Copyright (c) 1994, Regents of the University of California
813 *
914 *
1015 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.34 2000/11/16 05:51:00 momjian Exp $
16+ * $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.35 2000/11/25 03:45:47 tgl Exp $
1217 *
1318 *-------------------------------------------------------------------------
1419 */
1520#include "postgres.h"
1621
1722#include <pwd.h>
1823#include <unistd.h>
24+ #ifdef USE_LOCALE
25+ #include <locale.h>
26+ #endif
1927
2028#if defined(__alpha ) && !defined(linux ) && !defined(__FreeBSD__ )
2129#include <sys/sysinfo.h>
2533#undef ASSEMBLER
2634#endif
2735
28- #ifdef USE_LOCALE
29- #include <locale.h>
30- #endif
3136#include "miscadmin.h"
3237#include "bootstrap/bootstrap.h"
3338#include "tcop/tcopprot.h"
3439
40+
3541#define NOROOTEXEC "\
36- \n\"root\" execution of the PostgreSQL backend is not permitted.\n\n\
37- The backend must be started under its own userid to prevent\n\
42+ \n\"root\" execution of the PostgreSQL server is not permitted.\n\n\
43+ The server must be started under an unprivileged userid to prevent\n\
3844a possible system security compromise. See the INSTALL file for\n\
39- more information on how to properly start the postmaster.\n\n"
45+ more information on how to properly start the server.\n\n"
46+
4047
4148int
4249main (int argc , char * argv [])
4350{
4451 int len ;
52+ struct passwd * pw ;
53+
54+ /*
55+ * Place platform-specific startup hacks here. This is the right
56+ * place to put code that must be executed early in launch of either
57+ * a postmaster, a standalone backend, or a standalone bootstrap run.
58+ * Note that this code will NOT be executed when a backend or
59+ * sub-bootstrap run is forked by the postmaster.
60+ *
61+ * XXX The need for code here is proof that the platform in question
62+ * is too brain-dead to provide a standard C execution environment
63+ * without help. Avoid adding more here, if you can.
64+ */
4565
4666#if defined(__alpha )
4767#ifdef NOFIXADE
@@ -52,78 +72,89 @@ main(int argc, char *argv[])
5272 int buffer [] = {SSIN_UACPROC , UAC_NOPRINT };
5373
5474#endif /* NOPRINTADE */
55- #endif
75+ #endif /* __alpha */
5676
57- #ifdef USE_LOCALE
58- setlocale (LC_CTYPE , "" ); /* take locale information from an
59- * environment */
60- setlocale (LC_COLLATE , "" );
61- setlocale (LC_MONETARY , "" );
62- #endif
6377#if defined(NOFIXADE ) || defined(NOPRINTADE )
6478
65- /*
66- * Must be first so that the bootstrap code calls it, too. (Only
67- * needed on some RISC architectures.)
68- */
69-
7079#if defined(ultrix4 )
7180 syscall (SYS_sysmips , MIPS_FIXADE , 0 , NULL , NULL , NULL );
7281#endif
7382
7483#if defined(__alpha )
7584 if (setsysinfo (SSI_NVPAIRS , buffer , 1 , (caddr_t ) NULL ,
7685 (unsigned long ) NULL ) < 0 )
77- elog ( NOTICE , "setsysinfo failed: %d\n" , errno );
86+ fprintf ( stderr , "setsysinfo failed: %d\n" , errno );
7887#endif
7988
8089#endif /* NOFIXADE || NOPRINTADE */
8190
91+ #ifdef __BEOS__
92+ /* BeOS-specific actions on startup */
93+ beos_startup (argc ,argv );
94+ #endif
95+
8296 /*
83- * use one executable for both postgres and postmaster, invoke one or
84- * the other depending on the name of the executable
97+ * Not-quite-so-platform-specific startup environment checks.
98+ * Still best to minimize these.
8599 */
86- len = strlen (argv [0 ]);
87100
88- /* OK this is going to seem weird, but BeOS is presently basically
89- * a single user system. There is work going on, but at present it'll
90- * say that every user is uid 0, i.e. root. We'll inhibit this check
91- * until Be get the system working with multiple users!!
92- */
101+ /*
102+ * Make sure we are not running as root.
103+ *
104+ * BeOS currently runs everything as root :-(, so this check must
105+ * be temporarily disabled there...
106+ */
93107#ifndef __BEOS__
94- if (! geteuid ())
108+ if (geteuid () == 0 )
95109 {
96110 fprintf (stderr , "%s" , NOROOTEXEC );
97111 exit (1 );
98112 }
99113#endif /* __BEOS__ */
100114
101- #ifdef __BEOS__
102- /* Specific beos actions on startup */
103- beos_startup (argc ,argv );
115+ /*
116+ * Set up locale information from environment, in only the categories
117+ * needed by Postgres; leave other categories set to default "C".
118+ * (Note that CTYPE and COLLATE will be overridden later from pg_control
119+ * if we are in an already-initialized database. We set them here so
120+ * that they will be available to fill pg_control during initdb.)
121+ */
122+ #ifdef USE_LOCALE
123+ setlocale (LC_CTYPE , "" );
124+ setlocale (LC_COLLATE , "" );
125+ setlocale (LC_MONETARY , "" );
104126#endif
105127
128+ /*
129+ * Now dispatch to one of PostmasterMain, PostgresMain, or BootstrapMain
130+ * depending on the program name (and possibly first argument) we
131+ * were called with. The lack of consistency here is historical.
132+ */
133+ len = strlen (argv [0 ]);
106134
107- if (len >= 10 && !strcmp (argv [0 ] + len - 10 , "postmaster" ))
135+ if (len >= 10 && strcmp (argv [0 ] + len - 10 , "postmaster" ) == 0 )
136+ {
137+ /* Called as "postmaster" */
108138 exit (PostmasterMain (argc , argv ));
139+ }
109140
110141 /*
111- * if the first argument is "-boot", then invoke the backend in
112- * bootstrap mode
142+ * If the first argument is "-boot", then invoke bootstrap mode.
143+ * Note we remove "-boot" from the arguments passed on to BootstrapMain.
113144 */
114145 if (argc > 1 && strcmp (argv [1 ], "-boot" ) == 0 )
115- exit (BootstrapMain (argc - 1 , argv + 1 )); /* remove the -boot arg
116- * from the command line */
117- else
146+ exit (BootstrapMain (argc - 1 , argv + 1 ));
147+
148+ /*
149+ * Otherwise we're a standalone backend. Invoke PostgresMain,
150+ * specifying current userid as the "authenticated" Postgres user name.
151+ */
152+ pw = getpwuid (geteuid ());
153+ if (pw == NULL )
118154 {
119- struct passwd * pw ;
120-
121- pw = getpwuid (geteuid ());
122- if (!pw )
123- {
124- fprintf (stderr , "%s: invalid current euid" , argv [0 ]);
125- exit (1 );
126- }
127- exit (PostgresMain (argc , argv , argc , argv , pw -> pw_name ));
155+ fprintf (stderr , "%s: invalid current euid" , argv [0 ]);
156+ exit (1 );
128157 }
158+
159+ exit (PostgresMain (argc , argv , argc , argv , pw -> pw_name ));
129160}
0 commit comments