main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. extern const char *wpa_supplicant_version;
  21. extern const char *wpa_supplicant_license;
  22. #ifndef CONFIG_NO_STDOUT_DEBUG
  23. extern const char *wpa_supplicant_full_license1;
  24. extern const char *wpa_supplicant_full_license2;
  25. extern const char *wpa_supplicant_full_license3;
  26. extern const char *wpa_supplicant_full_license4;
  27. extern const char *wpa_supplicant_full_license5;
  28. #endif /* CONFIG_NO_STDOUT_DEBUG */
  29. extern struct wpa_driver_ops *wpa_supplicant_drivers[];
  30. static void usage(void)
  31. {
  32. int i;
  33. printf("%s\n\n%s\n"
  34. "usage:\n"
  35. " wpa_supplicant [-BddhKLqqtuvW] [-P<pid file>] "
  36. "[-g<global ctrl>] \\\n"
  37. " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
  38. "[-p<driver_param>] \\\n"
  39. " [-b<br_ifname>] [-f<debug file>] \\\n"
  40. " [-N -i<ifname> -c<conf> [-C<ctrl>] "
  41. "[-D<driver>] \\\n"
  42. " [-p<driver_param>] [-b<br_ifname>] ...]\n"
  43. "\n"
  44. "drivers:\n",
  45. wpa_supplicant_version, wpa_supplicant_license);
  46. for (i = 0; wpa_supplicant_drivers[i]; i++) {
  47. printf(" %s = %s\n",
  48. wpa_supplicant_drivers[i]->name,
  49. wpa_supplicant_drivers[i]->desc);
  50. }
  51. #ifndef CONFIG_NO_STDOUT_DEBUG
  52. printf("options:\n"
  53. " -b = optional bridge interface name\n"
  54. " -B = run daemon in the background\n"
  55. " -c = Configuration file\n"
  56. " -C = ctrl_interface parameter (only used if -c is not)\n"
  57. " -i = interface name\n"
  58. " -d = increase debugging verbosity (-dd even more)\n"
  59. " -D = driver name\n"
  60. #ifdef CONFIG_DEBUG_FILE
  61. " -f = log output to debug file instead of stdout\n"
  62. #endif /* CONFIG_DEBUG_FILE */
  63. " -g = global ctrl_interface\n"
  64. " -K = include keys (passwords, etc.) in debug output\n"
  65. " -t = include timestamp in debug messages\n"
  66. " -h = show this help text\n"
  67. " -L = show license (GPL and BSD)\n");
  68. printf(" -p = driver parameters\n"
  69. " -P = PID file\n"
  70. " -q = decrease debugging verbosity (-qq even less)\n"
  71. #ifdef CONFIG_CTRL_IFACE_DBUS
  72. " -u = enable DBus control interface\n"
  73. #endif /* CONFIG_CTRL_IFACE_DBUS */
  74. " -v = show version\n"
  75. " -W = wait for a control interface monitor before starting\n"
  76. " -N = start describing new interface\n");
  77. printf("example:\n"
  78. " wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
  79. #endif /* CONFIG_NO_STDOUT_DEBUG */
  80. }
  81. static void license(void)
  82. {
  83. #ifndef CONFIG_NO_STDOUT_DEBUG
  84. printf("%s\n\n%s%s%s%s%s\n",
  85. wpa_supplicant_version,
  86. wpa_supplicant_full_license1,
  87. wpa_supplicant_full_license2,
  88. wpa_supplicant_full_license3,
  89. wpa_supplicant_full_license4,
  90. wpa_supplicant_full_license5);
  91. #endif /* CONFIG_NO_STDOUT_DEBUG */
  92. }
  93. static void wpa_supplicant_fd_workaround(void)
  94. {
  95. #ifdef __linux__
  96. int s, i;
  97. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  98. * fd 0, 1, and 2 closed. This will cause some issues because many
  99. * places in wpa_supplicant are still printing out to stdout. As a
  100. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  101. * sockets. */
  102. for (i = 0; i < 3; i++) {
  103. s = open("/dev/null", O_RDWR);
  104. if (s > 2) {
  105. close(s);
  106. break;
  107. }
  108. }
  109. #endif /* __linux__ */
  110. }
  111. int main(int argc, char *argv[])
  112. {
  113. int c, i;
  114. struct wpa_interface *ifaces, *iface;
  115. int iface_count, exitcode = -1;
  116. struct wpa_params params;
  117. struct wpa_global *global;
  118. if (os_program_init())
  119. return -1;
  120. os_memset(&params, 0, sizeof(params));
  121. params.wpa_debug_level = MSG_INFO;
  122. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  123. if (ifaces == NULL)
  124. return -1;
  125. iface_count = 1;
  126. wpa_supplicant_fd_workaround();
  127. for (;;) {
  128. c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qtuvW");
  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. #ifdef CONFIG_DEBUG_FILE
  158. case 'f':
  159. params.wpa_debug_file_path = optarg;
  160. break;
  161. #endif /* CONFIG_DEBUG_FILE */
  162. case 'g':
  163. params.ctrl_interface = optarg;
  164. break;
  165. case 'h':
  166. usage();
  167. exitcode = 0;
  168. goto out;
  169. case 'i':
  170. iface->ifname = optarg;
  171. break;
  172. case 'K':
  173. params.wpa_debug_show_keys++;
  174. break;
  175. case 'L':
  176. license();
  177. exitcode = 0;
  178. goto out;
  179. case 'p':
  180. iface->driver_param = optarg;
  181. break;
  182. case 'P':
  183. os_free(params.pid_file);
  184. params.pid_file = os_rel2abs_path(optarg);
  185. break;
  186. case 'q':
  187. params.wpa_debug_level++;
  188. break;
  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. }