source: trunk/third/bonobo-activation/bonobo-activation/bonobo-activation-init.c @ 18563

Revision 18563, 13.6 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18562, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2/*
3 *  bonobo-activation: A library for accessing bonobo-activation-server.
4 *
5 *  Copyright (C) 1999, 2000 Red Hat, Inc.
6 *  Copyright (C) 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 Library General Public
10 *  License as published by the Free Software Foundation; either
11 *  version 2 of the 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 *  Library General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Library General Public
19 *  License along with this library; if not, write to the Free
20 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *  Author: Elliot Lee <sopwith@redhat.com>
23 */
24
25#include <config.h>
26#include "bonobo-activation-init.h"
27#include "bonobo-activation-client.h"
28
29#include "Bonobo_ActivationContext.h"
30#include "bonobo-activation-i18n.h"
31#include "bonobo-activation-private.h"
32#include "bonobo-activation-register.h"
33#include "bonobo-activation-version.h"
34#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <fcntl.h>
37#include <glib.h>
38#include <netdb.h>
39#include <popt.h>
40#include <signal.h>
41#include <stdlib.h>
42#include <string.h>
43#include <sys/socket.h>
44#include <sys/types.h>
45#include <sys/wait.h>
46#include <unistd.h>
47
48/****************** ORBit-specific stuff ****************/
49
50#include <orbit/orbit.h>
51
52#ifdef ORBIT2
53#  define ORBIT_USES_GLIB_MAIN_LOOP 1
54#endif
55
56#ifndef ORBIT_USES_GLIB_MAIN_LOOP
57static int bonobo_activation_corba_prio = G_PRIORITY_LOW;
58
59static gboolean
60orb_handle_connection (GIOChannel * source, GIOCondition cond,
61                       GIOPConnection * cnx)
62{
63        /* The best way to know about an fd exception is if select()/poll()
64         * tells you about it, so we just relay that information on to ORBit
65         * if possible
66         */
67
68        if (cond & (G_IO_HUP | G_IO_NVAL | G_IO_ERR))
69                giop_main_handle_connection_exception (cnx);
70        else
71                giop_main_handle_connection (cnx);
72
73        return TRUE;
74}
75
76static void
77orb_add_connection (GIOPConnection * cnx)
78{
79        int tag;
80        GIOChannel *channel;
81
82        channel = g_io_channel_unix_new (GIOP_CONNECTION_GET_FD (cnx));
83        tag = g_io_add_watch_full (channel, bonobo_activation_corba_prio,
84                                   G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
85                                   (GIOFunc) orb_handle_connection,
86                                   cnx, NULL);
87        g_io_channel_unref (channel);
88
89        cnx->user_data = GUINT_TO_POINTER (tag);
90}
91
92static void
93orb_remove_connection (GIOPConnection * cnx)
94{
95        g_source_remove (GPOINTER_TO_UINT (cnx->user_data));
96        cnx->user_data = GINT_TO_POINTER (-1);
97}
98
99#endif /* !ORBIT_USES_GLIB_MAIN_LOOP */
100
101
102static CORBA_ORB bonobo_activation_orb = CORBA_OBJECT_NIL;
103static CORBA_Context bonobo_activation_context;
104static gboolean is_initialized = FALSE;
105
106/* prevent registering with OAF when bonobo_activation_active_server_register() */
107gboolean bonobo_activation_private = FALSE;
108
109
110/**
111 * bonobo_activation_orb_get:
112 *
113 * Returns the ORB used by OAF.
114 *
115 * Return value: the ORB used by OAF.
116 */
117CORBA_ORB
118bonobo_activation_orb_get (void)
119{
120        return bonobo_activation_orb;
121}
122
123const char *
124bonobo_activation_hostname_get (void)
125{
126        static char *hostname = NULL;
127        char ha_tmp[4], hn_tmp[65];
128        struct hostent *hent;
129
130        if (!hostname) {
131                gethostname (hn_tmp, sizeof (hn_tmp) - 1);
132
133                hent = gethostbyname (hn_tmp);
134                if (hent) {
135                        memcpy (ha_tmp, hent->h_addr, 4);
136                        hent = gethostbyaddr (ha_tmp, 4, AF_INET);
137                        if (hent)
138                                hostname = g_strdup (hent->h_name);
139                        else
140                                hostname =
141                                        g_strdup (inet_ntoa
142                                                  (*
143                                                   ((struct in_addr *)
144                                                    ha_tmp)));
145                } else
146                        hostname = g_strdup (hn_tmp);
147        }
148
149        return hostname;
150}
151
152
153CORBA_Context
154bonobo_activation_context_get (void)
155{
156        return bonobo_activation_context;
157}
158
159const char *
160bonobo_activation_session_name_get (void)
161{
162        const char *dumbptr = "local";
163
164        return dumbptr;
165}
166
167const char *
168bonobo_activation_domain_get (void)
169{
170        return NULL;
171}
172
173CORBA_Object
174bonobo_activation_internal_activation_context_get_extended (gboolean           existing_only,
175                                                            CORBA_Environment *ev)
176{
177        BonoboActivationBaseService base_service = { NULL };
178
179        base_service.name = "IDL:Bonobo/ActivationContext:1.0";
180        base_service.session_name = bonobo_activation_session_name_get ();
181
182        return bonobo_activation_internal_service_get_extended (&base_service, existing_only,
183                                                   ev);
184}
185
186CORBA_Object
187bonobo_activation_activation_context_get (void)
188{
189        BonoboActivationBaseService base_service = { NULL };
190
191        base_service.name = "IDL:Bonobo/ActivationContext:1.0";
192        base_service.session_name = bonobo_activation_session_name_get ();
193
194        return bonobo_activation_service_get (&base_service);
195}
196
197static Bonobo_ObjectDirectory object_directory = CORBA_OBJECT_NIL;
198
199CORBA_Object
200bonobo_activation_object_directory_get (const char *username,
201                                        const char *hostname)
202{
203        CORBA_Environment ev;
204        Bonobo_ActivationContext new_ac;
205        Bonobo_ObjectDirectoryList *od_list;
206        static Bonobo_ActivationContext ac = CORBA_OBJECT_NIL;
207
208        new_ac = bonobo_activation_activation_context_get ();
209        if (ac == new_ac)
210                return object_directory;
211        ac = new_ac;
212
213        CORBA_exception_init (&ev);
214
215        od_list = Bonobo_ActivationContext__get_directories (ac, &ev);
216        if (ev._major != CORBA_NO_EXCEPTION) {
217                CORBA_exception_free (&ev);
218                return CORBA_OBJECT_NIL;
219        }
220
221        if (od_list->_length != 1) {
222                g_warning ("Extremely strange, strange object directories (%d)"
223                           "registered with the activation context", od_list->_length);
224                CORBA_free (od_list);
225                CORBA_exception_free (&ev);
226                return CORBA_OBJECT_NIL;
227        }
228
229        object_directory = CORBA_Object_duplicate (od_list->_buffer[0], &ev);
230       
231        CORBA_free (od_list);
232        CORBA_exception_free (&ev);
233
234        return object_directory;
235}
236
237static int   bonobo_activation_ior_fd = 1;
238static char *bonobo_activation_activate_iid = NULL;
239
240struct poptOption bonobo_activation_popt_options[] = {
241        { NULL, '\0', POPT_ARG_INTL_DOMAIN, PACKAGE, 0, NULL, NULL },
242        { "oaf-ior-fd", '\0', POPT_ARG_INT, &bonobo_activation_ior_fd, 0,
243          N_("File descriptor to print IOR on"), N_("FD") },
244        { "oaf-activate-iid", '\0', POPT_ARG_STRING, &bonobo_activation_activate_iid, 0,
245          N_("IID to activate"), "IID" },
246        { "oaf-private", '\0', POPT_ARG_NONE, &bonobo_activation_private, 0,
247          N_("Prevent registering of server with OAF"), NULL },
248        { NULL }
249};
250
251/**
252 * bonobo_activation_activation_iid_get:
253 *
254 * If this process was launched to activate an exe server, this
255 * function gives the IID of the server requested, otherwise it
256 * returns NULL.
257 *
258 * Return value: The IID of the activated server or NULL.
259 */
260
261const char *
262bonobo_activation_iid_get (void)
263{
264        return bonobo_activation_activate_iid;
265}
266
267int
268bonobo_activation_ior_fd_get (void)
269{
270        return bonobo_activation_ior_fd;
271}
272
273void
274bonobo_activation_preinit (gpointer app, gpointer mod_info)
275{
276}
277
278void
279bonobo_activation_postinit (gpointer app, gpointer mod_info)
280{
281        bonobo_activation_base_service_init ();
282
283        if (bonobo_activation_ior_fd > 2)
284                fcntl (bonobo_activation_ior_fd, F_SETFD, FD_CLOEXEC);
285
286        if (bonobo_activation_activate_iid)
287                g_timeout_add_full (G_PRIORITY_LOW,
288                                    BONOBO_ACTIVATION_FACTORY_TIMEOUT,
289                                    bonobo_activation_timeout_reg_check,
290                                    NULL, NULL);
291        else
292                bonobo_activation_timeout_reg_check_set (FALSE);
293
294        is_initialized = TRUE;
295}
296
297#ifdef BONOBO_ACTIVATION_DEBUG
298static void
299do_barrier (int signum)
300{
301        volatile int barrier = 1;
302
303        while (barrier);
304}
305#endif
306
307/**
308 * bonobo_activation_is_initialized:
309 *
310 * Tells you whether or not bonobo-activation is initialized.
311 *
312 * Return value: whether bonobo-activation is initialized or not.
313 */
314gboolean
315bonobo_activation_is_initialized (void)
316{
317        return is_initialized;
318}
319
320
321/**
322 * bonobo_activation_get_popt_table_name:
323 *
324 * Get the table name to use for the oaf popt options table when
325 * registering with libgnome
326 *
327 * Return value: A localized copy of the string "bonobo activation options"
328 */
329
330char *
331bonobo_activation_get_popt_table_name (void)
332{
333        bindtextdomain (PACKAGE, BONOBO_ACTIVATION_LOCALEDIR);
334        return _("Bonobo activation options");
335}
336
337
338/**
339 * bonobo_activation_init:
340 * @argc: number of command-line arguments passed to the program.
341 * @argv: array of strings containing the command-line
342 *        arguments of the program.
343 *
344 * Initializes bonobo-activation. Should be called before any other
345 * call to the library.
346 *
347 * Return value: the ORB used by bonobo-activation.
348 */
349CORBA_ORB
350bonobo_activation_init (int argc, char **argv)
351{
352        CORBA_ORB retval;
353        int i;
354
355        g_return_val_if_fail (is_initialized == FALSE, bonobo_activation_orb);
356
357        bindtextdomain (PACKAGE, BONOBO_ACTIVATION_LOCALEDIR);
358
359        bonobo_activation_preinit (NULL, NULL);
360
361        retval = bonobo_activation_orb_init (&argc, argv);
362
363        /* Handle non-popt case */
364        for (i = 1; i < argc; i++) {
365                if (!strncmp ("--oaf-ior-fd=", argv[i],
366                              strlen ("--oaf-ior-fd="))) {
367                        bonobo_activation_ior_fd =
368                                atoi (argv[i] + strlen ("--oaf-ior-fd="));
369                        if (!bonobo_activation_ior_fd)
370                                bonobo_activation_ior_fd = 1;
371                } else if (!strncmp
372                           ("--oaf-activate-iid=", argv[i],
373                            strlen ("--oaf-activate-iid="))) {
374                        bonobo_activation_activate_iid =
375                                g_strdup (argv[i] + strlen ("--oaf-activate-iid="));
376                } else if (!strcmp
377                           ("--oaf-private", argv[i])) {
378                        bonobo_activation_private = TRUE;
379                }     
380        }
381
382        bonobo_activation_postinit (NULL, NULL);
383
384        return retval;
385}
386
387CORBA_ORB
388bonobo_activation_orb_init (int *argc, char **argv)
389{
390        CORBA_Context def_ctx;
391        CORBA_Environment ev;
392        const char *hostname;
393
394#ifndef ORBIT_USES_GLIB_MAIN_LOOP
395        IIOPAddConnectionHandler = orb_add_connection;
396        IIOPRemoveConnectionHandler = orb_remove_connection;
397#endif /* !ORBIT_USES_GLIB_MAIN_LOOP */
398
399        CORBA_exception_init (&ev);
400
401        bonobo_activation_orb = CORBA_ORB_init (argc, argv, "orbit-local-orb", &ev);
402        g_assert (ev._major == CORBA_NO_EXCEPTION);
403
404        bonobo_activation_init_activation_env ();
405
406        /* Set values in default context */
407        CORBA_ORB_get_default_context (bonobo_activation_orb, &def_ctx, &ev);
408        CORBA_Context_create_child (def_ctx, "activation", &bonobo_activation_context, &ev);
409        g_assert (ev._major == CORBA_NO_EXCEPTION);
410        CORBA_Object_release ((CORBA_Object) def_ctx, &ev);
411        g_assert (ev._major == CORBA_NO_EXCEPTION);
412
413        hostname = bonobo_activation_hostname_get ();
414        CORBA_Context_set_one_value (bonobo_activation_context, "hostname",
415                                     (char *) hostname, &ev);
416        CORBA_Context_set_one_value (bonobo_activation_context, "username",
417                                     (char *) g_get_user_name (), &ev);
418
419        CORBA_exception_free (&ev);
420
421#ifdef BONOBO_ACTIVATION_DEBUG
422        if (getenv ("BONOBO_ACTIVATION_TRAP_SEGV")) {
423                struct sigaction sa;
424                sa.sa_handler = do_barrier;
425                sigaction (SIGSEGV, &sa, NULL);
426                sigaction (SIGPIPE, &sa, NULL);
427        }
428        if (getenv ("BONOBO_ACTIVATION_BARRIER_INIT")) {
429                volatile int barrier = 1;
430                while (barrier);
431        }
432#endif
433
434        return bonobo_activation_orb;
435}
436
437/**
438 * bonobo_activation_debug_shutdown:
439 *
440 *   A debugging function to shutdown the ORB and process
441 * any reference count leaks that may have occured.
442 *
443 * Return value: FALSE if there were leaks detected, else TRUE
444 **/
445gboolean
446bonobo_activation_debug_shutdown (void)
447{
448        int retval = TRUE;
449        CORBA_Environment ev;
450
451        if (is_initialized) {
452                CORBA_exception_init (&ev);
453
454                bonobo_activation_base_service_debug_shutdown (&ev);
455                if (ev._major != CORBA_NO_EXCEPTION) {
456                        retval = FALSE;
457                }
458
459                if (bonobo_activation_context != CORBA_OBJECT_NIL) {
460                        CORBA_Object_release (
461                                (CORBA_Object) bonobo_activation_context, &ev);
462                        bonobo_activation_context = CORBA_OBJECT_NIL;
463                }
464
465                bonobo_activation_release_corba_client ();
466
467                if (object_directory != CORBA_OBJECT_NIL) {
468                        CORBA_Object_release (object_directory, &ev);
469                        object_directory = CORBA_OBJECT_NIL;
470                }
471
472                if (bonobo_activation_orb != CORBA_OBJECT_NIL) {
473                        CORBA_ORB_destroy (bonobo_activation_orb, &ev);
474                        if (ev._major != CORBA_NO_EXCEPTION) {
475                                retval = FALSE;
476                        }
477                        CORBA_Object_release (
478                                (CORBA_Object) bonobo_activation_orb, &ev);
479                        is_initialized = FALSE;
480                }
481
482                CORBA_exception_free (&ev);
483
484        }
485
486        return retval;
487}
488
489const char  bonobo_activation_version []    = VERSION;
490const guint bonobo_activation_major_version = BONOBO_ACTIVATION_MAJOR_VERSION;
491const guint bonobo_activation_minor_version = BONOBO_ACTIVATION_MINOR_VERSION;
492const guint bonobo_activation_micro_version = BONOBO_ACTIVATION_MICRO_VERSION;
Note: See TracBrowser for help on using the repository browser.