/* *input: add any valid filename * */ void pipe_example (char* in_ip, int in_port, char* filename) { int port=in_port; char* ip=in_ip; FILE* fp; singleClient* sc = single_connect(port,ip); if((fp=freopen(filename,"a",stdin))==NULL){ fprintf(stderr,"error redirection\n"); } char* key=(char*)malloc(500); char* value=(char*)malloc(500); char* revalue=(char*)malloc(500); puts("hehe"); pipe_getReply(sc,revalue); pipe_set(sc,"testk","testv"); pipe_set(sc,"testk1","testv1"); puts(revalue); pipe_get(sc,"testk"); pipe_get(sc,"testk1"); pipe_getReply(sc,revalue); puts(revalue); pipe_getReply(sc,revalue); puts(revalue); pipe_getReply(sc,revalue); puts(revalue); }
int fork_and_exec_fifo_out(int connfd, char **cmd, int target_id) { int client_id = clients_get_id_from_socket(connfd); int fd; char fifo_path[PATH_LENGTH]; pid_t pid; pid = fork(); if(pid<0) { // if error fprintf(stderr, "Fork failed\n"); exit(EXIT_FAILURE); } else if (pid ==0) { // if child sprintf(fifo_path, "%sclient_%d_%d", FIFO_PATH_DIR, client_id, target_id); fprintf(stderr, "fifo_path:%s\n", fifo_path); if((fd = open(fifo_path, O_NONBLOCK | O_WRONLY) ) < 0) { perror("open"); } dup2(fd, STDOUT_FILENO); if(!DEBUG) dup2(fd, STDERR_FILENO); // redirect STDIN to pipe_map[0][READ] int fd_in = pipe_get(client_id); if(fd_in) dup2(fd_in, STDIN_FILENO); if( cmd[0][0]=='/' || execvp(cmd[0], cmd)<0 ) { fprintf(stderr, "Unknown command: [%s].\n", cmd[0]); fprintf(stdout, "Unknown command: [%s].\n", cmd[0]); fflush(stderr); close(fd); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } else { int return_val; waitpid(pid, &return_val, 0); if( WEXITSTATUS(return_val) == EXIT_FAILURE ) return EXIT_FAILURE; } return EXIT_SUCCESS; }
// simple exec (no pipe included), for last command (no following pipe) int fork_and_exec_last(int connfd, char **cmd) { int client_id = clients_get_id_from_socket(connfd); pid_t pid; pid = fork(); if(pid < 0) { // if error fprintf(stderr, "Fork failed\n"); exit(EXIT_FAILURE); } else if (pid == 0) { // if child // bind pipe in int fd_in = pipe_get(client_id); if(fd_in) dup2(fd_in, STDIN_FILENO); if(DEBUG) debug_fork_and_exec_last(client_id, cmd, fd_in); // bind out to stdout dup2(connfd, STDOUT_FILENO); // duplicate socket on stdout if(!DEBUG) dup2(connfd, STDERR_FILENO); if( cmd[0][0]=='/' || execvp(cmd[0], cmd)<0 ) { fprintf(stderr, "Unknown command: [%s].\n", cmd[0]); fprintf(stdout, "Unknown command: [%s].\n", cmd[0]); fflush(stderr); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } else { // if parent int return_val; waitpid(pid, &return_val, 0); if(WEXITSTATUS(return_val) == EXIT_FAILURE) return EXIT_FAILURE; } return EXIT_SUCCESS; }
// output to file ('>') int fork_and_exec_file(int connfd, char **cmd, char *filepath) { int client_id = clients_get_id_from_socket(connfd); pid_t pid; pid = fork(); if(pid<0) { // if error fprintf(stderr, "Fork failed\n"); exit(EXIT_FAILURE); } else if (pid ==0) { // if child // bind stdout to file int fd_file = open(filepath, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); dup2(fd_file, STDOUT_FILENO); if(!DEBUG) dup2(connfd, STDERR_FILENO); // redirect STDIN to pipe_map[0][READ] int fd_in = pipe_get(client_id); if(fd_in) dup2(fd_in, STDIN_FILENO); if( cmd[0][0]=='/' || execvp(cmd[0], cmd)<0 ) { fprintf(stderr, "Unknown command: [%s].\n", cmd[0]); fprintf(stdout, "Unknown command: [%s].\n", cmd[0]); fflush(stderr); close(fd_file); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } else { // if parent int return_val; waitpid(pid, &return_val, 0); if( WEXITSTATUS(return_val) == EXIT_FAILURE ) return EXIT_FAILURE; } return EXIT_SUCCESS; }
// Updates program clock reference time void pcr_update_thread( void * arg ) { sdesc *desc; UINT64 pcr_time; UINT64 pcr_received_time; UINT64 curr_time; while(1) { desc = pipe_get(VRDMA_PCR); if(desc == NULL) { goto cleanup; } sSLICE_HDR *hdr = (sSLICE_HDR *) desc->data; // Update PCR time pcr_time = hdr->timestamp; // Free descriptor desc_put(desc); // Cache received time pcr_received_time = sink_time_get(); cleanup: // Get current time curr_time = sink_time_get(); pthread_mutex_lock(&f_cblk.lock); f_cblk.curr_time = pcr_time + (curr_time - pcr_received_time) - system_AUDIO_SOURCE_DELAY_MS - system_DELAY_MS; pthread_mutex_unlock(&f_cblk.lock); usleep(2*1000); } }
// Slice packing thread void slice_packing_thread( void * arg ) { sdesc *desc; desc = NULL; while(1) { UINT32 len = pipe_len_get(VRDMA_SLICE_READY); if(len >= 10) { // More than enough. Try again next iteration goto next_iter; } UINT32 slices_to_dump = 10 - len; do { desc = pipe_get(VRDMA_SLICE); if(desc == NULL) { goto next_iter; } // Dump slice video_scheduler_slice_dump(desc); slices_to_dump--; } while (slices_to_dump > 0); next_iter: usleep(2*1000); } }
void video_scheduler_thread( void * arg ) { sdesc *desc; UINT64 slice_present_time; desc = NULL; while(1) { while(1) { // Get slice if(desc == NULL) { desc = pipe_get(VRDMA_SLICE_READY); if(desc == NULL) { goto next_iter; } sSLICE_HDR *hdr = (sSLICE_HDR *) desc->data; // Get PTS slice_present_time = ((sSLICE_HDR *) desc->data)->timestamp; } UINT64 estimated_source_time = estimated_source_time_get(); UINT8 present = (estimated_source_time > slice_present_time) ? 1 : 0; if(!present) { goto next_iter; } // Present the slice sdesc *curr = desc->next; sdesc *next; do { video_sink_buf_set((sDECODER_HW_BUFFER *) curr->data); next = curr->next; free(curr); curr = next; } while (curr != NULL); free(desc->data); free(desc); // Set descriptor pointer to NULL desc = NULL; } next_iter: usleep(1*1000); } }
// simple exec (no pipe included, but for piping OUTs) int fork_and_exec_pipe(int connfd, char **cmd, int p_n) { // if(strcmp(cmd[0], "cat")==0 && cmd[1]==NULL) return SKIP_SHIFT; // create pipe int client_id = clients_get_id_from_socket(connfd); int *fd = pipe_create(client_id, p_n); pid_t pid; pid = fork(); if(pid<0) { // if error fprintf(stderr, "Fork failed\n"); exit(EXIT_FAILURE); } else if (pid ==0) { // if child // output old_pipe content if exist int *op = get_old_pipe(client_id); if(op!=NULL) { memset(pipe_buff, 0, sizeof(pipe_buff)); int count; if( (count = read(op[READ], pipe_buff, SIZE_PIPE_BUFF)) < 0 ) { fprintf(stderr, "read pipe connent error: %s\n", strerror(errno)); } if(close(op[READ]) < 0) { fprintf(stderr, "pipe close error (old_pipe in): %s\n", strerror(errno)); exit(EXIT_FAILURE); } if(DEBUG) debug_print_pipe_cat_content(count); if( write(fd[WRITE], pipe_buff, count) < 0 ) { fprintf(stderr, "write pipe connent error: %s\n", strerror(errno)); } } // redirect STDOUT to pipe if(close(fd[READ]) < 0) { fprintf(stderr, "pipe close error (fd in): %s\n", strerror(errno)); exit(EXIT_FAILURE); } dup2(fd[WRITE], STDOUT_FILENO); if(!DEBUG) dup2(connfd, STDERR_FILENO); // DEBUG if(DEBUG) debug_print_pipe_map(client_id); // redirect STDIN to pipe_map[0][READ] int fd_in = pipe_get(client_id); if(fd_in) dup2(fd_in, STDIN_FILENO); if(cmd[0][0]=='/' || execvp(cmd[0], cmd)<0) { fprintf(stderr, "Unknown command: [%s].\n", cmd[0]); fprintf(stdout, "Unknown command: [%s].\n", cmd[0]); fflush(stderr); close(fd[WRITE]); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } else { // if parent int return_val; waitpid(pid, &return_val, 0); if( close(fd[WRITE]) < 0 ) { fprintf(stderr, "pipe close error (fd out): %s\n", strerror(errno)); exit(EXIT_FAILURE); } if(WEXITSTATUS(return_val) == EXIT_FAILURE) return EXIT_FAILURE; } return EXIT_SUCCESS; }