diff --git a/lib/libc/sys/fcntl.2 b/lib/libc/sys/fcntl.2 --- a/lib/libc/sys/fcntl.2 +++ b/lib/libc/sys/fcntl.2 @@ -28,7 +28,7 @@ .\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94 .\" $FreeBSD$ .\" -.Dd January 6, 2021 +.Dd December 7, 2021 .Dt FCNTL 2 .Os .Sh NAME @@ -53,7 +53,7 @@ .Fa cmd , .Fn fcntl can take an additional third argument -.Fa "int arg" . +.Fa "long arg" . .Bl -tag -width F_DUP2FD_CLOEXEC .It Dv F_DUPFD Return a new descriptor as follows: @@ -190,6 +190,19 @@ .Xr mount 2 or unionfs). This is a hack not intended to be used outside of libc. +.It Dv F_KINFO +Fills a +.Vt struct kinfo_file +for the file referenced by the specified file descriptor. +The +.Fa arg +argument should point to the storage for +.Vt struct kinfo_file . +The +.Va kf_structsize +member of the passed structure must be initialized with the sizeof of +.Vt struct kinfo_file , +to allow for the interface versioning and evolution. .El .Pp The flags for the diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -107,6 +107,9 @@ static int closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, bool holdleaders, bool audit); +static void export_file_to_kinfo(struct file *fp, int fd, + cap_rights_t *rightsp, struct kinfo_file *kif, + struct filedesc *fdp, int flags); static int fd_first_free(struct filedesc *fdp, int low, int size); static void fdgrowtable(struct filedesc *fdp, int nfd); static void fdgrowtable_exp(struct filedesc *fdp, int nfd); @@ -477,7 +480,8 @@ struct proc *p; struct vnode *vp; struct mount *mp; - int error, flg, seals, tmp; + struct kinfo_file *kif; + int error, flg, kif_sz, seals, tmp; uint64_t bsize; off_t foffset; @@ -852,6 +856,41 @@ fdrop(fp, td); break; + case F_KINFO: +#ifdef CAPABILITY_MODE + if (IN_CAPABILITY_MODE(td)) { + error = ECAPMODE; + break; + } +#endif + error = copyin((void *)arg, &kif_sz, sizeof(kif_sz)); + if (error != 0) + break; + if (kif_sz != sizeof(*kif)) { + error = EINVAL; + break; + } + kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK | M_ZERO); + FILEDESC_SLOCK(fdp); + error = fget_cap_locked(fdp, fd, &cap_fcntl_rights, &fp, NULL); + if (error == 0 && fhold(fp)) { + export_file_to_kinfo(fp, fd, NULL, kif, fdp, 0); + FILEDESC_SUNLOCK(fdp); + fdrop(fp, td); + if ((kif->kf_status & KF_ATTR_VALID) != 0) { + kif->kf_structsize = sizeof(*kif); + error = copyout(kif, (void *)arg, sizeof(*kif)); + } else { + error = EBADF; + } + } else { + FILEDESC_SUNLOCK(fdp); + if (error == 0) + error = EBADF; + } + free(kif, M_TEMP); + break; + default: error = EINVAL; break; diff --git a/sys/sys/fcntl.h b/sys/sys/fcntl.h --- a/sys/sys/fcntl.h +++ b/sys/sys/fcntl.h @@ -270,6 +270,7 @@ #define F_ADD_SEALS 19 #define F_GET_SEALS 20 #define F_ISUNIONSTACK 21 /* Kludge for libc, don't use it. */ +#define F_KINFO 22 /* Return kinfo_file for this fd */ /* Seals (F_ADD_SEALS, F_GET_SEALS). */ #define F_SEAL_SEAL 0x0001 /* Prevent adding sealings */