summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Zaoui <daniel.zaoui@yahoo.com>2015-09-29 16:14:20 +0300
committerDaniel Zaoui <daniel.zaoui@yahoo.com>2015-09-29 16:20:06 +0300
commit1daf7945607c2ea156d686b23b842667abb26b7f (patch)
treea910f5ffcf628e8ee16e562882dd46eda65a0136
parentc25562165fa3a425e10b231804633fcf88c28c0e (diff)
Cleaning and many fixes
- Separate sdb and no-sdb - No more magic for the moment - Functions renaming - Fix big packets issue - Fix su/su app for sdb mode - Bridge quits when parent exits or connection with daemon terminates - Delete client on disconnection - Comment Daemon self-launch
-rw-r--r--src/client.c271
-rw-r--r--src/debugd.c307
2 files changed, 236 insertions, 342 deletions
diff --git a/src/client.c b/src/client.c
index 684e68d..f89e04c 100644
--- a/src/client.c
+++ b/src/client.c
@@ -9,12 +9,17 @@
9 9
10#define BUFFER_SIZE 1000000 10#define BUFFER_SIZE 1000000
11 11
12typedef enum
13{
14 AUTO_MODE = 1 << 0,
15 SDB_MODE = 1 << 1
16} Client_Mode;
17static int mode = 0;
18
12Ecore_Exe *g_child = NULL; 19Ecore_Exe *g_child = NULL;
13Eina_Bool test_mode = EINA_FALSE;
14char *g_bridge_file = NULL; 20char *g_bridge_file = NULL;
15char *g_bridge_lock_file = NULL; 21char *g_bridge_lock_file = NULL;
16 22
17//static int _auto_remaining_tests = 10000;
18static int _auto_remaining_tests = 1000; 23static int _auto_remaining_tests = 1000;
19static int _auto_buf_size; 24static int _auto_buf_size;
20static char _auto_buf[BUFFER_SIZE]; 25static char _auto_buf[BUFFER_SIZE];
@@ -22,36 +27,41 @@ static char _auto_buf[BUFFER_SIZE];
22static Eina_Bool 27static Eina_Bool
23_send_data(char *msg, int size) 28_send_data(char *msg, int size)
24{ 29{
25 char tmp[1024]; 30 if (mode & SDB_MODE)
26 char msg2[BUFFER_SIZE]; 31 {
27 static char seq = 1; 32 char tmp[1024];
28 int nm; 33 char msg2[BUFFER_SIZE];
29 34 static char seq = 1;
30 if (!g_bridge_file) return EINA_FALSE;
31 35
32 sprintf(tmp, "sdb push msg %s &> /dev/null", g_bridge_file); 36 if (!g_bridge_file) return EINA_FALSE;
33 37
34 msg2[0] = seq++; 38 sprintf(tmp, "sdb push msg %s &> /dev/null", g_bridge_file);
35 39
36 memcpy(msg2 + 1, msg, size); 40 msg2[0] = seq++;
37 41
38 FILE *f = fopen("msg", "w+"); 42 memcpy(msg2 + 1, msg, size);
39 nm = fwrite(msg2, sizeof(char), size, f);
40 fflush(f);
41 fclose(f);
42 43
43 printf("Exec (size=%d): %s\n", size, tmp); 44 FILE *f = fopen("msg", "w+");
44 ecore_exe_run(tmp, NULL); 45 fwrite(msg2, sizeof(char), size, f);
46 fflush(f);
47 fclose(f);
45 48
46 sprintf(tmp, "sdb shell \"sleep 1 && echo 0 >> %s\"", g_bridge_lock_file); 49 printf("Exec (size=%d): %s\n", size, tmp);
47 printf("Exec: %s\n", tmp); 50 ecore_exe_run(tmp, NULL);
48 ecore_exe_run(tmp, NULL);
49 51
52 sprintf(tmp, "sdb shell \"sleep 1 && echo 0 >> %s\"", g_bridge_lock_file);
53 printf("Exec: %s\n", tmp);
54 ecore_exe_run(tmp, NULL);
55 }
56 else
57 {
58 ecore_exe_send(g_child, msg, size);
59 }
50 return EINA_TRUE; 60 return EINA_TRUE;
51} 61}
52 62
53static Eina_Bool 63static Eina_Bool
54_fd_handler_cb(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED) 64_recv_from_stdin(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
55{ 65{
56 char msg[BUFFER_SIZE]; 66 char msg[BUFFER_SIZE];
57 67
@@ -70,174 +80,132 @@ static void
70_pattern_auto_generate() 80_pattern_auto_generate()
71{ 81{
72 int i; 82 int i;
73 int *magic = (int *)_auto_buf, *size = (int *)(_auto_buf + sizeof(*magic)); 83 int *size = (int *)(_auto_buf);
74 char *msg = _auto_buf + sizeof(*magic) + sizeof(*size); 84 char *msg = _auto_buf + sizeof(*size);
75 if (!_auto_remaining_tests--) exit(0); 85 if (!_auto_remaining_tests--) exit(0);
76 do 86 do
77 { 87 {
78 _auto_buf_size = rand() % 32000; 88 _auto_buf_size = rand() % 100000;
79 } while(!_auto_buf_size); 89 } while(!_auto_buf_size);
80 *magic = 0xDEADBEEF;
81 *size = _auto_buf_size; 90 *size = _auto_buf_size;
82 printf("Generating pattern: size %d\n", _auto_buf_size); 91 printf("Generating pattern: size %d\n", _auto_buf_size);
83 for (i = 0; i < _auto_buf_size; i++) 92 for (i = 0; i < _auto_buf_size; i++)
84 { 93 {
85 msg[i] = rand() % 256; 94 msg[i] = rand() % 256;
86 } 95 }
87 _send_data(_auto_buf, _auto_buf_size + sizeof(*magic) + sizeof(*size)); 96 _send_data(_auto_buf, _auto_buf_size + sizeof(*size));
88} 97}
89 98
90static Eina_Bool 99static Eina_Bool
91_msg_from_child_auto_handler(void *fdata EINA_UNUSED, int type EINA_UNUSED, 100_msg_receive_from_bridge(void *fdata EINA_UNUSED, int type EINA_UNUSED,
92 void *event) 101 void *event)
93{ 102{
94 printf("Message from child\n");
95 Ecore_Exe_Event_Data *cdata = event;
96 if (!g_bridge_file)
97 {
98 printf("Initializing bridge file (size=%d)\n", cdata->size);
99 char msg[1024], tmp[1024];
100 strncpy(msg, cdata->data, cdata->size);
101 msg[cdata->size] = 0;
102 g_bridge_file = strdup(msg);
103 sprintf(tmp, "%s.lock", msg);
104 g_bridge_lock_file = strdup(tmp);
105 printf("Destination file set to %s\n", g_bridge_file);
106 return ECORE_CALLBACK_DONE;
107 }
108
109 static char msg[BUFFER_SIZE]; 103 static char msg[BUFFER_SIZE];
110 static int cur = 0, remaining = 0; 104 static int cur = 0, remaining = 0;
111 int *magic, *size; 105 printf("Message from bridge\n");
112 printf(" -- Received from bridge: %d bytes\n", cdata->size); 106 Ecore_Exe_Event_Data *cdata = event;
113 if (remaining <= 0) 107 Ecore_Exe *exe = cdata->exe;
108 if (mode & SDB_MODE)
114 { 109 {
115 printf(" ---- Doing magic checks\n"); 110 static Eina_Bool _su_exec = EINA_FALSE;
116 magic = (int *)cdata->data; 111 static Eina_Bool _su_app_exec = EINA_FALSE;
117 size = (int *)(cdata->data + sizeof(*magic)); 112 if (!_su_exec)
118
119 if (*magic != 0xDEADBEEF)
120 { 113 {
121 int off; 114 const char *cmd = "su\n";
122 char str[cdata->size + 1]; 115 ecore_exe_send(exe, cmd, strlen(cmd));
123 printf("Bad magic number %X. Data size: %d\n", *magic, cdata->size); 116 _su_exec = EINA_TRUE;
124 strncpy(str, cdata->data, cdata->size);
125 str[cdata->size] = 0;
126 printf(" -- Maybe it's a string: %s\n", str);
127 for (off = 0; off < cdata->size; off++)
128 {
129 char c = ((char *)cdata->data)[off];
130 if (off) printf(", ");
131 printf("%c", c);
132 }
133 printf("\n");
134 return ECORE_CALLBACK_DONE; 117 return ECORE_CALLBACK_DONE;
135 } 118 }
136 remaining = *size + sizeof(*magic) + sizeof(*size); 119 if (!_su_app_exec)
137 }
138 remaining -= cdata->size;
139 memcpy(msg + cur, cdata->data, cdata->size);
140 cur += cdata->size;
141
142 printf(" -- Check if there are remaining packets\n");
143 if (remaining <= 0)
144 {
145 remaining = 0;
146 printf(" ---- Received whole data\n");
147 cur = 0;
148 magic = (int *)msg;
149 size = (int *)(msg + sizeof(*magic));
150 if (*magic != 0xDEADBEEF) printf("Bad magic number %X\n", *magic);
151 else if (_auto_buf_size != *size)
152 printf("Bad size (%d-%d) for received buffer\n", _auto_buf_size, *size);
153 else if (memcmp(_auto_buf, msg+sizeof(*magic)+sizeof(*size), _auto_buf_size))
154 { 120 {
155 printf("Bad contents for received buffer\n"); 121 const char *cmd = "su app -c \"/home/developer/debugd bridge\"\n";
122 ecore_exe_send(exe, cmd, strlen(cmd));
123 _su_app_exec = EINA_TRUE;
124 return ECORE_CALLBACK_DONE;
156 } 125 }
157 else 126 if (!g_bridge_file)
158 { 127 {
159 printf(" ---- Received buffer ok\n"); 128 if (strncmp(cdata->data, "/home", 5)) return ECORE_CALLBACK_DONE;
160 _pattern_auto_generate(); 129 printf("Initializing bridge file (size=%d)\n", cdata->size);
130 char msg[1024], tmp[1024];
131 strncpy(msg, cdata->data, cdata->size);
132 msg[cdata->size] = 0;
133 g_bridge_file = strdup(msg);
134 sprintf(tmp, "%s.lock", msg);
135 g_bridge_lock_file = strdup(tmp);
136 printf("Destination file set to %s\n", g_bridge_file);
137 return ECORE_CALLBACK_DONE;
161 } 138 }
162 } 139 }
163 return ECORE_CALLBACK_DONE; 140 int *size;
164} 141 int i = 0, j = 0;
165 142 char *buf = malloc(cdata->size);
166static Eina_Bool 143 const char *data = cdata->data;
167_msg_from_child_handler(void *data EINA_UNUSED, int type EINA_UNUSED, 144 if (mode & SDB_MODE)
168 void *event)
169{
170 Ecore_Exe_Event_Data *cdata = event;
171 char msg[BUFFER_SIZE];
172
173 memcpy(msg, cdata->data, cdata->size);
174 msg[cdata->size] = 0;
175 fprintf(stdout, "Father: my child sent me (size=%d): %s\n", cdata->size, msg);
176
177 return ECORE_CALLBACK_DONE;
178}
179
180static Eina_Bool
181_su_timer(void *data EINA_UNUSED)
182{
183 Ecore_Exe *exe = data;
184 char *cmd = "su\n";
185 if (exe && !test_mode)
186 { 145 {
187 /* Once we have the shell, we will run the bridge start command. 146 while (j <= cdata->size)
188 * This is a workaround because 'sdb shell [command]' does
189 * '/bin/sh -' */
190 printf("Executing bridge command on sdb shell\n");
191 if (!ecore_exe_send(exe, cmd, strlen(cmd)))
192 { 147 {
193 printf("Failed sending cmd to sdb exe\n"); 148 if (data[j] == 0x0D && data[j+1] == 0x0A) j++;
194 149 buf[i++] = data[j++];
195 } 150 }
151 cdata->size = i;
196 } 152 }
197 return ECORE_CALLBACK_CANCEL; 153 else
198} 154 memcpy(buf, cdata->data, cdata->size);
155 if (!remaining)
156 {
157 size = (int *)buf;
158 remaining = (*size > cdata->size ? *size : cdata->size);
159 }
160 remaining -= cdata->size;
161 memcpy(msg + cur, buf, cdata->size);
162 free(buf);
163 cur += cdata->size;
164 printf("Received from bridge: %d bytes\n", cdata->size);
199 165
200static Eina_Bool 166 if (remaining <= 0)
201_bridge_timer(void *data)
202{
203 char *cmd = "/home/developer/debugd bridge\n";
204 Ecore_Exe *exe = data;
205 if (exe && !test_mode)
206 { 167 {
207 /* Once we have the shell, we will run the bridge start command. 168 cur = 0;
208 * This is a workaround because 'sdb shell [command]' does 169 remaining = 0;
209 * '/bin/sh -' */ 170 size = (int *)msg;
210 printf("Executing bridge command on sdb shell\n"); 171 /* Data is complete and recieved from daemon. lets try to unpack it */
211 if (!ecore_exe_send(exe, cmd, strlen(cmd))) 172 if (mode & AUTO_MODE)
212 { 173 {
213 printf("Failed sending cmd to sdb exe\n"); 174 if (_auto_buf_size != *size)
214 175 printf("Bad size (%d-%d) for received buffer\n", _auto_buf_size, *size);
176 else if (memcmp(_auto_buf, msg, _auto_buf_size))
177 {
178 printf("Bad contents for received buffer\n");
179 }
180 else
181 {
182 printf(" ---- Received buffer ok\n");
183 _pattern_auto_generate();
184 }
185 }
186 else
187 {
188 msg[cdata->size] = 0;
189 fprintf(stdout, "Father: my child sent me (size=%d): %s\n", cdata->size, msg);
215 } 190 }
216 } 191 }
217 return ECORE_CALLBACK_CANCEL; 192 return ECORE_CALLBACK_DONE;
218} 193}
219 194
220static Ecore_Exe * 195static Ecore_Exe *
221_init_bridge(void) 196_bridge_init(void)
222{ 197{
223 Ecore_Exe *exe; 198 Ecore_Exe *exe;
224 const char *cmd_bridge = "export PATH=$PATH:~/tizen-sdk/tools; sdb shell /home/developer/debugd bridge"; 199 const char *cmd_sdb = "export PATH=$PATH:~/tizen-sdk/tools; sdb shell /home/developer/debugd bridge sdb";
225 const char *cmd_test = "./debugd bridge"; 200 const char *cmd = "./debugd bridge";
226 201
227 const char *cmd; 202 if (mode & SDB_MODE) cmd = cmd_sdb;
228 const char *profile = getenv("DBG_PROFILE");
229 203
230 if (profile && !strcmp(profile, "test")) 204 printf ("Starting bridge with command: %s\n", cmd);
231 { 205 exe = ecore_exe_pipe_run(cmd,
232 printf ("Starting test profile\n");
233 cmd_bridge = cmd_test;
234 test_mode = EINA_TRUE;
235 }
236
237 printf ("Starting bridge with command: %s\n", cmd_bridge);
238 exe = ecore_exe_pipe_run(cmd_bridge,
239 ECORE_EXE_PIPE_WRITE | 206 ECORE_EXE_PIPE_WRITE |
240 ECORE_EXE_PIPE_READ, 207 ECORE_EXE_PIPE_READ |
208 ECORE_EXE_TERM_WITH_PARENT,
241 NULL); 209 NULL);
242 return exe; 210 return exe;
243} 211}
@@ -254,7 +222,7 @@ int main (int argc, char **argv)
254 if (!ecore_init()) 222 if (!ecore_init())
255 goto exit; 223 goto exit;
256 224
257 g_child = _init_bridge(); 225 g_child = _bridge_init();
258 226
259 if (!g_child) 227 if (!g_child)
260 { 228 {
@@ -262,20 +230,23 @@ int main (int argc, char **argv)
262 goto error; 230 goto error;
263 } 231 }
264 232
233 ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_receive_from_bridge, NULL);
265 if (argc > 1 && !strcmp(argv[1], "auto")) 234 if (argc > 1 && !strcmp(argv[1], "auto"))
266 { 235 {
267 ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_auto_handler, NULL); 236 mode |= AUTO_MODE;
268 ecore_timer_add(1.0, _on_timer, NULL); 237 ecore_timer_add(1.0, _on_timer, NULL);
269 } 238 }
270 else 239 else
271 { 240 {
272 ecore_main_fd_handler_add(STDIN_FILENO, 241 ecore_main_fd_handler_add(STDIN_FILENO,
273 ECORE_FD_READ, 242 ECORE_FD_READ,
274 _fd_handler_cb, 243 _recv_from_stdin,
275 NULL, NULL, NULL); 244 NULL, NULL, NULL);
276 ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_handler, NULL);
277 } 245 }
278 246
247 const char *profile = getenv("DBG_PROFILE");
248
249 if (!profile || strcmp(profile, "no_sdb")) mode |= SDB_MODE;
279 ecore_main_loop_begin(); 250 ecore_main_loop_begin();
280 251
281 ecore_exe_free(g_child); /* Does not affect the child process */ 252 ecore_exe_free(g_child); /* Does not affect the child process */
diff --git a/src/debugd.c b/src/debugd.c
index d7e646b..aa0e39e 100644
--- a/src/debugd.c
+++ b/src/debugd.c
@@ -17,26 +17,35 @@ static Ecore_Event_Handler *hnd_add = NULL;
17static Ecore_Event_Handler *hnd_del = NULL; 17static Ecore_Event_Handler *hnd_del = NULL;
18static Ecore_Event_Handler *hnd_data = NULL; 18static Ecore_Event_Handler *hnd_data = NULL;
19 19
20typedef enum
21{
22 DAEMON_MODE_LOOP_APP = 1 << 0,
23 DAEMON_MODE_BRIDGE = 1 << 1,
24 DAEMON_SDB_MODE = 1 << 2
25} Daemon_Mode;
26
27static int mode = 0;
28
20#define IPC_HEAD(_type) \ 29#define IPC_HEAD(_type) \
21 Ecore_Ipc_Event_Client_##_type *e = event; \ 30 Ecore_Ipc_Event_Client_##_type *e = event; \
22 if (ecore_ipc_client_server_get(e->client) != ipc) \ 31 if (ecore_ipc_client_server_get(e->client) != ipc) \
23 return ECORE_CALLBACK_PASS_ON 32 return ECORE_CALLBACK_PASS_ON
24
25static Eina_Bool 33static Eina_Bool
26_daemon_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 34_daemon_client_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
27{ 35{
28 IPC_HEAD(Add); 36 IPC_HEAD(Add);
29 printf("Client %p connected\n", e->client); 37 printf("Client %p connected\n", e->client);
38 ecore_ipc_client_data_size_max_set(e->client, BUFFER_SIZE);
30 39
31 return ECORE_CALLBACK_DONE; 40 return ECORE_CALLBACK_DONE;
32} 41}
33 42
34static Eina_Bool 43static Eina_Bool
35_daemon_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 44_daemon_client_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
36{ 45{
37 IPC_HEAD(Del); 46 IPC_HEAD(Del);
38 printf("Client deleted\n"); 47 printf("Client %p disconnected\n", e->client);
39 //ecore_ipc_client_del(e->client); 48 ecore_ipc_client_del(e->client);
40 return ECORE_CALLBACK_DONE; 49 return ECORE_CALLBACK_DONE;
41} 50}
42 51
@@ -64,12 +73,10 @@ _broadcast(Ecore_Ipc_Server *svr, int major, int minor, void *data, int size,
64} 73}
65 74
66static Eina_Bool 75static Eina_Bool
67_daemon_data_from_bridge(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 76_daemon_recv_from_bridge(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
68{ 77{
69 printf("Hello world\n");
70 IPC_HEAD(Data); 78 IPC_HEAD(Data);
71 int *magic = (int *)e->data; 79 printf("Received Data from client: size %d\n", e->size);
72 printf("Received Data from client: size %d magic %X\n", e->size, *magic);
73 80
74 _broadcast(ipc, 1, 0, e->data, e->size, e->client); 81 _broadcast(ipc, 1, 0, e->data, e->size, e->client);
75 82
@@ -78,7 +85,7 @@ _daemon_data_from_bridge(void *data EINA_UNUSED, int type EINA_UNUSED, void *eve
78 85
79////// Server //////////////////// 86////// Server ////////////////////
80static Eina_Bool 87static Eina_Bool
81_server_shutdown(void) 88_daemon_shutdown(void)
82{ 89{
83 if (!ipc) return EINA_TRUE; 90 if (!ipc) return EINA_TRUE;
84 91
@@ -95,103 +102,81 @@ _server_shutdown(void)
95} 102}
96 103
97static Eina_Bool 104static Eina_Bool
98_server_init(void) 105_daemon_init(void)
99{ 106{
100 if (ipc) return EINA_TRUE; 107 if (ipc) return EINA_TRUE;
101 108
102 ipc = ecore_ipc_server_add(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL); 109 ipc = ecore_ipc_server_add(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
103 if (!ipc) 110 if (!ipc)
104 { 111 {
105 _server_shutdown(); 112 _daemon_shutdown();
106 return EINA_FALSE; 113 return EINA_FALSE;
107 } 114 }
108 hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD, 115 hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD,
109 _daemon_add, NULL); 116 _daemon_client_add, NULL);
110 hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL, 117 hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL,
111 _daemon_del, NULL); 118 _daemon_client_del, NULL);
112 hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA, 119 hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA,
113 _daemon_data_from_bridge, NULL); 120 _daemon_recv_from_bridge, NULL);
121 ecore_ipc_server_data_size_max_set(ipc, BUFFER_SIZE);
114 return EINA_TRUE; 122 return EINA_TRUE;
115} 123}
116 124
117typedef enum
118{
119 DAEMON_MODE_NORMAL,
120 DAEMON_MODE_TEST,
121 DAEMON_MODE_BRIDGE
122} Daemon_Mode;
123
124Eina_Bool 125Eina_Bool
125_daemon(void) 126_daemon_launch(void)
126{ 127{
127 if (!_server_init()) 128 if (!_daemon_init())
128 { 129 {
129 return EINA_FALSE; 130 return EINA_FALSE;
130 } 131 }
131 ecore_main_loop_begin(); 132 ecore_main_loop_begin();
132 _server_shutdown(); 133 _daemon_shutdown();
133 134
134 return EINA_TRUE; 135 return EINA_TRUE;
135} 136}
136 137
137/////// Bridge-Daemon Comm. ///////// 138/////// Bridge-Daemon Comm. /////////
138static Ecore_Ipc_Server *srv_ipc = NULL; 139static Ecore_Ipc_Server *srv_ipc = NULL;
139/* Server-to-client cb's */
140static Ecore_Event_Handler *svr_hnd_add = NULL;
141static Ecore_Event_Handler *svr_hnd_del = NULL;
142static Ecore_Event_Handler *svr_hnd_data = NULL;
143 140
144const char *debug_prefix = "."; 141const char *debug_prefix = ".";
145 142
146
147static Eina_Bool 143static Eina_Bool
148_cb_server_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 144_bridge_conn_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
149{ 145{
146 ecore_main_loop_quit();
150 return ECORE_CALLBACK_DONE; 147 return ECORE_CALLBACK_DONE;
151} 148}
152 149
153static Eina_Bool 150static Eina_Bool
154_cb_server_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 151_bridge_recv_from_daemon(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
155{ 152{
156 return ECORE_CALLBACK_DONE; 153 Ecore_Ipc_Event_Server_Data *e = event;
157} 154 //int nb = fwrite(e->data, e->size, 1, stdout);
158 155 int nb = fwrite(e->data, e->size, 1, stdout);
159#define SVR_IPC_HEAD(_type) \ 156 fflush(stdout);
160 Ecore_Ipc_Event_Server_##_type *e = event; \
161 if (e->server != srv_ipc) \
162 return ECORE_CALLBACK_PASS_ON
163
164static Eina_Bool
165_bridge_data_from_daemon(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
166{
167 SVR_IPC_HEAD(Data);
168
169 if (e->major == 1) // registration
170 {
171 FILE *tmp;
172 static int sent_times = 1;
173 char str[1024];
174 //int nb = fwrite(e->data, e->size, 1, stdout);
175 int nb = fwrite(e->data, e->size, 1, stdout);
176 fflush(stdout);
177
178 sprintf(str, "/home/developer/sent%d", sent_times);
179 tmp = fopen(str, "w+");
180 fprintf(tmp, "item size: %d, written items: %d\n", e->size, nb);
181 fflush(tmp);
182 fclose(tmp);
183
184 sprintf(str, "/home/developer/sent%d.data", sent_times);
185 tmp = fopen(str, "w+");
186 fwrite(e->data, e->size, 1, tmp);
187 fflush(tmp);
188 fclose(tmp);
189 157
190 sent_times++; 158#if 0
191 } 159 FILE *tmp;
160 char str[1024];
161 static int sent_times = 1;
162 sprintf(str, "/home/developer/sent%d", sent_times);
163 tmp = fopen(str, "w+");
164 fprintf(tmp, "item size: %d, written items: %d\n", e->size, nb);
165 fflush(tmp);
166 fclose(tmp);
167
168 sprintf(str, "/home/developer/sent%d.data", sent_times);
169 tmp = fopen(str, "w+");
170 fwrite(e->data, e->size, 1, tmp);
171 fflush(tmp);
172 fclose(tmp);
173
174 sent_times++;
175#endif
192 return ECORE_CALLBACK_DONE; 176 return ECORE_CALLBACK_DONE;
193} 177}
194 178
179#if 0
195static void 180static void
196_ipc_launch(void) 181_ipc_launch(void)
197{ 182{
@@ -212,12 +197,14 @@ _ipc_launch(void)
212 //fprintf(stderr, "Launched server but could not connect after 500 tries\n"); 197 //fprintf(stderr, "Launched server but could not connect after 500 tries\n");
213 } 198 }
214} 199}
200#endif
215 201
216static Eina_Bool 202static Eina_Bool
217_bridge_init(void) 203_bridge_init(void)
218{ 204{
219 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL); 205 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
220 206
207#if 0
221 if (!srv_ipc) _ipc_launch(); 208 if (!srv_ipc) _ipc_launch();
222 209
223 if (!srv_ipc) 210 if (!srv_ipc)
@@ -228,28 +215,29 @@ _bridge_init(void)
228 } 215 }
229 svr_hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_ADD, 216 svr_hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_ADD,
230 _cb_server_add, NULL); 217 _cb_server_add, NULL);
231 svr_hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL, 218#endif
232 _cb_server_del, NULL); 219 ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL,
233 svr_hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA, 220 _bridge_conn_del, NULL);
234 _bridge_data_from_daemon, NULL); 221 if (srv_ipc)
222 ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA,
223 _bridge_recv_from_daemon, NULL);
224 ecore_ipc_server_data_size_max_set(srv_ipc, BUFFER_SIZE);
235 225
236 return EINA_TRUE;; 226 return EINA_TRUE;;
237} 227}
238 228
239/////// Bridge-CLI Comm ///////// 229/////// Bridge-CLI Comm /////////
240static Eina_Bool 230static Eina_Bool
241_fd_handler_cb(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED) 231_bridge_recv_from_stdin(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
242{ 232{
243 char msg[BUFFER_SIZE], *tmp; 233 char msg[BUFFER_SIZE], *tmp;
244 int *magic = (int *)msg, *size = (int *)(msg + sizeof(*magic)); 234 int *size = (int *)msg;
245 int len; 235 int len;
246 236
247 fread(magic, sizeof(*magic), 1, stdin);
248 if (*magic != 0xDEADBEEF) return ECORE_CALLBACK_RENEW;
249 fread(size, sizeof(*size), 1, stdin); 237 fread(size, sizeof(*size), 1, stdin);
250 len = *size; 238 len = *size;
251 //fprintf(stderr, "Bridge: have to receive from client %d+8 bytes\n", *size); 239 //fprintf(stderr, "Bridge: have to receive from client %d+8 bytes\n", *size);
252 tmp = msg + sizeof(*magic) + sizeof(*size); 240 tmp = msg + sizeof(*size);
253 while(len > 0) 241 while(len > 0)
254 { 242 {
255 int nb = fread(tmp, 1, len, stdin); 243 int nb = fread(tmp, 1, len, stdin);
@@ -262,41 +250,16 @@ _fd_handler_cb(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
262 tmp += nb; 250 tmp += nb;
263 } 251 }
264 //fprintf(stderr, "Bridge: received from client %d+8 bytes\n", *size); 252 //fprintf(stderr, "Bridge: received from client %d+8 bytes\n", *size);
265 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, msg, *size + sizeof(*magic) + sizeof(*size)); 253 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, msg, *size + sizeof(*size));
266 return ECORE_CALLBACK_RENEW; 254 return ECORE_CALLBACK_RENEW;
267} 255}
268 256
269const char *tmp_dir = "/home/developer/tmp";
270
271static char *
272_check_strip_cmd(char* cmd)
273{
274 static char seq = 1;
275 char tmp = *cmd;
276
277 if (tmp != seq)
278 {
279 return NULL;
280 }
281
282 seq++; /* all ok. increment seq */
283 return cmd + 1;
284}
285
286static void
287_process_data(char *data, int size)
288{
289 //fprintf(stderr, "Bridge: received from client %d+8 bytes\n", *size);
290 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, data, size);
291}
292
293static void 257static void
294_lock_file_monitor_cb(void *data, Ecore_File_Monitor *em, 258_lock_file_monitor_cb(void *data, Ecore_File_Monitor *em,
295 Ecore_File_Event event, const char *path) 259 Ecore_File_Event event, const char *path)
296{ 260{
297 char *bridge_file = data; 261 char *bridge_file = data;
298 char cmd[BUFFER_SIZE]; 262 char cmd[BUFFER_SIZE];
299 char *cmd2;
300 263
301 if (event != ECORE_FILE_EVENT_CLOSED) return; 264 if (event != ECORE_FILE_EVENT_CLOSED) return;
302 265
@@ -306,105 +269,73 @@ _lock_file_monitor_cb(void *data, Ecore_File_Monitor *em,
306 int nm = fread(cmd, sizeof(char), BUFFER_SIZE, f); 269 int nm = fread(cmd, sizeof(char), BUFFER_SIZE, f);
307 fclose(f); 270 fclose(f);
308 if (!nm) return; 271 if (!nm) return;
309 cmd2 = _check_strip_cmd(cmd);
310 272
311 if (cmd2) 273 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, cmd, nm);
312 {
313 _process_data(cmd2, nm); /* minus the seq counter */
314 }
315} 274}
316 275
317static Eina_Bool 276static Eina_Bool
318_bridge(void) 277_bridge_launch(void)
319{ 278{
320 char tmp[1024], tmp2[1024]; 279 Eina_Bool ret = EINA_FALSE;
321 FILE *f; 280 if (!ecore_file_init()) goto end;
322 Ecore_File_Monitor *fm;
323
324 /* We use files here. Yeah... */
325 if (!ecore_file_init())
326 {
327 return EINA_FALSE;
328 }
329
330 /* Send first message to client with the required cmd file path to push */
331 sprintf(tmp, "%s/bridge-%d", tmp_dir, getpid());
332
333 /* Sending monitored file that is pushed from client */
334 fprintf(stdout, "%s", tmp);
335 fflush(stdout);
336
337 sprintf(tmp2, "%s/bridge-%d.lock", tmp_dir, getpid());
338 f = fopen(tmp2, "w+");
339 fclose(f);
340 f = fopen("/home/developer/lock", "w+");
341 fprintf(f, "%s\n", tmp2);
342 fclose(f);
343 fm = ecore_file_monitor_add(tmp2, _lock_file_monitor_cb, tmp);
344 281
345 /* Initialize debug daemon */ 282 /* Initialize debug daemon */
346 //fprintf(stderr, "This is the bridge process..\n"); 283 if (!_bridge_init()) goto end;
347 if (!_bridge_init()) 284
285 if (mode & DAEMON_SDB_MODE)
348 { 286 {
349 //fprintf(stderr, "Exiting...\n"); 287 const char *tmp_dir = "/home/developer/tmp";
350 return EINA_FALSE; 288 FILE *f;
351 } 289 char tmp[1024], tmp2[1024];
290 /* Send first message to client with the required cmd file path to push */
291 sprintf(tmp, "%s/bridge-%d", tmp_dir, getpid());
292
293 /* Sending monitored file that is pushed from client */
294 fprintf(stdout, "%s", tmp);
295 fflush(stdout);
352 296
353 ecore_main_fd_handler_add(STDIN_FILENO, 297 sprintf(tmp2, "%s/bridge-%d.lock", tmp_dir, getpid());
298 f = fopen(tmp2, "w+");
299 fclose(f);
300 f = fopen("/home/developer/lock", "w+");
301 fprintf(f, "%s\n", tmp2);
302 fclose(f);
303 ecore_file_monitor_add(tmp2, _lock_file_monitor_cb, tmp);
304 }
305 else
306 {
307 ecore_main_fd_handler_add(STDIN_FILENO,
354 ECORE_FD_READ, 308 ECORE_FD_READ,
355 _fd_handler_cb, 309 _bridge_recv_from_stdin,
356 NULL, NULL, NULL); 310 NULL, NULL, NULL);
311 }
312 ret = EINA_TRUE;
357 ecore_main_loop_begin(); 313 ecore_main_loop_begin();
314end:
358 ecore_file_shutdown(); 315 ecore_file_shutdown();
359 316
360 return EINA_TRUE; 317 return ret;
361} 318}
362 319
363static Eina_Bool 320static Eina_Bool
364_test_data_from_daemon(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 321_loop_app_recv_from_daemon(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
365{ 322{
366 SVR_IPC_HEAD(Data); 323 Ecore_Ipc_Event_Server_Data *e = event;
367 324
368 int *magic = (int *)e->data; 325 printf("Looping packet of size %d\n", e->size);
369 printf("Received Message: size %d magic %X\n", e->size, *magic); 326 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, e->data, e->size);
370 if (e->major == 1)
371 {
372 printf("Test client sending data\n");
373 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, e->data, e->size);
374 }
375 return ECORE_CALLBACK_DONE; 327 return ECORE_CALLBACK_DONE;
376} 328}
377 329
378static Eina_Bool 330static Eina_Bool
379_test_client_init(void) 331_loop_app_launch(void)
380{
381 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
382
383 if (!srv_ipc)
384 {
385 ecore_ipc_shutdown();
386 return EINA_FALSE;
387 }
388 svr_hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_ADD,
389 _cb_server_add, NULL);
390 svr_hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL,
391 _cb_server_del, NULL);
392 svr_hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA,
393 _test_data_from_daemon, NULL);
394
395 return EINA_TRUE;;
396}
397
398static Eina_Bool
399_test(void)
400{ 332{
401 printf("Test mode\n"); 333 printf("Test mode\n");
402 /* Initialize debug daemon */ 334 /* Initialize debug daemon */
403 if (!_test_client_init()) 335 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
404 { 336 ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA,
405 printf("Could not connect to server\n"); 337 _loop_app_recv_from_daemon, NULL);
406 return EINA_FALSE; 338
407 }
408 ecore_main_loop_begin(); 339 ecore_main_loop_begin();
409 340
410 return EINA_TRUE; 341 return EINA_TRUE;
@@ -416,7 +347,6 @@ _test(void)
416 347
417int main (int argc, char **argv) 348int main (int argc, char **argv)
418{ 349{
419 Daemon_Mode mode;
420 if (!ecore_init()) goto exit; 350 if (!ecore_init()) goto exit;
421 /* All modes require ipc comm. */ 351 /* All modes require ipc comm. */
422 if (!ecore_ipc_init()) 352 if (!ecore_ipc_init())
@@ -425,34 +355,27 @@ int main (int argc, char **argv)
425 goto exit; 355 goto exit;
426 } 356 }
427 357
428 mode = DAEMON_MODE_NORMAL;
429 if (argc > 1) 358 if (argc > 1)
430 { 359 {
431 if (!strcmp(argv[1], "bridge")) 360 if (!strcmp(argv[1], "bridge"))
432 { 361 {
433 mode = DAEMON_MODE_BRIDGE; 362 mode = DAEMON_MODE_BRIDGE;
363 if (argc > 2 && !strcmp(argv[2], "sdb")) mode |= DAEMON_SDB_MODE;
434 } 364 }
435 else if (!strcmp(argv[1], "test")) 365 else if (!strcmp(argv[1], "loop"))
436 { 366 {
437 mode = DAEMON_MODE_TEST; 367 mode = DAEMON_MODE_LOOP_APP;
438 } 368 }
439
440 } 369 }
441 370
442 if (mode == DAEMON_MODE_BRIDGE) 371 if (mode & DAEMON_MODE_BRIDGE)
443 { 372 _bridge_launch();
444 if (!_bridge()) 373 else if (mode & DAEMON_MODE_LOOP_APP)
445 { 374 _loop_app_launch();
446 ecore_ipc_shutdown();
447 ecore_shutdown();
448 goto exit;
449 }
450 }
451 else if (mode == DAEMON_MODE_TEST)
452 _test();
453 else 375 else
454 _daemon(); 376 _daemon_launch();
455 377
378 ecore_ipc_shutdown();
456 ecore_shutdown(); 379 ecore_shutdown();
457 380
458exit: 381exit: