int main(int argc, char* argv[]){ /* * ensure the following items are proper: * - this process has cap_sys_chroot=ep * - the config files are secure * - the config files are readable * - enough arguments were provided */ /* ensure this process has the required capabilities */ ensure_capsyschroot(argv[0]); /* ensure config file is only writable by root */ ensure_config_secure(); /* ensure config file is readable */ ensure_config_readable(); /* ensure there are enough arguments */ ensure_enough_arguments(argc, argv); /* * gather the following pieces of information: * - the command (and its arguments) to run in the chroot * - the path to the chroot * - the directory to be cwd in the chroot */ /* get command to run in chroot */ char* shell[2]; char** chroot_command = get_chroot_command(argc, argv, shell); /* get path to chroot */ char chroot_path[PATH_MAX]; get_chroot_path(argv,chroot_path); /* get cwd - will attempt to make this cwd in chroot */ char* chroot_cwd = getcwd(NULL, PATH_MAX); /* * run the command in the proper context: * - if we're in a chroot, break out * - chroot the new directory, ensuring cwd is within it. * - change cwd to desired directory if it exists; remain in / otherwise. * - run command * - if needed, abort cleanly */ /* break out of chroot */ break_out_of_chroot(); /* chroot to new directory */ chdir(chroot_path); chroot("."); /* change cwd in the chroot to what it was previously, if possible */ if(chdir(chroot_cwd) != 0) fprintf(stderr,"WARNING: \"%s\" not present in target client, falling back to root directory\n", chroot_cwd); /* We need to free previously allocated memory */ free(chroot_cwd); /* run command */ execvp(chroot_command[0], chroot_command); /* if there is an error, abort cleanly */ perror("execvp"); return 2; }
static bool get_active_window_info(int *window_id, int *x, int *y, int *w, int *h) { char command_line[256]; FILE *command_pipe; char line[256]; int line_num; snprintf(command_line, sizeof(command_line), "%s xdotool getactivewindow getwindowgeometry", get_chroot_command()); command_pipe = popen(command_line, "r"); if (command_pipe == NULL) { print_error("popen() failed"); return false; } line_num = 1; while (fgets(line, sizeof(line), command_pipe) != NULL) { if (line_num == 1) { if (sscanf(line, "Window %d", window_id) != 1) { return false; } } else if (line_num == 2) { if (sscanf(line, " Position: %d,%d", x, y) != 2) { return false; } } else if (line_num == 3) { if (sscanf(line, " Geometry: %dx%d", w, h) != 2) { return false; } } line_num++; } pclose(command_pipe); //print_log("window_id = %d, %dx%d+%d,%d", *window_id, *w, *h, *x, *y); return true; }
static void *cap_osd_thread(void *data) { char command[256]; FILE *command_pipe; size_t read_size, offset; int window_id, x, y, w, h; int fd; int frame_width, frame_height; long long start_time, mid_time, end_time; while (!s_stopped) { start_time = get_current_time(); if (s_request_time < start_time - 2000) { usleep(50*1000); continue; } if (get_active_window_info(&window_id, &x, &y, &w, &h) == false) { print_error("get_active_window_info() failed"); continue; } if (is_nx1() || is_nx500()) { frame_width = NX1_500_FRAME_WIDTH; } else { // NX300 frame_width = NX300_FRAME_WIDTH; } frame_height = FRAME_HEIGHT; if (is_nx1()) { if (w == 1024 && h == 768 && x == 720) { // NX1 EVF s_evf = true; snprintf(command, sizeof(command), "xwd -id %d | " "%s convert -size 1024x768+%d -depth 8 bgra:- " "-scale x480 -background \"rgba(0,0,0,255)\" " "-gravity center -extent 720x480 bgra:-", window_id, get_chroot_command(), XWD_SKIP_BYTES); } else if (w == 720 && h == 480) { s_evf = false; snprintf(command, sizeof(command), "xwd -id %d", window_id); } else { s_evf = false; snprintf(command, sizeof(command), "xwd -root | " "%s convert -size 1744x768+%d -depth 8 bgra:- " "-crop 720x480+0+0 bgra:-", get_chroot_command(), XWD_SKIP_BYTES); } } else if (is_nx500()) { if (w == 720 && h == 480 && lcd_get_state() == LCD_OFF) { snprintf(command, sizeof(command), "xwd -id %lu", get_di_camera_app_window_id()); } else { snprintf(command, sizeof(command), "xwd -root"); } } else { // NX300 if (w == 480 && h == 800) { snprintf(command, sizeof(command), "xwd -id %d | " "%s convert -size 480x800+%d -depth 8 bgra:- " "-rotate 270 bgra:-", window_id, get_chroot_command(), XWD_SKIP_BYTES); } else { snprintf(command, sizeof(command), "xwd -root | " "%s convert -size 480x800+%d -depth 8 bgra:- " "-rotate 270 bgra:-", get_chroot_command(), XWD_SKIP_BYTES); } } //print_log("command = %s", command); command_pipe = popen(command, "r"); if (command_pipe == NULL) { print_error("popen() failed"); usleep(50*1000); continue; } fd = fileno(command_pipe); read_size = 0; offset = 0; while (offset < sizeof(s_buffer)) { read_size = read(fd, s_buffer + offset, sizeof(s_buffer) - offset); if (read_size == -1) { print_error("read() failed"); break; } else if (read_size == 0) { //print_log("read_size is 0"); break; } offset += read_size; } if (offset > frame_width * frame_height * 4) { s_buffer_shift_bytes = offset - (frame_width * frame_height * 4); //print_log("shift bytes = %d", s_buffer_shift_bytes); } else if (offset < frame_width * frame_height * 4) { print_log("frame read failed! offset = %d", offset); pclose(command_pipe); continue; } else { s_buffer_shift_bytes = 0; } pclose(command_pipe); mid_time = get_current_time(); update_osd_data(); // TODO: mutex lock? end_time = get_current_time(); print_log("cap_osd_thread() time = %lld + %lld = %lld", mid_time - start_time, end_time - mid_time, end_time - start_time); //usleep(50*1000); } print_log("cap_osd_thread() terimanted."); return NULL; }