📄 resin.c
字号:
/* * Copyright (c) 1998-2002 Caucho Technology -- all rights reserved * * Caucho Technology permits modification and use of this file in * source and binary form ("the Software") subject to the Caucho * Developer Source License 1.1 ("the License") which accompanies * this file. The License is also available at * http://www.caucho.com/download/cdsl1-1.xtp * * In addition to the terms of the License, the following conditions * must be met: * * 1. Each copy or derived work of the Software must preserve the copyright * notice and this notice unmodified. * * 2. Each copy of the Software in source or binary form must include * an unmodified copy of the License in a plain ASCII text file named * LICENSE. * * 3. Caucho reserves all rights to its names, trademarks and logos. * In particular, the names "Resin" and "Caucho" are trademarks of * Caucho and may not be used to endorse products derived from * this software. "Resin" and "Caucho" may not appear in the names * of products derived from this software. * * This Software is provided "AS IS," without a warranty of any kind. * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. * * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY * OF SUCH DAMAGES. * * @author Scott Ferguson */#include <sys/types.h>#include <sys/time.h>#include <sys/socket.h>#include <sys/stat.h>#include <netinet/in.h>#include <netdb.h>#include <fcntl.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <dirent.h>/* probably system-dependent */#include <dlfcn.h>#include <jni.h>#include <pwd.h>#include <grp.h>#include <errno.h>#include <signal.h>#include "cse.h"#include "version.h"#include "resin.h"#include "resin_jni.h"static config_t *g_config;static int g_port_count;static server_socket_t *g_ports[256];#define MAX_OPTIONS 256typedef struct options_t { char *resin_home; char *server_root; char *java_home; char *jvm_type; char *conf; char *classpath; char *loadpath; char *stdout_path; char *stderr_path; char *pid; char *server; int verbose; int start; int stop; int restart; int is_child; JavaVMOption options[MAX_OPTIONS]; int n_options;} options_t;static options_t g_options;java_t g_java;voidcse_log(char *fmt, ...){#ifdef DEBUG va_list list; va_start(list, fmt); vfprintf(stderr, fmt, list); va_end(list);#endif}voidcse_error(config_t *config, char *fmt, ...){ va_list list; va_start(list, fmt); vfprintf(stderr, fmt, list); va_end(list); exit(1);}voidcse_set_socket_cleanup(int socket, void *pool){}voidcse_kill_socket_cleanup(int socket, void *pool){}intcse_lock(void *lock){ return 1;}voidcse_unlock(void *lock){}void *cse_create_lock(config_t *config){ return 0;}/** * Returns true if the path is an absolute path */static intis_path_absolute(char *path){ return (path[0] == '/' || path[0] == '\\' || (path[1] == ':' && ((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z'))));}/** * Add a jvm option */static voidadd_jvm_option(char *fmt, ...){ char buf[16 * 1024]; va_list args; va_start(args, fmt); vsprintf(buf, fmt, args); va_end(args); if (strlen(buf) >= sizeof(buf)) { fprintf(stderr, "argument was too big."); exit(1); } if (g_options.n_options >= MAX_OPTIONS) { fprintf(stderr, "too many options\n"); exit(1); } g_options.options[g_options.n_options++].optionString = strdup(buf);}/** * Bind to a port. * * @param hostname name of the host interface * @param port port to bind * * @return server socket on success, -1 on failure. */static intcse_bind(char *hostname, int port){ struct sockaddr_in sin; int val = 0; struct hostent *hostent; int sock; memset(&sin, 0, sizeof(sin)); if (hostname && *hostname && *hostname != '*') { hostent = gethostbyname(hostname); if (! hostent || ! hostent->h_addr) { fprintf(stderr, "can't lookup host %s\n", hostname); return -1; } memcpy(&sin.sin_addr, hostent->h_addr, sizeof(struct in_addr)); } sin.sin_port = htons(port); sock = socket(AF_INET, SOCK_STREAM, 0); val = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(int)) < 0) { fprintf(stderr, "internal error: bad set sock opt\n"); return -1; } if (bind(sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) { return -1; } listen(sock, 5); /* set non-blocking */ /* flags = fcntl(sock, F_GETFL); fcntl(sock, F_SETFL, O_NONBLOCK|flags); */ return sock;}static voidset_classpath(){ char classpath[16 * 1024]; char path[16 * 1024]; DIR *dir; struct dirent *dir_entry; classpath[0] = 0; if (g_options.classpath) strcat(classpath, g_options.classpath); if (getenv("CLASSPATH")) { strcat(classpath, ":"); strcat(classpath, getenv("CLASSPATH")); } /* classes from resin/classes */ sprintf(path, ":%s/classes", g_options.resin_home); strcat(classpath, path); /* grab jars from resin/lib */ sprintf(path, "%s/lib", g_options.resin_home); dir = opendir(path); while (dir && (dir_entry = readdir(dir))) { int len = strlen(dir_entry->d_name); if (! strcmp(dir_entry->d_name + len - 4, ".jar") || ! strcmp(dir_entry->d_name + len - 4, ".zip")) { strcat(classpath, ":"); strcat(classpath, g_options.resin_home); strcat(classpath, "/lib/"); strcat(classpath, dir_entry->d_name); } } if (dir) closedir(dir); sprintf(path, ":%s/jre/lib/rt.jar", g_options.java_home); strcat(classpath, path); sprintf(path, ":%s/lib/tools.jar", g_options.java_home); strcat(classpath, path); /* * Apparently, this doesn't get added to the list * sprintf(path, "%s/jre/lib/ext", g_options.java_home); dir = opendir(path); while ((dir_entry = readdir(dir))) { int len = strlen(dir_entry->d_name); if (! strcmp(dir_entry->d_name + len - 4, ".jar") || ! strcmp(dir_entry->d_name + len - 4, ".zip")) { strcat(classpath, ":"); strcat(classpath, g_options.java_home); strcat(classpath, "/jre/lib/ext/"); strcat(classpath, dir_entry->d_name); } } closedir(dir); */ g_options.classpath = strdup(classpath); add_jvm_option("-Djava.class.path=%s", classpath);}/** * Returns true if prefix + tail is a file. */static intis_file(char *prefix, char *tail){ struct stat st; char buf[8192]; sprintf(buf, "%s/%s", prefix, tail); return ! stat(buf, &st) && S_ISREG(st.st_mode);}/** * Sets the dynamic loading library path LD_LIBRARY_PATH so the JVM classes * will be automatically picked up. * * @param java_home the JAVA_HOME directory * @param resin_home the RESIN_HOME directory */voidset_library_path(char *java_home, char *resin_home, char *type){ char envpath[8192]; char newpath[8192]; newpath[0] = 0; if (getenv("LD_LIBRARY_PATH")) strcat(newpath, getenv("LD_LIBRARY_PATH")); strcat(newpath, ":"); strcat(newpath, resin_home); strcat(newpath, "/libexec:"); /* IBM JDK */ if (is_file(java_home, "jre/bin/libjava.so")) { char jvmlib[8192]; if (! type) type = "classic"; sprintf(jvmlib, "jre/bin/%s/libjvm.so", type); if (! is_file(java_home, jvmlib)) { fprintf(stderr, "Can't load -%s JVM at %s/%s.\n", type, java_home, jvmlib); exit(1); } strcat(newpath, java_home); strcat(newpath, "/jre/bin/"); strcat(newpath, type); strcat(newpath, ":"); strcat(newpath, java_home); strcat(newpath, "/jre/bin"); } /* Sun JDK */ else if (is_file(java_home, "jre/lib/" CPU "/libjava.so")) { char jvmlib[8192]; if (! type) type = "server"; sprintf(jvmlib, "jre/lib/" CPU "/%s/libjvm.so", type); if (! is_file(java_home, jvmlib)) { fprintf(stderr, "Can't load -%s JVM at %s/%s.\n", type, java_home, jvmlib); exit(1); } strcat(newpath, java_home); strcat(newpath, "/jre/lib/" CPU "/"); strcat(newpath, type); strcat(newpath, ":"); strcat(newpath, java_home); strcat(newpath, "/jre/lib/" CPU "/native_threads:"); strcat(newpath, java_home); strcat(newpath, "/jre/lib/" CPU); } else { fprintf(stderr, "can't use JAVA_HOME=%s\n", java_home); exit(1); } strcpy(envpath, "LD_LIBRARY_PATH="); strcat(envpath, newpath); if (strlen(envpath) > sizeof(envpath)) { fprintf(stderr, "path too long\n"); exit(1); } putenv(strdup(envpath));}/** * Sets the dynamic loading library path LD_LIBRARY_PATH so the JVM classes * will be automatically picked up. * * @param java_home the JAVA_HOME directory * @param resin_home the RESIN_HOME directory */voidset_jvmpath(char *java_home, char *resin_home, char *type, char *path){ /* IBM JDK */ if (is_file(java_home, "jre/bin/libjava.so")) { if (! type) type = "classic"; sprintf(path, "%s/jre/bin/%s/libjvm.so", java_home, type); } /* Sun JDK */ else if (is_file(java_home, "jre/lib/" CPU "/libjava.so")) { if (! type) type = "server"; sprintf(path, "%s/jre/lib/" CPU "/%s/libjvm.so", java_home, type); } else { fprintf(stderr, "can't use JAVA_HOME=%s\n", java_home); exit(1); }}voidsetEnvVars(char *java_home){ char newpath[8192]; char *p; char *buffer = newpath; /* Now add our locale files to the beginning of XFILESEARCHPATH */ strcpy(buffer, "XFILESEARCHPATH="); strcat(buffer, java_home); strcat(buffer, "/jre/lib/locale/%L/%T/%N%S:"); p = getenv("XFILESEARCHPATH"); if (p != NULL) { strcat(buffer, p); } putenv(strdup(buffer));}int cse_test() { return 666; }static intcreate_vm(JavaVM **p_vm, void **env, JavaVMInitArgs *vm_args, char *jvmpath){ void *handle; int (*p_create_vm) (JavaVM **, void **, JavaVMInitArgs *); handle = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL); /* handle = dlopen("libjvm.so", RTLD_LAZY); */ if (! handle) { fprintf(stderr, "%s\n", dlerror()); exit(1); } p_create_vm = dlsym(handle, "JNI_CreateJavaVM"); if (! p_create_vm) { fprintf(stderr, "can't find JNI_CreateJavaVM\n"); exit(1); } return p_create_vm(p_vm, env, vm_args);}/** * Parses the command-line arguments */static char **parse_command_line(int argc, char **argv){ char **new_argv = (char **) malloc((argc + 10) * sizeof(char *)); int i; int j = 0; for (i = 1; i < argc; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -