2 * Copyright (c) 2003 Kip Macy All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $DragonFly: src/sys/checkpt/Attic/support.c,v 1.4 2004/06/05 16:34:04 dillon Exp $
28 /* prune the includes XXX */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/sysent.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
38 /* only on dragonfly */
39 #include <sys/file2.h>
40 #include <sys/fcntl.h>
41 #include <sys/signal.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/procfs.h>
48 #include <vm/vm_map.h>
51 #include <sys/sysproto.h>
52 #include <sys/malloc.h>
55 #include <sys/namei.h>
56 #include <sys/vnode.h>
57 #include <machine/limits.h>
58 #include <i386/include/frame.h>
59 #include <sys/signalvar.h>
60 #include <sys/syslog.h>
61 #include <sys/sysctl.h>
62 #include <i386/include/sigframe.h>
64 #include <sys/unistd.h>
66 #include <sys/kern_syscall.h>
70 static char ckptfilename[MAXPATHLEN] = {"%N.ckpt"};
71 SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename,
72 sizeof(ckptfilename), "process checkpoint name format string");
75 * expand_name(name, uid, pid)
76 * Expand the name described in corefilename, using name, uid, and pid.
77 * corefilename is a printf-like string, with three format specifiers:
78 * %N name of process ("name")
81 * For example, "%N.core" is the default; they can be disabled completely
82 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
83 * This is controlled by the sysctl variable kern.corefile (see above).
85 * -- taken from the coredump code
89 ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
93 char buf[11]; /* Buffer for pid/uid -- max 4B */
97 char *format = ckptfilename;
100 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
103 namelen = strlen(name);
105 if (ckptfilename[0] != '/') {
106 if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) {
111 bcopy(bp, temp, n + 1); /* normalize location of the path */
115 for (i= 0; n < MAXPATHLEN && format[i]; i++) {
118 case '%': /* Format character */
124 case 'N': /* process name */
125 if ((n + namelen) > MAXPATHLEN) {
126 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
127 pid, name, uid, temp, name);
131 memcpy(temp+n, name, namelen);
134 case 'P': /* process id */
135 l = sprintf(buf, "%u", pid);
136 if ((n + l) > MAXPATHLEN) {
137 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
138 pid, name, uid, temp, name);
142 memcpy(temp+n, buf, l);
145 case 'U': /* user id */
146 l = sprintf(buf, "%u", uid);
147 if ((n + l) > MAXPATHLEN) {
148 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
149 pid, name, uid, temp, name);
153 memcpy(temp+n, buf, l);
157 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
161 temp[n++] = format[i];