Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/java/lang/jspawnhelper.c
32287 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <errno.h>26#include <fcntl.h>27#include <stdio.h>28#include <stdlib.h>29#include <unistd.h>30#include <sys/types.h>31#include <sys/stat.h>3233#include "childproc.h"3435extern int errno;3637#define ALLOC(X,Y) { \38void *mptr; \39mptr = malloc (Y); \40if (mptr == 0) { \41error (fdout, ERR_MALLOC); \42} \43X = mptr; \44}4546#define ERR_MALLOC 147#define ERR_PIPE 248#define ERR_ARGS 34950void error (int fd, int err) {51write (fd, &err, sizeof(err));52exit (1);53}5455void shutItDown() {56fprintf(stdout, "This command is not for general use and should ");57fprintf(stdout, "only be run as the result of a call to\n");58fprintf(stdout, "ProcessBuilder.start() or Runtime.exec() in a java ");59fprintf(stdout, "application\n");60_exit(1);61}6263/*64* read the following off the pipefd65* - the ChildStuff struct66* - the SpawnInfo struct67* - the data strings for fields in ChildStuff68*/69void initChildStuff (int fdin, int fdout, ChildStuff *c) {70int n;71int argvBytes, nargv, envvBytes, nenvv;72int dirlen;73char *buf;74SpawnInfo sp;75int bufsize, offset=0;76int magic;77int res;7879res = readFully (fdin, &magic, sizeof(magic));80if (res != 4 || magic != magicNumber()) {81error (fdout, ERR_PIPE);82}8384if (readFully (fdin, c, sizeof(*c)) == -1) {85error (fdout, ERR_PIPE);86}8788if (readFully (fdin, &sp, sizeof(sp)) == -1) {89error (fdout, ERR_PIPE);90}9192bufsize = sp.argvBytes + sp.envvBytes +93sp.dirlen + sp.parentPathvBytes;9495ALLOC(buf, bufsize);9697if (readFully (fdin, buf, bufsize) == -1) {98error (fdout, ERR_PIPE);99}100101/* Initialize argv[] */102ALLOC(c->argv, sizeof(char *) * sp.nargv);103initVectorFromBlock (c->argv, buf+offset, sp.nargv-1);104offset += sp.argvBytes;105106/* Initialize envv[] */107if (sp.nenvv == 0) {108c->envv = 0;109} else {110ALLOC(c->envv, sizeof(char *) * sp.nenvv);111initVectorFromBlock (c->envv, buf+offset, sp.nenvv-1);112offset += sp.envvBytes;113}114115/* Initialize pdir */116if (sp.dirlen == 0) {117c->pdir = 0;118} else {119c->pdir = buf+offset;120offset += sp.dirlen;121}122123/* Initialize parentPathv[] */124ALLOC(parentPathv, sizeof (char *) * sp.nparentPathv)125initVectorFromBlock ((const char**)parentPathv, buf+offset, sp.nparentPathv-1);126offset += sp.parentPathvBytes;127}128129int main(int argc, char *argv[]) {130ChildStuff c;131int t;132struct stat buf;133/* argv[0] contains the fd number to read all the child info */134int r, fdin, fdout;135136r = sscanf (argv[argc-1], "%d:%d", &fdin, &fdout);137if (r == 2 && fcntl(fdin, F_GETFD) != -1) {138fstat(fdin, &buf);139if (!S_ISFIFO(buf.st_mode))140shutItDown();141} else {142shutItDown();143}144initChildStuff (fdin, fdout, &c);145146childProcess (&c);147return 0; /* NOT REACHED */148}149150151