0N/A/*
0N/A *
0N/A *
0N/A * A simple launcher to launch a program as if it was launched by inetd.
0N/A */
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <sys/types.h>
0N/A#include <sys/socket.h>
0N/A#include <unistd.h>
0N/A#include <dirent.h>
0N/A#include <sys/stat.h>
0N/A#include <fcntl.h>
0N/A#include <ctype.h>
0N/A
0N/A#include "jni.h"
0N/A
0N/A#include "Launcher.h"
0N/A
0N/A/*
0N/A * Throws the exception of the given class name and detail message
0N/A */
0N/Astatic void ThrowException(JNIEnv *env, const char *name, const char *msg) {
0N/A jclass cls = (*env)->FindClass(env, name);
0N/A if (cls != NULL) {
0N/A (*env)->ThrowNew(env, cls, msg);
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Convert a jstring to an ISO 8859_1 encoded C string
0N/A */
0N/Astatic char* getString8859_1Chars(JNIEnv *env, jstring jstr) {
0N/A int i;
0N/A char *result;
0N/A jint len = (*env)->GetStringLength(env, jstr);
0N/A const jchar *str = (*env)->GetStringCritical(env, jstr, 0);
0N/A if (str == 0) {
0N/A return NULL;
0N/A }
0N/A
0N/A result = (char*)malloc(len+1);
0N/A if (result == 0) {
0N/A (*env)->ReleaseStringCritical(env, jstr, str);
0N/A ThrowException(env, "java/lang/OutOfMemoryError", NULL);
0N/A return NULL;
0N/A }
0N/A
0N/A for (i=0; i<len; i++) {
0N/A jchar unicode = str[i];
0N/A if (unicode <= 0x00ff)
0N/A result[i] = unicode;
0N/A else
0N/A result[i] = '?';
0N/A }
0N/A
0N/A result[len] = 0;
0N/A (*env)->ReleaseStringCritical(env, jstr, str);
0N/A return result;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Class: Launcher
0N/A * Method: launch0
0N/A * Signature: ([Ljava/lang/String;I)V
0N/A */
0N/AJNIEXPORT void JNICALL Java_Launcher_launch0
0N/A (JNIEnv *env, jclass cls, jobjectArray cmdarray, jint serviceFd)
0N/A{
0N/A pid_t pid;
0N/A DIR* dp;
0N/A struct dirent* dirp;
0N/A int thisFd;
0N/A char** cmdv;
0N/A int i, cmdlen;
0N/A
0N/A /*
0N/A * Argument 0 of the command array is the program name.
0N/A * Here we just extract the program name and any arguments into
0N/A * a command array suitable for use with execvp.
0N/A */
0N/A cmdlen = (*env)->GetArrayLength(env, cmdarray);
0N/A if (cmdlen == 0) {
0N/A ThrowException(env, "java/lang/IllegalArgumentException",
0N/A "command array must at least include the program name");
0N/A return;
0N/A }
0N/A cmdv = (char **)malloc((cmdlen + 1) * sizeof(char *));
0N/A if (cmdv == NULL) {
0N/A ThrowException(env, "java/lang/OutOfMemoryError", NULL);
0N/A return;
0N/A }
0N/A
0N/A for (i=0; i<cmdlen; i++) {
0N/A jstring str = (*env)->GetObjectArrayElement(env, cmdarray, i);
0N/A cmdv[i] = (char *) getString8859_1Chars(env, str);
0N/A if (cmdv[i] == NULL) {
0N/A return;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Command array must have NULL as the last entry
0N/A */
0N/A cmdv[cmdlen] = NULL;
0N/A
0N/A /*
0N/A * Launch the program. As this isn't a complete inetd or Runtime.exec
0N/A * implementation we don't have a reaper to pick up child exit status.
0N/A */
0N/A#ifdef __solaris__
0N/A pid = fork1();
0N/A#else
0N/A pid = fork();
0N/A#endif
0N/A if (pid != 0) {
0N/A if (pid < 0) {
0N/A ThrowException(env, "java/io/IOException", "fork failed");
0N/A }
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * We need to close all file descriptors except for serviceFd. To
0N/A * get the list of open file descriptos we read through /proc/self/fd
0N/A * but to open this requires a file descriptor. We could use a specific
0N/A * file descriptor and fdopendir but Linux doesn't seem to support
0N/A * fdopendir. Instead we use opendir and make an assumption on the
0N/A * file descriptor that is used (by opening & closing a file).
0N/A */
0N/A thisFd = open("/dev/null", O_RDONLY);
0N/A if (thisFd < 0) {
0N/A _exit(-1);
0N/A }
0N/A close(thisFd);
0N/A
0N/A if ((dp = opendir("/proc/self/fd")) == NULL) {
0N/A _exit(-1);
0N/A }
0N/A
0N/A while ((dirp = readdir(dp)) != NULL) {
0N/A if (isdigit(dirp->d_name[0])) {
0N/A int fd = strtol(dirp->d_name, NULL, 10);
0N/A if (fd != serviceFd && fd != thisFd) {
0N/A close(fd);
0N/A }
0N/A }
0N/A }
0N/A closedir(dp);
0N/A
0N/A /*
0N/A * At this point all file descriptors are closed except for
0N/A * serviceFd. We not dup 0,1,2 to this file descriptor and
0N/A * close serviceFd. This should leave us with only 0,1,2
0N/A * open and all connected to the same socket.
0N/A */
0N/A dup2(serviceFd, STDIN_FILENO);
0N/A dup2(serviceFd, STDOUT_FILENO);
0N/A dup2(serviceFd, STDERR_FILENO);
0N/A close(serviceFd);
0N/A
0N/A execvp(cmdv[0], cmdv);
0N/A _exit(-1);
0N/A}