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
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"
00094 "V"
00095 "d"
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");
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 if (daemon(0, 0)<0)
00158 die("daemon() failed");
00159 }
00160
00161 write_pid(PIDFILE);
00162 atexit(pid_cleanup);
00163
00164 event_loop = g_main_loop_new(NULL, FALSE);
00165
00166 if (setup_dbus_connection(WLANCOND_SERVICE, init_dbus_handlers) < 0) {
00167 die("D-BUS connection setup failed!");
00168 }
00169
00170 if (signal(SIGINT, signal_exit) == SIG_ERR) {
00171 die("signal(SIGINT) failed");
00172 }
00173 if (signal(SIGTERM, signal_exit) == SIG_ERR) {
00174 die("signal(SIGTERM) failed");
00175 }
00176
00177
00178 if (get_we_device_name() < 0) {
00179 DLOG_ERR("No device supporting wireless extensions? Exiting.");
00180 exit(EXIT_FAILURE);
00181 }
00182
00183 if (init_dbus_handler() < 0) {
00184 exit(EXIT_FAILURE);
00185 }
00186
00187 DLOG_INFO("WLAN Connection Daemon %s started.", VERSION);
00188
00189 if (monitor_wi() != TRUE) {
00190 DLOG_ERR("Could not listen any wireless interface");
00191 exit(EXIT_FAILURE);
00192 }
00193
00194 gchar *mode = get_device_mode(get_dbus_connection());
00195
00196 if (mode != NULL) {
00197 mode_change(mode);
00198 g_free(mode);
00199 } else {
00200 DLOG_ERR("Unable to determine device mode. Assuming normal mode.");
00201 mode_change("normal");
00202 }
00203 #ifdef USE_MCE_COVER
00204 init_cover_state();
00205 #endif
00206
00207 g_main_loop_run(event_loop);
00208
00209 set_wlan_state(WLAN_NOT_INITIALIZED, DISCONNECTED_SIGNAL, FORCE_YES);
00210
00211 destroy_dbus_handlers(get_dbus_connection());
00212 close_dbus_connection();
00213 g_main_loop_unref(event_loop);
00214
00215 clean_data();
00216
00217 DLOG_INFO("Exiting.");
00218 LOG_CLOSE();
00219
00220 exit(EXIT_SUCCESS);
00221 }
00222