68e3a7b9b88f19d6bd7a4c4465265abcdb614fff
[platform/upstream/dbus.git] / bus / main.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* main.c  main() for message bus
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program 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
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "loop.h"
24 #include "activation.h"
25 #include "connection.h"
26 #include "driver.h"
27 #include <dbus/dbus-list.h>
28
29 static void
30 server_watch_callback (DBusWatch     *watch,
31                        unsigned int   condition,
32                        void          *data)
33 {
34   DBusServer *server = data;
35
36   dbus_server_handle_watch (server, watch, condition);
37 }
38
39 static void
40 add_server_watch (DBusWatch      *watch,
41                   DBusServer     *server)
42 {
43   bus_loop_add_watch (watch, server_watch_callback, server,
44                       NULL);
45 }
46
47 static void
48 remove_server_watch (DBusWatch      *watch,
49                      DBusServer     *server)
50 {
51   bus_loop_remove_watch (watch, server_watch_callback, server);
52 }
53
54 static void
55 setup_server (DBusServer *server)
56 {
57   dbus_server_set_watch_functions (server,
58                                    (DBusAddWatchFunction) add_server_watch,
59                                    (DBusRemoveWatchFunction) remove_server_watch,
60                                    server,
61                                    NULL);
62 }
63
64 static void
65 new_connection_callback (DBusServer     *server,
66                          DBusConnection *new_connection,
67                          void           *data)
68 {
69   if (!bus_connection_setup (new_connection))
70     ; /* we won't have ref'd the connection so it will die */
71 }
72
73 int
74 main (int argc, char **argv)
75 {
76   DBusServer *server;
77   DBusResultCode result;
78
79   if (argc < 2)
80     {
81       _dbus_warn ("Give the server address as an argument\n");
82       return 1;
83     }
84
85   if (argc < 3)
86     {
87       _dbus_warn ("No service location given, not activating activation\n");
88     }
89   else
90     {
91       char *paths[] = { argv[2], NULL };
92
93       bus_activation_init (paths);
94     }
95   
96   server = dbus_server_listen (argv[1], &result);
97   if (server == NULL)
98     {
99       _dbus_warn ("Failed to start server on %s: %s\n",
100                   argv[1], dbus_result_to_string (result));
101       return 1;
102     }
103   
104   setup_server (server);
105
106   bus_connection_init ();
107   
108   dbus_server_set_new_connection_function (server,
109                                            new_connection_callback,
110                                            NULL, NULL);
111
112   _dbus_verbose ("We are on D-Bus...\n");
113   bus_loop_run ();
114   
115   dbus_server_disconnect (server);
116   dbus_server_unref (server);
117
118   return 0;
119 }