version 1.4, 2000/01/31 01:34:23 |
version 1.10, 2002/05/02 03:08:28 |
|
|
/* $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.3 1999/11/04 02:12:31 takayama Exp $ */ |
/* $OpenXM: OpenXM/src/kxx/oxmain.c,v 1.9 2001/12/28 01:20:27 takayama Exp $ */ |
/* nullserver01 */ |
/* nullserver01 */ |
#include <stdio.h> |
#include <stdio.h> |
#include <fcntl.h> |
#include <fcntl.h> |
|
#include <unistd.h> |
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/socket.h> |
#include <sys/socket.h> |
#include <sys/time.h> |
#include <sys/time.h> |
Line 32 int Do_not_use_control_stream_to_tell_no_server = 1; |
|
Line 33 int Do_not_use_control_stream_to_tell_no_server = 1; |
|
static void errorToStartEngine(void); |
static void errorToStartEngine(void); |
static int findOxServer(char *server); |
static int findOxServer(char *server); |
static void couldNotFind(char *s); |
static void couldNotFind(char *s); |
|
#if defined(__CYGWIN__) |
|
int errno; |
|
#endif |
|
/* gcc -v -c hoge.c */ |
|
|
|
|
main(int argc, char *argv[]) { |
main(int argc, char *argv[]) { |
int fd; |
int fd; |
int size; |
int size; |
Line 48 main(int argc, char *argv[]) { |
|
Line 52 main(int argc, char *argv[]) { |
|
char portfile[1024]; |
char portfile[1024]; |
char *pass; |
char *pass; |
int result; |
int result; |
|
int sleepingTime = 0; |
|
|
strcpy(sname,"localhost"); |
strcpy(sname,"localhost"); |
strcpy(ServerName,SERVERNAME); |
strcpy(ServerName,SERVERNAME); |
Line 78 main(int argc, char *argv[]) { |
|
Line 83 main(int argc, char *argv[]) { |
|
}else if (strcmp(argv[i],"-portfile") == 0) { |
}else if (strcmp(argv[i],"-portfile") == 0) { |
i++; |
i++; |
if (i<argc) { |
if (i<argc) { |
sscanf(argv[i],"%s",portfile); |
sscanf(argv[i],"%s",portfile); |
portControl = 0; |
portControl = 0; |
portStream = 0; |
portStream = 0; |
NotifyPortnumber = 1; |
NotifyPortnumber = 1; |
} |
} |
}else if (strcmp(argv[i],"-pass") == 0) { |
}else if (strcmp(argv[i],"-pass") == 0) { |
i++; |
i++; |
if (i<argc) { |
if (i<argc) { |
pass = argv[i]; |
pass = argv[i]; |
} |
} |
|
}else if (strcmp(argv[i],"-wait") == 0) { |
|
i++; |
|
if (i<argc) { |
|
sscanf(argv[i],"%d",&sleepingTime); |
|
} |
}else { |
}else { |
fprintf(stderr,"Unknown option %s.\n",argv[i]); |
fprintf(stderr,"Unknown option %s.\n",argv[i]); |
oxmainUsage(); exit(10); |
oxmainUsage(); exit(10); |
Line 96 main(int argc, char *argv[]) { |
|
Line 106 main(int argc, char *argv[]) { |
|
} |
} |
|
|
if (Do_not_use_control_stream_to_tell_no_server) { |
if (Do_not_use_control_stream_to_tell_no_server) { |
if (findOxServer(ServerName) < 0) { |
if (findOxServer(ServerName) < 0) { |
fprintf(stderr,"Sleeping five seconds...\n"); |
fprintf(stderr,"Sleeping five seconds...\n"); |
sleep(5); |
sleep(5); |
exit(-1); |
exit(-1); |
} |
} |
} |
} |
|
|
|
if (sleepingTime) { |
|
fprintf(stderr,"Waiting to connect for %d seconds...\n",sleepingTime); |
|
sleep(sleepingTime); |
|
fprintf(stderr,"\nTrying to connect\n"); |
|
} |
|
|
if (reverse) { |
if (reverse) { |
/* The order is very important. */ |
/* The order is very important. */ |
fdControl = socketConnectWithPass(sname,portControl,pass); |
fdControl = socketConnectWithPass(sname,portControl,pass); |
fdStream = socketConnectWithPass(sname,portStream,pass); |
fdStream = socketConnectWithPass(sname,portStream,pass); |
|
|
fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream); |
fprintf(stderr,"Connected: control = %d, data = %d.\n",fdControl,fdStream); |
result = 0; |
result = 0; |
|
|
|
|
if (portControl != -1) { |
if (portControl != -1) { |
MyServerPid = fork(); |
MyServerPid = fork(); |
if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream); |
if (MyServerPid > 0 ) parentServerMain(fdControl,fdStream); |
Line 118 main(int argc, char *argv[]) { |
|
Line 136 main(int argc, char *argv[]) { |
|
result=childServerMain(fdControl,fdStream); |
result=childServerMain(fdControl,fdStream); |
} |
} |
/* This line will be never executed in case of success */ |
/* This line will be never executed in case of success */ |
if (result < 0 ) { |
if (result < 0 ) { |
errorToStartEngine(); |
errorToStartEngine(); |
} |
} |
} |
} |
|
|
/* non-reverse case. */ |
/* non-reverse case. */ |
Line 135 main(int argc, char *argv[]) { |
|
Line 153 main(int argc, char *argv[]) { |
|
fdControl = socketOpen(sname,portControl); |
fdControl = socketOpen(sname,portControl); |
portControl = OpenedSocket; |
portControl = OpenedSocket; |
if (NotifyPortnumber) { |
if (NotifyPortnumber) { |
oxWritePortFile(0,portControl,portfile); |
oxWritePortFile(0,portControl,portfile); |
} |
} |
fdControl = socketAcceptLocal(fdControl); |
fdControl = socketAcceptLocal(fdControl); |
fprintf(stderr,"\n control port %d : Connected.\n",portControl); |
fprintf(stderr,"\n control port %d : Connected.\n",portControl); |
Line 144 main(int argc, char *argv[]) { |
|
Line 162 main(int argc, char *argv[]) { |
|
fdStream = socketOpen(sname,portStream); |
fdStream = socketOpen(sname,portStream); |
portStream = OpenedSocket; |
portStream = OpenedSocket; |
if (NotifyPortnumber) { |
if (NotifyPortnumber) { |
oxWritePortFile(1,portStream,portfile); |
oxWritePortFile(1,portStream,portfile); |
} |
} |
fdStream = socketAcceptLocal(fdStream); |
fdStream = socketAcceptLocal(fdStream); |
fprintf(stderr,"\n stream port %d : Connected.\n",portStream); |
fprintf(stderr,"\n stream port %d : Connected.\n",portStream); |
Line 154 main(int argc, char *argv[]) { |
|
Line 172 main(int argc, char *argv[]) { |
|
fdControl = socketOpen(sname,portControl); |
fdControl = socketOpen(sname,portControl); |
portControl = OpenedSocket; |
portControl = OpenedSocket; |
if (NotifyPortnumber) { |
if (NotifyPortnumber) { |
oxWritePortFile(0,portControl,portfile); |
oxWritePortFile(0,portControl,portfile); |
} |
} |
fdControl = socketAccept(fdControl); |
fdControl = socketAccept(fdControl); |
fprintf(stderr,"\n control port %d : Connected.\n",portControl); |
fprintf(stderr,"\n control port %d : Connected.\n",portControl); |
Line 163 main(int argc, char *argv[]) { |
|
Line 181 main(int argc, char *argv[]) { |
|
fdStream = socketOpen(sname,portStream); |
fdStream = socketOpen(sname,portStream); |
portStream = OpenedSocket; |
portStream = OpenedSocket; |
if (NotifyPortnumber) { |
if (NotifyPortnumber) { |
oxWritePortFile(1,portStream,portfile); |
oxWritePortFile(1,portStream,portfile); |
} |
} |
fdStream = socketAccept(fdStream); |
fdStream = socketAccept(fdStream); |
fprintf(stderr,"\n stream port %d : Connected.\n",portStream); |
fprintf(stderr,"\n stream port %d : Connected.\n",portStream); |
Line 185 main(int argc, char *argv[]) { |
|
Line 203 main(int argc, char *argv[]) { |
|
static void errorToStartEngine(void) { |
static void errorToStartEngine(void) { |
fprintf(stderr,"Failed to start the engine. Childing process is terminating.\n"); |
fprintf(stderr,"Failed to start the engine. Childing process is terminating.\n"); |
/* You have to tell to the control server that there is no engine. |
/* You have to tell to the control server that there is no engine. |
And, the control server must tell the client that there is no |
And, the control server must tell the client that there is no |
engine. |
engine. |
This part has not yet been implemented. |
This part has not yet been implemented. |
If you implement this, set Do_not_use_control_stream_to_tell_no_server to |
If you implement this, set Do_not_use_control_stream_to_tell_no_server to |
zero. |
zero. |
*/ |
*/ |
sleep(2); |
sleep(2); |
exit(-1); |
exit(-1); |
} |
} |
Line 260 parentServerMain(int fdControl, int fdStream) { |
|
Line 278 parentServerMain(int fdControl, int fdStream) { |
|
r=kill(MyServerPid,SIGUSR1); |
r=kill(MyServerPid,SIGUSR1); |
if (message) printf("Result = %d\n",r); |
if (message) printf("Result = %d\n",r); |
fflush(NULL); |
fflush(NULL); |
oxSendResultOfControlInt32(fdControl,0); |
/* oxSendResultOfControlInt32(fdControl,0); */ |
break; |
break; |
default: |
default: |
fprintf(stderr,"[control] Unknown control message.\n"); |
fprintf(stderr,"[control] Unknown control message.\n"); |
Line 283 childServerMain(int fdControl, int fdStream) { |
|
Line 301 childServerMain(int fdControl, int fdStream) { |
|
dup2(fdStream,3); |
dup2(fdStream,3); |
dup2(fdStream,4); |
dup2(fdStream,4); |
/*close(0); |
/*close(0); |
#include <sys/param.h> |
#include <sys/param.h> |
for (i=5; i<NOFILE; i++) close(i); |
for (i=5; i<NOFILE; i++) close(i); |
*/ |
*/ |
if (!Do_not_use_control_stream_to_tell_no_server) { |
if (!Do_not_use_control_stream_to_tell_no_server) { |
if (findOxServer(ServerName) < 0) { |
if (findOxServer(ServerName) < 0) { |
return(-1); |
return(-1); |
} |
} |
} |
} |
|
fprintf(stderr,"childServerMain: Starting the server %s\n",ServerName); fflush(NULL); |
if (PacketMonitor) { |
if (PacketMonitor) { |
if (execl(ServerName,ServerName,"-monitor",NULL)) { |
if (execl(ServerName,ServerName,"-monitor",NULL)) { |
fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName); |
fprintf(stderr,"%s cannot be executed with -monitor.\n",ServerName); |
return(-1); |
fflush(NULL); |
|
return(-1); |
} |
} |
}else { |
}else { |
if (execl(ServerName,ServerName,NULL)) { |
if (execl(ServerName,ServerName,NULL)) { |
fprintf(stderr,"%s cannot be executed.\n",ServerName); |
fprintf(stderr,"%s cannot be executed.\n",ServerName); |
return(-1); |
fflush(NULL); |
|
return(-1); |
} |
} |
} |
} |
/* never reached. */ |
/* never reached. */ |
Line 316 static int findOxServer(char *server) { |
|
Line 337 static int findOxServer(char *server) { |
|
int fd; |
int fd; |
char *getenv(char *s); |
char *getenv(char *s); |
if (strlen(server) == 0) return(-1); |
if (strlen(server) == 0) return(-1); |
fd = open(server,O_RDONLY); |
/* fd = open(server,O_RDONLY); */ |
|
fd = access(server,X_OK&R_OK); |
if (fd >= 0) { |
if (fd >= 0) { |
fprintf(stderr,"Starting OX server : %s\n",server); |
fprintf(stderr,"Starting OX server : %s\n",server); |
close(fd); |
close(fd); |
return(0); |
return(0); |
} |
} |
if (server[0] == '/') { |
if (server[0] == '/') { |
couldNotFind(server); |
couldNotFind(server); |
return(-1); |
return(-1); |
} |
} |
fprintf(stderr,"The server %s was not found. Trying to find it under OpenXM/bin\n",server); |
fprintf(stderr,"The server %s was not found. Trying to find it under OpenXM/bin\n",server); |
p = getenv("OpenXM_HOME"); |
p = getenv("OpenXM_HOME"); |
Line 334 static int findOxServer(char *server) { |
|
Line 356 static int findOxServer(char *server) { |
|
p2 = (char *) malloc(sizeof(char)*(strlen(p)+strlen("/bin/")+3+strlen(server))); |
p2 = (char *) malloc(sizeof(char)*(strlen(p)+strlen("/bin/")+3+strlen(server))); |
if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); } |
if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); } |
strcpy(p2,p); strcat(p2,"/bin/"); strcat(p2,server); |
strcpy(p2,p); strcat(p2,"/bin/"); strcat(p2,server); |
fd = open(p2,O_RDONLY); |
/* fd = open(p2,O_RDONLY); */ |
|
fd = access(p2,X_OK&R_OK); |
if (fd >= 0) { |
if (fd >= 0) { |
fprintf(stderr,"Starting OX server : %s\n",p2); |
fprintf(stderr,"Starting OX server : %s\n",p2); |
if (strlen(p2) < SERVERNAME_SIZE) strcpy(server,p2); |
if (strlen(p2) < SERVERNAME_SIZE) strcpy(server,p2); |
else { |
else { |
couldNotFind("Too long ox server name."); |
couldNotFind("Too long ox server name."); |
return(-1); |
return(-1); |
} |
} |
close(fd); |
close(fd); |
return(0); |
return(0); |
} |
} |
couldNotFind(p2); |
couldNotFind(p2); |
return(-1); |
return(-1); |