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.1 2003/10/20 04:48:42 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>
69 static char ckptfilename[64] = {"%N.ckpt"};
70 SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename,
71 sizeof(ckptfilename), "process checkpoint name format string");
74 * expand_name(name, uid, pid)
75 * Expand the name described in corefilename, using name, uid, and pid.
76 * corefilename is a printf-like string, with three format specifiers:
77 * %N name of process ("name")
80 * For example, "%N.core" is the default; they can be disabled completely
81 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
82 * This is controlled by the sysctl variable kern.corefile (see above).
84 * -- taken from the coredump code
88 ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
91 char buf[11]; /* Buffer for pid/uid -- max 4B */
93 char *format = ckptfilename;
96 temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
99 namelen = strlen(name);
100 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
103 case '%': /* Format character */
109 case 'N': /* process name */
110 if ((n + namelen) > MAXPATHLEN) {
111 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
112 pid, name, uid, temp, name);
116 memcpy(temp+n, name, namelen);
119 case 'P': /* process id */
120 l = sprintf(buf, "%u", pid);
121 if ((n + l) > MAXPATHLEN) {
122 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
123 pid, name, uid, temp, name);
127 memcpy(temp+n, buf, l);
130 case 'U': /* user id */
131 l = sprintf(buf, "%u", uid);
132 if ((n + l) > MAXPATHLEN) {
133 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
134 pid, name, uid, temp, name);
138 memcpy(temp+n, buf, l);
142 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
146 temp[n++] = format[i];