summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sys/conf/files3
-rw-r--r--sys/conf/options1
-rw-r--r--sys/kern/imgact_elf.c172
-rw-r--r--sys/kern/kern_gzio.c406
-rw-r--r--sys/kern/kern_sig.c130
-rw-r--r--sys/net/zlib.h7
-rw-r--r--sys/net/zutil.h231
-rw-r--r--sys/sys/eventhandler.h13
-rw-r--r--sys/sys/imgact.h2
-rw-r--r--sys/sys/imgact_aout.h3
-rw-r--r--sys/sys/imgact_elf.h2
-rw-r--r--sys/sys/sysent.h2
12 files changed, 943 insertions, 29 deletions
diff --git a/sys/conf/files b/sys/conf/files
index 4e6a586..e723775 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -2053,6 +2053,7 @@ kern/kern_exec.c standard
kern/kern_exit.c standard
kern/kern_fail.c standard
kern/kern_fork.c standard
+kern/kern_gzio.c optional gzio
kern/kern_idle.c standard
kern/kern_intr.c standard
kern/kern_jail.c standard
@@ -2344,7 +2345,7 @@ net/slcompress.c optional netgraph_vjc | sppp | \
net/vnet.c optional vimage
net/zlib.c optional crypto | geom_uzip | ipsec | \
mxge | netgraph_deflate | \
- ddb_ctf
+ ddb_ctf | zlib
net80211/ieee80211.c optional wlan
net80211/ieee80211_acl.c optional wlan wlan_acl
net80211/ieee80211_action.c optional wlan
diff --git a/sys/conf/options b/sys/conf/options
index 84bfbd1..7869add 100644
--- a/sys/conf/options
+++ b/sys/conf/options
@@ -71,6 +71,7 @@ COMPAT_FREEBSD5 opt_compat.h
COMPAT_FREEBSD6 opt_compat.h
COMPAT_FREEBSD7 opt_compat.h
COMPILING_LINT opt_global.h
+COMPRESS_USER_CORES opt_core.h
CY_PCI_FASTINTR
DEADLKRES opt_watchdog.h
DIRECTIO
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index 56cd66f..fa6ddaf 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -32,6 +32,7 @@
__FBSDID("$FreeBSD$");
#include "opt_compat.h"
+#include "opt_core.h"
#include <sys/param.h>
#include <sys/exec.h>
@@ -58,6 +59,10 @@ __FBSDID("$FreeBSD$");
#include <sys/sysctl.h>
#include <sys/sysent.h>
#include <sys/vnode.h>
+#include <sys/syslog.h>
+#include <sys/eventhandler.h>
+
+#include <net/zlib.h>
#include <vm/vm.h>
#include <vm/vm_kern.h>
@@ -95,6 +100,12 @@ static boolean_t __elfN(check_note)(struct image_params *imgp,
SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
"");
+#ifdef COMPRESS_USER_CORES
+static int compress_core(gzFile, char *, char *, unsigned int,
+ struct thread * td);
+#define CORE_BUF_SIZE (16 * 1024)
+#endif
+
int __elfN(fallback_brand) = -1;
SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0,
@@ -1003,16 +1014,38 @@ static void cb_put_phdr(vm_map_entry_t, void *);
static void cb_size_segment(vm_map_entry_t, void *);
static void each_writable_segment(struct thread *, segment_callback, void *);
static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
- int, void *, size_t);
+ int, void *, size_t, gzFile);
static void __elfN(puthdr)(struct thread *, void *, size_t *, int);
static void __elfN(putnote)(void *, size_t *, const char *, int,
const void *, size_t);
+#ifdef COMPRESS_USER_CORES
+extern int compress_user_cores;
+extern int compress_user_cores_gzlevel;
+#endif
+
+static int
+core_output(struct vnode *vp, void *base, size_t len, off_t offset,
+ struct ucred *active_cred, struct ucred *file_cred,
+ struct thread *td, char *core_buf, gzFile gzfile) {
+
+ int error;
+ if (gzfile) {
+#ifdef COMPRESS_USER_CORES
+ error = compress_core(gzfile, base, core_buf, len, td);
+#else
+ panic("shouldn't be here");
+#endif
+ } else {
+ error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
+ UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
+ NULL, td);
+ }
+ return (error);
+}
+
int
-__elfN(coredump)(td, vp, limit)
- struct thread *td;
- struct vnode *vp;
- off_t limit;
+__elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
{
struct ucred *cred = td->td_ucred;
int error = 0;
@@ -1020,6 +1053,37 @@ __elfN(coredump)(td, vp, limit)
void *hdr;
size_t hdrsize;
+ gzFile gzfile = Z_NULL;
+ char *core_buf = NULL;
+#ifdef COMPRESS_USER_CORES
+ char gzopen_flags[8];
+ char *p;
+ int doing_compress = flags & IMGACT_CORE_COMPRESS;
+#endif
+
+ hdr = NULL;
+
+#ifdef COMPRESS_USER_CORES
+ if (doing_compress) {
+ p = gzopen_flags;
+ *p++ = 'w';
+ if (compress_user_cores_gzlevel >= 0 &&
+ compress_user_cores_gzlevel <= 9)
+ *p++ = '0' + compress_user_cores_gzlevel;
+ *p = 0;
+ gzfile = gz_open("", gzopen_flags, vp);
+ if (gzfile == Z_NULL) {
+ error = EFAULT;
+ goto done;
+ }
+ core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
+ if (!core_buf) {
+ error = ENOMEM;
+ goto done;
+ }
+ }
+#endif
+
/* Size the program segments. */
seginfo.count = 0;
seginfo.size = 0;
@@ -1044,7 +1108,8 @@ __elfN(coredump)(td, vp, limit)
if (hdr == NULL) {
return (EINVAL);
}
- error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize);
+ error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
+ gzfile);
/* Write the contents of all of the writable segments. */
if (error == 0) {
@@ -1055,17 +1120,28 @@ __elfN(coredump)(td, vp, limit)
php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
offset = hdrsize;
for (i = 0; i < seginfo.count; i++) {
- error = vn_rdwr_inchunks(UIO_WRITE, vp,
- (caddr_t)(uintptr_t)php->p_vaddr,
- php->p_filesz, offset, UIO_USERSPACE,
- IO_UNIT | IO_DIRECT, cred, NOCRED, NULL,
- curthread);
+ error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
+ php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
if (error != 0)
break;
offset += php->p_filesz;
php++;
}
}
+ if (error) {
+ log(LOG_WARNING,
+ "Failed to write core file for process %s (error %d)\n",
+ curproc->p_comm, error);
+ }
+
+#ifdef COMPRESS_USER_CORES
+done:
+#endif
+ if (core_buf)
+ free(core_buf, M_TEMP);
+ if (gzfile)
+ gzclose(gzfile);
+
free(hdr, M_TEMP);
return (error);
@@ -1189,13 +1265,14 @@ each_writable_segment(td, func, closure)
* the page boundary.
*/
static int
-__elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize)
+__elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize, gzfile)
struct thread *td;
struct vnode *vp;
struct ucred *cred;
int numsegs;
size_t hdrsize;
void *hdr;
+ gzFile gzfile;
{
size_t off;
@@ -1204,10 +1281,26 @@ __elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize)
off = 0;
__elfN(puthdr)(td, hdr, &off, numsegs);
- /* Write it to the core file. */
- return (vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
- UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NOCRED, NULL,
- td));
+ if (!gzfile) {
+ /* Write it to the core file. */
+ return (vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
+ UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NOCRED, NULL,
+ td));
+ } else {
+#ifdef COMPRESS_USER_CORES
+ if (gzwrite(gzfile, hdr, hdrsize) != hdrsize) {
+ log(LOG_WARNING,
+ "Failed to compress core file header for process"
+ " %s.\n", curproc->p_comm);
+ return (EFAULT);
+ }
+ else {
+ return (0);
+ }
+#else
+ panic("shouldn't be here");
+#endif
+ }
}
#if defined(COMPAT_IA32) && __ELF_WORD_SIZE == 32
@@ -1476,3 +1569,50 @@ static struct execsw __elfN(execsw) = {
__XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
};
EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
+
+#ifdef COMPRESS_USER_CORES
+/*
+ * Compress and write out a core segment for a user process.
+ *
+ * 'inbuf' is the starting address of a VM segment in the process' address
+ * space that is to be compressed and written out to the core file. 'dest_buf'
+ * is a buffer in the kernel's address space. The segment is copied from
+ * 'inbuf' to 'dest_buf' first before being processed by the compression
+ * routine gzwrite(). This copying is necessary because the content of the VM
+ * segment may change between the compression pass and the crc-computation pass
+ * in gzwrite(). This is because realtime threads may preempt the UNIX kernel.
+ */
+static int
+compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
+ struct thread *td)
+{
+ int len_compressed;
+ int error = 0;
+ unsigned int chunk_len;
+
+ while (len) {
+ chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
+ copyin(inbuf, dest_buf, chunk_len);
+ len_compressed = gzwrite(file, dest_buf, chunk_len);
+
+ EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
+
+ if ((unsigned int)len_compressed != chunk_len) {
+ log(LOG_WARNING,
+ "compress_core: length mismatch (0x%x returned, "
+ "0x%x expected)\n", len_compressed, chunk_len);
+ EVENTHANDLER_INVOKE(app_coredump_error, td,
+ "compress_core: length mismatch %x -> %x",
+ chunk_len, len_compressed);
+ error = EFAULT;
+ break;
+ }
+ inbuf += chunk_len;
+ len -= chunk_len;
+ if (ticks - PCPU_GET(switchticks) >= hogticks)
+ uio_yield();
+ }
+
+ return (error);
+}
+#endif /* COMPRESS_USER_CORES */
diff --git a/sys/kern/kern_gzio.c b/sys/kern/kern_gzio.c
new file mode 100644
index 0000000..1f9c387
--- /dev/null
+++ b/sys/kern/kern_gzio.c
@@ -0,0 +1,406 @@
+/*
+ * $Id: kern_gzio.c,v 1.6 2008-10-18 22:54:45 lbazinet Exp $
+ *
+ * core_gzip.c -- gzip routines used in compressing user process cores
+ *
+ * This file is derived from src/lib/libz/gzio.c in FreeBSD.
+ */
+
+/* gzio.c -- IO on .gz files
+ * Copyright (C) 1995-1998 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ *
+ */
+
+/* @(#) $FreeBSD$ */
+
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/malloc.h>
+#include <sys/vnode.h>
+#include <sys/syslog.h>
+#include <sys/endian.h>
+#include <net/zutil.h>
+#include <sys/libkern.h>
+
+#include <sys/vnode.h>
+#include <sys/mount.h>
+
+#define GZ_HEADER_LEN 10
+
+#ifndef Z_BUFSIZE
+# ifdef MAXSEG_64K
+# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
+# else
+# define Z_BUFSIZE 16384
+# endif
+#endif
+#ifndef Z_PRINTF_BUFSIZE
+# define Z_PRINTF_BUFSIZE 4096
+#endif
+
+#define ALLOC(size) malloc(size, M_TEMP, M_WAITOK | M_ZERO)
+#define TRYFREE(p) {if (p) free(p, M_TEMP);}
+
+static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
+
+/* gzip flag byte */
+#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
+#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
+#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
+#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
+#define COMMENT 0x10 /* bit 4 set: file comment present */
+#define RESERVED 0xE0 /* bits 5..7: reserved */
+
+typedef struct gz_stream {
+ z_stream stream;
+ int z_err; /* error code for last stream operation */
+ int z_eof; /* set if end of input file */
+ struct vnode *file; /* vnode pointer of .gz file */
+ Byte *inbuf; /* input buffer */
+ Byte *outbuf; /* output buffer */
+ uLong crc; /* crc32 of uncompressed data */
+ char *msg; /* error message */
+ char *path; /* path name for debugging only */
+ int transparent; /* 1 if input file is not a .gz file */
+ char mode; /* 'w' or 'r' */
+ long startpos; /* start of compressed data in file (header skipped) */
+ off_t outoff; /* current offset in output file */
+ int flags;
+} gz_stream;
+
+
+local int do_flush OF((gzFile file, int flush));
+local int destroy OF((gz_stream *s));
+local void putU32 OF((gz_stream *file, uint32_t x));
+local void *gz_alloc OF((void *notused, u_int items, u_int size));
+local void gz_free OF((void *notused, void *ptr));
+
+/* ===========================================================================
+ Opens a gzip (.gz) file for reading or writing. The mode parameter
+ is as in fopen ("rb" or "wb"). The file is given either by file descriptor
+ or path name (if fd == -1).
+ gz_open return NULL if the file could not be opened or if there was
+ insufficient memory to allocate the (de)compression state; errno
+ can be checked to distinguish the two cases (if errno is zero, the
+ zlib error is Z_MEM_ERROR).
+*/
+gzFile gz_open (path, mode, vp)
+ const char *path;
+ const char *mode;
+ struct vnode *vp;
+{
+ int err;
+ int level = Z_DEFAULT_COMPRESSION; /* compression level */
+ int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
+ const char *p = mode;
+ gz_stream *s;
+ char fmode[80]; /* copy of mode, without the compression level */
+ char *m = fmode;
+ int resid;
+ int error;
+ char buf[GZ_HEADER_LEN + 1];
+
+ if (!path || !mode) return Z_NULL;
+
+ s = (gz_stream *)ALLOC(sizeof(gz_stream));
+ if (!s) return Z_NULL;
+
+ s->stream.zalloc = (alloc_func)gz_alloc;
+ s->stream.zfree = (free_func)gz_free;
+ s->stream.opaque = (voidpf)0;
+ s->stream.next_in = s->inbuf = Z_NULL;
+ s->stream.next_out = s->outbuf = Z_NULL;
+ s->stream.avail_in = s->stream.avail_out = 0;
+ s->file = NULL;
+ s->z_err = Z_OK;
+ s->z_eof = 0;
+ s->crc = 0;
+ s->msg = NULL;
+ s->transparent = 0;
+ s->outoff = 0;
+ s->flags = 0;
+
+ s->path = (char*)ALLOC(strlen(path)+1);
+ if (s->path == NULL) {
+ return destroy(s), (gzFile)Z_NULL;
+ }
+ strcpy(s->path, path); /* do this early for debugging */
+
+ s->mode = '\0';
+ do {
+ if (*p == 'r') s->mode = 'r';
+ if (*p == 'w' || *p == 'a') s->mode = 'w';
+ if (*p >= '0' && *p <= '9') {
+ level = *p - '0';
+ } else if (*p == 'f') {
+ strategy = Z_FILTERED;
+ } else if (*p == 'h') {
+ strategy = Z_HUFFMAN_ONLY;
+ } else {
+ *m++ = *p; /* copy the mode */
+ }
+ } while (*p++ && m != fmode + sizeof(fmode));
+
+ if (s->mode != 'w') {
+ log(LOG_ERR, "gz_open: mode is not w (%c)\n", s->mode);
+ return destroy(s), (gzFile)Z_NULL;
+ }
+
+ err = deflateInit2(&(s->stream), level,
+ Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
+ /* windowBits is passed < 0 to suppress zlib header */
+
+ s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
+ if (err != Z_OK || s->outbuf == Z_NULL) {
+ return destroy(s), (gzFile)Z_NULL;
+ }
+
+ s->stream.avail_out = Z_BUFSIZE;
+ s->file = vp;
+
+ /* Write a very simple .gz header:
+ */
+ snprintf(buf, sizeof(buf), "%c%c%c%c%c%c%c%c%c%c", gz_magic[0],
+ gz_magic[1], Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/,
+ 0 /*xflags*/, OS_CODE);
+
+ if ((error = vn_rdwr(UIO_WRITE, s->file, buf, GZ_HEADER_LEN, s->outoff,
+ UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, curproc->p_ucred,
+ NOCRED, &resid, curthread))) {
+ s->outoff += GZ_HEADER_LEN - resid;
+ return destroy(s), (gzFile)Z_NULL;
+ }
+ s->outoff += GZ_HEADER_LEN;
+ s->startpos = 10L;
+
+ return (gzFile)s;
+}
+
+
+ /* ===========================================================================
+ * Cleanup then free the given gz_stream. Return a zlib error code.
+ Try freeing in the reverse order of allocations.
+ */
+local int destroy (s)
+ gz_stream *s;
+{
+ int err = Z_OK;
+
+ if (!s) return Z_STREAM_ERROR;
+
+ TRYFREE(s->msg);
+
+ if (s->stream.state != NULL) {
+ if (s->mode == 'w') {
+ err = deflateEnd(&(s->stream));
+ }
+ }
+ if (s->z_err < 0) err = s->z_err;
+
+ TRYFREE(s->inbuf);
+ TRYFREE(s->outbuf);
+ TRYFREE(s->path);
+ TRYFREE(s);
+ return err;
+}
+
+
+/* ===========================================================================
+ Writes the given number of uncompressed bytes into the compressed file.
+ gzwrite returns the number of bytes actually written (0 in case of error).
+*/
+int ZEXPORT gzwrite (file, buf, len)
+ gzFile file;
+ const voidp buf;
+ unsigned len;
+{
+ gz_stream *s = (gz_stream*)file;
+ off_t curoff;
+ size_t resid;
+ int error;
+ int vfslocked;
+
+ if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
+
+ s->stream.next_in = (Bytef*)buf;
+ s->stream.avail_in = len;
+
+ curoff = s->outoff;
+ while (s->stream.avail_in != 0) {
+
+ if (s->stream.avail_out == 0) {
+
+ s->stream.next_out = s->outbuf;
+ vfslocked = VFS_LOCK_GIANT(s->file->v_mount);
+ error = vn_rdwr_inchunks(UIO_WRITE, s->file, s->outbuf, Z_BUFSIZE,
+ curoff, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
+ curproc->p_ucred, NOCRED, &resid, curthread);
+ VFS_UNLOCK_GIANT(vfslocked);
+ if (error) {
+ log(LOG_ERR, "gzwrite: vn_rdwr return %d\n", error);
+ curoff += Z_BUFSIZE - resid;
+ s->z_err = Z_ERRNO;
+ break;
+ }
+ curoff += Z_BUFSIZE;
+ s->stream.avail_out = Z_BUFSIZE;
+ }
+ s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
+ if (s->z_err != Z_OK) {
+ log(LOG_ERR,
+ "gzwrite: deflate returned error %d\n", s->z_err);
+ break;
+ }
+ }
+
+ s->crc = ~crc32_raw(buf, len, ~s->crc);
+ s->outoff = curoff;
+
+ return (int)(len - s->stream.avail_in);
+}
+
+
+/* ===========================================================================
+ Flushes all pending output into the compressed file. The parameter
+ flush is as in the deflate() function.
+*/
+local int do_flush (file, flush)
+ gzFile file;
+ int flush;
+{
+ uInt len;
+ int done = 0;
+ gz_stream *s = (gz_stream*)file;
+ off_t curoff = s->outoff;
+ size_t resid;
+ int vfslocked = 0;
+ int error;
+
+ if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
+
+ if (s->stream.avail_in) {
+ log(LOG_WARNING, "do_flush: avail_in non-zero on entry\n");
+ }
+
+ s->stream.avail_in = 0; /* should be zero already anyway */
+
+ for (;;) {
+ len = Z_BUFSIZE - s->stream.avail_out;
+
+ if (len != 0) {
+ vfslocked = VFS_LOCK_GIANT(s->file->v_mount);
+ error = vn_rdwr_inchunks(UIO_WRITE, s->file, s->outbuf, len, curoff,
+ UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, curproc->p_ucred,
+ NOCRED, &resid, curthread);
+ VFS_UNLOCK_GIANT(vfslocked);
+ if (error) {
+ s->z_err = Z_ERRNO;
+ s->outoff = curoff + len - resid;
+ return Z_ERRNO;
+ }
+ s->stream.next_out = s->outbuf;
+ s->stream.avail_out = Z_BUFSIZE;
+ curoff += len;
+ }
+ if (done) break;
+ s->z_err = deflate(&(s->stream), flush);
+
+ /* Ignore the second of two consecutive flushes: */
+ if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
+
+ /* deflate has finished flushing only when it hasn't used up
+ * all the available space in the output buffer:
+ */
+ done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
+
+ if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
+ }
+ s->outoff = curoff;
+
+ return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
+}
+
+int ZEXPORT gzflush (file, flush)
+ gzFile file;
+ int flush;
+{
+ gz_stream *s = (gz_stream*)file;
+ int err = do_flush (file, flush);
+
+ if (err) return err;
+ return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
+}
+
+
+/* ===========================================================================
+ Outputs a long in LSB order to the given file
+*/
+local void putU32 (s, x)
+ gz_stream *s;
+ uint32_t x;
+{
+ uint32_t xx;
+ off_t curoff = s->outoff;
+ int resid;
+
+#if BYTE_ORDER == BIG_ENDIAN
+ xx = bswap32(x);
+#else
+ xx = x;
+#endif
+ vn_rdwr(UIO_WRITE, s->file, (caddr_t)&xx, sizeof(xx), curoff,
+ UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, curproc->p_ucred,
+ NOCRED, &resid, curthread);
+ s->outoff += sizeof(xx) - resid;
+}
+
+
+/* ===========================================================================
+ Flushes all pending output if necessary, closes the compressed file
+ and deallocates all the (de)compression state.
+*/
+int ZEXPORT gzclose (file)
+ gzFile file;
+{
+ int err;
+ gz_stream *s = (gz_stream*)file;
+
+ if (s == NULL) return Z_STREAM_ERROR;
+
+ if (s->mode == 'w') {
+ err = do_flush (file, Z_FINISH);
+ if (err != Z_OK) {
+ log(LOG_ERR, "gzclose: do_flush failed (err %d)\n", err);
+ return destroy((gz_stream*)file);
+ }
+#if 0
+ printf("gzclose: putting crc: %lld total: %lld\n",
+ (long long)s->crc, (long long)s->stream.total_in);
+ printf("sizeof uLong = %d\n", (int)sizeof(uLong));
+#endif
+ putU32 (s, s->crc);
+ putU32 (s, (uint32_t) s->stream.total_in);
+ }
+ return destroy((gz_stream*)file);
+}
+
+/*
+ * Space allocation and freeing routines for use by zlib routines when called
+ * from gzip modules.
+ */
+static void *
+gz_alloc(void *notused __unused, u_int items, u_int size)
+{
+ void *ptr;
+
+ MALLOC(ptr, void *, items * size, M_TEMP, M_NOWAIT | M_ZERO);
+ return ptr;
+}
+
+static void
+gz_free(void *opaque __unused, void *ptr)
+{
+ FREE(ptr, M_TEMP);
+}
+
diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c
index c0ec56f..98a121b 100644
--- a/sys/kern/kern_sig.c
+++ b/sys/kern/kern_sig.c
@@ -40,6 +40,7 @@ __FBSDID("$FreeBSD$");
#include "opt_compat.h"
#include "opt_kdtrace.h"
#include "opt_ktrace.h"
+#include "opt_core.h"
#include <sys/param.h>
#include <sys/systm.h>
@@ -49,6 +50,7 @@ __FBSDID("$FreeBSD$");
#include <sys/condvar.h>
#include <sys/event.h>
#include <sys/fcntl.h>
+#include <sys/imgact.h>
#include <sys/kernel.h>
#include <sys/ktr.h>
#include <sys/ktrace.h>
@@ -78,6 +80,8 @@ __FBSDID("$FreeBSD$");
#include <vm/vm_extern.h>
#include <vm/uma.h>
+#include <sys/jail.h>
+
#include <machine/cpu.h>
#include <security/audit/audit.h>
@@ -98,7 +102,7 @@ SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
static int coredump(struct thread *);
-static char *expand_name(const char *, uid_t, pid_t);
+static char *expand_name(const char *, uid_t, pid_t, struct thread *, int);
static int killpg1(struct thread *td, int sig, int pgid, int all,
ksiginfo_t *ksi);
static int issignal(struct thread *td, int stop_allowed);
@@ -2936,12 +2940,51 @@ childproc_exited(struct proc *p)
sigparent(p, reason, status);
}
+/*
+ * We only have 1 character for the core count in the format
+ * string, so the range will be 0-9
+ */
+#define MAX_NUM_CORES 10
+static int num_cores = 5;
+
+static int
+sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
+{
+ int error;
+ int new_val;
+
+ error = sysctl_handle_int(oidp, &new_val, 0, req);
+ if (error != 0 || req->newptr == NULL)
+ return (error);
+ if (new_val > MAX_NUM_CORES)
+ new_val = MAX_NUM_CORES;
+ if (new_val < 0)
+ new_val = 0;
+ num_cores = new_val;
+ return (0);
+}
+SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
+ 0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
+
+#if defined(COMPRESS_USER_CORES)
+int compress_user_cores = 1;
+SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
+ &compress_user_cores, 0, "");
+
+int compress_user_cores_gzlevel = -1; /* default level */
+SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
+ &compress_user_cores_gzlevel, -1, "user core gz compression level");
+
+#define GZ_SUFFIX ".gz"
+#define GZ_SUFFIX_LEN 3
+#endif
+
static char corefilename[MAXPATHLEN] = {"%N.core"};
SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
sizeof(corefilename), "process corefile name format string");
/*
- * expand_name(name, uid, pid)
+ * expand_name(name, uid, pid, td, compress)
* Expand the name described in corefilename, using name, uid, and pid.
* corefilename is a printf-like string, with three format specifiers:
* %N name of process ("name")
@@ -2952,20 +2995,21 @@ SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
* This is controlled by the sysctl variable kern.corefile (see above).
*/
static char *
-expand_name(name, uid, pid)
- const char *name;
- uid_t uid;
- pid_t pid;
+expand_name(const char *name, uid_t uid, pid_t pid, struct thread *td,
+ int compress)
{
struct sbuf sb;
const char *format;
char *temp;
size_t i;
-
+ int indexpos;
+ char hostname[MAXHOSTNAMELEN];
+
format = corefilename;
temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
if (temp == NULL)
return (NULL);
+ indexpos = -1;
(void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
for (i = 0; format[i]; i++) {
switch (format[i]) {
@@ -2975,6 +3019,15 @@ expand_name(name, uid, pid)
case '%':
sbuf_putc(&sb, '%');
break;
+ case 'H': /* hostname */
+ getcredhostname(td->td_ucred, hostname,
+ sizeof(hostname));
+ sbuf_printf(&sb, "%s", hostname);
+ break;
+ case 'I': /* autoincrementing index */
+ sbuf_printf(&sb, "0");
+ indexpos = sbuf_len(&sb) - 1;
+ break;
case 'N': /* process name */
sbuf_printf(&sb, "%s", name);
break;
@@ -2994,6 +3047,11 @@ expand_name(name, uid, pid)
sbuf_putc(&sb, format[i]);
}
}
+#ifdef COMPRESS_USER_CORES
+ if (compress) {
+ sbuf_printf(&sb, GZ_SUFFIX);
+ }
+#endif
if (sbuf_overflowed(&sb)) {
sbuf_delete(&sb);
log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
@@ -3003,6 +3061,53 @@ expand_name(name, uid, pid)
}
sbuf_finish(&sb);
sbuf_delete(&sb);
+
+ /*
+ * 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) {
+ struct nameidata nd;
+ int error, n;
+ int flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
+ int cmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
+ int vfslocked;
+
+ for (n = 0; n < num_cores; n++) {
+ temp[indexpos] = '0' + n;
+ NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE,
+ temp, td);
+ error = vn_open(&nd, &flags, cmode, 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, name, uid, temp, error);
+ free(temp, M_TEMP);
+ return (NULL);
+ }
+ vfslocked = NDHASGIANT(&nd);
+ NDFREE(&nd, NDF_ONLY_PNBUF);
+ VOP_UNLOCK(nd.ni_vp, 0);
+ error = vn_close(nd.ni_vp, FWRITE, td->td_ucred, td);
+ VFS_UNLOCK_GIANT(vfslocked);
+ if (error) {
+ log(LOG_ERR,
+ "pid %d (%s), uid (%u): Path `%s' failed "
+ "on close after initial open test, "
+ "error = %d\n",
+ pid, name, uid, temp, error);
+ free(temp, M_TEMP);
+ return (NULL);
+ }
+ break;
+ }
+ }
return (temp);
}
@@ -3028,12 +3133,19 @@ coredump(struct thread *td)
char *name; /* name of corefile */
off_t limit;
int vfslocked;
+ int compress;
+#ifdef COMPRESS_USER_CORES
+ compress = compress_user_cores;
+#else
+ compress = 0;
+#endif
PROC_LOCK_ASSERT(p, MA_OWNED);
MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
_STOPEVENT(p, S_CORE, 0);
- name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
+ name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid, td,
+ compress);
if (name == NULL) {
PROC_UNLOCK(p);
#ifdef AUDIT
@@ -3124,7 +3236,7 @@ restart:
PROC_UNLOCK(p);
error = p->p_sysent->sv_coredump ?
- p->p_sysent->sv_coredump(td, vp, limit) :
+ p->p_sysent->sv_coredump(td, vp, limit, compress ? IMGACT_CORE_COMPRESS : 0) :
ENOSYS;
if (locked) {
diff --git a/sys/net/zlib.h b/sys/net/zlib.h
index 9529e43..85db8d4 100644
--- a/sys/net/zlib.h
+++ b/sys/net/zlib.h
@@ -1010,6 +1010,13 @@ extern int EXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
uLongf *get_crc_table OF((void)); /* can be used by asm versions of crc32() */
+#ifdef _KERNEL
+struct vnode;
+extern gzFile gz_open OF((const char *path, const char *mode,
+ struct vnode *vp));
+#endif
+
+
#ifdef __cplusplus
}
#endif
diff --git a/sys/net/zutil.h b/sys/net/zutil.h
new file mode 100644
index 0000000..74f0221
--- /dev/null
+++ b/sys/net/zutil.h
@@ -0,0 +1,231 @@
+/* zutil.h -- internal interface and configuration of the compression library
+ * Copyright (C) 1995-1996 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* From: zutil.h,v 1.16 1996/07/24 13:41:13 me Exp $ */
+/* $FreeBSD$ */
+
+#ifndef _Z_UTIL_H
+#define _Z_UTIL_H
+
+#define ZEXPORT
+
+#ifdef _KERNEL
+#include <net/zlib.h>
+#else
+#include "zlib.h"
+#endif
+
+#ifdef _KERNEL
+/* Assume this is a *BSD or SVR4 kernel */
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/systm.h>
+#include <sys/module.h>
+#include <sys/errno.h>
+#include <sys/param.h>
+#include <sys/kernel.h>
+# define HAVE_MEMCPY
+# define memcpy(d, s, n) bcopy((s), (d), (n))
+# define memset(d, v, n) bzero((d), (n))
+# define memcmp bcmp
+
+#else
+#if defined(__KERNEL__)
+/* Assume this is a Linux kernel */
+#include <linux/string.h>
+#define HAVE_MEMCPY
+
+#else /* not kernel */
+
+#if defined(MSDOS)||defined(VMS)||defined(CRAY)||defined(WIN32)||defined(RISCOS)
+# include <stddef.h>
+# include <errno.h>
+#else
+ extern int errno;
+#endif
+#ifdef STDC
+# include <string.h>
+# include <stdlib.h>
+#endif
+#endif /* __KERNEL__ */
+#endif /* _KERNEL */
+
+#ifndef local
+# define local static
+#endif
+/* compile with -Dlocal if your debugger can't find static symbols */
+
+typedef unsigned char uch;
+typedef uch FAR uchf;
+typedef unsigned short ush;
+typedef ush FAR ushf;
+typedef unsigned long ulg;
+
+#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
+
+#define ERR_RETURN(strm,err) \
+ return (strm->msg = (const char*)ERR_MSG(err), (err))
+/* To be used only when the state is known to be valid */
+
+ /* common constants */
+
+#ifndef DEF_WBITS
+# define DEF_WBITS MAX_WBITS
+#endif
+/* default windowBits for decompression. MAX_WBITS is for compression only */
+
+#if MAX_MEM_LEVEL >= 8
+# define DEF_MEM_LEVEL 8
+#else
+# define DEF_MEM_LEVEL MAX_MEM_LEVEL
+#endif
+/* default memLevel */
+
+#define STORED_BLOCK 0
+#define STATIC_TREES 1
+#define DYN_TREES 2
+/* The three kinds of block type */
+
+#define MIN_MATCH 3
+#define MAX_MATCH 258
+/* The minimum and maximum match lengths */
+
+#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
+
+ /* target dependencies */
+
+#ifdef MSDOS
+# define OS_CODE 0x00
+# ifdef __TURBOC__
+# include <alloc.h>
+# else /* MSC or DJGPP */
+# include <malloc.h>
+# endif
+#endif
+
+#ifdef OS2
+# define OS_CODE 0x06
+#endif
+
+#ifdef WIN32 /* Window 95 & Windows NT */
+# define OS_CODE 0x0b
+#endif
+
+#if defined(VAXC) || defined(VMS)
+# define OS_CODE 0x02
+# define FOPEN(name, mode) \
+ fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
+#endif
+
+#ifdef AMIGA
+# define OS_CODE 0x01
+#endif
+
+#if defined(ATARI) || defined(atarist)
+# define OS_CODE 0x05
+#endif
+
+#ifdef MACOS
+# define OS_CODE 0x07
+#endif
+
+#ifdef __50SERIES /* Prime/PRIMOS */
+# define OS_CODE 0x0F
+#endif
+
+#ifdef TOPS20
+# define OS_CODE 0x0a
+#endif
+
+#if defined(_BEOS_) || defined(RISCOS)
+# define fdopen(fd,mode) NULL /* No fdopen() */
+#endif
+
+ /* Common defaults */
+
+#ifndef OS_CODE
+# define OS_CODE 0x03 /* assume Unix */
+#endif
+
+#ifndef FOPEN
+# define FOPEN(name, mode) fopen((name), (mode))
+#endif
+
+ /* functions */
+
+#ifdef HAVE_STRERROR
+ extern char *strerror OF((int));
+# define zstrerror(errnum) strerror(errnum)
+#else
+# define zstrerror(errnum) ""
+#endif
+
+#if defined(pyr)
+# define NO_MEMCPY
+#endif
+#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(_MSC_VER)
+ /* Use our own functions for small and medium model with MSC <= 5.0.
+ * You may have to use the same strategy for Borland C (untested).
+ */
+# define NO_MEMCPY
+#endif
+#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
+# define HAVE_MEMCPY
+#endif
+#ifdef HAVE_MEMCPY
+# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
+# define zmemcpy _fmemcpy
+# define zmemcmp _fmemcmp
+# define zmemzero(dest, len) _fmemset(dest, 0, len)
+# else
+# define zmemcpy memcpy
+# define zmemcmp memcmp
+# define zmemzero(dest, len) memset(dest, 0, len)
+# endif
+#else
+ extern void zmemcpy OF((Bytef* dest, Bytef* source, uInt len));
+ extern int zmemcmp OF((Bytef* s1, Bytef* s2, uInt len));
+ extern void zmemzero OF((Bytef* dest, uInt len));
+#endif
+
+/* Diagnostic functions */
+#ifdef DEBUG_ZLIB
+# include <stdio.h>
+# ifndef verbose
+# define verbose 0
+# endif
+ extern void z_error OF((char *m));
+# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
+# define Trace(x) fprintf x
+# define Tracev(x) {if (verbose) fprintf x ;}
+# define Tracevv(x) {if (verbose>1) fprintf x ;}
+# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
+# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
+#else
+# define Assert(cond,msg)
+# define Trace(x)
+# define Tracev(x)
+# define Tracevv(x)
+# define Tracec(c,x)
+# define Tracecv(c,x)
+#endif
+
+
+typedef uLong (*check_func) OF((uLong check, const Bytef *buf, uInt len));
+
+voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
+void zcfree OF((voidpf opaque, voidpf ptr));
+
+#define ZALLOC(strm, items, size) \
+ (*((strm)->zalloc))((strm)->opaque, (items), (size))
+#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
+#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
+
+#endif /* _Z_UTIL_H */
diff --git a/sys/sys/eventhandler.h b/sys/sys/eventhandler.h
index 1f26d48..6715efc 100644
--- a/sys/sys/eventhandler.h
+++ b/sys/sys/eventhandler.h
@@ -211,7 +211,20 @@ EVENTHANDLER_DECLARE(process_exit, exitlist_fn);
EVENTHANDLER_DECLARE(process_fork, forklist_fn);
EVENTHANDLER_DECLARE(process_exec, execlist_fn);
+/*
+ * application dump event
+ */
struct thread;
+typedef void (*app_coredump_start_fn)(void *, struct thread *, char *name);
+typedef void (*app_coredump_progress_fn)(void *, struct thread *td, int byte_count);
+typedef void (*app_coredump_finish_fn)(void *, struct thread *td);
+typedef void (*app_coredump_error_fn)(void *, struct thread *td, char *msg, ...);
+
+EVENTHANDLER_DECLARE(app_coredump_start, app_coredump_start_fn);
+EVENTHANDLER_DECLARE(app_coredump_progress, app_coredump_progress_fn);
+EVENTHANDLER_DECLARE(app_coredump_finish, app_coredump_finish_fn);
+EVENTHANDLER_DECLARE(app_coredump_error, app_coredump_error_fn);
+
typedef void (*thread_ctor_fn)(void *, struct thread *);
typedef void (*thread_dtor_fn)(void *, struct thread *);
typedef void (*thread_fini_fn)(void *, struct thread *);
diff --git a/sys/sys/imgact.h b/sys/sys/imgact.h
index e6acc00..79b389e 100644
--- a/sys/sys/imgact.h
+++ b/sys/sys/imgact.h
@@ -75,6 +75,8 @@ struct image_params {
struct sysentvec;
struct thread;
+#define IMGACT_CORE_COMPRESS 0x01
+
int exec_check_permissions(struct image_params *);
register_t *exec_copyout_strings(struct image_params *);
int exec_new_vmspace(struct image_params *, struct sysentvec *);
diff --git a/sys/sys/imgact_aout.h b/sys/sys/imgact_aout.h
index c386c1b..ca67cab 100644
--- a/sys/sys/imgact_aout.h
+++ b/sys/sys/imgact_aout.h
@@ -150,7 +150,8 @@ struct exec {
struct thread;
struct vnode;
-int aout_coredump(struct thread *td, struct vnode *vp, off_t limit);
+int aout_coredump(struct thread *td, struct vnode *vp, off_t limit,
+ int flags);
#endif
#endif /* !_IMGACT_AOUT_H_ */
diff --git a/sys/sys/imgact_elf.h b/sys/sys/imgact_elf.h
index 4ccee5d..1cc2a61 100644
--- a/sys/sys/imgact_elf.h
+++ b/sys/sys/imgact_elf.h
@@ -88,7 +88,7 @@ int __elfN(brand_inuse)(Elf_Brandinfo *entry);
int __elfN(insert_brand_entry)(Elf_Brandinfo *entry);
int __elfN(remove_brand_entry)(Elf_Brandinfo *entry);
int __elfN(freebsd_fixup)(register_t **, struct image_params *);
-int __elfN(coredump)(struct thread *, struct vnode *, off_t);
+int __elfN(coredump)(struct thread *, struct vnode *, off_t, int);
/* Machine specific function to dump per-thread information. */
void __elfN(dump_thread)(struct thread *, void *, size_t *);
diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h
index f17f4f03..a7a2907 100644
--- a/sys/sys/sysent.h
+++ b/sys/sys/sysent.h
@@ -87,7 +87,7 @@ struct sysentvec {
void (*sv_prepsyscall)(struct trapframe *, int *, u_int *,
caddr_t *);
char *sv_name; /* name of binary type */
- int (*sv_coredump)(struct thread *, struct vnode *, off_t);
+ int (*sv_coredump)(struct thread *, struct vnode *, off_t, int);
/* function to dump core, or NULL */
int (*sv_imgact_try)(struct image_params *);
int sv_minsigstksz; /* minimum signal stack size */
OpenPOWER on IntegriCloud