This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

programming a service for cygrunsrv (c, jni, rmi, servlet, tomcat)


Hi, I wrote a program in C to launch rmiregistry and to start my RMI server. The
Program works fine but when I tried `cygrunsrv --install MvServer --path
/cygdrive/c/classes/MvServer/MvServer.exe` and finally `cygrunsrv -S MvServer`
the RMI server seems to be death. I don't get any error messages in the log.
Below is my entire code of MvServer.c written in C.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <jni.h>

typedef struct _MvServer MvServer;

struct _MvServer{
  JavaVM *jvm;
  JNIEnv *env;
} server = {
  NULL, NULL
};

void* mv_server_rmi_thread(void *data);
void* mv_server_main_thread(void *data);
void mv_server_term_signal();

FILE *log_fd;

void*
mv_server_rmi_thread(void *data)
{
  execl("/cygdrive/c/Programme/Java/jdk1.6.0_13/bin/rmiregistry.exe\0", NULL);

  return(NULL);
}

void
mv_server_term_signal()
{
  jclass cls;
  jmethodID mid;
  jint rc;

  cls = (*(server.env))->FindClass((server.env), "MvServer\0");
  
  if(cls == 0){
    fprintf(log_fd, "couldn't invoke MvServer\n\0");
    fflush(log_fd);
    exit(0);
  }

  mid = (*(server.env))->GetStaticMethodID((server.env), cls, "shutdown\0",
"([Ljava/lang/String;)V\0");
  
  if(mid == 0){
    fprintf(log_fd, "couldn't get shutdown\n\0");
    fflush(log_fd);
    fclose(log_fd);
    exit(0);
  }else{
    (*(server.env))->CallStaticVoidMethod((server.env), cls, mid, 0);
    (*(server.jvm))->DestroyJavaVM((server.jvm));
  }
}

void*
mv_server_main_thread(void *data)
{
  JavaVM *jvm;       /* denotes a Java VM */
  JNIEnv *env;       /* pointer to native method interface */
  JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
  JavaVMOption options[3];
  jclass cls;
  jmethodID mid;
  jint rc;

  options[0].optionString =
"-Djava.class.path=C:\\classes\\MvServer;C:\\Programme\\jdom-1.1\\build\\jdom.jar;C:\\Programme\\Java\\jdk1.6.0_13;C:\\xampp\\tomcat\\lib\\servlet-api.jar\0";
  options[1].optionString =
"-Djava.rmi.server.codebase=file:///c/classes/MvServer\0";
  options[2].optionString = "-Djava.security.policy=policy.txt\0";

  vm_args.version = JNI_VERSION_1_6;
  vm_args.nOptions = 3;
  vm_args.options = options;
  vm_args.ignoreUnrecognized = 0;

  /* load and initialize a Java VM, return a JNI interface
   * pointer in env */
  rc = JNI_CreateJavaVM(&jvm, (void **) &env, (void *) &vm_args);

  if(rc < 0){
    fprintf(log_fd, "couldn't open Java VM\n\0");
    fflush(log_fd);
    fclose(log_fd);
    return(0);
  }

  server.jvm = jvm;
  server.env = env;

  /* invoke the Main.test method using the JNI */
  cls = (*env)->FindClass(env, "MvServer");

  if(cls == 0){
    fprintf(log_fd, "couldn't invoke MvServer\n\0");
    fflush(log_fd);
    fclose(log_fd);
    return(0);
  }

  mid = (*env)->GetStaticMethodID(env, cls, "main\0", "([Ljava/lang/String;)V\0");

  if(mid == 0){
    fprintf(log_fd, "couldn't get main\n\0");
    fflush(log_fd);
    fclose(log_fd);
    return(0);
  }

  (*env)->CallStaticVoidMethod(env, cls, mid, 0);
  /* We are done. */
  (*jvm)->DestroyJavaVM(jvm);

  return(NULL);
}

int
main(int argc, char **argv)
{
  pthread_t rmi_thread;
  pthread_t main_thread;
  pid_t child_pid, wpid;
  int status;

  signal(SIGTERM, mv_server_term_signal);

  log_fd = fopen("/cygdrive/c/classes/MvServer/error.log\0", "w+\0");
  fprintf(log_fd, "starting MvServer\n\0");
  fflush(log_fd);

  if((child_pid = fork()) < 0){
    fprintf(log_fd, "failed to fork process\n\0");
  }else if(child_pid == 0){
    pthread_create(&rmi_thread, NULL, mv_server_rmi_thread, NULL);
    usleep(5000);

    pthread_create(&main_thread, NULL, mv_server_main_thread, NULL);
  }else{
    wpid = waitpid(child_pid, &status,
		   WUNTRACED | WCONTINUED);

  }
  
  return(0);
}




--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]