main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. printf(" -t = include timestamp in debug messages\n"
  58. " -h = show this help text\n"
  59. " -L = show license (BSD)\n"
  60. " -o = override driver parameter for new interfaces\n"
  61. " -O = override ctrl_interface parameter for new interfaces\n"
  62. " -p = driver parameters\n"
  63. " -P = PID file\n"
  64. " -q = decrease debugging verbosity (-qq even less)\n");
  65. #ifdef CONFIG_DBUS
  66. printf(" -u = enable DBus control interface\n");
  67. #endif /* CONFIG_DBUS */
  68. printf(" -v = show version\n"
  69. " -W = wait for a control interface monitor before starting\n"
  70. " -N = start describing new interface\n");
  71. printf("example:\n"
  72. " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
  73. wpa_drivers[i] ? wpa_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:de:f:g:hi:KLNo:O:p: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. case 'e':
  153. params.entropy_file = optarg;
  154. break;
  155. #ifdef CONFIG_DEBUG_FILE
  156. case 'f':
  157. params.wpa_debug_file_path = optarg;
  158. break;
  159. #endif /* CONFIG_DEBUG_FILE */
  160. case 'g':
  161. params.ctrl_interface = optarg;
  162. break;
  163. case 'h':
  164. usage();
  165. exitcode = 0;
  166. goto out;
  167. case 'i':
  168. iface->ifname = optarg;
  169. break;
  170. case 'K':
  171. params.wpa_debug_show_keys++;
  172. break;
  173. case 'L':
  174. license();
  175. exitcode = 0;
  176. goto out;
  177. case 'o':
  178. params.override_driver = optarg;
  179. break;
  180. case 'O':
  181. params.override_ctrl_interface = optarg;
  182. break;
  183. case 'p':
  184. iface->driver_param = optarg;
  185. break;
  186. case 'P':
  187. os_free(params.pid_file);
  188. params.pid_file = os_rel2abs_path(optarg);
  189. break;
  190. case 'q':
  191. params.wpa_debug_level++;
  192. break;
  193. #ifdef CONFIG_DEBUG_SYSLOG
  194. case 's':
  195. params.wpa_debug_syslog++;
  196. break;
  197. #endif /* CONFIG_DEBUG_SYSLOG */
  198. case 't':
  199. params.wpa_debug_timestamp++;
  200. break;
  201. #ifdef CONFIG_DBUS
  202. case 'u':
  203. params.dbus_ctrl_interface = 1;
  204. break;
  205. #endif /* CONFIG_DBUS */
  206. case 'v':
  207. printf("%s\n", wpa_supplicant_version);
  208. exitcode = 0;
  209. goto out;
  210. case 'W':
  211. params.wait_for_monitor++;
  212. break;
  213. case 'N':
  214. iface_count++;
  215. iface = os_realloc(ifaces, iface_count *
  216. sizeof(struct wpa_interface));
  217. if (iface == NULL)
  218. goto out;
  219. ifaces = iface;
  220. iface = &ifaces[iface_count - 1];
  221. os_memset(iface, 0, sizeof(*iface));
  222. break;
  223. default:
  224. usage();
  225. exitcode = 0;
  226. goto out;
  227. }
  228. }
  229. exitcode = 0;
  230. global = wpa_supplicant_init(&params);
  231. if (global == NULL) {
  232. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  233. exitcode = -1;
  234. goto out;
  235. }
  236. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  237. if ((ifaces[i].confname == NULL &&
  238. ifaces[i].ctrl_interface == NULL) ||
  239. ifaces[i].ifname == NULL) {
  240. if (iface_count == 1 && (params.ctrl_interface ||
  241. params.dbus_ctrl_interface))
  242. break;
  243. usage();
  244. exitcode = -1;
  245. break;
  246. }
  247. if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
  248. exitcode = -1;
  249. }
  250. if (exitcode == 0)
  251. exitcode = wpa_supplicant_run(global);
  252. wpa_supplicant_deinit(global);
  253. out:
  254. os_free(ifaces);
  255. os_free(params.pid_file);
  256. os_program_deinit();
  257. return exitcode;
  258. }