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. #include "driver_i.h"
  21. extern struct wpa_driver_ops *wpa_supplicant_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_supplicant_drivers[i]; i++) {
  39. printf(" %s = %s\n",
  40. wpa_supplicant_drivers[i]->name,
  41. wpa_supplicant_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_supplicant_drivers[i] ?
  75. wpa_supplicant_drivers[i]->name : "wext");
  76. #endif /* CONFIG_NO_STDOUT_DEBUG */
  77. }
  78. static void license(void)
  79. {
  80. #ifndef CONFIG_NO_STDOUT_DEBUG
  81. printf("%s\n\n%s%s%s%s%s\n",
  82. wpa_supplicant_version,
  83. wpa_supplicant_full_license1,
  84. wpa_supplicant_full_license2,
  85. wpa_supplicant_full_license3,
  86. wpa_supplicant_full_license4,
  87. wpa_supplicant_full_license5);
  88. #endif /* CONFIG_NO_STDOUT_DEBUG */
  89. }
  90. static void wpa_supplicant_fd_workaround(void)
  91. {
  92. #ifdef __linux__
  93. int s, i;
  94. /* When started from pcmcia-cs scripts, wpa_supplicant might start with
  95. * fd 0, 1, and 2 closed. This will cause some issues because many
  96. * places in wpa_supplicant are still printing out to stdout. As a
  97. * workaround, make sure that fd's 0, 1, and 2 are not used for other
  98. * sockets. */
  99. for (i = 0; i < 3; i++) {
  100. s = open("/dev/null", O_RDWR);
  101. if (s > 2) {
  102. close(s);
  103. break;
  104. }
  105. }
  106. #endif /* __linux__ */
  107. }
  108. int main(int argc, char *argv[])
  109. {
  110. int c, i;
  111. struct wpa_interface *ifaces, *iface;
  112. int iface_count, exitcode = -1;
  113. struct wpa_params params;
  114. struct wpa_global *global;
  115. if (os_program_init())
  116. return -1;
  117. os_memset(&params, 0, sizeof(params));
  118. params.wpa_debug_level = MSG_INFO;
  119. iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
  120. if (ifaces == NULL)
  121. return -1;
  122. iface_count = 1;
  123. wpa_supplicant_fd_workaround();
  124. for (;;) {
  125. c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qstuvW");
  126. if (c < 0)
  127. break;
  128. switch (c) {
  129. case 'b':
  130. iface->bridge_ifname = optarg;
  131. break;
  132. case 'B':
  133. params.daemonize++;
  134. break;
  135. case 'c':
  136. iface->confname = optarg;
  137. break;
  138. case 'C':
  139. iface->ctrl_interface = optarg;
  140. break;
  141. case 'D':
  142. iface->driver = optarg;
  143. break;
  144. case 'd':
  145. #ifdef CONFIG_NO_STDOUT_DEBUG
  146. printf("Debugging disabled with "
  147. "CONFIG_NO_STDOUT_DEBUG=y build time "
  148. "option.\n");
  149. goto out;
  150. #else /* CONFIG_NO_STDOUT_DEBUG */
  151. params.wpa_debug_level--;
  152. break;
  153. #endif /* CONFIG_NO_STDOUT_DEBUG */
  154. #ifdef CONFIG_DEBUG_FILE
  155. case 'f':
  156. params.wpa_debug_file_path = optarg;
  157. break;
  158. #endif /* CONFIG_DEBUG_FILE */
  159. case 'g':
  160. params.ctrl_interface = optarg;
  161. break;
  162. case 'h':
  163. usage();
  164. exitcode = 0;
  165. goto out;
  166. case 'i':
  167. iface->ifname = optarg;
  168. break;
  169. case 'K':
  170. params.wpa_debug_show_keys++;
  171. break;
  172. case 'L':
  173. license();
  174. exitcode = 0;
  175. goto out;
  176. case 'p':
  177. iface->driver_param = optarg;
  178. break;
  179. case 'P':
  180. os_free(params.pid_file);
  181. params.pid_file = os_rel2abs_path(optarg);
  182. break;
  183. case 'q':
  184. params.wpa_debug_level++;
  185. break;
  186. #ifdef CONFIG_DEBUG_SYSLOG
  187. case 's':
  188. params.wpa_debug_syslog++;
  189. break;
  190. #endif /* CONFIG_DEBUG_SYSLOG */
  191. case 't':
  192. params.wpa_debug_timestamp++;
  193. break;
  194. #ifdef CONFIG_CTRL_IFACE_DBUS
  195. case 'u':
  196. params.dbus_ctrl_interface = 1;
  197. break;
  198. #endif /* CONFIG_CTRL_IFACE_DBUS */
  199. case 'v':
  200. printf("%s\n", wpa_supplicant_version);
  201. exitcode = 0;
  202. goto out;
  203. case 'W':
  204. params.wait_for_monitor++;
  205. break;
  206. case 'N':
  207. iface_count++;
  208. iface = os_realloc(ifaces, iface_count *
  209. sizeof(struct wpa_interface));
  210. if (iface == NULL)
  211. goto out;
  212. ifaces = iface;
  213. iface = &ifaces[iface_count - 1];
  214. os_memset(iface, 0, sizeof(*iface));
  215. break;
  216. default:
  217. usage();
  218. exitcode = 0;
  219. goto out;
  220. }
  221. }
  222. exitcode = 0;
  223. global = wpa_supplicant_init(&params);
  224. if (global == NULL) {
  225. wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
  226. exitcode = -1;
  227. goto out;
  228. }
  229. for (i = 0; exitcode == 0 && i < iface_count; i++) {
  230. if ((ifaces[i].confname == NULL &&
  231. ifaces[i].ctrl_interface == NULL) ||
  232. ifaces[i].ifname == NULL) {
  233. if (iface_count == 1 && (params.ctrl_interface ||
  234. params.dbus_ctrl_interface))
  235. break;
  236. usage();
  237. exitcode = -1;
  238. break;
  239. }
  240. if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
  241. exitcode = -1;
  242. }
  243. if (exitcode == 0)
  244. exitcode = wpa_supplicant_run(global);
  245. wpa_supplicant_deinit(global);
  246. out:
  247. os_free(ifaces);
  248. os_free(params.pid_file);
  249. os_program_deinit();
  250. return exitcode;
  251. }