Path: blob/master/src/java.base/unix/native/jspawnhelper/jspawnhelper.c
41119 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) {51if (write (fd, &err, sizeof(err)) != sizeof(err)) {52/* Not sure what to do here. I have no one to speak to. */53exit(0x80 + err);54}55exit (1);56}5758void shutItDown() {59fprintf(stdout, "This command is not for general use and should ");60fprintf(stdout, "only be run as the result of a call to\n");61fprintf(stdout, "ProcessBuilder.start() or Runtime.exec() in a java ");62fprintf(stdout, "application\n");63_exit(1);64}6566/*67* read the following off the pipefd68* - the ChildStuff struct69* - the SpawnInfo struct70* - the data strings for fields in ChildStuff71*/72void initChildStuff (int fdin, int fdout, ChildStuff *c) {73int n;74int argvBytes, nargv, envvBytes, nenvv;75int dirlen;76char *buf;77SpawnInfo sp;78int bufsize, offset=0;79int magic;80int res;8182res = readFully (fdin, &magic, sizeof(magic));83if (res != 4 || magic != magicNumber()) {84error (fdout, ERR_PIPE);85}8687if (readFully (fdin, c, sizeof(*c)) == -1) {88error (fdout, ERR_PIPE);89}9091if (readFully (fdin, &sp, sizeof(sp)) == -1) {92error (fdout, ERR_PIPE);93}9495bufsize = sp.argvBytes + sp.envvBytes +96sp.dirlen + sp.parentPathvBytes;9798ALLOC(buf, bufsize);99100if (readFully (fdin, buf, bufsize) == -1) {101error (fdout, ERR_PIPE);102}103104/* Initialize argv[] */105ALLOC(c->argv, sizeof(char *) * sp.nargv);106initVectorFromBlock (c->argv, buf+offset, sp.nargv-1);107offset += sp.argvBytes;108109/* Initialize envv[] */110if (sp.nenvv == 0) {111c->envv = 0;112} else {113ALLOC(c->envv, sizeof(char *) * sp.nenvv);114initVectorFromBlock (c->envv, buf+offset, sp.nenvv-1);115offset += sp.envvBytes;116}117118/* Initialize pdir */119if (sp.dirlen == 0) {120c->pdir = 0;121} else {122c->pdir = buf+offset;123offset += sp.dirlen;124}125126/* Initialize parentPathv[] */127ALLOC(parentPathv, sizeof (char *) * sp.nparentPathv)128initVectorFromBlock ((const char**)parentPathv, buf+offset, sp.nparentPathv-1);129offset += sp.parentPathvBytes;130}131132int main(int argc, char *argv[]) {133ChildStuff c;134int t;135struct stat buf;136/* argv[0] contains the fd number to read all the child info */137int r, fdin, fdout;138139r = sscanf (argv[argc-1], "%d:%d", &fdin, &fdout);140if (r == 2 && fcntl(fdin, F_GETFD) != -1) {141fstat(fdin, &buf);142if (!S_ISFIFO(buf.st_mode))143shutItDown();144} else {145shutItDown();146}147initChildStuff (fdin, fdout, &c);148149childProcess (&c);150return 0; /* NOT REACHED */151}152153154