Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

main.c

00001 /***************************************************************************
00002  * CVSID: $Id: main.c,v 1.20 2004/05/25 21:20:24 stefanb Exp $
00003  *
00004  * main.c : main() for MNT daemon
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <stefan@bambach.biz>
00007  *
00008  * Licensed under the GNU General Public License 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025  
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include <libmnt/libmnt.h>   /* for common defines etc. */
00035 
00036 #define _GNU_SOURCE
00037 #include <getopt.h>
00038 
00039 #include "errmanager.h"
00040 #include "mntd_hal.h"
00041 #include "mntd_file.h"
00042 #include "mntd_dbus_manager.h"
00043 #include "mntd_volume_manager.h"
00044 #include "main.h"
00045 
00046 /* global pointer to volume manager */
00047 PVOLUMEMANAGER vols = NULL;
00048 
00049 void usage(void);
00050 
00051 
00062 void 
00063 usage(void)
00064 {
00065     printf("Mount Daemon " PACKAGE_VERSION "\n");
00066     printf("Mounts devices automatically and unmount it again. It's meant\n");
00067     printf("to handle hotplug devices like USB storage or similar stuff.\n");
00068     printf("\n");
00069     printf("usage: mntd [-h] [-d] [-f configfile]\n");
00070     printf("\n");
00071     printf("    -h              Help (this text)\n");
00072     printf("    -d              Daemonize it\n");
00073     printf("    -f,--file       Use given configfile\n");
00074     printf("\n");
00075 }
00076 
00077 
00084 int main(int argc, char* argv[])
00085 {
00086     GMainLoop* loop;
00087     char *config = NULL;
00088     int i=0;
00089     char *configfile = NULL;
00090     char *configfiles[] = {
00091         MNTD_CONF_DIR,
00092         "/etc/mntd/mntd.conf",
00093         "/etc/mntd.conf",
00094         "./mntd.conf",
00095         "../mntd.conf",
00096         NULL
00097     };
00098     int daemonize = FALSE;
00099     
00100     // parse commandline options
00101     while(1) {
00102         int c;
00103         int option_index = 0;
00104         static struct option long_options[] = {
00105             {"help", no_argument, 0, 'h'},
00106             {"daemonize", no_argument, 0, 'd'},
00107             {"file", required_argument, 0, 'f'},
00108             {0, 0, 0, 0}
00109         };
00110         c = getopt_long (argc, argv, "hf:d", long_options, &option_index);
00111         if (c == -1) {
00112             break;
00113         }
00114         switch (c) {
00115         case 'd':
00116             daemonize = TRUE;
00117             break;
00118         
00119         case 'f':
00120             config = optarg;
00121             break;
00122         
00123         case 'h':
00124             usage();
00125             exit(0);
00126             break;
00127         
00128         default:
00129             usage();
00130             exit(1);
00131             break;
00132         }
00133     }
00134     
00135     // initialize error manager
00136     emInit(LOG_DEBUG, EM_TYPE_SYSLOG, NULL, NULL, NULL);
00137     
00138     // show version
00139     MSG_INF("MNT daemon version " PACKAGE_VERSION " starting up");
00140     
00141     // try to load config file
00142     if (config == NULL) {
00143         MSG_DEBUG("No config file specified. trying to find it.");
00144         i=0;
00145         while (configfiles[i]!=NULL) {
00146             configfile = configfiles[i];
00147             MSG_DEBUG("trying '%s' ...", configfile);
00148             if (mntd_file_is_file(configfile)) {
00149                 MSG_DEBUG("found '%s'", configfile);
00150                 config = configfile;
00151                 break;
00152             }
00153             i++;
00154         }
00155     }
00156     
00157     if (config == NULL) {
00158         MSG_EMERG("No config file given and nothing found. exiting.");
00159         exit(1);
00160     }
00161     if (!mntd_file_is_file(config)) {
00162         MSG_EMERG("Config file '%s' cannot be accessed.", config);
00163         exit(1);
00164     }
00165     
00166     // handle daemon mode
00167     if (daemonize==TRUE) {
00168         MSG_INF("daemonize not implemented yet -> ignored");
00169     }
00170     
00171     // start volume manager
00172     vols = new_VolumeManager(config);
00173     if (vols == NULL) {
00174         MSG_EMERG("Couldn't not initialize system.");
00175         exit(1);
00176     }
00177     
00178     loop = g_main_loop_new(NULL, FALSE);
00179     
00180     // initialize my dbus stuff
00181     mntd_dbus_init(vols);
00182     
00183     // start HAL handler
00184     mntd_hal_init();
00185     
00186     // Scan for Volumes
00187     vols->rescan(vols);
00188     
00189     // run the main loop
00190     g_main_loop_run(loop);
00191     
00192     // destroy HAL handler
00193     mntd_hal_shutdown();
00194     
00195     // destroy dbus handler
00196     mntd_dbus_quit();
00197     
00198     // free volume storage
00199     g_assert(vols!=NULL);
00200     vols->destroy(vols);
00201     
00202     // MNT daemon exists
00203     MSG_INF("MNT daemon version " PACKAGE_VERSION " exited.");
00204 
00205     return 0;
00206 }
00207 

Generated on Thu May 27 23:27:28 2004 for Mntd by doxygen 1.3.5