Make setthetime() static per the prototype.
[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.4 2004/06/05 16:34:04 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 <sys/kern_syscall.h>
67 #include "checkpt.h"
68
69
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");
73
74 /*
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")
79  *      %P      process id (pid)
80  *      %U      user id (uid)
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).
84  *
85  * -- taken from the coredump code
86  */
87
88 char *
89 ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
90 {
91         char *temp;
92         char *bp;
93         char buf[11];           /* Buffer for pid/uid -- max 4B */
94         int error;
95         int i;
96         int n;
97         char *format = ckptfilename;
98         size_t namelen;
99
100         temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
101         if (temp == NULL)
102                 return NULL;
103         namelen = strlen(name);
104         n = 0;
105         if (ckptfilename[0] != '/') {
106                 if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) {
107                         free(temp, M_TEMP);
108                         return NULL;
109                 }
110                 n = strlen(bp);
111                 bcopy(bp, temp, n + 1); /* normalize location of the path */
112                 temp[n++] = '/';
113                 temp[n] = '\0';
114         }
115         for (i= 0; n < MAXPATHLEN && format[i]; i++) {
116                 int l;
117                 switch (format[i]) {
118                 case '%':       /* Format character */
119                         i++;
120                         switch (format[i]) {
121                         case '%':
122                                 temp[n++] = '%';
123                                 break;
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);
128                                         free(temp, M_TEMP);
129                                         return NULL;
130                                 }
131                                 memcpy(temp+n, name, namelen);
132                                 n += namelen;
133                                 break;
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);
139                                         free(temp, M_TEMP);
140                                         return NULL;
141                                 }
142                                 memcpy(temp+n, buf, l);
143                                 n += l;
144                                 break;
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);
150                                         free(temp, M_TEMP);
151                                         return NULL;
152                                 }
153                                 memcpy(temp+n, buf, l);
154                                 n += l;
155                                 break;
156                         default:
157                                 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
158                         }
159                         break;
160                 default:
161                         temp[n++] = format[i];
162                 }
163         }
164         temp[n] = '\0';
165         return temp;
166 }
167