2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/types.h>
29 #include <X11/extensions/Xdamage.h>
30 #include <X11/extensions/Xfixes.h>
33 #include <glib-object.h>
36 #include <com-core_packet.h>
39 #include <livebox-service.h>
46 #define PROMPT "liveinfo "
96 Ecore_Fd_Handler *fd_handler;
97 Ecore_Fd_Handler *in_handler;
101 struct node *targetdir;
114 struct node *quick_search_node;
117 .fifo_handle = -EINVAL,
125 .input_fd = STDIN_FILENO,
131 .quick_search_node = NULL,
141 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler);
143 static Eina_Bool process_line_cb(void *data)
145 input_cb(NULL, NULL);
146 return ECORE_CALLBACK_CANCEL;
149 static inline void prompt(const char *cmdline)
153 if (s_info.input_fd != STDIN_FILENO) {
154 /* To prevent recursive call, add function to the main loop (idler) */
155 ecore_idler_add(process_line_cb, NULL);
159 path = node_to_abspath(s_info.curdir);
160 printf(PROMPT"%s # %s", path, cmdline ? cmdline : "");
164 static void provider_del_cb(struct node *node)
168 info = node_data(node);
178 static void package_del_cb(struct node *node)
180 struct package *info;
182 info = node_data(node);
187 free(info->slavename);
192 static void inst_del_cb(struct node *node)
194 struct instance *info;
196 info = node_data(node);
202 free(info->category);
207 static inline void ls(void)
214 struct node *next_node;
216 if (!(node_mode(s_info.targetdir) & NODE_READ)) {
217 printf("Access denied\n");
221 is_package = node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "package");
222 is_provider = !is_package && node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "provider");
223 is_instance = !is_package && !is_provider && node_parent(s_info.targetdir) && node_name(node_parent(s_info.targetdir)) && !strcmp(node_name(node_parent(s_info.targetdir)), "package");
225 node = node_child(s_info.targetdir);
228 struct package *info;
230 next_node = node_next_sibling(node);
231 if (node_age(node) != s_info.age) {
232 node_delete(node, package_del_cb);
237 info = node_data(node);
238 printf(" %3d %20s %5s ", info->inst_count, info->slavename ? info->slavename : "(none)", info->abi ? info->abi : "?");
239 } else if (is_provider) {
242 next_node = node_next_sibling(node);
243 if (node_age(node) != s_info.age) {
244 node_delete(node, provider_del_cb);
249 info = node_data(node);
250 printf("%6d %3d %5s %5.2f ", info->pid, info->loaded_inst, info->abi ? info->abi : "?", info->ttl);
251 } else if (is_instance) {
252 struct instance *info;
256 next_node = node_next_sibling(node);
258 if (node_age(node) != s_info.age) {
259 node_delete(node, inst_del_cb);
264 info = node_data(node);
266 printf(" %5.2f %6s %10s %10s %4dx%-4d ", info->period, info->state, info->cluster, info->category, info->width, info->height);
267 snprintf(buf, sizeof(buf), "/opt/usr/share/live_magazine/reader/%s", node_name(node));
268 if (lstat(buf, &stat) < 0)
269 printf("%3d ERR ", errno);
271 printf("%2.2lf KB ", (double)stat.st_size / 1024.0f);
274 if (node_type(node) == NODE_DIR)
275 printf("%s/", node_name(node));
276 else if (node_type(node) == NODE_FILE)
277 printf("%s", node_name(node));
280 node = node_next_sibling(node);
284 printf("Total: %d\n", cnt);
287 static void send_slave_list(void)
289 struct packet *packet;
291 if (s_info.cmd != NOP) {
292 printf("Previous command is not finished\n");
296 packet = packet_create_noack("slave_list", "d", 0.0f);
298 printf("Failed to create a packet\n");
302 com_core_packet_send_only(s_info.fd, packet);
303 packet_destroy(packet);
304 s_info.cmd = SLAVE_LIST;
309 * var = debug, slave_max_load
312 static void send_command(const char *cmd, const char *var, const char *val)
314 struct packet *packet;
316 if (s_info.cmd != NOP) {
317 printf("Previous command is not finished\n");
321 packet = packet_create_noack("master_ctrl", "sss", cmd, var, val);
322 com_core_packet_send_only(s_info.fd, packet);
323 packet_destroy(packet);
324 s_info.cmd = MASTER_CTRL;
328 static int pkglist_cb(const char *appid, const char *lbid, int is_prime, void *data)
330 struct node *parent = data;
332 struct package *info;
334 node = node_find(parent, lbid);
336 info = node_data(node);
338 printf("Invalid node\n");
343 info->pkgid = strdup(appid);
345 printf("Error: %s\n", strerror(errno));
349 node_set_age(node, s_info.age);
353 info = calloc(1, sizeof(*info));
355 printf("Error: %s\n", strerror(errno));
359 info->pkgid = strdup(appid);
361 printf("Error: %s\n", strerror(errno));
366 info->primary = is_prime;
368 node = node_create(parent, lbid, NODE_DIR);
375 node_set_mode(node, NODE_READ | NODE_EXEC);
376 node_set_data(node, info);
377 node_set_age(node, s_info.age);
381 static void send_pkg_list(void)
383 struct packet *packet;
385 if (s_info.cmd != NOP) {
386 printf("Previous command is not finished\n");
390 packet = packet_create_noack("pkg_list", "d", 0.0f);
392 printf("Failed to create a packet\n");
396 com_core_packet_send_only(s_info.fd, packet);
397 packet_destroy(packet);
398 s_info.cmd = PKG_LIST;
401 livebox_service_get_pkglist(pkglist_cb, s_info.targetdir);
404 static void send_inst_delete(void)
406 struct packet *packet;
409 struct instance *inst;
411 if (s_info.cmd != NOP) {
412 printf("Previous command is not finished\n");
416 parent = node_parent(s_info.targetdir);
418 printf("Invalid argument\n");
422 if (!node_parent(parent)) {
423 printf("Invalid argument\n");
427 name = node_name(node_parent(parent));
428 if (!name || strcmp(name, "package")) {
429 printf("Invalid argument\n");
433 inst = node_data(s_info.targetdir);
434 name = node_name(parent);
436 packet = packet_create_noack("pkg_ctrl", "sss", "rminst", name, inst->id);
437 com_core_packet_send_only(s_info.fd, packet);
438 packet_destroy(packet);
439 s_info.cmd = INST_CTRL;
443 static void send_inst_list(const char *pkgname)
445 struct packet *packet;
447 if (s_info.cmd != NOP) {
448 printf("Previous command is not finished\n");
452 packet = packet_create_noack("inst_list", "s", pkgname);
454 printf("Failed to create a packet\n");
458 com_core_packet_send_only(s_info.fd, packet);
459 packet_destroy(packet);
460 s_info.cmd = INST_LIST;
464 static inline void help(void)
466 printf("liveinfo - Livebox utility\n");
467 printf("------------------------------ [Option] ------------------------------\n");
468 printf("-b Batch mode\n");
469 printf("------------------------------ [Command list] ------------------------------\n");
470 printf("
\e[32mcd [PATH] - Change directory
\e[0m\n");
471 printf("
\e[32mls [ | PATH] - List up content as a file
\e[0m\n");
472 printf("
\e[32mrm [PKG_ID|INST_ID] - Delete package or instance
\e[0m\n");
473 printf("
\e[32mstat [path] - Display the information of given path
\e[0m\n");
474 printf("
\e[32mset [debug] [on|off] Set the control variable of master provider
\e[0m\n");
475 printf("
\e[32mx damage Pix x y w h - Create damage event for given pixmap
\e[0m\n");
476 printf("
\e[32mx move Pix x y - Move the window
\e[0m\n");
477 printf("
\e[32mx resize Pix w h - Resize the window
\e[0m\n");
478 printf("
\e[32mx map Pix - Show the window
\e[0m\n");
479 printf("
\e[32mx unmap Pix - Hide the window
\e[0m\n");
480 printf("
\e[32msh [command] Execute shell command, [command] should be abspath
\e[0m\n");
481 printf("
\e[32mexit -
\e[0m\n");
482 printf("
\e[32mquit -
\e[0m\n");
483 printf("----------------------------------------------------------------------------\n");
486 static inline void init_directory(void)
489 s_info.rootdir = node_create(NULL, NULL, NODE_DIR);
492 node_set_mode(s_info.rootdir, NODE_READ | NODE_EXEC);
494 node = node_create(s_info.rootdir, "provider", NODE_DIR);
496 node_destroy(s_info.rootdir);
497 s_info.rootdir = NULL;
500 node_set_mode(node, NODE_READ | NODE_EXEC);
502 node = node_create(s_info.rootdir, "package", NODE_DIR);
504 node_destroy(node_child(s_info.rootdir));
505 node_destroy(s_info.rootdir);
506 s_info.rootdir = NULL;
509 node_set_mode(node, NODE_READ | NODE_EXEC);
511 s_info.curdir = s_info.rootdir;
515 static inline void fini_directory(void)
519 static inline struct node *update_target_dir(const char *cmd)
523 node = (*cmd == '/') ? s_info.rootdir : s_info.curdir;
524 node = node_find(node, cmd);
529 static int get_token(const char *src, char *out)
532 while (*src && *src == ' ') src++;
537 while (*src && *src != ' ') {
546 static inline int do_stat(const char *cmd)
561 while (*cmd && *cmd == ' ') cmd++;
564 printf("Invalid argument\n");
568 node = node_find(*cmd == '/' ? s_info.rootdir : s_info.curdir, cmd);
570 printf("Invalid path\n");
576 parent = node_parent(node);
578 if (!node_name(parent)) {
579 printf("%s has no info\n", node_name(node));
581 } else if (!strcmp(node_name(parent), "package")) {
582 type = (i == 0) ? PKG : PKG_INSTANCE;
584 } else if (!strcmp(node_name(parent), "provider")){
585 type = (i == 0) ? PROVIDER : PROVIDER_INSTANCE;
589 parent = node_parent(parent);
592 printf("%s is invalid path\n", node_name(node));
599 tmp = livebox_service_i18n_name(node_name(node), NULL);
600 printf("Name: %s (", tmp);
603 i = livebox_service_is_enabled(node_name(node));
604 printf("%s)\n", i ? "enabled" : "disabled");
606 tmp = livebox_service_i18n_icon(node_name(node), NULL);
607 printf("Icon: %s\n", tmp);
610 tmp = livebox_service_provider_name(node_name(node));
611 printf("Provider: %s (content:", tmp);
614 tmp = livebox_service_content(node_name(node));
615 printf("%s)\n", tmp);
618 tmp = livebox_service_lb_script_path(node_name(node));
619 printf("LB Script: %s (", tmp);
622 tmp = livebox_service_lb_script_group(node_name(node));
623 printf("%s)\n", tmp);
626 tmp = livebox_service_pd_script_path(node_name(node));
627 printf("PD Script: %s (", tmp);
630 tmp = livebox_service_pd_script_group(node_name(node));
631 printf("%s)\n", tmp);
634 i = livebox_service_mouse_event(node_name(node));
635 printf("Mouse event: %s\n", i ? "enabled" : "disabled");
637 i = livebox_service_touch_effect(node_name(node));
638 printf("Touch effect: %s\n", i ? "enabled" : "disabled");
641 printf("Not supported yet\n");
644 printf("Not supported yet\n");
646 case PROVIDER_INSTANCE:
647 printf("Not supported yet\n");
650 printf("Not supported yet\n");
653 printf("Invalid type\n");
660 static inline int do_set(const char *cmd)
663 char variable[4096] = { '0', };
666 i = get_token(cmd, variable);
669 while (*cmd && *cmd == ' ') cmd++;
672 printf("Invalid argument(%s): set [VAR] [VAL]\n", cmd);
676 send_command("set", variable, cmd);
680 static inline int do_get(const char *cmd)
684 while (*cmd && *cmd == ' ') cmd++;
686 printf("Invalid argument(%s): get [VAR]\n", cmd);
690 send_command("get", cmd, "");
694 static inline int do_ls(const char *cmd)
701 while (*cmd && *cmd == ' ')
704 s_info.targetdir = *cmd ? update_target_dir(cmd) : s_info.curdir;
705 if (!s_info.targetdir) {
706 printf("%s is not exists\n", cmd);
710 name = node_name(s_info.targetdir);
712 if (!strcmp(name, "package")) {
713 if (s_info.cmd == NOP) {
718 printf("Waiting the server response\n");
720 } else if (!strcmp(name, "provider")) {
721 if (s_info.cmd == NOP) {
726 printf("Waiting the server response\n");
731 parent = node_parent(s_info.targetdir);
732 if (parent && node_name(parent)) {
733 if (!strcmp(node_name(parent), "package")) {
734 if (s_info.cmd != NOP) {
735 printf("Waiting the server response\n");
739 send_inst_list(name);
748 static inline int do_cd(const char *cmd)
752 while (*cmd && *cmd == ' ')
758 if (s_info.cmd != NOP) {
759 printf("Waiting the server response\n");
763 s_info.targetdir = update_target_dir(cmd);
764 if (!s_info.targetdir) {
765 printf("%s is not exists\n", cmd);
769 if (node_type(s_info.targetdir) != NODE_DIR) {
770 printf("Unable change directory to %s\n", cmd);
774 if (!(node_mode(s_info.targetdir) & NODE_EXEC)) {
775 printf("Access denied %s\n", cmd);
779 s_info.curdir = s_info.targetdir;
783 static inline int do_rm(const char *cmd)
786 while (*cmd && *cmd == ' ') cmd++;
790 if (s_info.cmd != NOP) {
791 printf("Waiting the server response\n");
795 s_info.targetdir = update_target_dir(cmd);
796 if (!s_info.targetdir) {
797 printf("%s is not exists\n", cmd);
801 if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
802 printf("Access denied %s\n", cmd);
810 #if !defined(WCOREDUMP)
811 #define WCOREDUMP(a) 0
814 static inline void do_sh(const char *cmd)
820 while (*cmd && *cmd == ' ') cmd++;
830 while (idx < sizeof(command) && *cmd && *cmd != ' ')
831 command[idx++] = *cmd++;
834 if (execl(command, cmd, NULL) < 0)
835 printf("Failed to execute: %s\n", strerror(errno));
838 } else if (pid < 0) {
839 printf("Failed to create a new process: %s\n", strerror(errno));
842 if (waitpid(pid, &status, 0) < 0) {
843 printf("error: %s\n", strerror(errno));
845 if (WIFEXITED(status)) {
846 printf("Exit: %d\n", WEXITSTATUS(status));
847 } else if (WIFSIGNALED(status)) {
848 printf("Terminated by %d %s\n", WTERMSIG(status), WCOREDUMP(status) ? " - core generated" : "");
849 } else if (WIFSTOPPED(status)) {
850 printf("Stopped by %d\n", WSTOPSIG(status));
851 } else if (WIFCONTINUED(status)) {
852 printf("Child is resumed\n");
858 static inline void do_x(const char *cmd)
864 while (*cmd && *cmd == ' ') cmd++;
868 disp = XOpenDisplay(NULL);
870 printf("Failed to connect to the X\n");
874 if (!strncasecmp(cmd, "damage ", 7)) {
877 XserverRegion region;
882 if (sscanf(cmd, "%u %d %d %d %d", &winId, &x, &y, &w, &h) != 5) {
883 printf("Invalid argument\nx damage WINID_DEC X Y W H\n");
890 region = XFixesCreateRegion(disp, &rect, 1);
891 XDamageAdd(disp, winId, region);
892 XFixesDestroyRegion(disp, region);
895 printf("Damage: %u %d %d %d %d\n", winId, x, y, w, h);
896 } else if (!strncasecmp(cmd, "resize ", 7)) {
903 if (sscanf(cmd, "%u %d %d", &winId, &w, &h) != 3) {
904 printf("Invalid argument\nx resize WINID_DEC W H\n");
908 XResizeWindow(disp, winId, w, h);
909 printf("Resize: %u %d %d\n", winId, w, h);
910 } else if (!strncasecmp(cmd, "move ", 5)) {
916 if (sscanf(cmd, "%u %d %d", &winId, &x, &y) != 3) {
917 printf("Invalid argument\nx move WINID_DEC X Y\n");
921 XMoveWindow(disp, winId, x, y);
922 printf("Move: %u %d %d\n", winId, x, y);
923 } else if (!strncasecmp(cmd, "map ", 4)) {
926 if (sscanf(cmd, "%u", &winId) != 1) {
927 printf("Invalid argument\nx map WINID_DEC\n");
930 XMapRaised(disp, winId);
931 printf("Map: %u\n", winId);
932 } else if (!strncasecmp(cmd, "unmap ", 6)) {
935 if (sscanf(cmd, "%u", &winId) != 1) {
936 printf("Invalid argument\nx unmap WINID_DEC\n");
939 XUnmapWindow(disp, winId);
940 printf("Unmap: %u\n", winId);
942 printf("Unknown command\n");
948 static inline void put_command(const char *cmd)
950 if (s_info.history[s_info.history_top]) {
951 free(s_info.history[s_info.history_top]);
952 s_info.history[s_info.history_top] = NULL;
955 s_info.history[s_info.history_top] = strdup(cmd);
956 s_info.history_top = (s_info.history_top + !!s_info.history[s_info.history_top]) % (sizeof(s_info.history) / sizeof(s_info.history[0]));
959 static inline const char *get_command(int idx)
961 idx = s_info.history_top + idx;
963 idx += (sizeof(s_info.history) / sizeof(s_info.history[0]));
965 return s_info.history[idx];
968 static inline void do_command(const char *cmd)
970 /* Skip the first spaces */
971 while (*cmd && *cmd == ' ') cmd++;
973 if (strlen(cmd) && *cmd != '#') {
974 if (!strncasecmp(cmd, "exit", 4) || !strncasecmp(cmd, "quit", 4)) {
975 ecore_main_loop_quit();
976 } else if (!strncasecmp(cmd, "set ", 4)) {
977 if (do_set(cmd) == 0)
979 } else if (!strncasecmp(cmd, "stat ", 5)) {
981 } else if (!strncasecmp(cmd, "get ", 4)) {
982 if (do_get(cmd) == 0)
984 } else if (!strncasecmp(cmd, "ls", 2)) {
987 } else if (!strncasecmp(cmd, "cd", 2)) {
990 } else if (!strncasecmp(cmd, "rm", 2)) {
993 } else if (!strncasecmp(cmd, "sh ", 3)) {
995 } else if (!strncasecmp(cmd, "x ", 2)) {
1006 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
1009 static char cmd_buffer[256];
1013 const char escape_str[] = { 0x1b, 0x5b, 0x0 };
1014 const char *escape_ptr = escape_str;
1018 fd = ecore_main_fd_handler_fd_get(fd_handler);
1020 printf("FD is not valid: %d\n", fd);
1021 return ECORE_CALLBACK_CANCEL;
1024 fd = s_info.input_fd;
1029 * Using this routine, we can implement the command recommend algorithm.
1030 * When a few more characters are matched with history of command, we can show it to user
1031 * Then the user will choose one or write new command
1035 while ((ret = read(fd, &ch, sizeof(ch))) == sizeof(ch)) {
1036 if (*escape_ptr == '\0') {
1040 printf("%s2K%s1G", escape_str, escape_str);
1041 tmp = get_command(--s_info.history_idx);
1043 s_info.history_idx = 0;
1044 cmd_buffer[0] = '\0';
1047 strcpy(cmd_buffer, tmp);
1048 idx = strlen(cmd_buffer);
1052 case 0x42: /* DOWN */
1053 if (s_info.history_idx >= 0)
1056 printf("%s2K%s1G", escape_str, escape_str);
1057 tmp = get_command(++s_info.history_idx);
1058 if (s_info.history_idx == 0) {
1059 s_info.history_idx = 0;
1060 cmd_buffer[0] = '\0';
1063 strcpy(cmd_buffer, tmp);
1064 idx = strlen(cmd_buffer);
1068 case 0x43: /* RIGHT */
1070 case 0x44: /* LEFT */
1076 escape_ptr = escape_str;
1078 } else if (ch == *escape_ptr) {
1084 case 0x08: /* BKSP */
1085 cmd_buffer[idx] = '\0';
1088 cmd_buffer[idx] = ' ';
1093 cmd_buffer[idx] = '\0';
1097 case 0x09: /* TAB */
1098 if (!s_info.quick_search_node) {
1099 s_info.quick_search_node = node_child(s_info.curdir);
1100 s_info.quick_idx = idx;
1102 s_info.quick_search_node = node_next_sibling(s_info.quick_search_node);
1103 idx = s_info.quick_idx;
1106 if (!s_info.quick_search_node)
1109 printf("%s2K%s1G", escape_str, escape_str);
1110 strcpy(cmd_buffer + idx, node_name(s_info.quick_search_node));
1111 idx += strlen(node_name(s_info.quick_search_node));
1116 cmd_buffer[idx] = '\0';
1118 if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1119 putc((int)'\n', stdout);
1120 do_command(cmd_buffer);
1121 put_command(cmd_buffer);
1122 memset(cmd_buffer, 0, sizeof(cmd_buffer));
1123 s_info.history_idx = 0;
1124 s_info.quick_search_node = NULL;
1126 /* Make a main loop processing for command handling */
1127 return ECORE_CALLBACK_RENEW;
1129 cmd_buffer[idx++] = ch;
1131 if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1132 putc((int)ch, stdout);
1134 if (idx == sizeof(cmd_buffer) - 1) {
1135 cmd_buffer[idx] = '\0';
1136 printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
1143 if (ret < 0 && !fd_handler)
1144 ecore_main_loop_quit();
1146 return ECORE_CALLBACK_RENEW;
1149 static void processing_line_buffer(const char *buffer)
1152 char slavename[256];
1170 struct package *pkginfo;
1171 struct instance *instinfo;
1172 struct slave *slaveinfo;
1175 switch (s_info.cmd) {
1177 if (sscanf(buffer, "%d %255[^ ] %255[^ ] %255[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
1178 printf("Invalid format : [%s]\n", buffer);
1182 node = node_find(s_info.targetdir, pkgname);
1184 pkginfo = calloc(1, sizeof(*pkginfo));
1186 printf("Error: %s\n", strerror(errno));
1190 pkginfo->pkgid = strdup("conf.file");
1191 if (!pkginfo->pkgid)
1192 printf("Error: %s\n", strerror(errno));
1194 pkginfo->primary = 1;
1196 node = node_create(s_info.targetdir, pkgname, NODE_DIR);
1198 free(pkginfo->pkgid);
1200 printf("Failed to create a new node (%s)\n", pkgname);
1204 node_set_mode(node, NODE_READ | NODE_EXEC);
1205 node_set_data(node, pkginfo);
1207 pkginfo = node_data(node);
1209 printf("Package info is inavlid\n");
1213 free(pkginfo->slavename);
1216 pkginfo->slavename = NULL;
1217 pkginfo->abi = NULL;
1220 node_set_age(node, s_info.age);
1222 pkginfo->slavename = strdup(slavename);
1223 if (!pkginfo->slavename)
1224 printf("Error: %s\n", strerror(errno));
1226 pkginfo->abi = strdup(abi);
1228 printf("Error: %s\n", strerror(errno));
1231 pkginfo->refcnt = refcnt;
1232 pkginfo->fault_count = fault_count;
1233 pkginfo->inst_count = list_count;
1236 if (sscanf(buffer, "%d %[^ ] %[^ ] %[^ ] %d %d %d %[^ ] %d %d %lf", &pid, slavename, pkgname, abi, &secured, &refcnt, &fault_count, state, &loaded_inst, &loaded_pkg, &ttl) != 11) {
1237 printf("Invalid format : [%s]\n", buffer);
1240 node = node_find(s_info.targetdir, slavename);
1242 slaveinfo = calloc(1, sizeof(*slaveinfo));
1244 printf("Error: %s\n", strerror(errno));
1248 node = node_create(s_info.targetdir, slavename, NODE_DIR);
1254 node_set_mode(node, NODE_READ | NODE_EXEC);
1255 node_set_data(node, slaveinfo);
1257 slaveinfo = node_data(node);
1260 node_set_age(node, s_info.age);
1262 free(slaveinfo->pkgname);
1263 free(slaveinfo->abi);
1264 free(slaveinfo->state);
1266 slaveinfo->pkgname = strdup(pkgname);
1267 if (!slaveinfo->pkgname)
1268 printf("Error: %s\n", strerror(errno));
1270 slaveinfo->abi = strdup(abi);
1271 if (!slaveinfo->abi)
1272 printf("Error: %s\n", strerror(errno));
1274 slaveinfo->state = strdup(state);
1275 if (!slaveinfo->state)
1276 printf("Error: %s\n", strerror(errno));
1278 slaveinfo->pid = pid;
1279 slaveinfo->secured = secured;
1280 slaveinfo->refcnt = refcnt;
1281 slaveinfo->fault_count = fault_count;
1282 slaveinfo->loaded_inst = loaded_inst;
1283 slaveinfo->loaded_pkg = loaded_pkg;
1284 slaveinfo->ttl = ttl;
1287 if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %lf %[^ ] %d %d", inst_id, cluster, category, &period, state, &width, &height) != 7) {
1288 printf("Invalid format : [%s]\n", buffer);
1292 for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
1293 i += (inst_id[i] == '/');
1295 node = node_find(s_info.targetdir, inst_id + i);
1297 instinfo = calloc(1, sizeof(*instinfo));
1299 printf("Error: %s\n", strerror(errno));
1303 node = node_create(s_info.targetdir, inst_id + i, NODE_FILE);
1309 node_set_mode(node, NODE_READ | NODE_WRITE);
1310 node_set_data(node, instinfo);
1312 instinfo = node_data(node);
1315 node_set_age(node, s_info.age);
1318 free(instinfo->cluster);
1319 free(instinfo->category);
1320 free(instinfo->state);
1322 instinfo->id = strdup(inst_id);
1324 printf("Error: %s\n", strerror(errno));
1326 instinfo->cluster = strdup(cluster);
1327 if (!instinfo->cluster)
1328 printf("Error: %s\n", strerror(errno));
1330 instinfo->category = strdup(category);
1331 if (!instinfo->category)
1332 printf("Error: %s\n", strerror(errno));
1334 instinfo->state = strdup(state);
1335 if (!instinfo->state)
1336 printf("Error: %s\n", strerror(errno));
1338 instinfo->period = period;
1339 instinfo->width = width;
1340 instinfo->height = height;
1343 sscanf(buffer, "%d", &i);
1344 printf("%s\n", strerror(i));
1345 printf("Result: %d\n", i);
1348 sscanf(buffer, "%d", &i);
1349 printf("Result: %d\n", i);
1352 sscanf(buffer, "%d", &i);
1353 printf("Result: %d\n", i);
1360 static inline void do_line_command(void)
1362 switch (s_info.cmd) {
1384 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
1387 static char *line_buffer = NULL;
1388 static int line_index = 0;
1389 static int bufsz = 256;
1392 fd = ecore_main_fd_handler_fd_get(fd_handler);
1394 printf("FD is not valid: %d\n", fd);
1395 return ECORE_CALLBACK_CANCEL;
1398 if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
1399 printf("Error: %s\n", strerror(errno));
1400 return ECORE_CALLBACK_CANCEL;
1405 line_buffer = malloc(bufsz);
1407 printf("Error: %s\n", strerror(errno));
1408 return ECORE_CALLBACK_CANCEL;
1412 if (ch == '\n') { /* End of a line */
1413 if (line_index == bufsz - 1) {
1415 new_buf = realloc(line_buffer, bufsz + 2);
1417 printf("Error: %s\n", strerror(errno));
1422 return ECORE_CALLBACK_CANCEL;
1425 line_buffer = new_buf;
1428 line_buffer[line_index] = '\0';
1430 if (!strcmp(line_buffer, "EOD")) {
1434 processing_line_buffer(line_buffer);
1444 line_buffer[line_index++] = ch;
1445 if (line_index == bufsz - 1) {
1447 new_buf = realloc(line_buffer, bufsz);
1449 printf("Error: %s\n", strerror(errno));
1454 return ECORE_CALLBACK_CANCEL;
1457 line_buffer = new_buf;
1461 return ECORE_CALLBACK_RENEW;
1464 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
1466 const char *fifo_name;
1469 if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
1470 printf("Invalid packet\n");
1475 printf("Returns %d\n", ret);
1479 printf("FIFO: %s\n", fifo_name);
1481 s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
1482 if (s_info.fifo_handle < 0) {
1483 printf("Error: %s\n", strerror(errno));
1484 s_info.fifo_handle = -EINVAL;
1485 ecore_main_loop_quit();
1489 s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
1490 if (!s_info.fd_handler) {
1491 printf("Failed to add a fd handler\n");
1492 close(s_info.fifo_handle);
1493 s_info.fifo_handle = -EINVAL;
1494 ecore_main_loop_quit();
1500 if (s_info.input_fd == STDIN_FILENO) {
1501 if (fcntl(s_info.input_fd, F_SETFL, O_NONBLOCK) < 0)
1502 printf("Error: %s\n", strerror(errno));
1504 s_info.in_handler = ecore_main_fd_handler_add(s_info.input_fd, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
1505 if (!s_info.in_handler) {
1506 printf("Failed to add a input handler\n");
1507 ecore_main_loop_quit();
1515 static int disconnected_cb(int handle, void *data)
1517 printf("Disconnected\n");
1518 ecore_main_loop_quit();
1522 static int connected_cb(int handle, void *data)
1524 struct packet *packet;
1526 printf("Connected\n");
1528 packet = packet_create("liveinfo_hello", "d", 0.0f);
1530 printf("Failed to build a packet for hello\n");
1531 com_core_packet_client_fini(s_info.fd);
1532 s_info.fd = -EINVAL;
1538 if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
1539 printf("Failed to send a packet hello\n");
1540 packet_destroy(packet);
1541 com_core_packet_client_fini(s_info.fd);
1542 s_info.fd = -EINVAL;
1546 packet_destroy(packet);
1550 int main(int argc, char *argv[])
1552 struct termios ttystate;
1553 static struct method s_table[] = {
1559 static struct option long_options[] = {
1560 { "batchmode", required_argument, 0, 'b' },
1561 { "help", no_argument, 0, 'h' },
1562 { "verbose", required_argument, 0, 'v' },
1569 c = getopt_long(argc, argv, "b:hv:", long_options, &option_index);
1572 if (!optarg || !*optarg) {
1573 printf("Invalid argument\n");
1578 if (s_info.input_fd != STDIN_FILENO) {
1579 /* Close the previously, opened file */
1580 close(s_info.input_fd);
1583 s_info.input_fd = open(optarg, O_RDONLY);
1584 if (s_info.input_fd < 0) {
1585 printf("Unable to access %s (%s)\n", optarg, strerror(errno));
1593 if (!optarg || !*optarg) {
1594 printf("Invalid argument\n");
1599 s_info.verbose = !strcmp(optarg, "true");
1609 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1610 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1611 livebox_service_init();
1613 s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
1614 if (s_info.fd < 0) {
1615 printf("Failed to make a connection\n");
1619 if (s_info.input_fd == STDIN_FILENO) {
1620 printf("Type your command on below empty line\n");
1622 if (tcgetattr(s_info.input_fd, &ttystate) < 0) {
1623 printf("Error: %s\n", strerror(errno));
1625 ttystate.c_lflag &= ~(ICANON | ECHO);
1626 ttystate.c_cc[VMIN] = 1;
1628 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1629 printf("Error: %s\n", strerror(errno));
1632 printf("Batch mode enabled\n");
1635 if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0)
1636 printf("Error: %s\n", strerror(errno));
1640 ecore_main_loop_begin();
1643 livebox_service_fini();
1645 if (s_info.fd > 0) {
1646 com_core_packet_client_fini(s_info.fd);
1647 s_info.fd = -EINVAL;
1650 if (s_info.fd_handler) {
1651 ecore_main_fd_handler_del(s_info.fd_handler);
1652 s_info.fd_handler = NULL;
1655 if (s_info.input_fd == STDIN_FILENO) {
1656 ttystate.c_lflag |= ICANON | ECHO;
1657 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1658 printf("Error: %s\n", strerror(errno));
1660 close(s_info.input_fd);
1663 if (s_info.fifo_handle > 0) {
1664 close(s_info.fifo_handle);
1665 s_info.fifo_handle = -EINVAL;
1668 if (s_info.in_handler) {
1669 ecore_main_fd_handler_del(s_info.in_handler);
1670 s_info.in_handler = NULL;
1674 putc((int)'\n', stdout);