diff --git a/sys/include/sys/descrip.h b/sys/include/sys/descrip.h index bb4b9a9..a012794 100644 --- a/sys/include/sys/descrip.h +++ b/sys/include/sys/descrip.h @@ -84,10 +84,21 @@ #define O_NDELAY O_NONBLOCK /* compat */ #define FPOSIXSHM O_NOFOLLOW +#define O_DIRECTORY 0x00020000 /* Fail if not directory */ +#define O_EXEC 0x00040000 /* Open for execute only */ + #define FCNTLFLAGS (FAPPEND|FASYNC|FFSYNC|FNONBLOCK|FPOSIXSHM|O_DIRECT) +/* #define FFLAGS(oflags) ((oflags) + 1) #define OFLAGS(fflags) ((fflags) - 1) +*/ + +/* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */ +#define FFLAGS(oflags) ((oflags) & O_EXEC ? (oflags) : (oflags) + 1) +#define OFLAGS(fflags) ((fflags) & O_EXEC ? (fflags) : (fflags) - 1) + +#define FMASK (FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK|O_DIRECT|FEXEC) struct fileOps; struct file; @@ -180,6 +191,8 @@ int ioctl(struct thread *, struct ioctl_args *); int getfd(struct thread *td, struct file **fp, int fd); +int_kern_openat(struct thread *,int, char *,int int); + #endif /*** diff --git a/sys/kernel/vfs_calls.c b/sys/kernel/vfs_calls.c index 067390f..a183143 100644 --- a/sys/kernel/vfs_calls.c +++ b/sys/kernel/vfs_calls.c @@ -36,36 +36,13 @@ #include int sys_open(struct thread *td, struct sys_open_args *args) { - int error = 0x0; - int fd = 0x0; - struct file *nfp = 0x0; - error = falloc(td, &nfp, &fd); + return(kern_openat(td, AT_FDCWD, args->path, args->flags, args->mode)); - if (error) { - td->td_retval[0] = -1; - return (error); - } - - - nfp->fd = fopen(args->path, "rb"); - - if (nfp->fd == 0x0) { - fdestroy(td, nfp, fd); - - td->td_retval[0] = -1; - error = -1; - } - else { - td->td_retval[0] = fd; - } - - //kprintf("sO: 0x%X:%s:", args->mode, args->path, td->td_retval[0]); - - return (error); } int sys_openat(struct thread *td, struct sys_openat_args *args) { + int error = 0x0; int fd = 0x0; struct file *nfp = 0x0; @@ -421,3 +398,52 @@ thr->td_retval[0] = 2; return (-1); } + +int kern_openat(struct thread *thr, int fd, char *path, int flags, int mode) { + int error = 0x0; + int fd = 0x0; + struct file *nfp = 0x0; + + /* + * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags + * may be specified. + */ + if (flags & O_EXEC) { + if (flags & O_ACCMODE) + return (EINVAL); + } + else if ((flags & O_ACCMODE) == O_ACCMODE) { + return (EINVAL); + } + else { + flags = FFLAGS(flags); + } + + error = falloc(td, &nfp, &fd); + + if (error) { + thr->td_retval[0] = -1; + return (error); + } + + nfp->f_flag = flags & FMASK; + + + nfp->fd = fopen(args->path, "rb"); + + + if (nfp->fd == 0x0) { + fdestroy(td, nfp, fd); + + td->td_retval[0] = -1; + error = -1; + } + else { + td->td_retval[0] = fd; + } + + //kprintf("sO: 0x%X:%s:", args->mode, args->path, td->td_retval[0]); + + return (error); + +}