[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[RFC] [PATCH 5/7] Add WPA support to netcfg



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wpa.c, this adds two functions to write out our wpasupplicant.conf and
start wpasupplicant and also a killwpa.sh script. I still haven't
managed to find a good way to get wpasupplicants pid and kill it but I
am still working on it.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIEs7BV8GyuTwyskMRAkpqAJ0Y3F4SnVabULNG6JafbTYaMZhYKgCfQ0HA
St4ZdXn57qqLMDJiNpRGZVs=
=nbyF
-----END PGP SIGNATURE-----

diff --git a/packages/netcfg/killwpa.sh b/packages/netcfg/killwpa.sh
new file mode 100755
index 0000000..b370bb0
--- /dev/null
+++ b/packages/netcfg/killwpa.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+# Killall for wpasupplicant
+
+# Use [] in sed /address/ to avoid matching sed command itself in ps output
+pids=$(ps ax | sed -n '/[w]pa/s/^[ ]*\([0-9]*\).*/\1/p')
+
+for pid in $pids; do
+  if kill -0 $pid 2>/dev/null; then
+    kill -TERM $pid
+    sleep 1
+    # Still alive? Die!
+    if kill -0 $pid 2>/dev/null; then
+      kill -KILL $pid
+    fi
+  fi
+done
diff --git a/packages/netcfg/wpa.c b/packages/netcfg/wpa.c
new file mode 100644
index 0000000..2b5538a
--- /dev/null
+++ b/packages/netcfg/wpa.c
@@ -0,0 +1,87 @@
+/*
+* WPA module for netcfg
+*
+* Copyright (C) 2008 Glenn Saberton <gsaberton@foomagic.org>
+*
+* Licensed under the terms of the GNU General Public License version 2
+*
+* Functions shamelessly copied from dhcp.c, if you are looking for comments
+* look in that file.
+*/
+
+#include "netcfg.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <debian-installer.h>
+
+
+static int wpa_supplicant_exit_status = 1;
+pid_t wpa_supplicant_pid = -1;
+
+/*
+* Build an /etc/wpa_supplicant.conf
+* file.
+*/
+
+int netcfg_write_wpa (char *essid, char *passphrase)
+{
+  FILE *fp;
+
+  if ((fp = file_open (WPASUPP_FILE, "w")))
+    {
+      fprintf (fp, "\n# wpa_supplicant.conf generated during install\n" 
+    		   "ctrl_interface=/var/run/wpa_supplicant\n" 
+    		   "ctrl_interface_group=0\n" 
+    		   "eapol_version=1\n" 
+    		   "ap_scan=1\n\n"	/* So we can associate to hidden ssid's  */
+	    	   "network={\n");
+      fprintf (fp, "\t\tssid=\"%s\"\n", essid);
+      fprintf (fp, "\t\tpsk=\"%s\"\n", passphrase);
+      fprintf (fp, "\t\tscan_ssid=1\n" "}\n");
+    }
+  fclose (fp);
+  return 0;
+}
+
+static void wpa_supplicant_sigchild (int sig __attribute__ ((unused)))
+{
+  if (wpa_supplicant_pid <= 0)
+    return;
+
+  waitpid (wpa_supplicant_pid, &wpa_supplicant_exit_status, 0);
+  wpa_supplicant_pid = -1;
+}
+
+int start_wpa_supplicant (struct debconfclient *client)
+{
+
+  if (access ("/sbin/wpa_supplicant", F_OK) == 0);
+
+  else
+    {
+      debconf_input (client, "critical", "netcfg/no_wpa_supplicant");
+      debconf_go (client);
+      exit (1);
+    }
+
+wpa_supplicant_pid = fork();
+
+  if (wpa_supplicant_pid == 0)
+    {
+        fclose(client->out);
+        if (execlp ("wpa_supplicant", "wpa_supplicant", "-i", interface, "-c",
+                    "/etc/wpa_supplicant/wpa_supplicant.conf", "-B", NULL) == -1)
+          {
+           di_error("could not exec wpasupplicant: %s", strerror(errno));
+           return 1;
+          }
+        else
+             return 0;
+    } 
+  else
+    {
+     signal(SIGCHLD, &wpa_supplicant_sigchild);
+     return 0;
+    }
+}

Reply to: