⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pg_ctl.c

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 C
📖 第 1 页 / 共 3 页
字号:
				if (postgres_path != NULL)					postgres_path = optline;			}			else				post_opts = optline;		}	}	/* No -D or -D already added during server start */	if (ctl_command == RESTART_COMMAND || pgdata_opt == NULL)		pgdata_opt = "";	if (postgres_path == NULL)	{		char	   *postmaster_path;		int			ret;		postmaster_path = pg_malloc(MAXPGPATH);		if ((ret = find_other_exec(argv0, "postmaster", PM_VERSIONSTR,								   postmaster_path)) < 0)		{			char		full_path[MAXPGPATH];			if (find_my_exec(argv0, full_path) < 0)				StrNCpy(full_path, progname, MAXPGPATH);			if (ret == -1)				write_stderr(_("The program \"postmaster\" is needed by %s "							   "but was not found in the\n"							   "same directory as \"%s\".\n"							   "Check your installation.\n"),							 progname, full_path);			else				write_stderr(_("The program \"postmaster\" was found by \"%s\"\n"							   "but was not the same version as %s.\n"							   "Check your installation.\n"),							 full_path, progname);			exit(1);		}		postgres_path = postmaster_path;	}	exitcode = start_postmaster();	if (exitcode != 0)	{		write_stderr(_("%s: could not start postmaster: exit code was %d\n"),					 progname, exitcode);		exit(1);	}	if (old_pid != 0)	{		pg_usleep(1000000);		pid = get_pgpid();		if (pid == old_pid)		{			write_stderr(_("%s: could not start postmaster\n"						   "Examine the log output.\n"),						 progname);			exit(1);		}	}	if (do_wait)	{		print_msg(_("waiting for postmaster to start..."));		if (test_postmaster_connection() == false)		{			printf(_("could not start postmaster\n"));			exit(1);		}		else		{			print_msg(_(" done\n"));			print_msg(_("postmaster started\n"));		}	}	else		print_msg(_("postmaster starting\n"));}static voiddo_stop(void){	int			cnt;	pgpid_t		pid;	pid = get_pgpid();	if (pid == 0)				/* no pid file */	{		write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);		write_stderr(_("Is postmaster running?\n"));		exit(1);	}	else if (pid < 0)			/* standalone backend, not postmaster */	{		pid = -pid;		write_stderr(_("%s: cannot stop postmaster; "					   "postgres is running (PID: %ld)\n"),					 progname, pid);		exit(1);	}	if (kill((pid_t) pid, sig) != 0)	{		write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,					 strerror(errno));		exit(1);	}	if (!do_wait)	{		print_msg(_("postmaster shutting down\n"));		return;	}	else	{		print_msg(_("waiting for postmaster to shut down..."));		for (cnt = 0; cnt < wait_seconds; cnt++)		{			if ((pid = get_pgpid()) != 0)			{				print_msg(".");				pg_usleep(1000000);		/* 1 sec */			}			else				break;		}		if (pid != 0)			/* pid file still exists */		{			print_msg(_(" failed\n"));			write_stderr(_("%s: postmaster does not shut down\n"), progname);			exit(1);		}		print_msg(_(" done\n"));		printf(_("postmaster stopped\n"));	}}/* *	restart/reload routines */static voiddo_restart(void){	int			cnt;	pgpid_t		pid;	pid = get_pgpid();	if (pid == 0)				/* no pid file */	{		write_stderr(_("%s: PID file \"%s\" does not exist\n"),					 progname, pid_file);		write_stderr(_("Is postmaster running?\n"));		write_stderr(_("starting postmaster anyway\n"));		do_start();		return;	}	else if (pid < 0)			/* standalone backend, not postmaster */	{		pid = -pid;		if (postmaster_is_alive((pid_t) pid))		{			write_stderr(_("%s: cannot restart postmaster; "						   "postgres is running (PID: %ld)\n"),						 progname, pid);			write_stderr(_("Please terminate postgres and try again.\n"));			exit(1);		}	}	if (postmaster_is_alive((pid_t) pid))	{		if (kill((pid_t) pid, sig) != 0)		{			write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,						 strerror(errno));			exit(1);		}		print_msg(_("waiting for postmaster to shut down..."));		/* always wait for restart */		for (cnt = 0; cnt < wait_seconds; cnt++)		{			if ((pid = get_pgpid()) != 0)			{				print_msg(".");				pg_usleep(1000000);		/* 1 sec */			}			else				break;		}		if (pid != 0)			/* pid file still exists */		{			print_msg(_(" failed\n"));			write_stderr(_("%s: postmaster does not shut down\n"), progname);			exit(1);		}		print_msg(_(" done\n"));		printf(_("postmaster stopped\n"));	}	else	{		write_stderr(_("%s: old postmaster process (PID: %ld) seems to be gone\n"),					 progname, pid);		write_stderr(_("starting postmaster anyway\n"));	}	do_start();}static voiddo_reload(void){	pgpid_t		pid;	pid = get_pgpid();	if (pid == 0)				/* no pid file */	{		write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);		write_stderr(_("Is postmaster running?\n"));		exit(1);	}	else if (pid < 0)			/* standalone backend, not postmaster */	{		pid = -pid;		write_stderr(_("%s: cannot reload postmaster; "					   "postgres is running (PID: %ld)\n"),					 progname, pid);		write_stderr(_("Please terminate postgres and try again.\n"));		exit(1);	}	if (kill((pid_t) pid, sig) != 0)	{		write_stderr(_("%s: could not send reload signal (PID: %ld): %s\n"),					 progname, pid, strerror(errno));		exit(1);	}	print_msg(_("postmaster signaled\n"));}/* *	utility routines */static boolpostmaster_is_alive(pid_t pid){	/*	 * Test to see if the process is still there.  Note that we do not	 * consider an EPERM failure to mean that the process is still there;	 * EPERM must mean that the given PID belongs to some other userid, and	 * considering the permissions on $PGDATA, that means it's not the	 * postmaster we are after.	 *	 * Don't believe that our own PID or parent shell's PID is the postmaster,	 * either.	(Windows hasn't got getppid(), though.)	 */	if (pid == getpid())		return false;#ifndef WIN32	if (pid == getppid())		return false;#endif	if (kill(pid, 0) == 0)		return true;	return false;}static voiddo_status(void){	pgpid_t		pid;	pid = get_pgpid();	if (pid != 0)				/* 0 means no pid file */	{		if (pid < 0)			/* standalone backend */		{			pid = -pid;			if (postmaster_is_alive((pid_t) pid))			{				printf(_("%s: a standalone backend \"postgres\" is running (PID: %ld)\n"),					   progname, pid);				return;			}		}		else			/* postmaster */		{			if (postmaster_is_alive((pid_t) pid))			{				char	  **optlines;				printf(_("%s: postmaster is running (PID: %ld)\n"),					   progname, pid);				optlines = readfile(postopts_file);				if (optlines != NULL)					for (; *optlines != NULL; optlines++)						fputs(*optlines, stdout);				return;			}		}	}	printf(_("%s: neither postmaster nor postgres running\n"), progname);	exit(1);}static voiddo_kill(pgpid_t pid){	if (kill((pid_t) pid, sig) != 0)	{		write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),					 progname, sig, pid, strerror(errno));		exit(1);	}}#if defined(WIN32) || defined(__CYGWIN__)static boolpgwin32_IsInstalled(SC_HANDLE hSCM){	SC_HANDLE	hService = OpenService(hSCM, register_servicename, SERVICE_QUERY_CONFIG);	bool		bResult = (hService != NULL);	if (bResult)		CloseServiceHandle(hService);	return bResult;}static char *pgwin32_CommandLine(bool registration){	static char cmdLine[MAXPGPATH];	int			ret;#ifdef __CYGWIN__	char		buf[MAXPGPATH];#endif	if (registration)	{		ret = find_my_exec(argv0, cmdLine);		if (ret != 0)		{			write_stderr(_("%s: could not find own program executable\n"), progname);			exit(1);		}	}	else	{		ret = find_other_exec(argv0, "postmaster", PM_VERSIONSTR, cmdLine);		if (ret != 0)		{			write_stderr(_("%s: could not find postmaster program executable\n"), progname);			exit(1);		}	}#ifdef __CYGWIN__	/* need to convert to windows path */	cygwin_conv_to_full_win32_path(cmdLine, buf);	strcpy(cmdLine, buf);#endif	if (registration)	{		if (pg_strcasecmp(cmdLine + strlen(cmdLine) - 4, ".exe"))		{			/* If commandline does not end in .exe, append it */			strcat(cmdLine, ".exe");		}		strcat(cmdLine, " runservice -N \"");		strcat(cmdLine, register_servicename);		strcat(cmdLine, "\"");	}	if (pg_data)	{		strcat(cmdLine, " -D \"");		strcat(cmdLine, pg_data);		strcat(cmdLine, "\"");	}	if (do_wait)		strcat(cmdLine, " -w");	if (post_opts)	{		strcat(cmdLine, " ");		if (registration)			strcat(cmdLine, " -o \"");		strcat(cmdLine, post_opts);		if (registration)			strcat(cmdLine, "\"");	}	return cmdLine;}static voidpgwin32_doRegister(void){	SC_HANDLE	hService;	SC_HANDLE	hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);	if (hSCM == NULL)	{		write_stderr(_("%s: could not open service manager\n"), progname);		exit(1);	}	if (pgwin32_IsInstalled(hSCM))	{		CloseServiceHandle(hSCM);		write_stderr(_("%s: service \"%s\" already registered\n"), progname, register_servicename);		exit(1);	}	if ((hService = CreateService(hSCM, register_servicename, register_servicename,							   SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,								  SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,								  pgwin32_CommandLine(true),	   NULL, NULL, "RPCSS\0", register_username, register_password)) == NULL)	{		CloseServiceHandle(hSCM);		write_stderr(_("%s: could not register service \"%s\": error code %d\n"), progname, register_servicename, (int) GetLastError());		exit(1);	}	CloseServiceHandle(hService);	CloseServiceHandle(hSCM);}static voidpgwin32_doUnregister(void){	SC_HANDLE	hService;	SC_HANDLE	hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);	if (hSCM == NULL)	{		write_stderr(_("%s: could not open service manager\n"), progname);		exit(1);	}	if (!pgwin32_IsInstalled(hSCM))	{		CloseServiceHandle(hSCM);		write_stderr(_("%s: service \"%s\" not registered\n"), progname, register_servicename);		exit(1);	}	if ((hService = OpenService(hSCM, register_servicename, DELETE)) == NULL)	{		CloseServiceHandle(hSCM);		write_stderr(_("%s: could not open service \"%s\": error code %d\n"), progname, register_servicename, (int) GetLastError());		exit(1);	}	if (!DeleteService(hService))	{		CloseServiceHandle(hService);		CloseServiceHandle(hSCM);		write_stderr(_("%s: could not unregister service \"%s\": error code %d\n"), progname, register_servicename, (int) GetLastError());		exit(1);	}	CloseServiceHandle(hService);	CloseServiceHandle(hSCM);}static SERVICE_STATUS status;static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0;static HANDLE shutdownHandles[2];static pid_t postmasterPID = -1;#define shutdownEvent	  shutdownHandles[0]#define postmasterProcess shutdownHandles[1]static voidpgwin32_SetServiceStatus(DWORD currentState){	status.dwCurrentState = currentState;	SetServiceStatus(hStatus, (LPSERVICE_STATUS) & status);}static void WINAPIpgwin32_ServiceHandler(DWORD request){	switch (request)	{		case SERVICE_CONTROL_STOP:		case SERVICE_CONTROL_SHUTDOWN:			/*			 * We only need a short wait hint here as it just needs to wait			 * for the next checkpoint. They occur every 5 seconds during			 * shutdown			 */			status.dwWaitHint = 10000;			pgwin32_SetServiceStatus(SERVICE_STOP_PENDING);			SetEvent(shutdownEvent);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -