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 #include <mce/mode-names.h>
00034
00035 #include "dbus-helper.h"
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 static char *program_name;
00045 static gboolean daemon_mode = FALSE;
00046 static GMainLoop *event_loop = NULL;
00047 extern gint debug_level;
00048
00049
00050 static struct option const long_options[] = {
00051 {"help", no_argument, 0, 'h'},
00052 {"version", no_argument, 0, 'V'},
00053 {"daemon", no_argument, 0, 'd'},
00054 {NULL, 0, NULL, 0}
00055 };
00056 static void usage(int status) __attribute__ ((noreturn));
00060 static void usage(int status) {
00061 printf("%s - WLAN Connection Deamon %s\n", program_name, VERSION);
00062
00063 printf("Compilation flags: ");
00064 #ifdef DEBUG
00065 printf("+DEBUG ");
00066 #else
00067 printf("-DEBUG ");
00068 #endif
00069
00070 printf(
00071 "\nUsage: %s [OPTION]...\n"
00072 "Options:\n"
00073 "-h, --help Display this help and exit\n"
00074 "-V, --version Output version information and exit\n"
00075 "-d, --daemon Send process to the background\n"
00076 "\n", program_name);
00077
00078 exit(status);
00079 }
00080
00085 static int decode_switches(int argc, char *argv[])
00086 {
00087 int c;
00088
00089 while ((c = getopt_long(argc, argv,
00090 "h"
00091 "V"
00092 "d"
00093 ,long_options, (int *) 0)) != EOF) {
00094 switch (c) {
00095 case 'V':
00096 printf("WLAN Connection Daemon %s\n", VERSION);
00097 exit(EXIT_SUCCESS);
00098
00099 case 'h':
00100 usage(EXIT_SUCCESS);
00101 break;
00102 case 'd':
00103 daemon_mode = TRUE;
00104 break;
00105 default:
00106 usage(EXIT_FAILURE);
00107 }
00108 }
00109
00110 return optind;
00111 }
00112
00113 static void clean_data(void)
00114 {
00115 clean_dbus_handler();
00116 if (wlan_status.latency_file)
00117 fclose(wlan_status.latency_file);
00118 }
00119
00123 static void signal_exit(int sig) {
00124 DLOG_INFO("Signal received: %s.", strsignal(sig));
00125
00126 g_main_loop_quit(event_loop);
00127 }
00128
00132 static void signal_debug(int sig) {
00133
00134 if (sig == SIGUSR1) {
00135
00136 debug_level = 2;
00137 }
00138 }
00139
00143 static void pid_cleanup(void) {
00144 (void) remove_pid(PIDFILE);
00145 }
00146
00151 int main(int argc, char *argv[]) {
00152 int i, old_pid, ret;
00153 dbus_uint32_t mode;
00154
00155 g_type_init();
00156
00157 program_name = argv[0];
00158 i = decode_switches(argc, argv);
00159
00160
00161 memset(&wlan_status, 0, sizeof(wlan_status));
00162
00163 init_logging();
00164
00165 if (monitor_iap_keys() != TRUE) {
00166 DLOG_ERR("Unable to monitor iap keys");
00167 }
00168
00169 DLOG_OPEN("wlancond");
00170
00171 old_pid = check_pid(PIDFILE);
00172 if (old_pid) {
00173 die("Unable to run: another instance running (PID %d)",
00174 old_pid);
00175 }
00176
00177 if (daemon_mode) {
00178 if (daemon(0, 0)<0)
00179 die("daemon() failed");
00180 }
00181
00182 write_pid(PIDFILE);
00183 atexit(pid_cleanup);
00184
00185 ret = init_nl80211();
00186 if (ret) {
00187 DLOG_ERR("nl80211: Failed to initialize nl80211.");
00188 exit(EXIT_FAILURE);
00189 }
00190
00191 event_loop = g_main_loop_new(NULL, FALSE);
00192
00193 if (setup_dbus_connection(WLANCOND_SERVICE, init_dbus_handlers) < 0) {
00194 die("D-BUS connection setup failed!");
00195 }
00196
00197 if (signal(SIGINT, signal_exit) == SIG_ERR) {
00198 die("signal(SIGINT) failed");
00199 }
00200 if (signal(SIGTERM, signal_exit) == SIG_ERR) {
00201 die("signal(SIGTERM) failed");
00202 }
00203 if (signal(SIGUSR1, signal_debug) == SIG_ERR) {
00204 die("signal(SIGUSR1) failed");
00205 }
00206
00207
00208 save_device_interface();
00209
00210 if (init_dbus_handler() < 0) {
00211 exit(EXIT_FAILURE);
00212 }
00213
00214 DLOG_INFO("WLAN Connection Daemon %s started.", VERSION);
00215
00216 if (monitor_wi() != TRUE) {
00217 DLOG_ERR("Could not listen any wireless interface");
00218 exit(EXIT_FAILURE);
00219 }
00220
00221 if (get_device_mode(get_dbus_connection(), &mode) != 0) {
00222 DLOG_ERR("Unable to determine device mode. "
00223 "Assuming normal mode.");
00224 mode = MCE_RADIO_STATE_WLAN;
00225 }
00226 mode_change(mode);
00227
00228 #ifdef USE_MCE_COVER
00229 init_cover_state();
00230 #endif
00231
00232 g_main_loop_run(event_loop);
00233
00234 set_wlan_state(WLAN_NOT_INITIALIZED, DISCONNECTED_SIGNAL, FALSE);
00235
00236 destroy_dbus_handlers(get_dbus_connection());
00237 close_dbus_connection();
00238 g_main_loop_unref(event_loop);
00239
00240 nl80211_cleanup();
00241
00242 if (wlan_status.setting != NULL) {
00243 conn_settings_close(wlan_status.setting);
00244 }
00245
00246 clean_data();
00247
00248 DLOG_INFO("Exiting.");
00249 LOG_CLOSE();
00250
00251 exit(EXIT_SUCCESS);
00252 }