main.c 7.1 KB

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