void cli_context_free (cli_context_t *ctx) { if (ctx->conn) { xmmsc_unref (ctx->conn); } if (ctx->mode == CLI_EXECUTION_MODE_SHELL) { readline_free (); } command_trie_free (ctx->commands); cli_cache_free (ctx->cache); configuration_free (ctx->config); cmdnames_free (ctx->cmdnames); g_free (ctx); }
int main(int argc, char **argv, char** envp) { if (argc == 3 && !strcmp(argv[1], "-H")) { hvport = atoi(argv[2]); if (hvport < 1024 || hvport > 65000) { printf("Invalid port\n"); exit(1); } /* keep program name for vpcs */ #ifdef cygwin /* using windows native API to get 'real' path */ if (GetModuleFileName(NULL, prgname, PATH_MAX) == 0) { #else if (!getpath(argv[0])) { #endif printf("Can not get file path\n"); return 1; } return hypervisor(hvport); } /* go to vpcs */ return vpcs(argc, argv); } int hypervisor(int port) { struct sockaddr_in serv; int on = 1; setsid(); #if 1 if (daemon(1, 1)) { perror("Daemonize fail"); goto ret; } #endif memset(vpcs_list, 0, MAX_DAEMONS * sizeof(struct list)); if (openpty(&ptyfdm, &ptyfds, NULL, NULL, NULL)) { perror("Create pseudo-terminal"); goto ret; } signal(SIGCHLD, SIG_IGN); signal(SIGPIPE, SIG_IGN); fptys = fdopen(ptyfds, "w"); rls = readline_init(50, 128); rls->fdin = ptyfds; rls->fdout = ptyfds; if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) >= 0) { (void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); //fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC); bzero((char *) &serv, sizeof(serv)); serv.sin_family = AF_INET; serv.sin_addr.s_addr = htonl(INADDR_ANY); serv.sin_port = htons(port); if (bind(sock, (struct sockaddr *) &serv, sizeof(serv)) < 0) { perror("Daemon bind port"); goto ret; } if (listen(sock, 5) < 0) { perror("Daemon listen"); goto ret; } loop(); close(sock); } ret: if (rls) readline_free(rls); return 1; } static void* pty_master(void *arg) { int i; u_char buf[128]; while (!cmd_quit) { memset(buf, 0, sizeof(buf)); i = read(ptyfdm, buf, sizeof(buf)); if (i > 0 && write(sock_cli, buf, i)) ; } return NULL; }