main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * WPA Supplicant / main() function for UNIX like OSes and MinGW
  3. * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifdef __linux__
  16. #include <fcntl.h>
  17. #endif /* __linux__ */
  18. #include "common.h"
  19. #include "wpa_supplicant_i.h"
  20. static void usage(void)
  21. {
  22. int i;
  23. printf("%s\n\n%s\n"
  24. "usage:\n"
  25. " wpa_supplicant [-BddhKLqqtuvW] [-P<pid file>] "
  26. "[-g<global ctrl>] \\\n"
  27. " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
  28. "[-p<driver_param>] \\\n"
  29. " [-b<br_ifname>] [-f<debug file>] \\\n"
  30. " [-N -i<ifname> -c<conf> [-C<ctrl>] "
  31. "[-D<driver>] \\\n"
  32. " [-p<driver_param>] [-b<br_ifname>] ...]\n"
  33. "\n"
  34. "drivers:\n",
  35. wpa_supplicant_version, wpa_supplicant_license);
  36. for (i = 0; wpa_supplicant_drivers[i]; i++) {
  37. printf(" %s = %s\n",
  38. wpa_supplicant_drivers[i]->name,
  39. wpa_supplicant_drivers[i]->desc);
  40. }
  41. #ifndef CONFIG_NO_STDOUT_DEBUG
  42. printf("options:\n"
  43. " -b = optional bridge interface name\n"
  44. " -B = run daemon in the background\n"
  45. " -c = Configuration file\n"
  46. " -C = ctrl_interface parameter (only used if -c is not)\n"
  47. " -i = interface name\n"
  48. " -d = increase debugging verbosity (-dd even more)\n"
  49. " -D = driver name (can be multiple drivers: nl80211,wext)\n");
  50. #ifdef CONFIG_DEBUG_FILE
  51. printf(" -f = log output to debug file instead of stdout\n");
  52. #endif /* CONFIG_DEBUG_FILE */
  53. printf(" -g = global ctrl_interface\n"
  54. " -K = include keys (passwords, etc.) in debug output\n"
  55. " -t = include timestamp in debug messages\n"
  56. " -h = show this help text\n"
  57. " -L = show license (GPL and BSD)\n"
  58. " -p = driver parameters\n"
  59. " -P = PID file\n"
  60. " -q = decrease debugging verbosity (-qq even less)\n");
  61. #ifdef CONFIG_CTRL_IFACE_DBUS
  62. printf(" -u = enable DBus control interface\n");
  63. #endif /* CONFIG_CTRL_IFACE_DBUS */
  64. printf(" -v = show version\n"
  65. " -W = wait for a control interface monitor before starting\n"
  66. " -N = start describing new interface\n");
  67. printf("example:\n"
  68. " wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
  69. #endif /* CONFIG_NO_STDOUT_DEBUG */
  70. }
  71. static void license(void)
  72. {
  73. #ifndef CONFIG_NO_STDOUT_DEBUG
  74. printf("%s\n\n%s%s%s%s%s\n",
  75. wpa_supplicant_version,
  76. wpa_supplicant_full_license1,
  77. wpa_supplicant_full_license2,
  78. wpa_supplicant_full_license3,
  79. wpa_supplicant_full_license4,
  80. wpa_supplicant_full_license5);
  81. #endif /* CONFIG_NO_STDOUT_DEBUG */
  82. }
  83. static void wpa_supplicant_fd_workaround(void)
  84. {
  85. #ifdef __linux__
  86. int s, i;
  87. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  88. * fd 0, 1, and 2 closed. This will cause some issues because many
  89. * places in wpa_supplicant are still printing out to stdout. As a
  90. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  91. * sockets. */
  92. for (i = 0; i < 3; i++) {
  93. s = open("/dev/null", O_RDWR);
  94. if (s > 2) {
  95. close(s);
  96. break;
  97. }
  98. }
  99. #endif /* __linux__ */
  100. }
  101. int main(int argc, char *argv[])
  102. {
  103. int c, i;
  104. struct wpa_interface *ifaces, *iface;
  105. int iface_count, exitcode = -1;
  106. struct wpa_params params;
  107. struct wpa_global *global;
  108. if (os_program_init())
  109. return -1;
  110. os_memset(&params, 0, sizeof(params));
  111. params.wpa_debug_level = MSG_INFO;
  112. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  113. if (ifaces == NULL)
  114. return -1;
  115. iface_count = 1;
  116. wpa_supplicant_fd_workaround();
  117. for (;;) {
  118. c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qtuvW");
  119. if (c < 0)
  120. break;
  121. switch (c) {
  122. case 'b':
  123. iface->bridge_ifname = optarg;
  124. break;
  125. case 'B':
  126. params.daemonize++;
  127. break;
  128. case 'c':
  129. iface->confname = optarg;
  130. break;
  131. case 'C':
  132. iface->ctrl_interface = optarg;
  133. break;
  134. case 'D':
  135. iface->driver = optarg;
  136. break;
  137. case 'd':
  138. #ifdef CONFIG_NO_STDOUT_DEBUG
  139. printf("Debugging disabled with "
  140. "CONFIG_NO_STDOUT_DEBUG=y build time "
  141. "option.\n");
  142. goto out;
  143. #else /* CONFIG_NO_STDOUT_DEBUG */
  144. params.wpa_debug_level--;
  145. break;
  146. #endif /* CONFIG_NO_STDOUT_DEBUG */
  147. #ifdef CONFIG_DEBUG_FILE
  148. case 'f':
  149. params.wpa_debug_file_path = optarg;
  150. break;
  151. #endif /* CONFIG_DEBUG_FILE */
  152. case 'g':
  153. params.ctrl_interface = optarg;
  154. break;
  155. case 'h':
  156. usage();
  157. exitcode = 0;
  158. goto out;
  159. case 'i':
  160. iface->ifname = optarg;
  161. break;
  162. case 'K':
  163. params.wpa_debug_show_keys++;
  164. break;
  165. case 'L':
  166. license();
  167. exitcode = 0;
  168. goto out;
  169. case 'p':
  170. iface->driver_param = optarg;
  171. break;
  172. case 'P':
  173. os_free(params.pid_file);
  174. params.pid_file = os_rel2abs_path(optarg);
  175. break;
  176. case 'q':
  177. params.wpa_debug_level++;
  178. break;
  179. case 't':
  180. params.wpa_debug_timestamp++;
  181. break;
  182. #ifdef CONFIG_CTRL_IFACE_DBUS
  183. case 'u':
  184. params.dbus_ctrl_interface = 1;
  185. break;
  186. #endif /* CONFIG_CTRL_IFACE_DBUS */
  187. case 'v':
  188. printf("%s\n", wpa_supplicant_version);
  189. exitcode = 0;
  190. goto out;
  191. case 'W':
  192. params.wait_for_monitor++;
  193. break;
  194. case 'N':
  195. iface_count++;
  196. iface = os_realloc(ifaces, iface_count *
  197. sizeof(struct wpa_interface));
  198. if (iface == NULL)
  199. goto out;
  200. ifaces = iface;
  201. iface = &ifaces[iface_count - 1];
  202. os_memset(iface, 0, sizeof(*iface));
  203. break;
  204. default:
  205. usage();
  206. exitcode = 0;
  207. goto out;
  208. }
  209. }
  210. exitcode = 0;
  211. global = wpa_supplicant_init(&params);
  212. if (global == NULL) {
  213. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  214. exitcode = -1;
  215. goto out;
  216. }
  217. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  218. if ((ifaces[i].confname == NULL &&
  219. ifaces[i].ctrl_interface == NULL) ||
  220. ifaces[i].ifname == NULL) {
  221. if (iface_count == 1 && (params.ctrl_interface ||
  222. params.dbus_ctrl_interface))
  223. break;
  224. usage();
  225. exitcode = -1;
  226. break;
  227. }
  228. if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
  229. exitcode = -1;
  230. }
  231. if (exitcode == 0)
  232. exitcode = wpa_supplicant_run(global);
  233. wpa_supplicant_deinit(global);
  234. out:
  235. os_free(ifaces);
  236. os_free(params.pid_file);
  237. os_program_deinit();
  238. return exitcode;
  239. }