xref: /haiku/src/system/libnetwork/init.cpp (revision e705c841d784f0035a0ef3e9e96f6e017df16681)
1 /*
2  * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  *		Oliver Tappe, zooey@hirschkaefer.de
8  */
9 
10 
11 #include <libroot_private.h>
12 
13 #include <OS.h>
14 #include <image.h>
15 
16 #include <string.h>
17 
18 
19 bool __gR5Compatibility = false;
20 addr_t __gNetworkStart;
21 addr_t __gNetworkEnd;
22 addr_t __gNetAPIStart;
23 addr_t __gNetAPIEnd;
24 
25 
26 static void
27 find_own_image()
28 {
29 	int32 cookie = 0;
30 	image_info info;
31 	while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
32 		if (((addr_t)info.text <= (addr_t)find_own_image
33 			&& (addr_t)info.text + (addr_t)info.text_size
34 				> (addr_t)find_own_image)) {
35 			// found us
36 			__gNetworkStart = (addr_t)min_c(info.text, info.data);
37 			__gNetworkEnd = min_c((addr_t)info.text + info.text_size,
38 				(addr_t)info.data + info.data_size);
39 			break;
40 		}
41 	}
42 }
43 
44 
45 extern "C" void
46 initialize_before()
47 {
48 	// determine if we have to run in BeOS compatibility mode
49 
50 	// get image of executable
51 	image_info info;
52 	uint32 cookie = 0;
53 	if (get_next_image_info(B_CURRENT_TEAM, (int32*)&cookie, &info) != B_OK)
54 		return;
55 
56 	if (get_image_symbol(info.id, "__gHaikuStartupCode", B_SYMBOL_TYPE_DATA,
57 			NULL) == B_OK) {
58 		// we were linked on/for Haiku
59 		return;
60 	}
61 
62 	// We're using the BeOS startup code, check if BONE libraries are in
63 	// use, and if not, enable the BeOS R5 compatibility layer.
64 	// As dependencies to network libraries may be "hidden" in libraries, we
65 	// may have to scan not only the executable, but every loaded image.
66 	int enable = 0;
67 	uint32 crumble;
68 	const char *name;
69 	do {
70 		crumble = 0;
71 		while (__get_next_image_dependency(info.id, &crumble, &name) == B_OK) {
72 			if (!strcmp(name, "libbind.so")
73 				|| !strcmp(name, "libsocket.so")
74 				|| !strcmp(name, "libbnetapi.so")
75 				|| !strcmp(name, "libnetwork.so"))
76 				enable -= 2;
77 			else if (!strcmp(name, "libnet.so")
78 				|| !strcmp(name, "libnetapi.so"))
79 				enable++;
80 		}
81 
82 		if (enable > 0) {
83 			__gR5Compatibility = true;
84 			find_own_image();
85 			debug_printf("libnetwork.so running in R5 compatibility mode.\n");
86 			return;
87 		}
88 	} while (enable == 0
89 		&& get_next_image_info(B_CURRENT_TEAM, (int32*)&cookie, &info) == B_OK);
90 }
91