Menu

[r141]: / branches / PHP-5 / php-java-bridge / bind.c  Maximize  Restore  History

Download this file

124 lines (110 with data), 2.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*-*- mode: C; tab-width:4 -*-*/
/* execve */
#include <unistd.h>
#include <sys/types.h>
/* fcntl */
#include <fcntl.h>
/* strings */
#include <string.h>
/* setenv */
#include <stdlib.h>
/* miscellaneous */
#include <stdio.h>
#include <assert.h>
#include <errno.h>
/* path and dir separators */
#include "php.h"
#include "zend.h"
#include "php_java.h"
void java_get_server_args(struct cfg*cfg, char*env[N_SENV], char*args[N_SARGS]) {
char *s, *p;
char*program=cfg->java;
char*cp=cfg->classpath;
char*lib_path=cfg->ld_library_path;
char*home=cfg->java_home;
args[0]=strdup(program);
s="-Djava.library.path=";
p=malloc(strlen(s)+strlen(lib_path)+1);
strcpy(p, s); strcat(p, lib_path);
args[1] = p; /* library path */
s="-Djava.class.path=";
p=malloc(strlen(s)+strlen(cp)+1);
strcpy(p, s); strcat(p, cp);
args[2] = p; /* user classes */
args[3] = strdup("-Djava.awt.headless=true");
/* disabled due to problems with IBM java, it could not find
default mime table anymore */
//s="-Djava.home=";
//p=malloc(strlen(s)+strlen(home)+1);
//strcpy(p, s); strcat(p, home);
//args[4] = p; /* java home */
args[4] = strdup("JavaBridge");
args[5] = strdup(cfg->sockname);
args[6] = strdup(cfg->logLevel);
args[7] = strdup(cfg->logFile);
args[8] = NULL;
s="JAVA_HOME=";
p=malloc(strlen(s)+strlen(home)+1);
strcpy(p, s); strcat(p, home);
env[0] = p; /* java home */
s="LD_LIBRARY_PATH=";
p=malloc(strlen(s)+strlen(lib_path)+1);
strcpy(p, s); strcat(p, lib_path);
env[1] = p; /* library path */
env[2] = NULL;
}
static void exec_vm(struct cfg*cfg) {
static char*env[N_SENV];
static char*args[N_SARGS];
java_get_server_args(cfg, env, args);
putenv(env[0]);
putenv(env[1]);
#ifdef CFG_JAVA_INPROCESS
{
extern int java_bridge_main(int argc, char**argv) ;
java_bridge_main(N_SARGS, args);
}
#else
execv(args[0], args);
#endif
}
/*
return 0 if user has hard-coded the socketname
*/
static short can_fork() {
#ifndef CFG_JAVA_SOCKET_ANON
return (java_ini_updated&U_SOCKNAME)==0;
#else
return 1; /* ignore the already running JVM and
start a new JVM with the anonymous
socket */
#endif
}
void java_start_server(struct cfg*cfg) {
int pid=0, err=0, p[2], p1[2];
if(pipe(p)!=-1) {
if(can_fork()) {
if(!(pid=fork())) { /* daemon */
close(p[0]);
if(!fork()) { /* guard */
if(!(pid=fork())) { /* java */
setsid();
close(p[1]);
exec_vm(cfg);
exit(105);
}
write(p[1], &pid, sizeof pid);
waitpid(pid, &err, 0);
write(p[1], &err, sizeof err);
exit(0);
}
exit(0);
}
close(p[1]);
wait(&err);
if((read(p[0], &pid, sizeof pid))!=(sizeof pid)) pid=0;
}
}
cfg->cid=pid;
cfg->err=p[0];
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.