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 char *od_domain = "session"; |
---|
62 | static int server_ac = 0, ior_fd = -1; |
---|
63 | |
---|
64 | static struct poptOption options[] = { |
---|
65 | |
---|
66 | {"od-source-dir", '\0', POPT_ARG_STRING, &od_source_dir, 0, |
---|
67 | N_("Directory to read .server files from"), N_("DIRECTORY")}, |
---|
68 | |
---|
69 | {"od-domain", '\0', POPT_ARG_STRING, &od_domain, 0, |
---|
70 | N_("Domain of ObjectDirectory"), N_("DOMAIN")}, |
---|
71 | |
---|
72 | {"ac-activate", '\0', POPT_ARG_NONE, &server_ac, 0, |
---|
73 | N_("Serve as an ActivationContext (default is as an ObjectDirectory only)"), |
---|
74 | NULL}, |
---|
75 | |
---|
76 | {"ior-output-fd", '\0', POPT_ARG_INT, &ior_fd, 0, |
---|
77 | N_("File descriptor to write IOR to"), N_("FD")}, |
---|
78 | |
---|
79 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
80 | {"register-server", '0', POPT_ARG_NONE, &server_reg, 0, |
---|
81 | "Register as the users' activation server without locking [!]", |
---|
82 | NULL}, |
---|
83 | |
---|
84 | {"evaluate", '\0', POPT_ARG_STRING, &ac_evaluate, 0, |
---|
85 | N_("Query expression to evaluate"), N_("EXPRESSION")}, |
---|
86 | #endif |
---|
87 | |
---|
88 | POPT_AUTOHELP {NULL} |
---|
89 | }; |
---|
90 | |
---|
91 | GMainLoop *main_loop = NULL; |
---|
92 | |
---|
93 | static GString * |
---|
94 | build_src_dir (void) |
---|
95 | { |
---|
96 | const char *env_od_source_dir; |
---|
97 | const char *gnome_env_od_source_dir; |
---|
98 | char *config_file_od_source_dir; |
---|
99 | GString *gnome_od_source_dir; |
---|
100 | char **gnome_dirs; |
---|
101 | GString *real_od_source_dir; |
---|
102 | int i; |
---|
103 | |
---|
104 | real_od_source_dir = g_string_new (SERVERINFODIR); |
---|
105 | env_od_source_dir = g_getenv ("BONOBO_ACTIVATION_PATH"); |
---|
106 | gnome_env_od_source_dir = g_getenv ("GNOME2_PATH"); |
---|
107 | config_file_od_source_dir = object_directory_load_config_file (); |
---|
108 | |
---|
109 | if (od_source_dir) { |
---|
110 | g_string_append_c (real_od_source_dir, ':'); |
---|
111 | g_string_append (real_od_source_dir, od_source_dir); |
---|
112 | } |
---|
113 | |
---|
114 | if (env_od_source_dir) { |
---|
115 | g_string_append_c (real_od_source_dir, ':'); |
---|
116 | g_string_append (real_od_source_dir, |
---|
117 | env_od_source_dir); |
---|
118 | } |
---|
119 | |
---|
120 | if (config_file_od_source_dir) { |
---|
121 | g_string_append_c (real_od_source_dir, ':'); |
---|
122 | g_string_append (real_od_source_dir, |
---|
123 | config_file_od_source_dir); |
---|
124 | g_free (config_file_od_source_dir); |
---|
125 | } |
---|
126 | |
---|
127 | if (gnome_env_od_source_dir) { |
---|
128 | gnome_dirs = g_strsplit (gnome_env_od_source_dir, ":", -1); |
---|
129 | gnome_od_source_dir = g_string_new(""); |
---|
130 | for (i=0; gnome_dirs[i]; i++) { |
---|
131 | g_string_append (gnome_od_source_dir, |
---|
132 | gnome_dirs[i]); |
---|
133 | g_string_append (gnome_od_source_dir, |
---|
134 | "/lib/bonobo/servers:"); |
---|
135 | } |
---|
136 | g_strfreev (gnome_dirs); |
---|
137 | g_string_append_c (real_od_source_dir, ':'); |
---|
138 | g_string_append (real_od_source_dir, |
---|
139 | gnome_od_source_dir->str); |
---|
140 | } |
---|
141 | |
---|
142 | return real_od_source_dir; |
---|
143 | } |
---|
144 | |
---|
145 | static int |
---|
146 | redirect_output (int ior_fd) |
---|
147 | { |
---|
148 | int dev_null_fd; |
---|
149 | const char *debug_output; |
---|
150 | |
---|
151 | debug_output = g_getenv ("BONOBO_ACTIVATION_DEBUG_OUTPUT"); |
---|
152 | |
---|
153 | dev_null_fd = -1; |
---|
154 | if (debug_output == NULL || strlen (debug_output) == 0) { |
---|
155 | dev_null_fd = open ("/dev/null", O_RDWR); |
---|
156 | if (ior_fd != 0) |
---|
157 | dup2 (dev_null_fd, 0); |
---|
158 | if (ior_fd != 1) |
---|
159 | dup2 (dev_null_fd, 1); |
---|
160 | if (ior_fd != 2) |
---|
161 | dup2 (dev_null_fd, 2); |
---|
162 | } |
---|
163 | |
---|
164 | return dev_null_fd; |
---|
165 | } |
---|
166 | |
---|
167 | static void |
---|
168 | nameserver_destroy (PortableServer_POA poa, |
---|
169 | const CORBA_Object reference, |
---|
170 | CORBA_Environment *ev) |
---|
171 | { |
---|
172 | PortableServer_ObjectId *oid; |
---|
173 | |
---|
174 | oid = PortableServer_POA_reference_to_id (poa, reference, ev); |
---|
175 | PortableServer_POA_deactivate_object (poa, oid, ev); |
---|
176 | CORBA_free (oid); |
---|
177 | } |
---|
178 | |
---|
179 | int |
---|
180 | main (int argc, char *argv[]) |
---|
181 | { |
---|
182 | CORBA_ORB orb; |
---|
183 | PortableServer_POA root_poa; |
---|
184 | PortableServer_POAManager poa_manager; |
---|
185 | CORBA_Environment real_ev, *ev; |
---|
186 | CORBA_Object naming_service; |
---|
187 | Bonobo_ObjectDirectory od; |
---|
188 | poptContext ctx; |
---|
189 | int dev_null_fd; |
---|
190 | char *ior; |
---|
191 | FILE *fh; |
---|
192 | struct sigaction sa; |
---|
193 | GString *src_dir; |
---|
194 | |
---|
195 | /* |
---|
196 | * Become process group leader, detach from controlling |
---|
197 | * terminal, etc. |
---|
198 | */ |
---|
199 | setsid (); |
---|
200 | |
---|
201 | /* internationalization. */ |
---|
202 | setlocale (LC_ALL, ""); |
---|
203 | bindtextdomain (PACKAGE, SERVER_LOCALEDIR); |
---|
204 | textdomain (PACKAGE); |
---|
205 | |
---|
206 | /* Ignore sig-pipe - as normal */ |
---|
207 | memset (&sa, 0, sizeof (sa)); |
---|
208 | sa.sa_handler = SIG_IGN; |
---|
209 | sigaction (SIGPIPE, &sa, NULL); |
---|
210 | |
---|
211 | |
---|
212 | ctx = poptGetContext ("oafd", argc, (const char **)argv, options, 0); |
---|
213 | while (poptGetNextOpt (ctx) >= 0) ; |
---|
214 | poptFreeContext (ctx); |
---|
215 | |
---|
216 | LIBXML_TEST_VERSION; |
---|
217 | xmlKeepBlanksDefault(0); |
---|
218 | |
---|
219 | #if 0 |
---|
220 | while (!g_file_test ("/tmp/orbit-go", G_FILE_TEST_EXISTS)) |
---|
221 | sleep (1); |
---|
222 | #endif |
---|
223 | |
---|
224 | dev_null_fd = redirect_output (ior_fd); |
---|
225 | |
---|
226 | orb = bonobo_activation_init (argc, argv); |
---|
227 | main_loop = g_main_loop_new (NULL, FALSE); |
---|
228 | |
---|
229 | add_initial_locales (); |
---|
230 | |
---|
231 | CORBA_exception_init ((ev = &real_ev)); |
---|
232 | |
---|
233 | root_poa = (PortableServer_POA) |
---|
234 | CORBA_ORB_resolve_initial_references (orb, "RootPOA", ev); |
---|
235 | |
---|
236 | src_dir = build_src_dir (); |
---|
237 | bonobo_object_directory_init (root_poa, od_domain, src_dir->str, ev); |
---|
238 | g_string_free (src_dir, TRUE); |
---|
239 | |
---|
240 | od = bonobo_object_directory_get (); |
---|
241 | |
---|
242 | naming_service = impl_CosNaming_NamingContext__create (root_poa, ev); |
---|
243 | if (naming_service == NULL) |
---|
244 | g_warning ("Failed to create naming service"); |
---|
245 | Bonobo_ObjectDirectory_register_new |
---|
246 | (od, NAMING_CONTEXT_IID, naming_service, ev); |
---|
247 | |
---|
248 | if (ior_fd < 0 && !server_ac) |
---|
249 | g_error ("\n\n-- \nThe bonobo-activation-server must be forked by\n" |
---|
250 | "libbonobo-activation, and cannot be run itself.\n" |
---|
251 | "This is due to us doing client side locking.\n-- \n"); |
---|
252 | |
---|
253 | /* |
---|
254 | * It is no longer useful at all to be a pure |
---|
255 | * ObjectDirectory we have binned that mode of |
---|
256 | * operation, as a bad, racy and inefficient job. |
---|
257 | */ |
---|
258 | g_assert (server_ac); |
---|
259 | |
---|
260 | activation_context_init (root_poa, od, ev); |
---|
261 | |
---|
262 | ior = CORBA_ORB_object_to_string (orb, activation_context_get (), ev); |
---|
263 | |
---|
264 | fh = NULL; |
---|
265 | if (ior_fd >= 0) |
---|
266 | fh = fdopen (ior_fd, "w"); |
---|
267 | if (fh) { |
---|
268 | fprintf (fh, "%s\n", ior); |
---|
269 | fclose (fh); |
---|
270 | if (ior_fd <= 2) |
---|
271 | dup2 (dev_null_fd, ior_fd); |
---|
272 | } else { |
---|
273 | printf ("%s\n", ior); |
---|
274 | fflush (stdout); |
---|
275 | } |
---|
276 | if (dev_null_fd != -1) |
---|
277 | close (dev_null_fd); |
---|
278 | |
---|
279 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
280 | debug_queries (); |
---|
281 | if (server_reg) { |
---|
282 | char *fname; |
---|
283 | fname = g_strconcat (linc_get_tmpdir (), |
---|
284 | "/bonobo-activation-server-ior", NULL); |
---|
285 | fh = fopen (fname, "w+"); |
---|
286 | fprintf (fh, "%s\n", ior); |
---|
287 | fclose (fh); |
---|
288 | g_free (fname); |
---|
289 | } |
---|
290 | #endif |
---|
291 | |
---|
292 | CORBA_free (ior); |
---|
293 | |
---|
294 | poa_manager = PortableServer_POA__get_the_POAManager (root_poa, ev); |
---|
295 | PortableServer_POAManager_activate (poa_manager, ev); |
---|
296 | |
---|
297 | g_main_loop_run (main_loop); |
---|
298 | |
---|
299 | nameserver_destroy (root_poa, naming_service, ev); |
---|
300 | CORBA_Object_release (naming_service, ev); |
---|
301 | |
---|
302 | bonobo_object_directory_shutdown (root_poa, ev); |
---|
303 | activation_context_shutdown (root_poa, ev); |
---|
304 | |
---|
305 | CORBA_Object_release ((CORBA_Object) poa_manager, ev); |
---|
306 | CORBA_Object_release ((CORBA_Object) root_poa, ev); |
---|
307 | |
---|
308 | return !bonobo_activation_debug_shutdown (); |
---|
309 | } |
---|
310 | |
---|
311 | #ifdef BONOBO_ACTIVATION_DEBUG |
---|
312 | static void |
---|
313 | debug_queries (void) |
---|
314 | { |
---|
315 | if (ac_evaluate) { |
---|
316 | QueryExpr *exp; |
---|
317 | const char *err; |
---|
318 | QueryContext tmpctx = { NULL, 0, CORBA_OBJECT_NIL }; |
---|
319 | |
---|
320 | err = qexp_parse (ac_evaluate, &exp); |
---|
321 | if (err) { |
---|
322 | g_print ("Parse error: %s\n", err); |
---|
323 | } else { |
---|
324 | QueryExprConst res; |
---|
325 | |
---|
326 | qexp_dump (exp); |
---|
327 | g_print ("\n"); |
---|
328 | g_print ("Evaluation with no server record is: "); |
---|
329 | res = qexp_evaluate (NULL, exp, &tmpctx); |
---|
330 | qexp_constant_dump (&res); |
---|
331 | g_print ("\n"); |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | #endif |
---|