xref: /haiku/src/tests/servers/registrar/run_test_registrar.cpp (revision 3be9edf8da228afd9fec0390f408c964766122aa)
1 /*
2  * Copyright 2005-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <OS.h>
8 #include <image.h>
9 #include <Entry.h>
10 #include <Query.h>
11 #include <Path.h>
12 #include <Volume.h>
13 #include <fs_info.h>
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 
19 
20 extern const char* __progname;
21 
22 
23 int
24 main()
25 {
26 	team_info teamInfo;
27 	int32 cookie = 0;
28 	while (get_next_team_info(&cookie, &teamInfo) == B_OK) {
29 		if (!strncmp(teamInfo.args, "/boot/beos/", 11)) {
30 			// this is a system component and not worth to investigate
31 			continue;
32 		}
33 
34 		thread_info threadInfo;
35 		int32 threadCookie = 0;
36 		while (get_next_thread_info(teamInfo.team, &threadCookie, &threadInfo)
37 				== B_OK) {
38 			// search for the roster thread
39 			if (!strcmp(threadInfo.name, "_roster_thread_")) {
40 				port_id port = find_port("haiku-test:roster");
41 				port_info portInfo;
42 				if (get_port_info(port, &portInfo) == B_OK
43 					&& portInfo.team == teamInfo.team) {
44 					puts("The Haiku Registrar is already running.");
45 					return 0;
46 				}
47 			}
48 		}
49 	}
50 
51 	// the Haiku registrar doesn't seem to run yet, change this
52 
53 	BPath currentPath(".");
54 
55 	BQuery query;
56 	query.SetPredicate("name==test_registrar");
57 
58 	// search on current volume only
59 	dev_t device = dev_for_path(".");
60 	BVolume volume(device);
61 
62 	query.SetVolume(&volume);
63 	query.Fetch();
64 
65 	entry_ref ref;
66 	while (query.GetNextRef(&ref) == B_OK) {
67 		BPath path(&ref);
68 		if (path.InitCheck() != B_OK)
69 			continue;
70 
71 		const char* registrarPath = path.Path();
72 		const char* generatedPath = strstr(registrarPath, "generated");
73 		if (generatedPath == NULL)
74 			continue;
75 
76 		if (!strncmp(currentPath.Path(), registrarPath, generatedPath - registrarPath)) {
77 			// gotcha!
78 			const char* args[] = { registrarPath, NULL };
79 			thread_id thread = load_image(1, args, (const char**)environ);
80 			if (thread < B_OK) {
81 				fprintf(stderr, "%s: Could not start the registrar: %s\n",
82 					__progname, strerror(thread));
83 				return -1;
84 			}
85 
86 			resume_thread(thread);
87 			return 0;
88 		}
89 	}
90 
91 	fprintf(stderr, "%s: Could not find the Haiku Registrar.\n"
92 		"    (This tool only works when used in the Haiku tree, but maybe\n"
93 		"    the registrar just needs to be built.)\n",
94 		__progname);
95 	return -1;
96 }
97 
98