Index: sys/kern/kern_sig.c =================================================================== --- sys/kern/kern_sig.c +++ sys/kern/kern_sig.c @@ -3300,6 +3300,87 @@ CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A", "Process corefile name format string"); +/* + * If the core format has a %I in it, then we need to check + * for existing corefiles before defining a name. + * To do this we iterate over 0..num_cores to find a + * non-existing core file name to use. If all core files are + * already used we choose the oldest one. + */ +static int +corefile_open_last(struct thread *td, char *name, int indexpos, + struct vnode **vpp) +{ + struct vnode *oldvp, *nextvp; + struct nameidata nd; + struct stat sb; + int error, i, flags, oflags, cmode; + time_t lastsec; + + nextvp = oldvp = NULL; + cmode = S_IRUSR | S_IWUSR; + oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | + (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); + + for (i = 0; i < num_cores; i++) { + struct vnode *vp; + + flags = O_CREAT | FWRITE | O_NOFOLLOW; + name[indexpos] = '0' + i; + + NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); + error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, + NULL); + if (error != 0) + break; + + vp = nd.ni_vp; + NDFREE(&nd, NDF_ONLY_PNBUF); + if ((flags & O_CREAT) == O_CREAT) { + nextvp = vp; + break; + } + + error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td); + if (error != 0) { + VOP_UNLOCK(vp, 0); + vn_close(vp, FWRITE, td->td_ucred, td); + break; + } + + if (oldvp == NULL || lastsec >= sb.st_mtim.tv_sec) { + if (oldvp != NULL) { + VOP_UNLOCK(oldvp, 0); + vn_close(oldvp, FWRITE, td->td_ucred, td); + } + oldvp = vp; + lastsec = sb.st_mtim.tv_sec; + } else { + VOP_UNLOCK(vp, 0); + vn_close(vp, FWRITE, td->td_ucred, td); + } + } + + if (oldvp != NULL) { + if (nextvp == NULL) { + nextvp = oldvp; + } else { + VOP_UNLOCK(oldvp, 0); + vn_close(oldvp, FWRITE, td->td_ucred, td); + } + } + if (error != 0) { + if (nextvp != NULL) { + VOP_UNLOCK(nextvp, 0); + vn_close(nextvp, FWRITE, td->td_ucred, td); + } + } else { + *vpp = nextvp; + } + + return (error); +} + /* * corefile_open(comm, uid, pid, td, compress, vpp, namep) * Expand the name described in corefilename, using name, uid, and pid @@ -3316,11 +3397,10 @@ corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, int compress, struct vnode **vpp, char **namep) { - struct nameidata nd; struct sbuf sb; const char *format; char *hostname, *name; - int indexpos, i, error, cmode, flags, oflags; + int indexpos, error, i; hostname = NULL; format = corefilename; @@ -3386,48 +3466,39 @@ sbuf_finish(&sb); sbuf_delete(&sb); - cmode = S_IRUSR | S_IWUSR; - oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | - (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); - - /* - * If the core format has a %I in it, then we need to check - * for existing corefiles before returning a name. - * To do this we iterate over 0..num_cores to find a - * non-existing core file name to use. - */ if (indexpos != -1) { - for (i = 0; i < num_cores; i++) { - flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW; - name[indexpos] = '0' + i; - NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); - error = vn_open_cred(&nd, &flags, cmode, oflags, - td->td_ucred, NULL); - if (error) { - if (error == EEXIST) - continue; - log(LOG_ERR, - "pid %d (%s), uid (%u): Path `%s' failed " - "on initial open test, error = %d\n", - pid, comm, uid, name, error); - } - goto out; + error = corefile_open_last(td, name, indexpos, vpp); + if (error != 0) { + log(LOG_ERR, + "pid %d (%s), uid (%u): Path `%s' failed " + "on initial open test, error = %d\n", + pid, comm, uid, name, error); + } + } else { + struct nameidata nd; + int cmode, flags, oflags; + + cmode = S_IRUSR | S_IWUSR; + oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE | + (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0); + flags = O_CREAT | FWRITE | O_NOFOLLOW; + + NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); + error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, + NULL); + if (error == 0) { + *vpp = nd.ni_vp; + NDFREE(&nd, NDF_ONLY_PNBUF); } } - flags = O_CREAT | FWRITE | O_NOFOLLOW; - NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); - error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL); -out: - if (error) { + if (error != 0) { #ifdef AUDIT audit_proc_coredump(td, name, error); #endif free(name, M_TEMP); return (error); } - NDFREE(&nd, NDF_ONLY_PNBUF); - *vpp = nd.ni_vp; *namep = name; return (0); }