1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * oafd: OAF CORBA dameon. |
---|
4 | * |
---|
5 | * Copyright (C) 1999, 2000 Red Hat, Inc. |
---|
6 | * Copyright (C) 1999, 2000 Eazel, Inc. |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License as |
---|
10 | * published by the Free Software Foundation; either version 2 of the |
---|
11 | * License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This library is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | * General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this library; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | * |
---|
22 | * Authors: Elliot Lee <sopwith@redhat.com>, |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <config.h> |
---|
27 | #include <unistd.h> |
---|
28 | #include <stdlib.h> |
---|
29 | |
---|
30 | #include "server.h" |
---|
31 | #include "bonobo-activation/bonobo-activation-i18n.h" |
---|
32 | |
---|
33 | #include "activation-context-query.h" |
---|
34 | #include "object-directory-config-file.h" |
---|
35 | |
---|
36 | #include <ORBitservices/CosNaming.h> |
---|
37 | #include <ORBitservices/CosNaming_impl.h> |
---|
38 | |
---|
39 | #include <sys/types.h> |
---|
40 | #include <sys/stat.h> |
---|
41 | #include <fcntl.h> |
---|
42 | #include <popt.h> |
---|
43 | #include <signal.h> |
---|
44 | #include <stdlib.h> |
---|
45 | #include <unistd.h> |
---|
46 | #include <locale.h> |
---|
47 | #include <string.h> |
---|
48 | |
---|
49 | #include <libxml/parser.h> |
---|
50 | |
---|
51 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
52 | static void debug_queries (void); |
---|
53 | #endif |
---|
54 | |
---|
55 | /* Option values */ |
---|
56 | static char *od_source_dir = NULL; |
---|
57 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
58 | static char *ac_evaluate = NULL; |
---|
59 | static gboolean server_reg = FALSE; |
---|
60 | #endif |
---|
61 | static int server_ac = 0, ior_fd = -1; |
---|
62 | |
---|
63 | static struct poptOption options[] = { |
---|
64 | |
---|
65 | {"od-source-dir", '\0', POPT_ARG_STRING, &od_source_dir, 0, |
---|
66 | N_("Directory to read .server files from"), N_("DIRECTORY")}, |
---|
67 | |
---|
68 | {"ac-activate", '\0', POPT_ARG_NONE, &server_ac, 0, |
---|
69 | N_("Serve as an ActivationContext (default is as an ObjectDirectory only)"), |
---|
70 | NULL}, |
---|
71 | |
---|
72 | {"ior-output-fd", '\0', POPT_ARG_INT, &ior_fd, 0, |
---|
73 | N_("File descriptor to write IOR to"), N_("FD")}, |
---|
74 | |
---|
75 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
76 | {"register-server", '0', POPT_ARG_NONE, &server_reg, 0, |
---|
77 | "Register as the users' activation server without locking [!]", |
---|
78 | NULL}, |
---|
79 | |
---|
80 | {"evaluate", '\0', POPT_ARG_STRING, &ac_evaluate, 0, |
---|
81 | N_("Query expression to evaluate"), N_("EXPRESSION")}, |
---|
82 | #endif |
---|
83 | |
---|
84 | POPT_AUTOHELP {NULL} |
---|
85 | }; |
---|
86 | |
---|
87 | GMainLoop *main_loop = NULL; |
---|
88 | |
---|
89 | static GString * |
---|
90 | build_src_dir (void) |
---|
91 | { |
---|
92 | const char *env_od_source_dir; |
---|
93 | const char *gnome_env_od_source_dir; |
---|
94 | char *config_file_od_source_dir; |
---|
95 | GString *gnome_od_source_dir; |
---|
96 | char **gnome_dirs; |
---|
97 | GString *real_od_source_dir; |
---|
98 | int i; |
---|
99 | |
---|
100 | real_od_source_dir = g_string_new (SERVERINFODIR); |
---|
101 | env_od_source_dir = g_getenv ("BONOBO_ACTIVATION_PATH"); |
---|
102 | gnome_env_od_source_dir = g_getenv ("GNOME2_PATH"); |
---|
103 | config_file_od_source_dir = object_directory_load_config_file (); |
---|
104 | |
---|
105 | if (od_source_dir) { |
---|
106 | g_string_append_c (real_od_source_dir, ':'); |
---|
107 | g_string_append (real_od_source_dir, od_source_dir); |
---|
108 | } |
---|
109 | |
---|
110 | if (env_od_source_dir) { |
---|
111 | g_string_append_c (real_od_source_dir, ':'); |
---|
112 | g_string_append (real_od_source_dir, |
---|
113 | env_od_source_dir); |
---|
114 | } |
---|
115 | |
---|
116 | if (config_file_od_source_dir) { |
---|
117 | g_string_append_c (real_od_source_dir, ':'); |
---|
118 | g_string_append (real_od_source_dir, |
---|
119 | config_file_od_source_dir); |
---|
120 | g_free (config_file_od_source_dir); |
---|
121 | } |
---|
122 | |
---|
123 | if (gnome_env_od_source_dir) { |
---|
124 | gnome_dirs = g_strsplit (gnome_env_od_source_dir, ":", -1); |
---|
125 | gnome_od_source_dir = g_string_new(""); |
---|
126 | for (i=0; gnome_dirs[i]; i++) { |
---|
127 | g_string_append (gnome_od_source_dir, |
---|
128 | gnome_dirs[i]); |
---|
129 | g_string_append (gnome_od_source_dir, |
---|
130 | "/lib/bonobo/servers:"); |
---|
131 | } |
---|
132 | g_strfreev (gnome_dirs); |
---|
133 | g_string_append_c (real_od_source_dir, ':'); |
---|
134 | g_string_append (real_od_source_dir, |
---|
135 | gnome_od_source_dir->str); |
---|
136 | } |
---|
137 | |
---|
138 | return real_od_source_dir; |
---|
139 | } |
---|
140 | |
---|
141 | static int |
---|
142 | redirect_output (int ior_fd) |
---|
143 | { |
---|
144 | int dev_null_fd; |
---|
145 | const char *debug_output; |
---|
146 | |
---|
147 | debug_output = g_getenv ("BONOBO_ACTIVATION_DEBUG_OUTPUT"); |
---|
148 | |
---|
149 | dev_null_fd = -1; |
---|
150 | if (debug_output == NULL || strlen (debug_output) == 0) { |
---|
151 | dev_null_fd = open ("/dev/null", O_RDWR); |
---|
152 | if (ior_fd != 0) |
---|
153 | dup2 (dev_null_fd, 0); |
---|
154 | if (ior_fd != 1) |
---|
155 | dup2 (dev_null_fd, 1); |
---|
156 | if (ior_fd != 2) |
---|
157 | dup2 (dev_null_fd, 2); |
---|
158 | } |
---|
159 | |
---|
160 | return dev_null_fd; |
---|
161 | } |
---|
162 | |
---|
163 | static void |
---|
164 | nameserver_destroy (PortableServer_POA poa, |
---|
165 | const CORBA_Object reference, |
---|
166 | CORBA_Environment *ev) |
---|
167 | { |
---|
168 | PortableServer_ObjectId *oid; |
---|
169 | |
---|
170 | oid = PortableServer_POA_reference_to_id (poa, reference, ev); |
---|
171 | PortableServer_POA_deactivate_object (poa, oid, ev); |
---|
172 | CORBA_free (oid); |
---|
173 | } |
---|
174 | |
---|
175 | int |
---|
176 | main (int argc, char *argv[]) |
---|
177 | { |
---|
178 | PortableServer_POAManager poa_manager; |
---|
179 | PortableServer_POA root_poa; |
---|
180 | CORBA_ORB orb; |
---|
181 | CORBA_Environment real_ev, *ev; |
---|
182 | CORBA_Object naming_service; |
---|
183 | Bonobo_ActivationEnvironment environment; |
---|
184 | Bonobo_ObjectDirectory od; |
---|
185 | poptContext ctx; |
---|
186 | int dev_null_fd; |
---|
187 | char *ior; |
---|
188 | FILE *fh; |
---|
189 | struct sigaction sa; |
---|
190 | GString *src_dir; |
---|
191 | |
---|
192 | /* |
---|
193 | * Become process group leader, detach from controlling |
---|
194 | * terminal, etc. |
---|
195 | */ |
---|
196 | setsid (); |
---|
197 | |
---|
198 | /* internationalization. */ |
---|
199 | setlocale (LC_ALL, ""); |
---|
200 | bindtextdomain (PACKAGE, SERVER_LOCALEDIR); |
---|
201 | textdomain (PACKAGE); |
---|
202 | |
---|
203 | /* Ignore sig-pipe - as normal */ |
---|
204 | memset (&sa, 0, sizeof (sa)); |
---|
205 | sa.sa_handler = SIG_IGN; |
---|
206 | sigaction (SIGPIPE, &sa, NULL); |
---|
207 | |
---|
208 | |
---|
209 | ctx = poptGetContext ("oafd", argc, (const char **)argv, options, 0); |
---|
210 | while (poptGetNextOpt (ctx) >= 0) ; |
---|
211 | poptFreeContext (ctx); |
---|
212 | |
---|
213 | LIBXML_TEST_VERSION; |
---|
214 | xmlKeepBlanksDefault(0); |
---|
215 | |
---|
216 | #if 0 |
---|
217 | while (!g_file_test ("/tmp/orbit-go", G_FILE_TEST_EXISTS)) |
---|
218 | sleep (1); |
---|
219 | #endif |
---|
220 | |
---|
221 | dev_null_fd = redirect_output (ior_fd); |
---|
222 | |
---|
223 | orb = bonobo_activation_init (argc, argv); |
---|
224 | main_loop = g_main_loop_new (NULL, FALSE); |
---|
225 | |
---|
226 | add_initial_locales (); |
---|
227 | |
---|
228 | CORBA_exception_init ((ev = &real_ev)); |
---|
229 | |
---|
230 | root_poa = (PortableServer_POA) |
---|
231 | CORBA_ORB_resolve_initial_references (orb, "RootPOA", ev); |
---|
232 | |
---|
233 | src_dir = build_src_dir (); |
---|
234 | bonobo_object_directory_init (root_poa, src_dir->str, ev); |
---|
235 | g_string_free (src_dir, TRUE); |
---|
236 | |
---|
237 | od = bonobo_object_directory_get (); |
---|
238 | |
---|
239 | memset (&environment, 0, sizeof (Bonobo_ActivationEnvironment)); |
---|
240 | |
---|
241 | naming_service = impl_CosNaming_NamingContext__create (root_poa, ev); |
---|
242 | if (naming_service == NULL) |
---|
243 | g_warning ("Failed to create naming service"); |
---|
244 | Bonobo_ObjectDirectory_register_new |
---|
245 | (od, NAMING_CONTEXT_IID, &environment, naming_service, ev); |
---|
246 | |
---|
247 | if (ior_fd < 0 && !server_ac) |
---|
248 | g_error ("\n\n-- \nThe bonobo-activation-server must be forked by\n" |
---|
249 | "libbonobo-activation, and cannot be run itself.\n" |
---|
250 | "This is due to us doing client side locking.\n-- \n"); |
---|
251 | |
---|
252 | /* |
---|
253 | * It is no longer useful at all to be a pure |
---|
254 | * ObjectDirectory we have binned that mode of |
---|
255 | * operation, as a bad, racy and inefficient job. |
---|
256 | */ |
---|
257 | g_assert (server_ac); |
---|
258 | |
---|
259 | activation_context_init (root_poa, od, ev); |
---|
260 | |
---|
261 | ior = CORBA_ORB_object_to_string (orb, activation_context_get (), ev); |
---|
262 | |
---|
263 | fh = NULL; |
---|
264 | if (ior_fd >= 0) |
---|
265 | fh = fdopen (ior_fd, "w"); |
---|
266 | if (fh) { |
---|
267 | fprintf (fh, "%s\n", ior); |
---|
268 | fclose (fh); |
---|
269 | if (ior_fd <= 2) |
---|
270 | dup2 (dev_null_fd, ior_fd); |
---|
271 | } else { |
---|
272 | printf ("%s\n", ior); |
---|
273 | fflush (stdout); |
---|
274 | } |
---|
275 | if (dev_null_fd != -1) |
---|
276 | close (dev_null_fd); |
---|
277 | |
---|
278 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
279 | debug_queries (); |
---|
280 | if (server_reg) { |
---|
281 | char *fname; |
---|
282 | fname = g_strconcat (linc_get_tmpdir (), |
---|
283 | "/bonobo-activation-server-ior", NULL); |
---|
284 | fh = fopen (fname, "w+"); |
---|
285 | fprintf (fh, "%s\n", ior); |
---|
286 | fclose (fh); |
---|
287 | g_free (fname); |
---|
288 | } |
---|
289 | #endif |
---|
290 | |
---|
291 | CORBA_free (ior); |
---|
292 | |
---|
293 | poa_manager = PortableServer_POA__get_the_POAManager (root_poa, ev); |
---|
294 | PortableServer_POAManager_activate (poa_manager, ev); |
---|
295 | |
---|
296 | g_main_loop_run (main_loop); |
---|
297 | |
---|
298 | nameserver_destroy (root_poa, naming_service, ev); |
---|
299 | CORBA_Object_release (naming_service, ev); |
---|
300 | |
---|
301 | bonobo_object_directory_shutdown (root_poa, ev); |
---|
302 | activation_context_shutdown (root_poa, ev); |
---|
303 | |
---|
304 | CORBA_Object_release ((CORBA_Object) poa_manager, ev); |
---|
305 | CORBA_Object_release ((CORBA_Object) root_poa, ev); |
---|
306 | |
---|
307 | return !bonobo_activation_debug_shutdown (); |
---|
308 | } |
---|
309 | |
---|
310 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
311 | static void |
---|
312 | debug_queries (void) |
---|
313 | { |
---|
314 | if (ac_evaluate) { |
---|
315 | QueryExpr *exp; |
---|
316 | const char *err; |
---|
317 | QueryContext tmpctx = { NULL, 0, CORBA_OBJECT_NIL }; |
---|
318 | |
---|
319 | err = qexp_parse (ac_evaluate, &exp); |
---|
320 | if (err) { |
---|
321 | g_print ("Parse error: %s\n", err); |
---|
322 | } else { |
---|
323 | QueryExprConst res; |
---|
324 | |
---|
325 | qexp_dump (exp); |
---|
326 | g_print ("\n"); |
---|
327 | g_print ("Evaluation with no server record is: "); |
---|
328 | res = qexp_evaluate (NULL, exp, &tmpctx); |
---|
329 | qexp_constant_dump (&res); |
---|
330 | g_print ("\n"); |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | #endif |
---|