main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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(int start)
  93. {
  94. #ifdef __linux__
  95. static int fd[3] = { -1, -1, -1 };
  96. int 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. if (start) {
  103. for (i = 0; i < 3; i++) {
  104. fd[i] = open("/dev/null", O_RDWR);
  105. if (fd[i] > 2) {
  106. close(fd[i]);
  107. fd[i] = -1;
  108. break;
  109. }
  110. }
  111. } else {
  112. for (i = 0; i < 3; i++) {
  113. if (fd[i] >= 0) {
  114. close(fd[i]);
  115. fd[i] = -1;
  116. }
  117. }
  118. }
  119. #endif /* __linux__ */
  120. }
  121. int main(int argc, char *argv[])
  122. {
  123. int c, i;
  124. struct wpa_interface *ifaces, *iface;
  125. int iface_count, exitcode = -1;
  126. struct wpa_params params;
  127. struct wpa_global *global;
  128. if (os_program_init())
  129. return -1;
  130. os_memset(&params, 0, sizeof(params));
  131. params.wpa_debug_level = MSG_INFO;
  132. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  133. if (ifaces == NULL)
  134. return -1;
  135. iface_count = 1;
  136. wpa_supplicant_fd_workaround(1);
  137. for (;;) {
  138. c = getopt(argc, argv,
  139. "b:Bc:C:D:de:f:g:hi:KLNo:O:p:P:qsTtuvW");
  140. if (c < 0)
  141. break;
  142. switch (c) {
  143. case 'b':
  144. iface->bridge_ifname = optarg;
  145. break;
  146. case 'B':
  147. params.daemonize++;
  148. break;
  149. case 'c':
  150. iface->confname = optarg;
  151. break;
  152. case 'C':
  153. iface->ctrl_interface = optarg;
  154. break;
  155. case 'D':
  156. iface->driver = optarg;
  157. break;
  158. case 'd':
  159. #ifdef CONFIG_NO_STDOUT_DEBUG
  160. printf("Debugging disabled with "
  161. "CONFIG_NO_STDOUT_DEBUG=y build time "
  162. "option.\n");
  163. goto out;
  164. #else /* CONFIG_NO_STDOUT_DEBUG */
  165. params.wpa_debug_level--;
  166. break;
  167. #endif /* CONFIG_NO_STDOUT_DEBUG */
  168. case 'e':
  169. params.entropy_file = optarg;
  170. break;
  171. #ifdef CONFIG_DEBUG_FILE
  172. case 'f':
  173. params.wpa_debug_file_path = optarg;
  174. break;
  175. #endif /* CONFIG_DEBUG_FILE */
  176. case 'g':
  177. params.ctrl_interface = optarg;
  178. break;
  179. case 'h':
  180. usage();
  181. exitcode = 0;
  182. goto out;
  183. case 'i':
  184. iface->ifname = optarg;
  185. break;
  186. case 'K':
  187. params.wpa_debug_show_keys++;
  188. break;
  189. case 'L':
  190. license();
  191. exitcode = 0;
  192. goto out;
  193. case 'o':
  194. params.override_driver = optarg;
  195. break;
  196. case 'O':
  197. params.override_ctrl_interface = optarg;
  198. break;
  199. case 'p':
  200. iface->driver_param = optarg;
  201. break;
  202. case 'P':
  203. os_free(params.pid_file);
  204. params.pid_file = os_rel2abs_path(optarg);
  205. break;
  206. case 'q':
  207. params.wpa_debug_level++;
  208. break;
  209. #ifdef CONFIG_DEBUG_SYSLOG
  210. case 's':
  211. params.wpa_debug_syslog++;
  212. break;
  213. #endif /* CONFIG_DEBUG_SYSLOG */
  214. #ifdef CONFIG_DEBUG_LINUX_TRACING
  215. case 'T':
  216. params.wpa_debug_tracing++;
  217. break;
  218. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  219. case 't':
  220. params.wpa_debug_timestamp++;
  221. break;
  222. #ifdef CONFIG_DBUS
  223. case 'u':
  224. params.dbus_ctrl_interface = 1;
  225. break;
  226. #endif /* CONFIG_DBUS */
  227. case 'v':
  228. printf("%s\n", wpa_supplicant_version);
  229. exitcode = 0;
  230. goto out;
  231. case 'W':
  232. params.wait_for_monitor++;
  233. break;
  234. case 'N':
  235. iface_count++;
  236. iface = os_realloc_array(ifaces, iface_count,
  237. sizeof(struct wpa_interface));
  238. if (iface == NULL)
  239. goto out;
  240. ifaces = iface;
  241. iface = &ifaces[iface_count - 1];
  242. os_memset(iface, 0, sizeof(*iface));
  243. break;
  244. default:
  245. usage();
  246. exitcode = 0;
  247. goto out;
  248. }
  249. }
  250. exitcode = 0;
  251. global = wpa_supplicant_init(&params);
  252. if (global == NULL) {
  253. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  254. exitcode = -1;
  255. goto out;
  256. } else {
  257. wpa_printf(MSG_INFO, "Successfully initialized "
  258. "wpa_supplicant");
  259. }
  260. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  261. if ((ifaces[i].confname == NULL &&
  262. ifaces[i].ctrl_interface == NULL) ||
  263. ifaces[i].ifname == NULL) {
  264. if (iface_count == 1 && (params.ctrl_interface ||
  265. params.dbus_ctrl_interface))
  266. break;
  267. usage();
  268. exitcode = -1;
  269. break;
  270. }
  271. if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
  272. exitcode = -1;
  273. }
  274. if (exitcode == 0)
  275. exitcode = wpa_supplicant_run(global);
  276. wpa_supplicant_deinit(global);
  277. out:
  278. wpa_supplicant_fd_workaround(0);
  279. os_free(ifaces);
  280. os_free(params.pid_file);
  281. os_program_deinit();
  282. return exitcode;
  283. }