static struct process *get_parent(struct thread *t) { if (t) return t->parent; return process_get(0); }
void handle_user_input(char *line, void *cbdata) { char **chunk_list; bt_config_t *config; bzero(chunkf, sizeof(chunkf)); bzero(outf, sizeof(outf)); config = (bt_config_t *) cbdata; if (sscanf(line, "GET %120s %120s", chunkf, outf)) { if (strlen(outf) > 0) { if ((chunk_list = process_get(chunkf, outf)) == NULL ) { perror("I/O error"); exit(-1); } // record all hash chunks for the current download work_queue = chunk_list; broadcast_query(chunk_list, config); } } }
void accept_request(int socket) { char buf[MAX_BUFFER]; int read_size = 0, len = 0; while ((read_size = recv(socket, &buf[len], (MAX_BUFFER-len), 0)) > 0) { char line[read_size]; char method[32]; char host[512]; char http_version[64]; strncpy(line, &buf[len], sizeof(line)); len += read_size; line[read_size] = '\0'; printf("Found: %s\n", line); sscanf(line, "%s %s %s", method, host, http_version); // printf("%s %s %s\n", method, host, http_version); if(strncmp(method, "GET", 3) == 0) { printf("Processing GET"); process_get(host, socket, line); } else { printf("This is not a supported method\n"); } } }
int vfs_read(struct thread *t, int fd, void *buf, size_t count) { int ret; struct process *p; struct req_rdwr req; struct file *file; /* Kernel request */ if (!t) p = process_get(0); else p = t->parent; ret = process_file_from_fd(p, fd, &file); if (ret < 0) return ret; if (!file->f_ops->read) return -ENOSYS; req.inode = 0; if (file->inode) req.inode = file->inode->inode; req.size = count; req.off = file->offset; ret = file->f_ops->read(file, p, &req, buf); if (ret < 0) return ret; file->offset = req.off; return ret; }
struct thread *syscall_exit(struct thread *image) { if (image->proc->pid == 1) { debug_panic("init died"); } process_switch(process_get(1)); process_kill(image->proc); return thread_switch(image, schedule_next()); }
void handle_user_input(char *line, void *cbdata) { char chunkf[128], outf[128]; bzero(chunkf, sizeof(chunkf)); bzero(outf, sizeof(outf)); if (sscanf(line, "GET %120s %120s", chunkf, outf)) { if (strlen(outf) > 0) { process_get(chunkf, outf); } } }
bool process_check_destroy(pid_t pid) { struct process *p = process_get(pid); // check whether the process has exited if (process_checkon(p) == -1) return false; process_destroy(p->ps_pid); // returning true removes pid from the pid_set // during pid_set_map() return true; }
// Handle input from Erlang VM static void outputv(ErlDrvData handle, ErlIOVec *ev) { bdb_drv_t* pdrv = (bdb_drv_t*) handle; ErlDrvBinary* data = ev->binv[1]; int command = data->orig_bytes[0]; // First byte is the command switch(command) { case 'O': process_open(pdrv, ev); break; case 'S': process_set(pdrv, ev); break; case 'G': process_get(pdrv, ev); break; case 'D': process_del(pdrv, ev); break; case 'C': process_count(pdrv, ev); break; case 'F': process_flush(pdrv, ev); break; case 'B': process_bulk_get(pdrv, ev); break; case 'Z': process_compact(pdrv, ev); break; case 'T': process_truncate(pdrv, ev); break; default: process_unkown(pdrv, ev); } }
int vfs_lseek(struct thread *t, int fd, off_t offset, int whence) { struct file *file; struct process *process; /* Kernel request */ if (!t) process = process_get(0); else process = t->parent; if (!(whence & VFS_SEEK_SET) && !(whence & VFS_SEEK_CUR) && !(whence & VFS_SEEK_END)) return -EINVAL; if (fd < 0 || fd > PROCESS_MAX_OPEN_FD) return -EINVAL; file = &process->files[fd]; if (!file->used) return -EINVAL; if (whence & VFS_SEEK_SET) { if (offset < 0) return -EINVAL; file->offset = offset; } else if (whence & VFS_SEEK_CUR) { size_t old_off = file->offset; file->offset += offset; if (offset < 0 && file->offset > old_off) { file->offset = old_off; return -EINVAL; } if (offset > 0 && file->offset < old_off) { process->files[fd].offset = old_off; return -EINVAL; } } else { kernel_panic("VFS_SEEK_END not implemented yet"); } return 0; }
/* PURPOSE: Creates a data connection whenever a LIST, GET or PUT command * is entered by client. */ void create_data_connect(char command[], char ip[], char portnum[]){ int sock_data; struct sockaddr_in inf; //create a tcp socket if ((sock_data = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("\nError in socket()"); exit(1); } //construct the struct memset(&inf, 0, sizeof(inf)); /* Clear struct */ inf.sin_family = AF_INET; /* Internet/IP */ inf.sin_addr.s_addr = inet_addr(ip); /* IP address */ //here we use the port number from the control connection + 1 inf.sin_port = htons(atoi(portnum) + 1); /* server port */ //connect to the server if (connect(sock_data, (struct sockaddr *) &inf, sizeof(inf)) < 0) { perror("\nError in connect()"); exit(1); } //if we issued a PUT command if(strncmp("PUT", command, 3) == 0){ process_put(command, sock_data); } else if(strncmp("GET", command, 3) == 0){ process_get(command, sock_data); } //we issued a LIST command else if(strncmp("LIST", command, 4) == 0){ process_list(sock_data); } else{ fprintf(stderr, "\nNot a valid command.\n\n"); } }
int process_request(struct request_state* req) { switch (req->cmd) { case cmd_get: if (process_get(req)) return -1; break; case cmd_set: if (process_set(req)) return -1; break; } req->state = conn_send; return 0; }
TSRM_API int pclose(FILE *stream) { DWORD termstat = 0; process_pair *process; if ((process = process_get(stream)) == NULL) { return 0; } fflush(process->stream); fclose(process->stream); WaitForSingleObject(process->prochnd, INFINITE); GetExitCodeProcess(process->prochnd, &termstat); process->stream = NULL; CloseHandle(process->prochnd); return termstat; }
intptr_t process_command(connection_t *conn){ intptr_t result=OK; char *data=conn->rc->command->data; if(strcmp(data,"get")==0){ result=process_get(conn); }else if(strcmp(data,"set")==0){ result=process_set(conn); }else if(strcmp(data,"version")==0){ process_version(conn); }else if(strcmp(data,"delete")==0){ result=process_delete(conn); }else if(strcmp(data,"quit")==0){ close_connection(conn); }else if(strcmp(data,"stats")==0){ process_stats(conn); }else{ process_client_error(conn,unsupport_command); } return result; }
struct thread *thread_send(struct thread *image, pid_t target, portid_t port, struct msg *msg) { struct process *p_targ; struct thread *new_image; /* find target process */ p_targ = process_get(target); /* check process */ if (!p_targ || !p_targ->entry) { return image; } /* create new thread */ new_image = thread_alloc(); thread_bind(new_image, p_targ); new_image->ds = 0x23; new_image->cs = 0x1B; new_image->ss = 0x23; new_image->eflags = 0; new_image->useresp = new_image->stack + SEGSZ; new_image->proc = p_targ; new_image->eip = p_targ->entry; /* set up registers in new thread */ new_image->ebx = 0; new_image->ecx = (msg) ? msg->count : 0; new_image->edx = port; new_image->esi = (image) ? image->proc->pid : 0; new_image->edi = 0; new_image->msg = msg; /* set new thread's user id */ new_image->user = (!image || p_targ->user) ? p_targ->user : image->user; /* insert new thread into scheduler */ schedule_insert(new_image); /* return new thread */ return new_image; }
struct thread *syscall_exit(struct thread *image) { struct thread *new_image; uint16_t pid; uint32_t parent; pid = image->proc->pid; if (pid == 1) { debug_panic("init died"); } parent = image->proc->parent->pid; process_switch(process_get(1)); new_image = thread_send(image, parent, PORT_CHILD, NULL); image->proc->status = image->eax; process_kill(image->proc); return new_image; }
/** * \brief Control request event handler * * This implementation handles the control requests for the GuiderPort device */ void EVENT_USB_Device_ControlRequest() { if (is_control()) { if (is_incoming()) { switch (USB_ControlRequest.bRequest) { case FOCUSER_RESET: process_reset(); break; case FOCUSER_SET: process_set(); break; case FOCUSER_LOCK: process_lock(); break; case FOCUSER_STOP: process_stop(); break; case FOCUSER_SERIAL: process_serial(); break; } } if (is_outgoing()) { switch (USB_ControlRequest.bRequest) { case FOCUSER_GET: process_get(); break; case FOCUSER_RCVR: process_rcvr(); break; case FOCUSER_SAVED: process_saved(); break; } } } }
void process_requests(void) { int retval = 0; request *current, *trailer; current = request_ready; while (current) { #ifdef CRASHDEBUG crashdebug_current = current; #endif if (current->buffer_end) { req_flush(current); if (current->status == CLOSE) retval = 0; else retval = 1; } else { switch (current->status) { case READ_HEADER: case ONE_CR: case ONE_LF: case TWO_CR: retval = read_header(current); break; case BODY_READ: retval = read_body(current); break; case BODY_WRITE: retval = write_body(current); break; case WRITE: retval = process_get(current); break; case PIPE_READ: retval = read_from_pipe(current); break; case PIPE_WRITE: retval = write_from_pipe(current); break; default: retval = 0; #if 0 fprintf(stderr, "Unknown status (%d), closing!\n", current->status); #endif break; } } if (lame_duck_mode) SQUASH_KA(current); switch (retval) { case -1: /* request blocked */ trailer = current; current = current->next; block_request(trailer); break; default: /* everything else means an error, jump ship */ send_r_error(current); /* fall-through */ case 0: /* request complete */ trailer = current; current = current->next; free_request(&request_ready, trailer); break; case 1: /* more to do */ current->time_last = time_counter; current = current->next; break; } } #ifdef CRASHDEBUG crashdebug_current = current; #endif }
static int parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, int err_abort) { char *path1, *path2, *tmp; int pflag, lflag, iflag, cmdnum, i; unsigned long n_arg; Attrib a, *aa; char path_buf[MAXPATHLEN]; int err = 0; glob_t g; path1 = path2 = NULL; cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, &path1, &path2); if (iflag != 0) err_abort = 0; memset(&g, 0, sizeof(g)); /* Perform command */ switch (cmdnum) { case 0: /* Blank line */ break; case -1: /* Unrecognized command */ err = -1; break; case I_GET: err = process_get(conn, path1, path2, *pwd, pflag); break; case I_PUT: err = process_put(conn, path1, path2, *pwd, pflag); break; case I_RENAME: path1 = make_absolute(path1, *pwd); path2 = make_absolute(path2, *pwd); err = do_rename(conn, path1, path2); break; case I_SYMLINK: path2 = make_absolute(path2, *pwd); err = do_symlink(conn, path1, path2); break; case I_RM: path1 = make_absolute(path1, *pwd); remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); for (i = 0; g.gl_pathv[i] && !interrupted; i++) { printf("Removing %s\n", g.gl_pathv[i]); err = do_rm(conn, g.gl_pathv[i]); if (err != 0 && err_abort) break; } break; case I_MKDIR: path1 = make_absolute(path1, *pwd); attrib_clear(&a); a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; a.perm = 0777; err = do_mkdir(conn, path1, &a); break; case I_RMDIR: path1 = make_absolute(path1, *pwd); err = do_rmdir(conn, path1); break; case I_CHDIR: path1 = make_absolute(path1, *pwd); if ((tmp = do_realpath(conn, path1)) == NULL) { err = 1; break; } if ((aa = do_stat(conn, tmp, 0)) == NULL) { xfree(tmp); err = 1; break; } if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { error("Can't change directory: Can't check target"); xfree(tmp); err = 1; break; } if (!S_ISDIR(aa->perm)) { error("Can't change directory: \"%s\" is not " "a directory", tmp); xfree(tmp); err = 1; break; } xfree(*pwd); *pwd = tmp; break; case I_LS: if (!path1) { do_globbed_ls(conn, *pwd, *pwd, lflag); break; } /* Strip pwd off beginning of non-absolute paths */ tmp = NULL; if (*path1 != '/') tmp = *pwd; path1 = make_absolute(path1, *pwd); err = do_globbed_ls(conn, path1, tmp, lflag); break; case I_LCHDIR: if (chdir(path1) == -1) { error("Couldn't change local directory to " "\"%s\": %s", path1, strerror(errno)); err = 1; } break; case I_LMKDIR: if (mkdir(path1, 0777) == -1) { error("Couldn't create local directory " "\"%s\": %s", path1, strerror(errno)); err = 1; } break; case I_LLS: local_do_ls(cmd); break; case I_SHELL: local_do_shell(cmd); break; case I_LUMASK: umask(n_arg); printf("Local umask: %03lo\n", n_arg); break; case I_CHMOD: path1 = make_absolute(path1, *pwd); attrib_clear(&a); a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; a.perm = n_arg; remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); for (i = 0; g.gl_pathv[i] && !interrupted; i++) { printf("Changing mode on %s\n", g.gl_pathv[i]); err = do_setstat(conn, g.gl_pathv[i], &a); if (err != 0 && err_abort) break; } break; case I_CHOWN: case I_CHGRP: path1 = make_absolute(path1, *pwd); remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); for (i = 0; g.gl_pathv[i] && !interrupted; i++) { if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { if (err != 0 && err_abort) break; else continue; } if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { error("Can't get current ownership of " "remote file \"%s\"", g.gl_pathv[i]); if (err != 0 && err_abort) break; else continue; } aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; if (cmdnum == I_CHOWN) { printf("Changing owner on %s\n", g.gl_pathv[i]); aa->uid = n_arg; } else { printf("Changing group on %s\n", g.gl_pathv[i]); aa->gid = n_arg; } err = do_setstat(conn, g.gl_pathv[i], aa); if (err != 0 && err_abort) break; } break; case I_PWD: printf("Remote working directory: %s\n", *pwd); break; case I_LPWD: if (!getcwd(path_buf, sizeof(path_buf))) { error("Couldn't get local cwd: %s", strerror(errno)); err = -1; break; } printf("Local working directory: %s\n", path_buf); break; case I_QUIT: /* Processed below */ break; case I_HELP: help(); break; case I_VERSION: printf("SFTP protocol version %u\n", sftp_proto_version(conn)); break; case I_PROGRESS: showprogress = !showprogress; if (showprogress) printf("Progress meter enabled\n"); else printf("Progress meter disabled\n"); break; default: fatal("%d is not implemented", cmdnum); } if (g.gl_pathc) globfree(&g); if (path1) xfree(path1); if (path2) xfree(path2); /* If an unignored error occurs in batch mode we should abort. */ if (err_abort && err != 0) return (-1); else if (cmdnum == I_QUIT) return (1); return (0); }
void peer_run(bt_config_t *config) { struct sockaddr_in myaddr; fd_set readfds; struct user_iobuf *userbuf; struct timeval timeout; int i, j; if ((userbuf = create_userbuf()) == NULL ) { perror("peer_run could not allocate userbuf"); exit(-1); } if ((config->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) { perror("peer_run could not create socket"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY ); myaddr.sin_port = htons(config->myport); if (bind(config->sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) { perror("peer_run could not bind socket"); exit(-1); } spiffy_init(config->identity, (struct sockaddr *) &myaddr, sizeof(myaddr)); timeout.tv_sec = 1; while (1) { int nfds; FD_SET(STDIN_FILENO, &readfds); FD_SET(config->sock, &readfds); nfds = select(config->sock + 1, &readfds, NULL, NULL, &timeout); if (nfds > 0) { if (FD_ISSET(config->sock, &readfds)) process_inbound_udp(config->sock, config); if (FD_ISSET(STDIN_FILENO, &readfds)) process_user_input(STDIN_FILENO, userbuf, handle_user_input, config); } // check timeout if (timeout.tv_sec == 0) { for (i = 0; i < BT_MAX_PEERS; i++) { for (j = 0; j < BT_CHUNK_SIZE; j++) { if (TTL[i][j] != -1) TTL[i][j]++; // set the DATA timeout to 2 if (TTL[i][j] == 2) { dprintf(STDOUT_FILENO, "retransmitting on timeout %d\n", j + 1); lastSent[i] = j; // lastACKed should be the same as j sendData(*getAddr(config, i), config); dup_ack[i] = 0; break; } } if (GETTTL[i] != -1) GETTTL[i]++; // set the GET timeout to 5 if (GETTTL[i] == 5) { if (++numGetMisses[i] > 3) { numGetMisses[i] = 0; numDataMisses[i] = -1; jobs[i] = -1; dup_ack[i] = 0; GETTTL[i] = -1; windowSize[i] = 0; congestionState[i] = -1; if ((work_queue = process_get(chunkf, outf)) == NULL ) { perror("I/O error"); exit(-1); } GETTTL[i] = -1; broadcast_query(work_queue, config); } else { dprintf(STDOUT_FILENO, "resending GET\n"); if ((work_queue = process_get(chunkf, outf)) == NULL ) { perror("I/O error"); exit(-1); } sendGetSW(config->sock, *getAddr(config, i)); } } if (numDataMisses[i] != -1) numDataMisses[i]++; if (numDataMisses[i] > 15) { numDataMisses[i] = -1; jobs[i] = -1; dup_ack[i] = 0; GETTTL[i] = -1; windowSize[i] = 0; congestionState[i] = -1; numGetMisses[i] = 0; resetCCRTT(); if ((work_queue = process_get(chunkf, outf)) == NULL ) { perror("I/O error"); exit(-1); } broadcast_query(work_queue, config); } } timeout.tv_sec = 1; } } }
void syscall_ipc_send(cpu_int_state_t *state) { // Extract arguments uint32_t pid = (uint32_t) state->state.rdi; uint16_t flags = (uint32_t) state->state.rbx; uint32_t length = (uint32_t) state->state.rcx; // Check if process exists process_t *process_target = (pid == process_current->pid) ? process_current : process_get(pid); if (0 == process_target) SYSCALL_RETURN_ERROR(1); // Check handler if (0 == process_target->message_handler) SYSCALL_RETURN_ERROR(2); // Check buffer size if (length > thread_current->ipc_buffer_sz[IPC_BUFFER_SEND]) SYSCALL_RETURN_ERROR(3); // Spawn handler thread thread_t *handler = thread_spawn( process_target, process_target->message_handler); // Set thread role ipc_role_ctx_t *role_ctx = (ipc_role_ctx_t *) heap_alloc(sizeof(ipc_role_ctx_t)); role_ctx->flags = flags; role_ctx->sender_process = process_current->pid; role_ctx->sender_thread = thread_current->tid; handler->role = THREAD_ROLE_IPC_RECEIVER; handler->role_ctx = role_ctx; // Move buffer to handler thread if (length > 0) ipc_buffer_move( thread_current, IPC_BUFFER_SEND, handler, IPC_BUFFER_RECV, process_target); // Write header to registers ipc_message_header( IPC_BUFFER_RECV, length, flags, process_current->pid, handler->tid, &handler->state); // Freeze the invoking thread, if a response is expected if (0 == (flags & IPC_FLAG_IGNORE_RESPONSE)) thread_freeze(thread_current); // Switch to handler thread thread_switch(handler, state); }
void syscall_ipc_respond(cpu_int_state_t *state) { // Check thread role if (THREAD_ROLE_IPC_RECEIVER != thread_current->role) SYSCALL_RETURN_ERROR(1); // Extract arguments uint16_t flags = (uint16_t) state->state.rbx; uint32_t length = (uint32_t) state->state.rcx; // Check length if (length > thread_current->ipc_buffer_sz[IPC_BUFFER_SEND]) SYSCALL_RETURN_ERROR(2); // Extract info from role ctx ipc_role_ctx_t *role_ctx = (ipc_role_ctx_t *) thread_current->role_ctx; uint32_t sender_pid = role_ctx->sender_process; uint32_t sender_tid = role_ctx->sender_thread; // Sender process still exists? process_t *sender_process = process_get(sender_pid); if (0 == sender_process) { thread_stop(process_current, thread_current); thread_switch(scheduler_next(), state); return; } // Sender thread still exists? thread_t *sender_thread = thread_get(sender_process, sender_tid); if (0 == sender_thread) { thread_stop(process_current, thread_current); thread_switch(scheduler_next(), state); return; } // Response ignored? if (0 != (role_ctx->flags & IPC_FLAG_IGNORE_RESPONSE)) { thread_stop(process_current, thread_current); thread_switch(sender_thread, state); return; } // Move buffer to sender thread (if length > 0) if (length > 0) ipc_buffer_move( thread_current, IPC_BUFFER_SEND, sender_thread, IPC_BUFFER_RECV, sender_process); // Write header to registers ipc_message_header( IPC_BUFFER_RECV, length, flags, process_current->pid, sender_thread->tid, &sender_thread->state); // Thaw thread thread_thaw(sender_thread, 0); // Stop current thread and switch to sender thread_stop(process_current, thread_current); thread_switch(sender_thread, state); }
TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd, char *env) { FILE *stream = NULL; int fno, type_len, read, mode; STARTUPINFO startup; PROCESS_INFORMATION process; SECURITY_ATTRIBUTES security; HANDLE in, out; DWORD dwCreateFlags = 0; BOOL res; process_pair *proc; char *cmd; int i; char *ptype = (char *)type; HANDLE thread_token = NULL; HANDLE token_user = NULL; BOOL asuser = TRUE; if (!type) { return NULL; } /*The following two checks can be removed once we drop XP support */ type_len = (int)strlen(type); if (type_len <1 || type_len > 2) { return NULL; } for (i=0; i < type_len; i++) { if (!(*ptype == 'r' || *ptype == 'w' || *ptype == 'b' || *ptype == 't')) { return NULL; } ptype++; } security.nLength = sizeof(SECURITY_ATTRIBUTES); security.bInheritHandle = TRUE; security.lpSecurityDescriptor = NULL; if (!type_len || !CreatePipe(&in, &out, &security, 2048L)) { return NULL; } memset(&startup, 0, sizeof(STARTUPINFO)); memset(&process, 0, sizeof(PROCESS_INFORMATION)); startup.cb = sizeof(STARTUPINFO); startup.dwFlags = STARTF_USESTDHANDLES; startup.hStdError = GetStdHandle(STD_ERROR_HANDLE); read = (type[0] == 'r') ? TRUE : FALSE; mode = ((type_len == 2) && (type[1] == 'b')) ? O_BINARY : O_TEXT; if (read) { in = dupHandle(in, FALSE); startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE); startup.hStdOutput = out; } else { out = dupHandle(out, FALSE); startup.hStdInput = in; startup.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); } dwCreateFlags = NORMAL_PRIORITY_CLASS; if (strcmp(sapi_module.name, "cli") != 0) { dwCreateFlags |= CREATE_NO_WINDOW; } /* Get a token with the impersonated user. */ if(OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &thread_token)) { DuplicateTokenEx(thread_token, MAXIMUM_ALLOWED, &security, SecurityImpersonation, TokenPrimary, &token_user); } else { DWORD err = GetLastError(); if (err == ERROR_NO_TOKEN) { asuser = FALSE; } } cmd = (char*)malloc(strlen(command)+strlen(TWG(comspec))+sizeof(" /c ")+2); if (!cmd) { return NULL; } sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command); if (asuser) { res = CreateProcessAsUser(token_user, NULL, cmd, &security, &security, security.bInheritHandle, dwCreateFlags, env, cwd, &startup, &process); CloseHandle(token_user); } else { res = CreateProcess(NULL, cmd, &security, &security, security.bInheritHandle, dwCreateFlags, env, cwd, &startup, &process); } free(cmd); if (!res) { return NULL; } CloseHandle(process.hThread); proc = process_get(NULL); if (read) { fno = _open_osfhandle((tsrm_intptr_t)in, _O_RDONLY | mode); CloseHandle(out); } else { fno = _open_osfhandle((tsrm_intptr_t)out, _O_WRONLY | mode); CloseHandle(in); } stream = _fdopen(fno, type); proc->prochnd = process.hProcess; proc->stream = stream; return stream; }
void process_requests(int server_sock) { int retval = 0; request *current, *trailer; if (pending_requests) { get_request(server_sock); #ifdef ORIGINAL_BEHAVIOR pending_requests = 0; #endif } current = request_ready; while (current) { time(¤t_time); retval = 1; /* emulate "success" in case we don't have to flush */ if (current->buffer_end && /* there is data in the buffer */ current->status < TIMED_OUT) { retval = req_flush(current); /* * retval can be -2=error, -1=blocked, or bytes left */ if (retval == -2) { /* error */ current->status = DEAD; retval = 0; } else if (retval >= 0) { /* notice the >= which is different from below? Here, we may just be flushing headers. We don't want to return 0 because we are not DONE or DEAD */ retval = 1; } } if (retval == 1) { switch (current->status) { case READ_HEADER: case ONE_CR: case ONE_LF: case TWO_CR: retval = read_header(current); break; case BODY_READ: retval = read_body(current); break; case BODY_WRITE: retval = write_body(current); break; case WRITE: retval = process_get(current); break; case PIPE_READ: retval = read_from_pipe(current); break; case PIPE_WRITE: retval = write_from_pipe(current); break; case IOSHUFFLE: #ifdef HAVE_SENDFILE retval = io_shuffle_sendfile(current); #else retval = io_shuffle(current); #endif break; case DONE: /* a non-status that will terminate the request */ retval = req_flush(current); /* * retval can be -2=error, -1=blocked, or bytes left */ if (retval == -2) { /* error */ current->status = DEAD; retval = 0; } else if (retval > 0) { retval = 1; } break; case TIMED_OUT: case DEAD: retval = 0; current->buffer_end = 0; SQUASH_KA(current); break; default: retval = 0; fprintf(stderr, "Unknown status (%d), " "closing!\n", current->status); current->status = DEAD; break; } } if (sigterm_flag) { SQUASH_KA(current); } /* we put this here instead of after the switch so that * if we are on the last request, and get_request is successful, * current->next is valid! */ if (pending_requests) get_request(server_sock); switch (retval) { case -1: /* request blocked */ trailer = current; current = current->next; block_request(trailer); break; case 0: /* request complete */ current->time_last = current_time; trailer = current; current = current->next; free_request(trailer); break; case 1: /* more to do */ current->time_last = current_time; current = current->next; break; default: log_error_doc(current); fprintf(stderr, "Unknown retval in process.c - " "Status: %d, retval: %d\n", current->status, retval); current->status = DEAD; current = current->next; break; } } }
void process_requests(int server_s, struct soap *soap)/*by SeanHou*/ { /* :TODO:Monday, December 01, 2014 11:17:36 HKT:SeanHou: */ int OnvifEN = 0; int lookupindex = 0; char service_uri[100] = ""; memset((void*)&soap->peer, 0, sizeof(soap->peer)); soap->socket = SOAP_INVALID_SOCKET; soap->error = SOAP_OK; soap->errmode = 0; soap->keep_alive = 0; fprintf(stderr, "Warning:" \ "(==>%s).\n", __func__); /* :TODO:End--- */ int retval = 0; request *current, *trailer; if (pending_requests) { get_request(server_s); #ifdef ORIGINAL_BEHAVIOR pending_requests = 0; #endif } current = request_ready; while (current) { /* :TODO:Monday, December 01, 2014 11:18:42 HKT:SeanHou: juge is onvif */ OnvifEN = isonvif(current->client_stream, service_uri, &lookupindex); if(OnvifEN == 1) { fprintf(stderr, "[boa:onvif] Warning: is onvif line[%d]remote port[%d]h2ns[%d]remote ip[%s]\n", __LINE__, current->remote_port, htons(current->remote_port), current->remote_ip_addr); struct sockaddr_in onvif_client_addr; memset(&onvif_client_addr, 0, sizeof(onvif_client_addr)); onvif_client_addr.sin_family = AF_INET; onvif_client_addr.sin_port = htons(current->remote_port);//随机端口 onvif_client_addr.sin_addr.s_addr = inet_addr(current->remote_ip_addr);// soap->socket = current->fd; soap->peer = onvif_client_addr; if (soap_valid_socket(soap->socket)) { soap->ip = ntohl(soap->peer.sin_addr.s_addr); soap->port = (int)ntohs(soap->peer.sin_port); soap->keep_alive = (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) != 0); } g_onvif_buffer = (char *)soap_malloc(soap, sizeof(current->client_stream)); strcpy(g_onvif_buffer, current->client_stream);//mark soap_begin_recv(soap); if (soap_envelope_begin_in(soap)) { soap_send_fault(soap); } if (soap_recv_header(soap)) { soap_send_fault(soap); } if (soap_body_begin_in(soap)) { soap_send_fault(soap); } int errorCode = 0; if (errorCode = soap_serve_request(soap)) { fprintf(stderr, "[boa:onvif]soap_serve_request fail, errorCode %d \n", errorCode); soap_send_fault(soap); } memset(current->client_stream, 0, CLIENT_STREAM_SIZE ); soap_dealloc(soap, NULL); soap_destroy(soap); soap_end(soap); current->status = DONE; close(soap->socket); continue; } /* :TODO:End--- */ time(¤t_time); if (current->buffer_end && /* there is data in the buffer */ current->status != DEAD && current->status != DONE) { retval = req_flush(current); /* * retval can be -2=error, -1=blocked, or bytes left */ if (retval == -2) { /* error */ current->status = DEAD; retval = 0; } else if (retval >= 0) { /* notice the >= which is different from below? Here, we may just be flushing headers. We don't want to return 0 because we are not DONE or DEAD */ retval = 1; } } else { switch (current->status) { case READ_HEADER: case ONE_CR: case ONE_LF: case TWO_CR: retval = read_header(current); break; case BODY_READ: retval = read_body(current); break; case BODY_WRITE: retval = write_body(current); break; case WRITE: retval = process_get(current); break; case PIPE_READ: retval = read_from_pipe(current); break; case PIPE_WRITE: retval = write_from_pipe(current); break; case DONE: /* a non-status that will terminate the request */ retval = req_flush(current); /* * retval can be -2=error, -1=blocked, or bytes left */ if (retval == -2) { /* error */ current->status = DEAD; retval = 0; } else if (retval > 0) { retval = 1; } break; case DEAD: retval = 0; current->buffer_end = 0; SQUASH_KA(current); break; default: retval = 0; fprintf(stderr, "Unknown status (%d), " "closing!\n", current->status); current->status = DEAD; break; } } if (sigterm_flag) SQUASH_KA(current); /* we put this here instead of after the switch so that * if we are on the last request, and get_request is successful, * current->next is valid! */ if (pending_requests) get_request(server_s); switch (retval) { case -1: /* request blocked */ trailer = current; current = current->next; block_request(trailer); break; case 0: /* request complete */ current->time_last = current_time; trailer = current; current = current->next; free_request(&request_ready, trailer); break; case 1: /* more to do */ current->time_last = current_time; current = current->next; break; default: log_error_time(); fprintf(stderr, "Unknown retval in process.c - " "Status: %d, retval: %d\n", current->status, retval); current = current->next; break; } } }