xref: /dflybsd-src/lib/libc/sys/sys_checkpoint.2 (revision 4a5f69f602439e4ddaa5c06d3f350823dac88dd0)
18877df94SMatthew Dillon.\" Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
28877df94SMatthew Dillon.\"
38877df94SMatthew Dillon.\" This code is derived from software contributed to The DragonFly Project
48877df94SMatthew Dillon.\" by Matthew Dillon <dillon@backplane.com>
58877df94SMatthew Dillon.\"
68877df94SMatthew Dillon.\" Redistribution and use in source and binary forms, with or without
78877df94SMatthew Dillon.\" modification, are permitted provided that the following conditions
88877df94SMatthew Dillon.\" are met:
98877df94SMatthew Dillon.\"
108877df94SMatthew Dillon.\" 1. Redistributions of source code must retain the above copyright
118877df94SMatthew Dillon.\"    notice, this list of conditions and the following disclaimer.
128877df94SMatthew Dillon.\" 2. Redistributions in binary form must reproduce the above copyright
138877df94SMatthew Dillon.\"    notice, this list of conditions and the following disclaimer in
148877df94SMatthew Dillon.\"    the documentation and/or other materials provided with the
158877df94SMatthew Dillon.\"    distribution.
168877df94SMatthew Dillon.\" 3. Neither the name of The DragonFly Project nor the names of its
178877df94SMatthew Dillon.\"    contributors may be used to endorse or promote products derived
188877df94SMatthew Dillon.\"    from this software without specific, prior written permission.
198877df94SMatthew Dillon.\"
208877df94SMatthew Dillon.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
218877df94SMatthew Dillon.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
228877df94SMatthew Dillon.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
238877df94SMatthew Dillon.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
248877df94SMatthew Dillon.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
258877df94SMatthew Dillon.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
268877df94SMatthew Dillon.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
278877df94SMatthew Dillon.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
288877df94SMatthew Dillon.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
298877df94SMatthew Dillon.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
308877df94SMatthew Dillon.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318877df94SMatthew Dillon.\" SUCH DAMAGE.
328877df94SMatthew Dillon.\"
33fb49adadSSascha Wildner.Dd June 29, 2007
348877df94SMatthew Dillon.Dt SYS_CHECKPOINT 2
358877df94SMatthew Dillon.Os
368877df94SMatthew Dillon.Sh NAME
378877df94SMatthew Dillon.Nm sys_checkpoint
38c1de475dSHiten Pandya.Nd checkpoint or restore a process
398877df94SMatthew Dillon.Sh LIBRARY
408877df94SMatthew Dillon.Lb libc
418877df94SMatthew Dillon.Sh SYNOPSIS
428877df94SMatthew Dillon.In sys/types.h
438877df94SMatthew Dillon.In sys/checkpoint.h
448877df94SMatthew Dillon.Ft int
458877df94SMatthew Dillon.Fn sys_checkpoint "int type" "int fd" "pid_t pid" "int retval"
468877df94SMatthew Dillon.Sh DESCRIPTION
47c1de475dSHiten PandyaThe
48c1de475dSHiten Pandya.Fn sys_checkpoint
49c1de475dSHiten Pandyasystem call executes a checkpoint function as specified by
508877df94SMatthew Dillon.Fa type .
518877df94SMatthew DillonSupported types are as follows:
52fb49adadSSascha Wildner.Bl -tag -width ".Dv CKPT_FREEZE" -offset indent
538877df94SMatthew Dillon.It Dv CKPT_FREEZE
548877df94SMatthew DillonGenerate a checkpoint file.
558877df94SMatthew DillonCurrently
568877df94SMatthew Dillon.Fa pid
578877df94SMatthew Dillonmust be -1 or the pid of the current process.
588877df94SMatthew DillonThe checkpoint file will be written out to
598877df94SMatthew Dillon.Fa fd ,
608877df94SMatthew Dillonand
618877df94SMatthew Dillon.Fa retval
628877df94SMatthew Dillonis unused but must be specified as -1.
638877df94SMatthew DillonAs a special case, if
648877df94SMatthew Dillon.Fa pid
658877df94SMatthew Dillonand
668877df94SMatthew Dillon.Fa fd
678877df94SMatthew Dillonare both specified as -1, the system will generate a checkpoint file
688877df94SMatthew Dillonusing the system checkpoint template.
698877df94SMatthew Dillon.Pp
708877df94SMatthew DillonThis function returns 0 on success, -1 on error, and typically 1
71fb49adadSSascha Wildneron resume.
72fb49adadSSascha WildnerThe value returned on resume is controlled by the
738877df94SMatthew Dillon.Fa retval
748877df94SMatthew Dillonargument passed to
758877df94SMatthew Dillon.Fn sys_checkpoint
76fb49adadSSascha Wildnerwhen resuming a checkpoint file.
77fb49adadSSascha WildnerA user program which installs its own
788877df94SMatthew Dillon.Dv SIGCKPT
798877df94SMatthew Dillonsignal handler and calls
808877df94SMatthew Dillon.Fn sys_checkpoint
818877df94SMatthew Dillonmanually thus has control over both termination/continuance and
828877df94SMatthew Dillonresumption.
838877df94SMatthew Dillon.It Dv CKPT_THAW
848877df94SMatthew DillonRestore a checkpointed program.
85c1de475dSHiten PandyaThe
86c1de475dSHiten Pandya.Fa pid
878877df94SMatthew Dillonmust be specified as -1, and
888877df94SMatthew Dillon.Fa fd
898877df94SMatthew Dillonrepresents the checkpoint file.
90c1de475dSHiten PandyaThe
91c1de475dSHiten Pandya.Fa retval
92c1de475dSHiten Pandyaspecifies the value returned to the resumed program if
938877df94SMatthew Dillon.Fn sys_checkpoint
94c1de475dSHiten Pandyawas called directly.
958877df94SMatthew Dillon.Pp
968877df94SMatthew DillonThe checkpointed program will replace the current program, similar to
97fb49adadSSascha Wildneran
98fb49adadSSascha Wildner.Xr exec 3
99fb49adadSSascha Wildnercall.
1008877df94SMatthew Dillon.El
1018877df94SMatthew Dillon.Sh RETURN VALUES
102fb49adadSSascha WildnerUpon successful completion, the value 0 is typically returned.
103fb49adadSSascha WildnerA checkpoint being resumed typically returns a positive value;
104fb49adadSSascha Wildnerotherwise the value -1 is returned and the global variable
1050f09e575SSascha Wildner.Va errno
106c1de475dSHiten Pandyais set to indicate the error.
107*4a5f69f6SSascha Wildner.Sh EXAMPLES
108c1de475dSHiten Pandya.Bd -literal -offset indent -compact
1098877df94SMatthew Dillon/*
110c1de475dSHiten Pandya * Demonstrate checkpointing.  Use control-E to checkpoint
111c1de475dSHiten Pandya * the program and 'checkpt -r x.ckpt' to resume it.
1128877df94SMatthew Dillon */
1138877df94SMatthew Dillon#include <sys/types.h>
1148877df94SMatthew Dillon#include <sys/signal.h>
1158877df94SMatthew Dillon#include <sys/checkpoint.h>
1168877df94SMatthew Dillon#include <stdio.h>
1178877df94SMatthew Dillon#include <unistd.h>
1188877df94SMatthew Dillon#include <fcntl.h>
1198877df94SMatthew Dillon#include <errno.h>
1208877df94SMatthew Dillon
1218877df94SMatthew Dillonvoid docheckpoint(void);
1228877df94SMatthew Dillon
1238877df94SMatthew Dillonint wantckpt;
1248877df94SMatthew Dillon
1258877df94SMatthew Dillonvoid
1268877df94SMatthew Dillondockpt(int sig)
1278877df94SMatthew Dillon{
1288877df94SMatthew Dillon    wantckpt = 1;
1298877df94SMatthew Dillon}
1308877df94SMatthew Dillon
1318877df94SMatthew Dillonint
1328877df94SMatthew Dillonmain(int argc, char** argv)
1338877df94SMatthew Dillon{
1348877df94SMatthew Dillon     int i = 0;
1358877df94SMatthew Dillon
1368877df94SMatthew Dillon     signal(SIGCKPT, dockpt);
1378877df94SMatthew Dillon
1388877df94SMatthew Dillon     for (;;) {
1398877df94SMatthew Dillon	printf("iteration: %d\en", i);
1408877df94SMatthew Dillon	++i;
1418877df94SMatthew Dillon	sleep(1);
1428877df94SMatthew Dillon	if (wantckpt) {
1438877df94SMatthew Dillon		wantckpt = 0;
1448877df94SMatthew Dillon		printf("Checkpoint requested\en");
1458877df94SMatthew Dillon		docheckpoint();
1468877df94SMatthew Dillon	}
1478877df94SMatthew Dillon    }
1488877df94SMatthew Dillon    return(0);
1498877df94SMatthew Dillon}
1508877df94SMatthew Dillon
1518877df94SMatthew Dillonvoid
1528877df94SMatthew Dillondocheckpoint(void)
1538877df94SMatthew Dillon{
1548877df94SMatthew Dillon    int ret;
1558877df94SMatthew Dillon    int fd;
1568877df94SMatthew Dillon
1578877df94SMatthew Dillon    fd = open("x.ckpt", O_RDWR|O_CREAT|O_TRUNC, 0666);
1588877df94SMatthew Dillon    if (fd < 0) {
159c1de475dSHiten Pandya	printf("unable to create checkpoint file: %s\en",
160c1de475dSHiten Pandya		strerror(errno));
1618877df94SMatthew Dillon	return;
1628877df94SMatthew Dillon    }
1638877df94SMatthew Dillon
1648877df94SMatthew Dillon    ret = sys_checkpoint(CKPT_FREEZE, fd, -1, -1);
1658877df94SMatthew Dillon    if (ret < 0) {
166c1de475dSHiten Pandya	printf("unable to checkpoint: %s\en",
167c1de475dSHiten Pandya		strerror(errno));
1688877df94SMatthew Dillon    } else if (ret == 0) {
1698877df94SMatthew Dillon	printf("checkpoint successful, continuing\en");
1708877df94SMatthew Dillon    } else if (ret == 1) {
1718877df94SMatthew Dillon	printf("resuming from checkpoint.\en");
1728877df94SMatthew Dillon    } else {
1738877df94SMatthew Dillon	printf("unknown return value %d from sys_checkpoint\en", ret);
1748877df94SMatthew Dillon	exit(1);
1758877df94SMatthew Dillon    }
1768877df94SMatthew Dillon    /* note that the file descriptor is still valid on a resume */
1778877df94SMatthew Dillon    close(fd);
1788877df94SMatthew Dillon}
1798877df94SMatthew Dillon.Ed
1800b84df5cSSascha Wildner.Sh ERRORS
1810b84df5cSSascha Wildner.Bl -tag -width Er
1820b84df5cSSascha Wildner.It Bq Er EBADF
1830b84df5cSSascha WildnerThe given
1840b84df5cSSascha Wildner.Fa fd
185fb49adadSSascha Wildneris not a valid regular file, socket descriptor, or pipe.
186fb49adadSSascha WildnerNote that not
1870b84df5cSSascha Wildnerall systems necessarily support checkpointing to sockets and pipes.
1880b84df5cSSascha Wildner.It Bq Er EPERM
1890b84df5cSSascha WildnerThe caller does not have permission to issue the checkpoint command.
1900b84df5cSSascha WildnerCheckpointing may be restricted or disabled using sysctls.
1910b84df5cSSascha Wildner.It Bq Er EIO
1920b84df5cSSascha WildnerAn I/O error occurred while reading from the file system.
1930b84df5cSSascha Wildner.It Bq Er EINVAL
1940b84df5cSSascha WildnerAn invalid parameter was specified.
1950b84df5cSSascha Wildner.El
1960b84df5cSSascha Wildner.Sh CHECKPOINT FEATURES
1970b84df5cSSascha WildnerThe system checkpointing code will save the process register state (including
1980b84df5cSSascha Wildnerfloating point registers), signal state, file descriptors representing
1990b84df5cSSascha Wildnerregular files or directories (anything that can be converted into a file
2000b84df5cSSascha Wildnerhandle for storage), and both shared and private memory mappings.
2010b84df5cSSascha WildnerPrivate, writable mappings are copied to the checkpoint file while shared
2020b84df5cSSascha Wildnermappings and stored by referencing the file handle and offset.
2030b84df5cSSascha WildnerNote that the system checkpointing code does not retain references to
2040b84df5cSSascha Wildnerdeleted files, so mappings and open descriptors of deleted files
2050b84df5cSSascha Wildnercannot be restored.
2060b84df5cSSascha WildnerUnpredictable operation will occur if a checkpoint-unaware program
2070b84df5cSSascha Wildneris restored and some of the underlying files mapped by the program
2080b84df5cSSascha Wildnerhave changed.
2090b84df5cSSascha Wildner.Pp
2100b84df5cSSascha WildnerThe system checkpointing code is not able to retain the process pid, process
211fb49adadSSascha Wildnergroup, user/group creds, or descriptors 0, 1, and 2.
212fb49adadSSascha WildnerThese will be inherited from whomever restores the checkpoint.
2130b84df5cSSascha Wildner.Pp
2140b84df5cSSascha WildnerWhen a checkpointed program is restored modified private mappings will
2150b84df5cSSascha Wildnerbe mapped from the checkpoint file itself, but major portions of the
2160b84df5cSSascha Wildneroriginal program binary will be mapped from the original program binary.
2170b84df5cSSascha WildnerIf the resumed program is checkpointed again the system will automatically
2180b84df5cSSascha Wildnercopy any mappings from the original checkpoint file to the new one, since
2190b84df5cSSascha Wildnerthe original is likely being replaced.
2200b84df5cSSascha WildnerThe caller must not truncate the existing checkpoint file when creating
2210b84df5cSSascha Wildnera new one or specify the existing file's file descriptor as the new
2220b84df5cSSascha Wildnerone as this will destroy the data that the checkpoint operation needs
223fb49adadSSascha Wildnerto copy to the new file.
224fb49adadSSascha WildnerIt is best to checkpoint to a new file and then rename-over the old, or to
225fb49adadSSascha Wildner.Xr remove 3
226fb49adadSSascha Wildnerthe old file before creating the new
2279caa806aSMatthew Dillonone so it remains valid as long as the program continues to run.
2289caa806aSMatthew Dillon.Pp
229fb49adadSSascha WildnerThreaded programs cannot currently be checkpointed.
230fb49adadSSascha WildnerThe program must be
2319caa806aSMatthew Dillonreduced to a single thread before it can be safely checkpointed.
2329caa806aSMatthew Dillon.Pp
233fb49adadSSascha Wildner.Dv MAP_VPAGETABLE
234fb49adadSSascha Wildnermappings cannot currently be checkpointed.
235fb49adadSSascha WildnerA program must restore such mappings manually on resumption.
236fb49adadSSascha WildnerOnly regular file and
237fb49adadSSascha Wildneranonymous memory mappings are checkpointed and restored.
238fb49adadSSascha WildnerDevice and other special mappings are not.
239fb49adadSSascha WildnerOnly regular file descriptors are checkpointed and restored.
240fb49adadSSascha WildnerDevices, pipes, sockets, and other special descriptors are not.
241fb49adadSSascha WildnerMemory wiring states are not checkpointed or restored.
242fb49adadSSascha Wildner.Xr madvise 2
243fb49adadSSascha Wildnerstates are not checkpointed or restored.
244fb49adadSSascha WildnerBasic mapping permissions are checkpointed and restored.
2450b84df5cSSascha Wildner.Sh SECURITY
2460b84df5cSSascha WildnerThe sysctl
247fb49adadSSascha Wildner.Va kern.ckptgroup
2480b84df5cSSascha Wildnercontrols which group can use system checkpointing.
2490b84df5cSSascha WildnerBy default, only users in the
2500b84df5cSSascha Wildner.Ql wheel
2510b84df5cSSascha Wildnergroup are allowed to checkpoint and restore processes.
2520b84df5cSSascha WildnerTo allow users in any group to have this capability (risky), set sysctl
253fb49adadSSascha Wildner.Va kern.ckptgroup
2540b84df5cSSascha Wildnerto -1.
2550b84df5cSSascha Wildner.Sh SIGNALS
2560b84df5cSSascha WildnerTwo signals are associated with checkpointing.
2570b84df5cSSascha Wildner.Dv SIGCKPT
258fb49adadSSascha Wildneris delivered via the tty ckpt character, usually control-E.
259fb49adadSSascha WildnerIts default action is to checkpoint a program and continue running it.
260fb49adadSSascha WildnerThe
2610b84df5cSSascha Wildner.Dv SIGCKPTEXIT
2620b84df5cSSascha Wildnersignal can only be delivered by
2630b84df5cSSascha Wildner.Xr kill 2 .
2640b84df5cSSascha WildnerIts default action is to checkpoint a program and then exit.
2650b84df5cSSascha Wildner.Dv SIGCKPTEXIT
266fb49adadSSascha Wildnermight not be implemented by the system.
267fb49adadSSascha WildnerBoth signals are defined to
2680b84df5cSSascha Wildnerbe greater or equal to signal 32 and cannot be manipulated using legacy
2690b84df5cSSascha Wildnermasking functions.
2700b84df5cSSascha Wildner.Pp
2710b84df5cSSascha WildnerIf a program overrides the default action for a checkpoint signal the
272fb49adadSSascha Wildnersystem will not undertake any action of its own.
273fb49adadSSascha WildnerThe program may issue
2740b84df5cSSascha Wildnerthe checkpoint command from the signal handler itself or simply set a
275fb49adadSSascha Wildnerreminder for later action.
276fb49adadSSascha WildnerIt is usually safest to set a reminder and
2770b84df5cSSascha Wildnerdo the actual checkpointing from your main loop.
2788877df94SMatthew Dillon.Sh SEE ALSO
279c1de475dSHiten Pandya.Xr checkpt 1 ,
280c1de475dSHiten Pandya.Xr signal 3
2818877df94SMatthew Dillon.Sh HISTORY
2828877df94SMatthew DillonThe
2838877df94SMatthew Dillon.Fn sys_checkpoint
284fb49adadSSascha Wildnerfunction call appeared in
285a3220ac5SSascha Wildner.Dx 1.1 .
286