main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 [-BddhKLqqstuvW] [-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. #ifdef CONFIG_DEBUG_SYSLOG
  56. printf(" -s = log output to syslog instead of stdout\n");
  57. #endif /* CONFIG_DEBUG_SYSLOG */
  58. printf(" -t = include timestamp in debug messages\n"
  59. " -h = show this help text\n"
  60. " -L = show license (GPL and BSD)\n"
  61. " -p = driver parameters\n"
  62. " -P = PID file\n"
  63. " -q = decrease debugging verbosity (-qq even less)\n");
  64. #ifdef CONFIG_CTRL_IFACE_DBUS
  65. printf(" -u = enable DBus control interface\n");
  66. #endif /* CONFIG_CTRL_IFACE_DBUS */
  67. printf(" -v = show version\n"
  68. " -W = wait for a control interface monitor before starting\n"
  69. " -N = start describing new interface\n");
  70. printf("example:\n"
  71. " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
  72. wpa_supplicant_drivers[i] ?
  73. wpa_supplicant_drivers[i]->name : "wext");
  74. #endif /* CONFIG_NO_STDOUT_DEBUG */
  75. }
  76. static void license(void)
  77. {
  78. #ifndef CONFIG_NO_STDOUT_DEBUG
  79. printf("%s\n\n%s%s%s%s%s\n",
  80. wpa_supplicant_version,
  81. wpa_supplicant_full_license1,
  82. wpa_supplicant_full_license2,
  83. wpa_supplicant_full_license3,
  84. wpa_supplicant_full_license4,
  85. wpa_supplicant_full_license5);
  86. #endif /* CONFIG_NO_STDOUT_DEBUG */
  87. }
  88. static void wpa_supplicant_fd_workaround(void)
  89. {
  90. #ifdef __linux__
  91. int s, i;
  92. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  93. * fd 0, 1, and 2 closed. This will cause some issues because many
  94. * places in wpa_supplicant are still printing out to stdout. As a
  95. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  96. * sockets. */
  97. for (i = 0; i < 3; i++) {
  98. s = open("/dev/null", O_RDWR);
  99. if (s > 2) {
  100. close(s);
  101. break;
  102. }
  103. }
  104. #endif /* __linux__ */
  105. }
  106. int main(int argc, char *argv[])
  107. {
  108. int c, i;
  109. struct wpa_interface *ifaces, *iface;
  110. int iface_count, exitcode = -1;
  111. struct wpa_params params;
  112. struct wpa_global *global;
  113. if (os_program_init())
  114. return -1;
  115. os_memset(&params, 0, sizeof(params));
  116. params.wpa_debug_level = MSG_INFO;
  117. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  118. if (ifaces == NULL)
  119. return -1;
  120. iface_count = 1;
  121. wpa_supplicant_fd_workaround();
  122. for (;;) {
  123. c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qstuvW");
  124. if (c < 0)
  125. break;
  126. switch (c) {
  127. case 'b':
  128. iface->bridge_ifname = optarg;
  129. break;
  130. case 'B':
  131. params.daemonize++;
  132. break;
  133. case 'c':
  134. iface->confname = optarg;
  135. break;
  136. case 'C':
  137. iface->ctrl_interface = optarg;
  138. break;
  139. case 'D':
  140. iface->driver = optarg;
  141. break;
  142. case 'd':
  143. #ifdef CONFIG_NO_STDOUT_DEBUG
  144. printf("Debugging disabled with "
  145. "CONFIG_NO_STDOUT_DEBUG=y build time "
  146. "option.\n");
  147. goto out;
  148. #else /* CONFIG_NO_STDOUT_DEBUG */
  149. params.wpa_debug_level--;
  150. break;
  151. #endif /* CONFIG_NO_STDOUT_DEBUG */
  152. #ifdef CONFIG_DEBUG_FILE
  153. case 'f':
  154. params.wpa_debug_file_path = optarg;
  155. break;
  156. #endif /* CONFIG_DEBUG_FILE */
  157. case 'g':
  158. params.ctrl_interface = optarg;
  159. break;
  160. case 'h':
  161. usage();
  162. exitcode = 0;
  163. goto out;
  164. case 'i':
  165. iface->ifname = optarg;
  166. break;
  167. case 'K':
  168. params.wpa_debug_show_keys++;
  169. break;
  170. case 'L':
  171. license();
  172. exitcode = 0;
  173. goto out;
  174. case 'p':
  175. iface->driver_param = optarg;
  176. break;
  177. case 'P':
  178. os_free(params.pid_file);
  179. params.pid_file = os_rel2abs_path(optarg);
  180. break;
  181. case 'q':
  182. params.wpa_debug_level++;
  183. break;
  184. #ifdef CONFIG_DEBUG_SYSLOG
  185. case 's':
  186. params.wpa_debug_syslog++;
  187. break;
  188. #endif /* CONFIG_DEBUG_SYSLOG */
  189. case 't':
  190. params.wpa_debug_timestamp++;
  191. break;
  192. #ifdef CONFIG_CTRL_IFACE_DBUS
  193. case 'u':
  194. params.dbus_ctrl_interface = 1;
  195. break;
  196. #endif /* CONFIG_CTRL_IFACE_DBUS */
  197. case 'v':
  198. printf("%s\n", wpa_supplicant_version);
  199. exitcode = 0;
  200. goto out;
  201. case 'W':
  202. params.wait_for_monitor++;
  203. break;
  204. case 'N':
  205. iface_count++;
  206. iface = os_realloc(ifaces, iface_count *
  207. sizeof(struct wpa_interface));
  208. if (iface == NULL)
  209. goto out;
  210. ifaces = iface;
  211. iface = &ifaces[iface_count - 1];
  212. os_memset(iface, 0, sizeof(*iface));
  213. break;
  214. default:
  215. usage();
  216. exitcode = 0;
  217. goto out;
  218. }
  219. }
  220. exitcode = 0;
  221. global = wpa_supplicant_init(&params);
  222. if (global == NULL) {
  223. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  224. exitcode = -1;
  225. goto out;
  226. }
  227. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  228. if ((ifaces[i].confname == NULL &&
  229. ifaces[i].ctrl_interface == NULL) ||
  230. ifaces[i].ifname == NULL) {
  231. if (iface_count == 1 && (params.ctrl_interface ||
  232. params.dbus_ctrl_interface))
  233. break;
  234. usage();
  235. exitcode = -1;
  236. break;
  237. }
  238. if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
  239. exitcode = -1;
  240. }
  241. if (exitcode == 0)
  242. exitcode = wpa_supplicant_run(global);
  243. wpa_supplicant_deinit(global);
  244. out:
  245. os_free(ifaces);
  246. os_free(params.pid_file);
  247. os_program_deinit();
  248. return exitcode;
  249. }