Merge from vendor branch TCPDUMP:
[dragonfly.git] / sys / checkpt / support.c
1 /*-
2  * Copyright (c) 2003 Kip Macy All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $DragonFly: src/sys/checkpt/Attic/support.c,v 1.1 2003/10/20 04:48:42 dillon Exp $
26  */
27
28 /* prune the includes XXX */
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/proc.h>
32 #include <sys/module.h>
33 #include <sys/sysent.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36
37 #include <sys/file.h>
38 /* only on dragonfly */
39 #include <sys/file2.h>
40 #include <sys/fcntl.h>
41 #include <sys/signal.h>
42 #include <vm/vm.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/procfs.h>
45
46 #include <sys/lock.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_map.h>
49
50 #include <sys/mman.h>
51 #include <sys/sysproto.h>
52 #include <sys/malloc.h>
53 #include <sys/stat.h>
54 #include <sys/uio.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>
63 #include <sys/exec.h>
64 #include <sys/unistd.h>
65 #include <sys/time.h>
66 #include "checkpt.h"
67
68
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");
72
73 /*
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")
78  *      %P      process id (pid)
79  *      %U      user id (uid)
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).
83  *
84  * -- taken from the coredump code
85  */
86
87 char *
88 ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
89 {
90         char *temp;
91         char buf[11];           /* Buffer for pid/uid -- max 4B */
92         int i, n;
93         char *format = ckptfilename;
94         size_t namelen;
95
96         temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
97         if (temp == NULL)
98                 return NULL;
99         namelen = strlen(name);
100         for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
101                 int l;
102                 switch (format[i]) {
103                 case '%':       /* Format character */
104                         i++;
105                         switch (format[i]) {
106                         case '%':
107                                 temp[n++] = '%';
108                                 break;
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);
113                                         free(temp, M_TEMP);
114                                         return NULL;
115                                 }
116                                 memcpy(temp+n, name, namelen);
117                                 n += namelen;
118                                 break;
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);
124                                         free(temp, M_TEMP);
125                                         return NULL;
126                                 }
127                                 memcpy(temp+n, buf, l);
128                                 n += l;
129                                 break;
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);
135                                         free(temp, M_TEMP);
136                                         return NULL;
137                                 }
138                                 memcpy(temp+n, buf, l);
139                                 n += l;
140                                 break;
141                         default:
142                                 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
143                         }
144                         break;
145                 default:
146                         temp[n++] = format[i];
147                 }
148         }
149         temp[n] = '\0';
150         return temp;
151 }
152