1 /*
2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * A simple launcher to launch a program as if it was launched by inetd.
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <ctype.h>
37 
38 #include "jni.h"
39 
40 #define CHECK(X) if ((X) == 0) {printf("JNI init error line %d\n", __LINE__); _exit(1);}
41 
42 /*
43  * Throws the exception of the given class name and detail message
44  */
ThrowException(JNIEnv * env,const char * name,const char * msg)45 static void ThrowException(JNIEnv *env, const char *name, const char *msg) {
46     jclass cls = (*env)->FindClass(env, name);
47     if (cls != NULL) {
48         (*env)->ThrowNew(env, cls, msg);
49     }
50 }
51 
52 /*
53  * Convert a jstring to an ISO 8859_1 encoded C string
54  */
getString8859_1Chars(JNIEnv * env,jstring jstr)55 static char* getString8859_1Chars(JNIEnv *env, jstring jstr) {
56     int i;
57     char *result;
58     jint len = (*env)->GetStringLength(env, jstr);
59     const jchar *str = (*env)->GetStringCritical(env, jstr, 0);
60     if (str == 0) {
61         return NULL;
62     }
63 
64     result = (char*)malloc(len+1);
65     if (result == 0) {
66         (*env)->ReleaseStringCritical(env, jstr, str);
67         ThrowException(env, "java/lang/OutOfMemoryError", NULL);
68         return NULL;
69     }
70 
71     for (i=0; i<len; i++) {
72         jchar unicode = str[i];
73         if (unicode <= 0x00ff)
74             result[i] = unicode;
75         else
76             result[i] = '?';
77     }
78 
79     result[len] = 0;
80     (*env)->ReleaseStringCritical(env, jstr, str);
81     return result;
82 }
83 
84 /*
85  * Class:     Launcher
86  * Method:    launch0
87  * Signature: ([Ljava/lang/String;I)V
88  */
Java_Launcher_launch0(JNIEnv * env,jclass cls,jobjectArray cmdarray,jint serviceFd)89 JNIEXPORT void JNICALL Java_Launcher_launch0
90   (JNIEnv *env, jclass cls, jobjectArray cmdarray, jint serviceFd)
91 {
92     pid_t pid;
93     DIR* dp;
94     struct dirent* dirp;
95     int thisFd;
96     char** cmdv;
97     int i, cmdlen;
98 
99     /*
100      * Argument 0 of the command array is the program name.
101      * Here we just extract the program name and any arguments into
102      * a command array suitable for use with execvp.
103      */
104     cmdlen = (*env)->GetArrayLength(env, cmdarray);
105     if (cmdlen == 0) {
106         ThrowException(env, "java/lang/IllegalArgumentException",
107             "command array must at least include the program name");
108         return;
109     }
110     cmdv = (char **)malloc((cmdlen + 1) * sizeof(char *));
111     if (cmdv == NULL) {
112         ThrowException(env, "java/lang/OutOfMemoryError", NULL);
113         return;
114     }
115 
116     for (i=0; i<cmdlen; i++) {
117         jstring str = (*env)->GetObjectArrayElement(env, cmdarray, i);
118         cmdv[i] = (char *) getString8859_1Chars(env, str);
119         if (cmdv[i] == NULL) {
120             return;
121         }
122     }
123 
124     /*
125      * Command array must have NULL as the last entry
126      */
127     cmdv[cmdlen] = NULL;
128 
129     /*
130      * Launch the program. As this isn't a complete inetd or Runtime.exec
131      * implementation we don't have a reaper to pick up child exit status.
132      */
133     pid = fork();
134     if (pid != 0) {
135         if (pid < 0) {
136             ThrowException(env, "java/io/IOException", "fork failed");
137         }
138         return;
139     }
140 
141     /*
142      * We need to close all file descriptors except for serviceFd. To
143      * get the list of open file descriptors we read through /proc/self/fd (/dev/fd)
144      * but to open this requires a file descriptor. We could use a specific
145      * file descriptor and fdopendir but Linux doesn't seem to support
146      * fdopendir. Instead we use opendir and make an assumption on the
147      * file descriptor that is used (by opening & closing a file).
148      */
149     thisFd = open("/dev/fd", O_RDONLY);
150     if (thisFd < 0) {
151         _exit(-1);
152     }
153 
154     if ((dp = fdopendir(thisFd)) == NULL) {
155         _exit(-1);
156     }
157 
158     while ((dirp = readdir(dp)) != NULL) {
159         if (isdigit(dirp->d_name[0])) {
160             int fd = strtol(dirp->d_name, NULL, 10);
161             if (fd != serviceFd && fd != thisFd) {
162                 close(fd);
163             }
164         }
165     }
166     closedir(dp);
167 
168     /*
169      * At this point all file descriptors are closed except for
170      * serviceFd. We not dup 0,1,2 to this file descriptor and
171      * close serviceFd. This should leave us with only 0,1,2
172      * open and all connected to the same socket.
173      */
174     dup2(serviceFd, STDIN_FILENO);
175     dup2(serviceFd, STDOUT_FILENO);
176     dup2(serviceFd, STDERR_FILENO);
177     close(serviceFd);
178 
179     execvp(cmdv[0], cmdv);
180     _exit(-1);
181 }
182