main.c

Go to the documentation of this file.
00001 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <stdint.h>
00026 #include <string.h>
00027 #include <getopt.h>
00028 #include <signal.h>
00029 #include <glib.h>
00030 #include <glib-object.h>
00031 #include <unistd.h>
00032 #include <wlancond-dbus.h>
00033 
00034 #include "dbus-helper.h"
00035 
00036 #include "daemon.h"
00037 #include "common.h"
00038 #include "dbus.h"
00039 #include "dbus-handler.h"
00040 #include "log.h"
00041 
00042 #define PIDFILE "/var/run/wlancond.pid"
00043 
00044 #if WIRELESS_EXT < 21
00045 #error "Wireless extensions are too old for this software"
00046 #endif
00047 
00048 static char *program_name;
00049 static gboolean daemon_mode = FALSE;
00050 static GMainLoop *event_loop = NULL;
00051 
00052 /* For getopt */
00053 static struct option const long_options[] = {
00054         {"help",     no_argument,       0, 'h'},
00055         {"version",  no_argument,       0, 'V'},
00056         {"daemon",   no_argument,       0, 'd'},
00057         {NULL, 0, NULL, 0}
00058 };
00059 static void usage(int status) __attribute__ ((noreturn));
00063 static void usage(int status) {
00064         printf("%s - WLAN Connection Deamon %s\n", program_name, VERSION);
00065         
00066         printf("Compilation flags: ");
00067 #ifdef DEBUG
00068         printf("+DEBUG ");
00069 #else
00070         printf("-DEBUG ");
00071 #endif
00072         
00073         printf(
00074                 "\nUsage: %s [OPTION]...\n"
00075                 "Options:\n"
00076                 "-h, --help                 Display this help and exit\n"
00077                 "-V, --version              Output version information and exit\n"
00078                 "-d, --daemon               Send process to the background\n"
00079                 "\n", program_name);
00080         
00081         exit(status);
00082 }
00083 
00088 static int decode_switches(int argc, char *argv[])
00089 {
00090         int c;
00091         
00092         while ((c = getopt_long(argc, argv,
00093                                 "h"   /* help      */
00094                                 "V"   /* version   */
00095                                 "d"   /* daemon    */
00096                                 ,long_options, (int *) 0)) != EOF) {
00097                 switch (c) {
00098                     case 'V':
00099                             printf("WLAN Connection Daemon %s\n", VERSION);
00100                             exit(EXIT_SUCCESS);
00101 
00102                     case 'h':
00103                             usage(EXIT_SUCCESS);
00104                             break;
00105                     case 'd':
00106                             daemon_mode = TRUE;
00107                             break;
00108                     default:
00109                             usage(EXIT_FAILURE);
00110                 }
00111         }
00112         
00113         return optind;
00114 }
00115 
00116 static void clean_data(void) 
00117 {
00118         del_all_interface_data();
00119         clean_dbus_handler();
00120 }
00121 
00125 static void signal_exit(int sig) {
00126         DLOG_INFO("Signal received: %s.", strsignal(sig));
00127         g_main_loop_quit(event_loop);
00128 }
00129 
00133 static void pid_cleanup(void) {
00134         (void) remove_pid(PIDFILE);
00135 }
00136 
00141 int main(int argc, char *argv[]) {
00142         int i, old_pid;
00143         
00144         g_type_init();
00145         
00146         program_name = argv[0];
00147         i = decode_switches(argc, argv);
00148         
00149         DLOG_OPEN("wlancond" " " VERSION);
00150         
00151         old_pid = check_pid(PIDFILE);
00152         if (old_pid) {
00153                 die("Unable to run: another instance running (PID %d)", old_pid);
00154         }
00155         
00156         if (daemon_mode) {
00157                 daemon(0, 0);
00158         }
00159         
00160         write_pid(PIDFILE);
00161         atexit(pid_cleanup);
00162         
00163         event_loop = g_main_loop_new(NULL, FALSE);
00164         
00165         if (setup_dbus_connection(WLANCOND_SERVICE, init_dbus_handlers) < 0) {
00166                 die("D-BUS connection setup failed!");
00167         }
00168         
00169         if (signal(SIGINT, signal_exit) == SIG_ERR) {
00170                 die("signal(SIGINT) failed");
00171         }
00172         if (signal(SIGTERM, signal_exit) == SIG_ERR) {
00173                 die("signal(SIGTERM) failed");
00174         }
00175         
00176         /* Assume there is only one wireless device */
00177         if (get_we_device_name() < 0) {
00178                 DLOG_ERR("No device supporting wireless extensions? Exiting.");
00179                 exit(EXIT_FAILURE);
00180         }
00181 
00182         if (init_dbus_handler() < 0) {
00183                 exit(EXIT_FAILURE);
00184         }
00185         
00186         DLOG_INFO("WLAN Connection Daemon %s started.", VERSION);
00187         
00188         if (monitor_wi() != TRUE) {
00189                 DLOG_ERR("Could not listen any wireless interface");
00190                 exit(EXIT_FAILURE);
00191         }
00192         
00193         gchar *mode = get_device_mode(get_dbus_connection());
00194         
00195         if (mode != NULL) {
00196                 mode_change(mode);
00197                 g_free(mode);
00198         } else {
00199                 DLOG_ERR("Unable to determine device mode. Assuming normal mode.");
00200                 mode_change("normal");
00201         }
00202 #ifdef USE_MCE_COVER
00203         init_cover_state();
00204 #endif
00205         /* Enter main loop */
00206         g_main_loop_run(event_loop);
00207 
00208         set_wlan_state(WLAN_NOT_INITIALIZED, DISCONNECTED_SIGNAL, FORCE_YES);
00209 
00210         destroy_dbus_handlers(get_dbus_connection());
00211         close_dbus_connection();
00212         g_main_loop_unref(event_loop);
00213 
00214         clean_data();
00215 
00216         DLOG_INFO("Exiting.");
00217         LOG_CLOSE();
00218         
00219         exit(EXIT_SUCCESS);
00220 }
00221 

Generated on Sat Dec 6 22:17:00 2008 for WLAN Connection Daemon by  doxygen 1.5.1