1/*
2 * Copyright (c) 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
23 * questions.
24 */
25
26#include <errno.h>
27#include <fcntl.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33
34#include "childproc.h"
35
36extern int errno;
37
38#define ALLOC(X,Y) { \
39 void *mptr; \
40 mptr = malloc (Y); \
41 if (mptr == 0) { \
42 error (fdout, ERR_MALLOC); \
43 } \
44 X = mptr; \
45}
46
47#define ERR_MALLOC 1
48#define ERR_PIPE 2
49#define ERR_ARGS 3
50
51void error (int fd, int err) {
52 if (write (fd, &err, sizeof(err)) != sizeof(err)) {
53 /* Not sure what to do here. I have no one to speak to. */
54 exit(0x80 + err);
55 }
56 exit (1);
57}
58
59void shutItDown() {
60 fprintf(stdout, "This command is not for general use and should ");
61 fprintf(stdout, "only be run as the result of a call to\n");
62 fprintf(stdout, "ProcessBuilder.start() or Runtime.exec() in a java ");
63 fprintf(stdout, "application\n");
64 _exit(1);
65}
66
67/*
68 * read the following off the pipefd
69 * - the ChildStuff struct
70 * - the SpawnInfo struct
71 * - the data strings for fields in ChildStuff
72 */
73void initChildStuff (int fdin, int fdout, ChildStuff *c) {
74 int n;
75 int argvBytes, nargv, envvBytes, nenvv;
76 int dirlen;
77 char *buf;
78 SpawnInfo sp;
79 int bufsize, offset=0;
80 int magic;
81 int res;
82
83 res = readFully (fdin, &magic, sizeof(magic));
84 if (res != 4 || magic != magicNumber()) {
85 error (fdout, ERR_PIPE);
86 }
87
88 if (readFully (fdin, c, sizeof(*c)) == -1) {
89 error (fdout, ERR_PIPE);
90 }
91
92 if (readFully (fdin, &sp, sizeof(sp)) == -1) {
93 error (fdout, ERR_PIPE);
94 }
95
96 bufsize = sp.argvBytes + sp.envvBytes +
97 sp.dirlen + sp.parentPathvBytes;
98
99 ALLOC(buf, bufsize);
100
101 if (readFully (fdin, buf, bufsize) == -1) {
102 error (fdout, ERR_PIPE);
103 }
104
105 /* Initialize argv[] */
106 ALLOC(c->argv, sizeof(char *) * sp.nargv);
107 initVectorFromBlock (c->argv, buf+offset, sp.nargv-1);
108 offset += sp.argvBytes;
109
110 /* Initialize envv[] */
111 if (sp.nenvv == 0) {
112 c->envv = 0;
113 } else {
114 ALLOC(c->envv, sizeof(char *) * sp.nenvv);
115 initVectorFromBlock (c->envv, buf+offset, sp.nenvv-1);
116 offset += sp.envvBytes;
117 }
118
119 /* Initialize pdir */
120 if (sp.dirlen == 0) {
121 c->pdir = 0;
122 } else {
123 c->pdir = buf+offset;
124 offset += sp.dirlen;
125 }
126
127 /* Initialize parentPathv[] */
128 ALLOC(parentPathv, sizeof (char *) * sp.nparentPathv)
129 initVectorFromBlock ((const char**)parentPathv, buf+offset, sp.nparentPathv-1);
130 offset += sp.parentPathvBytes;
131}
132
133int main(int argc, char *argv[]) {
134 ChildStuff c;
135 int t;
136 struct stat buf;
137 /* argv[0] contains the fd number to read all the child info */
138 int r, fdin, fdout;
139
140 r = sscanf (argv[argc-1], "%d:%d", &fdin, &fdout);
141 if (r == 2 && fcntl(fdin, F_GETFD) != -1) {
142 fstat(fdin, &buf);
143 if (!S_ISFIFO(buf.st_mode))
144 shutItDown();
145 } else {
146 shutItDown();
147 }
148 initChildStuff (fdin, fdout, &c);
149
150 childProcess (&c);
151 return 0; /* NOT REACHED */
152}
153