diff --git a/src/api.h b/src/api.h index 1b415cd6..7080ce39 100644 --- a/src/api.h +++ b/src/api.h @@ -28,6 +28,7 @@ enum { SETUPINTERFACE, SETUPROUTE, REMOVECONTAINER, + PSCONTAINER, }; /* diff --git a/src/init.c b/src/init.c index ceb4b4b3..4ec3878c 100644 --- a/src/init.c +++ b/src/init.c @@ -36,6 +36,56 @@ struct hyper_pod global_pod = { .exec_head = LIST_HEAD_INIT(global_pod.exec_head), }; +struct hyper_ps_data { + /** + * line to process, it contains information about a process. + * The format will vary depending on the arguments provided to + * the ps command. + * for example if the argument is '-ef': + * root 5123 2 0 09:51 ? 00:00:00 [kworker/2:0] + */ + const char *line; + + /** + * line length + */ + size_t line_len; + + /** + * pid index, it depends of ps output + * in next example pid_index is 1: + * UID PID PPID C STIME TTY TIME CMD + */ + size_t pid_index; + + /** + * container process id + */ + pid_t container_pid; + + /** + * mnt ns file serial number + * useful to check if a process is running inside a container + */ + ino_t container_st_ino; + + /** + * if current line has a pid that is running in the same namespace + * of container_pid then append it to output + */ + uint8_t **output; + + /** + * output length + */ + uint32_t *output_len; + + /** + * output capacity + */ + uint32_t output_cap; +}; + #define MAXEVENTS 10 #define PROC_UPTIME_PATH "/proc/uptime" @@ -720,6 +770,389 @@ static int hyper_remove_container(char *json, int length) return ret; } +/** + * converts a line to an array, splits the line in tokens with strtok using + * horizontal spaces (\n\t ) as delimiter + * + * \param line to process + * \param[out] array_size array size + * \param max_num_tokens max number of tokens that array should contain, if -1 + * then there is no limit + * + * \return an array on success, else NULL + */ +static char **hyper_line_to_array(char *line, size_t *array_size, int max_num_tokens) +{ + char **array = NULL; + size_t size = 12; + + if (max_num_tokens == 0) { + return NULL; + } + + array = (char **)calloc(size, sizeof(*array)); + if (array == NULL) { + return NULL; + } + + size_t i = 0; + char *delim = " \n\t"; + char *tok = strtok(line, delim); + while (tok != NULL) { + array[i++] = tok; + + if (i >= size-1) { + char **tmp = NULL; + size += size; + tmp = (char **)realloc(array, size * sizeof(*array)); + if (tmp == NULL) { + /* try to reallocate once again */ + tmp = (char **)realloc(array, size * sizeof(*array)); + } + if (tmp == NULL) { + /* realloc failed, free memory and return NULL */ + free(array); + return NULL; + } + array = tmp; + } + + if (max_num_tokens > 0 && (--max_num_tokens) == 0) { + break; + } + + tok = strtok(NULL, delim); + } + + if (array_size != NULL) { + *array_size = i; + } + + /* Add NULL terminator */ + array[i] = NULL; + + return array; +} + +/** + * gets the index of PID, for example in the next line pid index is 1 + * UID PID PPID C STIME TTY TIME CMD + * + * \param line to process + * + * \return pid index on success, else -1 + */ +static int hyper_get_pid_index(const char *line) +{ + char **array = NULL; + char *new_line = NULL; + int ret = -1; + const char* pid_str = "PID"; + const size_t pid_str_size = 3; + + if (line == NULL) { + return -1; + } + + new_line = strdup(line); + if (new_line == NULL) { + fprintf(stderr, "can not duplicate string: %s\n", line); + return -1; + } + + array = hyper_line_to_array(new_line, NULL, -1); + if (array == NULL) { + fprintf(stderr, "can not convert line to array: %s\n", new_line); + goto out; + } + + for (size_t i=0; array[i] != NULL; i++) { + if (strncmp(array[i], pid_str, pid_str_size) == 0) { + ret = i; + break; + } + } + +out: + free_if_set(new_line); + free_if_set(array); + return ret; +} + +/** + * appends current ps line to final ps output + * + * \param[in,out] ps_data \ref hyper_ps_data + * + * \return true on success, else false + */ +static bool hyper_append_ps_line(struct hyper_ps_data *ps_data) +{ + /* +1 for \n */ + size_t line_len = ps_data->line_len + 1; + size_t output_len = *ps_data->output_len; + const size_t buf_size_increment = 1024; + int ret = -1; + + if (ps_data == NULL) { + return false; + } + + if (ps_data->output_cap < line_len) { + uint8_t *tmp = NULL; + + tmp = (uint8_t *)realloc(*ps_data->output, + (ps_data->output_cap + output_len + buf_size_increment) * sizeof(**ps_data->output)); + + if (tmp == NULL) { + /* try to reallocate once again */ + tmp = (uint8_t *)realloc(*ps_data->output, + (ps_data->output_cap + output_len + buf_size_increment) * sizeof(**ps_data->output)); + } + + if (tmp == NULL) { + return false; + } + + *ps_data->output = tmp; + ps_data->output_cap += buf_size_increment; + } + + ret = snprintf((char*)(*ps_data->output + output_len), + line_len + 1, "%s\n", ps_data->line); + if (ret < 0) { + return false; + } + + ps_data->output_cap -= ret; + *ps_data->output_len += ret; + + return true; +} + +/** + * process each ps line: + * - if pid is equal to container pid then add current line to the + * final ps output (ps_data->output) + * - check if pid is running in the same container mount namespace, + * if it is then add current line to the final ps output (ps_data->output) + * + * \param[in,out] ps_data \ref hyper_ps_data + * + * \return true on success, else false + */ +static bool hyper_process_ps_line(struct hyper_ps_data *ps_data) +{ + char *new_process_line = NULL; + char **array = NULL; + size_t array_size = 0; + pid_t pid; + char mnt_ns_path[PATH_MAX]; + struct stat st; + + if (ps_data == NULL) { + return false; + } + + new_process_line = strdup(ps_data->line); + if (new_process_line == NULL) { + fprintf(stderr, "can not dup string '%s': %s\n", + ps_data->line, strerror(errno)); + return false; + } + + array = hyper_line_to_array(new_process_line, &array_size, ps_data->pid_index+1); + if (array == NULL) { + fprintf(stderr, "can not convert line to array: %s\n", new_process_line); + free_if_set(new_process_line); + return false; + } + + if (ps_data->pid_index >= array_size) { + /* current line has not a pid */ + goto out; + } + + /* get pid from line */ + pid = strtol(array[ps_data->pid_index], NULL, 10); + if (pid == 0) { + fprintf(stderr, "can not convert string '%s' to int: %s\n", + array[ps_data->pid_index], strerror(errno)); + goto out; + } + + /* if pid is equal to container pid */ + if (pid == ps_data->container_pid) { + hyper_append_ps_line(ps_data); + goto out; + } + + snprintf(mnt_ns_path, PATH_MAX, "/proc/%d/ns/mnt", pid); + if (stat(mnt_ns_path, &st) < 0) { + goto out; + } + + /* check if pid is running in the container's namespace */ + if (ps_data->container_st_ino == st.st_ino) { + hyper_append_ps_line(ps_data); + goto out; + } + +out: + free_if_set(new_process_line); + free_if_set(array); + + return true; +} + +/** + * runs ps command to see what command are running inside the container + * + * \param json contains ps command arguments and container id + * \param length json length + * \param[out] datalen ps command output length + * \param[out] data ps command output, this output only have the processes that + * are running inside the container + * + * \return 0 on success, else -1 + */ +static int hyper_ps_container(char *json, int length, uint32_t *datalen, uint8_t **data) +{ + struct hyper_container *c; + struct hyper_pod *pod = &global_pod; + int ret = -1; + char command[1024] = { 0 }; + uint8_t *output = NULL; + size_t output_size = 0; + + if (json == NULL || datalen == NULL || data == NULL) { + return -1; + } + + JSON_Value *value = hyper_json_parse(json, length); + if (value == NULL) { + goto out; + } + + /* get container id */ + const char *id = json_object_get_string(json_object(value), "container"); + c = hyper_find_container(pod, id); + if (c == NULL) { + fprintf(stderr, "can not find container whose id is %s\n", id); + goto out; + } + + /* get ps args */ + int bytes_copied = snprintf(command, sizeof(command), "ps"); + + if (bytes_copied < 0) { + fprintf(stderr, "can not copy 'ps' to command\n"); + goto out; + } + + JSON_Array *ps_args = json_object_get_array(json_object(value), "psargs"); + size_t ps_args_size = json_array_get_count(ps_args); + + /*copy ps args to command */ + for (size_t i=0; ins, &st) < 0) { + perror("fail to stat mnt ns"); + goto out; + } + + /* allocate at least 1 byte for \0 */ + *data = calloc(1, sizeof(**data)); + + struct hyper_ps_data ps_data = { + .pid_index = (size_t)pid_index, + .container_pid = c->exec.pid, + .container_st_ino = st.st_ino, + .output = data, + .output_len = datalen, + .output_cap = 0, + }; + + char *process_line = NULL; + + /* header_end+1 next line, skip header line */ + char *line_end = header_end+1; + + /* processing each line of ps output */ + while ((line_end-(char*)output) < output_size) { + process_line = line_end; + line_end = strstr(process_line, "\n"); + if (line_end == NULL) { + /* no more lines */ + break; + } + + *line_end = '\0'; + + /* copy current process line */ + ps_data.line = process_line; + ps_data.line_len = line_end - process_line; + + if (! hyper_process_ps_line(&ps_data)) { + break; + } + + line_end++; + } + + ret = 0; + +out: + free_if_set(output); + json_value_free(value); + return ret; +} + struct hyper_file_arg { int rw; int mntns; @@ -1146,6 +1579,9 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len) case SETUPROUTE: ret = hyper_cmd_setup_route((char *)buf->data + 8, len - 8); break; + case PSCONTAINER: + ret = hyper_ps_container((char *)buf->data + 8, len - 8, &datalen, &data); + break; default: ret = -1; break; diff --git a/src/portmapping.c b/src/portmapping.c index 48c5f6e2..7c931ed2 100644 --- a/src/portmapping.c +++ b/src/portmapping.c @@ -16,7 +16,7 @@ int hyper_init_modules() { - int status = hyper_cmd("depmod"); + int status = hyper_cmd("depmod", NULL, NULL); if (status != 0) { fprintf(stderr, "depmod failed, status: %d\n", status); return -1; @@ -39,7 +39,7 @@ int hyper_setup_iptables_rule(struct ipt_rule rule) } if (strlen(check_cmd) > 0) { - check = hyper_cmd(check_cmd); + check = hyper_cmd(check_cmd, NULL, NULL); fprintf(stdout, "check iptables '%s', ret: %d\n", check_cmd, check); } @@ -53,7 +53,7 @@ int hyper_setup_iptables_rule(struct ipt_rule rule) } } - int status = hyper_cmd(cmd); + int status = hyper_cmd(cmd, NULL, NULL); fprintf(stdout, "insert iptables '%s', ret: %d\n", cmd, status); if (status != 0) { fprintf(stderr, "insert iptables rule failed, ret: %d\n", status); diff --git a/src/util.c b/src/util.c index c5874635..f9b62544 100644 --- a/src/util.c +++ b/src/util.c @@ -89,7 +89,7 @@ int hyper_copy_dir(char *src, char *dest) char cmd[512]; snprintf(cmd, sizeof(cmd), "tar cf - -C %s . | tar fx - -C %s", src, dest); - return hyper_cmd(cmd); + return hyper_cmd(cmd, NULL, NULL); } void hyper_sync_time_hctosys() { @@ -775,15 +775,113 @@ void hyper_shutdown() reboot(LINUX_REBOOT_CMD_POWER_OFF); } -int hyper_cmd(char *cmd) +/** + * Reads command stdout + * + * \param stdout_pipe command stdout fd + * \param[out] output command stdout + * \param[out] size command stdout size, if is NULL then it is not used + * + * \return 0 on success, else -1 + */ +static int hyper_read_cmd_stdout(int stdout_pipe, uint8_t **output, size_t *size) +{ + size_t total_bytes_read = 0; + int bytes_read = 0; + size_t bufsize = 512; + uint8_t *buffer = NULL; + + if (stdout_pipe < 0 || output == NULL) { + return -1; + } + + buffer = calloc(bufsize, sizeof(*buffer)); + + if (buffer == NULL) { + /* try once again */ + buffer = calloc(bufsize, sizeof(*buffer)); + } + + if (buffer == NULL) { + return -1; + } + + do { + bytes_read = read(stdout_pipe, + buffer+total_bytes_read, bufsize-total_bytes_read); + + if (bytes_read < 0) { + /* there is no more data */ + break; + } + + if (bytes_read > 0) { + total_bytes_read += bytes_read; + } + + if (total_bytes_read > bufsize-1) { + uint8_t *tmp = NULL; + bufsize+=bufsize; + tmp = realloc(buffer, bufsize * sizeof(*buffer)); + if (tmp == NULL) { + /* try to reallocate once again */ + tmp = realloc(buffer, bufsize * sizeof(*buffer)); + } + if (tmp == NULL) { + /* still NULL, free and return -1 */ + free(buffer); + return -1; + } + buffer = tmp; + } + } while (bytes_read > 0); + + /* end of string */ + buffer[total_bytes_read] = '\0'; + + *output = buffer; + + if (size) { + *size = total_bytes_read; + } + + return 0; +} + +/** + * runs a command + * + * \param cmd command to run + * \param[out] output command stdout, if is NULL then it is not used + * \param[out] size command stdout size, if is NULL then it is not used + * + * \return 0 if command exited normally, else -1 + */ +int hyper_cmd(char *cmd, uint8_t **output, size_t *size) { int pid, status; + int stdout_pipe[2] = { -1, -1 }; + + if (output != NULL) { + if (pipe(stdout_pipe) == -1) { + perror("failed to open stdout pipe"); + return -1; + } + } pid = fork(); if (pid < 0) { perror("fail to fork"); return -1; } else if (pid > 0) { + if (output != NULL) { + close(stdout_pipe[1]); + if (hyper_read_cmd_stdout(stdout_pipe[0], output, size) != 0) { + return -1; + } + close(stdout_pipe[0]); + } + if (waitpid(pid, &status, 0) <= 0) { perror("waiting fork cmd failed"); return -1; @@ -799,6 +897,12 @@ int hyper_cmd(char *cmd) return -1; } else { fprintf(stdout, "executing cmd %s\n", cmd); + + if (output != NULL) { + close(stdout_pipe[0]); + dup2(stdout_pipe[1], STDOUT_FILENO); + } + execlp("sh", "sh", "-c", cmd, NULL); } diff --git a/src/util.h b/src/util.h index 2151f454..df02366d 100644 --- a/src/util.h +++ b/src/util.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "../config.h" struct hyper_pod; @@ -30,7 +31,7 @@ int hyper_copy_dir(char *src, char *dst); void hyper_sync_time_hctosys(); void online_cpu(void); void online_memory(void); -int hyper_cmd(char *cmd); +int hyper_cmd(char *cmd, uint8_t **output, size_t *size); int hyper_create_file(const char *hyper_path); void hyper_filize(char *hyper_path); int hyper_mkdir(char *path, mode_t mode);