Index: main.c
===================================================================
--- main.c	(revision 61)
+++ main.c	(revision 62)
@@ -4,22 +4,22 @@
  * 2011, Aleksander Besir (alex.besir@gmail.com)
  *
  */
-
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <syslog.h>
+#include "../hci_comm/hciChanged.h"
 #include "../hci_comm/hci_comm.h"
 #include "../init/scenariolist.h"
 #include "../init/devicelist.h"
 #include "../logics/device_threads.h"
 //#include "../logics/scenario_threads.h"
-#include <stdio.h>
-#include <pthread.h>
-#include <syslog.h>
-#include "../hci_comm/hciChanged.h"
 
 // Inter-thread shared variables
 // TODO: These variables could be configuration variables
 //       (eg: static boolean loggingEnabled)
-static device           *hDevice;   //device list
-static scenario         *hScenario; //scenario list
+static device           *hDevice;       //device list
+static scenario         *hScenario;     //scenario list
 static thread_id_holder *hThrIDhold_dev;//holds IDs of running threads (devices)
 static thread_id_holder *hThrIDhold_scn;//holds IDs of running threads (scenarios)
 //extern scenario *test;   //device list
@@ -52,10 +52,39 @@
 void *randomValThread_routine(void *dummy);
 void *scenarioThread_routine(void *dummy);
 int  reinitialiseLogic(); // on HCI request
+int  shutDownLogic();
 
 // Application entry point
 int main(int argc, char *argv[]) {
+    /** //DAEMON
+    pid_t pid, sid;
 
+    pid = fork();
+    if (pid < 0) {
+            exit(EXIT_FAILURE);
+    }
+    // EXIT PARENT
+    if (pid > 0) {
+            exit(EXIT_SUCCESS);
+    }
+
+    //umask(0);
+
+    // Open any logs here
+
+    // Create a new SID for the child process
+    sid = setsid();
+    if (sid < 0) {
+            // Log the failure
+            exit(EXIT_FAILURE);
+    }
+
+    // Close out the standard file descriptors
+    // Because daemons generally dont interact directly with user so there is no need of keeping these open
+    close(STDIN_FILENO);
+    close(STDOUT_FILENO);
+    close(STDERR_FILENO);
+    /**/
     /* initialisation */
     hDevice   = setDeviceList();
     hScenario = setNewScenarioList(hDevice);
@@ -174,9 +203,27 @@
                 {
                     // TODO syslog - "action:scen_ID:power_percentage"
                     int power = getPowerPercentage(scen->minval, scen->maxval, scen->optval, &scen->buf, scen->func);
-                    syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power);
-                    printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power);
-                    /*TODO execute action with parameter(s)*/
+
+                    /// Forking to execute action!
+                    pid_t pid, sid;
+
+                    pid = fork();
+                    if (pid < 0) {
+                            exit(EXIT_FAILURE);
+                    }
+                    // ignore parent
+                    if (pid > 0) {}
+                    else
+                    {
+                        /*TODO execute action with parameter(s)*/
+                        syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power);
+
+                        execl(scen->action, power);
+                        printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power);
+
+                        shutDownLogic();
+                    }
+
                 }
                 else
                     printf("Scenario %s not updated!\n", scen->scen_id);
@@ -206,7 +253,7 @@
     //set its value
     while(1) //TODO signal == true
     {
-        dev->buf=random(100)%10+20;// random 20-30
+        dev->buf=random()%10+20;// random 20-30
         //printf("device %s buf = %d\n", dev->id, dev->buf);
         sleep(dev->readitv);
     }
@@ -280,3 +327,42 @@
 
 	return 1; //sucess, TODO add fail conditions
 }
+
+int shutDownLogic()
+{
+    thread_id_holder *hold;
+    hold = hThrIDhold_dev;
+    //hThrIDhold_dev = NULL;
+
+    //kill device threads
+    printf("Attempting to cancel threads!\n");
+    while(hold)
+    {
+        printf("Cancelling dev thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx));
+        //TODO free thread_id_holder list too!
+        hold=hold->nxt;
+    }
+
+    //kill scenario threads
+    hold = hThrIDhold_scn;
+    //hThrIDhold_scn = NULL;
+    while(hold)
+    {
+        printf("Cancelling scn thread %ld with return state %d\n", hold->pthr_idx,pthread_cancel(hold->pthr_idx));
+        //TODO free thread_id_holder list too!
+        hold=hold->nxt;
+    }
+
+    //free memory
+    printf("Freeing memory!\n");
+    freeDeviceList(hDevice);
+    freeScenarioList(hScenario);
+    freeThreadIDList(hThrIDhold_dev);
+    freeThreadIDList(hThrIDhold_scn);
+
+    //stop main thread and HCI comm thread
+    pthread_cancel(decisionMakingThread);
+    pthread_cancel(hciConnectionThread);
+
+	return 1; //sucess, TODO add fail conditions
+}
