Wifi daemon

From Sistemi Operativi
Jump to navigation Jump to search

Intro

Dato che ogni mattina mentre sono in laboratorio la connessione con ALMAWIFI viene persa ogni 10 minuti, ho deciso di scrivere un demone che me la riattivi ogni volta. PROBLEMA: durante la wpa_supplicant ho ioctl[SIOCSIWAP]: Operation not permitted

daemon.c

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

void daemonFunction (char * argv[]);
void usage (int argc, char *argv[]);

int main (int argc, char *argv[])
{
	pid_t child;
	pid_t gChild;
	int status;

	usage (argc, argv);
	child = fork ();
	switch (child)
	{
		case -1:
			perror ("child");
			exit (1);
		case 0:
			gChild = fork ();
			switch (gChild)
			{
				  case -1:
					 perror ("gChild");
					 exit (1);

				  case 0:
					 daemonFunction(argv);

				  default:
					 //parent gChild
					 return 0;

			}
		default:

			//parent
			waitpid (child, &status, 0);
			return 0;

		}

		 return 0;
}

void daemonFunction (char *argv[])
{
	argv[0] = "./connect.sh";
	while (1)
	{
		 pid_t child;
		 child = fork ();
		 int status;

		 switch (child)
		 {
			case -1:
				  perror ("child");
				  exit (1);
			case 0:
				  execv("./connect.sh", argv);

				  perror ("execv");
				  exit (1);

			default:
				  //parent 
				  waitpid (child, &status, 0);
				  break;


		 }
	}
}

void usage (int argc, char *argv[])
{
	if (argc != 2)
	{
		 char *iface= "iface";
		 printf ("Usage: %s [%s]\n",
			  argv[0],
			  iface);
		 exit (1);
	}
}

connect.sh

#!/bin/bash

main ()
{
	  ping -q -c5 google.com >> ./.log

	  if [ $? -eq 0 ]
	  then
		 echo "internet ok" >> ./.log
		 sleep 10
	  else
		 connect "$@"

	  fi

}

connect ()
{
	  iface=$1

	  sudo killall wpa_supplicant
	  sudo ifconfig $iface down 
	  sudo iwconfig $iface mode Managed 
	  sudo ifconfig $iface up 
	  sudo wpa_supplicant -B -Dwext -i $iface -c ./wireless-wpa-enterprise.conf -dd 
	  sudo dhclient $iface 
	  sleep 10
}

main "$@"

wireless-wpa-enterprise.conf

ctrl_interface=/var/run/wpa_supplicant

network={
    ssid="ALMAWIFI"
    scan_ssid=0
    key_mgmt=WPA-EAP
    eap=PEAP
    phase2="auth=MSCHAPV2"
    identity="blabla@studio.unibo.it"
    password="psw"
}

Eddy (talk) 10.20 Mon, 02 Mar 2015 (CET)