Main Page   Modules   Data Structures   File List   Data Fields   Related Pages  

main.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* main.c  main() for message bus
00003  *
00004  * Copyright (C) 2003 Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 1.2
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 #include "bus.h"
00024 #include <dbus/dbus-internals.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <signal.h>
00029 #include <errno.h>
00030 
00031 static BusContext *context;
00032 static dbus_bool_t got_sighup = FALSE;
00033 
00034 static void
00035 signal_handler (int sig)
00036 {
00037   switch (sig)
00038     {
00039     case SIGHUP:
00040       got_sighup = TRUE;
00041     case SIGTERM:
00042       _dbus_loop_quit (bus_context_get_loop (context));
00043       break;
00044     }
00045 }
00046 
00047 static void
00048 usage (void)
00049 {
00050   fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n");
00051   exit (1);
00052 }
00053 
00054 static void
00055 version (void)
00056 {
00057   printf ("D-BUS Message Bus Daemon %s\n"
00058           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
00059           "This is free software; see the source for copying conditions.\n"
00060           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
00061           VERSION);
00062   exit (0);
00063 }
00064 
00065 static void
00066 check_two_config_files (const DBusString *config_file,
00067                         const char       *extra_arg)
00068 {
00069   if (_dbus_string_get_length (config_file) > 0)
00070     {
00071       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
00072                extra_arg, _dbus_string_get_const_data (config_file));
00073       exit (1);
00074     }
00075 }
00076 
00077 static void
00078 check_two_addr_descriptors (const DBusString *addr_fd,
00079                             const char       *extra_arg)
00080 {
00081   if (_dbus_string_get_length (addr_fd) > 0)
00082     {
00083       fprintf (stderr, "--%s specified but printing address to %s already requested\n",
00084                extra_arg, _dbus_string_get_const_data (addr_fd));
00085       exit (1);
00086     }
00087 }
00088 
00089 static void
00090 check_two_pid_descriptors (const DBusString *pid_fd,
00091                            const char       *extra_arg)
00092 {
00093   if (_dbus_string_get_length (pid_fd) > 0)
00094     {
00095       fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
00096                extra_arg, _dbus_string_get_const_data (pid_fd));
00097       exit (1);
00098     }
00099 }
00100 
00101 int
00102 main (int argc, char **argv)
00103 {
00104   DBusError error;
00105   DBusString config_file;
00106   DBusString addr_fd;
00107   DBusString pid_fd;
00108   const char *prev_arg;
00109   int print_addr_fd;
00110   int print_pid_fd;
00111   int i;
00112   dbus_bool_t print_address;
00113   dbus_bool_t print_pid;
00114   dbus_bool_t force_fork;
00115   
00116   if (!_dbus_string_init (&config_file))
00117     return 1;
00118 
00119   if (!_dbus_string_init (&addr_fd))
00120     return 1;
00121 
00122   if (!_dbus_string_init (&pid_fd))
00123     return 1;
00124   
00125   print_address = FALSE;
00126   print_pid = FALSE;
00127   force_fork = FALSE;
00128   
00129   prev_arg = NULL;
00130   i = 1;
00131   while (i < argc)
00132     {
00133       const char *arg = argv[i];
00134       
00135       if (strcmp (arg, "--help") == 0 ||
00136           strcmp (arg, "-h") == 0 ||
00137           strcmp (arg, "-?") == 0)
00138         usage ();
00139       else if (strcmp (arg, "--version") == 0)
00140         version ();
00141       else if (strcmp (arg, "--fork") == 0)
00142         force_fork = TRUE;
00143       else if (strcmp (arg, "--system") == 0)
00144         {
00145           check_two_config_files (&config_file, "system");
00146 
00147           if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
00148             exit (1);
00149         }
00150       else if (strcmp (arg, "--session") == 0)
00151         {
00152           check_two_config_files (&config_file, "session");
00153 
00154           if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
00155             exit (1);
00156         }
00157       else if (strstr (arg, "--config-file=") == arg)
00158         {
00159           const char *file;
00160 
00161           check_two_config_files (&config_file, "config-file");
00162           
00163           file = strchr (arg, '=');
00164           ++file;
00165 
00166           if (!_dbus_string_append (&config_file, file))
00167             exit (1);
00168         }
00169       else if (prev_arg &&
00170                strcmp (prev_arg, "--config-file") == 0)
00171         {
00172           check_two_config_files (&config_file, "config-file");
00173           
00174           if (!_dbus_string_append (&config_file, arg))
00175             exit (1);
00176         }
00177       else if (strcmp (arg, "--config-file") == 0)
00178         ; /* wait for next arg */
00179       else if (strstr (arg, "--print-address=") == arg)
00180         {
00181           const char *desc;
00182 
00183           check_two_addr_descriptors (&addr_fd, "print-address");
00184           
00185           desc = strchr (arg, '=');
00186           ++desc;
00187 
00188           if (!_dbus_string_append (&addr_fd, desc))
00189             exit (1);
00190 
00191           print_address = TRUE;
00192         }
00193       else if (prev_arg &&
00194                strcmp (prev_arg, "--print-address") == 0)
00195         {
00196           check_two_addr_descriptors (&addr_fd, "print-address");
00197           
00198           if (!_dbus_string_append (&addr_fd, arg))
00199             exit (1);
00200 
00201           print_address = TRUE;
00202         }
00203       else if (strcmp (arg, "--print-address") == 0)
00204         print_address = TRUE; /* and we'll get the next arg if appropriate */
00205       else if (strstr (arg, "--print-pid=") == arg)
00206         {
00207           const char *desc;
00208 
00209           check_two_pid_descriptors (&pid_fd, "print-pid");
00210           
00211           desc = strchr (arg, '=');
00212           ++desc;
00213 
00214           if (!_dbus_string_append (&pid_fd, desc))
00215             exit (1);
00216 
00217           print_pid = TRUE;
00218         }
00219       else if (prev_arg &&
00220                strcmp (prev_arg, "--print-pid") == 0)
00221         {
00222           check_two_pid_descriptors (&pid_fd, "print-pid");
00223           
00224           if (!_dbus_string_append (&pid_fd, arg))
00225             exit (1);
00226           
00227           print_pid = TRUE;
00228         }
00229       else if (strcmp (arg, "--print-pid") == 0)
00230         print_pid = TRUE; /* and we'll get the next arg if appropriate */
00231       else
00232         usage ();
00233       
00234       prev_arg = arg;
00235       
00236       ++i;
00237     }
00238 
00239   if (_dbus_string_get_length (&config_file) == 0)
00240     {
00241       fprintf (stderr, "No configuration file specified.\n");
00242       usage ();
00243     }
00244 
00245   print_addr_fd = -1;
00246   if (print_address)
00247     {
00248       print_addr_fd = 1; /* stdout */
00249       if (_dbus_string_get_length (&addr_fd) > 0)
00250         {
00251           long val;
00252           int end;
00253           if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
00254               end != _dbus_string_get_length (&addr_fd) ||
00255               val < 0 || val > _DBUS_INT_MAX)
00256             {
00257               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
00258                        _dbus_string_get_const_data (&addr_fd));
00259               exit (1);
00260             }
00261 
00262           print_addr_fd = val;
00263         }
00264     }
00265 
00266   print_pid_fd = -1;
00267   if (print_pid)
00268     {
00269       print_pid_fd = 1; /* stdout */
00270       if (_dbus_string_get_length (&pid_fd) > 0)
00271         {
00272           long val;
00273           int end;
00274           if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
00275               end != _dbus_string_get_length (&pid_fd) ||
00276               val < 0 || val > _DBUS_INT_MAX)
00277             {
00278               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
00279                        _dbus_string_get_const_data (&pid_fd));
00280               exit (1);
00281             }
00282 
00283           print_pid_fd = val;
00284         }
00285     }
00286   
00287   dbus_error_init (&error);
00288   context = bus_context_new (&config_file, force_fork,
00289                              print_addr_fd, print_pid_fd,
00290                              &error);
00291   _dbus_string_free (&config_file);
00292   if (context == NULL)
00293     {
00294       _dbus_warn ("Failed to start message bus: %s\n",
00295                   error.message);
00296       dbus_error_free (&error);
00297       exit (1);
00298     }
00299   
00300   /* FIXME we have to handle this properly below _dbus_set_signal_handler (SIGHUP, signal_handler); */
00301   _dbus_set_signal_handler (SIGTERM, signal_handler);
00302   
00303   _dbus_verbose ("We are on D-Bus...\n");
00304   _dbus_loop_run (bus_context_get_loop (context));
00305   
00306   bus_context_shutdown (context);
00307   bus_context_unref (context);
00308 
00309   /* If we exited on TERM we just exit, if we exited on
00310    * HUP we restart the daemon.
00311    */
00312   if (got_sighup)
00313     {
00314       /* FIXME execv (argv) basically */
00315     }
00316   
00317   return 0;
00318 }

Generated on Wed Oct 22 14:05:06 2003 for D-BUS by doxygen1.3-rc3