int do_open(const char *path, int mode) { int fd; struct file *f; struct inode *ip; if ((mode & O_CREATE) &&!(ip = icreate(path, T_FILE, 0, 0))) return -1; else if (!(ip = namei(path))) return -1; if (ip->type == T_DIR && mode != O_RDONLY) { iput(ip); return -1; } if (!(f = filealloc()) || (fd = fdalloc()) < 0) { if (f) fileclose(f); iput(ip); return -1; } f->type = FD_INODE; f->ip = ip; f->off = 0; f->readable = !(mode & O_WRONLY); f->writable = (mode & O_WRONLY) || (mode & O_RDWR); fs_current->ofile[fd] = f; return fd; }
int pipealloc(struct file **f0, struct file **f1) { struct pipe *p; p = 0; *f0 = *f1 = 0; if ((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0) { goto bad; } if ((p = (struct pipe *)kalloc()) == 0) { goto bad; } p->readopen = 1; p->writeopen = 1; p->nwrite = 0; p->nread = 0; initlock(&p->lock, "pipe"); (*f0)->type = FD_PIPE; (*f0)->readable = 1; (*f0)->writable = 0; (*f0)->pipe = p; (*f1)->type = FD_PIPE; (*f1)->readable = 0; (*f1)->writable = 1; (*f1)->pipe = p; return 0; //PAGEBREAK: 20 bad: if (p) { kfree((char *)p); } if (*f0) { fileclose(*f0); } if (*f1) { fileclose(*f1); } return -1; }
int sys_getcwd() { struct inode *ip = idup(current_proc->cwd); struct file * f = filealloc(); f->in = ip; f->offset = 0; f->type = FD_INODE; f->rw = FD_READABLE; int fd = fdalloc(f); if(fd > 0) return fd; return -1; }
int pipealloc(struct file **f0, struct file **f1) { struct pipe *p; p = 0; *f0 = *f1 = 0; if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0) goto bad; if((p = (struct pipe*)kalloc(PAGE)) == 0) goto bad; p->readopen = 1; p->writeopen = 1; p->writep = 0; p->readp = 0; initlock(&p->lock, "pipe"); (*f0)->type = FD_PIPE; (*f0)->readable = 1; (*f0)->writable = 0; (*f0)->pipe = p; (*f1)->type = FD_PIPE; (*f1)->readable = 0; (*f1)->writable = 1; (*f1)->pipe = p; return 0; bad: if(p) kfree((char*)p, PAGE); if(*f0){ (*f0)->type = FD_NONE; fileclose(*f0); } if(*f1){ (*f1)->type = FD_NONE; fileclose(*f1); } return -1; }
struct file* kernel_open(char* path, int omode){ int fd; struct file *f; struct inode *ip; if(omode & O_CREATE){ begin_trans(); ip = kernel_create(path, T_FILE, 0, 0); commit_trans(); if(ip == 0) return 0; } else { // cprintf("kernel_open - path is %s\n",path); if((ip = namei(path)) == 0) return 0; //cprintf("kernel_open - path is %s passed namei\n",path); ilock(ip); if(ip->type == T_DIR && omode != O_RDONLY){ iunlockput(ip); return 0; } } // cprintf("kernel_open - before filealloc path %s\n",path); if((f = filealloc()) == 0 || (fd = kernel_fdalloc(f)) < 0){ if(f) fileclose(f); iunlockput(ip); return 0; } iunlock(ip); f->type = FD_INODE; f->ip = ip; f->off = 0; f->readable = !(omode & O_WRONLY); f->writable = (omode & O_WRONLY) || (omode & O_RDWR); return f; //return fd; //return 0; }
/* creates the first process */ void userinit(){ struct proc *p; /* printf("entered userinit\n"); */ /* allocate memory for stdin wait queue */ _stdin = (struct read_proc*)kmalloc(sizeof(struct read_proc)); memset1((char *)_stdin,0,sizeof(struct read_proc)); memset1((char *)ptable.proc,0,sizeof(ptable)); p=alloc_proc(); initproc=p; fgproc=initproc; if(!(p->pml4_t=load_kern_vm())){ /* panic code goes here*/ printf("Kernel Panic: Unable To Create Initproc\n"); } //inituvm(p->pml4_t,binary_initcode_start,binary_initcode_size); p->size=FRAME_SIZE; /* clear the trapframe. this is only done for fist process */ memset1((char *)p->tf,0,sizeof(struct trapframe)); /* p->tf->cs=0x8/\* SEG_KCS() *\/; /\* kernel code segment *\/ */ /* p->tf->ss=0x10/\* SEG_KDS() *\/; /\* kernel data segment *\/ */ p->tf->cs=0x1B; /* user code segment */ p->tf->ss=0x23; /* user data segment */ p->tf->rflags=FL_IF; /* enable interrupts */ /* rsp will be sey in exec */ /* p->tf->rsp=FRAME_SIZE; /\* where does the user stack star from in the proc vm *\/ */ /* rip will be set in exec */ /* p->tf->rip=0; /\* begining of initcode.S *\/ */ strcpy(p->name,"initcode"); /* p->name has a size of 32 bytes */ p->state=RUNNABLE; /* printf("calling exec\n"); */ /* call exec */ proc=p; /* set cwd of initproc to root '/' */ strcpy(proc->cwd,"/"); char *a="bin/hello"; char *argv[3]; argv[0]=a;argv[1]="os/sbunix";argv[2]=NULL; char *envp[7]; envp[0]="PATH=/bin"; envp[1]="envp1"; envp[2]="envp2"; envp[3]="envp3"; envp[4]="envp4"; envp[5]="envp5"; envp[6]=NULL; cli(); exec("bin/hello",argv,envp); //p->tf->cs=(SEG_); /* ltr(0x2Bu); */ /* ltr 0x2B with RPL of 3 for user??? */ ltr(0x2Bu); /* set WP bit in cr0 */ set_wp_bit(); /* clear ftable */ memset1((char *)ftable.file,0,sizeof(struct file)*NFILE); /* clear file descriptor table of proc,i.e initproc */ int fd=0; for(fd=0;fd<NOFILE;fd++){ proc->ofile[fd]=NULL; } struct file *fp=NULL; /* give initproc STDIN, STDOUT,STDERR*/ proc->ofile[STDIN]=filealloc(); if(proc->ofile[STDIN]==NULL){ printf("Kernel Panic, STDIN fiel struct not allocd for initproc\n"); /* panic. kill proc */ } fp=proc->ofile[STDIN]; fp->type=FD_STDIN; fp->readable=1; /* mark readable */ fp->writable=0; /* mark not writable */ proc->ofile[STDOUT]=filealloc(); if(proc->ofile[STDOUT]==NULL){ printf("Kernel Panic: STDOUT file struct not allocd for initproc\n"); /* panic. kill proc */ } fp=proc->ofile[STDOUT]; fp->type=FD_STDOUT; fp->readable=0; /* mark NOT readable */ fp->writable=1; /* mark writable */ proc->ofile[STDERR]=filealloc(); if(proc->ofile[STDERR]==NULL){ printf("Kernel Panic: STDERR fiel struct not allocd for initproc\n"); /* panic. kill proc */ } fp=proc->ofile[STDERR]; fp->type=FD_STDERR; fp->readable=0; /* mark NOT readable */ fp->writable=1; /* mark writable */ /* initialize sleep_head and sleep_tail to NULL */ init_sleep_queue(); init_waitpid_queue(); init_stdin_queue(); /* initialize inodes */ /* init_inodes(); */ /* printf writes to <1MB mem region. Now user page tables are loaded. We cannot access <1MB since we did not map that region into user process < 1MB VM aread */ /* printf("calling scheduler\n"); */ scheduler(); }