void mk_cheetah_loop_stdin() { int len; char cmd[200]; char line[200]; char *rcmd; mk_cheetah_welcome_msg(); while (1) { CHEETAH_WRITE(MK_CHEETAH_PROMPT, ANSI_BOLD, ANSI_GREEN, ANSI_RESET); rcmd = fgets(line, sizeof(line), cheetah_input); if (!rcmd) { continue; } len = strlen(line); if (len == 0){ CHEETAH_WRITE("\n"); mk_cheetah_cmd_quit(); } strncpy(cmd, line, len - 1); cmd[len - 1] = '\0'; mk_cheetah_cmd(cmd); memset(line, '\0', sizeof(line)); } }
void mk_cheetah_welcome_msg() { CHEETAH_WRITE("\n%s%s***%s Welcome to %sCheetah!%s, the %sMonkey Shell %s:) %s***%s\n", ANSI_BOLD, ANSI_YELLOW, ANSI_WHITE, ANSI_GREEN, ANSI_WHITE, ANSI_RED, ANSI_WHITE, ANSI_YELLOW, ANSI_RESET); CHEETAH_WRITE("\n << %sType 'help' or '\\h' for help%s >>\n\n", ANSI_BLUE, ANSI_RESET); CHEETAH_FLUSH(); }
void mk_cheetah_cmd_plugins_print_core(struct mk_list *list) { struct mk_plugin *p; struct mk_list *head; CHEETAH_WRITE("\n%s[MASTER INIT]%s", ANSI_BOLD ANSI_BLUE, ANSI_RESET); mk_list_foreach(head, list) { p = mk_list_entry(head, struct mk_plugin, _head); if (p->master_init) { CHEETAH_WRITE("\n [%s] %s v%s on \"%s\"", p->shortname, p->name, p->version, p->path); } }
void mk_cheetah_cmd_uptime() { int days; int hours; int minutes; int seconds; long int upmind; long int upminh; long int uptime; /* uptime in seconds */ uptime = time(NULL) - init_time; /* days */ days = uptime / MK_CHEETAH_ONEDAY; upmind = uptime - (days * MK_CHEETAH_ONEDAY); /* hours */ hours = upmind / MK_CHEETAH_ONEHOUR; upminh = upmind - hours * MK_CHEETAH_ONEHOUR; /* minutes */ minutes = upminh / MK_CHEETAH_ONEMINUTE; seconds = upminh - minutes * MK_CHEETAH_ONEMINUTE; CHEETAH_WRITE ("Server has been running: %i day%s, %i hour%s, %i minute%s and %i second%s\n\n", days, (days > 1) ? "s" : "", hours, (hours > 1) ? "s" : "", minutes, (minutes > 1) ? "s" : "", seconds, (seconds > 1) ? "s" : ""); }
void mk_cheetah_print_worker_memory_usage(pid_t pid) { int s = 1024; char *buf; pid_t ppid; FILE *f; ppid = getpid(); buf = mk_api->mem_alloc(s); sprintf(buf, MK_CHEETAH_PROC_TASK, ppid, pid); f = fopen(buf, "r"); if (!f) { CHEETAH_WRITE("Cannot get details\n"); return; } buf = fgets(buf, s, f); if (!buf) { CHEETAH_WRITE("Cannot format details\n"); return; } fclose(f); CHEETAH_WRITE("\n"); return; /* int n, c; int init = 0; int last = 0; char *value; while ((n = mk_api->str_search(buf + last, " ", MK_STR_SENSITIVE)) > 0) { if (c == 23) { value = mk_api->str_copy_substr(buf, init, last + n); printf("%s\n", value); mk_mem_free(buf); mk_mem_free(value); return; } init = last + n + 1; last += n + 1; c++; }*/ }
void mk_cheetah_cmd_plugins_print_stage(struct mk_list *list, const char *stage, int stage_bw) { struct plugin *p; struct mk_list *head; if (mk_list_is_empty(list) == 0) { return; } CHEETAH_WRITE("%s[%s]%s", ANSI_BOLD ANSI_YELLOW, stage, ANSI_RESET); mk_list_foreach(head, list) { p = mk_list_entry(head, struct plugin, _head); if (p->hooks & stage_bw) { CHEETAH_WRITE("\n [%s] %s v%s on \"%s\"", p->shortname, p->name, p->version, p->path); } }
static int mk_cheetah_config(char *path) { unsigned long len; char *listen = NULL; char *default_file = NULL; struct mk_rconf *conf; struct mk_rconf_section *section; /* this variable is defined in cheetah.h and points to * the FILE *descriptor where to write out the data */ cheetah_output = NULL; /* read configuration file */ mk_api->str_build(&default_file, &len, "%scheetah.conf", path); conf = mk_api->config_open(default_file); if (!conf) { return -1; } section = mk_api->config_section_get(conf, "CHEETAH"); if (!section) { CHEETAH_WRITE("\nError, could not find CHEETAH tag"); return -1; } /* no longer needed */ mk_api->mem_free(default_file); /* Listen directive */ listen = mk_api->config_section_get_key(section, "Listen", MK_RCONF_STR); if (strcasecmp(listen, LISTEN_STDIN_STR) == 0) { listen_mode = LISTEN_STDIN; } else if (strcasecmp(listen, LISTEN_SERVER_STR) == 0) { listen_mode = LISTEN_SERVER; } else { printf("\nCheetah! Error: Invalid LISTEN value"); return -1; } /* Cheetah cannot work in STDIN mode if Monkey is working in background */ if (listen_mode == LISTEN_STDIN && mk_api->config->is_daemon == MK_TRUE) { printf("\nCheetah!: Forcing SERVER mode as Monkey is running in background\n"); fflush(stdout); listen_mode = LISTEN_SERVER; } return 0; }
int mk_cheetah_cmd(char *raw_cmd) { char *cmd = strip_whitespace(raw_cmd); if (strcmp(cmd, MK_CHEETAH_CONFIG) == 0 || strcmp(cmd, MK_CHEETAH_CONFIG_SC) == 0) { mk_cheetah_cmd_config(); } else if (strcmp(cmd, MK_CHEETAH_STATUS) == 0 || strcmp(cmd, MK_CHEETAH_STATUS_SC) == 0) { mk_cheetah_cmd_status(); } else if (strcmp(cmd, MK_CHEETAH_CLEAR) == 0 || strcmp(cmd, MK_CHEETAH_CLEAR_SC) == 0) { mk_cheetah_cmd_clear(); } else if (strcmp(cmd, MK_CHEETAH_UPTIME) == 0 || strcmp(cmd, MK_CHEETAH_UPTIME_SC) == 0) { mk_cheetah_cmd_uptime(); } else if (strcmp(cmd, MK_CHEETAH_PLUGINS) == 0 || strcmp(cmd, MK_CHEETAH_PLUGINS_SC) == 0) { mk_cheetah_cmd_plugins(); } else if (strcmp(cmd, MK_CHEETAH_WORKERS) == 0 || strcmp(cmd, MK_CHEETAH_WORKERS_SC) == 0) { mk_cheetah_cmd_workers(); } else if (strcmp(cmd, MK_CHEETAH_VHOSTS) == 0 || strcmp(cmd, MK_CHEETAH_VHOSTS_SC) == 0) { mk_cheetah_cmd_vhosts(); } else if (strcmp(cmd, MK_CHEETAH_HELP) == 0 || strcmp(cmd, MK_CHEETAH_HELP_SC) == 0 || strcmp(cmd, MK_CHEETAH_SHELP) == 0 || strcmp(cmd, MK_CHEETAH_SHELP_SC) == 0) { mk_cheetah_cmd_help(); } else if (strcmp(cmd, MK_CHEETAH_QUIT) == 0 || strcmp(cmd, MK_CHEETAH_QUIT_SC) == 0) { return mk_cheetah_cmd_quit(); } else if (strlen(cmd) == 0) { return 0; } else { CHEETAH_WRITE("Invalid command, type 'help' for a list of available commands\n"); } CHEETAH_FLUSH(); return 0; }
void mk_cheetah_print_running_user() { struct passwd pwd; struct passwd *result; char *buf; long bufsize; uid_t uid; bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); if (bufsize == -1) { bufsize = 16384; } buf = malloc(bufsize); uid = getuid(); getpwuid_r(uid, &pwd, buf, bufsize, &result); CHEETAH_WRITE("%s\n", pwd.pw_name); free(buf); }
void mk_cheetah_loop_server() { int n, ret; int buf_len; unsigned long len; char buf[1024]; char cmd[1024]; int server_fd; int remote_fd; size_t address_length; struct sockaddr_un address; socklen_t socket_size = sizeof(struct sockaddr_in); /* Create listening socket */ server_fd = socket(PF_UNIX, SOCK_STREAM, 0); if (server_fd < 0) { perror("socket() failed"); exit(EXIT_FAILURE); } cheetah_server = NULL; mk_api->str_build(&cheetah_server, &len, "/tmp/cheetah.%i", mk_api->config->serverport); unlink(cheetah_server); address.sun_family = AF_UNIX; sprintf(address.sun_path, "%s", cheetah_server); address_length = sizeof(address.sun_family) + len; if(bind(server_fd, (struct sockaddr *) &address, address_length) != 0) { perror("bind"); exit(EXIT_FAILURE); } if(listen(server_fd, 5) != 0) { perror("listen"); exit(EXIT_FAILURE); } while(1) { /* Listen for incoming connections */ remote_fd = accept(server_fd, (struct sockaddr *) &address, &socket_size); cheetah_socket = remote_fd; buf_len = 0; memset(buf, '\0', 1024); /* Send welcome message and prompt */ mk_cheetah_welcome_msg(); CHEETAH_WRITE(MK_CHEETAH_PROMPT, ANSI_BOLD, ANSI_GREEN, ANSI_RESET); while (1) { /* Read incoming data */ n = read(remote_fd, buf+buf_len, 1024 - buf_len); if (n <= 0) { break; } else { buf_len += n; if (buf[buf_len-1] == '\n') { /* Filter command */ strncpy(cmd, buf, buf_len - 1); cmd[buf_len - 1] = '\0'; /* Run command */ ret = mk_cheetah_cmd(cmd); if (ret == -1) { break; } /* Write prompt */ CHEETAH_WRITE(MK_CHEETAH_PROMPT, ANSI_BOLD, ANSI_GREEN, ANSI_RESET); buf_len = 0; memset(buf, '\0', 1024); } } } close(remote_fd); } }
void mk_cheetah_cmd_clear() { CHEETAH_WRITE("\033[2J\033[1;1H"); }