main.c 6.7 KB

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