main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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>] [-I<config file>] "
  31. "...]\n"
  32. "\n"
  33. "drivers:\n",
  34. wpa_supplicant_version, wpa_supplicant_license);
  35. for (i = 0; wpa_drivers[i]; i++) {
  36. printf(" %s = %s\n",
  37. wpa_drivers[i]->name,
  38. wpa_drivers[i]->desc);
  39. }
  40. #ifndef CONFIG_NO_STDOUT_DEBUG
  41. printf("options:\n"
  42. " -b = optional bridge interface name\n"
  43. " -B = run daemon in the background\n"
  44. " -c = Configuration file\n"
  45. " -C = ctrl_interface parameter (only used if -c is not)\n"
  46. " -i = interface name\n"
  47. " -I = additional configuration file\n"
  48. " -d = increase debugging verbosity (-dd even more)\n"
  49. " -D = driver name (can be multiple drivers: nl80211,wext)\n"
  50. " -e = entropy file\n");
  51. #ifdef CONFIG_DEBUG_FILE
  52. printf(" -f = log output to debug file instead of stdout\n");
  53. #endif /* CONFIG_DEBUG_FILE */
  54. printf(" -g = global ctrl_interface\n"
  55. " -K = include keys (passwords, etc.) in debug output\n");
  56. #ifdef CONFIG_DEBUG_SYSLOG
  57. printf(" -s = log output to syslog instead of stdout\n");
  58. #endif /* CONFIG_DEBUG_SYSLOG */
  59. #ifdef CONFIG_DEBUG_LINUX_TRACING
  60. printf(" -T = record to Linux tracing in addition to logging\n");
  61. printf(" (records all messages regardless of debug verbosity)\n");
  62. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  63. printf(" -t = include timestamp in debug messages\n"
  64. " -h = show this help text\n"
  65. " -L = show license (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(int start)
  95. {
  96. #ifdef __linux__
  97. static int fd[3] = { -1, -1, -1 };
  98. int i;
  99. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  100. * fd 0, 1, and 2 closed. This will cause some issues because many
  101. * places in wpa_supplicant are still printing out to stdout. As a
  102. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  103. * sockets. */
  104. if (start) {
  105. for (i = 0; i < 3; i++) {
  106. fd[i] = open("/dev/null", O_RDWR);
  107. if (fd[i] > 2) {
  108. close(fd[i]);
  109. fd[i] = -1;
  110. break;
  111. }
  112. }
  113. } else {
  114. for (i = 0; i < 3; i++) {
  115. if (fd[i] >= 0) {
  116. close(fd[i]);
  117. fd[i] = -1;
  118. }
  119. }
  120. }
  121. #endif /* __linux__ */
  122. }
  123. int main(int argc, char *argv[])
  124. {
  125. int c, i;
  126. struct wpa_interface *ifaces, *iface;
  127. int iface_count, exitcode = -1;
  128. struct wpa_params params;
  129. struct wpa_global *global;
  130. if (os_program_init())
  131. return -1;
  132. os_memset(&params, 0, sizeof(params));
  133. params.wpa_debug_level = MSG_INFO;
  134. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  135. if (ifaces == NULL)
  136. return -1;
  137. iface_count = 1;
  138. wpa_supplicant_fd_workaround(1);
  139. for (;;) {
  140. c = getopt(argc, argv,
  141. "b:Bc:C:D:de:f:g:hi:I:KLNo:O:p:P:qsTtuvW");
  142. if (c < 0)
  143. break;
  144. switch (c) {
  145. case 'b':
  146. iface->bridge_ifname = optarg;
  147. break;
  148. case 'B':
  149. params.daemonize++;
  150. break;
  151. case 'c':
  152. iface->confname = optarg;
  153. break;
  154. case 'C':
  155. iface->ctrl_interface = optarg;
  156. break;
  157. case 'D':
  158. iface->driver = optarg;
  159. break;
  160. case 'd':
  161. #ifdef CONFIG_NO_STDOUT_DEBUG
  162. printf("Debugging disabled with "
  163. "CONFIG_NO_STDOUT_DEBUG=y build time "
  164. "option.\n");
  165. goto out;
  166. #else /* CONFIG_NO_STDOUT_DEBUG */
  167. params.wpa_debug_level--;
  168. break;
  169. #endif /* CONFIG_NO_STDOUT_DEBUG */
  170. case 'e':
  171. params.entropy_file = optarg;
  172. break;
  173. #ifdef CONFIG_DEBUG_FILE
  174. case 'f':
  175. params.wpa_debug_file_path = optarg;
  176. break;
  177. #endif /* CONFIG_DEBUG_FILE */
  178. case 'g':
  179. params.ctrl_interface = optarg;
  180. break;
  181. case 'h':
  182. usage();
  183. exitcode = 0;
  184. goto out;
  185. case 'i':
  186. iface->ifname = optarg;
  187. break;
  188. case 'I':
  189. iface->confanother = optarg;
  190. break;
  191. case 'K':
  192. params.wpa_debug_show_keys++;
  193. break;
  194. case 'L':
  195. license();
  196. exitcode = 0;
  197. goto out;
  198. case 'o':
  199. params.override_driver = optarg;
  200. break;
  201. case 'O':
  202. params.override_ctrl_interface = optarg;
  203. break;
  204. case 'p':
  205. iface->driver_param = optarg;
  206. break;
  207. case 'P':
  208. os_free(params.pid_file);
  209. params.pid_file = os_rel2abs_path(optarg);
  210. break;
  211. case 'q':
  212. params.wpa_debug_level++;
  213. break;
  214. #ifdef CONFIG_DEBUG_SYSLOG
  215. case 's':
  216. params.wpa_debug_syslog++;
  217. break;
  218. #endif /* CONFIG_DEBUG_SYSLOG */
  219. #ifdef CONFIG_DEBUG_LINUX_TRACING
  220. case 'T':
  221. params.wpa_debug_tracing++;
  222. break;
  223. #endif /* CONFIG_DEBUG_LINUX_TRACING */
  224. case 't':
  225. params.wpa_debug_timestamp++;
  226. break;
  227. #ifdef CONFIG_DBUS
  228. case 'u':
  229. params.dbus_ctrl_interface = 1;
  230. break;
  231. #endif /* CONFIG_DBUS */
  232. case 'v':
  233. printf("%s\n", wpa_supplicant_version);
  234. exitcode = 0;
  235. goto out;
  236. case 'W':
  237. params.wait_for_monitor++;
  238. break;
  239. case 'N':
  240. iface_count++;
  241. iface = os_realloc_array(ifaces, iface_count,
  242. sizeof(struct wpa_interface));
  243. if (iface == NULL)
  244. goto out;
  245. ifaces = iface;
  246. iface = &ifaces[iface_count - 1];
  247. os_memset(iface, 0, sizeof(*iface));
  248. break;
  249. default:
  250. usage();
  251. exitcode = 0;
  252. goto out;
  253. }
  254. }
  255. exitcode = 0;
  256. global = wpa_supplicant_init(&params);
  257. if (global == NULL) {
  258. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  259. exitcode = -1;
  260. goto out;
  261. } else {
  262. wpa_printf(MSG_INFO, "Successfully initialized "
  263. "wpa_supplicant");
  264. }
  265. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  266. if ((ifaces[i].confname == NULL &&
  267. ifaces[i].ctrl_interface == NULL) ||
  268. ifaces[i].ifname == NULL) {
  269. if (iface_count == 1 && (params.ctrl_interface ||
  270. params.dbus_ctrl_interface))
  271. break;
  272. usage();
  273. exitcode = -1;
  274. break;
  275. }
  276. if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
  277. exitcode = -1;
  278. }
  279. if (exitcode == 0)
  280. exitcode = wpa_supplicant_run(global);
  281. wpa_supplicant_deinit(global);
  282. out:
  283. wpa_supplicant_fd_workaround(0);
  284. os_free(ifaces);
  285. os_free(params.pid_file);
  286. os_program_deinit();
  287. return exitcode;
  288. }