main.c 7.3 KB

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