xref: /dflybsd-src/lib/libc/sys/sys_checkpoint.2 (revision 8877df9482feff719e511e8051fbb4a43e3eefd8)
1*8877df94SMatthew Dillon.\" Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
2*8877df94SMatthew Dillon.\"
3*8877df94SMatthew Dillon.\" This code is derived from software contributed to The DragonFly Project
4*8877df94SMatthew Dillon.\" by Matthew Dillon <dillon@backplane.com>
5*8877df94SMatthew Dillon.\"
6*8877df94SMatthew Dillon.\" Redistribution and use in source and binary forms, with or without
7*8877df94SMatthew Dillon.\" modification, are permitted provided that the following conditions
8*8877df94SMatthew Dillon.\" are met:
9*8877df94SMatthew Dillon.\"
10*8877df94SMatthew Dillon.\" 1. Redistributions of source code must retain the above copyright
11*8877df94SMatthew Dillon.\"    notice, this list of conditions and the following disclaimer.
12*8877df94SMatthew Dillon.\" 2. Redistributions in binary form must reproduce the above copyright
13*8877df94SMatthew Dillon.\"    notice, this list of conditions and the following disclaimer in
14*8877df94SMatthew Dillon.\"    the documentation and/or other materials provided with the
15*8877df94SMatthew Dillon.\"    distribution.
16*8877df94SMatthew Dillon.\" 3. Neither the name of The DragonFly Project nor the names of its
17*8877df94SMatthew Dillon.\"    contributors may be used to endorse or promote products derived
18*8877df94SMatthew Dillon.\"    from this software without specific, prior written permission.
19*8877df94SMatthew Dillon.\"
20*8877df94SMatthew Dillon.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*8877df94SMatthew Dillon.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*8877df94SMatthew Dillon.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*8877df94SMatthew Dillon.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24*8877df94SMatthew Dillon.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*8877df94SMatthew Dillon.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*8877df94SMatthew Dillon.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*8877df94SMatthew Dillon.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*8877df94SMatthew Dillon.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*8877df94SMatthew Dillon.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30*8877df94SMatthew Dillon.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*8877df94SMatthew Dillon.\" SUCH DAMAGE.
32*8877df94SMatthew Dillon.\"
33*8877df94SMatthew Dillon.\" $DragonFly: src/lib/libc/sys/sys_checkpoint.2,v 1.1 2004/11/23 07:36:32 dillon Exp $
34*8877df94SMatthew Dillon.\"
35*8877df94SMatthew Dillon.Dd November 22, 2004
36*8877df94SMatthew Dillon.Dt SYS_CHECKPOINT 2
37*8877df94SMatthew Dillon.Os
38*8877df94SMatthew Dillon.Sh NAME
39*8877df94SMatthew Dillon.Nm sys_checkpoint
40*8877df94SMatthew Dillon.Nd checkpoint a process or restore a checkpointed process
41*8877df94SMatthew Dillon.Sh LIBRARY
42*8877df94SMatthew Dillon.Lb libc
43*8877df94SMatthew Dillon.Sh SYNOPSIS
44*8877df94SMatthew Dillon.In sys/types.h
45*8877df94SMatthew Dillon.In sys/checkpoint.h
46*8877df94SMatthew Dillon.Ft int
47*8877df94SMatthew Dillon.Fn sys_checkpoint "int type" "int fd" "pid_t pid" "int retval"
48*8877df94SMatthew Dillon.Sh DESCRIPTION
49*8877df94SMatthew Dillon.Fn Sys_checkpoint
50*8877df94SMatthew Dillonexecutes a checkpoint function as specified by
51*8877df94SMatthew Dillon.Fa type .
52*8877df94SMatthew DillonSupported types are as follows:
53*8877df94SMatthew Dillon.Pp
54*8877df94SMatthew Dillon.Bl -tag -width CKPT_FREEZE indent
55*8877df94SMatthew Dillon.It Dv CKPT_FREEZE
56*8877df94SMatthew DillonGenerate a checkpoint file.
57*8877df94SMatthew DillonCurrently
58*8877df94SMatthew Dillon.Fa pid
59*8877df94SMatthew Dillonmust be -1 or the pid of the current process.
60*8877df94SMatthew DillonThe checkpoint file will be written out to
61*8877df94SMatthew Dillon.Fa fd ,
62*8877df94SMatthew Dillonand
63*8877df94SMatthew Dillon.Fa retval
64*8877df94SMatthew Dillonis unused but must be specified as -1.
65*8877df94SMatthew DillonAs a special case, if
66*8877df94SMatthew Dillon.Fa pid
67*8877df94SMatthew Dillonand
68*8877df94SMatthew Dillon.Fa fd
69*8877df94SMatthew Dillonare both specified as -1, the system will generate a checkpoint file
70*8877df94SMatthew Dillonusing the system checkpoint template.
71*8877df94SMatthew Dillon.Pp
72*8877df94SMatthew DillonThis function returns 0 on success, -1 on error, and typically 1
73*8877df94SMatthew Dillonon resume.  The value returned on resume is controlled by the
74*8877df94SMatthew Dillon.Fa retval
75*8877df94SMatthew Dillonargument passed to
76*8877df94SMatthew Dillon.Fn sys_checkpoint
77*8877df94SMatthew Dillonwhen resuming a checkpoint file.  A user program which installs its
78*8877df94SMatthew Dillonown
79*8877df94SMatthew Dillon.Dv SIGCKPT
80*8877df94SMatthew Dillonsignal handler and calls
81*8877df94SMatthew Dillon.Fn sys_checkpoint
82*8877df94SMatthew Dillonmanually thus has control over both termination/continuance and
83*8877df94SMatthew Dillonresumption.
84*8877df94SMatthew Dillon.It Dv CKPT_THAW
85*8877df94SMatthew DillonRestore a checkpointed program.
86*8877df94SMatthew Dillon.Fa Pid
87*8877df94SMatthew Dillonmust be specified as -1, and
88*8877df94SMatthew Dillon.Fa fd
89*8877df94SMatthew Dillonrepresents the checkpoint file.
90*8877df94SMatthew Dillon.Fa Retval
91*8877df94SMatthew Dillonspecifies the value returned to the resumed program if it had called
92*8877df94SMatthew Dillon.Fn sys_checkpoint
93*8877df94SMatthew Dillondirectly.
94*8877df94SMatthew Dillon.Pp
95*8877df94SMatthew DillonThe checkpointed program will replace the current program, similar to
96*8877df94SMatthew Dillonan exec call.
97*8877df94SMatthew Dillon.El
98*8877df94SMatthew Dillon.Sh RETURN VALUES
99*8877df94SMatthew DillonUpon successful completion, the value 0 is typically returned.  A checkpoint
100*8877df94SMatthew Dillonbeing resumed typically returns a positive value; otherwise the value -1
101*8877df94SMatthew Dillonis returned and the global variable errno is set to indicate the
102*8877df94SMatthew Dillonerror.
103*8877df94SMatthew Dillon.Sh ERRORS
104*8877df94SMatthew Dillon.Bl -tag -width Er
105*8877df94SMatthew Dillon.It Bq Er EBADF
106*8877df94SMatthew Dillon.Fa Fd
107*8877df94SMatthew Dillonis not a valid regular file, socket descriptor, or pipe.  Note that not
108*8877df94SMatthew Dillonall systems necessarily support checkpointing to sockets and pipes.
109*8877df94SMatthew Dillon.It Bq Er EPERM
110*8877df94SMatthew DillonThe caller does not have permission to issue the checkpoint command.
111*8877df94SMatthew DillonCheckpointing may be restricted or disabled with sysctls.
112*8877df94SMatthew Dillon.It Bq Er EIO
113*8877df94SMatthew DillonAn I/O error occurred while reading from the file system.
114*8877df94SMatthew Dillon.It Bq Er EINVAL
115*8877df94SMatthew DillonAn invalid parameter was specified.
116*8877df94SMatthew Dillon.El
117*8877df94SMatthew Dillon.Pp
118*8877df94SMatthew Dillon.Sh CHECKPOINT FEATURES
119*8877df94SMatthew DillonThe system checkpointing code will save the process register state (including
120*8877df94SMatthew Dillonfloating point registers), signal state, file descriptors representing
121*8877df94SMatthew Dillonregular files or directories (anything that can be converted into a file
122*8877df94SMatthew Dillonhandle for storage), and both shared and private memory mappings.
123*8877df94SMatthew DillonPrivate, writable mappings are copied to the checkpoint file while shared
124*8877df94SMatthew Dillonmappings and stored by referencing the file handle and offset.
125*8877df94SMatthew DillonNote that the system checkpointing code does not retain references to
126*8877df94SMatthew Dillondeleted files, so mappings and open descriptors of deleted files
127*8877df94SMatthew Dilloncannot be restored.
128*8877df94SMatthew DillonUnpredictable operation will occur if a checkpoint-unaware program
129*8877df94SMatthew Dillonis restored and some of the underlying files mapped by the program
130*8877df94SMatthew Dillonhave changed.
131*8877df94SMatthew Dillon.Pp
132*8877df94SMatthew DillonThe system checkpointing code is not able to retain the process pid, process
133*8877df94SMatthew Dillongroup, user/group creds, or descriptors 0, 1, and 2.  These will be inherited
134*8877df94SMatthew Dillonfrom whomever restores the checkpoint.
135*8877df94SMatthew Dillon.Pp
136*8877df94SMatthew DillonWhen a checkpointed program is restored modified private mappings will
137*8877df94SMatthew Dillonbe mapped from the checkpoint file itself, but major portions of the
138*8877df94SMatthew Dillonoriginal program binary will be mapped from the original program binary.
139*8877df94SMatthew DillonIf the resumed program is checkpointed again the system will automatically
140*8877df94SMatthew Dilloncopy any mappings from the original checkpoint file to the new one, since
141*8877df94SMatthew Dillonthe original is likely being replaced.
142*8877df94SMatthew DillonThe caller must not truncate the existing checkpoint file when creating
143*8877df94SMatthew Dillona new one or specify the existing file's file descriptor as the new
144*8877df94SMatthew Dillonone as this will destroy the data that the checkpoint operation needs
145*8877df94SMatthew Dillonto copy to the new file.
146*8877df94SMatthew Dillon.Pp
147*8877df94SMatthew Dillon.Sh SIGNALS
148*8877df94SMatthew DillonTwo signals are associated with checkpointing.
149*8877df94SMatthew Dillon.Dv SIGCKPT
150*8877df94SMatthew Dillonis delivered via the tty ckpt character, usually control-E.  Its default
151*8877df94SMatthew Dillonaction is to checkpoint a program and continue running it.  The
152*8877df94SMatthew Dillon.Dv SIGCKPTEXIT
153*8877df94SMatthew Dillonsignal can only be delivered by
154*8877df94SMatthew Dillon.Xr kill 2 .
155*8877df94SMatthew DillonIts default action is to checkpoint a program and then exit.
156*8877df94SMatthew Dillon.Dv SIGCKPTEXIT
157*8877df94SMatthew Dillonmight not be implemented by the system.  Both signals are defined to
158*8877df94SMatthew Dillonbe greater or equal to signal 32 and cannot be manipulated using legacy
159*8877df94SMatthew Dillonmasking functions.
160*8877df94SMatthew Dillon.Pp
161*8877df94SMatthew DillonIf a program overrides the default action for a checkpoint signal the
162*8877df94SMatthew Dillonsystem will not undertake any action of its own.  The program may issue
163*8877df94SMatthew Dillonthe checkpoint command from the signal handler itself or simply set a
164*8877df94SMatthew Dillonreminder for later action.  It is usually safest to set a reminder and
165*8877df94SMatthew Dillondo the actual checkpointing from your main loop.
166*8877df94SMatthew Dillon.Pp
167*8877df94SMatthew Dillon.Sh EXAMPLE
168*8877df94SMatthew Dillon.Bd -literal indent -compact
169*8877df94SMatthew Dillon
170*8877df94SMatthew Dillon/*
171*8877df94SMatthew Dillon * Demonstrate checkpointing.  Use control-E to checkpoint the program
172*8877df94SMatthew Dillon * and checkpt -r x.ckpt to resume it.
173*8877df94SMatthew Dillon */
174*8877df94SMatthew Dillon#include <sys/types.h>
175*8877df94SMatthew Dillon#include <sys/signal.h>
176*8877df94SMatthew Dillon#include <sys/checkpoint.h>
177*8877df94SMatthew Dillon#include <stdio.h>
178*8877df94SMatthew Dillon#include <unistd.h>
179*8877df94SMatthew Dillon#include <fcntl.h>
180*8877df94SMatthew Dillon#include <errno.h>
181*8877df94SMatthew Dillon
182*8877df94SMatthew Dillonvoid docheckpoint(void);
183*8877df94SMatthew Dillon
184*8877df94SMatthew Dillonint wantckpt;
185*8877df94SMatthew Dillon
186*8877df94SMatthew Dillonvoid
187*8877df94SMatthew Dillondockpt(int sig)
188*8877df94SMatthew Dillon{
189*8877df94SMatthew Dillon    wantckpt = 1;
190*8877df94SMatthew Dillon}
191*8877df94SMatthew Dillon
192*8877df94SMatthew Dillonint
193*8877df94SMatthew Dillonmain(int argc, char** argv)
194*8877df94SMatthew Dillon{
195*8877df94SMatthew Dillon     int i = 0;
196*8877df94SMatthew Dillon
197*8877df94SMatthew Dillon     signal(SIGCKPT, dockpt);
198*8877df94SMatthew Dillon
199*8877df94SMatthew Dillon     for (;;) {
200*8877df94SMatthew Dillon	printf("iteration: %d\en", i);
201*8877df94SMatthew Dillon	++i;
202*8877df94SMatthew Dillon	sleep(1);
203*8877df94SMatthew Dillon	if (wantckpt) {
204*8877df94SMatthew Dillon		wantckpt = 0;
205*8877df94SMatthew Dillon		printf("Checkpoint requested\en");
206*8877df94SMatthew Dillon		docheckpoint();
207*8877df94SMatthew Dillon	}
208*8877df94SMatthew Dillon    }
209*8877df94SMatthew Dillon    return(0);
210*8877df94SMatthew Dillon}
211*8877df94SMatthew Dillon
212*8877df94SMatthew Dillonvoid
213*8877df94SMatthew Dillondocheckpoint(void)
214*8877df94SMatthew Dillon{
215*8877df94SMatthew Dillon    int ret;
216*8877df94SMatthew Dillon    int fd;
217*8877df94SMatthew Dillon
218*8877df94SMatthew Dillon    fd = open("x.ckpt", O_RDWR|O_CREAT|O_TRUNC, 0666);
219*8877df94SMatthew Dillon    if (fd < 0) {
220*8877df94SMatthew Dillon	printf("unable to create checkpoint file: %s\en", strerror(errno));
221*8877df94SMatthew Dillon	return;
222*8877df94SMatthew Dillon    }
223*8877df94SMatthew Dillon
224*8877df94SMatthew Dillon    ret = sys_checkpoint(CKPT_FREEZE, fd, -1, -1);
225*8877df94SMatthew Dillon    if (ret < 0) {
226*8877df94SMatthew Dillon	printf("unable to checkpoint: %s\en", strerror(errno));
227*8877df94SMatthew Dillon    } else if (ret == 0) {
228*8877df94SMatthew Dillon	printf("checkpoint successful, continuing\en");
229*8877df94SMatthew Dillon    } else if (ret == 1) {
230*8877df94SMatthew Dillon	printf("resuming from checkpoint.\en");
231*8877df94SMatthew Dillon    } else {
232*8877df94SMatthew Dillon	printf("unknown return value %d from sys_checkpoint\en", ret);
233*8877df94SMatthew Dillon	exit(1);
234*8877df94SMatthew Dillon    }
235*8877df94SMatthew Dillon    /* note that the file descriptor is still valid on a resume */
236*8877df94SMatthew Dillon    close(fd);
237*8877df94SMatthew Dillon}
238*8877df94SMatthew Dillon.Ed
239*8877df94SMatthew Dillon.Pp
240*8877df94SMatthew Dillon.Sh SEE ALSO
241*8877df94SMatthew Dillon.Xr checkpt 1
242*8877df94SMatthew Dillon.Sh HISTORY
243*8877df94SMatthew DillonThe
244*8877df94SMatthew Dillon.Fn sys_checkpoint
245*8877df94SMatthew Dillonfunction call
246*8877df94SMatthew Dillonappeared in
247*8877df94SMatthew DillonDragonFly 1.1 .
248