/** * @author Tom Nightingale * * @brief The input process. * @details This is the program's main process. It monitors all keyboard input * and passes characters on to the output process for echoing back. * It also stores the characters within a buffer until the special * <ENTER> character is received. When this occurs, the buffer is sent * to the translate function for processing. * * @param pid * An array containing all child process id's. * @param txpipe_output * A pipe write file descriptor for sending data to the output process. * @param txpipe_translate * A pipe write file descriptor for sending data to the translate process. */ void input_proc(int pid[2], int txpipe_output, int txpipe_translate) { int c = 0; int count = 0; char buffer[BUFFSIZE]; while (c != 'T' && (c = getchar()) != EOF) { write_pipe(txpipe_output, &c, 1); buffer[count++] = c; switch (c) { case KILL: kill(pid[TRANSLATE_PROC], SIGTERM); kill(pid[OUTPUT_PROC], SIGTERM); return; case 'T': // fall through. case 'E': write_pipe(txpipe_translate, buffer, count); // fall through. case 'K': count = 0; buffer[count] = '\0'; break; } } }
void fix_rt_mutex_waiter_list(struct rt_mutex *pmutex) { struct rt_mutex_waiter *pwaiter6, *pwaiter7; struct rt_mutex_waiter waiter6, waiter7; struct rt_mutex mutex; if(!pmutex) return; read_pipe(pmutex, &mutex, sizeof(mutex)); pwaiter6 = NODE_LIST_TO_WAITER(mutex.wait_list.node_list.next); if(!pwaiter6) return; read_pipe(pwaiter6, &waiter6, sizeof(waiter6)); pwaiter7 = NODE_LIST_TO_WAITER(waiter6.list_entry.node_list.next); if(!pwaiter7) return; read_pipe(pwaiter7, &waiter7, sizeof(waiter7)); waiter6.list_entry.prio_list.prev = waiter6.list_entry.prio_list.next; waiter7.list_entry.prio_list.next = waiter7.list_entry.prio_list.prev; mutex.wait_list.node_list.prev = waiter6.list_entry.node_list.next; waiter7.list_entry.node_list.next = waiter6.list_entry.node_list.prev; write_pipe(pmutex, &mutex, sizeof(mutex)); write_pipe(pwaiter6, &waiter6, sizeof(waiter6)); write_pipe(pwaiter7, &waiter7, sizeof(waiter7)); }
void anonymous_pipe() { int pipe_fd[2]; if (pipe(pipe_fd)) { perror("Create pipe error"); exit(1); } pid_t pid = fork(); if (pid < 0) { perror("fork error!"); } else if (0 == pid) { close_read_pipe(pipe_fd); write_pipe(pipe_fd, "hello bad man"); close_write_pipe(pipe_fd); exit(EXIT_SUCCESS); } else { close_write_pipe(pipe_fd); char buf[MAX_BUF_SIZE]; read_pipe(pipe_fd, buf, MAX_BUF_SIZE); close_read_pipe(pipe_fd); } // wait(NULL); puts("process end !"); }
int sys_write (unsigned int fd, char *buf, int count) { struct file *file; struct m_inode *inode; // 如果文件句柄值大于程序最多打开文件数NR_OPEN,或者需要写入的字节计数小于0,或者该句柄 // 的文件结构指针为空,则返回出错码并退出。 if (fd >= NR_OPEN || count < 0 || !(file = current->filp[fd])) return -EINVAL; // 若需读取的字节数count 等于0,则返回0,退出 if (!count) return 0; // 取文件对应的i 节点。若是管道文件,并且是写管道文件模式,则进行写管道操作,若成功则返回 // 写入的字节数,否则返回出错码,退出。 inode = file->f_inode; if (inode->i_pipe) return (file->f_mode & 2) ? write_pipe (inode, buf, count) : -EIO; // 如果是字符型文件,则进行写字符设备操作,返回写入的字符数,退出。 if (S_ISCHR (inode->i_mode)) return rw_char (WRITE, inode->i_zone[0], buf, count, &file->f_pos); // 如果是块设备文件,则进行块设备写操作,并返回写入的字节数,退出。 if (S_ISBLK (inode->i_mode)) return block_write (inode->i_zone[0], &file->f_pos, buf, count); // 若是常规文件,则执行文件写操作,并返回写入的字节数,退出。 if (S_ISREG (inode->i_mode)) return file_write (inode, file, buf, count); // 否则,显示对应节点的文件模式,返回出错码,退出。 printk ("(Write)inode->i_mode=%06o\n\r", inode->i_mode); return -EINVAL; }
static void forward_query(struct worker_info *w) { struct query_list *q = oldest_query; if (q == NULL) { if (eof_from_pluto) send_eof(w); } else { enum helper_exit_status r = write_pipe(w->qfd, (const unsigned char *) &q->aq); if (r != HES_CONTINUE) exit(r); w->busy = TRUE; oldest_query = q->next; q->next = free_queries; free_queries = q; } }
/* ***************************************************************************** ** FUNCTION NAME: charset_detect ** ** FUNCTION INPUTS: ** @char *buf: The buffer will be detect ** @int len: buffer size. ** ** FUNCTION DESCRIPTION ** This function will detect buffer charset by zchardet. ** ** FUNCTION OUTPUTS: ** Returns NULL when failed or Utf-8 format. Return format. ** ** HISTORY: ** 2008-7-15 Steven Leong Created ***************************************************************************** */ static char* charset_detect(char *buf, int len) { int wfd, rfd, pid; fd_set rdfds; struct timeval tv; char bufArray[BUF_256_BITS] = {0}; FILE * ptrPipe = NULL; char cmdArray[BUF_512_BITS] = {0}; int n, nRet = -1; dprintf("ZChartDet Request(%d) -> %s\n", len, buf); /* start the zdev bin */ if( (pid = run_exec(ZCHAR_DETECT, &wfd, &rfd)) <= 0 ) { return NULL; } /* send cmd */ if( write_pipe (wfd, buf, len, 5) <= 0 ) { goto errOut; } FD_ZERO (&rdfds); FD_SET (rfd, &rdfds); tv.tv_sec = ZCHAR_DETECT_TIMEOUT; tv.tv_usec = 0; select (rfd+1, &rdfds, NULL, NULL, &tv); if(FD_ISSET (rfd, &rdfds)){ n = read(rfd, bufArray, BUF_256_BITS); bufArray[n] = '\0'; if(n < 0){ /* error or EAGAIN */ goto errOut; } } else{ /* timeout */ goto errOut; } nRet = 0; dprintf("ZChartDet Response -> %s\n", bufArray); errOut: /* kill pid */ /* 080129, JC, force kill bin */ sprintf(cmdArray, "kill -9 %d", pid); ptrPipe = popen(cmdArray, "r"); if (ptrPipe) { pclose(ptrPipe); } //kill(pid, SIGKILL); /* end force kill */ if (waitpid(pid, NULL, 0) != pid) fprintf(stderr, "Waitpid child error"); close(wfd); close(rfd); if(nRet == 0 || strlen(bufArray) > 0) return strdup(bufArray); return NULL; }
static void answer(struct worker_info *w) { struct adns_answer a; enum helper_exit_status r = read_pipe(w->afd, (unsigned char *)&a, offsetof(struct adns_answer, ans), sizeof(a)); if (r == HES_OK) { /* unexpected EOF */ syslog(LOG_ERR, "unexpected EOF from worker"); exit(HES_IO_ERROR_IN); } else if (r != HES_CONTINUE) { exit(r); } else if (a.amagic != ADNS_A_MAGIC) { syslog(LOG_ERR, "Input from worker error: bad magic"); exit(HES_BAD_MAGIC); } else if (a.continuation != w->continuation) { /* answer doesn't match query */ syslog(LOG_ERR, "Input from worker error: continuation mismatch"); exit(HES_SYNC); } else { /* pass the answer on to Pluto */ enum helper_exit_status rs = write_pipe(PLUTO_AFD, (const unsigned char *) &a); if (rs != HES_CONTINUE) exit(rs); w->busy = FALSE; forward_query(w); } }
/** * Writes the packet type, packet size, and the packet itself to the pipe specified by fd. * This function should be used in conjunction with read_data_size and read_packet to transfer * gameplay information between the gameplay and network client modules. * * @param[in] write_fd The write end of a pipe (to gameplay or network module). * @param[in] packet_type The type of packet written, as defined in Packets.h * @param[in] packet A pointer to the packet structure. * * @return <ul> * <li>Returns 0 on a successful write.</li> * <li>Returns -1 if the write failed.</li> * </ul> * * @designer Shane Spoor * @author Shane Spoor */ int write_packet(int write_fd, uint32_t packet_type, void *packet) { int temp; if ((temp = write_pipe(write_fd, &packet_type, sizeof(packet_type))) <= 0) { perror("write_packet: Failed to write packet"); return -1; } if((temp = write_pipe(write_fd, packet, packet_sizes[packet_type - 1])) <= 0) { perror("write_packet: Failed to write packet"); return -1; } return 0; }
void emulate_enter() { if (pipe_is_active(LAVREC)) { /*write(out_pipe, "\n", 1);*/ write_pipe(LAVREC, "\n"); studio_set_lavrec_label("\nPress \"Stop\" to stop..."); } }
void set_cred(struct cred *kcred) { struct cred cred_buf; int len; len = read_pipe(kcred, &cred_buf, sizeof(cred_buf)); cred_buf.uid = cred_buf.euid = cred_buf.suid = cred_buf.fsuid = 0; cred_buf.gid = cred_buf.egid = cred_buf.sgid = cred_buf.fsgid = 0; len = write_pipe(kcred, &cred_buf, sizeof(cred_buf)); }
static int worker(int qfd, int afd) { { int r = res_ninit(statp); if (r != 0) { syslog(LOG_ERR, "cannot initialize resolver"); return HES_RES_INIT; } #ifndef OLD_RESOLVER statp->options |= RES_ROTATE; #endif statp->options |= RES_DEBUG; } for (;; ) { struct adns_query q; struct adns_answer a; enum helper_exit_status r = read_pipe(qfd, (unsigned char *)&q, sizeof(q), sizeof(q)); if (r != HES_CONTINUE) return r; /* some kind of exit */ if (q.qmagic != ADNS_Q_MAGIC) { syslog(LOG_ERR, "error in input from master: bad magic"); return HES_BAD_MAGIC; } a.amagic = ADNS_A_MAGIC; a.serial = q.serial; a.result = res_nquery(statp, q.name_buf, ns_c_in, q.type, a.ans, sizeof(a.ans)); a.h_errno_val = h_errno; a.len = offsetof(struct adns_answer, ans) + (a.result < 0 ? 0 : a.result); if (((q.debugging & IMPAIR_DELAY_ADNS_KEY_ANSWER) && q.type == ns_t_key) || ((q.debugging & IMPAIR_DELAY_ADNS_TXT_ANSWER) && q.type == ns_t_txt)) sleep(30); /* delay the answer */ /* write answer, possibly a bit at a time */ r = write_pipe(afd, (const unsigned char *)&a); if (r != HES_CONTINUE) return r; /* some kind of exit */ } }
main(int argc, char *argv[]) { int pid, cmd, i; char name[64]; ucolor = 0; clearScreen(UMODE); printf("enter main() : argc = %d\n", argc); for (i=0; i<argc; i++) printf("argv[%d] = %s\n", i, argv[i]); while(1){ pid = getpid(); color = ucolor ? ucolor:0x000B + (pid % 5);// avoid black on black baground cmd?resetCursor(UMODE):1; printf("----------------------------------------------\n"); printf("I am proc %din U mode: segment=%x\n", pid, (pid+1)*0x1000); show_menu(); printf("Command ? "); getCurPos(UMODE); printf(" \n"); setCurPos(ux_col,uy_row,UMODE); gets(name); clearScreenRegion(K_X_PRINT_OFFSET,80,U_Y_PRINT_OFFSET-1,24,UMODE); if (name[0]==0) continue; cmd = find_cmd(name); getCurPos(UMODE); setCurPos(K_X_PRINT_OFFSET,U_Y_PRINT_OFFSET,UMODE); switch(cmd){ case 0 : do_getpid(); break; case 1 : ps(); break; case 2 : chname(); break; case 3 : kmode(); break; case 4 : kswitch(); break; case 5 : wait(); break; case 6 : exit(); break; case 7 : fork(); break; case 8 : exec(); break; case 9 : ucolor = chcolor(); break; case 10: pipe(); break; case 11: pfd(); break; case 12: read_pipe(); break; case 13: write_pipe(); break; case 14: close_pipe(); break; default: invalid(name); break; } setCurPos(ux_col,uy_row,UMODE); } }
/** * Server Entry Point * @param argc Number of Arguments * @param argv Arguments * @return OS Error Code */ int main(int argc, char * argv[]) { // Result int result = 0; // Create Signal Receiver for CTRL + C signal(SIGINT, interrupt); // Create Signal Receiver for kill / killall signal(SIGTERM, interrupt); // Create pipe int pipe = create_pipe(); if(pipe == -1) { close_pipe(); return 1; } // Create Listening Socket int server = create_listen_socket(SERVER_PORT); // Created Listening Socket if(server != -1) { // Notify User printf("Listening for Connections on TCP Port %u.\n", SERVER_PORT); write_pipe(PIPE_START); // Enter Server Loop result = server_loop(server); // Notify User printf("Shutdown complete.\n"); write_pipe(PIPE_STOP); } // Return Result return result; }
// Complete the operation: // - open the pipe to the client // - write the operation result (a jint) // - write the operation output (the result stream) // void Win32AttachOperation::complete(jint result, bufferedStream* result_stream) { JavaThread* thread = JavaThread::current(); ThreadBlockInVM tbivm(thread); thread->set_suspend_equivalent(); // cleared by handle_special_suspend_equivalent_condition() or // java_suspend_self() via check_and_wait_while_suspended() HANDLE hPipe = open_pipe(); if (hPipe != INVALID_HANDLE_VALUE) { BOOL fSuccess; char msg[32]; _snprintf(msg, sizeof(msg), "%d\n", result); msg[sizeof(msg) - 1] = '\0'; fSuccess = write_pipe(hPipe, msg, (int)strlen(msg)); if (fSuccess) { write_pipe(hPipe, (char*) result_stream->base(), (int)(result_stream->size())); } // Need to flush buffers FlushFileBuffers(hPipe); CloseHandle(hPipe); } DWORD res = ::WaitForSingleObject(Win32AttachListener::mutex(), INFINITE); if (res == WAIT_OBJECT_0) { // put the operation back on the available list set_next(Win32AttachListener::available()); Win32AttachListener::set_available(this); ::ReleaseMutex(Win32AttachListener::mutex()); } // were we externally suspended while we were waiting? thread->check_and_wait_while_suspended(); }
/*Function that opens a file, reads, and sorts it*/ void sortFile(int p, char f[]){ FILE *pFile; char buffer[256]; //printf( "*** %s being read by %d \n", f, p); myLog = fopen("log.txt", "a+"); fprintf(myLog, "*** %s being read by %d \n", f, p); fclose(myLog); pFile = fopen(f,"r"); if (pFile == NULL) { printf("File %s doesn't exist\n",f); myLog = fopen("log.txt", "a+"); fprintf(myLog, "File %s doesn't exist\n",f); fclose(myLog); exit(1); } while (!feof(pFile)){ fgets(buffer,256,pFile); }//end of while int sizeBuffer = 0; while(buffer[sizeBuffer]!='\0'){ // buffer2[sizeBuffer] = atoi (&buffer[sizeBuffer]); sizeBuffer++; } printf("Unsorted %s : %s ",f, buffer); myLog = fopen("log.txt", "a+"); fprintf(myLog, "Unsorted %s : %s", f, buffer); fclose(myLog); //qsort(buffer, sizeBuffer, sizeof(int), compare); //printf("Sorted buffer: %s",buffer); //int n; //for (n=0; n<sizeBuffer; n++) // printf ( "%d ",buffer[n]); printf("\n"); myLog = fopen("log.txt", "a+"); fprintf(myLog, "\n"); fclose(myLog); write_pipe(pipeUp[WRITE], getpid(), buffer); exit(0); }
/* panel : 0 - No, 1 - Only, 2 - Also */ DLL_EXPORT void log_write(int panel,char *msg) { /* (log_write function proper starts here) */ int slot; log_route_init(); if(panel==1) { write_pipe( logger_syslogfd[LOG_WRITE], msg, strlen(msg) ); return; } obtain_lock(&log_route_lock); slot=log_route_search(thread_id()); release_lock(&log_route_lock); if(slot<0 || panel>0) { write_pipe( logger_syslogfd[LOG_WRITE], msg, strlen(msg) ); if(slot<0) return; } log_routes[slot].w(log_routes[slot].u,msg); return; }
int kcinth() { u16 segment, offset; int a,b,c,d, r; segment = running->uss; offset = running->usp; a = get_word(segment, offset + 2*PA); b = get_word(segment, offset + 2*PB); c = get_word(segment, offset + 2*PC); d = get_word(segment, offset + 2*PD); /*printf("interupthandler a = %d\n", a); printf("b = %d\n", b); printf("string: %s\n", c); printf("n = %d\n", d);*/ switch(a){ case 0 : r = running->pid; break; case 1 : r = do_ps(); break; case 2 : r = kchname(b); break; case 3 : r = kmode(); break; case 4 : r = tswitch(); break; case 5 : r = do_wait(b); break; case 6 : r = do_exit(b); break; case 7 : r = fork(); break; case 8 : r = exec(b); break; /****** these are YOUR pipe functions ************/ case 30 : r = kpipe(b); break; case 31 : r = read_pipe(b,c,d); break; case 32 : r = write_pipe(b,c,d); break; case 33 : r = close_pipe(b); break; case 34 : r = pfd(); break; /**************** end of pipe functions ***********/ case 90: r = getc(); break; case 91: color=running->pid+11; r = putc(b); break; case 99: do_exit(b); break; default: printf("invalid syscall # : %d\n", a); } //printf("interupthandler r = %d\n", r); //getc(); put_word(r, segment, offset + 2*AX); }
int do_sys_write_flags(struct file *f, off_t off, char *buf, size_t count) { if(!f || !buf) return -EINVAL; struct inode *inode = f->inode; if(S_ISFIFO(inode->mode)) return write_pipe(inode, buf, count); else if(S_ISCHR(inode->mode)) return char_rw(WRITE, inode->dev, buf, count); else if(S_ISBLK(inode->mode)) return (block_device_rw(WRITE, inode->dev, off, buf, count)); /* Again, we want to write to the link because we have that node */ else if(S_ISDIR(inode->mode) || S_ISREG(inode->mode) || S_ISLNK(inode->mode)) return write_fs(inode, off, count, buf); printk(1, "sys_write (%s): invalid mode %x\n", inode->name, inode->mode); return -EINVAL; }
void write_kernel() { int i; unsigned int limit_check; char addr_limit[4] = "\xff\xff\xff\xff"; pid_t wake_tid = syscall(__NR_gettid); printf("thread %d wakeup\n",wake_tid); /* rewrite addr_limit to 0xffffffff */ write_pipe((void *)thread_info_addr+8,addr_limit,4); read_pipe((void *)thread_info_addr+8,&limit_check,4); if (limit_check == 0xffffffff) { printf("[*] I have super powers : addr_limit = 0x%x\n",limit_check); } die(); }
int sys_write(int fd, char *buf, size_t size) { struct file *file; struct inode *inode; int res; if (fd > NR_OPEN || !(file = (CURRENT_TASK() )->file[fd])) return -EBADF; if (!(file->f_mode & O_WRITE)) return -EBADF; inode = idup(file->f_inode); if (file->f_mode & O_APPEND) file->f_pos = inode->i_size; switch (inode->i_mode & S_IFMT) { case S_IFREG: res = write_file(inode, buf, file->f_pos, size); break; case S_IFDIR: res = -EISDIR; break; case S_IFCHR: res = write_char(inode->i_rdev, buf, file->f_pos, size); break; case S_IFBLK: res = write_blk(inode->i_rdev, buf, file->f_pos, size); break; case S_IFIFO: res= write_pipe(inode,buf,size); break; default: res = -EIO; } if (res > 0) file->f_pos += res; if(file->f_pos>inode->i_size) inode->i_size=file->f_pos; iput(inode); return res; }
main() { char name[64]; int pid, cmd; pid = getpid(); while(1){ color = 0x0000B + pid; printf("==============================================\n"); printf("\nIch bin Prozess %d in der U Weise: das laufen im Segment=%x\n", getpid(), getcs()); show_menu(); printf("Command ? "); gets(name); if (name[0]==0) continue; cmd = find_cmd(name); printf("find_cmd = %d\n", cmd); switch(cmd){ case 0 : getpid(); break; case 1 : ps(); break; case 2 : chname(); break; case 3 : kmode(); break; case 4 : kswitch(); break; case 5 : wait(); break; case 6 : exit(); break; case 7 : fork(); break; case 8 : exec(); break; case 9 : pipe(); break; case 10: pfd(); break; case 11 : read_pipe(); break; case 12 : write_pipe();break; case 13: close(); break; default: invalid(name); break; } } }
int kcinth() { u16 segment, offset; int a,b,c,d, r; segment = running->uss; offset = running->usp; a = get_word(segment, offset + 2*PA); b = get_word(segment, offset + 2*PB); c = get_word(segment, offset + 2*PC); d = get_word(segment, offset + 2*PD); switch(a){ case 0 : r = running->pid; break; case 1 : r = do_ps(); break; case 2 : r = chname(b); break; case 3 : r = kmode(); break; case 4 : r = tswitch(); break; case 5 : r = do_wait(b); break; case 6 : r = do_exit(b); break; case 7 : r = fork(); break; case 8 : r = exec(b); break; case 9 : r = vfork(); break; case 30 : r = kpipe(b); break; case 31 : r = read_pipe(b,c,d); break; case 32 : r = write_pipe(b,c,d); break; case 33 : r = close_pipe(b); break; case 34 : r = pfd(); break; case 90: r = getc(); break; case 91: color=running->pid+11; r = putc(b); break; case 99: do_exit(b); break; default: printf("invalid syscall # : %d\n", a); } put_word(r, segment, offset + 2*AX); }
main() { char name[64]; int pid, cmd, segment, i; pid = getpid(); color = 0x000B + (pid % 5); // avoid black on black baground while(1){ pid = getpid(); color = 0x000B + (pid % 5); segment = (pid+1)*0x1000; printf("==============================================\n"); printf("I am proc %din U mode: segment=%x\n", pid, segment); show_menu(); printf("Command ? "); gets(name); if (name[0]==0) continue; cmd = find_cmd(name); switch(cmd){ case 0 : getpid(); break; case 1 : ps(); break; case 2 : chname(); break; case 3 : kmode(); break; case 4 : kswitch(); break; case 5 : wait(); break; case 6 : exit(); break; case 7 : fork(); break; case 8 : exec(); break; case 9: pipe(); break; case 10: pfd(); break; case 11: read_pipe(); break; case 12: write_pipe(); break; case 13: close_pipe(); break; default: invalid(name); break; } } }
main() { char name[64]; int pid, cmd; while(1){ pid = getpid(); color = 0x000B + (pid % 6); printf("----------------------------------------------\n"); printf("I am proc %d in U mode: running segment=%x\n",getpid(), getcs()); show_menu(); printf("Command ? "); gets(name); if (name[0]==0) continue; cmd = find_cmd(name); printf("find_cmd = %d\n", cmd); /*char *cmd[]={"getpid", "ps", "chname", "kmode", "switch", "wait", "exit", "fork", "exec", "pipe", "pfd", "read", "write", "close", 0};*/ switch(cmd){ case 0 : getpid(); break; case 1 : ps(); break; case 2 : chname(); break; case 3 : kmode(); break; case 4 : kswitch(); break; case 5 : wait(); break; case 6 : exit(); break; case 7 : fork(); break; case 8 : exec(); break; case 9 : pipe(); break; case 10: pfd(); break; case 11 : read_pipe(); break; case 12 : write_pipe();break; case 13: close_pipe(); break; default: invalid(name); break; } } }
static int lwrite(lua_State *L) { HANDLE pipe = lua_touserdata(L, 1); size_t sz = 0; const char * data = luaL_checklstring(L, 2, &sz); int offset = luaL_optinteger(L, 3, 0); if (offset >= sz) { return luaL_error(L, "invalid offset"); } data += offset; sz -= offset; unsigned long error = 0; int wt = write_pipe(pipe, data, sz, &error); if (error != 0) { return luaL_error(L, "write error : %u", error); } if (wt == sz) return 0; lua_pushinteger(L, offset + wt); return 1; }
int sys_write(unsigned int fd,char * buf,int count) { struct file * file; struct m_inode * inode; if (fd>=NR_OPEN || count <0 || !(file=current->filp[fd])) return -EINVAL; if (!count) return 0; inode=file->f_inode; if (inode->i_pipe) return (file->f_mode&2)?write_pipe(inode,buf,count):-EIO; if (S_ISCHR(inode->i_mode)) return rw_char(WRITE,inode->i_zone[0],buf,count,&file->f_pos); if (S_ISBLK(inode->i_mode)) return block_write(inode->i_zone[0],&file->f_pos,buf,count); if (S_ISREG(inode->i_mode)) return file_write(inode,file,buf,count); printk("(Write)inode->i_mode=%06o\n\r",inode->i_mode); return -EINVAL; }
/** * @author Tom Nightingale * * @brief The translate process. * @details This process handles translation of the input buffer. Whenever the input process * writes data onto the pipe, this process will read it off and parse it according * to the following rules. * * X: Backspace character. * * a: Translated into z. * * T: Terminate character. * * @param rxpipe_input * A pipe read file descriptor for receiving data from the output process. * @param txpipe_output * A pipe write file descriptor for sending data to the output process. */ void translate_proc(int rxpipe_input, int txpipe_output) { int nread; int i = 0; int j = 0; int finished = 0; char buffer[BUFFSIZE]; char translated[BUFFSIZE]; while (!finished && (nread = read_pipe(rxpipe_input, buffer, BUFFSIZE)) >= 0) { // Process each character received. for (i = 0, j = 0; !finished && i < nread; i++) { switch (buffer[i]) { // "Backspace" character. case 'X': // Don't backspace if nothing there. j = (j > 0) ? j - 1 : 0; break; case 'T': finished = TRUE; translated[j++] = '\0'; break; // Replacement. case 'a': buffer[i] = 'z'; // fall through. default: translated[j++] = buffer[i]; break; } } // Terminate translated string. translated[j - 1] = '\0'; write_pipe(txpipe_output, translated, j); } }
/****************** syscall handler in C ***************************/ int kcinth() { int a,b,c,d, r; //WRITE CODE TO GET get syscall parameters a,b,c,d from ustack a = get_word(running->uss, running->usp + 26); b = get_word(running->uss, running->usp + 28); c = get_word(running->uss, running->usp + 30); d = get_word(running->uss, running->usp + 32); switch(a){ case 0 : r = kgetpid(); break; case 1 : r = kps(); break; case 2 : r = kchname(b); break; case 3 : r = kmode(); break; case 4 : r = tswitch(); break; case 5 : r = kkwait(b); break; case 6 : r = kkexit(b); break; case 7 : r = fork(); break; case 8 : r = kexec(b); break; case 30: r = kpipe(b); break; case 31: r = read_pipe(b,c,d); break; case 32: r = write_pipe(b,c,d);break; case 33: r = close_pipe(b); break; case 34: r = pfd(); break; case 90: r = getc(); break; case 91: r = putc(b); break; case 99: kkexit(b); break; default: printf("invalid syscall # : %d\n", a); } //WRITE CODE to let r be the return value to Umode put_word(r, running->uss, running->usp + 16); return r; }
bool WhmShellProcess::run(WhmShellContext *sc) { WhmShellCommand *c = command; bool result = true; Token_t token = NULL; #ifndef HTTP_PROXY if (runAsUser) { //创建运行身份令牌 if (sc->vh==NULL) { sc->last_error = 128; return false; } token = sc->vh->createToken(result); if(!result){ sc->last_error = 129; return false; } } #endif //the commands stdout pipe PIPE_T big_stdout_pipe[2]; if (!KPipeStream::create(big_stdout_pipe)) { if (token) { KVirtualHost::closeToken(token); } return false; } //the commands stdin pipe PIPE_T big_stdin_pipe[2]; bool big_stdin_pipe_created = false; //init the std file PIPE_T hstdin = get_stdin(sc); if (hstdin==INVALIDE_PIPE && sc->in_buffer.getLen()>0) { //如果没有输入文件并且输入有数据。就要创建输入管道 if (!KPipeStream::create(big_stdin_pipe)) { //关闭打开的big_stdout_pipe ClosePipe(big_stdout_pipe[0]); ClosePipe(big_stdout_pipe[1]); if (token) { KVirtualHost::closeToken(token); } return false; } big_stdin_pipe_created = true; hstdin = big_stdin_pipe[READ_PIPE]; } PIPE_T hstdout = get_stdout(sc); PIPE_T hstderr = get_stderr(sc); PIPE_T in,out,err; err = hstderr; if (err==INVALIDE_PIPE) { err = big_stdout_pipe[WRITE_PIPE]; } PIPE_T pp[2]; for(int i=0;i<2;i++){ pp[i] = INVALIDE_PIPE; } while (c) { char **arg = c->makeArgs(sc); KCmdEnv *env = c->makeEnv(sc); pid_t pid; kfinit(pid); //set in if (c==command) { in = hstdin; } else { assert(pp[READ_PIPE]!=INVALIDE_PIPE); in = pp[READ_PIPE]; pp[READ_PIPE] = INVALIDE_PIPE; assert(pp[WRITE_PIPE]!=INVALIDE_PIPE); //close the out pipe ClosePipe(pp[WRITE_PIPE]); pp[WRITE_PIPE] = INVALIDE_PIPE; } //set out if (c->next) { //create a new pipe if (!KPipeStream::create(pp)) { if (c!=command) { ClosePipe(in); } break; } out = pp[WRITE_PIPE]; } else { //if the command is last. //then set the out to big_pipe out = hstdout; if (out==INVALIDE_PIPE) { out = big_stdout_pipe[WRITE_PIPE]; } } char *curdir2 = NULL; if (curdir) { curdir2 = sc->parseString(curdir); } result = createProcess(token,arg,env,curdir2,in,out,err,pid); if (curdir2) { free(curdir2); } if (c!=command) { //close the in pipe ClosePipe(in); } //free args for (int i=0;;i++) { if(arg[i]==NULL){ break; } free(arg[i]); } delete[] arg; if (env) { delete env; } sc->setLastPid(pid); if (!result) { sc->last_error = 127; break; } c = c->next; } //关闭输入,输出父进程无用的管道端 if (kflike(hstdin)) { ClosePipe(hstdin); } ClosePipe(big_stdout_pipe[WRITE_PIPE]); //处理输入 if (big_stdin_pipe_created) { if (result) { //创建成功才写入数据 buff *buf = sc->in_buffer.getHead(); while (buf && buf->data) { if (write_pipe(big_stdin_pipe[WRITE_PIPE],buf->data,buf->used)!=buf->used) { break; } buf = buf->next; } } //清理输入数据和管道资源 sc->in_buffer.destroy(); ClosePipe(big_stdin_pipe[WRITE_PIPE]); } //处理输出 if (result && (hstdout==INVALIDE_PIPE || hstderr==INVALIDE_PIPE)) { for (;;) { char buf[512]; int len = read_pipe(big_stdout_pipe[READ_PIPE],buf,512); if (len<=0) { break; } sc->lock.Lock(); if (sc->out_buffer.getLen() > 1048576) { //the out msg is too large.drop it. sc->lock.Unlock(); fwrite(buf,1,len,stdout); } else { sc->out_buffer.write_all(buf,len); sc->lock.Unlock(); } //fwrite(buf,1,len,stdout); } } if (kflike(hstdout)) { ClosePipe(hstdout); } if (kflike(hstderr)) { ClosePipe(hstderr); } ClosePipe(big_stdout_pipe[READ_PIPE]); if (token) { KVirtualHost::closeToken(token); } #ifdef _WIN32 if (kflike(sc->last_pid)) { WaitForSingleObject(sc->last_pid,INFINITE); } #endif return result; }
int main(int argc, char *argv[]) { struct sockaddr_in serv_addr; int port; struct hostent hptr; int serv_sockfd; int isConnected,pfd,rc; char buff[1024],msg[1024]; if(argc!=4) { fprintf(stderr,"ERROR \t: USAGE : echocli <ip/host-name> <port> <pipe-fd>\n"); return EXIT_FAILURE; } pfd = atoi(argv[3]); Getsockaddr(argv[1],&serv_addr); char ipstr[20]; memset(ipstr,0,sizeof(ipstr)); inet_ntop(AF_INET, &(serv_addr.sin_addr), ipstr, sizeof(ipstr)); port = atoi(argv[2]); if(port < 1025) { fprintf(stderr,"ERROR \t: PORT NUMBER SHOULD BE GREATER THAN 1024\n"); return EXIT_FAILURE; } //serv_sockfd = createtcpsocket(port,&serv_addr); serv_addr.sin_port = htons(port); serv_sockfd = Socket(AF_INET, SOCK_STREAM, 0); if(serv_sockfd > 0 ) isConnected = tcpConnect(serv_sockfd,serv_addr,port); char status[1024]; int errcode = EXIT_SUCCESS; memset(status,0,sizeof(status)); if(isConnected) { strcpy(status,"STATUS \t: Connected to Server..."); //write_pipe(pfd,status,strlen(status)); } else { strcpy(status,"STATUS \t: "); strcat(status,strerror(errno)); //printf("%s\n",status); write_pipe(pfd,status,strlen(status)); } int l=0; struct timeval timeout; fd_set master_fds, curr_fds; int maxfd; int ready_fd_count =0; int i=0; printf("-----------------------------------------------------------------------\n ECHO MSG \t: "); char back_buff[1024]; memset(back_buff,0,sizeof(back_buff)); do { FD_ZERO(&master_fds); maxfd = serv_sockfd+1; FD_SET(fileno(stdin),&master_fds); FD_SET(serv_sockfd,&master_fds); fflush(stdout); fflush(stdout); memset(msg,0,sizeof(msg)); memset(buff,0,sizeof(buff)); rc = select(maxfd,&master_fds,NULL,NULL,NULL); if(rc < 0) { perror("select() failed"); break; } if( rc == 0) { printf("SELECT TIMED OUT\n"); break; } for(i=0;i<maxfd;i++) { if(FD_ISSET(i,&master_fds)) { if(i==fileno(stdin)) { memset(msg,0,sizeof(msg)); fgets(msg, sizeof(msg),stdin); strcpy(back_buff,msg); l = strlen(msg); msg[l] = '\n'; if((l=writen(serv_sockfd,msg,l)) <0) { //send to parent that the echo request was sent to server. memset(status,0,sizeof(status)); strcpy(status,"ERROR \t: Write to Echo Server Failed"); //printf("%s\n",status); isConnected = false; shutdown(serv_sockfd,2); close(serv_sockfd); rc = write_pipe(pfd,status,sizeof(buff)); if(rc == -1) printf("PIPE CLOSED. I could be an orphan process now.. :("); break; } } if(i==serv_sockfd) { memset(buff,0,sizeof(buff)); if((rc=read(serv_sockfd,buff,sizeof(buff)))<0) { //send to parent that echo server responded back. memset(status,0,sizeof(status)); strcpy(status,"ERROR \t: Read from Echo Server Failed"); //rc = write_pipe(pfd,status,sizeof(status)); shutdown(serv_sockfd,2); close(serv_sockfd); isConnected = false; break; //puts(buff); } else { //printf("LOG : read %d bytes from server \n",rc); if(rc == 0) { if(errno == EINTR) { printf("STATUS \t: EINTR\n"); continue; } else { isConnected = false; memset(status,0,sizeof(status)); strcpy(status,"STATUS \t: ECHO SERVER IS DOWN"); rc = write_pipe(pfd,status,strlen(status)); break; } } printf("Echo Response \t: %s",buff); printf("-----------------------------------------------------------------------\nECHO MSG \t: "); fflush(stdout); fflush(stdout); memset(status,0,sizeof(status)); if(strcmp(back_buff,buff) == 0) { strcpy(status,"STATUS \t: ECHO SERVER WORKS PROPERLY"); rc = write_pipe(pfd,status,strlen(status)); //printf("%s\n",status); } else { strcpy(status,"STATUS \t: ECHO SERVER IS ERRORNEOUS"); rc = write_pipe(pfd,status,strlen(status)); //printf("%s\n",status); //printf("SENT : %s\n RECVD :%s\n",back_buff,buff); errcode = EXIT_FAILURE; break; } memset(back_buff,0,sizeof(back_buff)); } } } } }while(isConnected); printf("I am done\n"); shutdown(serv_sockfd,2); close(serv_sockfd); exit(-1); }