/** * Create a read-only file backed by a chunk of memory. * * Creates a special read-only file that is backed by the given chunk of memory. * This is useful to pass data stored in memory to code that expects to be * operating on files, such as the module loader. The given memory area will * not be duplicated, and therefore it must remain in memory for the lifetime * of the handle. * * @note Files created with this function do not support being * memory-mapped. * * @param buf Pointer to memory area to use. * @param size Size of memory area. * * @return Pointer to handle to file (has FILE_ACCESS_READ set). */ object_handle_t *memory_file_create(const void *buf, size_t size) { memory_file_t *file; file_handle_t *handle; file = kmalloc(sizeof(*file), MM_BOOT); file->file.ops = &memory_file_ops; file->file.type = FILE_TYPE_REGULAR; file->data = buf; file->size = size; handle = file_handle_alloc(&file->file, FILE_ACCESS_READ, 0); return file_handle_create(handle); }
//A pointer to this function is passed into the socket_listen call //It will be called whenever an incoming connection is setup on the listening port file_handle_t telnet_connect_accept(u16_t local_port, u16_t *remote_address, u16_t remote_port) { #ifdef __DEBUG unsigned char* raddr = (unsigned char*) remote_address; printf("\nTelnet: Connect from %hhd.%hhd.%hhd.%hhd port %d", raddr[1],raddr[0],raddr[3],raddr[2], remote_port ); #endif if( telnet_handle != FILE_INVALID_HANDLE ) return FILE_INVALID_HANDLE; buffer_init( &telnet_send_buffer, send_buf, SEND_BUF_SIZE ); buffer_init( &telnet_receive_buffer, receive_buf, REC_BUF_SIZE ); telnet_handle = file_handle_create( &telnet_receive_buffer, &telnet_send_buffer ); optneg_mode = 0; //we will do the echoing from this end. //It's just easier than to try and do full option negotiation telnet_mode = 0 | MODE_ECHO; file_putchar(TELNET_IAC, telnet_handle); file_putchar(TELNET_WILL, telnet_handle); file_putchar(TELNET_ECHO, telnet_handle); file_putchar(TELNET_IAC, telnet_handle); file_putchar(TELNET_WILL, telnet_handle); file_putchar(TELNET_SUPPRESS_GO_AHEAD, telnet_handle); file_puts("\r\nWeb platform telnet. Enter help for commands.\r\n>", telnet_handle); return telnet_handle; }
int runprogram(char *progname) { struct addrspace *as; struct vnode *v; vaddr_t entrypoint, stackptr; int result; /* Open the file. */ result = vfs_open(progname, O_RDONLY, 0, &v); if (result) { return result; } /* We should be a new process. */ KASSERT(proc_getas() == NULL); /* Create a new address space. */ as = as_create(); if (as == NULL) { vfs_close(v); return ENOMEM; } /* Switch to it and activate it. */ proc_setas(as); as_activate(); /* Load the executable. */ result = load_elf(v, &entrypoint); if (result) { /* p_addrspace will go away when curproc is destroyed */ vfs_close(v); return result; } /* Done with the file now. */ vfs_close(v); /* Define the user stack in the address space */ result = as_define_stack(as, &stackptr); if (result) { // p_addrspace will go away when curproc is destroyed return result; } struct vnode* v1; struct vnode* v2; struct vnode* v3; const char* name = "con:"; char *con_name = kstrdup(name); char *con_name2 = kstrdup(name); char *con_name3 = kstrdup(name); /*char con_name[5]; char con_name2[5]; char con_name3[5]; strcpy(con_name,name); strcpy(con_name2,name); strcpy(con_name3,name); */ result = vfs_open(con_name, O_RDONLY, 0664, &v1); if(result) { return result; } result = vfs_open(con_name2, O_WRONLY, 0664, &v2); if(result) { return result; } result = vfs_open(con_name3, O_WRONLY, 0664, &v3); if(result) { return result; } kfree(con_name); kfree(con_name2); kfree(con_name3); struct file_handle* fh1 = file_handle_create(); fh1->file = v1; fh1->openflags = O_RDONLY; fh1->ref_count = 1; strcpy(fh1->file_name,"con:"); curproc->t_file_table[0] = fh1; struct file_handle* fh2 = file_handle_create(); fh2->file = v2; fh2->openflags = O_WRONLY; fh2->ref_count = 1; strcpy(fh2->file_name, "con:"); curproc->t_file_table[1] = fh2; struct file_handle* fh3 = file_handle_create(); fh3->file = v3; fh3->openflags = O_WRONLY; fh3->ref_count = 1; strcpy(fh3->file_name, "con:"); curproc->t_file_table[2] = fh3; /* Warp to user mode. */ //kprintf("[run program] releasing semaphore \n"); enter_new_process(0 /*argc*/, 0 /*userspace addr of argv*/, NULL /*userspace addr of environment*/, stackptr, entrypoint); /* enter_new_process does not return. */ panic("enter_new_process returned\n"); return EINVAL; }