home *** CD-ROM | disk | FTP | other *** search
- /*
- * @(#)file.c 2.3 92/01/10
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ptrace.h>
- #include <sys/file.h>
- #include <sys/dirent.h>
- #include <sys/stat.h>
- #include <ustat.h>
- #include <sys/vfs.h>
- #include <sys/time.h>
- #include <sys/asynch.h>
-
- #include "defs.h"
-
- static Xlat openmodes[] = {
- {O_RDONLY, "RDONLY"}, /* read enable */
- {O_WRONLY, "WRONLY"}, /* write enable */
- {O_RDWR, "RDWR"}, /* read/write enable */
- {_FNDELAY, "NDELAY"}, /* non blocking I/O (4.2 style) */
- {_FAPPEND, "APPEND"}, /* append (writes guaranteed at the end) */
- {_FMARK, "MARK"}, /* internal; mark during gc() */
- {_FDEFER, "DEFER"}, /* internal; defer for next gc pass */
- {_FASYNC, "ASYNC"}, /* signal pgrp when data ready */
- {_FSHLOCK, "SHLOCK"}, /* BSD flock() shared lock present */
- {_FEXLOCK, "EXLOCK"}, /* BSD flock() exclusive lock present */
- {_FCREAT, "CREAT"}, /* open with file create */
- {_FTRUNC, "TRUNC"}, /* open with truncation */
- {_FEXCL, "EXCL"}, /* error on open if file exists */
- {_FNBIO, "NBIO"}, /* non blocking I/O (sys5 style) */
- {_FSYNC, "SYNC"}, /* do all writes synchronously */
- {_FNONBLOCK, "NONBLOCK"}, /* non blocking I/O (POSIX style) */
- {_FNOCTTY, "NOCTTY"}, /* don't assign a ctty on this open */
- {0, NULL},
- };
-
- int
- sys_open(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- /* flags */
- printxval(openmodes, tcp->u_args[1] & O_ACCMODE, "O_???");
- (void)addflags(openmodes, tcp->u_args[1] & ~O_ACCMODE);
- /* mode */
- tprintf(", %o", tcp->u_args[2]);
- }
- return 0;
- }
-
- int
- sys_creat(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", %o", tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_umask(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%o", tcp->u_args[0]);
- }
- return RVAL_OCT;
- }
-
- static Xlat whence[] = {
- 0, "SET",
- 1, "CUR",
- 2, "END",
- 0, NULL,
- };
-
- int
- sys_lseek(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u, %u, ", tcp->u_args[0], tcp->u_args[1]);
- printxval(whence, tcp->u_args[2], "L_???");
- }
- return RVAL_UDEC;
- }
-
- int
- sys_truncate(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", %u", tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_ftruncate(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u, %u", tcp->u_args[0], tcp->u_args[1]);
- }
- return 0;
- }
-
- static Xlat accessmodes[] = {
- {R_OK, "R_OK"}, /* read permission */
- {W_OK, "W_OK"}, /* write permission */
- {X_OK, "X_OK"}, /* execute permission */
- {F_OK, "F_OK"}, /* search permission */
- 0, NULL,
- };
-
- int
- sys_access(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- if (tcp->u_args[1] == F_OK)
- tprintf("F_OK");
- else if (!printflags(accessmodes, tcp->u_args[1]))
- tprintf("???_OK");
- }
- return 0;
- }
-
- static Xlat statmodes[] = {
- S_IFDIR, "DIR", /* directory */
- S_IFCHR, "CHR", /* character special */
- S_IFBLK, "BLK", /* block special */
- S_IFREG, "REG", /* regular */
- S_IFLNK, "LNK", /* symbolic link */
- S_IFSOCK, "SOCK", /* socket */
- S_IFIFO, "FIFO", /* fifo */
- 0, NULL,
- };
-
- /* several stats */
- static void
- printstat(pid, addr)
- {
- struct stat statbuf;
-
- if (umove(pid, addr, sizeof statbuf, (char *)&statbuf) < 0) {
- tprintf("(struct stat *)%#x", addr);
- return;
- }
- if (!verbose) {
- tprintf("[");
- printxval(statmodes, (int)(statbuf.st_mode & S_IFMT), "S_???");
- if (statbuf.st_mode & S_ISUID)
- tprintf(" SUID");
- if (statbuf.st_mode & S_ISGID)
- tprintf(" SGID");
- tprintf(" ino %u nlnks %d ...]",
- statbuf.st_ino, statbuf.st_nlink);
- } else {
- tprintf(
- "[dev %x,%x ino %u mode %o nlnks %d uid %u gid %u rdev %x,%x size %u atime %u mtime %u ctime %u blksiz %u blks %u]",
- major(statbuf.st_dev), minor(statbuf.st_dev),
- statbuf.st_mode, statbuf.st_ino, statbuf.st_nlink,
- statbuf.st_uid, statbuf.st_gid,
- major(statbuf.st_rdev), minor(statbuf.st_rdev),
- statbuf.st_size,
- statbuf.st_atime, statbuf.st_mtime, statbuf.st_ctime,
- statbuf.st_blksize, statbuf.st_blocks);
- }
- }
-
- int
- sys_stat(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- } else {
- if (syserror(tcp))
- tprintf("%#x", tcp->u_args[1]);
- else
- printstat(tcp->pid, tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_fstat(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u, ", tcp->u_args[0]);
- } else {
- if (syserror(tcp))
- tprintf("%#x", tcp->u_args[1]);
- else
- printstat(tcp->pid, tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_lstat(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- } else {
- if (syserror(tcp))
- tprintf("%#x", tcp->u_args[1]);
- else
- printstat(tcp->pid, tcp->u_args[1]);
- }
- return 0;
- }
-
- static void
- printstatfs(pid, addr)
- {
- struct statfs statbuf;
-
- if (umove(pid, addr, sizeof statbuf, (char *)&statbuf) < 0) {
- tprintf("[...]");
- return;
- }
- tprintf(
- "[type %x bsize %u blocks %u free %u files %u ffree %u fsid %u %u]",
- statbuf.f_type, statbuf.f_bsize, statbuf.f_blocks,
- statbuf.f_bfree, statbuf.f_files, statbuf.f_ffree,
- statbuf.f_fsid.val[0], statbuf.f_fsid.val[1]);
- }
-
- int
- sys_statfs(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- } else {
- if (syserror(tcp))
- tprintf("%#x", tcp->u_args[1]);
- else
- printstatfs(tcp->pid, tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_fstatfs(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u, ", tcp->u_args[0]);
- } else {
- if (syserror(tcp))
- tprintf("%#x", tcp->u_args[1]);
- else
- printstatfs(tcp->pid, tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_ustat(tcp)
- struct tcb *tcp;
- {
- struct ustat statbuf;
-
- if (entering(tcp)) {
- tprintf("dev %u %u, ",
- major(tcp->u_args[0]), minor(tcp->u_args[0]));
- } else {
- if (syserror(tcp)) {
- tprintf("%#x", tcp->u_args[1]);
- return 0;
- }
- if (umove(tcp->pid, tcp->u_args[1],
- sizeof statbuf, (char *)&statbuf) < 0) {
- tprintf("%#x", tcp->u_args[1]);
- return 0;
- }
- tprintf("[tfree %u tinode %u fname %.*s fpack %.*s]",
- statbuf.f_tfree, statbuf.f_tinode,
- sizeof(statbuf.f_fname), statbuf.f_fname,
- sizeof(statbuf.f_fpack), statbuf.f_fpack);
- }
- return 0;
- }
-
- /* directory */
- int
- sys_chdir(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- }
- return 0;
- }
-
- int
- sys_mkdir(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", %o", tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_rmdir(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- }
- return 0;
- }
-
- int
- sys_fchdir(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u", tcp->u_args[0]);
- }
- return 0;
- }
-
- int
- sys_chroot(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- }
- return 0;
- }
-
- int
- sys_fchroot(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u", tcp->u_args[0]);
- }
- return 0;
- }
-
- int
- sys_link(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- (void)printstr(tcp->pid, tcp->u_args[1], -1);
- }
- return 0;
- }
-
- int
- sys_unlink(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- }
- return 0;
- }
-
- int
- sys_symlink(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- (void)printstr(tcp->pid, tcp->u_args[1], -1);
- }
- return 0;
- }
-
- int
- sys_readlink(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- } else {
- if (syserror(tcp))
- tprintf("%#x", tcp->u_args[1]);
- else
- (void)printstr(tcp->pid, tcp->u_args[1], tcp->u_rval);
- tprintf(", %u", tcp->u_args[2]);
- }
- return 0;
- }
-
- int
- sys_rename(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- (void)printstr(tcp->pid, tcp->u_args[1], -1);
- }
- return 0;
- }
-
- int
- sys_chown(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", %u, %u", tcp->u_args[1], tcp->u_args[2]);
- }
- return 0;
- }
-
- int
- sys_fchown(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u, %u, %u",
- tcp->u_args[0], tcp->u_args[1], tcp->u_args[2]);
- }
- return 0;
- }
-
- int
- sys_chmod(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", %o", tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_fchmod(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u, %o", tcp->u_args[0], tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_utimes(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", ");
- printtv(tcp->pid, tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_mknod(tcp)
- struct tcb *tcp;
- {
- int mode = tcp->u_args[1];
-
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", %o(%s)", mode,
- mode==S_IFCHR?"CHR":(
- mode==S_IFBLK?"BLK":(
- mode==S_IFREG?"REG":(
- mode==S_IFIFO?"FIFO":""
- )
- )
- )
- );
- tprintf(", dev %x %x",
- major(tcp->u_args[2]), minor(tcp->u_args[2]));
- }
- return 0;
- }
-
- int
- sys_mkfifo(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- (void)printstr(tcp->pid, tcp->u_args[0], -1);
- tprintf(", %o", tcp->u_args[1]);
- }
- return 0;
- }
-
- int
- sys_fsync(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- tprintf("%u", tcp->u_args[0]);
- }
- return 0;
- }
-
- int
- sys_getdents(tcp)
- struct tcb *tcp;
- {
- int i, len;
- char *buf;
-
- if (entering(tcp)) {
- tprintf("%u, ", tcp->u_args[0]);
- } else {
- if (syserror(tcp)) {
- tprintf("%#x, %u", tcp->u_args[1], tcp->u_args[2]);
- return 0;
- }
- len = tcp->u_rval;
- if ((buf = malloc((u_int)len)) == NULL ||
- umove(tcp->pid, tcp->u_args[1], len, buf) < 0) {
- tprintf("%#x, %u", tcp->u_args[1], tcp->u_args[2]);
- } else {
- int dents = 0;
- for (i = 0; i < len;) {
- struct dirent *d = (struct dirent *)&buf[i];
- if (verbose) tprintf(
- "{off:%u,fno:%u,rl:%u,nl:%u\"%.*s\"}",
- d->d_off, d->d_fileno,
- d->d_reclen, d->d_namlen,
- d->d_namlen, d->d_name);
- i += d->d_reclen;
- dents++;
- }
- if (!verbose)
- tprintf("{Total: %u dents}", dents);
- tprintf(", %u", tcp->u_args[2]);
- }
- if (buf) free((char *)buf);
- }
- return 0;
- }
-
- int
- sys_aioread(tcp)
- struct tcb *tcp;
- {
- struct aio_result_t res;
-
- if (entering(tcp)) {
- tprintf("%u, ", tcp->u_args[0]);
- } else {
- if (syserror(tcp))
- tprintf("%#x", tcp->u_args[1]);
- else
- (void)printstr(tcp->pid, tcp->u_args[1], tcp->u_args[2]);
- tprintf(", %u, %u, ", tcp->u_args[2], tcp->u_args[3]);
- printxval(whence, tcp->u_args[4], "L_???");
- if (syserror(tcp) || tcp->u_args[5] == 0 ||
- umove(tcp->pid, tcp->u_args[5],
- sizeof res, (char *)&res) < 0)
- tprintf(", (struct aio_result_t *)%#x",
- tcp->u_args[5]);
- else
- tprintf(", {%d,%d}",
- res.aio_return, res.aio_errno);
- }
- return 0;
- }
-
- int
- sys_aiowrite(tcp)
- struct tcb *tcp;
- {
- struct aio_result_t res;
-
- if (entering(tcp)) {
- tprintf("%u, ", tcp->u_args[0]);
- (void)printstr(tcp->pid, tcp->u_args[1], tcp->u_args[2]);
- tprintf(", %u, %u, ", tcp->u_args[2], tcp->u_args[3]);
- printxval(whence, tcp->u_args[4], "L_???");
- } else {
- if (syserror(tcp) || tcp->u_args[5] == 0 ||
- umove(tcp->pid, tcp->u_args[5],
- sizeof res, (char *)&res) < 0)
- tprintf(", (struct aio_result_t *)%#x",
- tcp->u_args[5]);
- else
- tprintf(", {%d,%d}",
- res.aio_return, res.aio_errno);
- }
- return 0;
- }
-
- int
- sys_aiowait(tcp)
- struct tcb *tcp;
- {
- if (entering(tcp)) {
- printtv(tcp->pid, tcp->u_args[0]);
- }
- return 0;
- }
-
- int
- sys_aiocancel(tcp)
- struct tcb *tcp;
- {
- struct aio_result_t res;
-
- if (exiting(tcp)) {
- if (syserror(tcp) || tcp->u_args[0] == 0 ||
- umove(tcp->pid, tcp->u_args[0],
- sizeof res, (char *)&res) < 0)
- tprintf(", (struct aio_result_t *)%#x",
- tcp->u_args[0]);
- else
- tprintf(", {%d,%d}",
- res.aio_return, res.aio_errno);
- }
- return 0;
- }
-