xref: /haiku/src/system/libroot/os/image.cpp (revision 15fb7d88e971c4d6c787c6a3a5c159afb1ebf77b)
1 /*
2  * Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <image.h>
7 #include <image_private.h>
8 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <algorithm>
13 #include <new>
14 
15 #include <fs_attr.h>
16 
17 #include <AutoDeleter.h>
18 
19 #include <libroot_private.h>
20 #include <runtime_loader.h>
21 #include <syscalls.h>
22 #include <syscall_load_image.h>
23 #include <user_runtime.h>
24 
25 
26 struct EnvironmentFilter {
27 	EnvironmentFilter()
28 		:
29 		fBuffer(NULL),
30 		fEntries(NULL),
31 		fBufferSize(0),
32 		fEntryCount(0),
33 		fAdditionalEnvCount(0),
34 		fNextEntryIndex(0)
35 	{
36 	}
37 
38 	~EnvironmentFilter()
39 	{
40 		free(fBuffer);
41 		delete[] fEntries;
42 	}
43 
44 	void Init(const char* path, const char* const* env, size_t envCount)
45 	{
46 		int fd = open(path, O_RDONLY);
47 		if (fd < 0)
48 			return;
49 		FileDescriptorCloser fdCloser(fd);
50 
51 		static const char* const kEnvAttribute = "SYS:ENV";
52 		attr_info info;
53 		if (fs_stat_attr(fd, kEnvAttribute, &info) < 0)
54 			return;
55 
56 		_Init(fd, kEnvAttribute, info.size, env, envCount);
57 	}
58 
59 	size_t AdditionalSlotsNeeded() const
60 	{
61 		return fAdditionalEnvCount;
62 	}
63 
64 	size_t AdditionalSizeNeeded() const
65 	{
66 		return fBufferSize + fAdditionalEnvCount * sizeof(char*);
67 	}
68 
69 	size_t PrepareSlot(const char* env, int32 index, char* buffer)
70 	{
71 		if (fNextEntryIndex < fEntryCount
72 			&& fEntries[fNextEntryIndex].index == index) {
73 			env = fEntries[fNextEntryIndex].replacement;
74 			fNextEntryIndex++;
75 		}
76 
77 		return _FillSlot(env, buffer);
78 	}
79 
80 	void PrepareAdditionalSlots(char**& slot, char*& buffer)
81 	{
82 		for (size_t i = 0; i < fAdditionalEnvCount; i++) {
83 			size_t envSize = _FillSlot(fEntries[i].replacement, buffer);
84 			*slot++ = buffer;
85 			buffer += envSize;
86 		}
87 	}
88 
89 private:
90 	void _Init(int fd, const char* attribute, size_t size,
91 		const char* const* env, size_t envCount)
92 	{
93 		if (size == 0)
94 			return;
95 
96 		// read the attribute
97 		char* buffer = (char*)malloc(size + 1);
98 		if (buffer == NULL)
99 			return;
100 		MemoryDeleter bufferDeleter(buffer);
101 
102 		ssize_t bytesRead = fs_read_attr(fd, attribute, B_STRING_TYPE, 0,
103 			buffer, size);
104 		if (bytesRead < 0 || (size_t)bytesRead != size)
105 			return;
106 		buffer[size] = '\0';
107 
108 		// deescape the buffer and count the entries
109 		size_t entryCount = 1;
110 		char* out = buffer;
111 		for (const char* c = buffer; *c != '\0'; c++) {
112 			if (*c == '\\') {
113 				c++;
114 				if (*c == '\0')
115 					break;
116 				if (*c == '0') {
117 					*out++ = '\0';
118 					entryCount++;
119 				} else
120 					*out++ = *c;
121 			} else
122 				*out++ = *c;
123 		}
124 		*out++ = '\0';
125 		size = out - buffer + 1;
126 
127 		// create an entry array
128 		fEntries = new(std::nothrow) Entry[entryCount];
129 		if (fEntries == NULL)
130 			return;
131 
132 		bufferDeleter.Detach();
133 		fBuffer = buffer;
134 		fBufferSize = size;
135 
136 		// init the entries
137 		out = buffer;
138 		for (size_t i = 0; i < entryCount; i++) {
139 			const char* separator = strchr(out, '=');
140 			if (separator != NULL && separator != out) {
141 				fEntries[fEntryCount].replacement = out;
142 				fEntries[fEntryCount].index = _FindEnvEntry(env, envCount, out,
143 					separator - out);
144 				if (fEntries[fEntryCount].index < 0)
145 					fAdditionalEnvCount++;
146 				fEntryCount++;
147 			}
148 			out += strlen(out) + 1;
149 		}
150 
151 		if (fEntryCount > 1)
152 			std::sort(fEntries, fEntries + fEntryCount);
153 
154 		// Advance fNextEntryIndex to the first entry pointing to an existing
155 		// env variable.
156 		while (fNextEntryIndex < fEntryCount
157 			&& fEntries[fNextEntryIndex].index < 0) {
158 			fNextEntryIndex++;
159 		}
160 	}
161 
162 	int32 _FindEnvEntry(const char* const* env, size_t envCount,
163 		const char* variable, size_t variableLength)
164 	{
165 		for (size_t i = 0; i < envCount; i++) {
166 			if (strncmp(env[i], variable, variableLength) == 0
167 				&& env[i][variableLength] == '=') {
168 				return i;
169 			}
170 		}
171 
172 		return -1;
173 	}
174 
175 	size_t _FillSlot(const char* env, char* buffer)
176 	{
177 		size_t envSize = strlen(env) + 1;
178 		memcpy(buffer, env, envSize);
179 		return envSize;
180 	}
181 
182 private:
183 	struct Entry {
184 		char*	replacement;
185 		int32	index;
186 
187 		bool operator<(const Entry& other) const
188 		{
189 			return index < other.index;
190 		}
191 	};
192 
193 private:
194 	char*	fBuffer;
195 	Entry*	fEntries;
196 	size_t	fBufferSize;
197 	size_t	fEntryCount;
198 	size_t	fAdditionalEnvCount;
199 	size_t	fNextEntryIndex;
200 };
201 
202 
203 thread_id
204 load_image(int32 argCount, const char **args, const char **environ)
205 {
206 	char invoker[B_FILE_NAME_LENGTH];
207 	char **newArgs = NULL;
208 	int32 envCount = 0;
209 	thread_id thread;
210 
211 	if (argCount < 1 || environ == NULL)
212 		return B_BAD_VALUE;
213 
214 	// test validity of executable + support for scripts
215 	{
216 		status_t status = __test_executable(args[0], invoker);
217 		if (status < B_OK)
218 			return status;
219 
220 		if (invoker[0]) {
221 			status = __parse_invoke_line(invoker, &newArgs,
222 				(char * const **)&args, &argCount, args[0]);
223 			if (status < B_OK)
224 				return status;
225 		}
226 	}
227 
228 	// count environment variables
229 	while (environ[envCount] != NULL)
230 		envCount++;
231 
232 	char** flatArgs = NULL;
233 	size_t flatArgsSize;
234 	status_t status = __flatten_process_args(args, argCount, environ,
235 		&envCount, args[0], &flatArgs, &flatArgsSize);
236 
237 	if (status == B_OK) {
238 		thread = _kern_load_image(flatArgs, flatArgsSize, argCount, envCount,
239 			B_NORMAL_PRIORITY, B_WAIT_TILL_LOADED, -1, 0);
240 
241 		free(flatArgs);
242 	} else
243 		thread = status;
244 
245 	free(newArgs);
246 	return thread;
247 }
248 
249 
250 image_id
251 load_add_on(char const *name)
252 {
253 	if (name == NULL)
254 		return B_BAD_VALUE;
255 
256 	return __gRuntimeLoader->load_add_on(name, 0);
257 }
258 
259 
260 status_t
261 unload_add_on(image_id id)
262 {
263 	return __gRuntimeLoader->unload_add_on(id);
264 }
265 
266 
267 status_t
268 get_image_symbol(image_id id, char const *symbolName, int32 symbolType,
269 	void **_location)
270 {
271 	return __gRuntimeLoader->get_image_symbol(id, symbolName, symbolType,
272 		false, NULL, _location);
273 }
274 
275 
276 status_t
277 get_image_symbol_etc(image_id id, char const *symbolName, int32 symbolType,
278 	bool recursive, image_id *_inImage, void **_location)
279 {
280 	return __gRuntimeLoader->get_image_symbol(id, symbolName, symbolType,
281 		recursive, _inImage, _location);
282 }
283 
284 
285 status_t
286 get_nth_image_symbol(image_id id, int32 num, char *nameBuffer, int32 *_nameLength,
287 	int32 *_symbolType, void **_location)
288 {
289 	return __gRuntimeLoader->get_nth_image_symbol(id, num, nameBuffer, _nameLength, _symbolType, _location);
290 }
291 
292 
293 status_t
294 _get_image_info(image_id id, image_info *info, size_t infoSize)
295 {
296 	return _kern_get_image_info(id, info, infoSize);
297 }
298 
299 
300 status_t
301 _get_next_image_info(team_id team, int32 *cookie, image_info *info, size_t infoSize)
302 {
303 	return _kern_get_next_image_info(team, cookie, info, infoSize);
304 }
305 
306 
307 void
308 clear_caches(void *address, size_t length, uint32 flags)
309 {
310 	_kern_clear_caches(address, length, flags);
311 }
312 
313 
314 //	#pragma mark -
315 
316 
317 status_t
318 __look_up_in_path(const char *file, char *buffer)
319 {
320 	// get the PATH
321 	const char* paths = getenv("PATH");
322 	if (paths == NULL)
323 		return B_ENTRY_NOT_FOUND;
324 
325 	int fileNameLen = strlen(file);
326 
327 	// iterate through the paths
328 	const char* pathEnd = paths - 1;
329 	while (pathEnd != NULL) {
330 		paths = pathEnd + 1;
331 		pathEnd = strchr(paths, ':');
332 		int pathLen = (pathEnd ? pathEnd - paths : strlen(paths));
333 
334 		// We skip empty paths and those that would become too long.
335 		// The latter is not really correct, but practically irrelevant.
336 		if (pathLen == 0
337 			|| pathLen + 1 + fileNameLen >= B_PATH_NAME_LENGTH) {
338 			continue;
339 		}
340 
341 		// concatinate the program path
342 		char path[B_PATH_NAME_LENGTH];
343 		memcpy(path, paths, pathLen);
344 		path[pathLen] = '\0';
345 
346 		if (path[pathLen - 1] != '/')
347 			strcat(path, "/");
348 		strcat(path, file);
349 
350 		// check whether it is a file
351 		struct stat st;
352 		if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
353 			continue;
354 
355 		// if executable, we've found what we are looking for
356 		if (access(path, X_OK) == 0) {
357 			strlcpy(buffer, path, B_PATH_NAME_LENGTH);
358 			return B_OK;
359 		}
360 	}
361 
362 	return B_ENTRY_NOT_FOUND;
363 }
364 
365 
366 static char *
367 next_argument(char **_start, bool separate)
368 {
369 	char *line = *_start;
370 	char quote = 0;
371 	int32 i;
372 
373 	// eliminate leading spaces
374 	while (line[0] == ' ')
375 		line++;
376 
377 	if (line[0] == '"' || line[0] == '\'') {
378 		quote = line[0];
379 		line++;
380 	}
381 
382 	if (!line[0])
383 		return NULL;
384 
385 	for (i = 0;; i++) {
386 		if (line[i] == '\\' && line[i + 1] != '\0')
387 			continue;
388 
389 		if (line[i] == '\0') {
390 			*_start = &line[i];
391 			return line;
392 		}
393 		if ((!quote && line[i] == ' ') || line[i] == quote) {
394 			// argument separator!
395 			if (separate)
396 				line[i] = '\0';
397 			*_start = &line[i + 1];
398 			return line;
399 		}
400 	}
401 
402 	return NULL;
403 }
404 
405 
406 status_t
407 __parse_invoke_line(char *invoker, char ***_newArgs,
408 	char * const **_oldArgs, int32 *_argCount, const char *arg0)
409 {
410 	int32 i, count = 0;
411 	char *arg = invoker;
412 	char **newArgs;
413 
414 	// count arguments in the line
415 
416 	while (next_argument(&arg, false)) {
417 		count++;
418 	}
419 
420 	// this is a shell script and requires special treatment
421 	newArgs = (char**)malloc((*_argCount + count + 1) * sizeof(void *));
422 	if (newArgs == NULL)
423 		return B_NO_MEMORY;
424 
425 	// copy invoker and old arguments and to newArgs
426 
427 	for (i = 0; (arg = next_argument(&invoker, true)) != NULL; i++) {
428 		newArgs[i] = arg;
429 	}
430 	for (i = 0; i < *_argCount; i++) {
431 		if (i == 0)
432 			newArgs[i + count] = (char*)arg0;
433 		else
434 			newArgs[i + count] = (char *)(*_oldArgs)[i];
435 	}
436 
437 	newArgs[i + count] = NULL;
438 
439 	*_newArgs = newArgs;
440 	*_oldArgs = (char * const *)newArgs;
441 	*_argCount += count;
442 
443 	return B_OK;
444 }
445 
446 
447 status_t
448 __get_next_image_dependency(image_id id, uint32 *cookie, const char **_name)
449 {
450 	return __gRuntimeLoader->get_next_image_dependency(id, cookie, _name);
451 }
452 
453 
454 status_t
455 __test_executable(const char *path, char *invoker)
456 {
457 	return __gRuntimeLoader->test_executable(path, invoker);
458 }
459 
460 
461 /*!	Allocates a flat buffer and copies the argument and environment strings
462 	into it. The buffer starts with a char* array which contains pointers to
463 	the strings of the arguments and environment, followed by the strings. Both
464 	arguments and environment arrays are NULL-terminated.
465 
466 	If executablePath is non-NULL, it should refer to the executable to be
467 	executed. If the executable file specifies changes to environment variable
468 	values, those will be performed.
469 */
470 status_t
471 __flatten_process_args(const char* const* args, int32 argCount,
472 	const char* const* env, int32* _envCount, const char* executablePath,
473 	char*** _flatArgs, size_t* _flatSize)
474 {
475 	if (args == NULL || _envCount == NULL || (env == NULL && *_envCount != 0))
476 		return B_BAD_VALUE;
477 
478 	int32 envCount = *_envCount;
479 
480 	// determine total needed size
481 	int32 argSize = 0;
482 	for (int32 i = 0; i < argCount; i++) {
483 		if (args[i] == NULL)
484 			return B_BAD_VALUE;
485 		argSize += strlen(args[i]) + 1;
486 	}
487 
488 	int32 envSize = 0;
489 	for (int32 i = 0; i < envCount; i++) {
490 		if (env[i] == NULL)
491 			return B_BAD_VALUE;
492 		envSize += strlen(env[i]) + 1;
493 	}
494 
495 	EnvironmentFilter envFilter;
496 	if (executablePath != NULL)
497 		envFilter.Init(executablePath, env, envCount);
498 
499 	int32 totalSlotCount = argCount + envCount + 2
500 		+ envFilter.AdditionalSlotsNeeded();
501 	int32 size = totalSlotCount * sizeof(char*) + argSize + envSize
502 		+ envFilter.AdditionalSizeNeeded();
503 	if (size > MAX_PROCESS_ARGS_SIZE)
504 		return B_TOO_MANY_ARGS;
505 
506 	// allocate space
507 	char** flatArgs = (char**)malloc(size);
508 	if (flatArgs == NULL)
509 		return B_NO_MEMORY;
510 
511 	char** slot = flatArgs;
512 	char* stringSpace = (char*)(flatArgs + totalSlotCount);
513 
514 	// copy arguments and environment
515 	for (int32 i = 0; i < argCount; i++) {
516 		int32 argSize = strlen(args[i]) + 1;
517 		memcpy(stringSpace, args[i], argSize);
518 		*slot++ = stringSpace;
519 		stringSpace += argSize;
520 	}
521 
522 	*slot++ = NULL;
523 
524 	for (int32 i = 0; i < envCount; i++) {
525 		size_t envSize = envFilter.PrepareSlot(env[i], i, stringSpace);
526 		*slot++ = stringSpace;
527 		stringSpace += envSize;
528 	}
529 
530 	envFilter.PrepareAdditionalSlots(slot, stringSpace);
531 
532 	*slot++ = NULL;
533 
534 	*_envCount = envCount + envFilter.AdditionalSlotsNeeded();
535 	*_flatArgs = flatArgs;
536 	*_flatSize = stringSpace - (char*)flatArgs;
537 	return B_OK;
538 }
539 
540 
541 extern "C" void _call_init_routines_(void);
542 void
543 _call_init_routines_(void)
544 {
545 	// This is called by the original BeOS startup code.
546 	// We don't need it, because our loader already does the job, right?
547 }
548 
549