2003-03-16 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-server.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server.c DBusServer object
3  *
4  * Copyright (C) 2002, 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
24 #include "dbus-server.h"
25 #include "dbus-server-unix.h"
26 #ifdef DBUS_BUILD_TESTS
27 #include "dbus-server-debug.h"
28 #include "dbus-server-debug-pipe.h"
29 #endif
30 #include "dbus-address.h"
31
32 /**
33  * @defgroup DBusServer DBusServer
34  * @ingroup  DBus
35  * @brief Server that listens for new connections.
36  *
37  * Types and functions related to DBusServer.
38  * A DBusServer represents a server that other applications
39  * can connect to. Each connection from another application
40  * is represented by a DBusConnection.
41  *
42  * @todo Thread safety hasn't been looked at for #DBusServer
43  * @todo Need notification to apps of disconnection, may matter for some transports
44  */
45
46 /**
47  * @defgroup DBusServerInternals DBusServer implementation details
48  * @ingroup  DBusInternals
49  * @brief Implementation details of DBusServer
50  *
51  * @{
52  */
53
54 /**
55  * Initializes the members of the DBusServer base class.
56  * Chained up to by subclass constructors.
57  *
58  * @param server the server.
59  * @param vtable the vtable for the subclass.
60  * @returns #TRUE on success.
61  */
62 dbus_bool_t
63 _dbus_server_init_base (DBusServer             *server,
64                         const DBusServerVTable *vtable)
65 {
66   server->vtable = vtable;
67   server->refcount = 1;
68
69   server->watches = _dbus_watch_list_new ();
70   if (server->watches == NULL)
71     return FALSE;
72
73   server->timeouts = _dbus_timeout_list_new ();
74   if (server->timeouts == NULL)
75     {
76       _dbus_watch_list_free (server->watches);
77       server->watches = NULL;
78       return FALSE;
79     }
80   
81   server->connection_counter = _dbus_counter_new ();
82   if (server->connection_counter == NULL)
83     {
84       _dbus_watch_list_free (server->watches);
85       server->watches = NULL;
86       _dbus_timeout_list_free (server->timeouts);
87       server->timeouts = NULL;
88       
89       return FALSE;
90     }
91
92   server->max_connections = 256; /* same as an X server, seems like a nice default */
93
94   _dbus_data_slot_list_init (&server->slot_list);
95   
96   return TRUE;
97 }
98
99 /**
100  * Finalizes the members of the DBusServer base class.
101  * Chained up to by subclass finalizers.
102  *
103  * @param server the server.
104  */
105 void
106 _dbus_server_finalize_base (DBusServer *server)
107 {
108   /* calls out to application code... */
109   _dbus_data_slot_list_free (&server->slot_list);
110
111   dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
112
113   if (!server->disconnected)
114     dbus_server_disconnect (server);
115
116   _dbus_watch_list_free (server->watches);
117   _dbus_timeout_list_free (server->timeouts);
118   _dbus_counter_unref (server->connection_counter);
119 }
120
121 /**
122  * Adds a watch for this server, chaining out to application-provided
123  * watch handlers.
124  *
125  * @param server the server.
126  * @param watch the watch to add.
127  */
128 dbus_bool_t
129 _dbus_server_add_watch (DBusServer *server,
130                         DBusWatch  *watch)
131 {
132   return _dbus_watch_list_add_watch (server->watches, watch);
133 }
134
135 /**
136  * Removes a watch previously added with _dbus_server_remove_watch().
137  *
138  * @param server the server.
139  * @param watch the watch to remove.
140  */
141 void
142 _dbus_server_remove_watch  (DBusServer *server,
143                             DBusWatch  *watch)
144 {
145   _dbus_watch_list_remove_watch (server->watches, watch);
146 }
147
148 /**
149  * Toggles a watch and notifies app via server's
150  * DBusWatchToggledFunction if available. It's an error to call this
151  * function on a watch that was not previously added.
152  *
153  * @param server the server.
154  * @param watch the watch to toggle.
155  * @param enabled whether to enable or disable
156  */
157 void
158 _dbus_server_toggle_watch (DBusServer  *server,
159                            DBusWatch   *watch,
160                            dbus_bool_t  enabled)
161 {
162   if (server->watches) /* null during finalize */
163     _dbus_watch_list_toggle_watch (server->watches,
164                                    watch, enabled);
165 }
166
167 /**
168  * Adds a timeout for this server, chaining out to
169  * application-provided timeout handlers. The timeout should be
170  * repeatedly handled with dbus_timeout_handle() at its given interval
171  * until it is removed.
172  *
173  * @param server the server.
174  * @param timeout the timeout to add.
175  */
176 dbus_bool_t
177 _dbus_server_add_timeout (DBusServer  *server,
178                           DBusTimeout *timeout)
179 {
180   return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
181 }
182
183 /**
184  * Removes a timeout previously added with _dbus_server_add_timeout().
185  *
186  * @param server the server.
187  * @param timeout the timeout to remove.
188  */
189 void
190 _dbus_server_remove_timeout (DBusServer  *server,
191                              DBusTimeout *timeout)
192 {
193   _dbus_timeout_list_remove_timeout (server->timeouts, timeout);  
194 }
195
196 /**
197  * Toggles a timeout and notifies app via server's
198  * DBusTimeoutToggledFunction if available. It's an error to call this
199  * function on a timeout that was not previously added.
200  *
201  * @param server the server.
202  * @param timeout the timeout to toggle.
203  * @param enabled whether to enable or disable
204  */
205 void
206 _dbus_server_toggle_timeout (DBusServer  *server,
207                              DBusTimeout *timeout,
208                              dbus_bool_t  enabled)
209 {
210   if (server->timeouts) /* null during finalize */
211     _dbus_timeout_list_toggle_timeout (server->timeouts,
212                                        timeout, enabled);
213 }
214
215
216 /** @} */
217
218 /**
219  * @addtogroup DBusServer
220  *
221  * @{
222  */
223
224
225 /**
226  * @typedef DBusServer
227  *
228  * An opaque object representing a server that listens for
229  * connections from other applications. Each time a connection
230  * is made, a new DBusConnection is created and made available
231  * via an application-provided DBusNewConnectionFunction.
232  * The DBusNewConnectionFunction is provided with
233  * dbus_server_set_new_connection_function().
234  * 
235  */
236
237 /**
238  * Listens for new connections on the given address.
239  * Returns #NULL if listening fails for any reason.
240  * Otherwise returns a new #DBusServer.
241  * dbus_server_set_new_connection_function() and
242  * dbus_server_set_watch_functions() should be called
243  * immediately to render the server fully functional.
244  *
245  * @todo error messages on bad address could really be better.
246  * DBusResultCode is a bit limiting here.
247  *
248  * @param address the address of this server.
249  * @param result location to store rationale for failure.
250  * @returns a new DBusServer, or #NULL on failure.
251  * 
252  */
253 DBusServer*
254 dbus_server_listen (const char     *address,
255                     DBusResultCode *result)
256 {
257   DBusServer *server;
258   DBusAddressEntry **entries;
259   int len, i;
260   
261   if (!dbus_parse_address (address, &entries, &len, result))
262     return NULL;
263
264   server = NULL;
265   
266   for (i = 0; i < len; i++)
267     {
268       const char *method = dbus_address_entry_get_method (entries[i]);
269
270       if (strcmp (method, "unix") == 0)
271         {
272           const char *path = dbus_address_entry_get_value (entries[i], "path");
273
274           if (path == NULL)
275             goto bad_address;
276
277           server = _dbus_server_new_for_domain_socket (path, result);
278
279           if (server)
280             break;
281         }
282       else if (strcmp (method, "tcp") == 0)
283         {
284           const char *host = dbus_address_entry_get_value (entries[i], "host");
285           const char *port = dbus_address_entry_get_value (entries[i], "port");
286           DBusString  str;
287           long lport;
288           dbus_bool_t sresult;
289           
290           if (port == NULL)
291             goto bad_address;
292
293           _dbus_string_init_const (&str, port);
294           sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
295           _dbus_string_free (&str);
296           
297           if (sresult == FALSE || lport <= 0 || lport > 65535)
298             goto bad_address;
299           
300           server = _dbus_server_new_for_tcp_socket (host, lport, result);
301
302           if (server)
303             break;
304         }
305 #ifdef DBUS_BUILD_TESTS
306       else if (strcmp (method, "debug") == 0)
307         {
308           const char *name = dbus_address_entry_get_value (entries[i], "name");
309
310           if (name == NULL)
311             goto bad_address;
312
313           server = _dbus_server_debug_new (name, result);
314
315           if (server)
316             break;
317         }
318       else if (strcmp (method, "debug-pipe") == 0)
319         {
320           const char *name = dbus_address_entry_get_value (entries[i], "name");
321
322           if (name == NULL)
323             goto bad_address;
324
325           server = _dbus_server_debug_pipe_new (name, result);
326
327           if (server)
328             break;
329         }
330 #endif
331       else
332         goto bad_address;
333     }
334   
335   dbus_address_entries_free (entries);
336   return server;
337
338  bad_address:
339   dbus_address_entries_free (entries);
340   dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
341
342   return NULL;
343 }
344
345 /**
346  * Increments the reference count of a DBusServer.
347  *
348  * @param server the server.
349  */
350 void
351 dbus_server_ref (DBusServer *server)
352 {
353   server->refcount += 1;
354 }
355
356 /**
357  * Decrements the reference count of a DBusServer.  Finalizes the
358  * server if the reference count reaches zero. The server connection
359  * will be closed as with dbus_server_disconnect() when the server is
360  * finalized.
361  *
362  * @param server the server.
363  */
364 void
365 dbus_server_unref (DBusServer *server)
366 {
367   _dbus_assert (server != NULL);
368   _dbus_assert (server->refcount > 0);
369
370   server->refcount -= 1;
371   if (server->refcount == 0)
372     {
373       _dbus_assert (server->vtable->finalize != NULL);
374       
375       (* server->vtable->finalize) (server);
376     }
377 }
378
379 /**
380  * Releases the server's address and stops listening for
381  * new clients. If called more than once, only the first
382  * call has an effect. Does not modify the server's
383  * reference count.
384  * 
385  * @param server the server.
386  */
387 void
388 dbus_server_disconnect (DBusServer *server)
389 {
390   _dbus_assert (server->vtable->disconnect != NULL);
391
392   if (server->disconnected)
393     return;
394   
395   (* server->vtable->disconnect) (server);
396   server->disconnected = TRUE;
397 }
398
399 /**
400  * Returns #TRUE if the server is still listening for new connections.
401  *
402  * @param server the server.
403  */
404 dbus_bool_t
405 dbus_server_get_is_connected (DBusServer *server)
406 {
407   return !server->disconnected;
408 }
409
410 /**
411  * Sets a function to be used for handling new connections.  The given
412  * function is passed each new connection as the connection is
413  * created. If the new connection function increments the connection's
414  * reference count, the connection will stay alive. Otherwise, the
415  * connection will be unreferenced and closed.
416  *
417  * @param server the server.
418  * @param function a function to handle new connections.
419  * @param data data to pass to the new connection handler.
420  * @param free_data_function function to free the data.
421  */
422 void
423 dbus_server_set_new_connection_function (DBusServer                *server,
424                                          DBusNewConnectionFunction  function,
425                                          void                      *data,
426                                          DBusFreeFunction           free_data_function)
427 {
428   if (server->new_connection_free_data_function != NULL)
429     (* server->new_connection_free_data_function) (server->new_connection_data);
430   
431   server->new_connection_function = function;
432   server->new_connection_data = data;
433   server->new_connection_free_data_function = free_data_function;
434 }
435
436 /**
437  * Sets the watch functions for the connection. These functions are
438  * responsible for making the application's main loop aware of file
439  * descriptors that need to be monitored for events.
440  *
441  * This function behaves exactly like dbus_connection_set_watch_functions();
442  * see the documentation for that routine.
443  *
444  * @param server the server.
445  * @param add_function function to begin monitoring a new descriptor.
446  * @param remove_function function to stop monitoring a descriptor.
447  * @param toggled_function function to notify when the watch is enabled/disabled
448  * @param data data to pass to add_function and remove_function.
449  * @param free_data_function function to be called to free the data.
450  * @returns #FALSE on failure (no memory)
451  */
452 dbus_bool_t
453 dbus_server_set_watch_functions (DBusServer              *server,
454                                  DBusAddWatchFunction     add_function,
455                                  DBusRemoveWatchFunction  remove_function,
456                                  DBusWatchToggledFunction toggled_function,
457                                  void                    *data,
458                                  DBusFreeFunction         free_data_function)
459 {
460   return _dbus_watch_list_set_functions (server->watches,
461                                          add_function,
462                                          remove_function,
463                                          toggled_function,
464                                          data,
465                                          free_data_function);
466 }
467
468 /**
469  * Sets the timeout functions for the connection. These functions are
470  * responsible for making the application's main loop aware of timeouts.
471  *
472  * This function behaves exactly like dbus_connection_set_timeout_functions();
473  * see the documentation for that routine.
474  *
475  * @param server the server.
476  * @param add_function function to add a timeout.
477  * @param remove_function function to remove a timeout.
478  * @param toggled_function function to notify when the timeout is enabled/disabled
479  * @param data data to pass to add_function and remove_function.
480  * @param free_data_function function to be called to free the data.
481  * @returns #FALSE on failure (no memory)
482  */
483 dbus_bool_t
484 dbus_server_set_timeout_functions (DBusServer                *server,
485                                    DBusAddTimeoutFunction     add_function,
486                                    DBusRemoveTimeoutFunction  remove_function,
487                                    DBusTimeoutToggledFunction toggled_function,
488                                    void                      *data,
489                                    DBusFreeFunction           free_data_function)
490 {
491   return _dbus_timeout_list_set_functions (server->timeouts,
492                                            add_function, remove_function,
493                                            toggled_function,
494                                            data, free_data_function); 
495 }
496
497 /**
498  * Called to notify the server when a previously-added watch
499  * is ready for reading or writing, or has an exception such
500  * as a hangup.
501  * 
502  * If this function returns #FALSE, then the file descriptor may still
503  * be ready for reading or writing, but more memory is needed in order
504  * to do the reading or writing. If you ignore the #FALSE return, your
505  * application may spin in a busy loop on the file descriptor until
506  * memory becomes available, but nothing more catastrophic should
507  * happen.
508  *
509  * @param server the server.
510  * @param watch the watch.
511  * @param condition the current condition of the file descriptors being watched.
512  */
513 dbus_bool_t
514 dbus_server_handle_watch (DBusServer              *server,
515                           DBusWatch               *watch,
516                           unsigned int             condition)
517 {
518   _dbus_assert (server->vtable->handle_watch != NULL);
519
520   _dbus_watch_sanitize_condition (watch, &condition);
521   
522   return (* server->vtable->handle_watch) (server, watch, condition);
523 }
524
525 /**
526  * Sets the maximum number of connections that can be open at one
527  * time for this server. If the maximum is reached, and another
528  * client tries to connect, then the oldest unauthenticated client
529  * will be dropped. If no unauthenticated client exists, then
530  * the new connection will be refused.
531  *
532  * If the maximum is set to a number lower than the current
533  * number of connections, no current connections are
534  * disconnected.
535  *
536  * @todo honoring max_connections has not been implemented
537  * yet. The only real work involved is keeping a list
538  * of live connections on the DBusServer so the oldest
539  * unauthenticated client can be located when required.
540  * 
541  * @todo for a systemwide daemon, we need a max number of connections
542  * per user, since any user can authenticate a bunch of connections
543  * and create a DOS.
544  *
545  * @todo a single process might listen on multiple mechanisms
546  * (multiple DBusServer) and might want the max connections
547  * value to span all those servers. Should consider
548  * changing the API accordingly, though I'm inclined to
549  * punt this to the app that wants to do it instead of
550  * putting it in the library.
551  * 
552  * @param server the server
553  * @param max_connections maximum number of connections allowed
554  */
555 void
556 dbus_server_set_max_connections (DBusServer *server,
557                                  int         max_connections)
558 {
559   server->max_connections = max_connections;
560 }
561
562 /**
563  * Gets the maximum number of connections that can be active
564  * at a time for this server.
565  *
566  * @param server the server
567  * @returns maximum number of connections at once
568  */
569 int
570 dbus_server_get_max_connections (DBusServer *server)
571 {
572   return server->max_connections;
573 }
574
575 /**
576  * Gets the number of #DBusConnection to this server that
577  * have not yet been finalized. i.e. all #DBusConnection that
578  * were passed to #DBusNewConnectionFunction and have not yet been
579  * finalized will count in this total.
580  *
581  * @param server the server
582  * @returns the number of connections
583  */
584 int
585 dbus_server_get_n_connections (DBusServer *server)
586 {
587   return _dbus_counter_get_value (server->connection_counter);
588 }
589
590
591 static DBusDataSlotAllocator slot_allocator;
592
593 /**
594  * Initialize the mutex used for #DBusConnection data
595  * slot reservations.
596  *
597  * @returns the mutex
598  */
599 DBusMutex *
600 _dbus_server_slots_init_lock (void)
601 {
602   if (!_dbus_data_slot_allocator_init (&slot_allocator))
603     return NULL;
604   else
605     return slot_allocator.lock;
606 }
607
608 /**
609  * Allocates an integer ID to be used for storing application-specific
610  * data on any DBusServer. The allocated ID may then be used
611  * with dbus_server_set_data() and dbus_server_get_data().
612  * If allocation fails, -1 is returned. Again, the allocated
613  * slot is global, i.e. all DBusServer objects will
614  * have a slot with the given integer ID reserved.
615  *
616  * @returns -1 on failure, otherwise the data slot ID
617  */
618 int
619 dbus_server_allocate_data_slot (void)
620 {
621   return _dbus_data_slot_allocator_alloc (&slot_allocator);
622 }
623
624 /**
625  * Deallocates a global ID for server data slots.
626  * dbus_server_get_data() and dbus_server_set_data()
627  * may no longer be used with this slot.
628  * Existing data stored on existing DBusServer objects
629  * will be freed when the server is finalized,
630  * but may not be retrieved (and may only be replaced
631  * if someone else reallocates the slot).
632  *
633  * @param slot the slot to deallocate
634  */
635 void
636 dbus_server_free_data_slot (int slot)
637 {
638   _dbus_data_slot_allocator_free (&slot_allocator, slot);
639 }
640
641 /**
642  * Stores a pointer on a DBusServer, along
643  * with an optional function to be used for freeing
644  * the data when the data is set again, or when
645  * the server is finalized. The slot number
646  * must have been allocated with dbus_server_allocate_data_slot().
647  *
648  * @param server the server
649  * @param slot the slot number
650  * @param data the data to store
651  * @param free_data_func finalizer function for the data
652  * @returns #TRUE if there was enough memory to store the data
653  */
654 dbus_bool_t
655 dbus_server_set_data (DBusServer   *server,
656                       int               slot,
657                       void             *data,
658                       DBusFreeFunction  free_data_func)
659 {
660   DBusFreeFunction old_free_func;
661   void *old_data;
662   dbus_bool_t retval;
663
664 #if 0
665   dbus_mutex_lock (server->mutex);
666 #endif
667   
668   retval = _dbus_data_slot_list_set (&slot_allocator,
669                                      &server->slot_list,
670                                      slot, data, free_data_func,
671                                      &old_free_func, &old_data);
672
673 #if 0
674   dbus_mutex_unlock (server->mutex);
675 #endif
676   
677   if (retval)
678     {
679       /* Do the actual free outside the server lock */
680       if (old_free_func)
681         (* old_free_func) (old_data);
682     }
683
684   return retval;
685 }
686
687 /**
688  * Retrieves data previously set with dbus_server_set_data().
689  * The slot must still be allocated (must not have been freed).
690  *
691  * @param server the server
692  * @param slot the slot to get data from
693  * @returns the data, or #NULL if not found
694  */
695 void*
696 dbus_server_get_data (DBusServer   *server,
697                       int               slot)
698 {
699   void *res;
700   
701 #if 0
702   dbus_mutex_lock (server->mutex);
703 #endif
704   
705   res = _dbus_data_slot_list_get (&slot_allocator,
706                                   &server->slot_list,
707                                   slot);
708
709 #if 0
710   dbus_mutex_unlock (server->mutex);
711 #endif
712   
713   return res;
714 }
715
716 /** @} */
717