1 /*
2 * Copyright (c) 1995, 2012, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 #define _LARGEFILE64_SOURCE 1
28
29 #include "jni.h"
30 #include "jvm.h"
31 #include "jvm_md.h"
32 #include "jni_util.h"
33 #include "io_util.h"
34
35 /*
36 * Platform-specific support for java.lang.Process
37 */
38 #include <assert.h>
39 #include <stddef.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <ctype.h>
43 #include <sys/wait.h>
44 #include <signal.h>
45 #include <string.h>
46
47 #if defined(__solaris__) || defined(_ALLBSD_SOURCE)
48 #include <spawn.h>
49 #endif
50
51 #include "childproc.h"
52
53 /*
54 * There are 4 possible strategies we might use to "fork":
55 *
56 * - fork(2). Very portable and reliable but subject to
57 * failure due to overcommit (see the documentation on
58 * /proc/sys/vm/overcommit_memory in Linux proc(5)).
59 * This is the ancient problem of spurious failure whenever a large
60 * process starts a small subprocess.
61 *
62 * - vfork(). Using this is scary because all relevant man pages
63 * contain dire warnings, e.g. Linux vfork(2). But at least it's
64 * documented in the glibc docs and is standardized by XPG4.
65 * http://www.opengroup.org/onlinepubs/000095399/functions/vfork.html
66 * On Linux, one might think that vfork() would be implemented using
67 * the clone system call with flag CLONE_VFORK, but in fact vfork is
438
439 static pid_t
440 forkChild(ChildStuff *c) {
441 pid_t resultPid;
442
443 /*
444 * From Solaris fork(2): In Solaris 10, a call to fork() is
445 * identical to a call to fork1(); only the calling thread is
446 * replicated in the child process. This is the POSIX-specified
447 * behavior for fork().
448 */
449 resultPid = fork();
450
451 if (resultPid == 0) {
452 childProcess(c);
453 }
454 assert(resultPid != 0); /* childProcess never returns */
455 return resultPid;
456 }
457
458 #if defined(__solaris__) || defined(_ALLBSD_SOURCE)
459 static pid_t
460 spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) {
461 pid_t resultPid;
462 jboolean isCopy;
463 int i, offset, rval, bufsize, magic;
464 char *buf, buf1[16];
465 char *hlpargs[2];
466 SpawnInfo sp;
467
468 /* need to tell helper which fd is for receiving the childstuff
469 * and which fd to send response back on
470 */
471 snprintf(buf1, sizeof(buf1), "%d:%d", c->childenv[0], c->fail[1]);
472 /* put the fd string as argument to the helper cmd */
473 hlpargs[0] = buf1;
474 hlpargs[1] = 0;
475
476 /* Following items are sent down the pipe to the helper
477 * after it is spawned.
478 * All strings are null terminated. All arrays of strings
534 free(buf);
535
536 /* In this mode an external main() in invoked which calls back into
537 * childProcess() in this file, rather than directly
538 * via the statement below */
539 return resultPid;
540 }
541 #endif
542
543 /*
544 * Start a child process running function childProcess.
545 * This function only returns in the parent.
546 */
547 static pid_t
548 startChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) {
549 switch (c->mode) {
550 case MODE_VFORK:
551 return vforkChild(c);
552 case MODE_FORK:
553 return forkChild(c);
554 #if defined(__solaris__) || defined(_ALLBSD_SOURCE)
555 case MODE_POSIX_SPAWN:
556 return spawnChild(env, process, c, helperpath);
557 #endif
558 default:
559 return -1;
560 }
561 }
562
563 JNIEXPORT jint JNICALL
564 Java_java_lang_UNIXProcess_forkAndExec(JNIEnv *env,
565 jobject process,
566 jint mode,
567 jbyteArray helperpath,
568 jbyteArray prog,
569 jbyteArray argBlock, jint argc,
570 jbyteArray envBlock, jint envc,
571 jbyteArray dir,
572 jintArray std_fds,
573 jboolean redirectErrorStream)
574 {
|
1 /*
2 * Copyright (c) 1995, 2013, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 #define _LARGEFILE64_SOURCE 1
28
29 #include "jni.h"
30 #include "jvm.h"
31 #include "jvm_md.h"
32 #include "jni_util.h"
33 #include "io_util.h"
34
35 /*
36 * Platform-specific support for java.lang.Process
37 */
38 #include <assert.h>
39 #include <stddef.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <ctype.h>
43 #include <sys/wait.h>
44 #include <signal.h>
45 #include <string.h>
46
47 #if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
48 #include <spawn.h>
49 #endif
50
51 #include "childproc.h"
52
53 /*
54 * There are 4 possible strategies we might use to "fork":
55 *
56 * - fork(2). Very portable and reliable but subject to
57 * failure due to overcommit (see the documentation on
58 * /proc/sys/vm/overcommit_memory in Linux proc(5)).
59 * This is the ancient problem of spurious failure whenever a large
60 * process starts a small subprocess.
61 *
62 * - vfork(). Using this is scary because all relevant man pages
63 * contain dire warnings, e.g. Linux vfork(2). But at least it's
64 * documented in the glibc docs and is standardized by XPG4.
65 * http://www.opengroup.org/onlinepubs/000095399/functions/vfork.html
66 * On Linux, one might think that vfork() would be implemented using
67 * the clone system call with flag CLONE_VFORK, but in fact vfork is
438
439 static pid_t
440 forkChild(ChildStuff *c) {
441 pid_t resultPid;
442
443 /*
444 * From Solaris fork(2): In Solaris 10, a call to fork() is
445 * identical to a call to fork1(); only the calling thread is
446 * replicated in the child process. This is the POSIX-specified
447 * behavior for fork().
448 */
449 resultPid = fork();
450
451 if (resultPid == 0) {
452 childProcess(c);
453 }
454 assert(resultPid != 0); /* childProcess never returns */
455 return resultPid;
456 }
457
458 #if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
459 static pid_t
460 spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) {
461 pid_t resultPid;
462 jboolean isCopy;
463 int i, offset, rval, bufsize, magic;
464 char *buf, buf1[16];
465 char *hlpargs[2];
466 SpawnInfo sp;
467
468 /* need to tell helper which fd is for receiving the childstuff
469 * and which fd to send response back on
470 */
471 snprintf(buf1, sizeof(buf1), "%d:%d", c->childenv[0], c->fail[1]);
472 /* put the fd string as argument to the helper cmd */
473 hlpargs[0] = buf1;
474 hlpargs[1] = 0;
475
476 /* Following items are sent down the pipe to the helper
477 * after it is spawned.
478 * All strings are null terminated. All arrays of strings
534 free(buf);
535
536 /* In this mode an external main() in invoked which calls back into
537 * childProcess() in this file, rather than directly
538 * via the statement below */
539 return resultPid;
540 }
541 #endif
542
543 /*
544 * Start a child process running function childProcess.
545 * This function only returns in the parent.
546 */
547 static pid_t
548 startChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) {
549 switch (c->mode) {
550 case MODE_VFORK:
551 return vforkChild(c);
552 case MODE_FORK:
553 return forkChild(c);
554 #if defined(__solaris__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
555 case MODE_POSIX_SPAWN:
556 return spawnChild(env, process, c, helperpath);
557 #endif
558 default:
559 return -1;
560 }
561 }
562
563 JNIEXPORT jint JNICALL
564 Java_java_lang_UNIXProcess_forkAndExec(JNIEnv *env,
565 jobject process,
566 jint mode,
567 jbyteArray helperpath,
568 jbyteArray prog,
569 jbyteArray argBlock, jint argc,
570 jbyteArray envBlock, jint envc,
571 jbyteArray dir,
572 jintArray std_fds,
573 jboolean redirectErrorStream)
574 {
|