Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/debugtools/DDR_VM/zos_address_space_packer/multi_as.c
5991 views
1
/*******************************************************************************
2
* Copyright (c) 2009, 2014 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
24
#define _XOPEN_SOURCE_EXTENDED 1
25
#include <unistd.h>
26
#include <spawn.h>
27
#include <stdio.h>
28
#include <errno.h>
29
#include <sys/types.h>
30
#include <assert.h>
31
#include <string.h>
32
33
/*
34
* Test prog to pack multiple VMs into an address space
35
*
36
* This involves messing with environment variables that affect the spawn() call
37
* and can't easily be done from the USS command line (it's easy enough to put Java
38
* into the address space of the shell, seemingly impossible to put two Javas into
39
* the address space of the shell).
40
*/
41
42
extern char **environ;
43
44
struct ToSpawn
45
{
46
char * line;
47
char * execname;
48
char ** argv;
49
pid_t pid;
50
struct ToSpawn * next;
51
};
52
53
#define MAX_BUFFER 1024
54
#define MAX_ARGS 100
55
56
static struct ToSpawn * parseInputFile(FILE * inputFile)
57
{
58
char buffer[MAX_BUFFER];
59
struct ToSpawn * head = NULL;
60
struct ToSpawn * tail = NULL;
61
62
while (NULL != fgets(buffer,MAX_BUFFER,inputFile)) {
63
struct ToSpawn * allocated;
64
char * p;
65
char * localArgv[MAX_ARGS];
66
unsigned int i;
67
unsigned int argContext = 0;
68
69
if (buffer[0] == '\0') {
70
continue;
71
}
72
73
/* Strip out any funny characters */
74
p = buffer;
75
76
while(*p) {
77
if (*p == '\n' || *p == '\r') {
78
*p = ' ';
79
}
80
p++;
81
}
82
83
allocated = (struct toSpawn *)malloc(sizeof (struct ToSpawn));
84
assert(allocated != NULL);
85
86
memset(allocated, 0x00, sizeof(struct ToSpawn));
87
88
allocated->line = strdup(buffer);
89
assert(allocated->line != NULL);
90
91
p = strtok(buffer," ");
92
93
allocated->execname = strdup(p);
94
assert(allocated->execname != NULL);
95
96
while ( (p = strtok(NULL," ")) != NULL) {
97
localArgv[argContext] = strdup(p);
98
assert(localArgv[argContext] != NULL);
99
argContext++;
100
}
101
102
allocated->argv = (char **) malloc((argContext + 2) * sizeof(char**));
103
assert(allocated->argv != NULL);
104
105
allocated->argv[0] = allocated->execname;
106
for (i=0;i!=argContext;i++) {
107
allocated->argv[i + 1] = localArgv[i];
108
}
109
allocated->argv[argContext + 1] = NULL;
110
111
if (NULL == head) {
112
head = allocated;
113
tail = head;
114
} else {
115
tail->next = allocated;
116
tail = allocated;
117
}
118
}
119
120
return head;
121
}
122
123
static void spawnAndWait (struct ToSpawn * spawnList){
124
struct ToSpawn * current = spawnList;
125
126
while(current != NULL) {
127
#define SP_FD_NUM 3
128
#define MAX_ARGS 100
129
int sp_fd_cnt;
130
int sp_fd_map[SP_FD_NUM];
131
struct inheritance sp_inherit;
132
pid_t resultPid;
133
134
135
/* Map file descriptors for child. This enables the child to use */
136
/* the literal values (0, 1, 2) as file descriptors. */
137
sp_fd_cnt = SP_FD_NUM;
138
sp_fd_map[0] = 0;
139
sp_fd_map[1] = 1;
140
sp_fd_map[2] = 2;
141
142
memset(&sp_inherit, 0x00, sizeof(sp_inherit));
143
144
145
fprintf(stderr,"Spawning %s\n",current->line);
146
147
fprintf(stderr,"execname is %s\n",current->execname);
148
149
150
{
151
int i=0;
152
for(i=0;current->argv[i] != NULL;i++) {
153
fprintf(stderr,"arg[%d] = %p, %s, %d\n",i,current->argv[i],current->argv[i],strlen(current->argv[i]));
154
}
155
}
156
157
158
/* Spawn child process */
159
{
160
resultPid = spawn(current->execname,
161
sp_fd_cnt, sp_fd_map, &sp_inherit, current->argv, environ);
162
}
163
if (resultPid < 0) {
164
perror("spawn()");
165
exit(1);
166
}
167
168
fprintf(stderr,"Started PID was %d\n",resultPid);
169
current->pid = resultPid;
170
171
current = current->next;
172
}
173
174
current = spawnList;
175
176
while(current != NULL) {
177
int childExitCode;
178
pid_t waitResult;
179
180
waitResult = waitpid(current->pid,&childExitCode,0);
181
182
if (waitResult != current->pid) {
183
perror("waitpid()");
184
}
185
current = current->next;
186
}
187
}
188
189
/*
190
* One argument expected - list of commands to run
191
*/
192
int main(int argc, char * argv[])
193
{
194
FILE * inputFile;
195
struct ToSpawn * spawnChain = NULL;
196
197
if (argc != 2) {
198
fprintf(stderr,"Expected 1 argument, got %d\n",argc-1);
199
return 1;
200
}
201
202
inputFile = fopen(argv[1],"r");
203
204
if (NULL == inputFile) {
205
perror("Opening input file");
206
return 2;
207
}
208
209
/* Set the environment variable to push spawns into the same AS*/
210
putenv("_BPX_SHAREAS=MUST");
211
212
spawnChain = parseInputFile(inputFile);
213
214
fclose(inputFile);
215
216
spawnAndWait(spawnChain);
217
218
/*Deliberately end without freeing up*/
219
}
220
221