📄 jk_nt_service.c
字号:
// dwSize - size of buffer
//
// RETURN VALUE:
// destination buffer
//
// COMMENTS:
//
char *GetLastErrorText( char *lpszBuf, DWORD dwSize )
{
DWORD dwRet;
char *lpszTemp = NULL;
dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
NULL,
GetLastError(),
LANG_NEUTRAL,
(char *)&lpszTemp,
0,
NULL);
// supplied buffer is not long enough
if(!dwRet || ((long)dwSize < (long)dwRet+14)) {
lpszBuf[0] = '\0';
} else {
lpszTemp[lstrlen(lpszTemp)-2] = '\0'; //remove cr and newline character
sprintf(lpszBuf, "%s (0x%x)", lpszTemp, GetLastError());
}
if(lpszTemp) {
LocalFree((HLOCAL) lpszTemp );
}
return lpszBuf;
}
static void stop_tomcat(char *name,
int port,
const char *protocol,
char *secret,
HANDLE hTomcat)
{
struct sockaddr_in in;
if(strcasecmp(protocol, "cmd") == 0 ) {
exec_cmd( name, hTomcat, shutdown_cmd);
/* XXX sleep 100 */
TerminateProcess(hTomcat, 0);
return;
}
if(jk_resolve("localhost", port, &in)) {
int sd = jk_open_socket(&in, JK_TRUE, 0, -1, NULL);
if(sd >0) {
int rc = JK_FALSE;
if(strcasecmp(protocol, "catalina") == 0 ) {
char len;
if( secret==NULL )
secret="SHUTDOWN";
len=strlen( secret );
rc = send(sd, secret, len , 0);
if(len == rc) {
rc = JK_TRUE;
}
} else if(!strcasecmp(protocol, "ajp13")) {
jk_pool_t pool;
jk_msg_buf_t *msg = NULL;
jk_pool_atom_t buf[TINY_POOL_SIZE];
jk_open_pool(&pool, buf, sizeof(buf));
msg = jk_b_new(&pool);
jk_b_set_buffer_size(msg, 512);
rc = ajp13_marshal_shutdown_into_msgb(msg,
&pool,
NULL);
if( secret!=NULL ) {
/** will work with old clients, as well as new
*/
rc = jk_b_append_string(msg, secret);
}
if(rc) {
jk_b_end(msg, AJP13_PROTO);
if(0 > jk_tcp_socket_sendfull(sd,
msg->buf,
msg->len)) {
rc = JK_FALSE;
}
}
} else {
char b[] = {(char)254, (char)15};
rc = send(sd, b, 2, 0);
if(2 == rc) {
rc = JK_TRUE;
}
}
jk_close_socket(sd);
if(JK_TRUE == rc) {
if(WAIT_OBJECT_0 == WaitForSingleObject(hTomcat, 30*1000)) {
return;
}
}
}
}
TerminateProcess(hTomcat, 0);
}
static int exec_cmd(const char *name, HANDLE *hTomcat, char *cmdLine)
{
char tag[1024];
HKEY hk;
strcpy(tag, BASE_REGISTRY_LOCATION);
strcat(tag, name);
strcat(tag, "\\");
strcat(tag, PARAMS_LOCATION);
if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
tag,
(DWORD)0,
KEY_READ,
&hk)) {
char prp_file[2048];
if(get_registry_config_parameter(hk,
PRP_LOCATION,
prp_file,
sizeof(prp_file))) {
jk_map_t *init_map;
if(jk_map_alloc(&init_map)) {
if(jk_map_read_properties(init_map, prp_file, NULL)) {
jk_tomcat_startup_data_t data;
jk_pool_t p;
jk_pool_atom_t buf[HUGE_POOL_SIZE];
jk_open_pool(&p, buf, sizeof(buf));
if(read_startup_data(init_map, &data, &p)) {
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
if(data.extra_path) {
jk_append_libpath(&p, data.extra_path);
}
memset(&startupInfo, 0, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.lpTitle = "Jakarta Tomcat";
startupInfo.dwFlags = STARTF_USESTDHANDLES;
startupInfo.hStdInput = NULL;
startupInfo.hStdOutput = CreateFile(data.stdout_file,
GENERIC_WRITE,
FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
SetFilePointer(startupInfo.hStdOutput,
0,
NULL,
FILE_END);
startupInfo.hStdError = CreateFile(data.stderr_file,
GENERIC_WRITE,
FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
SetFilePointer(startupInfo.hStdError,
0,
NULL,
FILE_END);
memset(&processInformation, 0, sizeof(processInformation));
if( cmdLine==NULL )
cmdLine=data.cmd_line;
printf(cmdLine);
if(CreateProcess(data.java_bin,
cmdLine,
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
data.tomcat_home,
&startupInfo,
&processInformation)){
*hTomcat = processInformation.hProcess;
CloseHandle(processInformation.hThread);
CloseHandle(startupInfo.hStdOutput);
CloseHandle(startupInfo.hStdError);
shutdown_port = data.shutdown_port;
shutdown_secret = data.shutdown_secret;
shutdown_protocol = strdup(data.shutdown_protocol);
shutdown_cmd = strdup(data.stop_cmd);
return JK_TRUE;
} else {
printf("Error: Can not create new process - %s\n",
GetLastErrorText(szErr, sizeof(szErr)));
}
}
}
}
jk_map_free(&init_map);
}
RegCloseKey(hk);
}
return JK_FALSE;
}
static int start_tomcat(const char *name, HANDLE *hTomcat)
{
return exec_cmd( name, hTomcat, NULL );
}
static int create_registry_key(const char *tag,
HKEY *key)
{
LONG lrc = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
tag,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
NULL,
key,
NULL);
if(ERROR_SUCCESS != lrc) {
return JK_FALSE;
}
return JK_TRUE;
}
static int set_registry_config_parameter(HKEY hkey,
const char *tag,
char *value)
{
LONG lrc;
lrc = RegSetValueEx(hkey,
tag,
0,
REG_SZ,
value,
strlen(value));
if(ERROR_SUCCESS != lrc) {
return JK_FALSE;
}
return JK_TRUE;
}
static int get_registry_config_parameter(HKEY hkey,
const char *tag,
char *b,
DWORD sz)
{
DWORD type = 0;
LONG lrc;
lrc = RegQueryValueEx(hkey,
tag,
(LPDWORD)0,
&type,
(LPBYTE)b,
&sz);
if(ERROR_SUCCESS != lrc) {
return JK_FALSE;
}
b[sz] = '\0';
return JK_TRUE;
}
static int read_startup_data(jk_map_t *init_map,
jk_tomcat_startup_data_t *data,
jk_pool_t *p)
{
data->classpath = NULL;
data->tomcat_home = NULL;
data->stdout_file = NULL;
data->stderr_file = NULL;
data->java_bin = NULL;
data->extra_path = NULL;
data->tomcat_class = NULL;
data->server_file = NULL;
/* All this is wrong - you just need to configure cmd_line */
/* Optional - you may have cmd_line defined */
data->server_file = jk_map_get_string(init_map,
"wrapper.server_xml",
NULL);
data->classpath = jk_map_get_string(init_map,
"wrapper.class_path",
NULL);
data->tomcat_home = jk_map_get_string(init_map,
"wrapper.tomcat_home",
NULL);
data->java_bin = jk_map_get_string(init_map,
"wrapper.javabin",
NULL);
data->tomcat_class = jk_map_get_string(init_map,
"wrapper.startup_class",
"org.apache.tomcat.startup.Tomcat");
data->cmd_line = jk_map_get_string(init_map,
"wrapper.cmd_line",
NULL);
data->stop_cmd = jk_map_get_string(init_map,
"wrapper.stop_cmd",
NULL);
if(NULL == data->cmd_line &&
( (NULL == data->tomcat_class) ||
(NULL == data->server_file) ||
(NULL == data->tomcat_home) ||
(NULL == data->java_bin) )) {
return JK_FALSE;
}
if(NULL == data->cmd_line) {
data->cmd_line = (char *)jk_pool_alloc(p, (20 +
strlen(data->java_bin) +
strlen(" -classpath ") +
strlen(data->classpath) +
strlen(data->tomcat_class) +
strlen(" -home ") +
strlen(data->tomcat_home) +
strlen(" -config ") +
strlen(data->server_file)
) * sizeof(char));
if(NULL == data->cmd_line) {
return JK_FALSE;
}
strcpy(data->cmd_line, data->java_bin);
strcat(data->cmd_line, " -classpath ");
strcat(data->cmd_line, data->classpath);
strcat(data->cmd_line, " ");
strcat(data->cmd_line, data->tomcat_class);
strcat(data->cmd_line, " -home ");
strcat(data->cmd_line, data->tomcat_home);
strcat(data->cmd_line, " -config ");
strcat(data->cmd_line, data->server_file);
}
data->shutdown_port = jk_map_get_int(init_map,
"wrapper.shutdown_port",
8007);
data->shutdown_secret = jk_map_get_string(init_map,
"wrapper.shutdown_secret",
NULL);
data->shutdown_protocol = jk_map_get_string(init_map,
"wrapper.shutdown_protocol",
AJP12_TAG);
data->extra_path = jk_map_get_string(init_map,
"wrapper.ld_path",
NULL);
data->stdout_file = jk_map_get_string(init_map,
"wrapper.stdout",
NULL);
if(NULL == data->stdout_file && NULL == data->tomcat_home ) {
return JK_FALSE;
}
if(NULL == data->stdout_file) {
data->stdout_file = jk_pool_alloc(p, strlen(data->tomcat_home) + 2 + strlen("\\stdout.log"));
strcpy(data->stdout_file, data->tomcat_home);
strcat(data->stdout_file, "\\stdout.log");
}
data->stderr_file = jk_map_get_string(init_map,
"wrapper.stderr",
NULL);
if(NULL == data->stderr_file) {
data->stderr_file = jk_pool_alloc(p, strlen(data->tomcat_home) + 2 + strlen("\\stderr.log"));
strcpy(data->stderr_file, data->tomcat_home);
strcat(data->stderr_file, "\\stderr.log");
}
return JK_TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -