/* * call up the connection server and get a translation */ static int nettrans(char *addr, char *naddr, int na, char *file, int nf) { int i, fd; char buf[Maxpath]; char netdir[NETPATHLEN]; char *p, *p2; long n; /* * parse, get network directory */ p = strchr(addr, '!'); if (p == 0) { set_errstr("bad dial string: %s", addr); return -1; } if (*addr != '/') { strlcpy(netdir, "/net", sizeof(netdir)); } else { for (p2 = p; *p2 != '/'; p2--) ; i = p2 - addr; if (i == 0 || i >= sizeof(netdir)) { set_errstr("bad dial string: %s", addr); return -1; } strlcpy(netdir, addr, i + 1); addr = p2 + 1; } /* * ask the connection server */ snprintf(buf, sizeof(buf), "%s/cs", netdir); fd = sysopen(buf, O_RDWR); if (fd < 0) return identtrans(netdir, addr, naddr, na, file, nf); if (syswrite(fd, addr, strlen(addr)) < 0) { sysclose(fd); return -1; } sysseek(fd, 0, 0); n = sysread(fd, buf, sizeof(buf) - 1); sysclose(fd); if (n <= 0) return -1; buf[n] = 0; /* * parse the reply */ p = strchr(buf, ' '); if (p == 0) return -1; *p++ = 0; strlcpy(naddr, p, na); strlcpy(file, buf, nf); return 0; }
void syspread(void) { union { vlong v; ulong u[2]; } o; o.u[0] = getmem_w(reg.r[REGSP]+16); o.u[1] = getmem_w(reg.r[REGSP]+20); sysread(o.v); }
/* * announce a network service. */ int kannounce(char *addr, char *dir, size_t dirlen) { int ctl, n, m; char buf[NETPATHLEN]; char buf2[Maxpath]; char netdir[NETPATHLEN]; char naddr[Maxpath]; char *cp; /* * translate the address */ if (nettrans(addr, naddr, sizeof(naddr), netdir, sizeof(netdir)) < 0) return -1; /* * get a control channel */ ctl = sysopen(netdir, O_RDWR); if (ctl < 0) return -1; cp = strrchr(netdir, '/'); *cp = 0; /* * find out which line we have */ n = snprintf(buf, sizeof(buf), "%.*s/", sizeof buf, netdir); m = sysread(ctl, &buf[n], sizeof(buf) - n - 1); if (m <= 0) { sysclose(ctl); return -1; } buf[n + m] = 0; /* * make the call */ n = snprintf(buf2, sizeof buf2, "announce %s", naddr); if (syswrite(ctl, buf2, n) != n) { sysclose(ctl); return -1; } /* * return directory etc. */ if (dir) strlcpy(dir, buf, dirlen); return ctl; }
static int magic(const char* path, int* ux) { int fd; int r; int n; int m; int oerrno; #if CONVERT unsigned char buf[512]; #else unsigned char buf[2]; #endif oerrno = errno; if ((fd = sysopen(path, O_RDONLY, 0)) >= 0) { #if CONVERT if (ux) n = sizeof(buf); else #endif n = 2; r = (m = sysread(fd, buf, n)) >= 2 && (buf[1] == 0x5a && (buf[0] == 0x4c || buf[0] == 0x4d) || ux && buf[0] == '#' && buf[1] == '!' && (*ux = 1) && !(ux = 0)) ? 0 : -1; sysclose(fd); if (ux) { if (r) oerrno = ENOEXEC; else if (m > 61 && (n = buf[60] | (buf[61]<<8) + 92) < (m - 1)) *ux = (buf[n] | (buf[n+1]<<8)) == 3; else *ux = 0; } } else if (!ux) r = -1; else if (errno == ENOENT) { oerrno = errno; r = -1; } else { r = 0; *ux = 0; } errno = oerrno; return r; }
void syscallc(int user_eax, int arg1, char * arg2, int arg3) { switch (user_eax) { case SYSREAD_NUM: user_eax = sysread(arg1, arg2, arg3); break; case SYSWRITE_NUM: user_eax = syswrite(arg1, arg2, arg3); break; case SYSEXIT_NUM: user_eax = sysexit(arg1); break; default: printf("invalid syscall I.D.: %d\n", user_eax); break; } }
/* * listen for an incoming call */ int klisten(char *dir, char *newdir, size_t newdirlen) { int ctl, n, m; char buf[NETPATHLEN + 1]; char *cp; /* * open listen, wait for a call */ snprintf(buf, sizeof buf, "%s/listen", dir); ctl = sysopen(buf, O_RDWR); if (ctl < 0) return -1; /* * find out which line we have */ strlcpy(buf, dir, sizeof(buf)); cp = strrchr(buf, '/'); *++cp = 0; n = cp - buf; m = sysread(ctl, cp, sizeof(buf) - n - 1); if (m <= 0) { sysclose(ctl); return -1; } buf[n + m] = 0; /* * return directory etc. */ if (newdir) strlcpy(newdir, buf, newdirlen); return ctl; }
void syspread(void) { sysread(getmem_v(reg.r[13]+16)); }
void sys_read(void) { sysread(-1LL); }
static void syscall_handler (struct intr_frame* frame) { // -------- System Call Handler Overview -------- // Get system call number // switch statement using system call number // collect arguments for system call function if necessary // call system call function // set frame->eax to return value if necessary // ---------------------------------------------- uintptr_t* kpaddr_sp = (uintptr_t*) frame->esp; int syscall_num = -1; if(check_uptr(kpaddr_sp)) syscall_num = next_value(&kpaddr_sp); else sysexit(-1); switch(syscall_num) { case SYS_HALT: { // Terminates Pintos shutdown_power_off(); } break; case SYS_EXIT: { uintptr_t status = -1; if(check_uptr(kpaddr_sp)) status = next_value(&kpaddr_sp); sysexit(status); } break; case SYS_EXEC: //pid_t exec (const char *file); { const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); else sysexec(frame, file); } break; case SYS_WAIT: //int wait (pid_t); { uintptr_t childid = -1; if(check_uptr(kpaddr_sp)) childid = next_value(&kpaddr_sp); else sysexit(childid); int retval = process_wait((tid_t) childid); frame->eax = retval; } break; case SYS_CREATE: //bool create (const char *file, unsigned initial_size); { const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); uintptr_t size = 0; if(check_uptr(kpaddr_sp)) size = next_value(&kpaddr_sp); else sysexit(-1); syscreate(frame, file, size); } break; case SYS_REMOVE: //bool remove (const char *file); { const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); sysremove(frame, file); } break; case SYS_OPEN: { //int open (const char *file); const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); sysopen(frame, file); } break; case SYS_FILESIZE: { //int filesize (int fd); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); sysfilesize(frame, fd); } break; case SYS_READ: { //int read (int fd, void *buffer, unsigned length); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); unsigned length = 0; if (check_uptr(kpaddr_sp)) length = (unsigned) next_value(&kpaddr_sp); else sysexit(-1); sysread(frame, fd, (void*) file, length); } break; case SYS_WRITE: { //int write (int fd, const void *buffer, unsigned length); uintptr_t fd = 0; if(check_uptr(kpaddr_sp)) fd = next_value(&kpaddr_sp); else sysexit(-1); const char* file = next_charptr(&kpaddr_sp); if(file == NULL) sysexit(-1); unsigned len = strlen(file); if(!check_buffer(file, len)) sysexit(-1); uintptr_t length = 0; if(check_uptr(kpaddr_sp)) length = next_value(&kpaddr_sp); else sysexit(-1); if(fd == CONSOLEWRITE) // Write to Console { while(length > 0) { if(length > MAX_SIZE) { putbuf (file, MAX_SIZE); file += MAX_SIZE; length -= MAX_SIZE; } else { putbuf (file, length); length = 0; } } } else { syswrite(frame, fd, file, length); } } break; case SYS_SEEK: { //void seek (int fd, unsigned position); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); unsigned position = 0; if (check_uptr(kpaddr_sp)) position = (unsigned) next_value(&kpaddr_sp); else sysexit(-1); sysseek(fd, position); } break; case SYS_TELL: { //unsigned tell (int fd); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); systell(frame, fd); } break; case SYS_CLOSE: { //void close (int fd); int fd = 0; if (check_uptr(kpaddr_sp)) fd = (int) next_value(&kpaddr_sp); else sysexit(-1); sysclose(fd); } break; default: { printf("Unrecognized System Call\n"); sysexit(-1); } break; } }
static int readSyscall(int* args) { return sysread(args[0], (void*)args[1], (size_t)args[2]); }
static int call(char *clone, char *dest, DS * ds) { int fd, cfd, n, retval; char *name, *data, *err, *p; name = kmalloc(Maxpath, KMALLOC_WAIT); data = kmalloc(Maxpath, KMALLOC_WAIT); err = kmalloc(ERRMAX, KMALLOC_WAIT); cfd = sysopen(clone, O_RDWR); if (cfd < 0) { kerrstr(err, ERRMAX); set_errstr("%s (%s)", err, clone); retval = -1; goto out; } /* get directory name */ n = sysread(cfd, name, Maxpath - 1); if (n < 0) { kerrstr(err, ERRMAX); sysclose(cfd); set_errstr("read %s: %s", clone, err); retval = -1; goto out; } name[n] = 0; for (p = name; *p == ' '; p++) ; snprintf(name, Maxpath, "%ld", strtoul(p, 0, 0)); p = strrchr(clone, '/'); *p = 0; if (ds->dir) snprintf(ds->dir, NETPATHLEN, "%s/%s", clone, name); snprintf(data, Maxpath, "%s/%s/data", clone, name); /* connect */ if (ds->local) snprintf(name, Maxpath, "connect %s %s", dest, ds->local); else snprintf(name, Maxpath, "connect %s", dest); if (syswrite(cfd, name, strlen(name)) < 0) { err[0] = 0; kerrstr(err, ERRMAX); sysclose(cfd); set_errstr("%s (%s)", err, name); retval = -1; goto out; } /* open data connection */ fd = sysopen(data, O_RDWR); if (fd < 0) { err[0] = 0; kerrstr(err, ERRMAX); set_errstr("%s (%s)", err, data); sysclose(cfd); retval = -1; goto out; } if (ds->cfdp) *ds->cfdp = cfd; else sysclose(cfd); retval = fd; out: kfree(name); kfree(data); kfree(err); return retval; }
static int csdial(DS * ds) { int n, fd, rv = -1; char *p, *buf, *clone, *err, *besterr; buf = kmalloc(Maxstring, KMALLOC_WAIT); clone = kmalloc(Maxpath, KMALLOC_WAIT); err = kmalloc(ERRMAX, KMALLOC_WAIT); besterr = kmalloc(ERRMAX, KMALLOC_WAIT); /* * open connection server */ snprintf(buf, Maxstring, "%s/cs", ds->netdir); fd = sysopen(buf, O_RDWR); if (fd < 0) { /* no connection server, don't translate */ snprintf(clone, Maxpath, "%s/%s/clone", ds->netdir, ds->proto); rv = call(clone, ds->rem, ds); goto out; } /* * ask connection server to translate */ snprintf(buf, Maxstring, "%s!%s", ds->proto, ds->rem); if (syswrite(fd, buf, strlen(buf)) < 0) { kerrstr(err, ERRMAX); sysclose(fd); set_errstr("%s (%s)", err, buf); goto out; } /* * loop through each address from the connection server till * we get one that works. */ *besterr = 0; strlcpy(err, errno_to_string(ECONNRESET), ERRMAX); sysseek(fd, 0, 0); while ((n = sysread(fd, buf, Maxstring - 1)) > 0) { buf[n] = 0; p = strchr(buf, ' '); if (p == 0) continue; *p++ = 0; rv = call(buf, p, ds); if (rv >= 0) break; err[0] = 0; kerrstr(err, ERRMAX); if (strstr(err, "does not exist") == 0) memmove(besterr, err, ERRMAX); } sysclose(fd); if (rv < 0 && *besterr) kerrstr(besterr, ERRMAX); else kerrstr(err, ERRMAX); out: kfree(buf); kfree(clone); kfree(err); kfree(besterr); return rv; }