📄 run.cpp
字号:
/* * Copyright (c) 1999-2008 Caucho Technology. All rights reserved. * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free SoftwareFoundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */#include <windows.h>#include <winsock.h>#include <stdio.h>#include "process.h"#include "stdlib.h"#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <stdarg.h>#include <io.h>#include <fcntl.h>#include "common.h"#define BUF_SIZE (32 * 1024)static int g_is_dead;static int g_is_started;static int g_keepalive_handle = -1;static HANDLE g_mutex;static HWND g_window;static int g_is_service;static int g_is_standalone;static int g_console = 0;static PROCESS_INFORMATION g_procInfo_buf;static PROCESS_INFORMATION *g_procInfo;voidset_standalone(int is_standalone){ g_is_standalone = is_standalone;}intresin_is_service(){ return g_is_service;}static voidwrite_event_log(char *msg){}voiddie(char *msg, ...){ va_list args; char buf[8192]; va_start(args, msg); vsprintf(buf, msg, args); va_end(args); if (0 && resin_is_service()) { write_event_log(buf); } else if (err) { fprintf(err, "%s\n", buf); fflush(err); } else { FILE *file = fopen("e:/temp/foo.log", "a+"); if (file) { fprintf(file, "%s\n", buf); fclose(file); } } MessageBox(g_window, buf, "Fatal Error", MB_OK); exit(1);}void set_window(HWND window){ g_window = window;}voidstart_server(){ g_is_started = 1;}// XXX: potential sync issues.voidstop_server(){ g_is_started = 0; int handle = g_keepalive_handle; g_keepalive_handle = -1; if (handle >= 0) closesocket(handle); fprintf(stderr, "Stopping Resin\n"); if (g_procInfo) { WaitForSingleObject(g_procInfo_buf.hProcess, 5000); TerminateProcess(g_procInfo_buf.hProcess, 1); }}voidquit_server(){ g_is_dead = 1; stop_server();}intexec_java(char *exe, char **args){ return _spawnv(_P_WAIT, exe, args);}BOOL WINAPI handler(DWORD ctrlType){ return 1;}intspawn_java(char *exe, char **args){ g_is_started = 1; char cmd[32 * 1024]; char arg[32 * 1024]; arg[0] = 0; for (int i = 0; args[i]; i++) { if (i > 0) { strcat(arg, " "); add_path(arg, args[i]); } else strcat(arg, args[i]); } WORD wVersionRequested = MAKEWORD(1,1); WSADATA wsaData; if (WSAStartup(wVersionRequested, &wsaData) != 0) return 1; while (! g_is_dead) { while (! g_is_started) { Sleep(1000); } STARTUPINFO startInfo; memset(&startInfo, 0, sizeof(startInfo)); memset(&g_procInfo_buf, 0, sizeof(g_procInfo_buf)); startInfo.cb = sizeof(startInfo); char childarg[32 * 1024]; char portname[32]; strcpy(childarg, arg); int flag = 0; if (g_is_service) { flag = 0; // DETACHED_PROCESS; SetConsoleCtrlHandler(handler, TRUE); } int ok = CreateProcess(0, childarg, 0, 0, FALSE, flag, 0, 0, &startInfo, &g_procInfo_buf); if (! ok) { //closesocket(sock); fprintf(stderr, "Couldn't start %s %s.\n", cmd, childarg); return 1; } g_procInfo = &g_procInfo_buf; //g_keepalive_handle = sock; WaitForSingleObject(g_procInfo_buf.hProcess, INFINITE); DWORD status; GetExitCodeProcess(g_procInfo_buf.hProcess, &status); if (g_keepalive_handle >= 0) closesocket(g_keepalive_handle); g_keepalive_handle = -1; CloseHandle(g_procInfo_buf.hThread); CloseHandle(g_procInfo_buf.hProcess); g_procInfo = 0; if (status == 66 || g_is_standalone) return status; if (g_is_started || g_is_service) { Sleep(5000); } } return 0;}static voidusage(char *name){ die("usage: %s [flags]\n" " -h : this help\n" " -verbose : information on launching java\n" " -java_home <dir> : sets the JAVA_HOME\n" " -java_exe <path> : path to java executable\n" " -resin_home <dir> : home of Resin\n" " -classpath <dir> : java classpath\n" " -Jxxx : JVM arg xxx\n" " -J-Dfoo=bar : Set JVM variable\n" " -Xxxx : JVM -X parameter\n" " -install : install as NT service\n" " -install-as <name> : install as a named NT service\n" " -remove : remove as NT service\n" " -remove-as <name> : remove as a named NT service\n" " -user <name> : specify username for NT service\n" " -password <pwd> : specify password for NT\n" " -conf <resin.conf> : alternate configuration file\n", name);}static char **set_jdk_args(char *exe, char *cp, char *resin_home, char *server_root, int jit, char *main, int argc, char **argv, char **java_argv){ char buf[BUF_SIZE]; int j; char **args; int i = 0; int arg_count = argc; for (j = 0; java_argv[j]; j++) arg_count++; args = (char **) malloc((arg_count + 16) * (sizeof (char *))); args[i++] = strdup(rsprintf(buf, "\"%s\"", exe)); for (j = 0; java_argv[j]; j++) args[i++] = strdup(rsprintf(buf, "\"%s\"", java_argv[j])); /* args[i++] = "-classpath"; int k = 0; buf[k++] = '\"'; for (j = 0; cp[j]; j++) { if (cp[j] != '"') buf[k++] = cp[j]; } buf[k++] = '\"'; buf[k++] = 0; args[i++] = strdup(buf); */ /* args[i++] = strdup(rsprintf(buf, "-Dresin.home=\"%s\"", resin_home)); args[i++] = strdup(rsprintf(buf, "-Dresin.root=\"%s\"", server_root)); args[i++] = strdup(rsprintf(buf, "-Dserver.root=\"%s\"", server_root)); */ if (! jit) { //args[i++] = "-nojit"; args[i++] = strdup(rsprintf(buf, "-Djava.compiler=NONE")); } /* args[i++] = "-Djava.util.logging.manager=com.caucho.log.LogManagerImpl"; args[i++] = "-Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl"; args[i++] = "-Djava.system.class.loader=com.caucho.loader.SystemClassLoader"; */ // args[i++] = main; args[i++] = "-Xrs"; args[i++] = "-jar"; args[i++] = strdup(rsprintf(buf, "\"%s/lib/resin.jar\"", resin_home)); args[i++] = "--resin-home"; args[i++] = strdup(rsprintf(buf, "\"%s\"", resin_home)); args[i++] = "--root-directory"; args[i++] = strdup(rsprintf(buf, "\"%s\"", server_root)); while (argc > 0) { args[i++] = strdup(rsprintf(buf, "\"%s\"", argv[0])); argc--; argv++; } args[i] = 0; return args;}static char **set_ms_args(char *exe, char *cp, char *server_root, int jit, char *main, int argc, char **argv, char **java_argv){ char buf[BUF_SIZE]; char **args = (char **) malloc((12 + argc) * (sizeof (char *))); int i = 0; args[i++] = strdup(rsprintf(buf, "\"%s\"", exe)); args[i++] = "/cp:a"; args[i++] = strdup(rsprintf(buf, "\"%s\"", cp)); args[i++] = strdup(rsprintf(buf, "\"/d:resin.home=%s\"", server_root)); if (! jit) { args[i++] = strdup(rsprintf(buf, "/d:java.compiler=NONE")); } for (int j = 0; java_argv[j]; j++) args[i++] = strdup(rsprintf(buf, "\"%s\"", java_argv[j])); args[i++] = main; while (argc > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -