2012-05-14 07:38:02 -07:00
|
|
|
#include <stdio.h>
|
2012-05-14 07:37:49 -07:00
|
|
|
#include <unistd.h> /* misc. UNIX functions */
|
|
|
|
#include <fcntl.h>
|
2012-05-14 07:38:02 -07:00
|
|
|
#include <Ecore.h>
|
2012-05-14 07:38:31 -07:00
|
|
|
#include <Ecore_Ipc.h>
|
2012-05-14 07:37:49 -07:00
|
|
|
|
2012-05-14 07:38:02 -07:00
|
|
|
#include "helper.h"
|
2012-05-14 07:38:21 -07:00
|
|
|
#include "eet_dump.h"
|
2012-05-14 07:37:49 -07:00
|
|
|
|
|
|
|
#define RUNNING_DIR "/tmp"
|
|
|
|
#define LOCK_FILE "clouseaud.lock"
|
|
|
|
#define LOG_FILE "clouseaud.log"
|
2012-05-14 07:38:02 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#else
|
|
|
|
#define __UNUSED__
|
|
|
|
#endif
|
2012-05-14 07:37:49 -07:00
|
|
|
|
|
|
|
static Eina_List *info = NULL; /* Holds app info to be send to GUI client */
|
|
|
|
|
2012-05-14 07:38:02 -07:00
|
|
|
/* For Debug */
|
|
|
|
char msg_buf[MAX_LINE+1];
|
2012-05-14 07:37:49 -07:00
|
|
|
|
2012-05-14 07:38:02 -07:00
|
|
|
struct _Client {
|
|
|
|
int sdata;
|
|
|
|
};
|
|
|
|
|
2012-05-14 07:38:35 -07:00
|
|
|
struct _tree_info_st
|
|
|
|
{
|
2012-05-14 07:38:42 -07:00
|
|
|
void *app; /* app ptr to identify where the data came from */
|
|
|
|
void *data; /* Tree data */
|
2012-05-14 07:38:35 -07:00
|
|
|
};
|
|
|
|
typedef struct _tree_info_st tree_info_st;
|
|
|
|
|
2012-05-14 07:38:02 -07:00
|
|
|
static void
|
|
|
|
log_message(char *filename, char *mode, char *message)
|
2012-05-14 07:37:49 -07:00
|
|
|
{
|
|
|
|
FILE *logfile;
|
|
|
|
logfile=fopen(filename, mode);
|
|
|
|
if(!logfile) return;
|
|
|
|
fprintf(logfile,"%s\n",message);
|
|
|
|
fclose(logfile);
|
|
|
|
}
|
|
|
|
|
2012-05-14 07:38:42 -07:00
|
|
|
static int
|
|
|
|
_appcmp(const void *d1, const void *d2)
|
|
|
|
{
|
|
|
|
return ((((tree_info_st *) d1)->app) - d2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_List *trees = NULL;
|
|
|
|
|
|
|
|
static Eina_List *
|
|
|
|
tree_data_cleanup()
|
|
|
|
{
|
2012-05-14 07:38:35 -07:00
|
|
|
tree_info_st *tree;
|
2012-05-14 07:38:42 -07:00
|
|
|
log_message(LOG_FILE, "a","cleanup - clearing trees");
|
2012-05-14 07:38:35 -07:00
|
|
|
EINA_LIST_FREE(trees, tree)
|
|
|
|
{
|
2012-05-14 07:39:23 -07:00
|
|
|
item_tree_item_free(tree->data);
|
2012-05-14 07:38:35 -07:00
|
|
|
free(tree);
|
|
|
|
}
|
2012-05-14 07:38:02 -07:00
|
|
|
|
2012-05-14 07:38:42 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_List *
|
|
|
|
tree_data_add(void *a, void *d)
|
|
|
|
{
|
2012-05-14 07:38:49 -07:00
|
|
|
sprintf(msg_buf, "trees list size <%d>", eina_list_count(trees));
|
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:42 -07:00
|
|
|
tree_info_st *st = (tree_info_st *)
|
|
|
|
eina_list_search_unsorted(trees, _appcmp, a);
|
|
|
|
|
|
|
|
if (st)
|
|
|
|
{
|
2012-05-14 07:38:49 -07:00
|
|
|
sprintf(msg_buf, "Updating tree-data from APP <%p>", a);
|
2012-05-14 07:39:35 -07:00
|
|
|
item_tree_free(((st_tree_list *) st->data)->list);
|
2012-05-14 07:38:42 -07:00
|
|
|
st->data = d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tree_info_st *st = malloc(sizeof(*st));
|
|
|
|
if (st)
|
|
|
|
{
|
2012-05-14 07:38:49 -07:00
|
|
|
sprintf(msg_buf, "Got tree-data from APP <%p>", a);
|
2012-05-14 07:38:42 -07:00
|
|
|
|
|
|
|
st->app = a;
|
|
|
|
st->data = d;
|
|
|
|
|
|
|
|
trees = eina_list_append(trees, st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 07:38:49 -07:00
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:42 -07:00
|
|
|
return trees;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_daemon_cleanup(void)
|
|
|
|
{ /* Free strings */
|
2012-05-14 07:38:31 -07:00
|
|
|
Ecore_Ipc_Server *svr;
|
|
|
|
Ecore_Ipc_Client *cl;
|
2012-05-14 07:38:02 -07:00
|
|
|
const Eina_List *clients, *l;
|
|
|
|
|
2012-05-14 07:38:42 -07:00
|
|
|
trees = tree_data_cleanup();
|
2012-05-14 07:38:31 -07:00
|
|
|
clients = ecore_ipc_server_clients_get(svr);
|
2012-05-14 07:38:02 -07:00
|
|
|
sprintf(msg_buf,"Clients connected to this server when exiting: %d\n",
|
|
|
|
eina_list_count(clients));
|
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
|
|
|
EINA_LIST_FOREACH(clients, l, cl)
|
|
|
|
{
|
2012-05-14 07:38:31 -07:00
|
|
|
sprintf(msg_buf, "%s\n", ecore_ipc_client_ip_get(cl));
|
2012-05-14 07:38:02 -07:00
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:31 -07:00
|
|
|
free(ecore_ipc_client_data_get(cl));
|
2012-05-14 07:38:02 -07:00
|
|
|
}
|
|
|
|
|
2012-05-14 07:39:26 -07:00
|
|
|
data_descriptors_shutdown();
|
2012-05-14 07:38:31 -07:00
|
|
|
ecore_ipc_shutdown();
|
2012-05-14 07:38:02 -07:00
|
|
|
ecore_shutdown();
|
|
|
|
eina_shutdown();
|
|
|
|
}
|
|
|
|
|
2012-05-14 07:37:49 -07:00
|
|
|
void signal_handler(int sig)
|
|
|
|
{
|
|
|
|
switch(sig) {
|
|
|
|
case SIGHUP:
|
|
|
|
log_message(LOG_FILE, "a", "hangup signal catched");
|
|
|
|
break;
|
|
|
|
case SIGTERM:
|
|
|
|
log_message(LOG_FILE, "a", "terminate signal catched");
|
2012-05-14 07:38:02 -07:00
|
|
|
_daemon_cleanup();
|
2012-05-14 07:37:49 -07:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemonize(void)
|
|
|
|
{
|
|
|
|
int i,lfp;
|
|
|
|
char str[10];
|
|
|
|
time_t currentTime;
|
|
|
|
|
|
|
|
if(getppid()==1) return; /* already a daemon */
|
|
|
|
i=fork();
|
|
|
|
if (i<0) exit(1); /* fork error */
|
|
|
|
if (i>0) exit(0); /* parent exits */
|
|
|
|
|
|
|
|
time (¤tTime);
|
|
|
|
|
|
|
|
/* child (daemon) continues */
|
|
|
|
setsid(); /* obtain a new process group */
|
|
|
|
for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */
|
|
|
|
i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standart I/O */
|
|
|
|
umask(027); /* set newly created file permissions */
|
|
|
|
chdir(RUNNING_DIR); /* change running directory */
|
|
|
|
lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640);
|
|
|
|
if (lfp<0) exit(1); /* can not open */
|
|
|
|
if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */
|
|
|
|
/* first instance continues */
|
|
|
|
sprintf(str,"%d\n",getpid());
|
|
|
|
write(lfp,str,strlen(str)); /* record pid to lockfile */
|
|
|
|
signal(SIGCHLD,SIG_IGN); /* ignore child */
|
|
|
|
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
|
|
|
|
signal(SIGTTOU,SIG_IGN);
|
|
|
|
signal(SIGTTIN,SIG_IGN);
|
|
|
|
signal(SIGHUP,signal_handler); /* catch hangup signal */
|
|
|
|
signal(SIGTERM,signal_handler); /* catch kill signal */
|
|
|
|
|
|
|
|
log_message(LOG_FILE, "w", "Daemon Started");
|
|
|
|
log_message(LOG_FILE, "a", ctime(¤tTime));
|
|
|
|
}
|
|
|
|
|
2012-05-14 07:38:02 -07:00
|
|
|
/* START - Ecore communication callbacks */
|
|
|
|
Eina_Bool
|
2012-05-14 07:38:31 -07:00
|
|
|
_add(void *data __UNUSED__, int type __UNUSED__, Ecore_Ipc_Event_Client_Add *ev)
|
2012-05-14 07:37:49 -07:00
|
|
|
{
|
2012-05-14 07:38:09 -07:00
|
|
|
void *p;
|
2012-05-14 07:38:27 -07:00
|
|
|
char *msg="hello! - sent from the server";
|
2012-05-14 07:38:35 -07:00
|
|
|
int size = 0;
|
|
|
|
|
2012-05-14 07:38:39 -07:00
|
|
|
ecore_ipc_client_data_size_max_set(ev->client, -1);
|
2012-05-14 07:38:49 -07:00
|
|
|
sprintf(msg_buf, "<%s> msg from <%p>", __func__, ev->client);
|
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:42 -07:00
|
|
|
|
2012-05-14 07:38:39 -07:00
|
|
|
|
2012-05-14 07:38:35 -07:00
|
|
|
ack_st t = { msg };
|
2012-05-14 07:38:49 -07:00
|
|
|
p = packet_compose(DAEMON_ACK, &t, sizeof(t), &size);
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
ecore_ipc_client_send(ev->client, 0,0,0,0,EINA_FALSE, p, size);
|
|
|
|
ecore_ipc_client_flush(ev->client);
|
|
|
|
free(p);
|
|
|
|
}
|
2012-05-14 07:38:35 -07:00
|
|
|
|
2012-05-14 07:38:02 -07:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
2012-05-14 07:38:31 -07:00
|
|
|
_del(void *data __UNUSED__, int type __UNUSED__, Ecore_Ipc_Event_Client_Del *ev)
|
2012-05-14 07:38:02 -07:00
|
|
|
{
|
|
|
|
struct _Client *client;
|
|
|
|
|
|
|
|
if (!ev->client)
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
|
2012-05-14 07:38:49 -07:00
|
|
|
sprintf(msg_buf, "<%s> msg from <%p>", __func__, ev->client);
|
2012-05-14 07:38:02 -07:00
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:31 -07:00
|
|
|
ecore_ipc_client_del(ev->client);
|
2012-05-14 07:38:02 -07:00
|
|
|
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
2012-05-14 07:38:31 -07:00
|
|
|
_data(void *data __UNUSED__, int type __UNUSED__, Ecore_Ipc_Event_Client_Data *ev)
|
2012-05-14 07:38:02 -07:00
|
|
|
{
|
2012-05-14 07:38:35 -07:00
|
|
|
void *p;
|
|
|
|
char *msg="hello! - sent from the server";
|
|
|
|
int size = 0;
|
|
|
|
|
2012-05-14 07:38:49 -07:00
|
|
|
sprintf(msg_buf, "<%s> msg from <%p>", __func__, ev->client);
|
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:42 -07:00
|
|
|
|
2012-05-14 07:38:49 -07:00
|
|
|
Variant_st *v = packet_info_get(ev->data, ev->size);
|
2012-05-14 07:38:35 -07:00
|
|
|
if (v)
|
|
|
|
{
|
|
|
|
switch(packet_mapping_type_get(v->t.type))
|
|
|
|
{
|
|
|
|
case APP_ACK:
|
|
|
|
{
|
|
|
|
char *msg2="server asking for tree";
|
|
|
|
ack_st *t = v->data;
|
|
|
|
log_message(LOG_FILE, "a", "Got data from APP");
|
|
|
|
log_message(LOG_FILE, "a", t->text);
|
|
|
|
|
|
|
|
ack_st dt = { msg2 };
|
2012-05-14 07:38:49 -07:00
|
|
|
void *p = packet_compose(DAEMON_ACK, &dt, sizeof(dt), &size);
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
ecore_ipc_client_send(ev->client, 0,0,0,0,
|
|
|
|
EINA_FALSE, p, size);
|
|
|
|
ecore_ipc_client_flush(ev->client);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2012-05-14 07:38:35 -07:00
|
|
|
log_message(LOG_FILE, "a", "Asking tree from APP");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GUI_ACK:
|
|
|
|
{
|
2012-05-14 07:38:42 -07:00
|
|
|
int size = 0;
|
|
|
|
Eina_List *l;
|
|
|
|
tree_info_st *st;
|
2012-05-14 07:39:35 -07:00
|
|
|
st_tree_list tl;
|
2012-05-14 07:38:42 -07:00
|
|
|
EINA_LIST_FOREACH(trees, l, st)
|
|
|
|
{ /* Sending all trees */
|
2012-05-14 07:39:35 -07:00
|
|
|
tl.list = ((st_tree_list *) st->data)->list;
|
|
|
|
sprintf(msg_buf,"t = <%p>", tl.list);
|
2012-05-14 07:38:49 -07:00
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:39:35 -07:00
|
|
|
p = packet_compose(DAEMON_TREE_DATA, &tl,
|
|
|
|
sizeof(tl), &size);
|
|
|
|
|
2012-05-14 07:38:49 -07:00
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
ecore_ipc_client_send(ev->client, 0,0,0,0,
|
|
|
|
EINA_FALSE, p, size);
|
|
|
|
ecore_ipc_client_flush(ev->client);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(msg_buf, "DAEMON <%p> sending tree, size <%d>\n",
|
2012-05-14 07:38:42 -07:00
|
|
|
st->app, size);
|
|
|
|
|
2012-05-14 07:38:49 -07:00
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:42 -07:00
|
|
|
}
|
2012-05-14 07:38:35 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case APP_TREE_DATA:
|
2012-05-14 07:38:39 -07:00
|
|
|
{
|
2012-05-14 07:38:49 -07:00
|
|
|
sprintf(msg_buf, "<%s> got APP TREE DATA from <%p>", __func__, ev->client);
|
|
|
|
log_message(LOG_FILE, "a", msg_buf);
|
2012-05-14 07:38:42 -07:00
|
|
|
trees = tree_data_add(ev->client, v->data);
|
2012-05-14 07:38:39 -07:00
|
|
|
}
|
2012-05-14 07:38:35 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-05-14 07:38:49 -07:00
|
|
|
|
|
|
|
free(v); /* NOT variant_free(v), then comes from eet..decode */
|
2012-05-14 07:38:35 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
log_message(LOG_FILE, "a", "Failed to decode data.");
|
|
|
|
|
2012-05-14 07:38:49 -07:00
|
|
|
log_message(LOG_FILE, "a", "_data() finished");
|
2012-05-14 07:38:02 -07:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
/* END - Ecore communication callbacks */
|
|
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2012-05-14 07:38:31 -07:00
|
|
|
Ecore_Ipc_Server *svr;
|
|
|
|
Ecore_Ipc_Client *cl;
|
2012-05-14 07:38:02 -07:00
|
|
|
const Eina_List *clients, *l;
|
|
|
|
|
|
|
|
daemonize();
|
|
|
|
eina_init();
|
|
|
|
ecore_init();
|
2012-05-14 07:38:31 -07:00
|
|
|
ecore_ipc_init();
|
2012-05-14 07:38:02 -07:00
|
|
|
|
2012-05-14 07:38:31 -07:00
|
|
|
if (!(svr = ecore_ipc_server_add(ECORE_IPC_REMOTE_SYSTEM, LOCALHOST, PORT, NULL)))
|
2012-05-14 07:38:02 -07:00
|
|
|
exit(1);
|
|
|
|
|
2012-05-14 07:38:39 -07:00
|
|
|
ecore_ipc_server_data_size_max_set(svr, -1);
|
|
|
|
|
2012-05-14 07:38:31 -07:00
|
|
|
ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)_add, NULL);
|
|
|
|
ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)_del, NULL);
|
|
|
|
ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA, (Ecore_Event_Handler_Cb)_data, NULL);
|
2012-05-14 07:38:02 -07:00
|
|
|
|
|
|
|
ecore_main_loop_begin();
|
|
|
|
_daemon_cleanup();
|
|
|
|
|
|
|
|
return 0;
|
2012-05-14 07:37:49 -07:00
|
|
|
}
|