--- /dev/null Sat Dec 22 03:00:31 2012 +++ new/src/solaris/native/java/lang/jspawnhelper.c Sat Dec 22 03:00:29 2012 @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "childproc.h" + +#define ERR_MALLOC 1 +#define ERR_PIPE 2 +#define ERR_ARGS 3 + +void error (int fd, int err) { + write (fd, &err, sizeof(err)); + exit (1); +} + +static void* +xmalloc(size_t size, int fdout) +{ + void *p = malloc(size); + if (p == NULL) + error (fdout, ERR_MALLOC); + return p; +} + +#define NEW(type, n, fdout) ((type *) xmalloc((n) * sizeof(type), fdout)) + +void usageError() { + fprintf(stdout, "This command is not for general use and should "); + fprintf(stdout, "only be run as the result of a call to\n"); + fprintf(stdout, "ProcessBuilder.start() or Runtime.exec() in a java "); + fprintf(stdout, "application\n"); + _exit(1); +} + +/* + * read the following off the pipefd + * - the ChildStuff struct + * - the SpawnInfo struct + * - the data strings for fields in ChildStuff + */ +void initChildStuff (int fdin, int fdout, ChildStuff *c) { + int n; + int argvBytes, nargv, envvBytes, nenvv; + int dirlen; + char *buf; + SpawnInfo sp; + int bufsize, offset=0; + int magic; + int res; + + res = readFully (fdin, &magic, sizeof(magic)); + if (res != sizeof(magic) || magic != magicNumber()) { + error (fdout, ERR_PIPE); + } + + if (readFully (fdin, c, sizeof(*c)) == -1) { + error (fdout, ERR_PIPE); + } + + if (readFully (fdin, &sp, sizeof(sp)) == -1) { + error (fdout, ERR_PIPE); + } + + bufsize = sp.argvBytes + sp.envvBytes + sp.dirlen + + sp.parentPathLen + sp.parentPathvBytes; + + buf = NEW(char*, bufsize, fdout); + + if (readFully (fdin, buf, bufsize) == -1) { + error (fdout, ERR_PIPE); + } + + /* Initialize argv[] */ + c->argv = NEW(const char**, sizeof(char *) * sp.nargv, fdout); + initVectorFromBlock (c->argv, buf+offset, sp.nargv-1); + offset += sp.argvBytes; + + /* Initialize envv[] */ + if (sp.nenvv == 0) { + c->envv = 0; + } else { + c->envv = NEW(const char**, sizeof(char *) * sp.nenvv, fdout); + initVectorFromBlock (c->envv, buf+offset, sp.nenvv-1); + offset += sp.envvBytes; + } + + /* Initialize pdir */ + if (sp.dirlen == 0) { + c->pdir = 0; + } else { + c->pdir = buf+offset; + offset += sp.dirlen; + } + + /* Initialize parentPath */ + parentPath = buf+offset; + offset += sp.parentPathLen; + + /* Initialize parentPathv[] */ + parentPathv = + NEW(const char * const*, sizeof (char *) * sp.nparentPathv, fdout); + initVectorFromBlock (parentPathv, buf+offset, sp.nparentPathv-1); + offset += sp.parentPathvBytes; +} + +int main(int argc, char *argv[]) { + ChildStuff c; + int t; + struct stat buf; + /* argv[0] contains the fd number to read all the child info */ + int r, fdin, fdout; + + if (argc == 1) { + r = sscanf (argv[argc-1], "%d:%d", &fdin, &fdout); + if (r == 2 && fcntl(fdin, F_GETFD) != -1) { + fstat(fdin, &buf); + if (!S_ISFIFO(buf.st_mode)) + usageError(); + } else { + usageError(); + } + } else { + usageError(); + } + initChildStuff (fdin, fdout, &c); + + childProcess (&c); + return 0; /* NOT REACHED */ +}