int cli_start(void) { priv_init(); priv_down(); putenv("REMADDR="); // get username if ((username = getlogin()) == NULL) { username = getenv("USER"); } set_permissions_map(); // set login info cli_login(username, CLI_MAX_NODES, CLI_MAX_CMDS); // send welcome msg to the client sprintf(buffer, "%s", WELCOME_MSG); send(cli_cfg->cfd, buffer, strlen(buffer), 0); printf(WELCOME_MSG); printf("[CLI] started...\n"); sprintf(cli_cfg->prompt,"%s%c ", cli_prompt(), cli_cfg->promptchar); send(cli_cfg->cfd,cli_cfg->prompt, strlen(cli_cfg->prompt), 0); return 1; }
void *client_handler(void *socket_desc) { //Get the socket descriptor int sock = *(int*) socket_desc; int read_size, finish = 0; char client_message[MAX_BUFFER_SIZE]; char message[LOGSZ]; sprintf(message, "Started cli connection.\n"); logger(LOG_INFO, message); cli_prompt(sock); //Receive a message from client while (!(finish) && ((read_size = recv(sock, client_message, MAX_BUFFER_SIZE, 0)) > 0)) { client_message[read_size - 1] = '\0'; if (read_size) { finish = execute_commands(sock, client_message, read_size); //finish = cli_process_message(sock, client_message, read_size); } if (finish) { shutdown(sock, SHUT_RDWR); } if (read_size == 0) { puts("Client disconnected"); fflush(stdout); } else if (read_size == -1) { perror("recv failed"); } } //Free the socket pointer free(socket_desc); sprintf(message, "Closing cli connection.\n"); logger(LOG_INFO, message); return NULL; }